]> git.karo-electronics.de Git - karo-tx-linux.git/blob - block/blk-cgroup.c
blkcg: cfq doesn't need per-cpu dispatch stats
[karo-tx-linux.git] / block / blk-cgroup.c
1 /*
2  * Common Block IO controller cgroup interface
3  *
4  * Based on ideas and code from CFQ, CFS and BFQ:
5  * Copyright (C) 2003 Jens Axboe <axboe@kernel.dk>
6  *
7  * Copyright (C) 2008 Fabio Checconi <fabio@gandalf.sssup.it>
8  *                    Paolo Valente <paolo.valente@unimore.it>
9  *
10  * Copyright (C) 2009 Vivek Goyal <vgoyal@redhat.com>
11  *                    Nauman Rafique <nauman@google.com>
12  */
13 #include <linux/ioprio.h>
14 #include <linux/kdev_t.h>
15 #include <linux/module.h>
16 #include <linux/err.h>
17 #include <linux/blkdev.h>
18 #include <linux/slab.h>
19 #include <linux/genhd.h>
20 #include <linux/delay.h>
21 #include <linux/atomic.h>
22 #include "blk-cgroup.h"
23 #include "blk.h"
24
25 #define MAX_KEY_LEN 100
26
27 static DEFINE_SPINLOCK(blkio_list_lock);
28 static LIST_HEAD(blkio_list);
29
30 static DEFINE_MUTEX(all_q_mutex);
31 static LIST_HEAD(all_q_list);
32
33 /* List of groups pending per cpu stats allocation */
34 static DEFINE_SPINLOCK(alloc_list_lock);
35 static LIST_HEAD(alloc_list);
36
37 static void blkio_stat_alloc_fn(struct work_struct *);
38 static DECLARE_DELAYED_WORK(blkio_stat_alloc_work, blkio_stat_alloc_fn);
39
40 struct blkio_cgroup blkio_root_cgroup = { .weight = 2*BLKIO_WEIGHT_DEFAULT };
41 EXPORT_SYMBOL_GPL(blkio_root_cgroup);
42
43 static struct blkio_policy_type *blkio_policy[BLKIO_NR_POLICIES];
44
45 struct blkio_cgroup *cgroup_to_blkio_cgroup(struct cgroup *cgroup)
46 {
47         return container_of(cgroup_subsys_state(cgroup, blkio_subsys_id),
48                             struct blkio_cgroup, css);
49 }
50 EXPORT_SYMBOL_GPL(cgroup_to_blkio_cgroup);
51
52 static struct blkio_cgroup *task_blkio_cgroup(struct task_struct *tsk)
53 {
54         return container_of(task_subsys_state(tsk, blkio_subsys_id),
55                             struct blkio_cgroup, css);
56 }
57
58 struct blkio_cgroup *bio_blkio_cgroup(struct bio *bio)
59 {
60         if (bio && bio->bi_css)
61                 return container_of(bio->bi_css, struct blkio_cgroup, css);
62         return task_blkio_cgroup(current);
63 }
64 EXPORT_SYMBOL_GPL(bio_blkio_cgroup);
65
66 /*
67  * Worker for allocating per cpu stat for blk groups. This is scheduled on
68  * the system_nrt_wq once there are some groups on the alloc_list waiting
69  * for allocation.
70  */
71 static void blkio_stat_alloc_fn(struct work_struct *work)
72 {
73         static void *pcpu_stats[BLKIO_NR_POLICIES];
74         struct delayed_work *dwork = to_delayed_work(work);
75         struct blkio_group *blkg;
76         int i;
77         bool empty = false;
78
79 alloc_stats:
80         for (i = 0; i < BLKIO_NR_POLICIES; i++) {
81                 if (pcpu_stats[i] != NULL)
82                         continue;
83
84                 pcpu_stats[i] = alloc_percpu(struct blkio_group_stats_cpu);
85
86                 /* Allocation failed. Try again after some time. */
87                 if (pcpu_stats[i] == NULL) {
88                         queue_delayed_work(system_nrt_wq, dwork,
89                                                 msecs_to_jiffies(10));
90                         return;
91                 }
92         }
93
94         spin_lock_irq(&blkio_list_lock);
95         spin_lock(&alloc_list_lock);
96
97         /* cgroup got deleted or queue exited. */
98         if (!list_empty(&alloc_list)) {
99                 blkg = list_first_entry(&alloc_list, struct blkio_group,
100                                                 alloc_node);
101                 for (i = 0; i < BLKIO_NR_POLICIES; i++) {
102                         struct blkg_policy_data *pd = blkg->pd[i];
103
104                         if (blkio_policy[i] && pd && !pd->stats_cpu)
105                                 swap(pd->stats_cpu, pcpu_stats[i]);
106                 }
107
108                 list_del_init(&blkg->alloc_node);
109         }
110
111         empty = list_empty(&alloc_list);
112
113         spin_unlock(&alloc_list_lock);
114         spin_unlock_irq(&blkio_list_lock);
115
116         if (!empty)
117                 goto alloc_stats;
118 }
119
120 /**
121  * blkg_free - free a blkg
122  * @blkg: blkg to free
123  *
124  * Free @blkg which may be partially allocated.
125  */
126 static void blkg_free(struct blkio_group *blkg)
127 {
128         int i;
129
130         if (!blkg)
131                 return;
132
133         for (i = 0; i < BLKIO_NR_POLICIES; i++) {
134                 struct blkg_policy_data *pd = blkg->pd[i];
135
136                 if (pd) {
137                         free_percpu(pd->stats_cpu);
138                         kfree(pd);
139                 }
140         }
141
142         kfree(blkg);
143 }
144
145 /**
146  * blkg_alloc - allocate a blkg
147  * @blkcg: block cgroup the new blkg is associated with
148  * @q: request_queue the new blkg is associated with
149  *
150  * Allocate a new blkg assocating @blkcg and @q.
151  */
152 static struct blkio_group *blkg_alloc(struct blkio_cgroup *blkcg,
153                                       struct request_queue *q)
154 {
155         struct blkio_group *blkg;
156         int i;
157
158         /* alloc and init base part */
159         blkg = kzalloc_node(sizeof(*blkg), GFP_ATOMIC, q->node);
160         if (!blkg)
161                 return NULL;
162
163         blkg->q = q;
164         INIT_LIST_HEAD(&blkg->q_node);
165         INIT_LIST_HEAD(&blkg->alloc_node);
166         blkg->blkcg = blkcg;
167         blkg->refcnt = 1;
168         cgroup_path(blkcg->css.cgroup, blkg->path, sizeof(blkg->path));
169
170         for (i = 0; i < BLKIO_NR_POLICIES; i++) {
171                 struct blkio_policy_type *pol = blkio_policy[i];
172                 struct blkg_policy_data *pd;
173
174                 if (!pol)
175                         continue;
176
177                 /* alloc per-policy data and attach it to blkg */
178                 pd = kzalloc_node(sizeof(*pd) + pol->pdata_size, GFP_ATOMIC,
179                                   q->node);
180                 if (!pd) {
181                         blkg_free(blkg);
182                         return NULL;
183                 }
184
185                 blkg->pd[i] = pd;
186                 pd->blkg = blkg;
187         }
188
189         /* invoke per-policy init */
190         for (i = 0; i < BLKIO_NR_POLICIES; i++) {
191                 struct blkio_policy_type *pol = blkio_policy[i];
192
193                 if (pol)
194                         pol->ops.blkio_init_group_fn(blkg);
195         }
196
197         return blkg;
198 }
199
200 struct blkio_group *blkg_lookup_create(struct blkio_cgroup *blkcg,
201                                        struct request_queue *q,
202                                        bool for_root)
203         __releases(q->queue_lock) __acquires(q->queue_lock)
204 {
205         struct blkio_group *blkg;
206
207         WARN_ON_ONCE(!rcu_read_lock_held());
208         lockdep_assert_held(q->queue_lock);
209
210         /*
211          * This could be the first entry point of blkcg implementation and
212          * we shouldn't allow anything to go through for a bypassing queue.
213          * The following can be removed if blkg lookup is guaranteed to
214          * fail on a bypassing queue.
215          */
216         if (unlikely(blk_queue_bypass(q)) && !for_root)
217                 return ERR_PTR(blk_queue_dead(q) ? -EINVAL : -EBUSY);
218
219         blkg = blkg_lookup(blkcg, q);
220         if (blkg)
221                 return blkg;
222
223         /* blkg holds a reference to blkcg */
224         if (!css_tryget(&blkcg->css))
225                 return ERR_PTR(-EINVAL);
226
227         /*
228          * Allocate and initialize.
229          */
230         blkg = blkg_alloc(blkcg, q);
231
232         /* did alloc fail? */
233         if (unlikely(!blkg)) {
234                 blkg = ERR_PTR(-ENOMEM);
235                 goto out;
236         }
237
238         /* insert */
239         spin_lock(&blkcg->lock);
240         hlist_add_head_rcu(&blkg->blkcg_node, &blkcg->blkg_list);
241         list_add(&blkg->q_node, &q->blkg_list);
242         spin_unlock(&blkcg->lock);
243
244         spin_lock(&alloc_list_lock);
245         list_add(&blkg->alloc_node, &alloc_list);
246         /* Queue per cpu stat allocation from worker thread. */
247         queue_delayed_work(system_nrt_wq, &blkio_stat_alloc_work, 0);
248         spin_unlock(&alloc_list_lock);
249 out:
250         return blkg;
251 }
252 EXPORT_SYMBOL_GPL(blkg_lookup_create);
253
254 /* called under rcu_read_lock(). */
255 struct blkio_group *blkg_lookup(struct blkio_cgroup *blkcg,
256                                 struct request_queue *q)
257 {
258         struct blkio_group *blkg;
259         struct hlist_node *n;
260
261         hlist_for_each_entry_rcu(blkg, n, &blkcg->blkg_list, blkcg_node)
262                 if (blkg->q == q)
263                         return blkg;
264         return NULL;
265 }
266 EXPORT_SYMBOL_GPL(blkg_lookup);
267
268 static void blkg_destroy(struct blkio_group *blkg)
269 {
270         struct request_queue *q = blkg->q;
271         struct blkio_cgroup *blkcg = blkg->blkcg;
272
273         lockdep_assert_held(q->queue_lock);
274         lockdep_assert_held(&blkcg->lock);
275
276         /* Something wrong if we are trying to remove same group twice */
277         WARN_ON_ONCE(list_empty(&blkg->q_node));
278         WARN_ON_ONCE(hlist_unhashed(&blkg->blkcg_node));
279         list_del_init(&blkg->q_node);
280         hlist_del_init_rcu(&blkg->blkcg_node);
281
282         spin_lock(&alloc_list_lock);
283         list_del_init(&blkg->alloc_node);
284         spin_unlock(&alloc_list_lock);
285
286         /*
287          * Put the reference taken at the time of creation so that when all
288          * queues are gone, group can be destroyed.
289          */
290         blkg_put(blkg);
291 }
292
293 /*
294  * XXX: This updates blkg policy data in-place for root blkg, which is
295  * necessary across elevator switch and policy registration as root blkgs
296  * aren't shot down.  This broken and racy implementation is temporary.
297  * Eventually, blkg shoot down will be replaced by proper in-place update.
298  */
299 void update_root_blkg_pd(struct request_queue *q, enum blkio_policy_id plid)
300 {
301         struct blkio_policy_type *pol = blkio_policy[plid];
302         struct blkio_group *blkg = blkg_lookup(&blkio_root_cgroup, q);
303         struct blkg_policy_data *pd;
304
305         if (!blkg)
306                 return;
307
308         kfree(blkg->pd[plid]);
309         blkg->pd[plid] = NULL;
310
311         if (!pol)
312                 return;
313
314         pd = kzalloc(sizeof(*pd) + pol->pdata_size, GFP_KERNEL);
315         WARN_ON_ONCE(!pd);
316
317         pd->stats_cpu = alloc_percpu(struct blkio_group_stats_cpu);
318         WARN_ON_ONCE(!pd->stats_cpu);
319
320         blkg->pd[plid] = pd;
321         pd->blkg = blkg;
322         pol->ops.blkio_init_group_fn(blkg);
323 }
324 EXPORT_SYMBOL_GPL(update_root_blkg_pd);
325
326 /**
327  * blkg_destroy_all - destroy all blkgs associated with a request_queue
328  * @q: request_queue of interest
329  * @destroy_root: whether to destroy root blkg or not
330  *
331  * Destroy blkgs associated with @q.  If @destroy_root is %true, all are
332  * destroyed; otherwise, root blkg is left alone.
333  */
334 void blkg_destroy_all(struct request_queue *q, bool destroy_root)
335 {
336         struct blkio_group *blkg, *n;
337
338         spin_lock_irq(q->queue_lock);
339
340         list_for_each_entry_safe(blkg, n, &q->blkg_list, q_node) {
341                 struct blkio_cgroup *blkcg = blkg->blkcg;
342
343                 /* skip root? */
344                 if (!destroy_root && blkg->blkcg == &blkio_root_cgroup)
345                         continue;
346
347                 spin_lock(&blkcg->lock);
348                 blkg_destroy(blkg);
349                 spin_unlock(&blkcg->lock);
350         }
351
352         spin_unlock_irq(q->queue_lock);
353 }
354 EXPORT_SYMBOL_GPL(blkg_destroy_all);
355
356 static void blkg_rcu_free(struct rcu_head *rcu_head)
357 {
358         blkg_free(container_of(rcu_head, struct blkio_group, rcu_head));
359 }
360
361 void __blkg_release(struct blkio_group *blkg)
362 {
363         /* release the extra blkcg reference this blkg has been holding */
364         css_put(&blkg->blkcg->css);
365
366         /*
367          * A group is freed in rcu manner. But having an rcu lock does not
368          * mean that one can access all the fields of blkg and assume these
369          * are valid. For example, don't try to follow throtl_data and
370          * request queue links.
371          *
372          * Having a reference to blkg under an rcu allows acess to only
373          * values local to groups like group stats and group rate limits
374          */
375         call_rcu(&blkg->rcu_head, blkg_rcu_free);
376 }
377 EXPORT_SYMBOL_GPL(__blkg_release);
378
379 static void blkio_reset_stats_cpu(struct blkio_group *blkg, int plid)
380 {
381         struct blkg_policy_data *pd = blkg->pd[plid];
382         int cpu;
383
384         if (pd->stats_cpu == NULL)
385                 return;
386
387         for_each_possible_cpu(cpu) {
388                 struct blkio_group_stats_cpu *sc =
389                         per_cpu_ptr(pd->stats_cpu, cpu);
390
391                 blkg_rwstat_reset(&sc->service_bytes);
392                 blkg_rwstat_reset(&sc->serviced);
393         }
394 }
395
396 static int
397 blkiocg_reset_stats(struct cgroup *cgroup, struct cftype *cftype, u64 val)
398 {
399         struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgroup);
400         struct blkio_group *blkg;
401         struct hlist_node *n;
402
403         spin_lock(&blkio_list_lock);
404         spin_lock_irq(&blkcg->lock);
405
406         /*
407          * Note that stat reset is racy - it doesn't synchronize against
408          * stat updates.  This is a debug feature which shouldn't exist
409          * anyway.  If you get hit by a race, retry.
410          */
411         hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) {
412                 struct blkio_policy_type *pol;
413
414                 list_for_each_entry(pol, &blkio_list, list) {
415                         struct blkg_policy_data *pd = blkg->pd[pol->plid];
416                         struct blkio_group_stats *stats = &pd->stats;
417
418                         /* queued stats shouldn't be cleared */
419                         blkg_rwstat_reset(&stats->service_bytes);
420                         blkg_rwstat_reset(&stats->serviced);
421                         blkg_rwstat_reset(&stats->merged);
422                         blkg_rwstat_reset(&stats->service_time);
423                         blkg_rwstat_reset(&stats->wait_time);
424                         blkg_stat_reset(&stats->time);
425 #ifdef CONFIG_DEBUG_BLK_CGROUP
426                         blkg_stat_reset(&stats->unaccounted_time);
427                         blkg_stat_reset(&stats->avg_queue_size_sum);
428                         blkg_stat_reset(&stats->avg_queue_size_samples);
429                         blkg_stat_reset(&stats->dequeue);
430                         blkg_stat_reset(&stats->group_wait_time);
431                         blkg_stat_reset(&stats->idle_time);
432                         blkg_stat_reset(&stats->empty_time);
433 #endif
434                         blkio_reset_stats_cpu(blkg, pol->plid);
435                 }
436         }
437
438         spin_unlock_irq(&blkcg->lock);
439         spin_unlock(&blkio_list_lock);
440         return 0;
441 }
442
443 static const char *blkg_dev_name(struct blkio_group *blkg)
444 {
445         /* some drivers (floppy) instantiate a queue w/o disk registered */
446         if (blkg->q->backing_dev_info.dev)
447                 return dev_name(blkg->q->backing_dev_info.dev);
448         return NULL;
449 }
450
451 /**
452  * blkcg_print_blkgs - helper for printing per-blkg data
453  * @sf: seq_file to print to
454  * @blkcg: blkcg of interest
455  * @prfill: fill function to print out a blkg
456  * @pol: policy in question
457  * @data: data to be passed to @prfill
458  * @show_total: to print out sum of prfill return values or not
459  *
460  * This function invokes @prfill on each blkg of @blkcg if pd for the
461  * policy specified by @pol exists.  @prfill is invoked with @sf, the
462  * policy data and @data.  If @show_total is %true, the sum of the return
463  * values from @prfill is printed with "Total" label at the end.
464  *
465  * This is to be used to construct print functions for
466  * cftype->read_seq_string method.
467  */
468 void blkcg_print_blkgs(struct seq_file *sf, struct blkio_cgroup *blkcg,
469                        u64 (*prfill)(struct seq_file *, struct blkg_policy_data *, int),
470                        int pol, int data, bool show_total)
471 {
472         struct blkio_group *blkg;
473         struct hlist_node *n;
474         u64 total = 0;
475
476         spin_lock_irq(&blkcg->lock);
477         hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node)
478                 if (blkg->pd[pol])
479                         total += prfill(sf, blkg->pd[pol], data);
480         spin_unlock_irq(&blkcg->lock);
481
482         if (show_total)
483                 seq_printf(sf, "Total %llu\n", (unsigned long long)total);
484 }
485 EXPORT_SYMBOL_GPL(blkcg_print_blkgs);
486
487 /**
488  * __blkg_prfill_u64 - prfill helper for a single u64 value
489  * @sf: seq_file to print to
490  * @pd: policy data of interest
491  * @v: value to print
492  *
493  * Print @v to @sf for the device assocaited with @pd.
494  */
495 u64 __blkg_prfill_u64(struct seq_file *sf, struct blkg_policy_data *pd, u64 v)
496 {
497         const char *dname = blkg_dev_name(pd->blkg);
498
499         if (!dname)
500                 return 0;
501
502         seq_printf(sf, "%s %llu\n", dname, (unsigned long long)v);
503         return v;
504 }
505 EXPORT_SYMBOL_GPL(__blkg_prfill_u64);
506
507 /**
508  * __blkg_prfill_rwstat - prfill helper for a blkg_rwstat
509  * @sf: seq_file to print to
510  * @pd: policy data of interest
511  * @rwstat: rwstat to print
512  *
513  * Print @rwstat to @sf for the device assocaited with @pd.
514  */
515 u64 __blkg_prfill_rwstat(struct seq_file *sf, struct blkg_policy_data *pd,
516                          const struct blkg_rwstat *rwstat)
517 {
518         static const char *rwstr[] = {
519                 [BLKG_RWSTAT_READ]      = "Read",
520                 [BLKG_RWSTAT_WRITE]     = "Write",
521                 [BLKG_RWSTAT_SYNC]      = "Sync",
522                 [BLKG_RWSTAT_ASYNC]     = "Async",
523         };
524         const char *dname = blkg_dev_name(pd->blkg);
525         u64 v;
526         int i;
527
528         if (!dname)
529                 return 0;
530
531         for (i = 0; i < BLKG_RWSTAT_NR; i++)
532                 seq_printf(sf, "%s %s %llu\n", dname, rwstr[i],
533                            (unsigned long long)rwstat->cnt[i]);
534
535         v = rwstat->cnt[BLKG_RWSTAT_READ] + rwstat->cnt[BLKG_RWSTAT_WRITE];
536         seq_printf(sf, "%s Total %llu\n", dname, (unsigned long long)v);
537         return v;
538 }
539
540 static u64 blkg_prfill_stat(struct seq_file *sf, struct blkg_policy_data *pd,
541                             int off)
542 {
543         return __blkg_prfill_u64(sf, pd,
544                                  blkg_stat_read((void *)&pd->stats + off));
545 }
546
547 static u64 blkg_prfill_rwstat(struct seq_file *sf, struct blkg_policy_data *pd,
548                               int off)
549 {
550         struct blkg_rwstat rwstat = blkg_rwstat_read((void *)&pd->stats + off);
551
552         return __blkg_prfill_rwstat(sf, pd, &rwstat);
553 }
554
555 /* print blkg_stat specified by BLKCG_STAT_PRIV() */
556 int blkcg_print_stat(struct cgroup *cgrp, struct cftype *cft,
557                      struct seq_file *sf)
558 {
559         struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgrp);
560
561         blkcg_print_blkgs(sf, blkcg, blkg_prfill_stat,
562                           BLKCG_STAT_POL(cft->private),
563                           BLKCG_STAT_OFF(cft->private), false);
564         return 0;
565 }
566 EXPORT_SYMBOL_GPL(blkcg_print_stat);
567
568 /* print blkg_rwstat specified by BLKCG_STAT_PRIV() */
569 int blkcg_print_rwstat(struct cgroup *cgrp, struct cftype *cft,
570                        struct seq_file *sf)
571 {
572         struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgrp);
573
574         blkcg_print_blkgs(sf, blkcg, blkg_prfill_rwstat,
575                           BLKCG_STAT_POL(cft->private),
576                           BLKCG_STAT_OFF(cft->private), true);
577         return 0;
578 }
579 EXPORT_SYMBOL_GPL(blkcg_print_rwstat);
580
581 /**
582  * blkg_conf_prep - parse and prepare for per-blkg config update
583  * @blkcg: target block cgroup
584  * @input: input string
585  * @ctx: blkg_conf_ctx to be filled
586  *
587  * Parse per-blkg config update from @input and initialize @ctx with the
588  * result.  @ctx->blkg points to the blkg to be updated and @ctx->v the new
589  * value.  This function returns with RCU read locked and must be paired
590  * with blkg_conf_finish().
591  */
592 int blkg_conf_prep(struct blkio_cgroup *blkcg, const char *input,
593                    struct blkg_conf_ctx *ctx)
594         __acquires(rcu)
595 {
596         struct gendisk *disk;
597         struct blkio_group *blkg;
598         unsigned int major, minor;
599         unsigned long long v;
600         int part, ret;
601
602         if (sscanf(input, "%u:%u %llu", &major, &minor, &v) != 3)
603                 return -EINVAL;
604
605         disk = get_gendisk(MKDEV(major, minor), &part);
606         if (!disk || part)
607                 return -EINVAL;
608
609         rcu_read_lock();
610
611         spin_lock_irq(disk->queue->queue_lock);
612         blkg = blkg_lookup_create(blkcg, disk->queue, false);
613         spin_unlock_irq(disk->queue->queue_lock);
614
615         if (IS_ERR(blkg)) {
616                 ret = PTR_ERR(blkg);
617                 rcu_read_unlock();
618                 put_disk(disk);
619                 /*
620                  * If queue was bypassing, we should retry.  Do so after a
621                  * short msleep().  It isn't strictly necessary but queue
622                  * can be bypassing for some time and it's always nice to
623                  * avoid busy looping.
624                  */
625                 if (ret == -EBUSY) {
626                         msleep(10);
627                         ret = restart_syscall();
628                 }
629                 return ret;
630         }
631
632         ctx->disk = disk;
633         ctx->blkg = blkg;
634         ctx->v = v;
635         return 0;
636 }
637 EXPORT_SYMBOL_GPL(blkg_conf_prep);
638
639 /**
640  * blkg_conf_finish - finish up per-blkg config update
641  * @ctx: blkg_conf_ctx intiailized by blkg_conf_prep()
642  *
643  * Finish up after per-blkg config update.  This function must be paired
644  * with blkg_conf_prep().
645  */
646 void blkg_conf_finish(struct blkg_conf_ctx *ctx)
647         __releases(rcu)
648 {
649         rcu_read_unlock();
650         put_disk(ctx->disk);
651 }
652 EXPORT_SYMBOL_GPL(blkg_conf_finish);
653
654 struct cftype blkio_files[] = {
655         {
656                 .name = "reset_stats",
657                 .write_u64 = blkiocg_reset_stats,
658         },
659         { }     /* terminate */
660 };
661
662 /**
663  * blkiocg_pre_destroy - cgroup pre_destroy callback
664  * @cgroup: cgroup of interest
665  *
666  * This function is called when @cgroup is about to go away and responsible
667  * for shooting down all blkgs associated with @cgroup.  blkgs should be
668  * removed while holding both q and blkcg locks.  As blkcg lock is nested
669  * inside q lock, this function performs reverse double lock dancing.
670  *
671  * This is the blkcg counterpart of ioc_release_fn().
672  */
673 static int blkiocg_pre_destroy(struct cgroup *cgroup)
674 {
675         struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgroup);
676
677         spin_lock_irq(&blkcg->lock);
678
679         while (!hlist_empty(&blkcg->blkg_list)) {
680                 struct blkio_group *blkg = hlist_entry(blkcg->blkg_list.first,
681                                                 struct blkio_group, blkcg_node);
682                 struct request_queue *q = blkg->q;
683
684                 if (spin_trylock(q->queue_lock)) {
685                         blkg_destroy(blkg);
686                         spin_unlock(q->queue_lock);
687                 } else {
688                         spin_unlock_irq(&blkcg->lock);
689                         cpu_relax();
690                         spin_lock_irq(&blkcg->lock);
691                 }
692         }
693
694         spin_unlock_irq(&blkcg->lock);
695         return 0;
696 }
697
698 static void blkiocg_destroy(struct cgroup *cgroup)
699 {
700         struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgroup);
701
702         if (blkcg != &blkio_root_cgroup)
703                 kfree(blkcg);
704 }
705
706 static struct cgroup_subsys_state *blkiocg_create(struct cgroup *cgroup)
707 {
708         static atomic64_t id_seq = ATOMIC64_INIT(0);
709         struct blkio_cgroup *blkcg;
710         struct cgroup *parent = cgroup->parent;
711
712         if (!parent) {
713                 blkcg = &blkio_root_cgroup;
714                 goto done;
715         }
716
717         blkcg = kzalloc(sizeof(*blkcg), GFP_KERNEL);
718         if (!blkcg)
719                 return ERR_PTR(-ENOMEM);
720
721         blkcg->weight = BLKIO_WEIGHT_DEFAULT;
722         blkcg->id = atomic64_inc_return(&id_seq); /* root is 0, start from 1 */
723 done:
724         spin_lock_init(&blkcg->lock);
725         INIT_HLIST_HEAD(&blkcg->blkg_list);
726
727         return &blkcg->css;
728 }
729
730 /**
731  * blkcg_init_queue - initialize blkcg part of request queue
732  * @q: request_queue to initialize
733  *
734  * Called from blk_alloc_queue_node(). Responsible for initializing blkcg
735  * part of new request_queue @q.
736  *
737  * RETURNS:
738  * 0 on success, -errno on failure.
739  */
740 int blkcg_init_queue(struct request_queue *q)
741 {
742         int ret;
743
744         might_sleep();
745
746         ret = blk_throtl_init(q);
747         if (ret)
748                 return ret;
749
750         mutex_lock(&all_q_mutex);
751         INIT_LIST_HEAD(&q->all_q_node);
752         list_add_tail(&q->all_q_node, &all_q_list);
753         mutex_unlock(&all_q_mutex);
754
755         return 0;
756 }
757
758 /**
759  * blkcg_drain_queue - drain blkcg part of request_queue
760  * @q: request_queue to drain
761  *
762  * Called from blk_drain_queue().  Responsible for draining blkcg part.
763  */
764 void blkcg_drain_queue(struct request_queue *q)
765 {
766         lockdep_assert_held(q->queue_lock);
767
768         blk_throtl_drain(q);
769 }
770
771 /**
772  * blkcg_exit_queue - exit and release blkcg part of request_queue
773  * @q: request_queue being released
774  *
775  * Called from blk_release_queue().  Responsible for exiting blkcg part.
776  */
777 void blkcg_exit_queue(struct request_queue *q)
778 {
779         mutex_lock(&all_q_mutex);
780         list_del_init(&q->all_q_node);
781         mutex_unlock(&all_q_mutex);
782
783         blkg_destroy_all(q, true);
784
785         blk_throtl_exit(q);
786 }
787
788 /*
789  * We cannot support shared io contexts, as we have no mean to support
790  * two tasks with the same ioc in two different groups without major rework
791  * of the main cic data structures.  For now we allow a task to change
792  * its cgroup only if it's the only owner of its ioc.
793  */
794 static int blkiocg_can_attach(struct cgroup *cgrp, struct cgroup_taskset *tset)
795 {
796         struct task_struct *task;
797         struct io_context *ioc;
798         int ret = 0;
799
800         /* task_lock() is needed to avoid races with exit_io_context() */
801         cgroup_taskset_for_each(task, cgrp, tset) {
802                 task_lock(task);
803                 ioc = task->io_context;
804                 if (ioc && atomic_read(&ioc->nr_tasks) > 1)
805                         ret = -EINVAL;
806                 task_unlock(task);
807                 if (ret)
808                         break;
809         }
810         return ret;
811 }
812
813 static void blkcg_bypass_start(void)
814         __acquires(&all_q_mutex)
815 {
816         struct request_queue *q;
817
818         mutex_lock(&all_q_mutex);
819
820         list_for_each_entry(q, &all_q_list, all_q_node) {
821                 blk_queue_bypass_start(q);
822                 blkg_destroy_all(q, false);
823         }
824 }
825
826 static void blkcg_bypass_end(void)
827         __releases(&all_q_mutex)
828 {
829         struct request_queue *q;
830
831         list_for_each_entry(q, &all_q_list, all_q_node)
832                 blk_queue_bypass_end(q);
833
834         mutex_unlock(&all_q_mutex);
835 }
836
837 struct cgroup_subsys blkio_subsys = {
838         .name = "blkio",
839         .create = blkiocg_create,
840         .can_attach = blkiocg_can_attach,
841         .pre_destroy = blkiocg_pre_destroy,
842         .destroy = blkiocg_destroy,
843         .subsys_id = blkio_subsys_id,
844         .base_cftypes = blkio_files,
845         .module = THIS_MODULE,
846 };
847 EXPORT_SYMBOL_GPL(blkio_subsys);
848
849 void blkio_policy_register(struct blkio_policy_type *blkiop)
850 {
851         struct request_queue *q;
852
853         blkcg_bypass_start();
854         spin_lock(&blkio_list_lock);
855
856         BUG_ON(blkio_policy[blkiop->plid]);
857         blkio_policy[blkiop->plid] = blkiop;
858         list_add_tail(&blkiop->list, &blkio_list);
859
860         spin_unlock(&blkio_list_lock);
861         list_for_each_entry(q, &all_q_list, all_q_node)
862                 update_root_blkg_pd(q, blkiop->plid);
863         blkcg_bypass_end();
864
865         if (blkiop->cftypes)
866                 WARN_ON(cgroup_add_cftypes(&blkio_subsys, blkiop->cftypes));
867 }
868 EXPORT_SYMBOL_GPL(blkio_policy_register);
869
870 void blkio_policy_unregister(struct blkio_policy_type *blkiop)
871 {
872         struct request_queue *q;
873
874         if (blkiop->cftypes)
875                 cgroup_rm_cftypes(&blkio_subsys, blkiop->cftypes);
876
877         blkcg_bypass_start();
878         spin_lock(&blkio_list_lock);
879
880         BUG_ON(blkio_policy[blkiop->plid] != blkiop);
881         blkio_policy[blkiop->plid] = NULL;
882         list_del_init(&blkiop->list);
883
884         spin_unlock(&blkio_list_lock);
885         list_for_each_entry(q, &all_q_list, all_q_node)
886                 update_root_blkg_pd(q, blkiop->plid);
887         blkcg_bypass_end();
888 }
889 EXPORT_SYMBOL_GPL(blkio_policy_unregister);