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