2 * Hierarchical Budget Worst-case Fair Weighted Fair Queueing
3 * (B-WF2Q+): hierarchical scheduling algorithm by which the BFQ I/O
4 * scheduler schedules generic entities. The latter can represent
5 * either single bfq queues (associated with processes) or groups of
6 * bfq queues (associated with cgroups).
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of the
11 * License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 #include "bfq-iosched.h"
21 * bfq_gt - compare two timestamps.
25 * Return @a > @b, dealing with wrapping correctly.
27 static int bfq_gt(u64 a
, u64 b
)
29 return (s64
)(a
- b
) > 0;
32 static struct bfq_entity
*bfq_root_active_entity(struct rb_root
*tree
)
34 struct rb_node
*node
= tree
->rb_node
;
36 return rb_entry(node
, struct bfq_entity
, rb_node
);
39 static unsigned int bfq_class_idx(struct bfq_entity
*entity
)
41 struct bfq_queue
*bfqq
= bfq_entity_to_bfqq(entity
);
43 return bfqq
? bfqq
->ioprio_class
- 1 :
44 BFQ_DEFAULT_GRP_CLASS
- 1;
47 static struct bfq_entity
*bfq_lookup_next_entity(struct bfq_sched_data
*sd
);
49 static bool bfq_update_parent_budget(struct bfq_entity
*next_in_service
);
52 * bfq_update_next_in_service - update sd->next_in_service
53 * @sd: sched_data for which to perform the update.
54 * @new_entity: if not NULL, pointer to the entity whose activation,
55 * requeueing or repositionig triggered the invocation of
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.
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
74 static bool bfq_update_next_in_service(struct bfq_sched_data
*sd
,
75 struct bfq_entity
*new_entity
)
77 struct bfq_entity
*next_in_service
= sd
->next_in_service
;
78 bool parent_sched_may_change
= false;
81 * If this update is triggered by the activation, requeueing
82 * or repositiong of an entity that does not coincide with
83 * sd->next_in_service, then a full lookup in the active tree
84 * can be avoided. In fact, it is enough to check whether the
85 * just-modified entity has a higher priority than
86 * sd->next_in_service, or, even if it has the same priority
87 * as sd->next_in_service, is eligible and has a lower virtual
88 * finish time than sd->next_in_service. If this compound
89 * condition holds, then the new entity becomes the new
90 * next_in_service. Otherwise no change is needed.
92 if (new_entity
&& new_entity
!= sd
->next_in_service
) {
94 * Flag used to decide whether to replace
95 * sd->next_in_service with new_entity. Tentatively
96 * set to true, and left as true if
97 * sd->next_in_service is NULL.
99 bool replace_next
= true;
102 * If there is already a next_in_service candidate
103 * entity, then compare class priorities or timestamps
104 * to decide whether to replace sd->service_tree with
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
;
114 * For efficiency, evaluate the most likely
115 * sub-condition first.
118 (new_entity_class_idx
==
119 bfq_class_idx(next_in_service
)
121 !bfq_gt(new_entity
->start
, st
->vtime
)
123 bfq_gt(next_in_service
->finish
,
126 new_entity_class_idx
<
127 bfq_class_idx(next_in_service
);
131 next_in_service
= new_entity
;
132 } else /* invoked because of a deactivation: lookup needed */
133 next_in_service
= bfq_lookup_next_entity(sd
);
135 if (next_in_service
) {
136 parent_sched_may_change
= !sd
->next_in_service
||
137 bfq_update_parent_budget(next_in_service
);
140 sd
->next_in_service
= next_in_service
;
142 if (!next_in_service
)
143 return parent_sched_may_change
;
145 return parent_sched_may_change
;
148 #ifdef CONFIG_BFQ_GROUP_IOSCHED
150 struct bfq_group
*bfq_bfqq_to_bfqg(struct bfq_queue
*bfqq
)
152 struct bfq_entity
*group_entity
= bfqq
->entity
.parent
;
155 group_entity
= &bfqq
->bfqd
->root_group
->entity
;
157 return container_of(group_entity
, struct bfq_group
, entity
);
161 * Returns true if this budget changes may let next_in_service->parent
162 * become the next_in_service entity for its parent entity.
164 static bool bfq_update_parent_budget(struct bfq_entity
*next_in_service
)
166 struct bfq_entity
*bfqg_entity
;
167 struct bfq_group
*bfqg
;
168 struct bfq_sched_data
*group_sd
;
171 group_sd
= next_in_service
->sched_data
;
173 bfqg
= container_of(group_sd
, struct bfq_group
, sched_data
);
175 * bfq_group's my_entity field is not NULL only if the group
176 * is not the root group. We must not touch the root entity
177 * as it must never become an in-service entity.
179 bfqg_entity
= bfqg
->my_entity
;
181 if (bfqg_entity
->budget
> next_in_service
->budget
)
183 bfqg_entity
->budget
= next_in_service
->budget
;
190 * This function tells whether entity stops being a candidate for next
191 * service, according to the following logic.
193 * This function is invoked for an entity that is about to be set in
194 * service. If such an entity is a queue, then the entity is no longer
195 * a candidate for next service (i.e, a candidate entity to serve
196 * after the in-service entity is expired). The function then returns
199 * In contrast, the entity could stil be a candidate for next service
200 * if it is not a queue, and has more than one child. In fact, even if
201 * one of its children is about to be set in service, other children
202 * may still be the next to serve. As a consequence, a non-queue
203 * entity is not a candidate for next-service only if it has only one
204 * child. And only if this condition holds, then the function returns
205 * true for a non-queue entity.
207 static bool bfq_no_longer_next_in_service(struct bfq_entity
*entity
)
209 struct bfq_group
*bfqg
;
211 if (bfq_entity_to_bfqq(entity
))
214 bfqg
= container_of(entity
, struct bfq_group
, entity
);
216 if (bfqg
->active_entities
== 1)
222 #else /* CONFIG_BFQ_GROUP_IOSCHED */
224 struct bfq_group
*bfq_bfqq_to_bfqg(struct bfq_queue
*bfqq
)
226 return bfqq
->bfqd
->root_group
;
229 static bool bfq_update_parent_budget(struct bfq_entity
*next_in_service
)
234 static bool bfq_no_longer_next_in_service(struct bfq_entity
*entity
)
239 #endif /* CONFIG_BFQ_GROUP_IOSCHED */
242 * Shift for timestamp calculations. This actually limits the maximum
243 * service allowed in one timestamp delta (small shift values increase it),
244 * the maximum total weight that can be used for the queues in the system
245 * (big shift values increase it), and the period of virtual time
248 #define WFQ_SERVICE_SHIFT 22
250 struct bfq_queue
*bfq_entity_to_bfqq(struct bfq_entity
*entity
)
252 struct bfq_queue
*bfqq
= NULL
;
254 if (!entity
->my_sched_data
)
255 bfqq
= container_of(entity
, struct bfq_queue
, entity
);
262 * bfq_delta - map service into the virtual time domain.
263 * @service: amount of service.
264 * @weight: scale factor (weight of an entity or weight sum).
266 static u64
bfq_delta(unsigned long service
, unsigned long weight
)
268 u64 d
= (u64
)service
<< WFQ_SERVICE_SHIFT
;
275 * bfq_calc_finish - assign the finish time to an entity.
276 * @entity: the entity to act upon.
277 * @service: the service to be charged to the entity.
279 static void bfq_calc_finish(struct bfq_entity
*entity
, unsigned long service
)
281 struct bfq_queue
*bfqq
= bfq_entity_to_bfqq(entity
);
283 entity
->finish
= entity
->start
+
284 bfq_delta(service
, entity
->weight
);
287 bfq_log_bfqq(bfqq
->bfqd
, bfqq
,
288 "calc_finish: serv %lu, w %d",
289 service
, entity
->weight
);
290 bfq_log_bfqq(bfqq
->bfqd
, bfqq
,
291 "calc_finish: start %llu, finish %llu, delta %llu",
292 entity
->start
, entity
->finish
,
293 bfq_delta(service
, entity
->weight
));
298 * bfq_entity_of - get an entity from a node.
299 * @node: the node field of the entity.
301 * Convert a node pointer to the relative entity. This is used only
302 * to simplify the logic of some functions and not as the generic
303 * conversion mechanism because, e.g., in the tree walking functions,
304 * the check for a %NULL value would be redundant.
306 struct bfq_entity
*bfq_entity_of(struct rb_node
*node
)
308 struct bfq_entity
*entity
= NULL
;
311 entity
= rb_entry(node
, struct bfq_entity
, rb_node
);
317 * bfq_extract - remove an entity from a tree.
318 * @root: the tree root.
319 * @entity: the entity to remove.
321 static void bfq_extract(struct rb_root
*root
, struct bfq_entity
*entity
)
324 rb_erase(&entity
->rb_node
, root
);
328 * bfq_idle_extract - extract an entity from the idle tree.
329 * @st: the service tree of the owning @entity.
330 * @entity: the entity being removed.
332 static void bfq_idle_extract(struct bfq_service_tree
*st
,
333 struct bfq_entity
*entity
)
335 struct bfq_queue
*bfqq
= bfq_entity_to_bfqq(entity
);
336 struct rb_node
*next
;
338 if (entity
== st
->first_idle
) {
339 next
= rb_next(&entity
->rb_node
);
340 st
->first_idle
= bfq_entity_of(next
);
343 if (entity
== st
->last_idle
) {
344 next
= rb_prev(&entity
->rb_node
);
345 st
->last_idle
= bfq_entity_of(next
);
348 bfq_extract(&st
->idle
, entity
);
351 list_del(&bfqq
->bfqq_list
);
355 * bfq_insert - generic tree insertion.
357 * @entity: entity to insert.
359 * This is used for the idle and the active tree, since they are both
360 * ordered by finish time.
362 static void bfq_insert(struct rb_root
*root
, struct bfq_entity
*entity
)
364 struct bfq_entity
*entry
;
365 struct rb_node
**node
= &root
->rb_node
;
366 struct rb_node
*parent
= NULL
;
370 entry
= rb_entry(parent
, struct bfq_entity
, rb_node
);
372 if (bfq_gt(entry
->finish
, entity
->finish
))
373 node
= &parent
->rb_left
;
375 node
= &parent
->rb_right
;
378 rb_link_node(&entity
->rb_node
, parent
, node
);
379 rb_insert_color(&entity
->rb_node
, root
);
385 * bfq_update_min - update the min_start field of a entity.
386 * @entity: the entity to update.
387 * @node: one of its children.
389 * This function is called when @entity may store an invalid value for
390 * min_start due to updates to the active tree. The function assumes
391 * that the subtree rooted at @node (which may be its left or its right
392 * child) has a valid min_start value.
394 static void bfq_update_min(struct bfq_entity
*entity
, struct rb_node
*node
)
396 struct bfq_entity
*child
;
399 child
= rb_entry(node
, struct bfq_entity
, rb_node
);
400 if (bfq_gt(entity
->min_start
, child
->min_start
))
401 entity
->min_start
= child
->min_start
;
406 * bfq_update_active_node - recalculate min_start.
407 * @node: the node to update.
409 * @node may have changed position or one of its children may have moved,
410 * this function updates its min_start value. The left and right subtrees
411 * are assumed to hold a correct min_start value.
413 static void bfq_update_active_node(struct rb_node
*node
)
415 struct bfq_entity
*entity
= rb_entry(node
, struct bfq_entity
, rb_node
);
417 entity
->min_start
= entity
->start
;
418 bfq_update_min(entity
, node
->rb_right
);
419 bfq_update_min(entity
, node
->rb_left
);
423 * bfq_update_active_tree - update min_start for the whole active tree.
424 * @node: the starting node.
426 * @node must be the deepest modified node after an update. This function
427 * updates its min_start using the values held by its children, assuming
428 * that they did not change, and then updates all the nodes that may have
429 * changed in the path to the root. The only nodes that may have changed
430 * are the ones in the path or their siblings.
432 static void bfq_update_active_tree(struct rb_node
*node
)
434 struct rb_node
*parent
;
437 bfq_update_active_node(node
);
439 parent
= rb_parent(node
);
443 if (node
== parent
->rb_left
&& parent
->rb_right
)
444 bfq_update_active_node(parent
->rb_right
);
445 else if (parent
->rb_left
)
446 bfq_update_active_node(parent
->rb_left
);
453 * bfq_active_insert - insert an entity in the active tree of its
455 * @st: the service tree of the entity.
456 * @entity: the entity being inserted.
458 * The active tree is ordered by finish time, but an extra key is kept
459 * per each node, containing the minimum value for the start times of
460 * its children (and the node itself), so it's possible to search for
461 * the eligible node with the lowest finish time in logarithmic time.
463 static void bfq_active_insert(struct bfq_service_tree
*st
,
464 struct bfq_entity
*entity
)
466 struct bfq_queue
*bfqq
= bfq_entity_to_bfqq(entity
);
467 struct rb_node
*node
= &entity
->rb_node
;
468 #ifdef CONFIG_BFQ_GROUP_IOSCHED
469 struct bfq_sched_data
*sd
= NULL
;
470 struct bfq_group
*bfqg
= NULL
;
471 struct bfq_data
*bfqd
= NULL
;
474 bfq_insert(&st
->active
, entity
);
477 node
= node
->rb_left
;
478 else if (node
->rb_right
)
479 node
= node
->rb_right
;
481 bfq_update_active_tree(node
);
483 #ifdef CONFIG_BFQ_GROUP_IOSCHED
484 sd
= entity
->sched_data
;
485 bfqg
= container_of(sd
, struct bfq_group
, sched_data
);
486 bfqd
= (struct bfq_data
*)bfqg
->bfqd
;
489 list_add(&bfqq
->bfqq_list
, &bfqq
->bfqd
->active_list
);
490 #ifdef CONFIG_BFQ_GROUP_IOSCHED
492 bfq_weights_tree_add(bfqd
, entity
, &bfqd
->group_weights_tree
);
494 if (bfqg
!= bfqd
->root_group
)
495 bfqg
->active_entities
++;
500 * bfq_ioprio_to_weight - calc a weight from an ioprio.
501 * @ioprio: the ioprio value to convert.
503 unsigned short bfq_ioprio_to_weight(int ioprio
)
505 return (IOPRIO_BE_NR
- ioprio
) * BFQ_WEIGHT_CONVERSION_COEFF
;
509 * bfq_weight_to_ioprio - calc an ioprio from a weight.
510 * @weight: the weight value to convert.
512 * To preserve as much as possible the old only-ioprio user interface,
513 * 0 is used as an escape ioprio value for weights (numerically) equal or
514 * larger than IOPRIO_BE_NR * BFQ_WEIGHT_CONVERSION_COEFF.
516 static unsigned short bfq_weight_to_ioprio(int weight
)
519 IOPRIO_BE_NR
* BFQ_WEIGHT_CONVERSION_COEFF
- weight
);
522 static void bfq_get_entity(struct bfq_entity
*entity
)
524 struct bfq_queue
*bfqq
= bfq_entity_to_bfqq(entity
);
528 bfq_log_bfqq(bfqq
->bfqd
, bfqq
, "get_entity: %p %d",
534 * bfq_find_deepest - find the deepest node that an extraction can modify.
535 * @node: the node being removed.
537 * Do the first step of an extraction in an rb tree, looking for the
538 * node that will replace @node, and returning the deepest node that
539 * the following modifications to the tree can touch. If @node is the
540 * last node in the tree return %NULL.
542 static struct rb_node
*bfq_find_deepest(struct rb_node
*node
)
544 struct rb_node
*deepest
;
546 if (!node
->rb_right
&& !node
->rb_left
)
547 deepest
= rb_parent(node
);
548 else if (!node
->rb_right
)
549 deepest
= node
->rb_left
;
550 else if (!node
->rb_left
)
551 deepest
= node
->rb_right
;
553 deepest
= rb_next(node
);
554 if (deepest
->rb_right
)
555 deepest
= deepest
->rb_right
;
556 else if (rb_parent(deepest
) != node
)
557 deepest
= rb_parent(deepest
);
564 * bfq_active_extract - remove an entity from the active tree.
565 * @st: the service_tree containing the tree.
566 * @entity: the entity being removed.
568 static void bfq_active_extract(struct bfq_service_tree
*st
,
569 struct bfq_entity
*entity
)
571 struct bfq_queue
*bfqq
= bfq_entity_to_bfqq(entity
);
572 struct rb_node
*node
;
573 #ifdef CONFIG_BFQ_GROUP_IOSCHED
574 struct bfq_sched_data
*sd
= NULL
;
575 struct bfq_group
*bfqg
= NULL
;
576 struct bfq_data
*bfqd
= NULL
;
579 node
= bfq_find_deepest(&entity
->rb_node
);
580 bfq_extract(&st
->active
, entity
);
583 bfq_update_active_tree(node
);
585 #ifdef CONFIG_BFQ_GROUP_IOSCHED
586 sd
= entity
->sched_data
;
587 bfqg
= container_of(sd
, struct bfq_group
, sched_data
);
588 bfqd
= (struct bfq_data
*)bfqg
->bfqd
;
591 list_del(&bfqq
->bfqq_list
);
592 #ifdef CONFIG_BFQ_GROUP_IOSCHED
594 bfq_weights_tree_remove(bfqd
, entity
,
595 &bfqd
->group_weights_tree
);
597 if (bfqg
!= bfqd
->root_group
)
598 bfqg
->active_entities
--;
603 * bfq_idle_insert - insert an entity into the idle tree.
604 * @st: the service tree containing the tree.
605 * @entity: the entity to insert.
607 static void bfq_idle_insert(struct bfq_service_tree
*st
,
608 struct bfq_entity
*entity
)
610 struct bfq_queue
*bfqq
= bfq_entity_to_bfqq(entity
);
611 struct bfq_entity
*first_idle
= st
->first_idle
;
612 struct bfq_entity
*last_idle
= st
->last_idle
;
614 if (!first_idle
|| bfq_gt(first_idle
->finish
, entity
->finish
))
615 st
->first_idle
= entity
;
616 if (!last_idle
|| bfq_gt(entity
->finish
, last_idle
->finish
))
617 st
->last_idle
= entity
;
619 bfq_insert(&st
->idle
, entity
);
622 list_add(&bfqq
->bfqq_list
, &bfqq
->bfqd
->idle_list
);
626 * bfq_forget_entity - do not consider entity any longer for scheduling
627 * @st: the service tree.
628 * @entity: the entity being removed.
629 * @is_in_service: true if entity is currently the in-service entity.
631 * Forget everything about @entity. In addition, if entity represents
632 * a queue, and the latter is not in service, then release the service
633 * reference to the queue (the one taken through bfq_get_entity). In
634 * fact, in this case, there is really no more service reference to
635 * the queue, as the latter is also outside any service tree. If,
636 * instead, the queue is in service, then __bfq_bfqd_reset_in_service
637 * will take care of putting the reference when the queue finally
638 * stops being served.
640 static void bfq_forget_entity(struct bfq_service_tree
*st
,
641 struct bfq_entity
*entity
,
644 struct bfq_queue
*bfqq
= bfq_entity_to_bfqq(entity
);
646 entity
->on_st
= false;
647 st
->wsum
-= entity
->weight
;
648 if (bfqq
&& !is_in_service
)
653 * bfq_put_idle_entity - release the idle tree ref of an entity.
654 * @st: service tree for the entity.
655 * @entity: the entity being released.
657 void bfq_put_idle_entity(struct bfq_service_tree
*st
, struct bfq_entity
*entity
)
659 bfq_idle_extract(st
, entity
);
660 bfq_forget_entity(st
, entity
,
661 entity
== entity
->sched_data
->in_service_entity
);
665 * bfq_forget_idle - update the idle tree if necessary.
666 * @st: the service tree to act upon.
668 * To preserve the global O(log N) complexity we only remove one entry here;
669 * as the idle tree will not grow indefinitely this can be done safely.
671 static void bfq_forget_idle(struct bfq_service_tree
*st
)
673 struct bfq_entity
*first_idle
= st
->first_idle
;
674 struct bfq_entity
*last_idle
= st
->last_idle
;
676 if (RB_EMPTY_ROOT(&st
->active
) && last_idle
&&
677 !bfq_gt(last_idle
->finish
, st
->vtime
)) {
679 * Forget the whole idle tree, increasing the vtime past
680 * the last finish time of idle entities.
682 st
->vtime
= last_idle
->finish
;
685 if (first_idle
&& !bfq_gt(first_idle
->finish
, st
->vtime
))
686 bfq_put_idle_entity(st
, first_idle
);
689 struct bfq_service_tree
*bfq_entity_service_tree(struct bfq_entity
*entity
)
691 struct bfq_sched_data
*sched_data
= entity
->sched_data
;
692 unsigned int idx
= bfq_class_idx(entity
);
694 return sched_data
->service_tree
+ idx
;
698 struct bfq_service_tree
*
699 __bfq_entity_update_weight_prio(struct bfq_service_tree
*old_st
,
700 struct bfq_entity
*entity
)
702 struct bfq_service_tree
*new_st
= old_st
;
704 if (entity
->prio_changed
) {
705 struct bfq_queue
*bfqq
= bfq_entity_to_bfqq(entity
);
706 unsigned int prev_weight
, new_weight
;
707 struct bfq_data
*bfqd
= NULL
;
708 struct rb_root
*root
;
709 #ifdef CONFIG_BFQ_GROUP_IOSCHED
710 struct bfq_sched_data
*sd
;
711 struct bfq_group
*bfqg
;
716 #ifdef CONFIG_BFQ_GROUP_IOSCHED
718 sd
= entity
->my_sched_data
;
719 bfqg
= container_of(sd
, struct bfq_group
, sched_data
);
720 bfqd
= (struct bfq_data
*)bfqg
->bfqd
;
724 old_st
->wsum
-= entity
->weight
;
726 if (entity
->new_weight
!= entity
->orig_weight
) {
727 if (entity
->new_weight
< BFQ_MIN_WEIGHT
||
728 entity
->new_weight
> BFQ_MAX_WEIGHT
) {
729 pr_crit("update_weight_prio: new_weight %d\n",
731 if (entity
->new_weight
< BFQ_MIN_WEIGHT
)
732 entity
->new_weight
= BFQ_MIN_WEIGHT
;
734 entity
->new_weight
= BFQ_MAX_WEIGHT
;
736 entity
->orig_weight
= entity
->new_weight
;
739 bfq_weight_to_ioprio(entity
->orig_weight
);
743 bfqq
->ioprio_class
= bfqq
->new_ioprio_class
;
744 entity
->prio_changed
= 0;
747 * NOTE: here we may be changing the weight too early,
748 * this will cause unfairness. The correct approach
749 * would have required additional complexity to defer
750 * weight changes to the proper time instants (i.e.,
751 * when entity->finish <= old_st->vtime).
753 new_st
= bfq_entity_service_tree(entity
);
755 prev_weight
= entity
->weight
;
756 new_weight
= entity
->orig_weight
*
757 (bfqq
? bfqq
->wr_coeff
: 1);
759 * If the weight of the entity changes, remove the entity
760 * from its old weight counter (if there is a counter
761 * associated with the entity), and add it to the counter
762 * associated with its new weight.
764 if (prev_weight
!= new_weight
) {
765 root
= bfqq
? &bfqd
->queue_weights_tree
:
766 &bfqd
->group_weights_tree
;
767 bfq_weights_tree_remove(bfqd
, entity
, root
);
769 entity
->weight
= new_weight
;
771 * Add the entity to its weights tree only if it is
772 * not associated with a weight-raised queue.
774 if (prev_weight
!= new_weight
&&
775 (bfqq
? bfqq
->wr_coeff
== 1 : 1))
776 /* If we get here, root has been initialized. */
777 bfq_weights_tree_add(bfqd
, entity
, root
);
779 new_st
->wsum
+= entity
->weight
;
781 if (new_st
!= old_st
)
782 entity
->start
= new_st
->vtime
;
789 * bfq_bfqq_served - update the scheduler status after selection for
791 * @bfqq: the queue being served.
792 * @served: bytes to transfer.
794 * NOTE: this can be optimized, as the timestamps of upper level entities
795 * are synchronized every time a new bfqq is selected for service. By now,
796 * we keep it to better check consistency.
798 void bfq_bfqq_served(struct bfq_queue
*bfqq
, int served
)
800 struct bfq_entity
*entity
= &bfqq
->entity
;
801 struct bfq_service_tree
*st
;
803 for_each_entity(entity
) {
804 st
= bfq_entity_service_tree(entity
);
806 entity
->service
+= served
;
808 st
->vtime
+= bfq_delta(served
, st
->wsum
);
811 bfqg_stats_set_start_empty_time(bfqq_group(bfqq
));
812 bfq_log_bfqq(bfqq
->bfqd
, bfqq
, "bfqq_served %d secs", served
);
816 * bfq_bfqq_charge_time - charge an amount of service equivalent to the length
817 * of the time interval during which bfqq has been in
820 * @bfqq: the queue that needs a service update.
821 * @time_ms: the amount of time during which the queue has received service
823 * If a queue does not consume its budget fast enough, then providing
824 * the queue with service fairness may impair throughput, more or less
825 * severely. For this reason, queues that consume their budget slowly
826 * are provided with time fairness instead of service fairness. This
827 * goal is achieved through the BFQ scheduling engine, even if such an
828 * engine works in the service, and not in the time domain. The trick
829 * is charging these queues with an inflated amount of service, equal
830 * to the amount of service that they would have received during their
831 * service slot if they had been fast, i.e., if their requests had
832 * been dispatched at a rate equal to the estimated peak rate.
834 * It is worth noting that time fairness can cause important
835 * distortions in terms of bandwidth distribution, on devices with
836 * internal queueing. The reason is that I/O requests dispatched
837 * during the service slot of a queue may be served after that service
838 * slot is finished, and may have a total processing time loosely
839 * correlated with the duration of the service slot. This is
840 * especially true for short service slots.
842 void bfq_bfqq_charge_time(struct bfq_data
*bfqd
, struct bfq_queue
*bfqq
,
843 unsigned long time_ms
)
845 struct bfq_entity
*entity
= &bfqq
->entity
;
846 int tot_serv_to_charge
= entity
->service
;
847 unsigned int timeout_ms
= jiffies_to_msecs(bfq_timeout
);
849 if (time_ms
> 0 && time_ms
< timeout_ms
)
851 (bfqd
->bfq_max_budget
* time_ms
) / timeout_ms
;
853 if (tot_serv_to_charge
< entity
->service
)
854 tot_serv_to_charge
= entity
->service
;
856 /* Increase budget to avoid inconsistencies */
857 if (tot_serv_to_charge
> entity
->budget
)
858 entity
->budget
= tot_serv_to_charge
;
860 bfq_bfqq_served(bfqq
,
861 max_t(int, 0, tot_serv_to_charge
- entity
->service
));
864 static void bfq_update_fin_time_enqueue(struct bfq_entity
*entity
,
865 struct bfq_service_tree
*st
,
868 struct bfq_queue
*bfqq
= bfq_entity_to_bfqq(entity
);
870 st
= __bfq_entity_update_weight_prio(st
, entity
);
871 bfq_calc_finish(entity
, entity
->budget
);
874 * If some queues enjoy backshifting for a while, then their
875 * (virtual) finish timestamps may happen to become lower and
876 * lower than the system virtual time. In particular, if
877 * these queues often happen to be idle for short time
878 * periods, and during such time periods other queues with
879 * higher timestamps happen to be busy, then the backshifted
880 * timestamps of the former queues can become much lower than
881 * the system virtual time. In fact, to serve the queues with
882 * higher timestamps while the ones with lower timestamps are
883 * idle, the system virtual time may be pushed-up to much
884 * higher values than the finish timestamps of the idle
885 * queues. As a consequence, the finish timestamps of all new
886 * or newly activated queues may end up being much larger than
887 * those of lucky queues with backshifted timestamps. The
888 * latter queues may then monopolize the device for a lot of
889 * time. This would simply break service guarantees.
891 * To reduce this problem, push up a little bit the
892 * backshifted timestamps of the queue associated with this
893 * entity (only a queue can happen to have the backshifted
894 * flag set): just enough to let the finish timestamp of the
895 * queue be equal to the current value of the system virtual
896 * time. This may introduce a little unfairness among queues
897 * with backshifted timestamps, but it does not break
898 * worst-case fairness guarantees.
900 * As a special case, if bfqq is weight-raised, push up
901 * timestamps much less, to keep very low the probability that
902 * this push up causes the backshifted finish timestamps of
903 * weight-raised queues to become higher than the backshifted
904 * finish timestamps of non weight-raised queues.
906 if (backshifted
&& bfq_gt(st
->vtime
, entity
->finish
)) {
907 unsigned long delta
= st
->vtime
- entity
->finish
;
910 delta
/= bfqq
->wr_coeff
;
912 entity
->start
+= delta
;
913 entity
->finish
+= delta
;
916 bfq_active_insert(st
, entity
);
920 * __bfq_activate_entity - handle activation of entity.
921 * @entity: the entity being activated.
922 * @non_blocking_wait_rq: true if entity was waiting for a request
924 * Called for a 'true' activation, i.e., if entity is not active and
925 * one of its children receives a new request.
927 * Basically, this function updates the timestamps of entity and
928 * inserts entity into its active tree, ater possible extracting it
929 * from its idle tree.
931 static void __bfq_activate_entity(struct bfq_entity
*entity
,
932 bool non_blocking_wait_rq
)
934 struct bfq_service_tree
*st
= bfq_entity_service_tree(entity
);
935 bool backshifted
= false;
936 unsigned long long min_vstart
;
938 /* See comments on bfq_fqq_update_budg_for_activation */
939 if (non_blocking_wait_rq
&& bfq_gt(st
->vtime
, entity
->finish
)) {
941 min_vstart
= entity
->finish
;
943 min_vstart
= st
->vtime
;
945 if (entity
->tree
== &st
->idle
) {
947 * Must be on the idle tree, bfq_idle_extract() will
950 bfq_idle_extract(st
, entity
);
951 entity
->start
= bfq_gt(min_vstart
, entity
->finish
) ?
952 min_vstart
: entity
->finish
;
955 * The finish time of the entity may be invalid, and
956 * it is in the past for sure, otherwise the queue
957 * would have been on the idle tree.
959 entity
->start
= min_vstart
;
960 st
->wsum
+= entity
->weight
;
962 * entity is about to be inserted into a service tree,
963 * and then set in service: get a reference to make
964 * sure entity does not disappear until it is no
965 * longer in service or scheduled for service.
967 bfq_get_entity(entity
);
969 entity
->on_st
= true;
972 bfq_update_fin_time_enqueue(entity
, st
, backshifted
);
976 * __bfq_requeue_entity - handle requeueing or repositioning of an entity.
977 * @entity: the entity being requeued or repositioned.
979 * Requeueing is needed if this entity stops being served, which
980 * happens if a leaf descendant entity has expired. On the other hand,
981 * repositioning is needed if the next_inservice_entity for the child
982 * entity has changed. See the comments inside the function for
985 * Basically, this function: 1) removes entity from its active tree if
986 * present there, 2) updates the timestamps of entity and 3) inserts
987 * entity back into its active tree (in the new, right position for
988 * the new values of the timestamps).
990 static void __bfq_requeue_entity(struct bfq_entity
*entity
)
992 struct bfq_sched_data
*sd
= entity
->sched_data
;
993 struct bfq_service_tree
*st
= bfq_entity_service_tree(entity
);
995 if (entity
== sd
->in_service_entity
) {
997 * We are requeueing the current in-service entity,
998 * which may have to be done for one of the following
1000 * - entity represents the in-service queue, and the
1001 * in-service queue is being requeued after an
1003 * - entity represents a group, and its budget has
1004 * changed because one of its child entities has
1005 * just been either activated or requeued for some
1006 * reason; the timestamps of the entity need then to
1007 * be updated, and the entity needs to be enqueued
1008 * or repositioned accordingly.
1010 * In particular, before requeueing, the start time of
1011 * the entity must be moved forward to account for the
1012 * service that the entity has received while in
1013 * service. This is done by the next instructions. The
1014 * finish time will then be updated according to this
1015 * new value of the start time, and to the budget of
1018 bfq_calc_finish(entity
, entity
->service
);
1019 entity
->start
= entity
->finish
;
1021 * In addition, if the entity had more than one child
1022 * when set in service, then was not extracted from
1023 * the active tree. This implies that the position of
1024 * the entity in the active tree may need to be
1025 * changed now, because we have just updated the start
1026 * time of the entity, and we will update its finish
1027 * time in a moment (the requeueing is then, more
1028 * precisely, a repositioning in this case). To
1029 * implement this repositioning, we: 1) dequeue the
1030 * entity here, 2) update the finish time and
1031 * requeue the entity according to the new
1035 bfq_active_extract(st
, entity
);
1036 } else { /* The entity is already active, and not in service */
1038 * In this case, this function gets called only if the
1039 * next_in_service entity below this entity has
1040 * changed, and this change has caused the budget of
1041 * this entity to change, which, finally implies that
1042 * the finish time of this entity must be
1043 * updated. Such an update may cause the scheduling,
1044 * i.e., the position in the active tree, of this
1045 * entity to change. We handle this change by: 1)
1046 * dequeueing the entity here, 2) updating the finish
1047 * time and requeueing the entity according to the new
1048 * timestamps below. This is the same approach as the
1049 * non-extracted-entity sub-case above.
1051 bfq_active_extract(st
, entity
);
1054 bfq_update_fin_time_enqueue(entity
, st
, false);
1057 static void __bfq_activate_requeue_entity(struct bfq_entity
*entity
,
1058 struct bfq_sched_data
*sd
,
1059 bool non_blocking_wait_rq
)
1061 struct bfq_service_tree
*st
= bfq_entity_service_tree(entity
);
1063 if (sd
->in_service_entity
== entity
|| entity
->tree
== &st
->active
)
1065 * in service or already queued on the active tree,
1066 * requeue or reposition
1068 __bfq_requeue_entity(entity
);
1071 * Not in service and not queued on its active tree:
1072 * the activity is idle and this is a true activation.
1074 __bfq_activate_entity(entity
, non_blocking_wait_rq
);
1079 * bfq_activate_entity - activate or requeue an entity representing a bfq_queue,
1080 * and activate, requeue or reposition all ancestors
1081 * for which such an update becomes necessary.
1082 * @entity: the entity to activate.
1083 * @non_blocking_wait_rq: true if this entity was waiting for a request
1084 * @requeue: true if this is a requeue, which implies that bfqq is
1085 * being expired; thus ALL its ancestors stop being served and must
1086 * therefore be requeued
1088 static void bfq_activate_requeue_entity(struct bfq_entity
*entity
,
1089 bool non_blocking_wait_rq
,
1092 struct bfq_sched_data
*sd
;
1094 for_each_entity(entity
) {
1095 sd
= entity
->sched_data
;
1096 __bfq_activate_requeue_entity(entity
, sd
, non_blocking_wait_rq
);
1098 if (!bfq_update_next_in_service(sd
, entity
) && !requeue
)
1104 * __bfq_deactivate_entity - deactivate an entity from its service tree.
1105 * @entity: the entity to deactivate.
1106 * @ins_into_idle_tree: if false, the entity will not be put into the
1109 * Deactivates an entity, independently from its previous state. Must
1110 * be invoked only if entity is on a service tree. Extracts the entity
1111 * from that tree, and if necessary and allowed, puts it on the idle
1114 bool __bfq_deactivate_entity(struct bfq_entity
*entity
, bool ins_into_idle_tree
)
1116 struct bfq_sched_data
*sd
= entity
->sched_data
;
1117 struct bfq_service_tree
*st
;
1120 if (!entity
->on_st
) /* entity never activated, or already inactive */
1124 * If we get here, then entity is active, which implies that
1125 * bfq_group_set_parent has already been invoked for the group
1126 * represented by entity. Therefore, the field
1127 * entity->sched_data has been set, and we can safely use it.
1129 st
= bfq_entity_service_tree(entity
);
1130 is_in_service
= entity
== sd
->in_service_entity
;
1133 bfq_calc_finish(entity
, entity
->service
);
1135 if (entity
->tree
== &st
->active
)
1136 bfq_active_extract(st
, entity
);
1137 else if (!is_in_service
&& entity
->tree
== &st
->idle
)
1138 bfq_idle_extract(st
, entity
);
1140 if (!ins_into_idle_tree
|| !bfq_gt(entity
->finish
, st
->vtime
))
1141 bfq_forget_entity(st
, entity
, is_in_service
);
1143 bfq_idle_insert(st
, entity
);
1149 * bfq_deactivate_entity - deactivate an entity representing a bfq_queue.
1150 * @entity: the entity to deactivate.
1151 * @ins_into_idle_tree: true if the entity can be put on the idle tree
1153 static void bfq_deactivate_entity(struct bfq_entity
*entity
,
1154 bool ins_into_idle_tree
,
1157 struct bfq_sched_data
*sd
;
1158 struct bfq_entity
*parent
= NULL
;
1160 for_each_entity_safe(entity
, parent
) {
1161 sd
= entity
->sched_data
;
1163 if (!__bfq_deactivate_entity(entity
, ins_into_idle_tree
)) {
1165 * entity is not in any tree any more, so
1166 * this deactivation is a no-op, and there is
1167 * nothing to change for upper-level entities
1168 * (in case of expiration, this can never
1174 if (sd
->next_in_service
== entity
)
1176 * entity was the next_in_service entity,
1177 * then, since entity has just been
1178 * deactivated, a new one must be found.
1180 bfq_update_next_in_service(sd
, NULL
);
1182 if (sd
->next_in_service
)
1184 * The parent entity is still backlogged,
1185 * because next_in_service is not NULL. So, no
1186 * further upwards deactivation must be
1187 * performed. Yet, next_in_service has
1188 * changed. Then the schedule does need to be
1194 * If we get here, then the parent is no more
1195 * backlogged and we need to propagate the
1196 * deactivation upwards. Thus let the loop go on.
1200 * Also let parent be queued into the idle tree on
1201 * deactivation, to preserve service guarantees, and
1202 * assuming that who invoked this function does not
1203 * need parent entities too to be removed completely.
1205 ins_into_idle_tree
= true;
1209 * If the deactivation loop is fully executed, then there are
1210 * no more entities to touch and next loop is not executed at
1211 * all. Otherwise, requeue remaining entities if they are
1212 * about to stop receiving service, or reposition them if this
1216 for_each_entity(entity
) {
1218 * Invoke __bfq_requeue_entity on entity, even if
1219 * already active, to requeue/reposition it in the
1220 * active tree (because sd->next_in_service has
1223 __bfq_requeue_entity(entity
);
1225 sd
= entity
->sched_data
;
1226 if (!bfq_update_next_in_service(sd
, entity
) &&
1229 * next_in_service unchanged or not causing
1230 * any change in entity->parent->sd, and no
1231 * requeueing needed for expiration: stop
1239 * bfq_calc_vtime_jump - compute the value to which the vtime should jump,
1240 * if needed, to have at least one entity eligible.
1241 * @st: the service tree to act upon.
1243 * Assumes that st is not empty.
1245 static u64
bfq_calc_vtime_jump(struct bfq_service_tree
*st
)
1247 struct bfq_entity
*root_entity
= bfq_root_active_entity(&st
->active
);
1249 if (bfq_gt(root_entity
->min_start
, st
->vtime
))
1250 return root_entity
->min_start
;
1255 static void bfq_update_vtime(struct bfq_service_tree
*st
, u64 new_value
)
1257 if (new_value
> st
->vtime
) {
1258 st
->vtime
= new_value
;
1259 bfq_forget_idle(st
);
1264 * bfq_first_active_entity - find the eligible entity with
1265 * the smallest finish time
1266 * @st: the service tree to select from.
1267 * @vtime: the system virtual to use as a reference for eligibility
1269 * This function searches the first schedulable entity, starting from the
1270 * root of the tree and going on the left every time on this side there is
1271 * a subtree with at least one eligible (start >= vtime) entity. The path on
1272 * the right is followed only if a) the left subtree contains no eligible
1273 * entities and b) no eligible entity has been found yet.
1275 static struct bfq_entity
*bfq_first_active_entity(struct bfq_service_tree
*st
,
1278 struct bfq_entity
*entry
, *first
= NULL
;
1279 struct rb_node
*node
= st
->active
.rb_node
;
1282 entry
= rb_entry(node
, struct bfq_entity
, rb_node
);
1284 if (!bfq_gt(entry
->start
, vtime
))
1287 if (node
->rb_left
) {
1288 entry
= rb_entry(node
->rb_left
,
1289 struct bfq_entity
, rb_node
);
1290 if (!bfq_gt(entry
->min_start
, vtime
)) {
1291 node
= node
->rb_left
;
1297 node
= node
->rb_right
;
1304 * __bfq_lookup_next_entity - return the first eligible entity in @st.
1305 * @st: the service tree.
1307 * If there is no in-service entity for the sched_data st belongs to,
1308 * then return the entity that will be set in service if:
1309 * 1) the parent entity this st belongs to is set in service;
1310 * 2) no entity belonging to such parent entity undergoes a state change
1311 * that would influence the timestamps of the entity (e.g., becomes idle,
1312 * becomes backlogged, changes its budget, ...).
1314 * In this first case, update the virtual time in @st too (see the
1315 * comments on this update inside the function).
1317 * In constrast, if there is an in-service entity, then return the
1318 * entity that would be set in service if not only the above
1319 * conditions, but also the next one held true: the currently
1320 * in-service entity, on expiration,
1321 * 1) gets a finish time equal to the current one, or
1322 * 2) is not eligible any more, or
1325 static struct bfq_entity
*
1326 __bfq_lookup_next_entity(struct bfq_service_tree
*st
, bool in_service
)
1328 struct bfq_entity
*entity
;
1331 if (RB_EMPTY_ROOT(&st
->active
))
1335 * Get the value of the system virtual time for which at
1336 * least one entity is eligible.
1338 new_vtime
= bfq_calc_vtime_jump(st
);
1341 * If there is no in-service entity for the sched_data this
1342 * active tree belongs to, then push the system virtual time
1343 * up to the value that guarantees that at least one entity is
1344 * eligible. If, instead, there is an in-service entity, then
1345 * do not make any such update, because there is already an
1346 * eligible entity, namely the in-service one (even if the
1347 * entity is not on st, because it was extracted when set in
1351 bfq_update_vtime(st
, new_vtime
);
1353 entity
= bfq_first_active_entity(st
, new_vtime
);
1359 * bfq_lookup_next_entity - return the first eligible entity in @sd.
1360 * @sd: the sched_data.
1362 * This function is invoked when there has been a change in the trees
1363 * for sd, and we need know what is the new next entity after this
1366 static struct bfq_entity
*bfq_lookup_next_entity(struct bfq_sched_data
*sd
)
1368 struct bfq_service_tree
*st
= sd
->service_tree
;
1369 struct bfq_service_tree
*idle_class_st
= st
+ (BFQ_IOPRIO_CLASSES
- 1);
1370 struct bfq_entity
*entity
= NULL
;
1374 * Choose from idle class, if needed to guarantee a minimum
1375 * bandwidth to this class (and if there is some active entity
1376 * in idle class). This should also mitigate
1377 * priority-inversion problems in case a low priority task is
1378 * holding file system resources.
1380 if (time_is_before_jiffies(sd
->bfq_class_idle_last_service
+
1381 BFQ_CL_IDLE_TIMEOUT
)) {
1382 if (!RB_EMPTY_ROOT(&idle_class_st
->active
))
1383 class_idx
= BFQ_IOPRIO_CLASSES
- 1;
1384 /* About to be served if backlogged, or not yet backlogged */
1385 sd
->bfq_class_idle_last_service
= jiffies
;
1389 * Find the next entity to serve for the highest-priority
1390 * class, unless the idle class needs to be served.
1392 for (; class_idx
< BFQ_IOPRIO_CLASSES
; class_idx
++) {
1393 entity
= __bfq_lookup_next_entity(st
+ class_idx
,
1394 sd
->in_service_entity
);
1406 bool next_queue_may_preempt(struct bfq_data
*bfqd
)
1408 struct bfq_sched_data
*sd
= &bfqd
->root_group
->sched_data
;
1410 return sd
->next_in_service
!= sd
->in_service_entity
;
1414 * Get next queue for service.
1416 struct bfq_queue
*bfq_get_next_queue(struct bfq_data
*bfqd
)
1418 struct bfq_entity
*entity
= NULL
;
1419 struct bfq_sched_data
*sd
;
1420 struct bfq_queue
*bfqq
;
1422 if (bfqd
->busy_queues
== 0)
1426 * Traverse the path from the root to the leaf entity to
1427 * serve. Set in service all the entities visited along the
1430 sd
= &bfqd
->root_group
->sched_data
;
1431 for (; sd
; sd
= entity
->my_sched_data
) {
1433 * WARNING. We are about to set the in-service entity
1434 * to sd->next_in_service, i.e., to the (cached) value
1435 * returned by bfq_lookup_next_entity(sd) the last
1436 * time it was invoked, i.e., the last time when the
1437 * service order in sd changed as a consequence of the
1438 * activation or deactivation of an entity. In this
1439 * respect, if we execute bfq_lookup_next_entity(sd)
1440 * in this very moment, it may, although with low
1441 * probability, yield a different entity than that
1442 * pointed to by sd->next_in_service. This rare event
1443 * happens in case there was no CLASS_IDLE entity to
1444 * serve for sd when bfq_lookup_next_entity(sd) was
1445 * invoked for the last time, while there is now one
1448 * If the above event happens, then the scheduling of
1449 * such entity in CLASS_IDLE is postponed until the
1450 * service of the sd->next_in_service entity
1451 * finishes. In fact, when the latter is expired,
1452 * bfq_lookup_next_entity(sd) gets called again,
1453 * exactly to update sd->next_in_service.
1456 /* Make next_in_service entity become in_service_entity */
1457 entity
= sd
->next_in_service
;
1458 sd
->in_service_entity
= entity
;
1461 * Reset the accumulator of the amount of service that
1462 * the entity is about to receive.
1464 entity
->service
= 0;
1467 * If entity is no longer a candidate for next
1468 * service, then we extract it from its active tree,
1469 * for the following reason. To further boost the
1470 * throughput in some special case, BFQ needs to know
1471 * which is the next candidate entity to serve, while
1472 * there is already an entity in service. In this
1473 * respect, to make it easy to compute/update the next
1474 * candidate entity to serve after the current
1475 * candidate has been set in service, there is a case
1476 * where it is necessary to extract the current
1477 * candidate from its service tree. Such a case is
1478 * when the entity just set in service cannot be also
1479 * a candidate for next service. Details about when
1480 * this conditions holds are reported in the comments
1481 * on the function bfq_no_longer_next_in_service()
1484 if (bfq_no_longer_next_in_service(entity
))
1485 bfq_active_extract(bfq_entity_service_tree(entity
),
1489 * For the same reason why we may have just extracted
1490 * entity from its active tree, we may need to update
1491 * next_in_service for the sched_data of entity too,
1492 * regardless of whether entity has been extracted.
1493 * In fact, even if entity has not been extracted, a
1494 * descendant entity may get extracted. Such an event
1495 * would cause a change in next_in_service for the
1496 * level of the descendant entity, and thus possibly
1497 * back to upper levels.
1499 * We cannot perform the resulting needed update
1500 * before the end of this loop, because, to know which
1501 * is the correct next-to-serve candidate entity for
1502 * each level, we need first to find the leaf entity
1503 * to set in service. In fact, only after we know
1504 * which is the next-to-serve leaf entity, we can
1505 * discover whether the parent entity of the leaf
1506 * entity becomes the next-to-serve, and so on.
1511 bfqq
= bfq_entity_to_bfqq(entity
);
1514 * We can finally update all next-to-serve entities along the
1515 * path from the leaf entity just set in service to the root.
1517 for_each_entity(entity
) {
1518 struct bfq_sched_data
*sd
= entity
->sched_data
;
1520 if (!bfq_update_next_in_service(sd
, NULL
))
1527 void __bfq_bfqd_reset_in_service(struct bfq_data
*bfqd
)
1529 struct bfq_queue
*in_serv_bfqq
= bfqd
->in_service_queue
;
1530 struct bfq_entity
*in_serv_entity
= &in_serv_bfqq
->entity
;
1531 struct bfq_entity
*entity
= in_serv_entity
;
1533 bfq_clear_bfqq_wait_request(in_serv_bfqq
);
1534 hrtimer_try_to_cancel(&bfqd
->idle_slice_timer
);
1535 bfqd
->in_service_queue
= NULL
;
1538 * When this function is called, all in-service entities have
1539 * been properly deactivated or requeued, so we can safely
1540 * execute the final step: reset in_service_entity along the
1541 * path from entity to the root.
1543 for_each_entity(entity
)
1544 entity
->sched_data
->in_service_entity
= NULL
;
1547 * in_serv_entity is no longer in service, so, if it is in no
1548 * service tree either, then release the service reference to
1549 * the queue it represents (taken with bfq_get_entity).
1551 if (!in_serv_entity
->on_st
)
1552 bfq_put_queue(in_serv_bfqq
);
1555 void bfq_deactivate_bfqq(struct bfq_data
*bfqd
, struct bfq_queue
*bfqq
,
1556 bool ins_into_idle_tree
, bool expiration
)
1558 struct bfq_entity
*entity
= &bfqq
->entity
;
1560 bfq_deactivate_entity(entity
, ins_into_idle_tree
, expiration
);
1563 void bfq_activate_bfqq(struct bfq_data
*bfqd
, struct bfq_queue
*bfqq
)
1565 struct bfq_entity
*entity
= &bfqq
->entity
;
1567 bfq_activate_requeue_entity(entity
, bfq_bfqq_non_blocking_wait_rq(bfqq
),
1569 bfq_clear_bfqq_non_blocking_wait_rq(bfqq
);
1572 void bfq_requeue_bfqq(struct bfq_data
*bfqd
, struct bfq_queue
*bfqq
)
1574 struct bfq_entity
*entity
= &bfqq
->entity
;
1576 bfq_activate_requeue_entity(entity
, false,
1577 bfqq
== bfqd
->in_service_queue
);
1581 * Called when the bfqq no longer has requests pending, remove it from
1582 * the service tree. As a special case, it can be invoked during an
1585 void bfq_del_bfqq_busy(struct bfq_data
*bfqd
, struct bfq_queue
*bfqq
,
1588 bfq_log_bfqq(bfqd
, bfqq
, "del from busy");
1590 bfq_clear_bfqq_busy(bfqq
);
1592 bfqd
->busy_queues
--;
1594 if (!bfqq
->dispatched
)
1595 bfq_weights_tree_remove(bfqd
, &bfqq
->entity
,
1596 &bfqd
->queue_weights_tree
);
1598 if (bfqq
->wr_coeff
> 1)
1599 bfqd
->wr_busy_queues
--;
1601 bfqg_stats_update_dequeue(bfqq_group(bfqq
));
1603 bfq_deactivate_bfqq(bfqd
, bfqq
, true, expiration
);
1607 * Called when an inactive queue receives a new request.
1609 void bfq_add_bfqq_busy(struct bfq_data
*bfqd
, struct bfq_queue
*bfqq
)
1611 bfq_log_bfqq(bfqd
, bfqq
, "add to busy");
1613 bfq_activate_bfqq(bfqd
, bfqq
);
1615 bfq_mark_bfqq_busy(bfqq
);
1616 bfqd
->busy_queues
++;
1618 if (!bfqq
->dispatched
)
1619 if (bfqq
->wr_coeff
== 1)
1620 bfq_weights_tree_add(bfqd
, &bfqq
->entity
,
1621 &bfqd
->queue_weights_tree
);
1623 if (bfqq
->wr_coeff
> 1)
1624 bfqd
->wr_busy_queues
++;