2 * cgroups support for the BFQ I/O scheduler.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 #include <linux/module.h>
15 #include <linux/slab.h>
16 #include <linux/blkdev.h>
17 #include <linux/cgroup.h>
18 #include <linux/elevator.h>
19 #include <linux/ktime.h>
20 #include <linux/rbtree.h>
21 #include <linux/ioprio.h>
22 #include <linux/sbitmap.h>
23 #include <linux/delay.h>
25 #include "bfq-iosched.h"
27 #ifdef CONFIG_BFQ_GROUP_IOSCHED
29 /* bfqg stats flags */
30 enum bfqg_stats_flags
{
31 BFQG_stats_waiting
= 0,
36 #define BFQG_FLAG_FNS(name) \
37 static void bfqg_stats_mark_##name(struct bfqg_stats *stats) \
39 stats->flags |= (1 << BFQG_stats_##name); \
41 static void bfqg_stats_clear_##name(struct bfqg_stats *stats) \
43 stats->flags &= ~(1 << BFQG_stats_##name); \
45 static int bfqg_stats_##name(struct bfqg_stats *stats) \
47 return (stats->flags & (1 << BFQG_stats_##name)) != 0; \
50 BFQG_FLAG_FNS(waiting)
55 /* This should be called with the scheduler lock held. */
56 static void bfqg_stats_update_group_wait_time(struct bfqg_stats
*stats
)
58 unsigned long long now
;
60 if (!bfqg_stats_waiting(stats
))
64 if (time_after64(now
, stats
->start_group_wait_time
))
65 blkg_stat_add(&stats
->group_wait_time
,
66 now
- stats
->start_group_wait_time
);
67 bfqg_stats_clear_waiting(stats
);
70 /* This should be called with the scheduler lock held. */
71 static void bfqg_stats_set_start_group_wait_time(struct bfq_group
*bfqg
,
72 struct bfq_group
*curr_bfqg
)
74 struct bfqg_stats
*stats
= &bfqg
->stats
;
76 if (bfqg_stats_waiting(stats
))
78 if (bfqg
== curr_bfqg
)
80 stats
->start_group_wait_time
= sched_clock();
81 bfqg_stats_mark_waiting(stats
);
84 /* This should be called with the scheduler lock held. */
85 static void bfqg_stats_end_empty_time(struct bfqg_stats
*stats
)
87 unsigned long long now
;
89 if (!bfqg_stats_empty(stats
))
93 if (time_after64(now
, stats
->start_empty_time
))
94 blkg_stat_add(&stats
->empty_time
,
95 now
- stats
->start_empty_time
);
96 bfqg_stats_clear_empty(stats
);
99 void bfqg_stats_update_dequeue(struct bfq_group
*bfqg
)
101 blkg_stat_add(&bfqg
->stats
.dequeue
, 1);
104 void bfqg_stats_set_start_empty_time(struct bfq_group
*bfqg
)
106 struct bfqg_stats
*stats
= &bfqg
->stats
;
108 if (blkg_rwstat_total(&stats
->queued
))
112 * group is already marked empty. This can happen if bfqq got new
113 * request in parent group and moved to this group while being added
114 * to service tree. Just ignore the event and move on.
116 if (bfqg_stats_empty(stats
))
119 stats
->start_empty_time
= sched_clock();
120 bfqg_stats_mark_empty(stats
);
123 void bfqg_stats_update_idle_time(struct bfq_group
*bfqg
)
125 struct bfqg_stats
*stats
= &bfqg
->stats
;
127 if (bfqg_stats_idling(stats
)) {
128 unsigned long long now
= sched_clock();
130 if (time_after64(now
, stats
->start_idle_time
))
131 blkg_stat_add(&stats
->idle_time
,
132 now
- stats
->start_idle_time
);
133 bfqg_stats_clear_idling(stats
);
137 void bfqg_stats_set_start_idle_time(struct bfq_group
*bfqg
)
139 struct bfqg_stats
*stats
= &bfqg
->stats
;
141 stats
->start_idle_time
= sched_clock();
142 bfqg_stats_mark_idling(stats
);
145 void bfqg_stats_update_avg_queue_size(struct bfq_group
*bfqg
)
147 struct bfqg_stats
*stats
= &bfqg
->stats
;
149 blkg_stat_add(&stats
->avg_queue_size_sum
,
150 blkg_rwstat_total(&stats
->queued
));
151 blkg_stat_add(&stats
->avg_queue_size_samples
, 1);
152 bfqg_stats_update_group_wait_time(stats
);
156 * blk-cgroup policy-related handlers
157 * The following functions help in converting between blk-cgroup
158 * internal structures and BFQ-specific structures.
161 static struct bfq_group
*pd_to_bfqg(struct blkg_policy_data
*pd
)
163 return pd
? container_of(pd
, struct bfq_group
, pd
) : NULL
;
166 struct blkcg_gq
*bfqg_to_blkg(struct bfq_group
*bfqg
)
168 return pd_to_blkg(&bfqg
->pd
);
171 static struct bfq_group
*blkg_to_bfqg(struct blkcg_gq
*blkg
)
173 return pd_to_bfqg(blkg_to_pd(blkg
, &blkcg_policy_bfq
));
178 * The following functions help in navigating the bfq_group hierarchy
179 * by allowing to find the parent of a bfq_group or the bfq_group
180 * associated to a bfq_queue.
183 static struct bfq_group
*bfqg_parent(struct bfq_group
*bfqg
)
185 struct blkcg_gq
*pblkg
= bfqg_to_blkg(bfqg
)->parent
;
187 return pblkg
? blkg_to_bfqg(pblkg
) : NULL
;
190 struct bfq_group
*bfqq_group(struct bfq_queue
*bfqq
)
192 struct bfq_entity
*group_entity
= bfqq
->entity
.parent
;
194 return group_entity
? container_of(group_entity
, struct bfq_group
,
196 bfqq
->bfqd
->root_group
;
200 * The following two functions handle get and put of a bfq_group by
201 * wrapping the related blk-cgroup hooks.
204 static void bfqg_get(struct bfq_group
*bfqg
)
209 static void bfqg_put(struct bfq_group
*bfqg
)
217 static void bfqg_and_blkg_get(struct bfq_group
*bfqg
)
219 /* see comments in bfq_bic_update_cgroup for why refcounting bfqg */
222 blkg_get(bfqg_to_blkg(bfqg
));
225 void bfqg_and_blkg_put(struct bfq_group
*bfqg
)
227 blkg_put(bfqg_to_blkg(bfqg
));
232 void bfqg_stats_update_io_add(struct bfq_group
*bfqg
, struct bfq_queue
*bfqq
,
235 blkg_rwstat_add(&bfqg
->stats
.queued
, op
, 1);
236 bfqg_stats_end_empty_time(&bfqg
->stats
);
237 if (!(bfqq
== ((struct bfq_data
*)bfqg
->bfqd
)->in_service_queue
))
238 bfqg_stats_set_start_group_wait_time(bfqg
, bfqq_group(bfqq
));
241 void bfqg_stats_update_io_remove(struct bfq_group
*bfqg
, unsigned int op
)
243 blkg_rwstat_add(&bfqg
->stats
.queued
, op
, -1);
246 void bfqg_stats_update_io_merged(struct bfq_group
*bfqg
, unsigned int op
)
248 blkg_rwstat_add(&bfqg
->stats
.merged
, op
, 1);
251 void bfqg_stats_update_completion(struct bfq_group
*bfqg
, uint64_t start_time
,
252 uint64_t io_start_time
, unsigned int op
)
254 struct bfqg_stats
*stats
= &bfqg
->stats
;
255 unsigned long long now
= sched_clock();
257 if (time_after64(now
, io_start_time
))
258 blkg_rwstat_add(&stats
->service_time
, op
,
259 now
- io_start_time
);
260 if (time_after64(io_start_time
, start_time
))
261 blkg_rwstat_add(&stats
->wait_time
, op
,
262 io_start_time
- start_time
);
266 static void bfqg_stats_reset(struct bfqg_stats
*stats
)
268 /* queued stats shouldn't be cleared */
269 blkg_rwstat_reset(&stats
->merged
);
270 blkg_rwstat_reset(&stats
->service_time
);
271 blkg_rwstat_reset(&stats
->wait_time
);
272 blkg_stat_reset(&stats
->time
);
273 blkg_stat_reset(&stats
->avg_queue_size_sum
);
274 blkg_stat_reset(&stats
->avg_queue_size_samples
);
275 blkg_stat_reset(&stats
->dequeue
);
276 blkg_stat_reset(&stats
->group_wait_time
);
277 blkg_stat_reset(&stats
->idle_time
);
278 blkg_stat_reset(&stats
->empty_time
);
282 static void bfqg_stats_add_aux(struct bfqg_stats
*to
, struct bfqg_stats
*from
)
287 /* queued stats shouldn't be cleared */
288 blkg_rwstat_add_aux(&to
->merged
, &from
->merged
);
289 blkg_rwstat_add_aux(&to
->service_time
, &from
->service_time
);
290 blkg_rwstat_add_aux(&to
->wait_time
, &from
->wait_time
);
291 blkg_stat_add_aux(&from
->time
, &from
->time
);
292 blkg_stat_add_aux(&to
->avg_queue_size_sum
, &from
->avg_queue_size_sum
);
293 blkg_stat_add_aux(&to
->avg_queue_size_samples
,
294 &from
->avg_queue_size_samples
);
295 blkg_stat_add_aux(&to
->dequeue
, &from
->dequeue
);
296 blkg_stat_add_aux(&to
->group_wait_time
, &from
->group_wait_time
);
297 blkg_stat_add_aux(&to
->idle_time
, &from
->idle_time
);
298 blkg_stat_add_aux(&to
->empty_time
, &from
->empty_time
);
302 * Transfer @bfqg's stats to its parent's aux counts so that the ancestors'
303 * recursive stats can still account for the amount used by this bfqg after
306 static void bfqg_stats_xfer_dead(struct bfq_group
*bfqg
)
308 struct bfq_group
*parent
;
310 if (!bfqg
) /* root_group */
313 parent
= bfqg_parent(bfqg
);
315 lockdep_assert_held(bfqg_to_blkg(bfqg
)->q
->queue_lock
);
317 if (unlikely(!parent
))
320 bfqg_stats_add_aux(&parent
->stats
, &bfqg
->stats
);
321 bfqg_stats_reset(&bfqg
->stats
);
324 void bfq_init_entity(struct bfq_entity
*entity
, struct bfq_group
*bfqg
)
326 struct bfq_queue
*bfqq
= bfq_entity_to_bfqq(entity
);
328 entity
->weight
= entity
->new_weight
;
329 entity
->orig_weight
= entity
->new_weight
;
331 bfqq
->ioprio
= bfqq
->new_ioprio
;
332 bfqq
->ioprio_class
= bfqq
->new_ioprio_class
;
334 * Make sure that bfqg and its associated blkg do not
335 * disappear before entity.
337 bfqg_and_blkg_get(bfqg
);
339 entity
->parent
= bfqg
->my_entity
; /* NULL for root group */
340 entity
->sched_data
= &bfqg
->sched_data
;
343 static void bfqg_stats_exit(struct bfqg_stats
*stats
)
345 blkg_rwstat_exit(&stats
->merged
);
346 blkg_rwstat_exit(&stats
->service_time
);
347 blkg_rwstat_exit(&stats
->wait_time
);
348 blkg_rwstat_exit(&stats
->queued
);
349 blkg_stat_exit(&stats
->time
);
350 blkg_stat_exit(&stats
->avg_queue_size_sum
);
351 blkg_stat_exit(&stats
->avg_queue_size_samples
);
352 blkg_stat_exit(&stats
->dequeue
);
353 blkg_stat_exit(&stats
->group_wait_time
);
354 blkg_stat_exit(&stats
->idle_time
);
355 blkg_stat_exit(&stats
->empty_time
);
358 static int bfqg_stats_init(struct bfqg_stats
*stats
, gfp_t gfp
)
360 if (blkg_rwstat_init(&stats
->merged
, gfp
) ||
361 blkg_rwstat_init(&stats
->service_time
, gfp
) ||
362 blkg_rwstat_init(&stats
->wait_time
, gfp
) ||
363 blkg_rwstat_init(&stats
->queued
, gfp
) ||
364 blkg_stat_init(&stats
->time
, gfp
) ||
365 blkg_stat_init(&stats
->avg_queue_size_sum
, gfp
) ||
366 blkg_stat_init(&stats
->avg_queue_size_samples
, gfp
) ||
367 blkg_stat_init(&stats
->dequeue
, gfp
) ||
368 blkg_stat_init(&stats
->group_wait_time
, gfp
) ||
369 blkg_stat_init(&stats
->idle_time
, gfp
) ||
370 blkg_stat_init(&stats
->empty_time
, gfp
)) {
371 bfqg_stats_exit(stats
);
378 static struct bfq_group_data
*cpd_to_bfqgd(struct blkcg_policy_data
*cpd
)
380 return cpd
? container_of(cpd
, struct bfq_group_data
, pd
) : NULL
;
383 static struct bfq_group_data
*blkcg_to_bfqgd(struct blkcg
*blkcg
)
385 return cpd_to_bfqgd(blkcg_to_cpd(blkcg
, &blkcg_policy_bfq
));
388 static struct blkcg_policy_data
*bfq_cpd_alloc(gfp_t gfp
)
390 struct bfq_group_data
*bgd
;
392 bgd
= kzalloc(sizeof(*bgd
), gfp
);
398 static void bfq_cpd_init(struct blkcg_policy_data
*cpd
)
400 struct bfq_group_data
*d
= cpd_to_bfqgd(cpd
);
402 d
->weight
= cgroup_subsys_on_dfl(io_cgrp_subsys
) ?
403 CGROUP_WEIGHT_DFL
: BFQ_WEIGHT_LEGACY_DFL
;
406 static void bfq_cpd_free(struct blkcg_policy_data
*cpd
)
408 kfree(cpd_to_bfqgd(cpd
));
411 static struct blkg_policy_data
*bfq_pd_alloc(gfp_t gfp
, int node
)
413 struct bfq_group
*bfqg
;
415 bfqg
= kzalloc_node(sizeof(*bfqg
), gfp
, node
);
419 if (bfqg_stats_init(&bfqg
->stats
, gfp
)) {
424 /* see comments in bfq_bic_update_cgroup for why refcounting */
429 static void bfq_pd_init(struct blkg_policy_data
*pd
)
431 struct blkcg_gq
*blkg
= pd_to_blkg(pd
);
432 struct bfq_group
*bfqg
= blkg_to_bfqg(blkg
);
433 struct bfq_data
*bfqd
= blkg
->q
->elevator
->elevator_data
;
434 struct bfq_entity
*entity
= &bfqg
->entity
;
435 struct bfq_group_data
*d
= blkcg_to_bfqgd(blkg
->blkcg
);
437 entity
->orig_weight
= entity
->weight
= entity
->new_weight
= d
->weight
;
438 entity
->my_sched_data
= &bfqg
->sched_data
;
439 bfqg
->my_entity
= entity
; /*
440 * the root_group's will be set to NULL
441 * in bfq_init_queue()
444 bfqg
->active_entities
= 0;
445 bfqg
->rq_pos_tree
= RB_ROOT
;
448 static void bfq_pd_free(struct blkg_policy_data
*pd
)
450 struct bfq_group
*bfqg
= pd_to_bfqg(pd
);
452 bfqg_stats_exit(&bfqg
->stats
);
456 static void bfq_pd_reset_stats(struct blkg_policy_data
*pd
)
458 struct bfq_group
*bfqg
= pd_to_bfqg(pd
);
460 bfqg_stats_reset(&bfqg
->stats
);
463 static void bfq_group_set_parent(struct bfq_group
*bfqg
,
464 struct bfq_group
*parent
)
466 struct bfq_entity
*entity
;
468 entity
= &bfqg
->entity
;
469 entity
->parent
= parent
->my_entity
;
470 entity
->sched_data
= &parent
->sched_data
;
473 static struct bfq_group
*bfq_lookup_bfqg(struct bfq_data
*bfqd
,
476 struct blkcg_gq
*blkg
;
478 blkg
= blkg_lookup(blkcg
, bfqd
->queue
);
480 return blkg_to_bfqg(blkg
);
484 struct bfq_group
*bfq_find_set_group(struct bfq_data
*bfqd
,
487 struct bfq_group
*bfqg
, *parent
;
488 struct bfq_entity
*entity
;
490 bfqg
= bfq_lookup_bfqg(bfqd
, blkcg
);
496 * Update chain of bfq_groups as we might be handling a leaf group
497 * which, along with some of its relatives, has not been hooked yet
498 * to the private hierarchy of BFQ.
500 entity
= &bfqg
->entity
;
501 for_each_entity(entity
) {
502 struct bfq_group
*curr_bfqg
= container_of(entity
,
503 struct bfq_group
, entity
);
504 if (curr_bfqg
!= bfqd
->root_group
) {
505 parent
= bfqg_parent(curr_bfqg
);
507 parent
= bfqd
->root_group
;
508 bfq_group_set_parent(curr_bfqg
, parent
);
516 * bfq_bfqq_move - migrate @bfqq to @bfqg.
517 * @bfqd: queue descriptor.
518 * @bfqq: the queue to move.
519 * @bfqg: the group to move to.
521 * Move @bfqq to @bfqg, deactivating it from its old group and reactivating
522 * it on the new one. Avoid putting the entity on the old group idle tree.
524 * Must be called under the scheduler lock, to make sure that the blkg
525 * owning @bfqg does not disappear (see comments in
526 * bfq_bic_update_cgroup on guaranteeing the consistency of blkg
529 void bfq_bfqq_move(struct bfq_data
*bfqd
, struct bfq_queue
*bfqq
,
530 struct bfq_group
*bfqg
)
532 struct bfq_entity
*entity
= &bfqq
->entity
;
534 /* If bfqq is empty, then bfq_bfqq_expire also invokes
535 * bfq_del_bfqq_busy, thereby removing bfqq and its entity
536 * from data structures related to current group. Otherwise we
537 * need to remove bfqq explicitly with bfq_deactivate_bfqq, as
540 if (bfqq
== bfqd
->in_service_queue
)
541 bfq_bfqq_expire(bfqd
, bfqd
->in_service_queue
,
542 false, BFQQE_PREEMPTED
);
544 if (bfq_bfqq_busy(bfqq
))
545 bfq_deactivate_bfqq(bfqd
, bfqq
, false, false);
546 else if (entity
->on_st
)
547 bfq_put_idle_entity(bfq_entity_service_tree(entity
), entity
);
548 bfqg_and_blkg_put(bfqq_group(bfqq
));
550 entity
->parent
= bfqg
->my_entity
;
551 entity
->sched_data
= &bfqg
->sched_data
;
552 /* pin down bfqg and its associated blkg */
553 bfqg_and_blkg_get(bfqg
);
555 if (bfq_bfqq_busy(bfqq
)) {
556 bfq_pos_tree_add_move(bfqd
, bfqq
);
557 bfq_activate_bfqq(bfqd
, bfqq
);
560 if (!bfqd
->in_service_queue
&& !bfqd
->rq_in_driver
)
561 bfq_schedule_dispatch(bfqd
);
565 * __bfq_bic_change_cgroup - move @bic to @cgroup.
566 * @bfqd: the queue descriptor.
567 * @bic: the bic to move.
568 * @blkcg: the blk-cgroup to move to.
570 * Move bic to blkcg, assuming that bfqd->lock is held; which makes
571 * sure that the reference to cgroup is valid across the call (see
572 * comments in bfq_bic_update_cgroup on this issue)
574 * NOTE: an alternative approach might have been to store the current
575 * cgroup in bfqq and getting a reference to it, reducing the lookup
576 * time here, at the price of slightly more complex code.
578 static struct bfq_group
*__bfq_bic_change_cgroup(struct bfq_data
*bfqd
,
579 struct bfq_io_cq
*bic
,
582 struct bfq_queue
*async_bfqq
= bic_to_bfqq(bic
, 0);
583 struct bfq_queue
*sync_bfqq
= bic_to_bfqq(bic
, 1);
584 struct bfq_group
*bfqg
;
585 struct bfq_entity
*entity
;
587 bfqg
= bfq_find_set_group(bfqd
, blkcg
);
590 bfqg
= bfqd
->root_group
;
593 entity
= &async_bfqq
->entity
;
595 if (entity
->sched_data
!= &bfqg
->sched_data
) {
596 bic_set_bfqq(bic
, NULL
, 0);
597 bfq_log_bfqq(bfqd
, async_bfqq
,
598 "bic_change_group: %p %d",
599 async_bfqq
, async_bfqq
->ref
);
600 bfq_put_queue(async_bfqq
);
605 entity
= &sync_bfqq
->entity
;
606 if (entity
->sched_data
!= &bfqg
->sched_data
)
607 bfq_bfqq_move(bfqd
, sync_bfqq
, bfqg
);
613 void bfq_bic_update_cgroup(struct bfq_io_cq
*bic
, struct bio
*bio
)
615 struct bfq_data
*bfqd
= bic_to_bfqd(bic
);
616 struct bfq_group
*bfqg
= NULL
;
620 serial_nr
= bio_blkcg(bio
)->css
.serial_nr
;
623 * Check whether blkcg has changed. The condition may trigger
624 * spuriously on a newly created cic but there's no harm.
626 if (unlikely(!bfqd
) || likely(bic
->blkcg_serial_nr
== serial_nr
))
629 bfqg
= __bfq_bic_change_cgroup(bfqd
, bic
, bio_blkcg(bio
));
631 * Update blkg_path for bfq_log_* functions. We cache this
632 * path, and update it here, for the following
633 * reasons. Operations on blkg objects in blk-cgroup are
634 * protected with the request_queue lock, and not with the
635 * lock that protects the instances of this scheduler
636 * (bfqd->lock). This exposes BFQ to the following sort of
639 * The blkg_lookup performed in bfq_get_queue, protected
640 * through rcu, may happen to return the address of a copy of
641 * the original blkg. If this is the case, then the
642 * bfqg_and_blkg_get performed in bfq_get_queue, to pin down
643 * the blkg, is useless: it does not prevent blk-cgroup code
644 * from destroying both the original blkg and all objects
645 * directly or indirectly referred by the copy of the
648 * On the bright side, destroy operations on a blkg invoke, as
649 * a first step, hooks of the scheduler associated with the
650 * blkg. And these hooks are executed with bfqd->lock held for
651 * BFQ. As a consequence, for any blkg associated with the
652 * request queue this instance of the scheduler is attached
653 * to, we are guaranteed that such a blkg is not destroyed, and
654 * that all the pointers it contains are consistent, while we
655 * are holding bfqd->lock. A blkg_lookup performed with
656 * bfqd->lock held then returns a fully consistent blkg, which
657 * remains consistent until this lock is held.
659 * Thanks to the last fact, and to the fact that: (1) bfqg has
660 * been obtained through a blkg_lookup in the above
661 * assignment, and (2) bfqd->lock is being held, here we can
662 * safely use the policy data for the involved blkg (i.e., the
663 * field bfqg->pd) to get to the blkg associated with bfqg,
664 * and then we can safely use any field of blkg. After we
665 * release bfqd->lock, even just getting blkg through this
666 * bfqg may cause dangling references to be traversed, as
667 * bfqg->pd may not exist any more.
669 * In view of the above facts, here we cache, in the bfqg, any
670 * blkg data we may need for this bic, and for its associated
671 * bfq_queue. As of now, we need to cache only the path of the
672 * blkg, which is used in the bfq_log_* functions.
674 * Finally, note that bfqg itself needs to be protected from
675 * destruction on the blkg_free of the original blkg (which
676 * invokes bfq_pd_free). We use an additional private
677 * refcounter for bfqg, to let it disappear only after no
678 * bfq_queue refers to it any longer.
680 blkg_path(bfqg_to_blkg(bfqg
), bfqg
->blkg_path
, sizeof(bfqg
->blkg_path
));
681 bic
->blkcg_serial_nr
= serial_nr
;
687 * bfq_flush_idle_tree - deactivate any entity on the idle tree of @st.
688 * @st: the service tree being flushed.
690 static void bfq_flush_idle_tree(struct bfq_service_tree
*st
)
692 struct bfq_entity
*entity
= st
->first_idle
;
694 for (; entity
; entity
= st
->first_idle
)
695 __bfq_deactivate_entity(entity
, false);
699 * bfq_reparent_leaf_entity - move leaf entity to the root_group.
700 * @bfqd: the device data structure with the root group.
701 * @entity: the entity to move.
703 static void bfq_reparent_leaf_entity(struct bfq_data
*bfqd
,
704 struct bfq_entity
*entity
)
706 struct bfq_queue
*bfqq
= bfq_entity_to_bfqq(entity
);
708 bfq_bfqq_move(bfqd
, bfqq
, bfqd
->root_group
);
712 * bfq_reparent_active_entities - move to the root group all active
714 * @bfqd: the device data structure with the root group.
715 * @bfqg: the group to move from.
716 * @st: the service tree with the entities.
718 static void bfq_reparent_active_entities(struct bfq_data
*bfqd
,
719 struct bfq_group
*bfqg
,
720 struct bfq_service_tree
*st
)
722 struct rb_root
*active
= &st
->active
;
723 struct bfq_entity
*entity
= NULL
;
725 if (!RB_EMPTY_ROOT(&st
->active
))
726 entity
= bfq_entity_of(rb_first(active
));
728 for (; entity
; entity
= bfq_entity_of(rb_first(active
)))
729 bfq_reparent_leaf_entity(bfqd
, entity
);
731 if (bfqg
->sched_data
.in_service_entity
)
732 bfq_reparent_leaf_entity(bfqd
,
733 bfqg
->sched_data
.in_service_entity
);
737 * bfq_pd_offline - deactivate the entity associated with @pd,
738 * and reparent its children entities.
739 * @pd: descriptor of the policy going offline.
741 * blkio already grabs the queue_lock for us, so no need to use
744 static void bfq_pd_offline(struct blkg_policy_data
*pd
)
746 struct bfq_service_tree
*st
;
747 struct bfq_group
*bfqg
= pd_to_bfqg(pd
);
748 struct bfq_data
*bfqd
= bfqg
->bfqd
;
749 struct bfq_entity
*entity
= bfqg
->my_entity
;
753 spin_lock_irqsave(&bfqd
->lock
, flags
);
755 if (!entity
) /* root group */
756 goto put_async_queues
;
759 * Empty all service_trees belonging to this group before
760 * deactivating the group itself.
762 for (i
= 0; i
< BFQ_IOPRIO_CLASSES
; i
++) {
763 st
= bfqg
->sched_data
.service_tree
+ i
;
766 * The idle tree may still contain bfq_queues belonging
767 * to exited task because they never migrated to a different
768 * cgroup from the one being destroyed now.
770 bfq_flush_idle_tree(st
);
773 * It may happen that some queues are still active
774 * (busy) upon group destruction (if the corresponding
775 * processes have been forced to terminate). We move
776 * all the leaf entities corresponding to these queues
778 * Also, it may happen that the group has an entity
779 * in service, which is disconnected from the active
780 * tree: it must be moved, too.
781 * There is no need to put the sync queues, as the
782 * scheduler has taken no reference.
784 bfq_reparent_active_entities(bfqd
, bfqg
, st
);
787 __bfq_deactivate_entity(entity
, false);
790 bfq_put_async_queues(bfqd
, bfqg
);
792 spin_unlock_irqrestore(&bfqd
->lock
, flags
);
794 * @blkg is going offline and will be ignored by
795 * blkg_[rw]stat_recursive_sum(). Transfer stats to the parent so
796 * that they don't get lost. If IOs complete after this point, the
797 * stats for them will be lost. Oh well...
799 bfqg_stats_xfer_dead(bfqg
);
802 void bfq_end_wr_async(struct bfq_data
*bfqd
)
804 struct blkcg_gq
*blkg
;
806 list_for_each_entry(blkg
, &bfqd
->queue
->blkg_list
, q_node
) {
807 struct bfq_group
*bfqg
= blkg_to_bfqg(blkg
);
809 bfq_end_wr_async_queues(bfqd
, bfqg
);
811 bfq_end_wr_async_queues(bfqd
, bfqd
->root_group
);
814 static int bfq_io_show_weight(struct seq_file
*sf
, void *v
)
816 struct blkcg
*blkcg
= css_to_blkcg(seq_css(sf
));
817 struct bfq_group_data
*bfqgd
= blkcg_to_bfqgd(blkcg
);
818 unsigned int val
= 0;
823 seq_printf(sf
, "%u\n", val
);
828 static int bfq_io_set_weight_legacy(struct cgroup_subsys_state
*css
,
829 struct cftype
*cftype
,
832 struct blkcg
*blkcg
= css_to_blkcg(css
);
833 struct bfq_group_data
*bfqgd
= blkcg_to_bfqgd(blkcg
);
834 struct blkcg_gq
*blkg
;
837 if (val
< BFQ_MIN_WEIGHT
|| val
> BFQ_MAX_WEIGHT
)
841 spin_lock_irq(&blkcg
->lock
);
842 bfqgd
->weight
= (unsigned short)val
;
843 hlist_for_each_entry(blkg
, &blkcg
->blkg_list
, blkcg_node
) {
844 struct bfq_group
*bfqg
= blkg_to_bfqg(blkg
);
849 * Setting the prio_changed flag of the entity
850 * to 1 with new_weight == weight would re-set
851 * the value of the weight to its ioprio mapping.
852 * Set the flag only if necessary.
854 if ((unsigned short)val
!= bfqg
->entity
.new_weight
) {
855 bfqg
->entity
.new_weight
= (unsigned short)val
;
857 * Make sure that the above new value has been
858 * stored in bfqg->entity.new_weight before
859 * setting the prio_changed flag. In fact,
860 * this flag may be read asynchronously (in
861 * critical sections protected by a different
862 * lock than that held here), and finding this
863 * flag set may cause the execution of the code
864 * for updating parameters whose value may
865 * depend also on bfqg->entity.new_weight (in
866 * __bfq_entity_update_weight_prio).
867 * This barrier makes sure that the new value
868 * of bfqg->entity.new_weight is correctly
872 bfqg
->entity
.prio_changed
= 1;
875 spin_unlock_irq(&blkcg
->lock
);
880 static ssize_t
bfq_io_set_weight(struct kernfs_open_file
*of
,
881 char *buf
, size_t nbytes
,
885 /* First unsigned long found in the file is used */
886 int ret
= kstrtoull(strim(buf
), 0, &weight
);
891 ret
= bfq_io_set_weight_legacy(of_css(of
), NULL
, weight
);
892 return ret
?: nbytes
;
895 static int bfqg_print_stat(struct seq_file
*sf
, void *v
)
897 blkcg_print_blkgs(sf
, css_to_blkcg(seq_css(sf
)), blkg_prfill_stat
,
898 &blkcg_policy_bfq
, seq_cft(sf
)->private, false);
902 static int bfqg_print_rwstat(struct seq_file
*sf
, void *v
)
904 blkcg_print_blkgs(sf
, css_to_blkcg(seq_css(sf
)), blkg_prfill_rwstat
,
905 &blkcg_policy_bfq
, seq_cft(sf
)->private, true);
909 static u64
bfqg_prfill_stat_recursive(struct seq_file
*sf
,
910 struct blkg_policy_data
*pd
, int off
)
912 u64 sum
= blkg_stat_recursive_sum(pd_to_blkg(pd
),
913 &blkcg_policy_bfq
, off
);
914 return __blkg_prfill_u64(sf
, pd
, sum
);
917 static u64
bfqg_prfill_rwstat_recursive(struct seq_file
*sf
,
918 struct blkg_policy_data
*pd
, int off
)
920 struct blkg_rwstat sum
= blkg_rwstat_recursive_sum(pd_to_blkg(pd
),
923 return __blkg_prfill_rwstat(sf
, pd
, &sum
);
926 static int bfqg_print_stat_recursive(struct seq_file
*sf
, void *v
)
928 blkcg_print_blkgs(sf
, css_to_blkcg(seq_css(sf
)),
929 bfqg_prfill_stat_recursive
, &blkcg_policy_bfq
,
930 seq_cft(sf
)->private, false);
934 static int bfqg_print_rwstat_recursive(struct seq_file
*sf
, void *v
)
936 blkcg_print_blkgs(sf
, css_to_blkcg(seq_css(sf
)),
937 bfqg_prfill_rwstat_recursive
, &blkcg_policy_bfq
,
938 seq_cft(sf
)->private, true);
942 static u64
bfqg_prfill_sectors(struct seq_file
*sf
, struct blkg_policy_data
*pd
,
945 u64 sum
= blkg_rwstat_total(&pd
->blkg
->stat_bytes
);
947 return __blkg_prfill_u64(sf
, pd
, sum
>> 9);
950 static int bfqg_print_stat_sectors(struct seq_file
*sf
, void *v
)
952 blkcg_print_blkgs(sf
, css_to_blkcg(seq_css(sf
)),
953 bfqg_prfill_sectors
, &blkcg_policy_bfq
, 0, false);
957 static u64
bfqg_prfill_sectors_recursive(struct seq_file
*sf
,
958 struct blkg_policy_data
*pd
, int off
)
960 struct blkg_rwstat tmp
= blkg_rwstat_recursive_sum(pd
->blkg
, NULL
,
961 offsetof(struct blkcg_gq
, stat_bytes
));
962 u64 sum
= atomic64_read(&tmp
.aux_cnt
[BLKG_RWSTAT_READ
]) +
963 atomic64_read(&tmp
.aux_cnt
[BLKG_RWSTAT_WRITE
]);
965 return __blkg_prfill_u64(sf
, pd
, sum
>> 9);
968 static int bfqg_print_stat_sectors_recursive(struct seq_file
*sf
, void *v
)
970 blkcg_print_blkgs(sf
, css_to_blkcg(seq_css(sf
)),
971 bfqg_prfill_sectors_recursive
, &blkcg_policy_bfq
, 0,
976 static u64
bfqg_prfill_avg_queue_size(struct seq_file
*sf
,
977 struct blkg_policy_data
*pd
, int off
)
979 struct bfq_group
*bfqg
= pd_to_bfqg(pd
);
980 u64 samples
= blkg_stat_read(&bfqg
->stats
.avg_queue_size_samples
);
984 v
= blkg_stat_read(&bfqg
->stats
.avg_queue_size_sum
);
985 v
= div64_u64(v
, samples
);
987 __blkg_prfill_u64(sf
, pd
, v
);
991 /* print avg_queue_size */
992 static int bfqg_print_avg_queue_size(struct seq_file
*sf
, void *v
)
994 blkcg_print_blkgs(sf
, css_to_blkcg(seq_css(sf
)),
995 bfqg_prfill_avg_queue_size
, &blkcg_policy_bfq
,
1000 struct bfq_group
*bfq_create_group_hierarchy(struct bfq_data
*bfqd
, int node
)
1004 ret
= blkcg_activate_policy(bfqd
->queue
, &blkcg_policy_bfq
);
1008 return blkg_to_bfqg(bfqd
->queue
->root_blkg
);
1011 struct blkcg_policy blkcg_policy_bfq
= {
1012 .dfl_cftypes
= bfq_blkg_files
,
1013 .legacy_cftypes
= bfq_blkcg_legacy_files
,
1015 .cpd_alloc_fn
= bfq_cpd_alloc
,
1016 .cpd_init_fn
= bfq_cpd_init
,
1017 .cpd_bind_fn
= bfq_cpd_init
,
1018 .cpd_free_fn
= bfq_cpd_free
,
1020 .pd_alloc_fn
= bfq_pd_alloc
,
1021 .pd_init_fn
= bfq_pd_init
,
1022 .pd_offline_fn
= bfq_pd_offline
,
1023 .pd_free_fn
= bfq_pd_free
,
1024 .pd_reset_stats_fn
= bfq_pd_reset_stats
,
1027 struct cftype bfq_blkcg_legacy_files
[] = {
1029 .name
= "bfq.weight",
1030 .flags
= CFTYPE_NOT_ON_ROOT
,
1031 .seq_show
= bfq_io_show_weight
,
1032 .write_u64
= bfq_io_set_weight_legacy
,
1035 /* statistics, covers only the tasks in the bfqg */
1038 .private = offsetof(struct bfq_group
, stats
.time
),
1039 .seq_show
= bfqg_print_stat
,
1042 .name
= "bfq.sectors",
1043 .seq_show
= bfqg_print_stat_sectors
,
1046 .name
= "bfq.io_service_bytes",
1047 .private = (unsigned long)&blkcg_policy_bfq
,
1048 .seq_show
= blkg_print_stat_bytes
,
1051 .name
= "bfq.io_serviced",
1052 .private = (unsigned long)&blkcg_policy_bfq
,
1053 .seq_show
= blkg_print_stat_ios
,
1056 .name
= "bfq.io_service_time",
1057 .private = offsetof(struct bfq_group
, stats
.service_time
),
1058 .seq_show
= bfqg_print_rwstat
,
1061 .name
= "bfq.io_wait_time",
1062 .private = offsetof(struct bfq_group
, stats
.wait_time
),
1063 .seq_show
= bfqg_print_rwstat
,
1066 .name
= "bfq.io_merged",
1067 .private = offsetof(struct bfq_group
, stats
.merged
),
1068 .seq_show
= bfqg_print_rwstat
,
1071 .name
= "bfq.io_queued",
1072 .private = offsetof(struct bfq_group
, stats
.queued
),
1073 .seq_show
= bfqg_print_rwstat
,
1076 /* the same statictics which cover the bfqg and its descendants */
1078 .name
= "bfq.time_recursive",
1079 .private = offsetof(struct bfq_group
, stats
.time
),
1080 .seq_show
= bfqg_print_stat_recursive
,
1083 .name
= "bfq.sectors_recursive",
1084 .seq_show
= bfqg_print_stat_sectors_recursive
,
1087 .name
= "bfq.io_service_bytes_recursive",
1088 .private = (unsigned long)&blkcg_policy_bfq
,
1089 .seq_show
= blkg_print_stat_bytes_recursive
,
1092 .name
= "bfq.io_serviced_recursive",
1093 .private = (unsigned long)&blkcg_policy_bfq
,
1094 .seq_show
= blkg_print_stat_ios_recursive
,
1097 .name
= "bfq.io_service_time_recursive",
1098 .private = offsetof(struct bfq_group
, stats
.service_time
),
1099 .seq_show
= bfqg_print_rwstat_recursive
,
1102 .name
= "bfq.io_wait_time_recursive",
1103 .private = offsetof(struct bfq_group
, stats
.wait_time
),
1104 .seq_show
= bfqg_print_rwstat_recursive
,
1107 .name
= "bfq.io_merged_recursive",
1108 .private = offsetof(struct bfq_group
, stats
.merged
),
1109 .seq_show
= bfqg_print_rwstat_recursive
,
1112 .name
= "bfq.io_queued_recursive",
1113 .private = offsetof(struct bfq_group
, stats
.queued
),
1114 .seq_show
= bfqg_print_rwstat_recursive
,
1117 .name
= "bfq.avg_queue_size",
1118 .seq_show
= bfqg_print_avg_queue_size
,
1121 .name
= "bfq.group_wait_time",
1122 .private = offsetof(struct bfq_group
, stats
.group_wait_time
),
1123 .seq_show
= bfqg_print_stat
,
1126 .name
= "bfq.idle_time",
1127 .private = offsetof(struct bfq_group
, stats
.idle_time
),
1128 .seq_show
= bfqg_print_stat
,
1131 .name
= "bfq.empty_time",
1132 .private = offsetof(struct bfq_group
, stats
.empty_time
),
1133 .seq_show
= bfqg_print_stat
,
1136 .name
= "bfq.dequeue",
1137 .private = offsetof(struct bfq_group
, stats
.dequeue
),
1138 .seq_show
= bfqg_print_stat
,
1143 struct cftype bfq_blkg_files
[] = {
1145 .name
= "bfq.weight",
1146 .flags
= CFTYPE_NOT_ON_ROOT
,
1147 .seq_show
= bfq_io_show_weight
,
1148 .write
= bfq_io_set_weight
,
1153 #else /* CONFIG_BFQ_GROUP_IOSCHED */
1155 void bfqg_stats_update_io_add(struct bfq_group
*bfqg
, struct bfq_queue
*bfqq
,
1156 unsigned int op
) { }
1157 void bfqg_stats_update_io_remove(struct bfq_group
*bfqg
, unsigned int op
) { }
1158 void bfqg_stats_update_io_merged(struct bfq_group
*bfqg
, unsigned int op
) { }
1159 void bfqg_stats_update_completion(struct bfq_group
*bfqg
, uint64_t start_time
,
1160 uint64_t io_start_time
, unsigned int op
) { }
1161 void bfqg_stats_update_dequeue(struct bfq_group
*bfqg
) { }
1162 void bfqg_stats_set_start_empty_time(struct bfq_group
*bfqg
) { }
1163 void bfqg_stats_update_idle_time(struct bfq_group
*bfqg
) { }
1164 void bfqg_stats_set_start_idle_time(struct bfq_group
*bfqg
) { }
1165 void bfqg_stats_update_avg_queue_size(struct bfq_group
*bfqg
) { }
1167 void bfq_bfqq_move(struct bfq_data
*bfqd
, struct bfq_queue
*bfqq
,
1168 struct bfq_group
*bfqg
) {}
1170 void bfq_init_entity(struct bfq_entity
*entity
, struct bfq_group
*bfqg
)
1172 struct bfq_queue
*bfqq
= bfq_entity_to_bfqq(entity
);
1174 entity
->weight
= entity
->new_weight
;
1175 entity
->orig_weight
= entity
->new_weight
;
1177 bfqq
->ioprio
= bfqq
->new_ioprio
;
1178 bfqq
->ioprio_class
= bfqq
->new_ioprio_class
;
1180 entity
->sched_data
= &bfqg
->sched_data
;
1183 void bfq_bic_update_cgroup(struct bfq_io_cq
*bic
, struct bio
*bio
) {}
1185 void bfq_end_wr_async(struct bfq_data
*bfqd
)
1187 bfq_end_wr_async_queues(bfqd
, bfqd
->root_group
);
1190 struct bfq_group
*bfq_find_set_group(struct bfq_data
*bfqd
, struct blkcg
*blkcg
)
1192 return bfqd
->root_group
;
1195 struct bfq_group
*bfqq_group(struct bfq_queue
*bfqq
)
1197 return bfqq
->bfqd
->root_group
;
1200 struct bfq_group
*bfq_create_group_hierarchy(struct bfq_data
*bfqd
, int node
)
1202 struct bfq_group
*bfqg
;
1205 bfqg
= kmalloc_node(sizeof(*bfqg
), GFP_KERNEL
| __GFP_ZERO
, node
);
1209 for (i
= 0; i
< BFQ_IOPRIO_CLASSES
; i
++)
1210 bfqg
->sched_data
.service_tree
[i
] = BFQ_SERVICE_TREE_INIT
;
1214 #endif /* CONFIG_BFQ_GROUP_IOSCHED */