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