]> git.ipfire.org Git - people/ms/linux.git/blame - block/bfq-cgroup.c
blk-cgroup: move struct blkg_stat to bfq
[people/ms/linux.git] / block / bfq-cgroup.c
CommitLineData
a497ee34 1// SPDX-License-Identifier: GPL-2.0-or-later
ea25da48
PV
2/*
3 * cgroups support for the BFQ I/O scheduler.
ea25da48
PV
4 */
5#include <linux/module.h>
6#include <linux/slab.h>
7#include <linux/blkdev.h>
8#include <linux/cgroup.h>
9#include <linux/elevator.h>
10#include <linux/ktime.h>
11#include <linux/rbtree.h>
12#include <linux/ioprio.h>
13#include <linux/sbitmap.h>
14#include <linux/delay.h>
15
16#include "bfq-iosched.h"
17
a33801e8 18#if defined(CONFIG_BFQ_GROUP_IOSCHED) && defined(CONFIG_DEBUG_BLK_CGROUP)
ea25da48 19
c0ce79dc
CH
20static int bfq_stat_init(struct bfq_stat *stat, gfp_t gfp)
21{
22 int ret;
23
24 ret = percpu_counter_init(&stat->cpu_cnt, 0, gfp);
25 if (ret)
26 return ret;
27
28 atomic64_set(&stat->aux_cnt, 0);
29 return 0;
30}
31
32static void bfq_stat_exit(struct bfq_stat *stat)
33{
34 percpu_counter_destroy(&stat->cpu_cnt);
35}
36
37/**
38 * bfq_stat_add - add a value to a bfq_stat
39 * @stat: target bfq_stat
40 * @val: value to add
41 *
42 * Add @val to @stat. The caller must ensure that IRQ on the same CPU
43 * don't re-enter this function for the same counter.
44 */
45static inline void bfq_stat_add(struct bfq_stat *stat, uint64_t val)
46{
47 percpu_counter_add_batch(&stat->cpu_cnt, val, BLKG_STAT_CPU_BATCH);
48}
49
50/**
51 * bfq_stat_read - read the current value of a bfq_stat
52 * @stat: bfq_stat to read
53 */
54static inline uint64_t bfq_stat_read(struct bfq_stat *stat)
55{
56 return percpu_counter_sum_positive(&stat->cpu_cnt);
57}
58
59/**
60 * bfq_stat_reset - reset a bfq_stat
61 * @stat: bfq_stat to reset
62 */
63static inline void bfq_stat_reset(struct bfq_stat *stat)
64{
65 percpu_counter_set(&stat->cpu_cnt, 0);
66 atomic64_set(&stat->aux_cnt, 0);
67}
68
69/**
70 * bfq_stat_add_aux - add a bfq_stat into another's aux count
71 * @to: the destination bfq_stat
72 * @from: the source
73 *
74 * Add @from's count including the aux one to @to's aux count.
75 */
76static inline void bfq_stat_add_aux(struct bfq_stat *to,
77 struct bfq_stat *from)
78{
79 atomic64_add(bfq_stat_read(from) + atomic64_read(&from->aux_cnt),
80 &to->aux_cnt);
81}
82
83/**
84 * bfq_stat_recursive_sum - collect hierarchical bfq_stat
85 * @blkg: blkg of interest
86 * @pol: blkcg_policy which contains the bfq_stat
87 * @off: offset to the bfq_stat in blkg_policy_data or @blkg
88 *
89 * Collect the bfq_stat specified by @blkg, @pol and @off and all its
90 * online descendants and their aux counts. The caller must be holding the
91 * queue lock for online tests.
92 *
93 * If @pol is NULL, bfq_stat is at @off bytes into @blkg; otherwise, it is
94 * at @off bytes into @blkg's blkg_policy_data of the policy.
95 */
96static u64 bfq_stat_recursive_sum(struct blkcg_gq *blkg,
97 struct blkcg_policy *pol, int off)
98{
99 struct blkcg_gq *pos_blkg;
100 struct cgroup_subsys_state *pos_css;
101 u64 sum = 0;
102
103 lockdep_assert_held(&blkg->q->queue_lock);
104
105 rcu_read_lock();
106 blkg_for_each_descendant_pre(pos_blkg, pos_css, blkg) {
107 struct bfq_stat *stat;
108
109 if (!pos_blkg->online)
110 continue;
111
112 if (pol)
113 stat = (void *)blkg_to_pd(pos_blkg, pol) + off;
114 else
115 stat = (void *)blkg + off;
116
117 sum += bfq_stat_read(stat) + atomic64_read(&stat->aux_cnt);
118 }
119 rcu_read_unlock();
120
121 return sum;
122}
123
124/**
125 * blkg_prfill_stat - prfill callback for bfq_stat
126 * @sf: seq_file to print to
127 * @pd: policy private data of interest
128 * @off: offset to the bfq_stat in @pd
129 *
130 * prfill callback for printing a bfq_stat.
131 */
132static u64 blkg_prfill_stat(struct seq_file *sf, struct blkg_policy_data *pd,
133 int off)
134{
135 return __blkg_prfill_u64(sf, pd, bfq_stat_read((void *)pd + off));
136}
137
ea25da48
PV
138/* bfqg stats flags */
139enum bfqg_stats_flags {
140 BFQG_stats_waiting = 0,
141 BFQG_stats_idling,
142 BFQG_stats_empty,
143};
144
145#define BFQG_FLAG_FNS(name) \
146static void bfqg_stats_mark_##name(struct bfqg_stats *stats) \
147{ \
148 stats->flags |= (1 << BFQG_stats_##name); \
149} \
150static void bfqg_stats_clear_##name(struct bfqg_stats *stats) \
151{ \
152 stats->flags &= ~(1 << BFQG_stats_##name); \
153} \
154static int bfqg_stats_##name(struct bfqg_stats *stats) \
155{ \
156 return (stats->flags & (1 << BFQG_stats_##name)) != 0; \
157} \
158
159BFQG_FLAG_FNS(waiting)
160BFQG_FLAG_FNS(idling)
161BFQG_FLAG_FNS(empty)
162#undef BFQG_FLAG_FNS
163
8f9bebc3 164/* This should be called with the scheduler lock held. */
ea25da48
PV
165static void bfqg_stats_update_group_wait_time(struct bfqg_stats *stats)
166{
84c7afce 167 u64 now;
ea25da48
PV
168
169 if (!bfqg_stats_waiting(stats))
170 return;
171
84c7afce
OS
172 now = ktime_get_ns();
173 if (now > stats->start_group_wait_time)
c0ce79dc 174 bfq_stat_add(&stats->group_wait_time,
ea25da48
PV
175 now - stats->start_group_wait_time);
176 bfqg_stats_clear_waiting(stats);
177}
178
8f9bebc3 179/* This should be called with the scheduler lock held. */
ea25da48
PV
180static void bfqg_stats_set_start_group_wait_time(struct bfq_group *bfqg,
181 struct bfq_group *curr_bfqg)
182{
183 struct bfqg_stats *stats = &bfqg->stats;
184
185 if (bfqg_stats_waiting(stats))
186 return;
187 if (bfqg == curr_bfqg)
188 return;
84c7afce 189 stats->start_group_wait_time = ktime_get_ns();
ea25da48
PV
190 bfqg_stats_mark_waiting(stats);
191}
192
8f9bebc3 193/* This should be called with the scheduler lock held. */
ea25da48
PV
194static void bfqg_stats_end_empty_time(struct bfqg_stats *stats)
195{
84c7afce 196 u64 now;
ea25da48
PV
197
198 if (!bfqg_stats_empty(stats))
199 return;
200
84c7afce
OS
201 now = ktime_get_ns();
202 if (now > stats->start_empty_time)
c0ce79dc 203 bfq_stat_add(&stats->empty_time,
ea25da48
PV
204 now - stats->start_empty_time);
205 bfqg_stats_clear_empty(stats);
206}
207
208void bfqg_stats_update_dequeue(struct bfq_group *bfqg)
209{
c0ce79dc 210 bfq_stat_add(&bfqg->stats.dequeue, 1);
ea25da48
PV
211}
212
213void bfqg_stats_set_start_empty_time(struct bfq_group *bfqg)
214{
215 struct bfqg_stats *stats = &bfqg->stats;
216
217 if (blkg_rwstat_total(&stats->queued))
218 return;
219
220 /*
221 * group is already marked empty. This can happen if bfqq got new
222 * request in parent group and moved to this group while being added
223 * to service tree. Just ignore the event and move on.
224 */
225 if (bfqg_stats_empty(stats))
226 return;
227
84c7afce 228 stats->start_empty_time = ktime_get_ns();
ea25da48
PV
229 bfqg_stats_mark_empty(stats);
230}
231
232void bfqg_stats_update_idle_time(struct bfq_group *bfqg)
233{
234 struct bfqg_stats *stats = &bfqg->stats;
235
236 if (bfqg_stats_idling(stats)) {
84c7afce 237 u64 now = ktime_get_ns();
ea25da48 238
84c7afce 239 if (now > stats->start_idle_time)
c0ce79dc 240 bfq_stat_add(&stats->idle_time,
ea25da48
PV
241 now - stats->start_idle_time);
242 bfqg_stats_clear_idling(stats);
243 }
244}
245
246void bfqg_stats_set_start_idle_time(struct bfq_group *bfqg)
247{
248 struct bfqg_stats *stats = &bfqg->stats;
249
84c7afce 250 stats->start_idle_time = ktime_get_ns();
ea25da48
PV
251 bfqg_stats_mark_idling(stats);
252}
253
254void bfqg_stats_update_avg_queue_size(struct bfq_group *bfqg)
255{
256 struct bfqg_stats *stats = &bfqg->stats;
257
c0ce79dc 258 bfq_stat_add(&stats->avg_queue_size_sum,
ea25da48 259 blkg_rwstat_total(&stats->queued));
c0ce79dc 260 bfq_stat_add(&stats->avg_queue_size_samples, 1);
ea25da48
PV
261 bfqg_stats_update_group_wait_time(stats);
262}
263
a33801e8
LM
264void bfqg_stats_update_io_add(struct bfq_group *bfqg, struct bfq_queue *bfqq,
265 unsigned int op)
266{
267 blkg_rwstat_add(&bfqg->stats.queued, op, 1);
268 bfqg_stats_end_empty_time(&bfqg->stats);
269 if (!(bfqq == ((struct bfq_data *)bfqg->bfqd)->in_service_queue))
270 bfqg_stats_set_start_group_wait_time(bfqg, bfqq_group(bfqq));
271}
272
273void bfqg_stats_update_io_remove(struct bfq_group *bfqg, unsigned int op)
274{
275 blkg_rwstat_add(&bfqg->stats.queued, op, -1);
276}
277
278void bfqg_stats_update_io_merged(struct bfq_group *bfqg, unsigned int op)
279{
280 blkg_rwstat_add(&bfqg->stats.merged, op, 1);
281}
282
84c7afce
OS
283void bfqg_stats_update_completion(struct bfq_group *bfqg, u64 start_time_ns,
284 u64 io_start_time_ns, unsigned int op)
a33801e8
LM
285{
286 struct bfqg_stats *stats = &bfqg->stats;
84c7afce 287 u64 now = ktime_get_ns();
a33801e8 288
84c7afce 289 if (now > io_start_time_ns)
a33801e8 290 blkg_rwstat_add(&stats->service_time, op,
84c7afce
OS
291 now - io_start_time_ns);
292 if (io_start_time_ns > start_time_ns)
a33801e8 293 blkg_rwstat_add(&stats->wait_time, op,
84c7afce 294 io_start_time_ns - start_time_ns);
a33801e8
LM
295}
296
297#else /* CONFIG_BFQ_GROUP_IOSCHED && CONFIG_DEBUG_BLK_CGROUP */
298
299void bfqg_stats_update_io_add(struct bfq_group *bfqg, struct bfq_queue *bfqq,
300 unsigned int op) { }
301void bfqg_stats_update_io_remove(struct bfq_group *bfqg, unsigned int op) { }
302void bfqg_stats_update_io_merged(struct bfq_group *bfqg, unsigned int op) { }
84c7afce
OS
303void bfqg_stats_update_completion(struct bfq_group *bfqg, u64 start_time_ns,
304 u64 io_start_time_ns, unsigned int op) { }
a33801e8
LM
305void bfqg_stats_update_dequeue(struct bfq_group *bfqg) { }
306void bfqg_stats_set_start_empty_time(struct bfq_group *bfqg) { }
307void bfqg_stats_update_idle_time(struct bfq_group *bfqg) { }
308void bfqg_stats_set_start_idle_time(struct bfq_group *bfqg) { }
309void bfqg_stats_update_avg_queue_size(struct bfq_group *bfqg) { }
310
311#endif /* CONFIG_BFQ_GROUP_IOSCHED && CONFIG_DEBUG_BLK_CGROUP */
312
313#ifdef CONFIG_BFQ_GROUP_IOSCHED
314
ea25da48
PV
315/*
316 * blk-cgroup policy-related handlers
317 * The following functions help in converting between blk-cgroup
318 * internal structures and BFQ-specific structures.
319 */
320
321static struct bfq_group *pd_to_bfqg(struct blkg_policy_data *pd)
322{
323 return pd ? container_of(pd, struct bfq_group, pd) : NULL;
324}
325
326struct blkcg_gq *bfqg_to_blkg(struct bfq_group *bfqg)
327{
328 return pd_to_blkg(&bfqg->pd);
329}
330
331static struct bfq_group *blkg_to_bfqg(struct blkcg_gq *blkg)
332{
333 return pd_to_bfqg(blkg_to_pd(blkg, &blkcg_policy_bfq));
334}
335
336/*
337 * bfq_group handlers
338 * The following functions help in navigating the bfq_group hierarchy
339 * by allowing to find the parent of a bfq_group or the bfq_group
340 * associated to a bfq_queue.
341 */
342
343static struct bfq_group *bfqg_parent(struct bfq_group *bfqg)
344{
345 struct blkcg_gq *pblkg = bfqg_to_blkg(bfqg)->parent;
346
347 return pblkg ? blkg_to_bfqg(pblkg) : NULL;
348}
349
350struct bfq_group *bfqq_group(struct bfq_queue *bfqq)
351{
352 struct bfq_entity *group_entity = bfqq->entity.parent;
353
354 return group_entity ? container_of(group_entity, struct bfq_group,
355 entity) :
356 bfqq->bfqd->root_group;
357}
358
359/*
360 * The following two functions handle get and put of a bfq_group by
361 * wrapping the related blk-cgroup hooks.
362 */
363
364static void bfqg_get(struct bfq_group *bfqg)
365{
8f9bebc3 366 bfqg->ref++;
ea25da48
PV
367}
368
dfb79af5 369static void bfqg_put(struct bfq_group *bfqg)
ea25da48 370{
8f9bebc3
PV
371 bfqg->ref--;
372
373 if (bfqg->ref == 0)
374 kfree(bfqg);
375}
376
377static void bfqg_and_blkg_get(struct bfq_group *bfqg)
378{
379 /* see comments in bfq_bic_update_cgroup for why refcounting bfqg */
380 bfqg_get(bfqg);
381
382 blkg_get(bfqg_to_blkg(bfqg));
383}
384
385void bfqg_and_blkg_put(struct bfq_group *bfqg)
386{
8f9bebc3 387 blkg_put(bfqg_to_blkg(bfqg));
d5274b3c
KK
388
389 bfqg_put(bfqg);
ea25da48
PV
390}
391
ea25da48
PV
392/* @stats = 0 */
393static void bfqg_stats_reset(struct bfqg_stats *stats)
394{
a33801e8 395#ifdef CONFIG_DEBUG_BLK_CGROUP
ea25da48
PV
396 /* queued stats shouldn't be cleared */
397 blkg_rwstat_reset(&stats->merged);
398 blkg_rwstat_reset(&stats->service_time);
399 blkg_rwstat_reset(&stats->wait_time);
c0ce79dc
CH
400 bfq_stat_reset(&stats->time);
401 bfq_stat_reset(&stats->avg_queue_size_sum);
402 bfq_stat_reset(&stats->avg_queue_size_samples);
403 bfq_stat_reset(&stats->dequeue);
404 bfq_stat_reset(&stats->group_wait_time);
405 bfq_stat_reset(&stats->idle_time);
406 bfq_stat_reset(&stats->empty_time);
a33801e8 407#endif
ea25da48
PV
408}
409
410/* @to += @from */
411static void bfqg_stats_add_aux(struct bfqg_stats *to, struct bfqg_stats *from)
412{
413 if (!to || !from)
414 return;
415
a33801e8 416#ifdef CONFIG_DEBUG_BLK_CGROUP
ea25da48
PV
417 /* queued stats shouldn't be cleared */
418 blkg_rwstat_add_aux(&to->merged, &from->merged);
419 blkg_rwstat_add_aux(&to->service_time, &from->service_time);
420 blkg_rwstat_add_aux(&to->wait_time, &from->wait_time);
c0ce79dc
CH
421 bfq_stat_add_aux(&from->time, &from->time);
422 bfq_stat_add_aux(&to->avg_queue_size_sum, &from->avg_queue_size_sum);
423 bfq_stat_add_aux(&to->avg_queue_size_samples,
ea25da48 424 &from->avg_queue_size_samples);
c0ce79dc
CH
425 bfq_stat_add_aux(&to->dequeue, &from->dequeue);
426 bfq_stat_add_aux(&to->group_wait_time, &from->group_wait_time);
427 bfq_stat_add_aux(&to->idle_time, &from->idle_time);
428 bfq_stat_add_aux(&to->empty_time, &from->empty_time);
a33801e8 429#endif
ea25da48
PV
430}
431
432/*
433 * Transfer @bfqg's stats to its parent's aux counts so that the ancestors'
434 * recursive stats can still account for the amount used by this bfqg after
435 * it's gone.
436 */
437static void bfqg_stats_xfer_dead(struct bfq_group *bfqg)
438{
439 struct bfq_group *parent;
440
441 if (!bfqg) /* root_group */
442 return;
443
444 parent = bfqg_parent(bfqg);
445
0d945c1f 446 lockdep_assert_held(&bfqg_to_blkg(bfqg)->q->queue_lock);
ea25da48
PV
447
448 if (unlikely(!parent))
449 return;
450
451 bfqg_stats_add_aux(&parent->stats, &bfqg->stats);
452 bfqg_stats_reset(&bfqg->stats);
453}
454
455void bfq_init_entity(struct bfq_entity *entity, struct bfq_group *bfqg)
456{
457 struct bfq_queue *bfqq = bfq_entity_to_bfqq(entity);
458
459 entity->weight = entity->new_weight;
460 entity->orig_weight = entity->new_weight;
461 if (bfqq) {
462 bfqq->ioprio = bfqq->new_ioprio;
463 bfqq->ioprio_class = bfqq->new_ioprio_class;
8f9bebc3
PV
464 /*
465 * Make sure that bfqg and its associated blkg do not
466 * disappear before entity.
467 */
468 bfqg_and_blkg_get(bfqg);
ea25da48
PV
469 }
470 entity->parent = bfqg->my_entity; /* NULL for root group */
471 entity->sched_data = &bfqg->sched_data;
472}
473
474static void bfqg_stats_exit(struct bfqg_stats *stats)
475{
a33801e8 476#ifdef CONFIG_DEBUG_BLK_CGROUP
ea25da48
PV
477 blkg_rwstat_exit(&stats->merged);
478 blkg_rwstat_exit(&stats->service_time);
479 blkg_rwstat_exit(&stats->wait_time);
480 blkg_rwstat_exit(&stats->queued);
c0ce79dc
CH
481 bfq_stat_exit(&stats->time);
482 bfq_stat_exit(&stats->avg_queue_size_sum);
483 bfq_stat_exit(&stats->avg_queue_size_samples);
484 bfq_stat_exit(&stats->dequeue);
485 bfq_stat_exit(&stats->group_wait_time);
486 bfq_stat_exit(&stats->idle_time);
487 bfq_stat_exit(&stats->empty_time);
a33801e8 488#endif
ea25da48
PV
489}
490
491static int bfqg_stats_init(struct bfqg_stats *stats, gfp_t gfp)
492{
a33801e8 493#ifdef CONFIG_DEBUG_BLK_CGROUP
ea25da48
PV
494 if (blkg_rwstat_init(&stats->merged, gfp) ||
495 blkg_rwstat_init(&stats->service_time, gfp) ||
496 blkg_rwstat_init(&stats->wait_time, gfp) ||
497 blkg_rwstat_init(&stats->queued, gfp) ||
c0ce79dc
CH
498 bfq_stat_init(&stats->time, gfp) ||
499 bfq_stat_init(&stats->avg_queue_size_sum, gfp) ||
500 bfq_stat_init(&stats->avg_queue_size_samples, gfp) ||
501 bfq_stat_init(&stats->dequeue, gfp) ||
502 bfq_stat_init(&stats->group_wait_time, gfp) ||
503 bfq_stat_init(&stats->idle_time, gfp) ||
504 bfq_stat_init(&stats->empty_time, gfp)) {
ea25da48
PV
505 bfqg_stats_exit(stats);
506 return -ENOMEM;
507 }
a33801e8 508#endif
ea25da48
PV
509
510 return 0;
511}
512
513static struct bfq_group_data *cpd_to_bfqgd(struct blkcg_policy_data *cpd)
514{
515 return cpd ? container_of(cpd, struct bfq_group_data, pd) : NULL;
516}
517
518static struct bfq_group_data *blkcg_to_bfqgd(struct blkcg *blkcg)
519{
520 return cpd_to_bfqgd(blkcg_to_cpd(blkcg, &blkcg_policy_bfq));
521}
522
dfb79af5 523static struct blkcg_policy_data *bfq_cpd_alloc(gfp_t gfp)
ea25da48
PV
524{
525 struct bfq_group_data *bgd;
526
527 bgd = kzalloc(sizeof(*bgd), gfp);
528 if (!bgd)
529 return NULL;
530 return &bgd->pd;
531}
532
dfb79af5 533static void bfq_cpd_init(struct blkcg_policy_data *cpd)
ea25da48
PV
534{
535 struct bfq_group_data *d = cpd_to_bfqgd(cpd);
536
537 d->weight = cgroup_subsys_on_dfl(io_cgrp_subsys) ?
538 CGROUP_WEIGHT_DFL : BFQ_WEIGHT_LEGACY_DFL;
539}
540
dfb79af5 541static void bfq_cpd_free(struct blkcg_policy_data *cpd)
ea25da48
PV
542{
543 kfree(cpd_to_bfqgd(cpd));
544}
545
dfb79af5 546static struct blkg_policy_data *bfq_pd_alloc(gfp_t gfp, int node)
ea25da48
PV
547{
548 struct bfq_group *bfqg;
549
550 bfqg = kzalloc_node(sizeof(*bfqg), gfp, node);
551 if (!bfqg)
552 return NULL;
553
554 if (bfqg_stats_init(&bfqg->stats, gfp)) {
555 kfree(bfqg);
556 return NULL;
557 }
558
8f9bebc3
PV
559 /* see comments in bfq_bic_update_cgroup for why refcounting */
560 bfqg_get(bfqg);
ea25da48
PV
561 return &bfqg->pd;
562}
563
dfb79af5 564static void bfq_pd_init(struct blkg_policy_data *pd)
ea25da48
PV
565{
566 struct blkcg_gq *blkg = pd_to_blkg(pd);
567 struct bfq_group *bfqg = blkg_to_bfqg(blkg);
568 struct bfq_data *bfqd = blkg->q->elevator->elevator_data;
569 struct bfq_entity *entity = &bfqg->entity;
570 struct bfq_group_data *d = blkcg_to_bfqgd(blkg->blkcg);
571
572 entity->orig_weight = entity->weight = entity->new_weight = d->weight;
573 entity->my_sched_data = &bfqg->sched_data;
574 bfqg->my_entity = entity; /*
575 * the root_group's will be set to NULL
576 * in bfq_init_queue()
577 */
578 bfqg->bfqd = bfqd;
579 bfqg->active_entities = 0;
580 bfqg->rq_pos_tree = RB_ROOT;
581}
582
dfb79af5 583static void bfq_pd_free(struct blkg_policy_data *pd)
ea25da48
PV
584{
585 struct bfq_group *bfqg = pd_to_bfqg(pd);
586
587 bfqg_stats_exit(&bfqg->stats);
8f9bebc3 588 bfqg_put(bfqg);
ea25da48
PV
589}
590
dfb79af5 591static void bfq_pd_reset_stats(struct blkg_policy_data *pd)
ea25da48
PV
592{
593 struct bfq_group *bfqg = pd_to_bfqg(pd);
594
595 bfqg_stats_reset(&bfqg->stats);
596}
597
598static void bfq_group_set_parent(struct bfq_group *bfqg,
599 struct bfq_group *parent)
600{
601 struct bfq_entity *entity;
602
603 entity = &bfqg->entity;
604 entity->parent = parent->my_entity;
605 entity->sched_data = &parent->sched_data;
606}
607
608static struct bfq_group *bfq_lookup_bfqg(struct bfq_data *bfqd,
609 struct blkcg *blkcg)
610{
611 struct blkcg_gq *blkg;
612
613 blkg = blkg_lookup(blkcg, bfqd->queue);
614 if (likely(blkg))
615 return blkg_to_bfqg(blkg);
616 return NULL;
617}
618
619struct bfq_group *bfq_find_set_group(struct bfq_data *bfqd,
620 struct blkcg *blkcg)
621{
622 struct bfq_group *bfqg, *parent;
623 struct bfq_entity *entity;
624
625 bfqg = bfq_lookup_bfqg(bfqd, blkcg);
626
627 if (unlikely(!bfqg))
628 return NULL;
629
630 /*
631 * Update chain of bfq_groups as we might be handling a leaf group
632 * which, along with some of its relatives, has not been hooked yet
633 * to the private hierarchy of BFQ.
634 */
635 entity = &bfqg->entity;
636 for_each_entity(entity) {
637 bfqg = container_of(entity, struct bfq_group, entity);
638 if (bfqg != bfqd->root_group) {
639 parent = bfqg_parent(bfqg);
640 if (!parent)
641 parent = bfqd->root_group;
642 bfq_group_set_parent(bfqg, parent);
643 }
644 }
645
646 return bfqg;
647}
648
649/**
650 * bfq_bfqq_move - migrate @bfqq to @bfqg.
651 * @bfqd: queue descriptor.
652 * @bfqq: the queue to move.
653 * @bfqg: the group to move to.
654 *
655 * Move @bfqq to @bfqg, deactivating it from its old group and reactivating
656 * it on the new one. Avoid putting the entity on the old group idle tree.
657 *
8f9bebc3
PV
658 * Must be called under the scheduler lock, to make sure that the blkg
659 * owning @bfqg does not disappear (see comments in
660 * bfq_bic_update_cgroup on guaranteeing the consistency of blkg
661 * objects).
ea25da48
PV
662 */
663void bfq_bfqq_move(struct bfq_data *bfqd, struct bfq_queue *bfqq,
664 struct bfq_group *bfqg)
665{
666 struct bfq_entity *entity = &bfqq->entity;
667
668 /* If bfqq is empty, then bfq_bfqq_expire also invokes
669 * bfq_del_bfqq_busy, thereby removing bfqq and its entity
670 * from data structures related to current group. Otherwise we
671 * need to remove bfqq explicitly with bfq_deactivate_bfqq, as
672 * we do below.
673 */
674 if (bfqq == bfqd->in_service_queue)
675 bfq_bfqq_expire(bfqd, bfqd->in_service_queue,
676 false, BFQQE_PREEMPTED);
677
678 if (bfq_bfqq_busy(bfqq))
679 bfq_deactivate_bfqq(bfqd, bfqq, false, false);
680 else if (entity->on_st)
681 bfq_put_idle_entity(bfq_entity_service_tree(entity), entity);
8f9bebc3 682 bfqg_and_blkg_put(bfqq_group(bfqq));
ea25da48 683
ea25da48
PV
684 entity->parent = bfqg->my_entity;
685 entity->sched_data = &bfqg->sched_data;
8f9bebc3
PV
686 /* pin down bfqg and its associated blkg */
687 bfqg_and_blkg_get(bfqg);
ea25da48
PV
688
689 if (bfq_bfqq_busy(bfqq)) {
8cacc5ab
PV
690 if (unlikely(!bfqd->nonrot_with_queueing))
691 bfq_pos_tree_add_move(bfqd, bfqq);
ea25da48
PV
692 bfq_activate_bfqq(bfqd, bfqq);
693 }
694
695 if (!bfqd->in_service_queue && !bfqd->rq_in_driver)
696 bfq_schedule_dispatch(bfqd);
697}
698
699/**
700 * __bfq_bic_change_cgroup - move @bic to @cgroup.
701 * @bfqd: the queue descriptor.
702 * @bic: the bic to move.
703 * @blkcg: the blk-cgroup to move to.
704 *
8f9bebc3
PV
705 * Move bic to blkcg, assuming that bfqd->lock is held; which makes
706 * sure that the reference to cgroup is valid across the call (see
707 * comments in bfq_bic_update_cgroup on this issue)
ea25da48
PV
708 *
709 * NOTE: an alternative approach might have been to store the current
710 * cgroup in bfqq and getting a reference to it, reducing the lookup
711 * time here, at the price of slightly more complex code.
712 */
713static struct bfq_group *__bfq_bic_change_cgroup(struct bfq_data *bfqd,
714 struct bfq_io_cq *bic,
715 struct blkcg *blkcg)
716{
717 struct bfq_queue *async_bfqq = bic_to_bfqq(bic, 0);
718 struct bfq_queue *sync_bfqq = bic_to_bfqq(bic, 1);
719 struct bfq_group *bfqg;
720 struct bfq_entity *entity;
721
722 bfqg = bfq_find_set_group(bfqd, blkcg);
723
724 if (unlikely(!bfqg))
725 bfqg = bfqd->root_group;
726
727 if (async_bfqq) {
728 entity = &async_bfqq->entity;
729
730 if (entity->sched_data != &bfqg->sched_data) {
731 bic_set_bfqq(bic, NULL, 0);
732 bfq_log_bfqq(bfqd, async_bfqq,
733 "bic_change_group: %p %d",
734 async_bfqq, async_bfqq->ref);
735 bfq_put_queue(async_bfqq);
736 }
737 }
738
739 if (sync_bfqq) {
740 entity = &sync_bfqq->entity;
741 if (entity->sched_data != &bfqg->sched_data)
742 bfq_bfqq_move(bfqd, sync_bfqq, bfqg);
743 }
744
745 return bfqg;
746}
747
748void bfq_bic_update_cgroup(struct bfq_io_cq *bic, struct bio *bio)
749{
750 struct bfq_data *bfqd = bic_to_bfqd(bic);
751 struct bfq_group *bfqg = NULL;
752 uint64_t serial_nr;
753
754 rcu_read_lock();
0fe061b9 755 serial_nr = __bio_blkcg(bio)->css.serial_nr;
ea25da48
PV
756
757 /*
758 * Check whether blkcg has changed. The condition may trigger
759 * spuriously on a newly created cic but there's no harm.
760 */
761 if (unlikely(!bfqd) || likely(bic->blkcg_serial_nr == serial_nr))
762 goto out;
763
0fe061b9 764 bfqg = __bfq_bic_change_cgroup(bfqd, bic, __bio_blkcg(bio));
8f9bebc3
PV
765 /*
766 * Update blkg_path for bfq_log_* functions. We cache this
767 * path, and update it here, for the following
768 * reasons. Operations on blkg objects in blk-cgroup are
769 * protected with the request_queue lock, and not with the
770 * lock that protects the instances of this scheduler
771 * (bfqd->lock). This exposes BFQ to the following sort of
772 * race.
773 *
774 * The blkg_lookup performed in bfq_get_queue, protected
775 * through rcu, may happen to return the address of a copy of
776 * the original blkg. If this is the case, then the
777 * bfqg_and_blkg_get performed in bfq_get_queue, to pin down
778 * the blkg, is useless: it does not prevent blk-cgroup code
779 * from destroying both the original blkg and all objects
780 * directly or indirectly referred by the copy of the
781 * blkg.
782 *
783 * On the bright side, destroy operations on a blkg invoke, as
784 * a first step, hooks of the scheduler associated with the
785 * blkg. And these hooks are executed with bfqd->lock held for
786 * BFQ. As a consequence, for any blkg associated with the
787 * request queue this instance of the scheduler is attached
788 * to, we are guaranteed that such a blkg is not destroyed, and
789 * that all the pointers it contains are consistent, while we
790 * are holding bfqd->lock. A blkg_lookup performed with
791 * bfqd->lock held then returns a fully consistent blkg, which
792 * remains consistent until this lock is held.
793 *
794 * Thanks to the last fact, and to the fact that: (1) bfqg has
795 * been obtained through a blkg_lookup in the above
796 * assignment, and (2) bfqd->lock is being held, here we can
797 * safely use the policy data for the involved blkg (i.e., the
798 * field bfqg->pd) to get to the blkg associated with bfqg,
799 * and then we can safely use any field of blkg. After we
800 * release bfqd->lock, even just getting blkg through this
801 * bfqg may cause dangling references to be traversed, as
802 * bfqg->pd may not exist any more.
803 *
804 * In view of the above facts, here we cache, in the bfqg, any
805 * blkg data we may need for this bic, and for its associated
806 * bfq_queue. As of now, we need to cache only the path of the
807 * blkg, which is used in the bfq_log_* functions.
808 *
809 * Finally, note that bfqg itself needs to be protected from
810 * destruction on the blkg_free of the original blkg (which
811 * invokes bfq_pd_free). We use an additional private
812 * refcounter for bfqg, to let it disappear only after no
813 * bfq_queue refers to it any longer.
814 */
815 blkg_path(bfqg_to_blkg(bfqg), bfqg->blkg_path, sizeof(bfqg->blkg_path));
ea25da48
PV
816 bic->blkcg_serial_nr = serial_nr;
817out:
818 rcu_read_unlock();
819}
820
821/**
822 * bfq_flush_idle_tree - deactivate any entity on the idle tree of @st.
823 * @st: the service tree being flushed.
824 */
825static void bfq_flush_idle_tree(struct bfq_service_tree *st)
826{
827 struct bfq_entity *entity = st->first_idle;
828
829 for (; entity ; entity = st->first_idle)
830 __bfq_deactivate_entity(entity, false);
831}
832
833/**
834 * bfq_reparent_leaf_entity - move leaf entity to the root_group.
835 * @bfqd: the device data structure with the root group.
836 * @entity: the entity to move.
837 */
838static void bfq_reparent_leaf_entity(struct bfq_data *bfqd,
839 struct bfq_entity *entity)
840{
841 struct bfq_queue *bfqq = bfq_entity_to_bfqq(entity);
842
843 bfq_bfqq_move(bfqd, bfqq, bfqd->root_group);
844}
845
846/**
847 * bfq_reparent_active_entities - move to the root group all active
848 * entities.
849 * @bfqd: the device data structure with the root group.
850 * @bfqg: the group to move from.
851 * @st: the service tree with the entities.
ea25da48
PV
852 */
853static void bfq_reparent_active_entities(struct bfq_data *bfqd,
854 struct bfq_group *bfqg,
855 struct bfq_service_tree *st)
856{
857 struct rb_root *active = &st->active;
858 struct bfq_entity *entity = NULL;
859
860 if (!RB_EMPTY_ROOT(&st->active))
861 entity = bfq_entity_of(rb_first(active));
862
863 for (; entity ; entity = bfq_entity_of(rb_first(active)))
864 bfq_reparent_leaf_entity(bfqd, entity);
865
866 if (bfqg->sched_data.in_service_entity)
867 bfq_reparent_leaf_entity(bfqd,
868 bfqg->sched_data.in_service_entity);
869}
870
871/**
872 * bfq_pd_offline - deactivate the entity associated with @pd,
873 * and reparent its children entities.
874 * @pd: descriptor of the policy going offline.
875 *
876 * blkio already grabs the queue_lock for us, so no need to use
877 * RCU-based magic
878 */
dfb79af5 879static void bfq_pd_offline(struct blkg_policy_data *pd)
ea25da48
PV
880{
881 struct bfq_service_tree *st;
882 struct bfq_group *bfqg = pd_to_bfqg(pd);
883 struct bfq_data *bfqd = bfqg->bfqd;
884 struct bfq_entity *entity = bfqg->my_entity;
885 unsigned long flags;
886 int i;
887
52257ffb
PV
888 spin_lock_irqsave(&bfqd->lock, flags);
889
ea25da48 890 if (!entity) /* root group */
52257ffb 891 goto put_async_queues;
ea25da48 892
ea25da48
PV
893 /*
894 * Empty all service_trees belonging to this group before
895 * deactivating the group itself.
896 */
897 for (i = 0; i < BFQ_IOPRIO_CLASSES; i++) {
898 st = bfqg->sched_data.service_tree + i;
899
900 /*
901 * The idle tree may still contain bfq_queues belonging
902 * to exited task because they never migrated to a different
8f9bebc3 903 * cgroup from the one being destroyed now.
ea25da48
PV
904 */
905 bfq_flush_idle_tree(st);
906
907 /*
908 * It may happen that some queues are still active
909 * (busy) upon group destruction (if the corresponding
910 * processes have been forced to terminate). We move
911 * all the leaf entities corresponding to these queues
912 * to the root_group.
913 * Also, it may happen that the group has an entity
914 * in service, which is disconnected from the active
915 * tree: it must be moved, too.
916 * There is no need to put the sync queues, as the
917 * scheduler has taken no reference.
918 */
919 bfq_reparent_active_entities(bfqd, bfqg, st);
920 }
921
922 __bfq_deactivate_entity(entity, false);
52257ffb
PV
923
924put_async_queues:
ea25da48
PV
925 bfq_put_async_queues(bfqd, bfqg);
926
927 spin_unlock_irqrestore(&bfqd->lock, flags);
928 /*
929 * @blkg is going offline and will be ignored by
930 * blkg_[rw]stat_recursive_sum(). Transfer stats to the parent so
931 * that they don't get lost. If IOs complete after this point, the
932 * stats for them will be lost. Oh well...
933 */
934 bfqg_stats_xfer_dead(bfqg);
935}
936
937void bfq_end_wr_async(struct bfq_data *bfqd)
938{
939 struct blkcg_gq *blkg;
940
941 list_for_each_entry(blkg, &bfqd->queue->blkg_list, q_node) {
942 struct bfq_group *bfqg = blkg_to_bfqg(blkg);
943
944 bfq_end_wr_async_queues(bfqd, bfqg);
945 }
946 bfq_end_wr_async_queues(bfqd, bfqd->root_group);
947}
948
949static int bfq_io_show_weight(struct seq_file *sf, void *v)
950{
951 struct blkcg *blkcg = css_to_blkcg(seq_css(sf));
952 struct bfq_group_data *bfqgd = blkcg_to_bfqgd(blkcg);
953 unsigned int val = 0;
954
955 if (bfqgd)
956 val = bfqgd->weight;
957
958 seq_printf(sf, "%u\n", val);
959
960 return 0;
961}
962
963static int bfq_io_set_weight_legacy(struct cgroup_subsys_state *css,
964 struct cftype *cftype,
965 u64 val)
966{
967 struct blkcg *blkcg = css_to_blkcg(css);
968 struct bfq_group_data *bfqgd = blkcg_to_bfqgd(blkcg);
969 struct blkcg_gq *blkg;
970 int ret = -ERANGE;
971
972 if (val < BFQ_MIN_WEIGHT || val > BFQ_MAX_WEIGHT)
973 return ret;
974
975 ret = 0;
976 spin_lock_irq(&blkcg->lock);
977 bfqgd->weight = (unsigned short)val;
978 hlist_for_each_entry(blkg, &blkcg->blkg_list, blkcg_node) {
979 struct bfq_group *bfqg = blkg_to_bfqg(blkg);
980
981 if (!bfqg)
982 continue;
983 /*
984 * Setting the prio_changed flag of the entity
985 * to 1 with new_weight == weight would re-set
986 * the value of the weight to its ioprio mapping.
987 * Set the flag only if necessary.
988 */
989 if ((unsigned short)val != bfqg->entity.new_weight) {
990 bfqg->entity.new_weight = (unsigned short)val;
991 /*
992 * Make sure that the above new value has been
993 * stored in bfqg->entity.new_weight before
994 * setting the prio_changed flag. In fact,
995 * this flag may be read asynchronously (in
996 * critical sections protected by a different
997 * lock than that held here), and finding this
998 * flag set may cause the execution of the code
999 * for updating parameters whose value may
1000 * depend also on bfqg->entity.new_weight (in
1001 * __bfq_entity_update_weight_prio).
1002 * This barrier makes sure that the new value
1003 * of bfqg->entity.new_weight is correctly
1004 * seen in that code.
1005 */
1006 smp_wmb();
1007 bfqg->entity.prio_changed = 1;
1008 }
1009 }
1010 spin_unlock_irq(&blkcg->lock);
1011
1012 return ret;
1013}
1014
1015static ssize_t bfq_io_set_weight(struct kernfs_open_file *of,
1016 char *buf, size_t nbytes,
1017 loff_t off)
1018{
1019 u64 weight;
1020 /* First unsigned long found in the file is used */
1021 int ret = kstrtoull(strim(buf), 0, &weight);
1022
1023 if (ret)
1024 return ret;
1025
fc8ebd01
MS
1026 ret = bfq_io_set_weight_legacy(of_css(of), NULL, weight);
1027 return ret ?: nbytes;
ea25da48
PV
1028}
1029
a33801e8 1030#ifdef CONFIG_DEBUG_BLK_CGROUP
ea25da48
PV
1031static int bfqg_print_stat(struct seq_file *sf, void *v)
1032{
1033 blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)), blkg_prfill_stat,
1034 &blkcg_policy_bfq, seq_cft(sf)->private, false);
1035 return 0;
1036}
1037
1038static int bfqg_print_rwstat(struct seq_file *sf, void *v)
1039{
1040 blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)), blkg_prfill_rwstat,
1041 &blkcg_policy_bfq, seq_cft(sf)->private, true);
1042 return 0;
1043}
1044
1045static u64 bfqg_prfill_stat_recursive(struct seq_file *sf,
1046 struct blkg_policy_data *pd, int off)
1047{
c0ce79dc 1048 u64 sum = bfq_stat_recursive_sum(pd_to_blkg(pd),
ea25da48
PV
1049 &blkcg_policy_bfq, off);
1050 return __blkg_prfill_u64(sf, pd, sum);
1051}
1052
1053static u64 bfqg_prfill_rwstat_recursive(struct seq_file *sf,
1054 struct blkg_policy_data *pd, int off)
1055{
7af6fd91 1056 struct blkg_rwstat_sample sum;
5d0b6e48
CH
1057
1058 blkg_rwstat_recursive_sum(pd_to_blkg(pd), &blkcg_policy_bfq, off, &sum);
ea25da48
PV
1059 return __blkg_prfill_rwstat(sf, pd, &sum);
1060}
1061
1062static int bfqg_print_stat_recursive(struct seq_file *sf, void *v)
1063{
1064 blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)),
1065 bfqg_prfill_stat_recursive, &blkcg_policy_bfq,
1066 seq_cft(sf)->private, false);
1067 return 0;
1068}
1069
1070static int bfqg_print_rwstat_recursive(struct seq_file *sf, void *v)
1071{
1072 blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)),
1073 bfqg_prfill_rwstat_recursive, &blkcg_policy_bfq,
1074 seq_cft(sf)->private, true);
1075 return 0;
1076}
1077
1078static u64 bfqg_prfill_sectors(struct seq_file *sf, struct blkg_policy_data *pd,
1079 int off)
1080{
1081 u64 sum = blkg_rwstat_total(&pd->blkg->stat_bytes);
1082
1083 return __blkg_prfill_u64(sf, pd, sum >> 9);
1084}
1085
1086static int bfqg_print_stat_sectors(struct seq_file *sf, void *v)
1087{
1088 blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)),
1089 bfqg_prfill_sectors, &blkcg_policy_bfq, 0, false);
1090 return 0;
1091}
1092
1093static u64 bfqg_prfill_sectors_recursive(struct seq_file *sf,
1094 struct blkg_policy_data *pd, int off)
1095{
7af6fd91 1096 struct blkg_rwstat_sample tmp;
5d0b6e48
CH
1097
1098 blkg_rwstat_recursive_sum(pd->blkg, NULL,
1099 offsetof(struct blkcg_gq, stat_bytes), &tmp);
ea25da48 1100
7af6fd91
CH
1101 return __blkg_prfill_u64(sf, pd,
1102 (tmp.cnt[BLKG_RWSTAT_READ] + tmp.cnt[BLKG_RWSTAT_WRITE]) >> 9);
ea25da48
PV
1103}
1104
1105static int bfqg_print_stat_sectors_recursive(struct seq_file *sf, void *v)
1106{
1107 blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)),
1108 bfqg_prfill_sectors_recursive, &blkcg_policy_bfq, 0,
1109 false);
1110 return 0;
1111}
1112
1113static u64 bfqg_prfill_avg_queue_size(struct seq_file *sf,
1114 struct blkg_policy_data *pd, int off)
1115{
1116 struct bfq_group *bfqg = pd_to_bfqg(pd);
c0ce79dc 1117 u64 samples = bfq_stat_read(&bfqg->stats.avg_queue_size_samples);
ea25da48
PV
1118 u64 v = 0;
1119
1120 if (samples) {
c0ce79dc 1121 v = bfq_stat_read(&bfqg->stats.avg_queue_size_sum);
ea25da48
PV
1122 v = div64_u64(v, samples);
1123 }
1124 __blkg_prfill_u64(sf, pd, v);
1125 return 0;
1126}
1127
1128/* print avg_queue_size */
1129static int bfqg_print_avg_queue_size(struct seq_file *sf, void *v)
1130{
1131 blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)),
1132 bfqg_prfill_avg_queue_size, &blkcg_policy_bfq,
1133 0, false);
1134 return 0;
1135}
a33801e8 1136#endif /* CONFIG_DEBUG_BLK_CGROUP */
ea25da48
PV
1137
1138struct bfq_group *bfq_create_group_hierarchy(struct bfq_data *bfqd, int node)
1139{
1140 int ret;
1141
1142 ret = blkcg_activate_policy(bfqd->queue, &blkcg_policy_bfq);
1143 if (ret)
1144 return NULL;
1145
1146 return blkg_to_bfqg(bfqd->queue->root_blkg);
1147}
1148
1149struct blkcg_policy blkcg_policy_bfq = {
1150 .dfl_cftypes = bfq_blkg_files,
1151 .legacy_cftypes = bfq_blkcg_legacy_files,
1152
1153 .cpd_alloc_fn = bfq_cpd_alloc,
1154 .cpd_init_fn = bfq_cpd_init,
1155 .cpd_bind_fn = bfq_cpd_init,
1156 .cpd_free_fn = bfq_cpd_free,
1157
1158 .pd_alloc_fn = bfq_pd_alloc,
1159 .pd_init_fn = bfq_pd_init,
1160 .pd_offline_fn = bfq_pd_offline,
1161 .pd_free_fn = bfq_pd_free,
1162 .pd_reset_stats_fn = bfq_pd_reset_stats,
1163};
1164
1165struct cftype bfq_blkcg_legacy_files[] = {
1166 {
1167 .name = "bfq.weight",
cf892988 1168 .flags = CFTYPE_NOT_ON_ROOT,
ea25da48
PV
1169 .seq_show = bfq_io_show_weight,
1170 .write_u64 = bfq_io_set_weight_legacy,
1171 },
1172
1173 /* statistics, covers only the tasks in the bfqg */
ea25da48
PV
1174 {
1175 .name = "bfq.io_service_bytes",
1176 .private = (unsigned long)&blkcg_policy_bfq,
1177 .seq_show = blkg_print_stat_bytes,
1178 },
1179 {
1180 .name = "bfq.io_serviced",
1181 .private = (unsigned long)&blkcg_policy_bfq,
1182 .seq_show = blkg_print_stat_ios,
1183 },
a33801e8
LM
1184#ifdef CONFIG_DEBUG_BLK_CGROUP
1185 {
1186 .name = "bfq.time",
1187 .private = offsetof(struct bfq_group, stats.time),
1188 .seq_show = bfqg_print_stat,
1189 },
1190 {
1191 .name = "bfq.sectors",
1192 .seq_show = bfqg_print_stat_sectors,
1193 },
ea25da48
PV
1194 {
1195 .name = "bfq.io_service_time",
1196 .private = offsetof(struct bfq_group, stats.service_time),
1197 .seq_show = bfqg_print_rwstat,
1198 },
1199 {
1200 .name = "bfq.io_wait_time",
1201 .private = offsetof(struct bfq_group, stats.wait_time),
1202 .seq_show = bfqg_print_rwstat,
1203 },
1204 {
1205 .name = "bfq.io_merged",
1206 .private = offsetof(struct bfq_group, stats.merged),
1207 .seq_show = bfqg_print_rwstat,
1208 },
1209 {
1210 .name = "bfq.io_queued",
1211 .private = offsetof(struct bfq_group, stats.queued),
1212 .seq_show = bfqg_print_rwstat,
1213 },
a33801e8 1214#endif /* CONFIG_DEBUG_BLK_CGROUP */
ea25da48 1215
636b8fe8 1216 /* the same statistics which cover the bfqg and its descendants */
ea25da48
PV
1217 {
1218 .name = "bfq.io_service_bytes_recursive",
1219 .private = (unsigned long)&blkcg_policy_bfq,
1220 .seq_show = blkg_print_stat_bytes_recursive,
1221 },
1222 {
1223 .name = "bfq.io_serviced_recursive",
1224 .private = (unsigned long)&blkcg_policy_bfq,
1225 .seq_show = blkg_print_stat_ios_recursive,
1226 },
a33801e8
LM
1227#ifdef CONFIG_DEBUG_BLK_CGROUP
1228 {
1229 .name = "bfq.time_recursive",
1230 .private = offsetof(struct bfq_group, stats.time),
1231 .seq_show = bfqg_print_stat_recursive,
1232 },
1233 {
1234 .name = "bfq.sectors_recursive",
1235 .seq_show = bfqg_print_stat_sectors_recursive,
1236 },
ea25da48
PV
1237 {
1238 .name = "bfq.io_service_time_recursive",
1239 .private = offsetof(struct bfq_group, stats.service_time),
1240 .seq_show = bfqg_print_rwstat_recursive,
1241 },
1242 {
1243 .name = "bfq.io_wait_time_recursive",
1244 .private = offsetof(struct bfq_group, stats.wait_time),
1245 .seq_show = bfqg_print_rwstat_recursive,
1246 },
1247 {
1248 .name = "bfq.io_merged_recursive",
1249 .private = offsetof(struct bfq_group, stats.merged),
1250 .seq_show = bfqg_print_rwstat_recursive,
1251 },
1252 {
1253 .name = "bfq.io_queued_recursive",
1254 .private = offsetof(struct bfq_group, stats.queued),
1255 .seq_show = bfqg_print_rwstat_recursive,
1256 },
1257 {
1258 .name = "bfq.avg_queue_size",
1259 .seq_show = bfqg_print_avg_queue_size,
1260 },
1261 {
1262 .name = "bfq.group_wait_time",
1263 .private = offsetof(struct bfq_group, stats.group_wait_time),
1264 .seq_show = bfqg_print_stat,
1265 },
1266 {
1267 .name = "bfq.idle_time",
1268 .private = offsetof(struct bfq_group, stats.idle_time),
1269 .seq_show = bfqg_print_stat,
1270 },
1271 {
1272 .name = "bfq.empty_time",
1273 .private = offsetof(struct bfq_group, stats.empty_time),
1274 .seq_show = bfqg_print_stat,
1275 },
1276 {
1277 .name = "bfq.dequeue",
1278 .private = offsetof(struct bfq_group, stats.dequeue),
1279 .seq_show = bfqg_print_stat,
1280 },
a33801e8 1281#endif /* CONFIG_DEBUG_BLK_CGROUP */
ea25da48
PV
1282 { } /* terminate */
1283};
1284
1285struct cftype bfq_blkg_files[] = {
1286 {
1287 .name = "bfq.weight",
cf892988 1288 .flags = CFTYPE_NOT_ON_ROOT,
ea25da48
PV
1289 .seq_show = bfq_io_show_weight,
1290 .write = bfq_io_set_weight,
1291 },
1292 {} /* terminate */
1293};
1294
1295#else /* CONFIG_BFQ_GROUP_IOSCHED */
1296
ea25da48
PV
1297void bfq_bfqq_move(struct bfq_data *bfqd, struct bfq_queue *bfqq,
1298 struct bfq_group *bfqg) {}
1299
1300void bfq_init_entity(struct bfq_entity *entity, struct bfq_group *bfqg)
1301{
1302 struct bfq_queue *bfqq = bfq_entity_to_bfqq(entity);
1303
1304 entity->weight = entity->new_weight;
1305 entity->orig_weight = entity->new_weight;
1306 if (bfqq) {
1307 bfqq->ioprio = bfqq->new_ioprio;
1308 bfqq->ioprio_class = bfqq->new_ioprio_class;
1309 }
1310 entity->sched_data = &bfqg->sched_data;
1311}
1312
1313void bfq_bic_update_cgroup(struct bfq_io_cq *bic, struct bio *bio) {}
1314
1315void bfq_end_wr_async(struct bfq_data *bfqd)
1316{
1317 bfq_end_wr_async_queues(bfqd, bfqd->root_group);
1318}
1319
1320struct bfq_group *bfq_find_set_group(struct bfq_data *bfqd, struct blkcg *blkcg)
1321{
1322 return bfqd->root_group;
1323}
1324
1325struct bfq_group *bfqq_group(struct bfq_queue *bfqq)
1326{
1327 return bfqq->bfqd->root_group;
1328}
1329
1330struct bfq_group *bfq_create_group_hierarchy(struct bfq_data *bfqd, int node)
1331{
1332 struct bfq_group *bfqg;
1333 int i;
1334
1335 bfqg = kmalloc_node(sizeof(*bfqg), GFP_KERNEL | __GFP_ZERO, node);
1336 if (!bfqg)
1337 return NULL;
1338
1339 for (i = 0; i < BFQ_IOPRIO_CLASSES; i++)
1340 bfqg->sched_data.service_tree[i] = BFQ_SERVICE_TREE_INIT;
1341
1342 return bfqg;
1343}
1344#endif /* CONFIG_BFQ_GROUP_IOSCHED */