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