Merge tag 'v3.3.7' into 3.3/master
[zen-stable.git] / block / bfq-sched.c
blob3e20c4f3eef0d6c4ee508cf66fa87cc1d2cac390
1 /*
2 * BFQ: Hierarchical B-WF2Q+ scheduler.
4 * Based on ideas and code from CFQ:
5 * Copyright (C) 2003 Jens Axboe <axboe@kernel.dk>
7 * Copyright (C) 2008 Fabio Checconi <fabio@gandalf.sssup.it>
8 * Paolo Valente <paolo.valente@unimore.it>
9 */
11 #ifdef CONFIG_CGROUP_BFQIO
12 #define for_each_entity(entity) \
13 for (; entity != NULL; entity = entity->parent)
15 #define for_each_entity_safe(entity, parent) \
16 for (; entity && ({ parent = entity->parent; 1; }); entity = parent)
18 static struct bfq_entity *bfq_lookup_next_entity(struct bfq_sched_data *sd,
19 int extract,
20 struct bfq_data *bfqd);
22 static int bfq_update_next_active(struct bfq_sched_data *sd)
24 struct bfq_group *bfqg;
25 struct bfq_entity *entity, *next_active;
27 if (sd->active_entity != NULL)
28 /* will update/requeue at the end of service */
29 return 0;
32 * NOTE: this can be improved in many ways, such as returning
33 * 1 (and thus propagating upwards the update) only when the
34 * budget changes, or caching the bfqq that will be scheduled
35 * next from this subtree. By now we worry more about
36 * correctness than about performance...
38 next_active = bfq_lookup_next_entity(sd, 0, NULL);
39 sd->next_active = next_active;
41 if (next_active != NULL) {
42 bfqg = container_of(sd, struct bfq_group, sched_data);
43 entity = bfqg->my_entity;
44 if (entity != NULL)
45 entity->budget = next_active->budget;
48 return 1;
51 static inline void bfq_check_next_active(struct bfq_sched_data *sd,
52 struct bfq_entity *entity)
54 BUG_ON(sd->next_active != entity);
56 #else
57 #define for_each_entity(entity) \
58 for (; entity != NULL; entity = NULL)
60 #define for_each_entity_safe(entity, parent) \
61 for (parent = NULL; entity != NULL; entity = parent)
63 static inline int bfq_update_next_active(struct bfq_sched_data *sd)
65 return 0;
68 static inline void bfq_check_next_active(struct bfq_sched_data *sd,
69 struct bfq_entity *entity)
72 #endif
75 * Shift for timestamp calculations. This actually limits the maximum
76 * service allowed in one timestamp delta (small shift values increase it),
77 * the maximum total weight that can be used for the queues in the system
78 * (big shift values increase it), and the period of virtual time wraparounds.
80 #define WFQ_SERVICE_SHIFT 22
82 /**
83 * bfq_gt - compare two timestamps.
84 * @a: first ts.
85 * @b: second ts.
87 * Return @a > @b, dealing with wrapping correctly.
89 static inline int bfq_gt(u64 a, u64 b)
91 return (s64)(a - b) > 0;
94 static inline struct bfq_queue *bfq_entity_to_bfqq(struct bfq_entity *entity)
96 struct bfq_queue *bfqq = NULL;
98 BUG_ON(entity == NULL);
100 if (entity->my_sched_data == NULL)
101 bfqq = container_of(entity, struct bfq_queue, entity);
103 return bfqq;
108 * bfq_delta - map service into the virtual time domain.
109 * @service: amount of service.
110 * @weight: scale factor (weight of an entity or weight sum).
112 static inline u64 bfq_delta(unsigned long service,
113 unsigned long weight)
115 u64 d = (u64)service << WFQ_SERVICE_SHIFT;
117 do_div(d, weight);
118 return d;
122 * bfq_calc_finish - assign the finish time to an entity.
123 * @entity: the entity to act upon.
124 * @service: the service to be charged to the entity.
126 static inline void bfq_calc_finish(struct bfq_entity *entity,
127 unsigned long service)
129 struct bfq_queue *bfqq = bfq_entity_to_bfqq(entity);
131 BUG_ON(entity->weight == 0);
133 entity->finish = entity->start +
134 bfq_delta(service, entity->weight);
136 if (bfqq != NULL) {
137 bfq_log_bfqq(bfqq->bfqd, bfqq,
138 "calc_finish: serv %lu, w %d",
139 service, entity->weight);
140 bfq_log_bfqq(bfqq->bfqd, bfqq,
141 "calc_finish: start %llu, finish %llu, delta %llu",
142 entity->start, entity->finish,
143 bfq_delta(service, entity->weight));
148 * bfq_entity_of - get an entity from a node.
149 * @node: the node field of the entity.
151 * Convert a node pointer to the relative entity. This is used only
152 * to simplify the logic of some functions and not as the generic
153 * conversion mechanism because, e.g., in the tree walking functions,
154 * the check for a %NULL value would be redundant.
156 static inline struct bfq_entity *bfq_entity_of(struct rb_node *node)
158 struct bfq_entity *entity = NULL;
160 if (node != NULL)
161 entity = rb_entry(node, struct bfq_entity, rb_node);
163 return entity;
167 * bfq_extract - remove an entity from a tree.
168 * @root: the tree root.
169 * @entity: the entity to remove.
171 static inline void bfq_extract(struct rb_root *root,
172 struct bfq_entity *entity)
174 BUG_ON(entity->tree != root);
176 entity->tree = NULL;
177 rb_erase(&entity->rb_node, root);
181 * bfq_idle_extract - extract an entity from the idle tree.
182 * @st: the service tree of the owning @entity.
183 * @entity: the entity being removed.
185 static void bfq_idle_extract(struct bfq_service_tree *st,
186 struct bfq_entity *entity)
188 struct bfq_queue *bfqq = bfq_entity_to_bfqq(entity);
189 struct rb_node *next;
191 BUG_ON(entity->tree != &st->idle);
193 if (entity == st->first_idle) {
194 next = rb_next(&entity->rb_node);
195 st->first_idle = bfq_entity_of(next);
198 if (entity == st->last_idle) {
199 next = rb_prev(&entity->rb_node);
200 st->last_idle = bfq_entity_of(next);
203 bfq_extract(&st->idle, entity);
205 if (bfqq != NULL)
206 list_del(&bfqq->bfqq_list);
210 * bfq_insert - generic tree insertion.
211 * @root: tree root.
212 * @entity: entity to insert.
214 * This is used for the idle and the active tree, since they are both
215 * ordered by finish time.
217 static void bfq_insert(struct rb_root *root, struct bfq_entity *entity)
219 struct bfq_entity *entry;
220 struct rb_node **node = &root->rb_node;
221 struct rb_node *parent = NULL;
223 BUG_ON(entity->tree != NULL);
225 while (*node != NULL) {
226 parent = *node;
227 entry = rb_entry(parent, struct bfq_entity, rb_node);
229 if (bfq_gt(entry->finish, entity->finish))
230 node = &parent->rb_left;
231 else
232 node = &parent->rb_right;
235 rb_link_node(&entity->rb_node, parent, node);
236 rb_insert_color(&entity->rb_node, root);
238 entity->tree = root;
242 * bfq_update_min - update the min_start field of a entity.
243 * @entity: the entity to update.
244 * @node: one of its children.
246 * This function is called when @entity may store an invalid value for
247 * min_start due to updates to the active tree. The function assumes
248 * that the subtree rooted at @node (which may be its left or its right
249 * child) has a valid min_start value.
251 static inline void bfq_update_min(struct bfq_entity *entity,
252 struct rb_node *node)
254 struct bfq_entity *child;
256 if (node != NULL) {
257 child = rb_entry(node, struct bfq_entity, rb_node);
258 if (bfq_gt(entity->min_start, child->min_start))
259 entity->min_start = child->min_start;
264 * bfq_update_active_node - recalculate min_start.
265 * @node: the node to update.
267 * @node may have changed position or one of its children may have moved,
268 * this function updates its min_start value. The left and right subtrees
269 * are assumed to hold a correct min_start value.
271 static inline void bfq_update_active_node(struct rb_node *node)
273 struct bfq_entity *entity = rb_entry(node, struct bfq_entity, rb_node);
275 entity->min_start = entity->start;
276 bfq_update_min(entity, node->rb_right);
277 bfq_update_min(entity, node->rb_left);
281 * bfq_update_active_tree - update min_start for the whole active tree.
282 * @node: the starting node.
284 * @node must be the deepest modified node after an update. This function
285 * updates its min_start using the values held by its children, assuming
286 * that they did not change, and then updates all the nodes that may have
287 * changed in the path to the root. The only nodes that may have changed
288 * are the ones in the path or their siblings.
290 static void bfq_update_active_tree(struct rb_node *node)
292 struct rb_node *parent;
295 bfq_update_active_node(node);
297 parent = rb_parent(node);
298 if (parent == NULL)
299 return;
301 if (node == parent->rb_left && parent->rb_right != NULL)
302 bfq_update_active_node(parent->rb_right);
303 else if (parent->rb_left != NULL)
304 bfq_update_active_node(parent->rb_left);
306 node = parent;
307 goto up;
311 * bfq_active_insert - insert an entity in the active tree of its group/device.
312 * @st: the service tree of the entity.
313 * @entity: the entity being inserted.
315 * The active tree is ordered by finish time, but an extra key is kept
316 * per each node, containing the minimum value for the start times of
317 * its children (and the node itself), so it's possible to search for
318 * the eligible node with the lowest finish time in logarithmic time.
320 static void bfq_active_insert(struct bfq_service_tree *st,
321 struct bfq_entity *entity)
323 struct bfq_queue *bfqq = bfq_entity_to_bfqq(entity);
324 struct rb_node *node = &entity->rb_node;
326 bfq_insert(&st->active, entity);
328 if (node->rb_left != NULL)
329 node = node->rb_left;
330 else if (node->rb_right != NULL)
331 node = node->rb_right;
333 bfq_update_active_tree(node);
335 if (bfqq != NULL)
336 list_add(&bfqq->bfqq_list, &bfqq->bfqd->active_list);
340 * bfq_ioprio_to_weight - calc a weight from an ioprio.
341 * @ioprio: the ioprio value to convert.
343 static unsigned short bfq_ioprio_to_weight(int ioprio)
345 WARN_ON(ioprio < 0 || ioprio >= IOPRIO_BE_NR);
346 return IOPRIO_BE_NR - ioprio;
350 * bfq_weight_to_ioprio - calc an ioprio from a weight.
351 * @weight: the weight value to convert.
353 * To preserve as mush as possible the old only-ioprio user interface,
354 * 0 is used as an escape ioprio value for weights (numerically) equal or
355 * larger than IOPRIO_BE_NR
357 static unsigned short bfq_weight_to_ioprio(int weight)
359 WARN_ON(weight < BFQ_MIN_WEIGHT || weight > BFQ_MAX_WEIGHT);
360 return IOPRIO_BE_NR - weight < 0 ? 0 : IOPRIO_BE_NR - weight;
363 static inline void bfq_get_entity(struct bfq_entity *entity)
365 struct bfq_queue *bfqq = bfq_entity_to_bfqq(entity);
366 struct bfq_sched_data *sd;
368 if (bfqq != NULL) {
369 sd = entity->sched_data;
370 atomic_inc(&bfqq->ref);
371 bfq_log_bfqq(bfqq->bfqd, bfqq, "get_entity: %p %d",
372 bfqq, atomic_read(&bfqq->ref));
377 * bfq_find_deepest - find the deepest node that an extraction can modify.
378 * @node: the node being removed.
380 * Do the first step of an extraction in an rb tree, looking for the
381 * node that will replace @node, and returning the deepest node that
382 * the following modifications to the tree can touch. If @node is the
383 * last node in the tree return %NULL.
385 static struct rb_node *bfq_find_deepest(struct rb_node *node)
387 struct rb_node *deepest;
389 if (node->rb_right == NULL && node->rb_left == NULL)
390 deepest = rb_parent(node);
391 else if (node->rb_right == NULL)
392 deepest = node->rb_left;
393 else if (node->rb_left == NULL)
394 deepest = node->rb_right;
395 else {
396 deepest = rb_next(node);
397 if (deepest->rb_right != NULL)
398 deepest = deepest->rb_right;
399 else if (rb_parent(deepest) != node)
400 deepest = rb_parent(deepest);
403 return deepest;
407 * bfq_active_extract - remove an entity from the active tree.
408 * @st: the service_tree containing the tree.
409 * @entity: the entity being removed.
411 static void bfq_active_extract(struct bfq_service_tree *st,
412 struct bfq_entity *entity)
414 struct bfq_queue *bfqq = bfq_entity_to_bfqq(entity);
415 struct rb_node *node;
417 node = bfq_find_deepest(&entity->rb_node);
418 bfq_extract(&st->active, entity);
420 if (node != NULL)
421 bfq_update_active_tree(node);
423 if (bfqq != NULL)
424 list_del(&bfqq->bfqq_list);
428 * bfq_idle_insert - insert an entity into the idle tree.
429 * @st: the service tree containing the tree.
430 * @entity: the entity to insert.
432 static void bfq_idle_insert(struct bfq_service_tree *st,
433 struct bfq_entity *entity)
435 struct bfq_queue *bfqq = bfq_entity_to_bfqq(entity);
436 struct bfq_entity *first_idle = st->first_idle;
437 struct bfq_entity *last_idle = st->last_idle;
439 if (first_idle == NULL || bfq_gt(first_idle->finish, entity->finish))
440 st->first_idle = entity;
441 if (last_idle == NULL || bfq_gt(entity->finish, last_idle->finish))
442 st->last_idle = entity;
444 bfq_insert(&st->idle, entity);
446 if (bfqq != NULL)
447 list_add(&bfqq->bfqq_list, &bfqq->bfqd->idle_list);
451 * bfq_forget_entity - remove an entity from the wfq trees.
452 * @st: the service tree.
453 * @entity: the entity being removed.
455 * Update the device status and forget everything about @entity, putting
456 * the device reference to it, if it is a queue. Entities belonging to
457 * groups are not refcounted.
459 static void bfq_forget_entity(struct bfq_service_tree *st,
460 struct bfq_entity *entity)
462 struct bfq_queue *bfqq = bfq_entity_to_bfqq(entity);
463 struct bfq_sched_data *sd;
465 BUG_ON(!entity->on_st);
467 entity->on_st = 0;
468 st->wsum -= entity->weight;
469 if (bfqq != NULL) {
470 sd = entity->sched_data;
471 bfq_log_bfqq(bfqq->bfqd, bfqq, "forget_entity: %p %d",
472 bfqq, atomic_read(&bfqq->ref));
473 bfq_put_queue(bfqq);
478 * bfq_put_idle_entity - release the idle tree ref of an entity.
479 * @st: service tree for the entity.
480 * @entity: the entity being released.
482 static void bfq_put_idle_entity(struct bfq_service_tree *st,
483 struct bfq_entity *entity)
485 bfq_idle_extract(st, entity);
486 bfq_forget_entity(st, entity);
490 * bfq_forget_idle - update the idle tree if necessary.
491 * @st: the service tree to act upon.
493 * To preserve the global O(log N) complexity we only remove one entry here;
494 * as the idle tree will not grow indefinitely this can be done safely.
496 static void bfq_forget_idle(struct bfq_service_tree *st)
498 struct bfq_entity *first_idle = st->first_idle;
499 struct bfq_entity *last_idle = st->last_idle;
501 if (RB_EMPTY_ROOT(&st->active) && last_idle != NULL &&
502 !bfq_gt(last_idle->finish, st->vtime)) {
504 * Forget the whole idle tree, increasing the vtime past
505 * the last finish time of idle entities.
507 st->vtime = last_idle->finish;
510 if (first_idle != NULL && !bfq_gt(first_idle->finish, st->vtime))
511 bfq_put_idle_entity(st, first_idle);
514 static struct bfq_service_tree *
515 __bfq_entity_update_weight_prio(struct bfq_service_tree *old_st,
516 struct bfq_entity *entity)
518 struct bfq_service_tree *new_st = old_st;
520 if (entity->ioprio_changed) {
521 struct bfq_queue *bfqq = bfq_entity_to_bfqq(entity);
523 BUG_ON(old_st->wsum < entity->weight);
524 old_st->wsum -= entity->weight;
526 if (entity->new_weight != entity->orig_weight) {
527 entity->orig_weight = entity->new_weight;
528 entity->ioprio =
529 bfq_weight_to_ioprio(entity->orig_weight);
530 } else if (entity->new_ioprio != entity->ioprio) {
531 entity->ioprio = entity->new_ioprio;
532 entity->orig_weight =
533 bfq_ioprio_to_weight(entity->ioprio);
534 } else
535 entity->new_weight = entity->orig_weight =
536 bfq_ioprio_to_weight(entity->ioprio);
538 entity->ioprio_class = entity->new_ioprio_class;
539 entity->ioprio_changed = 0;
542 * NOTE: here we may be changing the weight too early,
543 * this will cause unfairness. The correct approach
544 * would have required additional complexity to defer
545 * weight changes to the proper time instants (i.e.,
546 * when entity->finish <= old_st->vtime).
548 new_st = bfq_entity_service_tree(entity);
549 entity->weight = entity->orig_weight *
550 (bfqq != NULL ? bfqq->raising_coeff : 1);
551 new_st->wsum += entity->weight;
553 if (new_st != old_st)
554 entity->start = new_st->vtime;
557 return new_st;
561 * bfq_bfqq_served - update the scheduler status after selection for service.
562 * @bfqq: the queue being served.
563 * @served: bytes to transfer.
565 * NOTE: this can be optimized, as the timestamps of upper level entities
566 * are synchronized every time a new bfqq is selected for service. By now,
567 * we keep it to better check consistency.
569 static void bfq_bfqq_served(struct bfq_queue *bfqq, unsigned long served)
571 struct bfq_entity *entity = &bfqq->entity;
572 struct bfq_service_tree *st;
574 for_each_entity(entity) {
575 st = bfq_entity_service_tree(entity);
577 entity->service += served;
578 WARN_ON_ONCE(entity->service > entity->budget);
579 BUG_ON(st->wsum == 0);
581 st->vtime += bfq_delta(served, st->wsum);
582 bfq_forget_idle(st);
584 bfq_log_bfqq(bfqq->bfqd, bfqq, "bfqq_served %lu secs", served);
588 * bfq_bfqq_charge_full_budget - set the service to the entity budget.
589 * @bfqq: the queue that needs a service update.
591 * When it's not possible to be fair in the service domain, because
592 * a queue is not consuming its budget fast enough (the meaning of
593 * fast depends on the timeout parameter), we charge it a full
594 * budget. In this way we should obtain a sort of time-domain
595 * fairness among all the seeky/slow queues.
597 static inline void bfq_bfqq_charge_full_budget(struct bfq_queue *bfqq)
599 struct bfq_entity *entity = &bfqq->entity;
601 bfq_log_bfqq(bfqq->bfqd, bfqq, "charge_full_budget");
603 bfq_bfqq_served(bfqq, entity->budget - entity->service);
607 * __bfq_activate_entity - activate an entity.
608 * @entity: the entity being activated.
610 * Called whenever an entity is activated, i.e., it is not active and one
611 * of its children receives a new request, or has to be reactivated due to
612 * budget exhaustion. It uses the current budget of the entity (and the
613 * service received if @entity is active) of the queue to calculate its
614 * timestamps.
616 static void __bfq_activate_entity(struct bfq_entity *entity)
618 struct bfq_sched_data *sd = entity->sched_data;
619 struct bfq_service_tree *st = bfq_entity_service_tree(entity);
621 if (entity == sd->active_entity) {
622 BUG_ON(entity->tree != NULL);
624 * If we are requeueing the current entity we have
625 * to take care of not charging to it service it has
626 * not received.
628 bfq_calc_finish(entity, entity->service);
629 entity->start = entity->finish;
630 sd->active_entity = NULL;
631 } else if (entity->tree == &st->active) {
633 * Requeueing an entity due to a change of some
634 * next_active entity below it. We reuse the old
635 * start time.
637 bfq_active_extract(st, entity);
638 } else if (entity->tree == &st->idle) {
640 * Must be on the idle tree, bfq_idle_extract() will
641 * check for that.
643 bfq_idle_extract(st, entity);
644 entity->start = bfq_gt(st->vtime, entity->finish) ?
645 st->vtime : entity->finish;
646 } else {
648 * The finish time of the entity may be invalid, and
649 * it is in the past for sure, otherwise the queue
650 * would have been on the idle tree.
652 entity->start = st->vtime;
653 st->wsum += entity->weight;
654 bfq_get_entity(entity);
656 BUG_ON(entity->on_st);
657 entity->on_st = 1;
660 st = __bfq_entity_update_weight_prio(st, entity);
661 bfq_calc_finish(entity, entity->budget);
662 bfq_active_insert(st, entity);
666 * bfq_activate_entity - activate an entity and its ancestors if necessary.
667 * @entity: the entity to activate.
669 * Activate @entity and all the entities on the path from it to the root.
671 static void bfq_activate_entity(struct bfq_entity *entity)
673 struct bfq_sched_data *sd;
675 for_each_entity(entity) {
676 __bfq_activate_entity(entity);
678 sd = entity->sched_data;
679 if (!bfq_update_next_active(sd))
681 * No need to propagate the activation to the
682 * upper entities, as they will be updated when
683 * the active entity is rescheduled.
685 break;
690 * __bfq_deactivate_entity - deactivate an entity from its service tree.
691 * @entity: the entity to deactivate.
692 * @requeue: if false, the entity will not be put into the idle tree.
694 * Deactivate an entity, independently from its previous state. If the
695 * entity was not on a service tree just return, otherwise if it is on
696 * any scheduler tree, extract it from that tree, and if necessary
697 * and if the caller did not specify @requeue, put it on the idle tree.
699 * Return %1 if the caller should update the entity hierarchy, i.e.,
700 * if the entity was under service or if it was the next_active for
701 * its sched_data; return %0 otherwise.
703 static int __bfq_deactivate_entity(struct bfq_entity *entity, int requeue)
705 struct bfq_sched_data *sd = entity->sched_data;
706 struct bfq_service_tree *st = bfq_entity_service_tree(entity);
707 int was_active = entity == sd->active_entity;
708 int ret = 0;
710 if (!entity->on_st)
711 return 0;
713 BUG_ON(was_active && entity->tree != NULL);
715 if (was_active) {
716 bfq_calc_finish(entity, entity->service);
717 sd->active_entity = NULL;
718 } else if (entity->tree == &st->active)
719 bfq_active_extract(st, entity);
720 else if (entity->tree == &st->idle)
721 bfq_idle_extract(st, entity);
722 else if (entity->tree != NULL)
723 BUG();
725 if (was_active || sd->next_active == entity)
726 ret = bfq_update_next_active(sd);
728 if (!requeue || !bfq_gt(entity->finish, st->vtime))
729 bfq_forget_entity(st, entity);
730 else
731 bfq_idle_insert(st, entity);
733 BUG_ON(sd->active_entity == entity);
734 BUG_ON(sd->next_active == entity);
736 return ret;
740 * bfq_deactivate_entity - deactivate an entity.
741 * @entity: the entity to deactivate.
742 * @requeue: true if the entity can be put on the idle tree
744 static void bfq_deactivate_entity(struct bfq_entity *entity, int requeue)
746 struct bfq_sched_data *sd;
747 struct bfq_entity *parent;
749 for_each_entity_safe(entity, parent) {
750 sd = entity->sched_data;
752 if (!__bfq_deactivate_entity(entity, requeue))
754 * The parent entity is still backlogged, and
755 * we don't need to update it as it is still
756 * under service.
758 break;
760 if (sd->next_active != NULL)
762 * The parent entity is still backlogged and
763 * the budgets on the path towards the root
764 * need to be updated.
766 goto update;
769 * If we reach there the parent is no more backlogged and
770 * we want to propagate the dequeue upwards.
772 requeue = 1;
775 return;
777 update:
778 entity = parent;
779 for_each_entity(entity) {
780 __bfq_activate_entity(entity);
782 sd = entity->sched_data;
783 if (!bfq_update_next_active(sd))
784 break;
789 * bfq_update_vtime - update vtime if necessary.
790 * @st: the service tree to act upon.
792 * If necessary update the service tree vtime to have at least one
793 * eligible entity, skipping to its start time. Assumes that the
794 * active tree of the device is not empty.
796 * NOTE: this hierarchical implementation updates vtimes quite often,
797 * we may end up with reactivated tasks getting timestamps after a
798 * vtime skip done because we needed a ->first_active entity on some
799 * intermediate node.
801 static void bfq_update_vtime(struct bfq_service_tree *st)
803 struct bfq_entity *entry;
804 struct rb_node *node = st->active.rb_node;
806 entry = rb_entry(node, struct bfq_entity, rb_node);
807 if (bfq_gt(entry->min_start, st->vtime)) {
808 st->vtime = entry->min_start;
809 bfq_forget_idle(st);
814 * bfq_first_active - find the eligible entity with the smallest finish time
815 * @st: the service tree to select from.
817 * This function searches the first schedulable entity, starting from the
818 * root of the tree and going on the left every time on this side there is
819 * a subtree with at least one eligible (start >= vtime) entity. The path
820 * on the right is followed only if a) the left subtree contains no eligible
821 * entities and b) no eligible entity has been found yet.
823 static struct bfq_entity *bfq_first_active_entity(struct bfq_service_tree *st)
825 struct bfq_entity *entry, *first = NULL;
826 struct rb_node *node = st->active.rb_node;
828 while (node != NULL) {
829 entry = rb_entry(node, struct bfq_entity, rb_node);
830 left:
831 if (!bfq_gt(entry->start, st->vtime))
832 first = entry;
834 BUG_ON(bfq_gt(entry->min_start, st->vtime));
836 if (node->rb_left != NULL) {
837 entry = rb_entry(node->rb_left,
838 struct bfq_entity, rb_node);
839 if (!bfq_gt(entry->min_start, st->vtime)) {
840 node = node->rb_left;
841 goto left;
844 if (first != NULL)
845 break;
846 node = node->rb_right;
849 BUG_ON(first == NULL && !RB_EMPTY_ROOT(&st->active));
850 return first;
854 * __bfq_lookup_next_entity - return the first eligible entity in @st.
855 * @st: the service tree.
857 * Update the virtual time in @st and return the first eligible entity
858 * it contains.
860 static struct bfq_entity *__bfq_lookup_next_entity(struct bfq_service_tree *st)
862 struct bfq_entity *entity;
864 if (RB_EMPTY_ROOT(&st->active))
865 return NULL;
867 bfq_update_vtime(st);
868 entity = bfq_first_active_entity(st);
869 BUG_ON(bfq_gt(entity->start, st->vtime));
871 return entity;
875 * bfq_lookup_next_entity - return the first eligible entity in @sd.
876 * @sd: the sched_data.
877 * @extract: if true the returned entity will be also extracted from @sd.
879 * NOTE: since we cache the next_active entity at each level of the
880 * hierarchy, the complexity of the lookup can be decreased with
881 * absolutely no effort just returning the cached next_active value;
882 * we prefer to do full lookups to test the consistency of * the data
883 * structures.
885 static struct bfq_entity *bfq_lookup_next_entity(struct bfq_sched_data *sd,
886 int extract,
887 struct bfq_data *bfqd)
889 struct bfq_service_tree *st = sd->service_tree;
890 struct bfq_entity *entity;
891 int i=0;
893 BUG_ON(sd->active_entity != NULL);
895 if (bfqd != NULL &&
896 jiffies - bfqd->bfq_class_idle_last_service > BFQ_CL_IDLE_TIMEOUT) {
897 entity = __bfq_lookup_next_entity(st + BFQ_IOPRIO_CLASSES - 1);
898 if (entity != NULL) {
899 i = BFQ_IOPRIO_CLASSES - 1;
900 bfqd->bfq_class_idle_last_service = jiffies;
901 sd->next_active = entity;
904 for (; i < BFQ_IOPRIO_CLASSES; i++) {
905 entity = __bfq_lookup_next_entity(st + i);
906 if (entity != NULL) {
907 if (extract) {
908 bfq_check_next_active(sd, entity);
909 bfq_active_extract(st + i, entity);
910 sd->active_entity = entity;
911 sd->next_active = NULL;
913 break;
917 return entity;
921 * Get next queue for service.
923 static struct bfq_queue *bfq_get_next_queue(struct bfq_data *bfqd)
925 struct bfq_entity *entity = NULL;
926 struct bfq_sched_data *sd;
927 struct bfq_queue *bfqq;
929 BUG_ON(bfqd->active_queue != NULL);
931 if (bfqd->busy_queues == 0)
932 return NULL;
934 sd = &bfqd->root_group->sched_data;
935 for (; sd != NULL; sd = entity->my_sched_data) {
936 entity = bfq_lookup_next_entity(sd, 1, bfqd);
937 BUG_ON(entity == NULL);
938 entity->service = 0;
941 bfqq = bfq_entity_to_bfqq(entity);
942 BUG_ON(bfqq == NULL);
944 return bfqq;
948 * Forced extraction of the given queue.
950 static void bfq_get_next_queue_forced(struct bfq_data *bfqd,
951 struct bfq_queue *bfqq)
953 struct bfq_entity *entity;
954 struct bfq_sched_data *sd;
956 BUG_ON(bfqd->active_queue != NULL);
958 entity = &bfqq->entity;
960 * Bubble up extraction/update from the leaf to the root.
962 for_each_entity(entity) {
963 sd = entity->sched_data;
964 bfq_update_vtime(bfq_entity_service_tree(entity));
965 bfq_active_extract(bfq_entity_service_tree(entity), entity);
966 sd->active_entity = entity;
967 sd->next_active = NULL;
968 entity->service = 0;
971 return;
974 static void __bfq_bfqd_reset_active(struct bfq_data *bfqd)
976 if (bfqd->active_bic != NULL) {
977 put_io_context(bfqd->active_bic->icq.ioc);
978 bfqd->active_bic = NULL;
981 bfqd->active_queue = NULL;
982 del_timer(&bfqd->idle_slice_timer);
985 static void bfq_deactivate_bfqq(struct bfq_data *bfqd, struct bfq_queue *bfqq,
986 int requeue)
988 struct bfq_entity *entity = &bfqq->entity;
990 if (bfqq == bfqd->active_queue)
991 __bfq_bfqd_reset_active(bfqd);
993 bfq_deactivate_entity(entity, requeue);
996 static void bfq_activate_bfqq(struct bfq_data *bfqd, struct bfq_queue *bfqq)
998 struct bfq_entity *entity = &bfqq->entity;
1000 bfq_activate_entity(entity);
1004 * Called when the bfqq no longer has requests pending, remove it from
1005 * the service tree.
1007 static void bfq_del_bfqq_busy(struct bfq_data *bfqd, struct bfq_queue *bfqq,
1008 int requeue)
1010 BUG_ON(!bfq_bfqq_busy(bfqq));
1011 BUG_ON(!RB_EMPTY_ROOT(&bfqq->sort_list));
1013 bfq_log_bfqq(bfqd, bfqq, "del from busy");
1015 bfq_clear_bfqq_busy(bfqq);
1017 BUG_ON(bfqd->busy_queues == 0);
1018 bfqd->busy_queues--;
1020 bfq_deactivate_bfqq(bfqd, bfqq, requeue);
1024 * Called when an inactive queue receives a new request.
1026 static void bfq_add_bfqq_busy(struct bfq_data *bfqd, struct bfq_queue *bfqq)
1028 BUG_ON(bfq_bfqq_busy(bfqq));
1029 BUG_ON(bfqq == bfqd->active_queue);
1031 bfq_log_bfqq(bfqd, bfqq, "add to busy");
1033 bfq_activate_bfqq(bfqd, bfqq);
1035 bfq_mark_bfqq_busy(bfqq);
1036 bfqd->busy_queues++;