]> asedeno.scripts.mit.edu Git - linux.git/blob - block/bfq-cgroup.c
a691dca7e966dd4dde0e38540c15d63037db6ee4
[linux.git] / block / bfq-cgroup.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * cgroups support for the BFQ I/O scheduler.
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
18 #if defined(CONFIG_BFQ_GROUP_IOSCHED) &&  defined(CONFIG_DEBUG_BLK_CGROUP)
19
20 static 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
32 static 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  */
45 static 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  */
54 static 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  */
63 static 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  */
76 static 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  */
96 static 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  */
132 static 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
138 /* bfqg stats flags */
139 enum bfqg_stats_flags {
140         BFQG_stats_waiting = 0,
141         BFQG_stats_idling,
142         BFQG_stats_empty,
143 };
144
145 #define BFQG_FLAG_FNS(name)                                             \
146 static void bfqg_stats_mark_##name(struct bfqg_stats *stats)    \
147 {                                                                       \
148         stats->flags |= (1 << BFQG_stats_##name);                       \
149 }                                                                       \
150 static void bfqg_stats_clear_##name(struct bfqg_stats *stats)   \
151 {                                                                       \
152         stats->flags &= ~(1 << BFQG_stats_##name);                      \
153 }                                                                       \
154 static int bfqg_stats_##name(struct bfqg_stats *stats)          \
155 {                                                                       \
156         return (stats->flags & (1 << BFQG_stats_##name)) != 0;          \
157 }                                                                       \
158
159 BFQG_FLAG_FNS(waiting)
160 BFQG_FLAG_FNS(idling)
161 BFQG_FLAG_FNS(empty)
162 #undef BFQG_FLAG_FNS
163
164 /* This should be called with the scheduler lock held. */
165 static void bfqg_stats_update_group_wait_time(struct bfqg_stats *stats)
166 {
167         u64 now;
168
169         if (!bfqg_stats_waiting(stats))
170                 return;
171
172         now = ktime_get_ns();
173         if (now > stats->start_group_wait_time)
174                 bfq_stat_add(&stats->group_wait_time,
175                               now - stats->start_group_wait_time);
176         bfqg_stats_clear_waiting(stats);
177 }
178
179 /* This should be called with the scheduler lock held. */
180 static 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;
189         stats->start_group_wait_time = ktime_get_ns();
190         bfqg_stats_mark_waiting(stats);
191 }
192
193 /* This should be called with the scheduler lock held. */
194 static void bfqg_stats_end_empty_time(struct bfqg_stats *stats)
195 {
196         u64 now;
197
198         if (!bfqg_stats_empty(stats))
199                 return;
200
201         now = ktime_get_ns();
202         if (now > stats->start_empty_time)
203                 bfq_stat_add(&stats->empty_time,
204                               now - stats->start_empty_time);
205         bfqg_stats_clear_empty(stats);
206 }
207
208 void bfqg_stats_update_dequeue(struct bfq_group *bfqg)
209 {
210         bfq_stat_add(&bfqg->stats.dequeue, 1);
211 }
212
213 void 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
228         stats->start_empty_time = ktime_get_ns();
229         bfqg_stats_mark_empty(stats);
230 }
231
232 void bfqg_stats_update_idle_time(struct bfq_group *bfqg)
233 {
234         struct bfqg_stats *stats = &bfqg->stats;
235
236         if (bfqg_stats_idling(stats)) {
237                 u64 now = ktime_get_ns();
238
239                 if (now > stats->start_idle_time)
240                         bfq_stat_add(&stats->idle_time,
241                                       now - stats->start_idle_time);
242                 bfqg_stats_clear_idling(stats);
243         }
244 }
245
246 void bfqg_stats_set_start_idle_time(struct bfq_group *bfqg)
247 {
248         struct bfqg_stats *stats = &bfqg->stats;
249
250         stats->start_idle_time = ktime_get_ns();
251         bfqg_stats_mark_idling(stats);
252 }
253
254 void bfqg_stats_update_avg_queue_size(struct bfq_group *bfqg)
255 {
256         struct bfqg_stats *stats = &bfqg->stats;
257
258         bfq_stat_add(&stats->avg_queue_size_sum,
259                       blkg_rwstat_total(&stats->queued));
260         bfq_stat_add(&stats->avg_queue_size_samples, 1);
261         bfqg_stats_update_group_wait_time(stats);
262 }
263
264 void 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
273 void bfqg_stats_update_io_remove(struct bfq_group *bfqg, unsigned int op)
274 {
275         blkg_rwstat_add(&bfqg->stats.queued, op, -1);
276 }
277
278 void bfqg_stats_update_io_merged(struct bfq_group *bfqg, unsigned int op)
279 {
280         blkg_rwstat_add(&bfqg->stats.merged, op, 1);
281 }
282
283 void bfqg_stats_update_completion(struct bfq_group *bfqg, u64 start_time_ns,
284                                   u64 io_start_time_ns, unsigned int op)
285 {
286         struct bfqg_stats *stats = &bfqg->stats;
287         u64 now = ktime_get_ns();
288
289         if (now > io_start_time_ns)
290                 blkg_rwstat_add(&stats->service_time, op,
291                                 now - io_start_time_ns);
292         if (io_start_time_ns > start_time_ns)
293                 blkg_rwstat_add(&stats->wait_time, op,
294                                 io_start_time_ns - start_time_ns);
295 }
296
297 #else /* CONFIG_BFQ_GROUP_IOSCHED && CONFIG_DEBUG_BLK_CGROUP */
298
299 void bfqg_stats_update_io_add(struct bfq_group *bfqg, struct bfq_queue *bfqq,
300                               unsigned int op) { }
301 void bfqg_stats_update_io_remove(struct bfq_group *bfqg, unsigned int op) { }
302 void bfqg_stats_update_io_merged(struct bfq_group *bfqg, unsigned int op) { }
303 void bfqg_stats_update_completion(struct bfq_group *bfqg, u64 start_time_ns,
304                                   u64 io_start_time_ns, unsigned int op) { }
305 void bfqg_stats_update_dequeue(struct bfq_group *bfqg) { }
306 void bfqg_stats_set_start_empty_time(struct bfq_group *bfqg) { }
307 void bfqg_stats_update_idle_time(struct bfq_group *bfqg) { }
308 void bfqg_stats_set_start_idle_time(struct bfq_group *bfqg) { }
309 void 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
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
321 static 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
326 struct blkcg_gq *bfqg_to_blkg(struct bfq_group *bfqg)
327 {
328         return pd_to_blkg(&bfqg->pd);
329 }
330
331 static 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
343 static 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
350 struct 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
364 static void bfqg_get(struct bfq_group *bfqg)
365 {
366         bfqg->ref++;
367 }
368
369 static void bfqg_put(struct bfq_group *bfqg)
370 {
371         bfqg->ref--;
372
373         if (bfqg->ref == 0)
374                 kfree(bfqg);
375 }
376
377 static 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
385 void bfqg_and_blkg_put(struct bfq_group *bfqg)
386 {
387         blkg_put(bfqg_to_blkg(bfqg));
388
389         bfqg_put(bfqg);
390 }
391
392 /* @stats = 0 */
393 static void bfqg_stats_reset(struct bfqg_stats *stats)
394 {
395 #ifdef CONFIG_DEBUG_BLK_CGROUP
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);
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);
407 #endif
408 }
409
410 /* @to += @from */
411 static void bfqg_stats_add_aux(struct bfqg_stats *to, struct bfqg_stats *from)
412 {
413         if (!to || !from)
414                 return;
415
416 #ifdef CONFIG_DEBUG_BLK_CGROUP
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);
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,
424                           &from->avg_queue_size_samples);
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);
429 #endif
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  */
437 static 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
446         lockdep_assert_held(&bfqg_to_blkg(bfqg)->q->queue_lock);
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
455 void 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;
464                 /*
465                  * Make sure that bfqg and its associated blkg do not
466                  * disappear before entity.
467                  */
468                 bfqg_and_blkg_get(bfqg);
469         }
470         entity->parent = bfqg->my_entity; /* NULL for root group */
471         entity->sched_data = &bfqg->sched_data;
472 }
473
474 static void bfqg_stats_exit(struct bfqg_stats *stats)
475 {
476 #ifdef CONFIG_DEBUG_BLK_CGROUP
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);
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);
488 #endif
489 }
490
491 static int bfqg_stats_init(struct bfqg_stats *stats, gfp_t gfp)
492 {
493 #ifdef CONFIG_DEBUG_BLK_CGROUP
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) ||
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)) {
505                 bfqg_stats_exit(stats);
506                 return -ENOMEM;
507         }
508 #endif
509
510         return 0;
511 }
512
513 static 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
518 static 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
523 static struct blkcg_policy_data *bfq_cpd_alloc(gfp_t gfp)
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
533 static void bfq_cpd_init(struct blkcg_policy_data *cpd)
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
541 static void bfq_cpd_free(struct blkcg_policy_data *cpd)
542 {
543         kfree(cpd_to_bfqgd(cpd));
544 }
545
546 static struct blkg_policy_data *bfq_pd_alloc(gfp_t gfp, int node)
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
559         /* see comments in bfq_bic_update_cgroup for why refcounting */
560         bfqg_get(bfqg);
561         return &bfqg->pd;
562 }
563
564 static void bfq_pd_init(struct blkg_policy_data *pd)
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
583 static void bfq_pd_free(struct blkg_policy_data *pd)
584 {
585         struct bfq_group *bfqg = pd_to_bfqg(pd);
586
587         bfqg_stats_exit(&bfqg->stats);
588         bfqg_put(bfqg);
589 }
590
591 static void bfq_pd_reset_stats(struct blkg_policy_data *pd)
592 {
593         struct bfq_group *bfqg = pd_to_bfqg(pd);
594
595         bfqg_stats_reset(&bfqg->stats);
596 }
597
598 static 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
608 static 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
619 struct 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  *
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).
662  */
663 void 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);
682         bfqg_and_blkg_put(bfqq_group(bfqq));
683
684         entity->parent = bfqg->my_entity;
685         entity->sched_data = &bfqg->sched_data;
686         /* pin down bfqg and its associated blkg  */
687         bfqg_and_blkg_get(bfqg);
688
689         if (bfq_bfqq_busy(bfqq)) {
690                 if (unlikely(!bfqd->nonrot_with_queueing))
691                         bfq_pos_tree_add_move(bfqd, bfqq);
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  *
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)
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  */
713 static 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
748 void 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();
755         serial_nr = __bio_blkcg(bio)->css.serial_nr;
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
764         bfqg = __bfq_bic_change_cgroup(bfqd, bic, __bio_blkcg(bio));
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));
816         bic->blkcg_serial_nr = serial_nr;
817 out:
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  */
825 static 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  */
838 static 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.
852  */
853 static 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  */
879 static void bfq_pd_offline(struct blkg_policy_data *pd)
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
888         spin_lock_irqsave(&bfqd->lock, flags);
889
890         if (!entity) /* root group */
891                 goto put_async_queues;
892
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
903                  * cgroup from the one being destroyed now.
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);
923
924 put_async_queues:
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
937 void 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
949 static 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
963 static 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
1015 static 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
1026         ret = bfq_io_set_weight_legacy(of_css(of), NULL, weight);
1027         return ret ?: nbytes;
1028 }
1029
1030 #ifdef CONFIG_DEBUG_BLK_CGROUP
1031 static 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
1038 static 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
1045 static u64 bfqg_prfill_stat_recursive(struct seq_file *sf,
1046                                       struct blkg_policy_data *pd, int off)
1047 {
1048         u64 sum = bfq_stat_recursive_sum(pd_to_blkg(pd),
1049                                           &blkcg_policy_bfq, off);
1050         return __blkg_prfill_u64(sf, pd, sum);
1051 }
1052
1053 static u64 bfqg_prfill_rwstat_recursive(struct seq_file *sf,
1054                                         struct blkg_policy_data *pd, int off)
1055 {
1056         struct blkg_rwstat_sample sum;
1057
1058         blkg_rwstat_recursive_sum(pd_to_blkg(pd), &blkcg_policy_bfq, off, &sum);
1059         return __blkg_prfill_rwstat(sf, pd, &sum);
1060 }
1061
1062 static 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
1070 static 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
1078 static 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
1086 static 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
1093 static u64 bfqg_prfill_sectors_recursive(struct seq_file *sf,
1094                                          struct blkg_policy_data *pd, int off)
1095 {
1096         struct blkg_rwstat_sample tmp;
1097
1098         blkg_rwstat_recursive_sum(pd->blkg, NULL,
1099                         offsetof(struct blkcg_gq, stat_bytes), &tmp);
1100
1101         return __blkg_prfill_u64(sf, pd,
1102                 (tmp.cnt[BLKG_RWSTAT_READ] + tmp.cnt[BLKG_RWSTAT_WRITE]) >> 9);
1103 }
1104
1105 static 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
1113 static 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);
1117         u64 samples = bfq_stat_read(&bfqg->stats.avg_queue_size_samples);
1118         u64 v = 0;
1119
1120         if (samples) {
1121                 v = bfq_stat_read(&bfqg->stats.avg_queue_size_sum);
1122                 v = div64_u64(v, samples);
1123         }
1124         __blkg_prfill_u64(sf, pd, v);
1125         return 0;
1126 }
1127
1128 /* print avg_queue_size */
1129 static 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 }
1136 #endif /* CONFIG_DEBUG_BLK_CGROUP */
1137
1138 struct 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
1149 struct 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
1165 struct cftype bfq_blkcg_legacy_files[] = {
1166         {
1167                 .name = "bfq.weight",
1168                 .flags = CFTYPE_NOT_ON_ROOT,
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 */
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         },
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         },
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         },
1214 #endif /* CONFIG_DEBUG_BLK_CGROUP */
1215
1216         /* the same statistics which cover the bfqg and its descendants */
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         },
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         },
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         },
1281 #endif  /* CONFIG_DEBUG_BLK_CGROUP */
1282         { }     /* terminate */
1283 };
1284
1285 struct cftype bfq_blkg_files[] = {
1286         {
1287                 .name = "bfq.weight",
1288                 .flags = CFTYPE_NOT_ON_ROOT,
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
1297 void bfq_bfqq_move(struct bfq_data *bfqd, struct bfq_queue *bfqq,
1298                    struct bfq_group *bfqg) {}
1299
1300 void 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
1313 void bfq_bic_update_cgroup(struct bfq_io_cq *bic, struct bio *bio) {}
1314
1315 void bfq_end_wr_async(struct bfq_data *bfqd)
1316 {
1317         bfq_end_wr_async_queues(bfqd, bfqd->root_group);
1318 }
1319
1320 struct bfq_group *bfq_find_set_group(struct bfq_data *bfqd, struct blkcg *blkcg)
1321 {
1322         return bfqd->root_group;
1323 }
1324
1325 struct bfq_group *bfqq_group(struct bfq_queue *bfqq)
1326 {
1327         return bfqq->bfqd->root_group;
1328 }
1329
1330 struct 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 */