blob: d6e4555c982fe5bacb54719f1bf442869e9cac6b [file] [log] [blame]
Vivek Goyal31e4c282009-12-03 12:59:42 -05001/*
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>
Vivek Goyal22084192009-12-03 12:59:49 -050014#include <linux/kdev_t.h>
Vivek Goyal9d6a9862009-12-04 10:36:41 -050015#include <linux/module.h>
Stephen Rothwellaccee782009-12-07 19:29:39 +110016#include <linux/err.h>
Divyesh Shah91952912010-04-01 15:01:41 -070017#include <linux/blkdev.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/slab.h>
Gui Jianfeng34d0f172010-04-13 16:05:49 +080019#include <linux/genhd.h>
Tejun Heo72e06c22012-03-05 13:15:00 -080020#include <linux/delay.h>
Tejun Heo9a9e8a22012-03-19 15:10:56 -070021#include <linux/atomic.h>
Tejun Heo72e06c22012-03-05 13:15:00 -080022#include "blk-cgroup.h"
Tejun Heo5efd6112012-03-05 13:15:12 -080023#include "blk.h"
Vivek Goyal3e252062009-12-04 10:36:42 -050024
Divyesh Shah84c124d2010-04-09 08:31:19 +020025#define MAX_KEY_LEN 100
26
Tejun Heobc0d6502012-04-13 13:11:26 -070027static DEFINE_MUTEX(blkcg_pol_mutex);
Tejun Heo923adde2012-03-05 13:15:13 -080028static DEFINE_MUTEX(all_q_mutex);
29static LIST_HEAD(all_q_list);
30
Tejun Heo3381cb82012-04-01 14:38:44 -070031struct blkio_cgroup blkio_root_cgroup = { .cfq_weight = 2 * CFQ_WEIGHT_DEFAULT };
Vivek Goyal9d6a9862009-12-04 10:36:41 -050032EXPORT_SYMBOL_GPL(blkio_root_cgroup);
33
Tejun Heo8bd435b2012-04-13 13:11:28 -070034static struct blkio_policy_type *blkio_policy[BLKCG_MAX_POLS];
Tejun Heo035d10b2012-03-05 13:15:04 -080035
Vivek Goyal31e4c282009-12-03 12:59:42 -050036struct blkio_cgroup *cgroup_to_blkio_cgroup(struct cgroup *cgroup)
37{
38 return container_of(cgroup_subsys_state(cgroup, blkio_subsys_id),
39 struct blkio_cgroup, css);
40}
Vivek Goyal9d6a9862009-12-04 10:36:41 -050041EXPORT_SYMBOL_GPL(cgroup_to_blkio_cgroup);
Vivek Goyal31e4c282009-12-03 12:59:42 -050042
Tejun Heo4f85cb92012-03-05 13:15:28 -080043static struct blkio_cgroup *task_blkio_cgroup(struct task_struct *tsk)
Vivek Goyal70087dc2011-05-16 15:24:08 +020044{
45 return container_of(task_subsys_state(tsk, blkio_subsys_id),
46 struct blkio_cgroup, css);
47}
Tejun Heo4f85cb92012-03-05 13:15:28 -080048
49struct blkio_cgroup *bio_blkio_cgroup(struct bio *bio)
50{
51 if (bio && bio->bi_css)
52 return container_of(bio->bi_css, struct blkio_cgroup, css);
53 return task_blkio_cgroup(current);
54}
55EXPORT_SYMBOL_GPL(bio_blkio_cgroup);
Vivek Goyal70087dc2011-05-16 15:24:08 +020056
Tejun Heo03814112012-03-05 13:15:14 -080057/**
58 * blkg_free - free a blkg
59 * @blkg: blkg to free
60 *
61 * Free @blkg which may be partially allocated.
62 */
63static void blkg_free(struct blkio_group *blkg)
64{
Tejun Heoe8989fa2012-03-05 13:15:20 -080065 int i;
Tejun Heo549d3aa2012-03-05 13:15:16 -080066
67 if (!blkg)
68 return;
69
Tejun Heo8bd435b2012-04-13 13:11:28 -070070 for (i = 0; i < BLKCG_MAX_POLS; i++) {
Tejun Heo9ade5ea2012-04-01 14:38:44 -070071 struct blkio_policy_type *pol = blkio_policy[i];
Tejun Heoe8989fa2012-03-05 13:15:20 -080072 struct blkg_policy_data *pd = blkg->pd[i];
73
Tejun Heo9ade5ea2012-04-01 14:38:44 -070074 if (!pd)
75 continue;
76
77 if (pol && pol->ops.blkio_exit_group_fn)
78 pol->ops.blkio_exit_group_fn(blkg);
79
Tejun Heo9ade5ea2012-04-01 14:38:44 -070080 kfree(pd);
Tejun Heo03814112012-03-05 13:15:14 -080081 }
Tejun Heoe8989fa2012-03-05 13:15:20 -080082
Tejun Heo549d3aa2012-03-05 13:15:16 -080083 kfree(blkg);
Tejun Heo03814112012-03-05 13:15:14 -080084}
85
86/**
87 * blkg_alloc - allocate a blkg
88 * @blkcg: block cgroup the new blkg is associated with
89 * @q: request_queue the new blkg is associated with
Tejun Heo03814112012-03-05 13:15:14 -080090 *
Tejun Heoe8989fa2012-03-05 13:15:20 -080091 * Allocate a new blkg assocating @blkcg and @q.
Tejun Heo03814112012-03-05 13:15:14 -080092 */
93static struct blkio_group *blkg_alloc(struct blkio_cgroup *blkcg,
Tejun Heoe8989fa2012-03-05 13:15:20 -080094 struct request_queue *q)
Tejun Heo03814112012-03-05 13:15:14 -080095{
96 struct blkio_group *blkg;
Tejun Heoe8989fa2012-03-05 13:15:20 -080097 int i;
Tejun Heo03814112012-03-05 13:15:14 -080098
99 /* alloc and init base part */
100 blkg = kzalloc_node(sizeof(*blkg), GFP_ATOMIC, q->node);
101 if (!blkg)
102 return NULL;
103
Tejun Heoc875f4d2012-03-05 13:15:22 -0800104 blkg->q = q;
Tejun Heoe8989fa2012-03-05 13:15:20 -0800105 INIT_LIST_HEAD(&blkg->q_node);
Tejun Heo03814112012-03-05 13:15:14 -0800106 blkg->blkcg = blkcg;
Tejun Heo1adaf3d2012-03-05 13:15:15 -0800107 blkg->refcnt = 1;
Tejun Heo03814112012-03-05 13:15:14 -0800108 cgroup_path(blkcg->css.cgroup, blkg->path, sizeof(blkg->path));
109
Tejun Heo8bd435b2012-04-13 13:11:28 -0700110 for (i = 0; i < BLKCG_MAX_POLS; i++) {
Tejun Heoe8989fa2012-03-05 13:15:20 -0800111 struct blkio_policy_type *pol = blkio_policy[i];
112 struct blkg_policy_data *pd;
Tejun Heo03814112012-03-05 13:15:14 -0800113
Tejun Heoe8989fa2012-03-05 13:15:20 -0800114 if (!pol)
115 continue;
Tejun Heo549d3aa2012-03-05 13:15:16 -0800116
Tejun Heoe8989fa2012-03-05 13:15:20 -0800117 /* alloc per-policy data and attach it to blkg */
118 pd = kzalloc_node(sizeof(*pd) + pol->pdata_size, GFP_ATOMIC,
119 q->node);
120 if (!pd) {
121 blkg_free(blkg);
122 return NULL;
123 }
Tejun Heo549d3aa2012-03-05 13:15:16 -0800124
Tejun Heoe8989fa2012-03-05 13:15:20 -0800125 blkg->pd[i] = pd;
126 pd->blkg = blkg;
Tejun Heo03814112012-03-05 13:15:14 -0800127 }
128
Tejun Heo549d3aa2012-03-05 13:15:16 -0800129 /* invoke per-policy init */
Tejun Heo8bd435b2012-04-13 13:11:28 -0700130 for (i = 0; i < BLKCG_MAX_POLS; i++) {
Tejun Heoe8989fa2012-03-05 13:15:20 -0800131 struct blkio_policy_type *pol = blkio_policy[i];
132
133 if (pol)
134 pol->ops.blkio_init_group_fn(blkg);
135 }
136
Tejun Heo03814112012-03-05 13:15:14 -0800137 return blkg;
138}
139
Tejun Heo80fd9972012-04-13 14:50:53 -0700140static struct blkio_group *__blkg_lookup(struct blkio_cgroup *blkcg,
141 struct request_queue *q)
142{
143 struct blkio_group *blkg;
144 struct hlist_node *n;
145
146 hlist_for_each_entry_rcu(blkg, n, &blkcg->blkg_list, blkcg_node)
147 if (blkg->q == q)
148 return blkg;
149 return NULL;
150}
151
152/**
153 * blkg_lookup - lookup blkg for the specified blkcg - q pair
154 * @blkcg: blkcg of interest
155 * @q: request_queue of interest
156 *
157 * Lookup blkg for the @blkcg - @q pair. This function should be called
158 * under RCU read lock and is guaranteed to return %NULL if @q is bypassing
159 * - see blk_queue_bypass_start() for details.
160 */
161struct blkio_group *blkg_lookup(struct blkio_cgroup *blkcg,
162 struct request_queue *q)
163{
164 WARN_ON_ONCE(!rcu_read_lock_held());
165
166 if (unlikely(blk_queue_bypass(q)))
167 return NULL;
168 return __blkg_lookup(blkcg, q);
169}
170EXPORT_SYMBOL_GPL(blkg_lookup);
171
Tejun Heocd1604f2012-03-05 13:15:06 -0800172struct blkio_group *blkg_lookup_create(struct blkio_cgroup *blkcg,
173 struct request_queue *q,
Tejun Heocd1604f2012-03-05 13:15:06 -0800174 bool for_root)
175 __releases(q->queue_lock) __acquires(q->queue_lock)
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400176{
Vivek Goyal1cd9e032012-03-08 10:53:56 -0800177 struct blkio_group *blkg;
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400178
Tejun Heocd1604f2012-03-05 13:15:06 -0800179 WARN_ON_ONCE(!rcu_read_lock_held());
180 lockdep_assert_held(q->queue_lock);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500181
Tejun Heocd1604f2012-03-05 13:15:06 -0800182 /*
183 * This could be the first entry point of blkcg implementation and
184 * we shouldn't allow anything to go through for a bypassing queue.
Tejun Heocd1604f2012-03-05 13:15:06 -0800185 */
186 if (unlikely(blk_queue_bypass(q)) && !for_root)
187 return ERR_PTR(blk_queue_dead(q) ? -EINVAL : -EBUSY);
188
Tejun Heo80fd9972012-04-13 14:50:53 -0700189 blkg = __blkg_lookup(blkcg, q);
Tejun Heocd1604f2012-03-05 13:15:06 -0800190 if (blkg)
191 return blkg;
192
Tejun Heo7ee9c562012-03-05 13:15:11 -0800193 /* blkg holds a reference to blkcg */
Tejun Heocd1604f2012-03-05 13:15:06 -0800194 if (!css_tryget(&blkcg->css))
195 return ERR_PTR(-EINVAL);
196
197 /*
198 * Allocate and initialize.
Tejun Heocd1604f2012-03-05 13:15:06 -0800199 */
Vivek Goyal1cd9e032012-03-08 10:53:56 -0800200 blkg = blkg_alloc(blkcg, q);
Tejun Heocd1604f2012-03-05 13:15:06 -0800201
202 /* did alloc fail? */
Vivek Goyal1cd9e032012-03-08 10:53:56 -0800203 if (unlikely(!blkg)) {
Tejun Heocd1604f2012-03-05 13:15:06 -0800204 blkg = ERR_PTR(-ENOMEM);
205 goto out;
206 }
207
208 /* insert */
209 spin_lock(&blkcg->lock);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500210 hlist_add_head_rcu(&blkg->blkcg_node, &blkcg->blkg_list);
Tejun Heoe8989fa2012-03-05 13:15:20 -0800211 list_add(&blkg->q_node, &q->blkg_list);
Tejun Heocd1604f2012-03-05 13:15:06 -0800212 spin_unlock(&blkcg->lock);
213out:
Tejun Heocd1604f2012-03-05 13:15:06 -0800214 return blkg;
Vivek Goyal31e4c282009-12-03 12:59:42 -0500215}
Tejun Heocd1604f2012-03-05 13:15:06 -0800216EXPORT_SYMBOL_GPL(blkg_lookup_create);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500217
Tejun Heoe8989fa2012-03-05 13:15:20 -0800218static void blkg_destroy(struct blkio_group *blkg)
Tejun Heo72e06c22012-03-05 13:15:00 -0800219{
Tejun Heo03aa264a2012-03-05 13:15:19 -0800220 struct request_queue *q = blkg->q;
Tejun Heo9f13ef62012-03-05 13:15:21 -0800221 struct blkio_cgroup *blkcg = blkg->blkcg;
Tejun Heo03aa264a2012-03-05 13:15:19 -0800222
223 lockdep_assert_held(q->queue_lock);
Tejun Heo9f13ef62012-03-05 13:15:21 -0800224 lockdep_assert_held(&blkcg->lock);
Tejun Heo03aa264a2012-03-05 13:15:19 -0800225
226 /* Something wrong if we are trying to remove same group twice */
Tejun Heoe8989fa2012-03-05 13:15:20 -0800227 WARN_ON_ONCE(list_empty(&blkg->q_node));
Tejun Heo9f13ef62012-03-05 13:15:21 -0800228 WARN_ON_ONCE(hlist_unhashed(&blkg->blkcg_node));
Tejun Heoe8989fa2012-03-05 13:15:20 -0800229 list_del_init(&blkg->q_node);
Tejun Heo9f13ef62012-03-05 13:15:21 -0800230 hlist_del_init_rcu(&blkg->blkcg_node);
Tejun Heo03aa264a2012-03-05 13:15:19 -0800231
Tejun Heo03aa264a2012-03-05 13:15:19 -0800232 /*
233 * Put the reference taken at the time of creation so that when all
234 * queues are gone, group can be destroyed.
235 */
236 blkg_put(blkg);
237}
238
Tejun Heoe8989fa2012-03-05 13:15:20 -0800239/*
240 * XXX: This updates blkg policy data in-place for root blkg, which is
241 * necessary across elevator switch and policy registration as root blkgs
242 * aren't shot down. This broken and racy implementation is temporary.
243 * Eventually, blkg shoot down will be replaced by proper in-place update.
244 */
Tejun Heoec399342012-04-13 13:11:27 -0700245void update_root_blkg_pd(struct request_queue *q,
246 const struct blkio_policy_type *pol)
Tejun Heoe8989fa2012-03-05 13:15:20 -0800247{
Tejun Heoe8989fa2012-03-05 13:15:20 -0800248 struct blkio_group *blkg = blkg_lookup(&blkio_root_cgroup, q);
249 struct blkg_policy_data *pd;
250
251 if (!blkg)
252 return;
253
Tejun Heoec399342012-04-13 13:11:27 -0700254 kfree(blkg->pd[pol->plid]);
255 blkg->pd[pol->plid] = NULL;
Tejun Heoe8989fa2012-03-05 13:15:20 -0800256
257 if (!pol)
258 return;
259
260 pd = kzalloc(sizeof(*pd) + pol->pdata_size, GFP_KERNEL);
261 WARN_ON_ONCE(!pd);
262
Tejun Heoec399342012-04-13 13:11:27 -0700263 blkg->pd[pol->plid] = pd;
Tejun Heoe8989fa2012-03-05 13:15:20 -0800264 pd->blkg = blkg;
265 pol->ops.blkio_init_group_fn(blkg);
266}
267EXPORT_SYMBOL_GPL(update_root_blkg_pd);
268
Tejun Heo9f13ef62012-03-05 13:15:21 -0800269/**
270 * blkg_destroy_all - destroy all blkgs associated with a request_queue
271 * @q: request_queue of interest
272 * @destroy_root: whether to destroy root blkg or not
273 *
274 * Destroy blkgs associated with @q. If @destroy_root is %true, all are
275 * destroyed; otherwise, root blkg is left alone.
276 */
Tejun Heoe8989fa2012-03-05 13:15:20 -0800277void blkg_destroy_all(struct request_queue *q, bool destroy_root)
Tejun Heo03aa264a2012-03-05 13:15:19 -0800278{
279 struct blkio_group *blkg, *n;
Tejun Heo72e06c22012-03-05 13:15:00 -0800280
Tejun Heo9f13ef62012-03-05 13:15:21 -0800281 spin_lock_irq(q->queue_lock);
Tejun Heo72e06c22012-03-05 13:15:00 -0800282
Tejun Heo9f13ef62012-03-05 13:15:21 -0800283 list_for_each_entry_safe(blkg, n, &q->blkg_list, q_node) {
284 struct blkio_cgroup *blkcg = blkg->blkcg;
Tejun Heo72e06c22012-03-05 13:15:00 -0800285
Tejun Heo9f13ef62012-03-05 13:15:21 -0800286 /* skip root? */
287 if (!destroy_root && blkg->blkcg == &blkio_root_cgroup)
288 continue;
Tejun Heo03aa264a2012-03-05 13:15:19 -0800289
Tejun Heo9f13ef62012-03-05 13:15:21 -0800290 spin_lock(&blkcg->lock);
291 blkg_destroy(blkg);
292 spin_unlock(&blkcg->lock);
Tejun Heo72e06c22012-03-05 13:15:00 -0800293 }
Tejun Heo9f13ef62012-03-05 13:15:21 -0800294
295 spin_unlock_irq(q->queue_lock);
Tejun Heo72e06c22012-03-05 13:15:00 -0800296}
Tejun Heo03aa264a2012-03-05 13:15:19 -0800297EXPORT_SYMBOL_GPL(blkg_destroy_all);
Tejun Heo72e06c22012-03-05 13:15:00 -0800298
Tejun Heo1adaf3d2012-03-05 13:15:15 -0800299static void blkg_rcu_free(struct rcu_head *rcu_head)
300{
301 blkg_free(container_of(rcu_head, struct blkio_group, rcu_head));
302}
303
304void __blkg_release(struct blkio_group *blkg)
305{
306 /* release the extra blkcg reference this blkg has been holding */
307 css_put(&blkg->blkcg->css);
308
309 /*
310 * A group is freed in rcu manner. But having an rcu lock does not
311 * mean that one can access all the fields of blkg and assume these
312 * are valid. For example, don't try to follow throtl_data and
313 * request queue links.
314 *
315 * Having a reference to blkg under an rcu allows acess to only
316 * values local to groups like group stats and group rate limits
317 */
318 call_rcu(&blkg->rcu_head, blkg_rcu_free);
319}
320EXPORT_SYMBOL_GPL(__blkg_release);
321
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700322static int
Divyesh Shah84c124d2010-04-09 08:31:19 +0200323blkiocg_reset_stats(struct cgroup *cgroup, struct cftype *cftype, u64 val)
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700324{
Tejun Heo997a0262012-03-08 10:53:58 -0800325 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgroup);
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700326 struct blkio_group *blkg;
327 struct hlist_node *n;
Tejun Heobc0d6502012-04-13 13:11:26 -0700328 int i;
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700329
Tejun Heobc0d6502012-04-13 13:11:26 -0700330 mutex_lock(&blkcg_pol_mutex);
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700331 spin_lock_irq(&blkcg->lock);
Tejun Heo997a0262012-03-08 10:53:58 -0800332
333 /*
334 * Note that stat reset is racy - it doesn't synchronize against
335 * stat updates. This is a debug feature which shouldn't exist
336 * anyway. If you get hit by a race, retry.
337 */
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700338 hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) {
Tejun Heo8bd435b2012-04-13 13:11:28 -0700339 for (i = 0; i < BLKCG_MAX_POLS; i++) {
Tejun Heobc0d6502012-04-13 13:11:26 -0700340 struct blkio_policy_type *pol = blkio_policy[i];
Tejun Heo549d3aa2012-03-05 13:15:16 -0800341
Tejun Heobc0d6502012-04-13 13:11:26 -0700342 if (pol && pol->ops.blkio_reset_group_stats_fn)
Tejun Heo9ade5ea2012-04-01 14:38:44 -0700343 pol->ops.blkio_reset_group_stats_fn(blkg);
Tejun Heobc0d6502012-04-13 13:11:26 -0700344 }
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700345 }
Vivek Goyalf0bdc8c2011-05-19 15:38:30 -0400346
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700347 spin_unlock_irq(&blkcg->lock);
Tejun Heobc0d6502012-04-13 13:11:26 -0700348 mutex_unlock(&blkcg_pol_mutex);
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700349 return 0;
350}
351
Tejun Heod3d32e692012-04-01 14:38:42 -0700352static const char *blkg_dev_name(struct blkio_group *blkg)
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700353{
Tejun Heod3d32e692012-04-01 14:38:42 -0700354 /* some drivers (floppy) instantiate a queue w/o disk registered */
355 if (blkg->q->backing_dev_info.dev)
356 return dev_name(blkg->q->backing_dev_info.dev);
357 return NULL;
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700358}
359
Tejun Heod3d32e692012-04-01 14:38:42 -0700360/**
361 * blkcg_print_blkgs - helper for printing per-blkg data
362 * @sf: seq_file to print to
363 * @blkcg: blkcg of interest
364 * @prfill: fill function to print out a blkg
365 * @pol: policy in question
366 * @data: data to be passed to @prfill
367 * @show_total: to print out sum of prfill return values or not
368 *
369 * This function invokes @prfill on each blkg of @blkcg if pd for the
370 * policy specified by @pol exists. @prfill is invoked with @sf, the
371 * policy data and @data. If @show_total is %true, the sum of the return
372 * values from @prfill is printed with "Total" label at the end.
373 *
374 * This is to be used to construct print functions for
375 * cftype->read_seq_string method.
376 */
Tejun Heo829fdb52012-04-01 14:38:43 -0700377void blkcg_print_blkgs(struct seq_file *sf, struct blkio_cgroup *blkcg,
Tejun Heod366e7e2012-04-01 14:38:44 -0700378 u64 (*prfill)(struct seq_file *, void *, int),
Tejun Heoec399342012-04-13 13:11:27 -0700379 const struct blkio_policy_type *pol, int data,
380 bool show_total)
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400381{
Tejun Heod3d32e692012-04-01 14:38:42 -0700382 struct blkio_group *blkg;
383 struct hlist_node *n;
384 u64 total = 0;
385
386 spin_lock_irq(&blkcg->lock);
387 hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node)
Tejun Heoec399342012-04-13 13:11:27 -0700388 if (blkg->pd[pol->plid])
389 total += prfill(sf, blkg->pd[pol->plid]->pdata, data);
Tejun Heod3d32e692012-04-01 14:38:42 -0700390 spin_unlock_irq(&blkcg->lock);
391
392 if (show_total)
393 seq_printf(sf, "Total %llu\n", (unsigned long long)total);
394}
Tejun Heo829fdb52012-04-01 14:38:43 -0700395EXPORT_SYMBOL_GPL(blkcg_print_blkgs);
Tejun Heod3d32e692012-04-01 14:38:42 -0700396
397/**
398 * __blkg_prfill_u64 - prfill helper for a single u64 value
399 * @sf: seq_file to print to
Tejun Heod366e7e2012-04-01 14:38:44 -0700400 * @pdata: policy private data of interest
Tejun Heod3d32e692012-04-01 14:38:42 -0700401 * @v: value to print
402 *
Tejun Heod366e7e2012-04-01 14:38:44 -0700403 * Print @v to @sf for the device assocaited with @pdata.
Tejun Heod3d32e692012-04-01 14:38:42 -0700404 */
Tejun Heod366e7e2012-04-01 14:38:44 -0700405u64 __blkg_prfill_u64(struct seq_file *sf, void *pdata, u64 v)
Tejun Heod3d32e692012-04-01 14:38:42 -0700406{
Tejun Heod366e7e2012-04-01 14:38:44 -0700407 const char *dname = blkg_dev_name(pdata_to_blkg(pdata));
Tejun Heod3d32e692012-04-01 14:38:42 -0700408
409 if (!dname)
410 return 0;
411
412 seq_printf(sf, "%s %llu\n", dname, (unsigned long long)v);
413 return v;
414}
Tejun Heo829fdb52012-04-01 14:38:43 -0700415EXPORT_SYMBOL_GPL(__blkg_prfill_u64);
Tejun Heod3d32e692012-04-01 14:38:42 -0700416
417/**
418 * __blkg_prfill_rwstat - prfill helper for a blkg_rwstat
419 * @sf: seq_file to print to
Tejun Heod366e7e2012-04-01 14:38:44 -0700420 * @pdata: policy private data of interest
Tejun Heod3d32e692012-04-01 14:38:42 -0700421 * @rwstat: rwstat to print
422 *
Tejun Heod366e7e2012-04-01 14:38:44 -0700423 * Print @rwstat to @sf for the device assocaited with @pdata.
Tejun Heod3d32e692012-04-01 14:38:42 -0700424 */
Tejun Heod366e7e2012-04-01 14:38:44 -0700425u64 __blkg_prfill_rwstat(struct seq_file *sf, void *pdata,
Tejun Heo829fdb52012-04-01 14:38:43 -0700426 const struct blkg_rwstat *rwstat)
Tejun Heod3d32e692012-04-01 14:38:42 -0700427{
428 static const char *rwstr[] = {
429 [BLKG_RWSTAT_READ] = "Read",
430 [BLKG_RWSTAT_WRITE] = "Write",
431 [BLKG_RWSTAT_SYNC] = "Sync",
432 [BLKG_RWSTAT_ASYNC] = "Async",
433 };
Tejun Heod366e7e2012-04-01 14:38:44 -0700434 const char *dname = blkg_dev_name(pdata_to_blkg(pdata));
Tejun Heod3d32e692012-04-01 14:38:42 -0700435 u64 v;
436 int i;
437
438 if (!dname)
439 return 0;
440
441 for (i = 0; i < BLKG_RWSTAT_NR; i++)
442 seq_printf(sf, "%s %s %llu\n", dname, rwstr[i],
443 (unsigned long long)rwstat->cnt[i]);
444
445 v = rwstat->cnt[BLKG_RWSTAT_READ] + rwstat->cnt[BLKG_RWSTAT_WRITE];
446 seq_printf(sf, "%s Total %llu\n", dname, (unsigned long long)v);
447 return v;
448}
449
Tejun Heo5bc4afb12012-04-01 14:38:45 -0700450/**
451 * blkg_prfill_stat - prfill callback for blkg_stat
452 * @sf: seq_file to print to
453 * @pdata: policy private data of interest
454 * @off: offset to the blkg_stat in @pdata
455 *
456 * prfill callback for printing a blkg_stat.
457 */
458u64 blkg_prfill_stat(struct seq_file *sf, void *pdata, int off)
Tejun Heod3d32e692012-04-01 14:38:42 -0700459{
Tejun Heod366e7e2012-04-01 14:38:44 -0700460 return __blkg_prfill_u64(sf, pdata, blkg_stat_read(pdata + off));
Tejun Heod3d32e692012-04-01 14:38:42 -0700461}
Tejun Heo5bc4afb12012-04-01 14:38:45 -0700462EXPORT_SYMBOL_GPL(blkg_prfill_stat);
Tejun Heod3d32e692012-04-01 14:38:42 -0700463
Tejun Heo5bc4afb12012-04-01 14:38:45 -0700464/**
465 * blkg_prfill_rwstat - prfill callback for blkg_rwstat
466 * @sf: seq_file to print to
467 * @pdata: policy private data of interest
468 * @off: offset to the blkg_rwstat in @pdata
469 *
470 * prfill callback for printing a blkg_rwstat.
471 */
472u64 blkg_prfill_rwstat(struct seq_file *sf, void *pdata, int off)
Tejun Heod3d32e692012-04-01 14:38:42 -0700473{
Tejun Heod366e7e2012-04-01 14:38:44 -0700474 struct blkg_rwstat rwstat = blkg_rwstat_read(pdata + off);
Tejun Heod3d32e692012-04-01 14:38:42 -0700475
Tejun Heod366e7e2012-04-01 14:38:44 -0700476 return __blkg_prfill_rwstat(sf, pdata, &rwstat);
Tejun Heod3d32e692012-04-01 14:38:42 -0700477}
Tejun Heo5bc4afb12012-04-01 14:38:45 -0700478EXPORT_SYMBOL_GPL(blkg_prfill_rwstat);
Tejun Heod3d32e692012-04-01 14:38:42 -0700479
Tejun Heo3a8b31d2012-04-01 14:38:43 -0700480/**
481 * blkg_conf_prep - parse and prepare for per-blkg config update
482 * @blkcg: target block cgroup
Tejun Heoda8b0662012-04-13 13:11:29 -0700483 * @pol: target policy
Tejun Heo3a8b31d2012-04-01 14:38:43 -0700484 * @input: input string
485 * @ctx: blkg_conf_ctx to be filled
486 *
487 * Parse per-blkg config update from @input and initialize @ctx with the
488 * result. @ctx->blkg points to the blkg to be updated and @ctx->v the new
Tejun Heoda8b0662012-04-13 13:11:29 -0700489 * value. This function returns with RCU read lock and queue lock held and
490 * must be paired with blkg_conf_finish().
Tejun Heo3a8b31d2012-04-01 14:38:43 -0700491 */
Tejun Heoda8b0662012-04-13 13:11:29 -0700492int blkg_conf_prep(struct blkio_cgroup *blkcg,
493 const struct blkio_policy_type *pol, const char *input,
Tejun Heo829fdb52012-04-01 14:38:43 -0700494 struct blkg_conf_ctx *ctx)
Tejun Heoda8b0662012-04-13 13:11:29 -0700495 __acquires(rcu) __acquires(disk->queue->queue_lock)
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800496{
Tejun Heo3a8b31d2012-04-01 14:38:43 -0700497 struct gendisk *disk;
498 struct blkio_group *blkg;
Tejun Heo726fa692012-04-01 14:38:43 -0700499 unsigned int major, minor;
500 unsigned long long v;
501 int part, ret;
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800502
Tejun Heo726fa692012-04-01 14:38:43 -0700503 if (sscanf(input, "%u:%u %llu", &major, &minor, &v) != 3)
504 return -EINVAL;
Tejun Heo3a8b31d2012-04-01 14:38:43 -0700505
Tejun Heo726fa692012-04-01 14:38:43 -0700506 disk = get_gendisk(MKDEV(major, minor), &part);
Tejun Heo4bfd4822012-03-05 13:15:08 -0800507 if (!disk || part)
Tejun Heo726fa692012-04-01 14:38:43 -0700508 return -EINVAL;
Tejun Heoe56da7e2012-03-05 13:15:07 -0800509
510 rcu_read_lock();
Tejun Heo4bfd4822012-03-05 13:15:08 -0800511 spin_lock_irq(disk->queue->queue_lock);
Tejun Heoda8b0662012-04-13 13:11:29 -0700512
Tejun Heoaaec55a2012-04-01 14:38:42 -0700513 blkg = blkg_lookup_create(blkcg, disk->queue, false);
Tejun Heoe56da7e2012-03-05 13:15:07 -0800514
Tejun Heo4bfd4822012-03-05 13:15:08 -0800515 if (IS_ERR(blkg)) {
516 ret = PTR_ERR(blkg);
Tejun Heo3a8b31d2012-04-01 14:38:43 -0700517 rcu_read_unlock();
Tejun Heoda8b0662012-04-13 13:11:29 -0700518 spin_unlock_irq(disk->queue->queue_lock);
Tejun Heo3a8b31d2012-04-01 14:38:43 -0700519 put_disk(disk);
520 /*
521 * If queue was bypassing, we should retry. Do so after a
522 * short msleep(). It isn't strictly necessary but queue
523 * can be bypassing for some time and it's always nice to
524 * avoid busy looping.
525 */
526 if (ret == -EBUSY) {
527 msleep(10);
528 ret = restart_syscall();
Vivek Goyal7702e8f2010-09-15 17:06:36 -0400529 }
Tejun Heo726fa692012-04-01 14:38:43 -0700530 return ret;
Vivek Goyal062a6442010-09-15 17:06:33 -0400531 }
Tejun Heoe56da7e2012-03-05 13:15:07 -0800532
Tejun Heo3a8b31d2012-04-01 14:38:43 -0700533 ctx->disk = disk;
534 ctx->blkg = blkg;
Tejun Heo726fa692012-04-01 14:38:43 -0700535 ctx->v = v;
536 return 0;
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800537}
Tejun Heo829fdb52012-04-01 14:38:43 -0700538EXPORT_SYMBOL_GPL(blkg_conf_prep);
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800539
Tejun Heo3a8b31d2012-04-01 14:38:43 -0700540/**
541 * blkg_conf_finish - finish up per-blkg config update
542 * @ctx: blkg_conf_ctx intiailized by blkg_conf_prep()
543 *
544 * Finish up after per-blkg config update. This function must be paired
545 * with blkg_conf_prep().
546 */
Tejun Heo829fdb52012-04-01 14:38:43 -0700547void blkg_conf_finish(struct blkg_conf_ctx *ctx)
Tejun Heoda8b0662012-04-13 13:11:29 -0700548 __releases(ctx->disk->queue->queue_lock) __releases(rcu)
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800549{
Tejun Heoda8b0662012-04-13 13:11:29 -0700550 spin_unlock_irq(ctx->disk->queue->queue_lock);
Tejun Heo3a8b31d2012-04-01 14:38:43 -0700551 rcu_read_unlock();
552 put_disk(ctx->disk);
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800553}
Tejun Heo829fdb52012-04-01 14:38:43 -0700554EXPORT_SYMBOL_GPL(blkg_conf_finish);
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800555
Vivek Goyal31e4c282009-12-03 12:59:42 -0500556struct cftype blkio_files[] = {
557 {
Divyesh Shah84c124d2010-04-09 08:31:19 +0200558 .name = "reset_stats",
559 .write_u64 = blkiocg_reset_stats,
Vivek Goyal22084192009-12-03 12:59:49 -0500560 },
Tejun Heo4baf6e32012-04-01 12:09:55 -0700561 { } /* terminate */
Vivek Goyal31e4c282009-12-03 12:59:42 -0500562};
563
Tejun Heo9f13ef62012-03-05 13:15:21 -0800564/**
565 * blkiocg_pre_destroy - cgroup pre_destroy callback
Tejun Heo9f13ef62012-03-05 13:15:21 -0800566 * @cgroup: cgroup of interest
567 *
568 * This function is called when @cgroup is about to go away and responsible
569 * for shooting down all blkgs associated with @cgroup. blkgs should be
570 * removed while holding both q and blkcg locks. As blkcg lock is nested
571 * inside q lock, this function performs reverse double lock dancing.
572 *
573 * This is the blkcg counterpart of ioc_release_fn().
574 */
Tejun Heo959d8512012-04-01 12:30:01 -0700575static int blkiocg_pre_destroy(struct cgroup *cgroup)
Vivek Goyal31e4c282009-12-03 12:59:42 -0500576{
577 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgroup);
578
Tejun Heo9f13ef62012-03-05 13:15:21 -0800579 spin_lock_irq(&blkcg->lock);
Tejun Heo7ee9c562012-03-05 13:15:11 -0800580
Tejun Heo9f13ef62012-03-05 13:15:21 -0800581 while (!hlist_empty(&blkcg->blkg_list)) {
582 struct blkio_group *blkg = hlist_entry(blkcg->blkg_list.first,
583 struct blkio_group, blkcg_node);
Tejun Heoc875f4d2012-03-05 13:15:22 -0800584 struct request_queue *q = blkg->q;
Vivek Goyalb1c35762009-12-03 12:59:47 -0500585
Tejun Heo9f13ef62012-03-05 13:15:21 -0800586 if (spin_trylock(q->queue_lock)) {
587 blkg_destroy(blkg);
588 spin_unlock(q->queue_lock);
589 } else {
590 spin_unlock_irq(&blkcg->lock);
Tejun Heo9f13ef62012-03-05 13:15:21 -0800591 cpu_relax();
Dan Carpentera5567932012-03-29 20:57:08 +0200592 spin_lock_irq(&blkcg->lock);
Jens Axboe0f3942a2010-05-03 14:28:55 +0200593 }
Tejun Heo9f13ef62012-03-05 13:15:21 -0800594 }
Jens Axboe0f3942a2010-05-03 14:28:55 +0200595
Tejun Heo9f13ef62012-03-05 13:15:21 -0800596 spin_unlock_irq(&blkcg->lock);
Tejun Heo7ee9c562012-03-05 13:15:11 -0800597 return 0;
598}
599
Li Zefan761b3ef2012-01-31 13:47:36 +0800600static void blkiocg_destroy(struct cgroup *cgroup)
Tejun Heo7ee9c562012-03-05 13:15:11 -0800601{
602 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgroup);
603
Ben Blum67523c42010-03-10 15:22:11 -0800604 if (blkcg != &blkio_root_cgroup)
605 kfree(blkcg);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500606}
607
Li Zefan761b3ef2012-01-31 13:47:36 +0800608static struct cgroup_subsys_state *blkiocg_create(struct cgroup *cgroup)
Vivek Goyal31e4c282009-12-03 12:59:42 -0500609{
Tejun Heo9a9e8a22012-03-19 15:10:56 -0700610 static atomic64_t id_seq = ATOMIC64_INIT(0);
Li Zefan03415092010-05-07 08:57:00 +0200611 struct blkio_cgroup *blkcg;
612 struct cgroup *parent = cgroup->parent;
Vivek Goyal31e4c282009-12-03 12:59:42 -0500613
Li Zefan03415092010-05-07 08:57:00 +0200614 if (!parent) {
Vivek Goyal31e4c282009-12-03 12:59:42 -0500615 blkcg = &blkio_root_cgroup;
616 goto done;
617 }
618
Vivek Goyal31e4c282009-12-03 12:59:42 -0500619 blkcg = kzalloc(sizeof(*blkcg), GFP_KERNEL);
620 if (!blkcg)
621 return ERR_PTR(-ENOMEM);
622
Tejun Heo3381cb82012-04-01 14:38:44 -0700623 blkcg->cfq_weight = CFQ_WEIGHT_DEFAULT;
Tejun Heo9a9e8a22012-03-19 15:10:56 -0700624 blkcg->id = atomic64_inc_return(&id_seq); /* root is 0, start from 1 */
Vivek Goyal31e4c282009-12-03 12:59:42 -0500625done:
626 spin_lock_init(&blkcg->lock);
627 INIT_HLIST_HEAD(&blkcg->blkg_list);
628
629 return &blkcg->css;
630}
631
Tejun Heo5efd6112012-03-05 13:15:12 -0800632/**
633 * blkcg_init_queue - initialize blkcg part of request queue
634 * @q: request_queue to initialize
635 *
636 * Called from blk_alloc_queue_node(). Responsible for initializing blkcg
637 * part of new request_queue @q.
638 *
639 * RETURNS:
640 * 0 on success, -errno on failure.
641 */
642int blkcg_init_queue(struct request_queue *q)
643{
Tejun Heo923adde2012-03-05 13:15:13 -0800644 int ret;
645
Tejun Heo5efd6112012-03-05 13:15:12 -0800646 might_sleep();
647
Tejun Heo923adde2012-03-05 13:15:13 -0800648 ret = blk_throtl_init(q);
649 if (ret)
650 return ret;
651
652 mutex_lock(&all_q_mutex);
653 INIT_LIST_HEAD(&q->all_q_node);
654 list_add_tail(&q->all_q_node, &all_q_list);
655 mutex_unlock(&all_q_mutex);
656
657 return 0;
Tejun Heo5efd6112012-03-05 13:15:12 -0800658}
659
660/**
661 * blkcg_drain_queue - drain blkcg part of request_queue
662 * @q: request_queue to drain
663 *
664 * Called from blk_drain_queue(). Responsible for draining blkcg part.
665 */
666void blkcg_drain_queue(struct request_queue *q)
667{
668 lockdep_assert_held(q->queue_lock);
669
670 blk_throtl_drain(q);
671}
672
673/**
674 * blkcg_exit_queue - exit and release blkcg part of request_queue
675 * @q: request_queue being released
676 *
677 * Called from blk_release_queue(). Responsible for exiting blkcg part.
678 */
679void blkcg_exit_queue(struct request_queue *q)
680{
Tejun Heo923adde2012-03-05 13:15:13 -0800681 mutex_lock(&all_q_mutex);
682 list_del_init(&q->all_q_node);
683 mutex_unlock(&all_q_mutex);
684
Tejun Heoe8989fa2012-03-05 13:15:20 -0800685 blkg_destroy_all(q, true);
686
Tejun Heo5efd6112012-03-05 13:15:12 -0800687 blk_throtl_exit(q);
688}
689
Vivek Goyal31e4c282009-12-03 12:59:42 -0500690/*
691 * We cannot support shared io contexts, as we have no mean to support
692 * two tasks with the same ioc in two different groups without major rework
693 * of the main cic data structures. For now we allow a task to change
694 * its cgroup only if it's the only owner of its ioc.
695 */
Li Zefan761b3ef2012-01-31 13:47:36 +0800696static int blkiocg_can_attach(struct cgroup *cgrp, struct cgroup_taskset *tset)
Vivek Goyal31e4c282009-12-03 12:59:42 -0500697{
Tejun Heobb9d97b2011-12-12 18:12:21 -0800698 struct task_struct *task;
Vivek Goyal31e4c282009-12-03 12:59:42 -0500699 struct io_context *ioc;
700 int ret = 0;
701
702 /* task_lock() is needed to avoid races with exit_io_context() */
Tejun Heobb9d97b2011-12-12 18:12:21 -0800703 cgroup_taskset_for_each(task, cgrp, tset) {
704 task_lock(task);
705 ioc = task->io_context;
706 if (ioc && atomic_read(&ioc->nr_tasks) > 1)
707 ret = -EINVAL;
708 task_unlock(task);
709 if (ret)
710 break;
711 }
Vivek Goyal31e4c282009-12-03 12:59:42 -0500712 return ret;
713}
714
Tejun Heo923adde2012-03-05 13:15:13 -0800715static void blkcg_bypass_start(void)
716 __acquires(&all_q_mutex)
717{
718 struct request_queue *q;
719
720 mutex_lock(&all_q_mutex);
721
722 list_for_each_entry(q, &all_q_list, all_q_node) {
723 blk_queue_bypass_start(q);
Tejun Heoe8989fa2012-03-05 13:15:20 -0800724 blkg_destroy_all(q, false);
Tejun Heo923adde2012-03-05 13:15:13 -0800725 }
726}
727
728static void blkcg_bypass_end(void)
729 __releases(&all_q_mutex)
730{
731 struct request_queue *q;
732
733 list_for_each_entry(q, &all_q_list, all_q_node)
734 blk_queue_bypass_end(q);
735
736 mutex_unlock(&all_q_mutex);
737}
738
Tejun Heo676f7c82012-04-01 12:09:55 -0700739struct cgroup_subsys blkio_subsys = {
740 .name = "blkio",
741 .create = blkiocg_create,
742 .can_attach = blkiocg_can_attach,
Tejun Heo959d8512012-04-01 12:30:01 -0700743 .pre_destroy = blkiocg_pre_destroy,
Tejun Heo676f7c82012-04-01 12:09:55 -0700744 .destroy = blkiocg_destroy,
Tejun Heo676f7c82012-04-01 12:09:55 -0700745 .subsys_id = blkio_subsys_id,
Tejun Heo4baf6e32012-04-01 12:09:55 -0700746 .base_cftypes = blkio_files,
Tejun Heo676f7c82012-04-01 12:09:55 -0700747 .module = THIS_MODULE,
748};
749EXPORT_SYMBOL_GPL(blkio_subsys);
750
Tejun Heo8bd435b2012-04-13 13:11:28 -0700751/**
752 * blkio_policy_register - register a blkcg policy
753 * @blkiop: blkcg policy to register
754 *
755 * Register @blkiop with blkcg core. Might sleep and @blkiop may be
756 * modified on successful registration. Returns 0 on success and -errno on
757 * failure.
758 */
759int blkio_policy_register(struct blkio_policy_type *blkiop)
Vivek Goyal3e252062009-12-04 10:36:42 -0500760{
Tejun Heoe8989fa2012-03-05 13:15:20 -0800761 struct request_queue *q;
Tejun Heo8bd435b2012-04-13 13:11:28 -0700762 int i, ret;
Tejun Heoe8989fa2012-03-05 13:15:20 -0800763
Tejun Heobc0d6502012-04-13 13:11:26 -0700764 mutex_lock(&blkcg_pol_mutex);
765
Tejun Heo8bd435b2012-04-13 13:11:28 -0700766 /* find an empty slot */
767 ret = -ENOSPC;
768 for (i = 0; i < BLKCG_MAX_POLS; i++)
769 if (!blkio_policy[i])
770 break;
771 if (i >= BLKCG_MAX_POLS)
772 goto out_unlock;
Tejun Heo035d10b2012-03-05 13:15:04 -0800773
Tejun Heo8bd435b2012-04-13 13:11:28 -0700774 /* register and update blkgs */
775 blkiop->plid = i;
776 blkio_policy[i] = blkiop;
777
778 blkcg_bypass_start();
Tejun Heoe8989fa2012-03-05 13:15:20 -0800779 list_for_each_entry(q, &all_q_list, all_q_node)
Tejun Heoec399342012-04-13 13:11:27 -0700780 update_root_blkg_pd(q, blkiop);
Tejun Heo923adde2012-03-05 13:15:13 -0800781 blkcg_bypass_end();
Tejun Heo44ea53d2012-04-01 14:38:43 -0700782
Tejun Heo8bd435b2012-04-13 13:11:28 -0700783 /* everything is in place, add intf files for the new policy */
Tejun Heo44ea53d2012-04-01 14:38:43 -0700784 if (blkiop->cftypes)
785 WARN_ON(cgroup_add_cftypes(&blkio_subsys, blkiop->cftypes));
Tejun Heo8bd435b2012-04-13 13:11:28 -0700786 ret = 0;
787out_unlock:
Tejun Heobc0d6502012-04-13 13:11:26 -0700788 mutex_unlock(&blkcg_pol_mutex);
Tejun Heo8bd435b2012-04-13 13:11:28 -0700789 return ret;
Vivek Goyal3e252062009-12-04 10:36:42 -0500790}
791EXPORT_SYMBOL_GPL(blkio_policy_register);
792
Tejun Heo8bd435b2012-04-13 13:11:28 -0700793/**
794 * blkiop_policy_unregister - unregister a blkcg policy
795 * @blkiop: blkcg policy to unregister
796 *
797 * Undo blkio_policy_register(@blkiop). Might sleep.
798 */
Vivek Goyal3e252062009-12-04 10:36:42 -0500799void blkio_policy_unregister(struct blkio_policy_type *blkiop)
800{
Tejun Heoe8989fa2012-03-05 13:15:20 -0800801 struct request_queue *q;
802
Tejun Heobc0d6502012-04-13 13:11:26 -0700803 mutex_lock(&blkcg_pol_mutex);
804
Tejun Heo8bd435b2012-04-13 13:11:28 -0700805 if (WARN_ON(blkio_policy[blkiop->plid] != blkiop))
806 goto out_unlock;
807
808 /* kill the intf files first */
Tejun Heo44ea53d2012-04-01 14:38:43 -0700809 if (blkiop->cftypes)
810 cgroup_rm_cftypes(&blkio_subsys, blkiop->cftypes);
811
Tejun Heo8bd435b2012-04-13 13:11:28 -0700812 /* unregister and update blkgs */
Tejun Heo035d10b2012-03-05 13:15:04 -0800813 blkio_policy[blkiop->plid] = NULL;
Tejun Heo035d10b2012-03-05 13:15:04 -0800814
Tejun Heo8bd435b2012-04-13 13:11:28 -0700815 blkcg_bypass_start();
Tejun Heoe8989fa2012-03-05 13:15:20 -0800816 list_for_each_entry(q, &all_q_list, all_q_node)
Tejun Heoec399342012-04-13 13:11:27 -0700817 update_root_blkg_pd(q, blkiop);
Tejun Heo923adde2012-03-05 13:15:13 -0800818 blkcg_bypass_end();
Tejun Heo8bd435b2012-04-13 13:11:28 -0700819out_unlock:
Tejun Heobc0d6502012-04-13 13:11:26 -0700820 mutex_unlock(&blkcg_pol_mutex);
Vivek Goyal3e252062009-12-04 10:36:42 -0500821}
822EXPORT_SYMBOL_GPL(blkio_policy_unregister);