blob: b1807d4ecedbcd44127d3ae03fc43d34b9b405f5 [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 -080028
Tejun Heo3381cb82012-04-01 14:38:44 -070029struct blkio_cgroup blkio_root_cgroup = { .cfq_weight = 2 * CFQ_WEIGHT_DEFAULT };
Vivek Goyal9d6a9862009-12-04 10:36:41 -050030EXPORT_SYMBOL_GPL(blkio_root_cgroup);
31
Tejun Heo8bd435b2012-04-13 13:11:28 -070032static struct blkio_policy_type *blkio_policy[BLKCG_MAX_POLS];
Tejun Heo035d10b2012-03-05 13:15:04 -080033
Vivek Goyal31e4c282009-12-03 12:59:42 -050034struct blkio_cgroup *cgroup_to_blkio_cgroup(struct cgroup *cgroup)
35{
36 return container_of(cgroup_subsys_state(cgroup, blkio_subsys_id),
37 struct blkio_cgroup, css);
38}
Vivek Goyal9d6a9862009-12-04 10:36:41 -050039EXPORT_SYMBOL_GPL(cgroup_to_blkio_cgroup);
Vivek Goyal31e4c282009-12-03 12:59:42 -050040
Tejun Heo4f85cb92012-03-05 13:15:28 -080041static struct blkio_cgroup *task_blkio_cgroup(struct task_struct *tsk)
Vivek Goyal70087dc2011-05-16 15:24:08 +020042{
43 return container_of(task_subsys_state(tsk, blkio_subsys_id),
44 struct blkio_cgroup, css);
45}
Tejun Heo4f85cb92012-03-05 13:15:28 -080046
47struct blkio_cgroup *bio_blkio_cgroup(struct bio *bio)
48{
49 if (bio && bio->bi_css)
50 return container_of(bio->bi_css, struct blkio_cgroup, css);
51 return task_blkio_cgroup(current);
52}
53EXPORT_SYMBOL_GPL(bio_blkio_cgroup);
Vivek Goyal70087dc2011-05-16 15:24:08 +020054
Tejun Heoa2b16932012-04-13 13:11:33 -070055static bool blkcg_policy_enabled(struct request_queue *q,
56 const struct blkio_policy_type *pol)
57{
58 return pol && test_bit(pol->plid, q->blkcg_pols);
59}
60
61static size_t blkg_pd_size(const struct blkio_policy_type *pol)
62{
63 return sizeof(struct blkg_policy_data) + pol->pdata_size;
64}
65
Tejun Heo03814112012-03-05 13:15:14 -080066/**
67 * blkg_free - free a blkg
68 * @blkg: blkg to free
69 *
70 * Free @blkg which may be partially allocated.
71 */
72static void blkg_free(struct blkio_group *blkg)
73{
Tejun Heoe8989fa2012-03-05 13:15:20 -080074 int i;
Tejun Heo549d3aa2012-03-05 13:15:16 -080075
76 if (!blkg)
77 return;
78
Tejun Heo8bd435b2012-04-13 13:11:28 -070079 for (i = 0; i < BLKCG_MAX_POLS; i++) {
Tejun Heo9ade5ea2012-04-01 14:38:44 -070080 struct blkio_policy_type *pol = blkio_policy[i];
Tejun Heoe8989fa2012-03-05 13:15:20 -080081 struct blkg_policy_data *pd = blkg->pd[i];
82
Tejun Heo9ade5ea2012-04-01 14:38:44 -070083 if (!pd)
84 continue;
85
86 if (pol && pol->ops.blkio_exit_group_fn)
87 pol->ops.blkio_exit_group_fn(blkg);
88
Tejun Heo9ade5ea2012-04-01 14:38:44 -070089 kfree(pd);
Tejun Heo03814112012-03-05 13:15:14 -080090 }
Tejun Heoe8989fa2012-03-05 13:15:20 -080091
Tejun Heo549d3aa2012-03-05 13:15:16 -080092 kfree(blkg);
Tejun Heo03814112012-03-05 13:15:14 -080093}
94
95/**
96 * blkg_alloc - allocate a blkg
97 * @blkcg: block cgroup the new blkg is associated with
98 * @q: request_queue the new blkg is associated with
Tejun Heo03814112012-03-05 13:15:14 -080099 *
Tejun Heoe8989fa2012-03-05 13:15:20 -0800100 * Allocate a new blkg assocating @blkcg and @q.
Tejun Heo03814112012-03-05 13:15:14 -0800101 */
102static struct blkio_group *blkg_alloc(struct blkio_cgroup *blkcg,
Tejun Heoe8989fa2012-03-05 13:15:20 -0800103 struct request_queue *q)
Tejun Heo03814112012-03-05 13:15:14 -0800104{
105 struct blkio_group *blkg;
Tejun Heoe8989fa2012-03-05 13:15:20 -0800106 int i;
Tejun Heo03814112012-03-05 13:15:14 -0800107
108 /* alloc and init base part */
109 blkg = kzalloc_node(sizeof(*blkg), GFP_ATOMIC, q->node);
110 if (!blkg)
111 return NULL;
112
Tejun Heoc875f4d2012-03-05 13:15:22 -0800113 blkg->q = q;
Tejun Heoe8989fa2012-03-05 13:15:20 -0800114 INIT_LIST_HEAD(&blkg->q_node);
Tejun Heo03814112012-03-05 13:15:14 -0800115 blkg->blkcg = blkcg;
Tejun Heo1adaf3d2012-03-05 13:15:15 -0800116 blkg->refcnt = 1;
Tejun Heo03814112012-03-05 13:15:14 -0800117 cgroup_path(blkcg->css.cgroup, blkg->path, sizeof(blkg->path));
118
Tejun Heo8bd435b2012-04-13 13:11:28 -0700119 for (i = 0; i < BLKCG_MAX_POLS; i++) {
Tejun Heoe8989fa2012-03-05 13:15:20 -0800120 struct blkio_policy_type *pol = blkio_policy[i];
121 struct blkg_policy_data *pd;
Tejun Heo03814112012-03-05 13:15:14 -0800122
Tejun Heoa2b16932012-04-13 13:11:33 -0700123 if (!blkcg_policy_enabled(q, pol))
Tejun Heoe8989fa2012-03-05 13:15:20 -0800124 continue;
Tejun Heo549d3aa2012-03-05 13:15:16 -0800125
Tejun Heoe8989fa2012-03-05 13:15:20 -0800126 /* alloc per-policy data and attach it to blkg */
Tejun Heoa2b16932012-04-13 13:11:33 -0700127 pd = kzalloc_node(blkg_pd_size(pol), GFP_ATOMIC, q->node);
Tejun Heoe8989fa2012-03-05 13:15:20 -0800128 if (!pd) {
129 blkg_free(blkg);
130 return NULL;
131 }
Tejun Heo549d3aa2012-03-05 13:15:16 -0800132
Tejun Heoe8989fa2012-03-05 13:15:20 -0800133 blkg->pd[i] = pd;
134 pd->blkg = blkg;
Tejun Heo03814112012-03-05 13:15:14 -0800135 }
136
Tejun Heo549d3aa2012-03-05 13:15:16 -0800137 /* invoke per-policy init */
Tejun Heo8bd435b2012-04-13 13:11:28 -0700138 for (i = 0; i < BLKCG_MAX_POLS; i++) {
Tejun Heoe8989fa2012-03-05 13:15:20 -0800139 struct blkio_policy_type *pol = blkio_policy[i];
140
Tejun Heoa2b16932012-04-13 13:11:33 -0700141 if (blkcg_policy_enabled(blkg->q, pol))
Tejun Heoe8989fa2012-03-05 13:15:20 -0800142 pol->ops.blkio_init_group_fn(blkg);
143 }
144
Tejun Heo03814112012-03-05 13:15:14 -0800145 return blkg;
146}
147
Tejun Heo80fd9972012-04-13 14:50:53 -0700148static struct blkio_group *__blkg_lookup(struct blkio_cgroup *blkcg,
149 struct request_queue *q)
150{
151 struct blkio_group *blkg;
152 struct hlist_node *n;
153
154 hlist_for_each_entry_rcu(blkg, n, &blkcg->blkg_list, blkcg_node)
155 if (blkg->q == q)
156 return blkg;
157 return NULL;
158}
159
160/**
161 * blkg_lookup - lookup blkg for the specified blkcg - q pair
162 * @blkcg: blkcg of interest
163 * @q: request_queue of interest
164 *
165 * Lookup blkg for the @blkcg - @q pair. This function should be called
166 * under RCU read lock and is guaranteed to return %NULL if @q is bypassing
167 * - see blk_queue_bypass_start() for details.
168 */
169struct blkio_group *blkg_lookup(struct blkio_cgroup *blkcg,
170 struct request_queue *q)
171{
172 WARN_ON_ONCE(!rcu_read_lock_held());
173
174 if (unlikely(blk_queue_bypass(q)))
175 return NULL;
176 return __blkg_lookup(blkcg, q);
177}
178EXPORT_SYMBOL_GPL(blkg_lookup);
179
Tejun Heo3c96cb32012-04-13 13:11:34 -0700180static struct blkio_group *__blkg_lookup_create(struct blkio_cgroup *blkcg,
181 struct request_queue *q)
Tejun Heocd1604f2012-03-05 13:15:06 -0800182 __releases(q->queue_lock) __acquires(q->queue_lock)
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400183{
Vivek Goyal1cd9e032012-03-08 10:53:56 -0800184 struct blkio_group *blkg;
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400185
Tejun Heocd1604f2012-03-05 13:15:06 -0800186 WARN_ON_ONCE(!rcu_read_lock_held());
187 lockdep_assert_held(q->queue_lock);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500188
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 Heo3c96cb32012-04-13 13:11:34 -0700216
217struct blkio_group *blkg_lookup_create(struct blkio_cgroup *blkcg,
218 struct request_queue *q)
219{
220 /*
221 * This could be the first entry point of blkcg implementation and
222 * we shouldn't allow anything to go through for a bypassing queue.
223 */
224 if (unlikely(blk_queue_bypass(q)))
225 return ERR_PTR(blk_queue_dead(q) ? -EINVAL : -EBUSY);
226 return __blkg_lookup_create(blkcg, q);
227}
Tejun Heocd1604f2012-03-05 13:15:06 -0800228EXPORT_SYMBOL_GPL(blkg_lookup_create);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500229
Tejun Heoe8989fa2012-03-05 13:15:20 -0800230static void blkg_destroy(struct blkio_group *blkg)
Tejun Heo72e06c22012-03-05 13:15:00 -0800231{
Tejun Heo03aa2642012-03-05 13:15:19 -0800232 struct request_queue *q = blkg->q;
Tejun Heo9f13ef62012-03-05 13:15:21 -0800233 struct blkio_cgroup *blkcg = blkg->blkcg;
Tejun Heo03aa2642012-03-05 13:15:19 -0800234
235 lockdep_assert_held(q->queue_lock);
Tejun Heo9f13ef62012-03-05 13:15:21 -0800236 lockdep_assert_held(&blkcg->lock);
Tejun Heo03aa2642012-03-05 13:15:19 -0800237
238 /* Something wrong if we are trying to remove same group twice */
Tejun Heoe8989fa2012-03-05 13:15:20 -0800239 WARN_ON_ONCE(list_empty(&blkg->q_node));
Tejun Heo9f13ef62012-03-05 13:15:21 -0800240 WARN_ON_ONCE(hlist_unhashed(&blkg->blkcg_node));
Tejun Heoe8989fa2012-03-05 13:15:20 -0800241 list_del_init(&blkg->q_node);
Tejun Heo9f13ef62012-03-05 13:15:21 -0800242 hlist_del_init_rcu(&blkg->blkcg_node);
Tejun Heo03aa2642012-03-05 13:15:19 -0800243
Tejun Heo03aa2642012-03-05 13:15:19 -0800244 /*
245 * Put the reference taken at the time of creation so that when all
246 * queues are gone, group can be destroyed.
247 */
248 blkg_put(blkg);
249}
250
Tejun Heo9f13ef62012-03-05 13:15:21 -0800251/**
252 * blkg_destroy_all - destroy all blkgs associated with a request_queue
253 * @q: request_queue of interest
Tejun Heo9f13ef62012-03-05 13:15:21 -0800254 *
Tejun Heo3c96cb32012-04-13 13:11:34 -0700255 * Destroy all blkgs associated with @q.
Tejun Heo9f13ef62012-03-05 13:15:21 -0800256 */
Tejun Heo3c96cb32012-04-13 13:11:34 -0700257static void blkg_destroy_all(struct request_queue *q)
Tejun Heo03aa2642012-03-05 13:15:19 -0800258{
259 struct blkio_group *blkg, *n;
Tejun Heo72e06c22012-03-05 13:15:00 -0800260
Tejun Heo6d18b002012-04-13 13:11:35 -0700261 lockdep_assert_held(q->queue_lock);
Tejun Heo72e06c22012-03-05 13:15:00 -0800262
Tejun Heo9f13ef62012-03-05 13:15:21 -0800263 list_for_each_entry_safe(blkg, n, &q->blkg_list, q_node) {
264 struct blkio_cgroup *blkcg = blkg->blkcg;
Tejun Heo72e06c22012-03-05 13:15:00 -0800265
Tejun Heo9f13ef62012-03-05 13:15:21 -0800266 spin_lock(&blkcg->lock);
267 blkg_destroy(blkg);
268 spin_unlock(&blkcg->lock);
Tejun Heo72e06c22012-03-05 13:15:00 -0800269 }
270}
271
Tejun Heo1adaf3d2012-03-05 13:15:15 -0800272static void blkg_rcu_free(struct rcu_head *rcu_head)
273{
274 blkg_free(container_of(rcu_head, struct blkio_group, rcu_head));
275}
276
277void __blkg_release(struct blkio_group *blkg)
278{
279 /* release the extra blkcg reference this blkg has been holding */
280 css_put(&blkg->blkcg->css);
281
282 /*
283 * A group is freed in rcu manner. But having an rcu lock does not
284 * mean that one can access all the fields of blkg and assume these
285 * are valid. For example, don't try to follow throtl_data and
286 * request queue links.
287 *
288 * Having a reference to blkg under an rcu allows acess to only
289 * values local to groups like group stats and group rate limits
290 */
291 call_rcu(&blkg->rcu_head, blkg_rcu_free);
292}
293EXPORT_SYMBOL_GPL(__blkg_release);
294
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700295static int
Divyesh Shah84c124d2010-04-09 08:31:19 +0200296blkiocg_reset_stats(struct cgroup *cgroup, struct cftype *cftype, u64 val)
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700297{
Tejun Heo997a0262012-03-08 10:53:58 -0800298 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgroup);
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700299 struct blkio_group *blkg;
300 struct hlist_node *n;
Tejun Heobc0d6502012-04-13 13:11:26 -0700301 int i;
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700302
Tejun Heobc0d6502012-04-13 13:11:26 -0700303 mutex_lock(&blkcg_pol_mutex);
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700304 spin_lock_irq(&blkcg->lock);
Tejun Heo997a0262012-03-08 10:53:58 -0800305
306 /*
307 * Note that stat reset is racy - it doesn't synchronize against
308 * stat updates. This is a debug feature which shouldn't exist
309 * anyway. If you get hit by a race, retry.
310 */
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700311 hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) {
Tejun Heo8bd435b2012-04-13 13:11:28 -0700312 for (i = 0; i < BLKCG_MAX_POLS; i++) {
Tejun Heobc0d6502012-04-13 13:11:26 -0700313 struct blkio_policy_type *pol = blkio_policy[i];
Tejun Heo549d3aa2012-03-05 13:15:16 -0800314
Tejun Heoa2b16932012-04-13 13:11:33 -0700315 if (blkcg_policy_enabled(blkg->q, pol) &&
316 pol->ops.blkio_reset_group_stats_fn)
Tejun Heo9ade5ea2012-04-01 14:38:44 -0700317 pol->ops.blkio_reset_group_stats_fn(blkg);
Tejun Heobc0d6502012-04-13 13:11:26 -0700318 }
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700319 }
Vivek Goyalf0bdc8c2011-05-19 15:38:30 -0400320
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700321 spin_unlock_irq(&blkcg->lock);
Tejun Heobc0d6502012-04-13 13:11:26 -0700322 mutex_unlock(&blkcg_pol_mutex);
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700323 return 0;
324}
325
Tejun Heod3d32e62012-04-01 14:38:42 -0700326static const char *blkg_dev_name(struct blkio_group *blkg)
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700327{
Tejun Heod3d32e62012-04-01 14:38:42 -0700328 /* some drivers (floppy) instantiate a queue w/o disk registered */
329 if (blkg->q->backing_dev_info.dev)
330 return dev_name(blkg->q->backing_dev_info.dev);
331 return NULL;
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700332}
333
Tejun Heod3d32e62012-04-01 14:38:42 -0700334/**
335 * blkcg_print_blkgs - helper for printing per-blkg data
336 * @sf: seq_file to print to
337 * @blkcg: blkcg of interest
338 * @prfill: fill function to print out a blkg
339 * @pol: policy in question
340 * @data: data to be passed to @prfill
341 * @show_total: to print out sum of prfill return values or not
342 *
343 * This function invokes @prfill on each blkg of @blkcg if pd for the
344 * policy specified by @pol exists. @prfill is invoked with @sf, the
345 * policy data and @data. If @show_total is %true, the sum of the return
346 * values from @prfill is printed with "Total" label at the end.
347 *
348 * This is to be used to construct print functions for
349 * cftype->read_seq_string method.
350 */
Tejun Heo829fdb52012-04-01 14:38:43 -0700351void blkcg_print_blkgs(struct seq_file *sf, struct blkio_cgroup *blkcg,
Tejun Heod366e7e2012-04-01 14:38:44 -0700352 u64 (*prfill)(struct seq_file *, void *, int),
Tejun Heoec399342012-04-13 13:11:27 -0700353 const struct blkio_policy_type *pol, int data,
354 bool show_total)
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400355{
Tejun Heod3d32e62012-04-01 14:38:42 -0700356 struct blkio_group *blkg;
357 struct hlist_node *n;
358 u64 total = 0;
359
360 spin_lock_irq(&blkcg->lock);
361 hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node)
Tejun Heoa2b16932012-04-13 13:11:33 -0700362 if (blkcg_policy_enabled(blkg->q, pol))
Tejun Heoec399342012-04-13 13:11:27 -0700363 total += prfill(sf, blkg->pd[pol->plid]->pdata, data);
Tejun Heod3d32e62012-04-01 14:38:42 -0700364 spin_unlock_irq(&blkcg->lock);
365
366 if (show_total)
367 seq_printf(sf, "Total %llu\n", (unsigned long long)total);
368}
Tejun Heo829fdb52012-04-01 14:38:43 -0700369EXPORT_SYMBOL_GPL(blkcg_print_blkgs);
Tejun Heod3d32e62012-04-01 14:38:42 -0700370
371/**
372 * __blkg_prfill_u64 - prfill helper for a single u64 value
373 * @sf: seq_file to print to
Tejun Heod366e7e2012-04-01 14:38:44 -0700374 * @pdata: policy private data of interest
Tejun Heod3d32e62012-04-01 14:38:42 -0700375 * @v: value to print
376 *
Tejun Heod366e7e2012-04-01 14:38:44 -0700377 * Print @v to @sf for the device assocaited with @pdata.
Tejun Heod3d32e62012-04-01 14:38:42 -0700378 */
Tejun Heod366e7e2012-04-01 14:38:44 -0700379u64 __blkg_prfill_u64(struct seq_file *sf, void *pdata, u64 v)
Tejun Heod3d32e62012-04-01 14:38:42 -0700380{
Tejun Heod366e7e2012-04-01 14:38:44 -0700381 const char *dname = blkg_dev_name(pdata_to_blkg(pdata));
Tejun Heod3d32e62012-04-01 14:38:42 -0700382
383 if (!dname)
384 return 0;
385
386 seq_printf(sf, "%s %llu\n", dname, (unsigned long long)v);
387 return v;
388}
Tejun Heo829fdb52012-04-01 14:38:43 -0700389EXPORT_SYMBOL_GPL(__blkg_prfill_u64);
Tejun Heod3d32e62012-04-01 14:38:42 -0700390
391/**
392 * __blkg_prfill_rwstat - prfill helper for a blkg_rwstat
393 * @sf: seq_file to print to
Tejun Heod366e7e2012-04-01 14:38:44 -0700394 * @pdata: policy private data of interest
Tejun Heod3d32e62012-04-01 14:38:42 -0700395 * @rwstat: rwstat to print
396 *
Tejun Heod366e7e2012-04-01 14:38:44 -0700397 * Print @rwstat to @sf for the device assocaited with @pdata.
Tejun Heod3d32e62012-04-01 14:38:42 -0700398 */
Tejun Heod366e7e2012-04-01 14:38:44 -0700399u64 __blkg_prfill_rwstat(struct seq_file *sf, void *pdata,
Tejun Heo829fdb52012-04-01 14:38:43 -0700400 const struct blkg_rwstat *rwstat)
Tejun Heod3d32e62012-04-01 14:38:42 -0700401{
402 static const char *rwstr[] = {
403 [BLKG_RWSTAT_READ] = "Read",
404 [BLKG_RWSTAT_WRITE] = "Write",
405 [BLKG_RWSTAT_SYNC] = "Sync",
406 [BLKG_RWSTAT_ASYNC] = "Async",
407 };
Tejun Heod366e7e2012-04-01 14:38:44 -0700408 const char *dname = blkg_dev_name(pdata_to_blkg(pdata));
Tejun Heod3d32e62012-04-01 14:38:42 -0700409 u64 v;
410 int i;
411
412 if (!dname)
413 return 0;
414
415 for (i = 0; i < BLKG_RWSTAT_NR; i++)
416 seq_printf(sf, "%s %s %llu\n", dname, rwstr[i],
417 (unsigned long long)rwstat->cnt[i]);
418
419 v = rwstat->cnt[BLKG_RWSTAT_READ] + rwstat->cnt[BLKG_RWSTAT_WRITE];
420 seq_printf(sf, "%s Total %llu\n", dname, (unsigned long long)v);
421 return v;
422}
423
Tejun Heo5bc4afb12012-04-01 14:38:45 -0700424/**
425 * blkg_prfill_stat - prfill callback for blkg_stat
426 * @sf: seq_file to print to
427 * @pdata: policy private data of interest
428 * @off: offset to the blkg_stat in @pdata
429 *
430 * prfill callback for printing a blkg_stat.
431 */
432u64 blkg_prfill_stat(struct seq_file *sf, void *pdata, int off)
Tejun Heod3d32e62012-04-01 14:38:42 -0700433{
Tejun Heod366e7e2012-04-01 14:38:44 -0700434 return __blkg_prfill_u64(sf, pdata, blkg_stat_read(pdata + off));
Tejun Heod3d32e62012-04-01 14:38:42 -0700435}
Tejun Heo5bc4afb12012-04-01 14:38:45 -0700436EXPORT_SYMBOL_GPL(blkg_prfill_stat);
Tejun Heod3d32e62012-04-01 14:38:42 -0700437
Tejun Heo5bc4afb12012-04-01 14:38:45 -0700438/**
439 * blkg_prfill_rwstat - prfill callback for blkg_rwstat
440 * @sf: seq_file to print to
441 * @pdata: policy private data of interest
442 * @off: offset to the blkg_rwstat in @pdata
443 *
444 * prfill callback for printing a blkg_rwstat.
445 */
446u64 blkg_prfill_rwstat(struct seq_file *sf, void *pdata, int off)
Tejun Heod3d32e62012-04-01 14:38:42 -0700447{
Tejun Heod366e7e2012-04-01 14:38:44 -0700448 struct blkg_rwstat rwstat = blkg_rwstat_read(pdata + off);
Tejun Heod3d32e62012-04-01 14:38:42 -0700449
Tejun Heod366e7e2012-04-01 14:38:44 -0700450 return __blkg_prfill_rwstat(sf, pdata, &rwstat);
Tejun Heod3d32e62012-04-01 14:38:42 -0700451}
Tejun Heo5bc4afb12012-04-01 14:38:45 -0700452EXPORT_SYMBOL_GPL(blkg_prfill_rwstat);
Tejun Heod3d32e62012-04-01 14:38:42 -0700453
Tejun Heo3a8b31d2012-04-01 14:38:43 -0700454/**
455 * blkg_conf_prep - parse and prepare for per-blkg config update
456 * @blkcg: target block cgroup
Tejun Heoda8b0662012-04-13 13:11:29 -0700457 * @pol: target policy
Tejun Heo3a8b31d2012-04-01 14:38:43 -0700458 * @input: input string
459 * @ctx: blkg_conf_ctx to be filled
460 *
461 * Parse per-blkg config update from @input and initialize @ctx with the
462 * result. @ctx->blkg points to the blkg to be updated and @ctx->v the new
Tejun Heoda8b0662012-04-13 13:11:29 -0700463 * value. This function returns with RCU read lock and queue lock held and
464 * must be paired with blkg_conf_finish().
Tejun Heo3a8b31d2012-04-01 14:38:43 -0700465 */
Tejun Heoda8b0662012-04-13 13:11:29 -0700466int blkg_conf_prep(struct blkio_cgroup *blkcg,
467 const struct blkio_policy_type *pol, const char *input,
Tejun Heo829fdb52012-04-01 14:38:43 -0700468 struct blkg_conf_ctx *ctx)
Tejun Heoda8b0662012-04-13 13:11:29 -0700469 __acquires(rcu) __acquires(disk->queue->queue_lock)
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800470{
Tejun Heo3a8b31d2012-04-01 14:38:43 -0700471 struct gendisk *disk;
472 struct blkio_group *blkg;
Tejun Heo726fa692012-04-01 14:38:43 -0700473 unsigned int major, minor;
474 unsigned long long v;
475 int part, ret;
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800476
Tejun Heo726fa692012-04-01 14:38:43 -0700477 if (sscanf(input, "%u:%u %llu", &major, &minor, &v) != 3)
478 return -EINVAL;
Tejun Heo3a8b31d2012-04-01 14:38:43 -0700479
Tejun Heo726fa692012-04-01 14:38:43 -0700480 disk = get_gendisk(MKDEV(major, minor), &part);
Tejun Heo4bfd4822012-03-05 13:15:08 -0800481 if (!disk || part)
Tejun Heo726fa692012-04-01 14:38:43 -0700482 return -EINVAL;
Tejun Heoe56da7e2012-03-05 13:15:07 -0800483
484 rcu_read_lock();
Tejun Heo4bfd4822012-03-05 13:15:08 -0800485 spin_lock_irq(disk->queue->queue_lock);
Tejun Heoda8b0662012-04-13 13:11:29 -0700486
Tejun Heoa2b16932012-04-13 13:11:33 -0700487 if (blkcg_policy_enabled(disk->queue, pol))
Tejun Heo3c96cb32012-04-13 13:11:34 -0700488 blkg = blkg_lookup_create(blkcg, disk->queue);
Tejun Heoa2b16932012-04-13 13:11:33 -0700489 else
490 blkg = ERR_PTR(-EINVAL);
Tejun Heoe56da7e2012-03-05 13:15:07 -0800491
Tejun Heo4bfd4822012-03-05 13:15:08 -0800492 if (IS_ERR(blkg)) {
493 ret = PTR_ERR(blkg);
Tejun Heo3a8b31d2012-04-01 14:38:43 -0700494 rcu_read_unlock();
Tejun Heoda8b0662012-04-13 13:11:29 -0700495 spin_unlock_irq(disk->queue->queue_lock);
Tejun Heo3a8b31d2012-04-01 14:38:43 -0700496 put_disk(disk);
497 /*
498 * If queue was bypassing, we should retry. Do so after a
499 * short msleep(). It isn't strictly necessary but queue
500 * can be bypassing for some time and it's always nice to
501 * avoid busy looping.
502 */
503 if (ret == -EBUSY) {
504 msleep(10);
505 ret = restart_syscall();
Vivek Goyal7702e8f2010-09-15 17:06:36 -0400506 }
Tejun Heo726fa692012-04-01 14:38:43 -0700507 return ret;
Vivek Goyal062a6442010-09-15 17:06:33 -0400508 }
Tejun Heoe56da7e2012-03-05 13:15:07 -0800509
Tejun Heo3a8b31d2012-04-01 14:38:43 -0700510 ctx->disk = disk;
511 ctx->blkg = blkg;
Tejun Heo726fa692012-04-01 14:38:43 -0700512 ctx->v = v;
513 return 0;
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800514}
Tejun Heo829fdb52012-04-01 14:38:43 -0700515EXPORT_SYMBOL_GPL(blkg_conf_prep);
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800516
Tejun Heo3a8b31d2012-04-01 14:38:43 -0700517/**
518 * blkg_conf_finish - finish up per-blkg config update
519 * @ctx: blkg_conf_ctx intiailized by blkg_conf_prep()
520 *
521 * Finish up after per-blkg config update. This function must be paired
522 * with blkg_conf_prep().
523 */
Tejun Heo829fdb52012-04-01 14:38:43 -0700524void blkg_conf_finish(struct blkg_conf_ctx *ctx)
Tejun Heoda8b0662012-04-13 13:11:29 -0700525 __releases(ctx->disk->queue->queue_lock) __releases(rcu)
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800526{
Tejun Heoda8b0662012-04-13 13:11:29 -0700527 spin_unlock_irq(ctx->disk->queue->queue_lock);
Tejun Heo3a8b31d2012-04-01 14:38:43 -0700528 rcu_read_unlock();
529 put_disk(ctx->disk);
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800530}
Tejun Heo829fdb52012-04-01 14:38:43 -0700531EXPORT_SYMBOL_GPL(blkg_conf_finish);
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800532
Vivek Goyal31e4c282009-12-03 12:59:42 -0500533struct cftype blkio_files[] = {
534 {
Divyesh Shah84c124d2010-04-09 08:31:19 +0200535 .name = "reset_stats",
536 .write_u64 = blkiocg_reset_stats,
Vivek Goyal22084192009-12-03 12:59:49 -0500537 },
Tejun Heo4baf6e32012-04-01 12:09:55 -0700538 { } /* terminate */
Vivek Goyal31e4c282009-12-03 12:59:42 -0500539};
540
Tejun Heo9f13ef62012-03-05 13:15:21 -0800541/**
542 * blkiocg_pre_destroy - cgroup pre_destroy callback
Tejun Heo9f13ef62012-03-05 13:15:21 -0800543 * @cgroup: cgroup of interest
544 *
545 * This function is called when @cgroup is about to go away and responsible
546 * for shooting down all blkgs associated with @cgroup. blkgs should be
547 * removed while holding both q and blkcg locks. As blkcg lock is nested
548 * inside q lock, this function performs reverse double lock dancing.
549 *
550 * This is the blkcg counterpart of ioc_release_fn().
551 */
Tejun Heo959d8512012-04-01 12:30:01 -0700552static int blkiocg_pre_destroy(struct cgroup *cgroup)
Vivek Goyal31e4c282009-12-03 12:59:42 -0500553{
554 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgroup);
555
Tejun Heo9f13ef62012-03-05 13:15:21 -0800556 spin_lock_irq(&blkcg->lock);
Tejun Heo7ee9c562012-03-05 13:15:11 -0800557
Tejun Heo9f13ef62012-03-05 13:15:21 -0800558 while (!hlist_empty(&blkcg->blkg_list)) {
559 struct blkio_group *blkg = hlist_entry(blkcg->blkg_list.first,
560 struct blkio_group, blkcg_node);
Tejun Heoc875f4d2012-03-05 13:15:22 -0800561 struct request_queue *q = blkg->q;
Vivek Goyalb1c35762009-12-03 12:59:47 -0500562
Tejun Heo9f13ef62012-03-05 13:15:21 -0800563 if (spin_trylock(q->queue_lock)) {
564 blkg_destroy(blkg);
565 spin_unlock(q->queue_lock);
566 } else {
567 spin_unlock_irq(&blkcg->lock);
Tejun Heo9f13ef62012-03-05 13:15:21 -0800568 cpu_relax();
Dan Carpentera5567932012-03-29 20:57:08 +0200569 spin_lock_irq(&blkcg->lock);
Jens Axboe0f3942a2010-05-03 14:28:55 +0200570 }
Tejun Heo9f13ef62012-03-05 13:15:21 -0800571 }
Jens Axboe0f3942a2010-05-03 14:28:55 +0200572
Tejun Heo9f13ef62012-03-05 13:15:21 -0800573 spin_unlock_irq(&blkcg->lock);
Tejun Heo7ee9c562012-03-05 13:15:11 -0800574 return 0;
575}
576
Li Zefan761b3ef2012-01-31 13:47:36 +0800577static void blkiocg_destroy(struct cgroup *cgroup)
Tejun Heo7ee9c562012-03-05 13:15:11 -0800578{
579 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgroup);
580
Ben Blum67523c42010-03-10 15:22:11 -0800581 if (blkcg != &blkio_root_cgroup)
582 kfree(blkcg);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500583}
584
Li Zefan761b3ef2012-01-31 13:47:36 +0800585static struct cgroup_subsys_state *blkiocg_create(struct cgroup *cgroup)
Vivek Goyal31e4c282009-12-03 12:59:42 -0500586{
Tejun Heo9a9e8a22012-03-19 15:10:56 -0700587 static atomic64_t id_seq = ATOMIC64_INIT(0);
Li Zefan03415092010-05-07 08:57:00 +0200588 struct blkio_cgroup *blkcg;
589 struct cgroup *parent = cgroup->parent;
Vivek Goyal31e4c282009-12-03 12:59:42 -0500590
Li Zefan03415092010-05-07 08:57:00 +0200591 if (!parent) {
Vivek Goyal31e4c282009-12-03 12:59:42 -0500592 blkcg = &blkio_root_cgroup;
593 goto done;
594 }
595
Vivek Goyal31e4c282009-12-03 12:59:42 -0500596 blkcg = kzalloc(sizeof(*blkcg), GFP_KERNEL);
597 if (!blkcg)
598 return ERR_PTR(-ENOMEM);
599
Tejun Heo3381cb82012-04-01 14:38:44 -0700600 blkcg->cfq_weight = CFQ_WEIGHT_DEFAULT;
Tejun Heo9a9e8a22012-03-19 15:10:56 -0700601 blkcg->id = atomic64_inc_return(&id_seq); /* root is 0, start from 1 */
Vivek Goyal31e4c282009-12-03 12:59:42 -0500602done:
603 spin_lock_init(&blkcg->lock);
604 INIT_HLIST_HEAD(&blkcg->blkg_list);
605
606 return &blkcg->css;
607}
608
Tejun Heo5efd6112012-03-05 13:15:12 -0800609/**
610 * blkcg_init_queue - initialize blkcg part of request queue
611 * @q: request_queue to initialize
612 *
613 * Called from blk_alloc_queue_node(). Responsible for initializing blkcg
614 * part of new request_queue @q.
615 *
616 * RETURNS:
617 * 0 on success, -errno on failure.
618 */
619int blkcg_init_queue(struct request_queue *q)
620{
621 might_sleep();
622
Tejun Heo3c96cb32012-04-13 13:11:34 -0700623 return blk_throtl_init(q);
Tejun Heo5efd6112012-03-05 13:15:12 -0800624}
625
626/**
627 * blkcg_drain_queue - drain blkcg part of request_queue
628 * @q: request_queue to drain
629 *
630 * Called from blk_drain_queue(). Responsible for draining blkcg part.
631 */
632void blkcg_drain_queue(struct request_queue *q)
633{
634 lockdep_assert_held(q->queue_lock);
635
636 blk_throtl_drain(q);
637}
638
639/**
640 * blkcg_exit_queue - exit and release blkcg part of request_queue
641 * @q: request_queue being released
642 *
643 * Called from blk_release_queue(). Responsible for exiting blkcg part.
644 */
645void blkcg_exit_queue(struct request_queue *q)
646{
Tejun Heo6d18b002012-04-13 13:11:35 -0700647 spin_lock_irq(q->queue_lock);
Tejun Heo3c96cb32012-04-13 13:11:34 -0700648 blkg_destroy_all(q);
Tejun Heo6d18b002012-04-13 13:11:35 -0700649 spin_unlock_irq(q->queue_lock);
650
Tejun Heo5efd6112012-03-05 13:15:12 -0800651 blk_throtl_exit(q);
652}
653
Vivek Goyal31e4c282009-12-03 12:59:42 -0500654/*
655 * We cannot support shared io contexts, as we have no mean to support
656 * two tasks with the same ioc in two different groups without major rework
657 * of the main cic data structures. For now we allow a task to change
658 * its cgroup only if it's the only owner of its ioc.
659 */
Li Zefan761b3ef2012-01-31 13:47:36 +0800660static int blkiocg_can_attach(struct cgroup *cgrp, struct cgroup_taskset *tset)
Vivek Goyal31e4c282009-12-03 12:59:42 -0500661{
Tejun Heobb9d97b2011-12-12 18:12:21 -0800662 struct task_struct *task;
Vivek Goyal31e4c282009-12-03 12:59:42 -0500663 struct io_context *ioc;
664 int ret = 0;
665
666 /* task_lock() is needed to avoid races with exit_io_context() */
Tejun Heobb9d97b2011-12-12 18:12:21 -0800667 cgroup_taskset_for_each(task, cgrp, tset) {
668 task_lock(task);
669 ioc = task->io_context;
670 if (ioc && atomic_read(&ioc->nr_tasks) > 1)
671 ret = -EINVAL;
672 task_unlock(task);
673 if (ret)
674 break;
675 }
Vivek Goyal31e4c282009-12-03 12:59:42 -0500676 return ret;
677}
678
Tejun Heo676f7c82012-04-01 12:09:55 -0700679struct cgroup_subsys blkio_subsys = {
680 .name = "blkio",
681 .create = blkiocg_create,
682 .can_attach = blkiocg_can_attach,
Tejun Heo959d8512012-04-01 12:30:01 -0700683 .pre_destroy = blkiocg_pre_destroy,
Tejun Heo676f7c82012-04-01 12:09:55 -0700684 .destroy = blkiocg_destroy,
Tejun Heo676f7c82012-04-01 12:09:55 -0700685 .subsys_id = blkio_subsys_id,
Tejun Heo4baf6e32012-04-01 12:09:55 -0700686 .base_cftypes = blkio_files,
Tejun Heo676f7c82012-04-01 12:09:55 -0700687 .module = THIS_MODULE,
688};
689EXPORT_SYMBOL_GPL(blkio_subsys);
690
Tejun Heo8bd435b2012-04-13 13:11:28 -0700691/**
Tejun Heoa2b16932012-04-13 13:11:33 -0700692 * blkcg_activate_policy - activate a blkcg policy on a request_queue
693 * @q: request_queue of interest
694 * @pol: blkcg policy to activate
695 *
696 * Activate @pol on @q. Requires %GFP_KERNEL context. @q goes through
697 * bypass mode to populate its blkgs with policy_data for @pol.
698 *
699 * Activation happens with @q bypassed, so nobody would be accessing blkgs
700 * from IO path. Update of each blkg is protected by both queue and blkcg
701 * locks so that holding either lock and testing blkcg_policy_enabled() is
702 * always enough for dereferencing policy data.
703 *
704 * The caller is responsible for synchronizing [de]activations and policy
705 * [un]registerations. Returns 0 on success, -errno on failure.
706 */
707int blkcg_activate_policy(struct request_queue *q,
708 const struct blkio_policy_type *pol)
709{
710 LIST_HEAD(pds);
711 struct blkio_group *blkg;
712 struct blkg_policy_data *pd, *n;
713 int cnt = 0, ret;
714
715 if (blkcg_policy_enabled(q, pol))
716 return 0;
717
718 blk_queue_bypass_start(q);
719
720 /* make sure the root blkg exists and count the existing blkgs */
721 spin_lock_irq(q->queue_lock);
722
723 rcu_read_lock();
Tejun Heo3c96cb32012-04-13 13:11:34 -0700724 blkg = __blkg_lookup_create(&blkio_root_cgroup, q);
Tejun Heoa2b16932012-04-13 13:11:33 -0700725 rcu_read_unlock();
726
727 if (IS_ERR(blkg)) {
728 ret = PTR_ERR(blkg);
729 goto out_unlock;
730 }
731 q->root_blkg = blkg;
732
733 list_for_each_entry(blkg, &q->blkg_list, q_node)
734 cnt++;
735
736 spin_unlock_irq(q->queue_lock);
737
738 /* allocate policy_data for all existing blkgs */
739 while (cnt--) {
740 pd = kzalloc_node(blkg_pd_size(pol), GFP_KERNEL, q->node);
741 if (!pd) {
742 ret = -ENOMEM;
743 goto out_free;
744 }
745 list_add_tail(&pd->alloc_node, &pds);
746 }
747
748 /*
749 * Install the allocated pds. With @q bypassing, no new blkg
750 * should have been created while the queue lock was dropped.
751 */
752 spin_lock_irq(q->queue_lock);
753
754 list_for_each_entry(blkg, &q->blkg_list, q_node) {
755 if (WARN_ON(list_empty(&pds))) {
756 /* umm... this shouldn't happen, just abort */
757 ret = -ENOMEM;
758 goto out_unlock;
759 }
760 pd = list_first_entry(&pds, struct blkg_policy_data, alloc_node);
761 list_del_init(&pd->alloc_node);
762
763 /* grab blkcg lock too while installing @pd on @blkg */
764 spin_lock(&blkg->blkcg->lock);
765
766 blkg->pd[pol->plid] = pd;
767 pd->blkg = blkg;
768 pol->ops.blkio_init_group_fn(blkg);
769
770 spin_unlock(&blkg->blkcg->lock);
771 }
772
773 __set_bit(pol->plid, q->blkcg_pols);
774 ret = 0;
775out_unlock:
776 spin_unlock_irq(q->queue_lock);
777out_free:
778 blk_queue_bypass_end(q);
779 list_for_each_entry_safe(pd, n, &pds, alloc_node)
780 kfree(pd);
781 return ret;
782}
783EXPORT_SYMBOL_GPL(blkcg_activate_policy);
784
785/**
786 * blkcg_deactivate_policy - deactivate a blkcg policy on a request_queue
787 * @q: request_queue of interest
788 * @pol: blkcg policy to deactivate
789 *
790 * Deactivate @pol on @q. Follows the same synchronization rules as
791 * blkcg_activate_policy().
792 */
793void blkcg_deactivate_policy(struct request_queue *q,
794 const struct blkio_policy_type *pol)
795{
796 struct blkio_group *blkg;
797
798 if (!blkcg_policy_enabled(q, pol))
799 return;
800
801 blk_queue_bypass_start(q);
802 spin_lock_irq(q->queue_lock);
803
804 __clear_bit(pol->plid, q->blkcg_pols);
805
Tejun Heo6d18b002012-04-13 13:11:35 -0700806 /* if no policy is left, no need for blkgs - shoot them down */
807 if (bitmap_empty(q->blkcg_pols, BLKCG_MAX_POLS))
808 blkg_destroy_all(q);
809
Tejun Heoa2b16932012-04-13 13:11:33 -0700810 list_for_each_entry(blkg, &q->blkg_list, q_node) {
811 /* grab blkcg lock too while removing @pd from @blkg */
812 spin_lock(&blkg->blkcg->lock);
813
814 if (pol->ops.blkio_exit_group_fn)
815 pol->ops.blkio_exit_group_fn(blkg);
816
817 kfree(blkg->pd[pol->plid]);
818 blkg->pd[pol->plid] = NULL;
819
820 spin_unlock(&blkg->blkcg->lock);
821 }
822
823 spin_unlock_irq(q->queue_lock);
824 blk_queue_bypass_end(q);
825}
826EXPORT_SYMBOL_GPL(blkcg_deactivate_policy);
827
828/**
Tejun Heo8bd435b2012-04-13 13:11:28 -0700829 * blkio_policy_register - register a blkcg policy
830 * @blkiop: blkcg policy to register
831 *
832 * Register @blkiop with blkcg core. Might sleep and @blkiop may be
833 * modified on successful registration. Returns 0 on success and -errno on
834 * failure.
835 */
836int blkio_policy_register(struct blkio_policy_type *blkiop)
Vivek Goyal3e252062009-12-04 10:36:42 -0500837{
Tejun Heo8bd435b2012-04-13 13:11:28 -0700838 int i, ret;
Tejun Heoe8989fa2012-03-05 13:15:20 -0800839
Tejun Heobc0d6502012-04-13 13:11:26 -0700840 mutex_lock(&blkcg_pol_mutex);
841
Tejun Heo8bd435b2012-04-13 13:11:28 -0700842 /* find an empty slot */
843 ret = -ENOSPC;
844 for (i = 0; i < BLKCG_MAX_POLS; i++)
845 if (!blkio_policy[i])
846 break;
847 if (i >= BLKCG_MAX_POLS)
848 goto out_unlock;
Tejun Heo035d10b2012-03-05 13:15:04 -0800849
Tejun Heo8bd435b2012-04-13 13:11:28 -0700850 /* register and update blkgs */
851 blkiop->plid = i;
852 blkio_policy[i] = blkiop;
853
Tejun Heo8bd435b2012-04-13 13:11:28 -0700854 /* everything is in place, add intf files for the new policy */
Tejun Heo44ea53d2012-04-01 14:38:43 -0700855 if (blkiop->cftypes)
856 WARN_ON(cgroup_add_cftypes(&blkio_subsys, blkiop->cftypes));
Tejun Heo8bd435b2012-04-13 13:11:28 -0700857 ret = 0;
858out_unlock:
Tejun Heobc0d6502012-04-13 13:11:26 -0700859 mutex_unlock(&blkcg_pol_mutex);
Tejun Heo8bd435b2012-04-13 13:11:28 -0700860 return ret;
Vivek Goyal3e252062009-12-04 10:36:42 -0500861}
862EXPORT_SYMBOL_GPL(blkio_policy_register);
863
Tejun Heo8bd435b2012-04-13 13:11:28 -0700864/**
865 * blkiop_policy_unregister - unregister a blkcg policy
866 * @blkiop: blkcg policy to unregister
867 *
868 * Undo blkio_policy_register(@blkiop). Might sleep.
869 */
Vivek Goyal3e252062009-12-04 10:36:42 -0500870void blkio_policy_unregister(struct blkio_policy_type *blkiop)
871{
Tejun Heobc0d6502012-04-13 13:11:26 -0700872 mutex_lock(&blkcg_pol_mutex);
873
Tejun Heo8bd435b2012-04-13 13:11:28 -0700874 if (WARN_ON(blkio_policy[blkiop->plid] != blkiop))
875 goto out_unlock;
876
877 /* kill the intf files first */
Tejun Heo44ea53d2012-04-01 14:38:43 -0700878 if (blkiop->cftypes)
879 cgroup_rm_cftypes(&blkio_subsys, blkiop->cftypes);
880
Tejun Heo8bd435b2012-04-13 13:11:28 -0700881 /* unregister and update blkgs */
Tejun Heo035d10b2012-03-05 13:15:04 -0800882 blkio_policy[blkiop->plid] = NULL;
Tejun Heo8bd435b2012-04-13 13:11:28 -0700883out_unlock:
Tejun Heobc0d6502012-04-13 13:11:26 -0700884 mutex_unlock(&blkcg_pol_mutex);
Vivek Goyal3e252062009-12-04 10:36:42 -0500885}
886EXPORT_SYMBOL_GPL(blkio_policy_unregister);