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