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