blob: 82283859727e99d5ffd1f11272ebcc36b9d20c87 [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 Heo3c798392012-04-16 13:57:25 -070029struct blkcg blkcg_root = { .cfq_weight = 2 * CFQ_WEIGHT_DEFAULT };
30EXPORT_SYMBOL_GPL(blkcg_root);
Vivek Goyal9d6a9862009-12-04 10:36:41 -050031
Tejun Heo3c798392012-04-16 13:57:25 -070032static struct blkcg_policy *blkcg_policy[BLKCG_MAX_POLS];
Tejun Heo035d10b2012-03-05 13:15:04 -080033
Tejun Heo3c798392012-04-16 13:57:25 -070034struct blkcg *cgroup_to_blkcg(struct cgroup *cgroup)
Vivek Goyal31e4c282009-12-03 12:59:42 -050035{
36 return container_of(cgroup_subsys_state(cgroup, blkio_subsys_id),
Tejun Heo3c798392012-04-16 13:57:25 -070037 struct blkcg, css);
Vivek Goyal31e4c282009-12-03 12:59:42 -050038}
Tejun Heo3c798392012-04-16 13:57:25 -070039EXPORT_SYMBOL_GPL(cgroup_to_blkcg);
Vivek Goyal31e4c282009-12-03 12:59:42 -050040
Tejun Heo3c798392012-04-16 13:57:25 -070041static struct blkcg *task_blkcg(struct task_struct *tsk)
Vivek Goyal70087dc2011-05-16 15:24:08 +020042{
43 return container_of(task_subsys_state(tsk, blkio_subsys_id),
Tejun Heo3c798392012-04-16 13:57:25 -070044 struct blkcg, css);
Vivek Goyal70087dc2011-05-16 15:24:08 +020045}
Tejun Heo4f85cb92012-03-05 13:15:28 -080046
Tejun Heo3c798392012-04-16 13:57:25 -070047struct blkcg *bio_blkcg(struct bio *bio)
Tejun Heo4f85cb92012-03-05 13:15:28 -080048{
49 if (bio && bio->bi_css)
Tejun Heo3c798392012-04-16 13:57:25 -070050 return container_of(bio->bi_css, struct blkcg, css);
51 return task_blkcg(current);
Tejun Heo4f85cb92012-03-05 13:15:28 -080052}
Tejun Heo3c798392012-04-16 13:57:25 -070053EXPORT_SYMBOL_GPL(bio_blkcg);
Vivek Goyal70087dc2011-05-16 15:24:08 +020054
Tejun Heoa2b16932012-04-13 13:11:33 -070055static bool blkcg_policy_enabled(struct request_queue *q,
Tejun Heo3c798392012-04-16 13:57:25 -070056 const struct blkcg_policy *pol)
Tejun Heoa2b16932012-04-13 13:11:33 -070057{
58 return pol && test_bit(pol->plid, q->blkcg_pols);
59}
60
Tejun Heo03814112012-03-05 13:15:14 -080061/**
62 * blkg_free - free a blkg
63 * @blkg: blkg to free
64 *
65 * Free @blkg which may be partially allocated.
66 */
Tejun Heo3c798392012-04-16 13:57:25 -070067static void blkg_free(struct blkcg_gq *blkg)
Tejun Heo03814112012-03-05 13:15:14 -080068{
Tejun Heoe8989fa2012-03-05 13:15:20 -080069 int i;
Tejun Heo549d3aa2012-03-05 13:15:16 -080070
71 if (!blkg)
72 return;
73
Tejun Heo8bd435b2012-04-13 13:11:28 -070074 for (i = 0; i < BLKCG_MAX_POLS; i++) {
Tejun Heo3c798392012-04-16 13:57:25 -070075 struct blkcg_policy *pol = blkcg_policy[i];
Tejun Heoe8989fa2012-03-05 13:15:20 -080076 struct blkg_policy_data *pd = blkg->pd[i];
77
Tejun Heo9ade5ea2012-04-01 14:38:44 -070078 if (!pd)
79 continue;
80
Tejun Heof9fcc2d2012-04-16 13:57:27 -070081 if (pol && pol->pd_exit_fn)
82 pol->pd_exit_fn(blkg);
Tejun Heo9ade5ea2012-04-01 14:38:44 -070083
Tejun Heo9ade5ea2012-04-01 14:38:44 -070084 kfree(pd);
Tejun Heo03814112012-03-05 13:15:14 -080085 }
Tejun Heoe8989fa2012-03-05 13:15:20 -080086
Tejun Heo549d3aa2012-03-05 13:15:16 -080087 kfree(blkg);
Tejun Heo03814112012-03-05 13:15:14 -080088}
89
90/**
91 * blkg_alloc - allocate a blkg
92 * @blkcg: block cgroup the new blkg is associated with
93 * @q: request_queue the new blkg is associated with
Tejun Heo03814112012-03-05 13:15:14 -080094 *
Tejun Heoe8989fa2012-03-05 13:15:20 -080095 * Allocate a new blkg assocating @blkcg and @q.
Tejun Heo03814112012-03-05 13:15:14 -080096 */
Tejun Heo3c798392012-04-16 13:57:25 -070097static struct blkcg_gq *blkg_alloc(struct blkcg *blkcg, struct request_queue *q)
Tejun Heo03814112012-03-05 13:15:14 -080098{
Tejun Heo3c798392012-04-16 13:57:25 -070099 struct blkcg_gq *blkg;
Tejun Heoe8989fa2012-03-05 13:15:20 -0800100 int i;
Tejun Heo03814112012-03-05 13:15:14 -0800101
102 /* alloc and init base part */
103 blkg = kzalloc_node(sizeof(*blkg), GFP_ATOMIC, q->node);
104 if (!blkg)
105 return NULL;
106
Tejun Heoc875f4d2012-03-05 13:15:22 -0800107 blkg->q = q;
Tejun Heoe8989fa2012-03-05 13:15:20 -0800108 INIT_LIST_HEAD(&blkg->q_node);
Tejun Heo03814112012-03-05 13:15:14 -0800109 blkg->blkcg = blkcg;
Tejun Heo1adaf3d2012-03-05 13:15:15 -0800110 blkg->refcnt = 1;
Tejun Heo03814112012-03-05 13:15:14 -0800111
Tejun Heo8bd435b2012-04-13 13:11:28 -0700112 for (i = 0; i < BLKCG_MAX_POLS; i++) {
Tejun Heo3c798392012-04-16 13:57:25 -0700113 struct blkcg_policy *pol = blkcg_policy[i];
Tejun Heoe8989fa2012-03-05 13:15:20 -0800114 struct blkg_policy_data *pd;
Tejun Heo03814112012-03-05 13:15:14 -0800115
Tejun Heoa2b16932012-04-13 13:11:33 -0700116 if (!blkcg_policy_enabled(q, pol))
Tejun Heoe8989fa2012-03-05 13:15:20 -0800117 continue;
Tejun Heo549d3aa2012-03-05 13:15:16 -0800118
Tejun Heoe8989fa2012-03-05 13:15:20 -0800119 /* alloc per-policy data and attach it to blkg */
Tejun Heof95a04a2012-04-16 13:57:26 -0700120 pd = kzalloc_node(pol->pd_size, GFP_ATOMIC, q->node);
Tejun Heoe8989fa2012-03-05 13:15:20 -0800121 if (!pd) {
122 blkg_free(blkg);
123 return NULL;
124 }
Tejun Heo549d3aa2012-03-05 13:15:16 -0800125
Tejun Heoe8989fa2012-03-05 13:15:20 -0800126 blkg->pd[i] = pd;
127 pd->blkg = blkg;
Tejun Heo03814112012-03-05 13:15:14 -0800128 }
129
Tejun Heo549d3aa2012-03-05 13:15:16 -0800130 /* invoke per-policy init */
Tejun Heo8bd435b2012-04-13 13:11:28 -0700131 for (i = 0; i < BLKCG_MAX_POLS; i++) {
Tejun Heo3c798392012-04-16 13:57:25 -0700132 struct blkcg_policy *pol = blkcg_policy[i];
Tejun Heoe8989fa2012-03-05 13:15:20 -0800133
Tejun Heoa2b16932012-04-13 13:11:33 -0700134 if (blkcg_policy_enabled(blkg->q, pol))
Tejun Heof9fcc2d2012-04-16 13:57:27 -0700135 pol->pd_init_fn(blkg);
Tejun Heoe8989fa2012-03-05 13:15:20 -0800136 }
137
Tejun Heo03814112012-03-05 13:15:14 -0800138 return blkg;
139}
140
Tejun Heo3c798392012-04-16 13:57:25 -0700141static struct blkcg_gq *__blkg_lookup(struct blkcg *blkcg,
142 struct request_queue *q)
Tejun Heo80fd9972012-04-13 14:50:53 -0700143{
Tejun Heo3c798392012-04-16 13:57:25 -0700144 struct blkcg_gq *blkg;
Tejun Heo80fd9972012-04-13 14:50:53 -0700145 struct hlist_node *n;
146
147 hlist_for_each_entry_rcu(blkg, n, &blkcg->blkg_list, blkcg_node)
148 if (blkg->q == q)
149 return blkg;
150 return NULL;
151}
152
153/**
154 * blkg_lookup - lookup blkg for the specified blkcg - q pair
155 * @blkcg: blkcg of interest
156 * @q: request_queue of interest
157 *
158 * Lookup blkg for the @blkcg - @q pair. This function should be called
159 * under RCU read lock and is guaranteed to return %NULL if @q is bypassing
160 * - see blk_queue_bypass_start() for details.
161 */
Tejun Heo3c798392012-04-16 13:57:25 -0700162struct blkcg_gq *blkg_lookup(struct blkcg *blkcg, struct request_queue *q)
Tejun Heo80fd9972012-04-13 14:50:53 -0700163{
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 Heo3c798392012-04-16 13:57:25 -0700172static struct blkcg_gq *__blkg_lookup_create(struct blkcg *blkcg,
173 struct request_queue *q)
Tejun Heocd1604f2012-03-05 13:15:06 -0800174 __releases(q->queue_lock) __acquires(q->queue_lock)
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400175{
Tejun Heo3c798392012-04-16 13:57:25 -0700176 struct blkcg_gq *blkg;
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400177
Tejun Heocd1604f2012-03-05 13:15:06 -0800178 WARN_ON_ONCE(!rcu_read_lock_held());
179 lockdep_assert_held(q->queue_lock);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500180
Tejun Heo80fd9972012-04-13 14:50:53 -0700181 blkg = __blkg_lookup(blkcg, q);
Tejun Heocd1604f2012-03-05 13:15:06 -0800182 if (blkg)
183 return blkg;
184
Tejun Heo7ee9c562012-03-05 13:15:11 -0800185 /* blkg holds a reference to blkcg */
Tejun Heocd1604f2012-03-05 13:15:06 -0800186 if (!css_tryget(&blkcg->css))
187 return ERR_PTR(-EINVAL);
188
189 /*
190 * Allocate and initialize.
Tejun Heocd1604f2012-03-05 13:15:06 -0800191 */
Vivek Goyal1cd9e032012-03-08 10:53:56 -0800192 blkg = blkg_alloc(blkcg, q);
Tejun Heocd1604f2012-03-05 13:15:06 -0800193
194 /* did alloc fail? */
Vivek Goyal1cd9e032012-03-08 10:53:56 -0800195 if (unlikely(!blkg)) {
Tejun Heocd1604f2012-03-05 13:15:06 -0800196 blkg = ERR_PTR(-ENOMEM);
197 goto out;
198 }
199
200 /* insert */
201 spin_lock(&blkcg->lock);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500202 hlist_add_head_rcu(&blkg->blkcg_node, &blkcg->blkg_list);
Tejun Heoe8989fa2012-03-05 13:15:20 -0800203 list_add(&blkg->q_node, &q->blkg_list);
Tejun Heocd1604f2012-03-05 13:15:06 -0800204 spin_unlock(&blkcg->lock);
205out:
Tejun Heocd1604f2012-03-05 13:15:06 -0800206 return blkg;
Vivek Goyal31e4c282009-12-03 12:59:42 -0500207}
Tejun Heo3c96cb32012-04-13 13:11:34 -0700208
Tejun Heo3c798392012-04-16 13:57:25 -0700209struct blkcg_gq *blkg_lookup_create(struct blkcg *blkcg,
210 struct request_queue *q)
Tejun Heo3c96cb32012-04-13 13:11:34 -0700211{
212 /*
213 * This could be the first entry point of blkcg implementation and
214 * we shouldn't allow anything to go through for a bypassing queue.
215 */
216 if (unlikely(blk_queue_bypass(q)))
217 return ERR_PTR(blk_queue_dead(q) ? -EINVAL : -EBUSY);
218 return __blkg_lookup_create(blkcg, q);
219}
Tejun Heocd1604f2012-03-05 13:15:06 -0800220EXPORT_SYMBOL_GPL(blkg_lookup_create);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500221
Tejun Heo3c798392012-04-16 13:57:25 -0700222static void blkg_destroy(struct blkcg_gq *blkg)
Tejun Heo72e06c22012-03-05 13:15:00 -0800223{
Tejun Heo03aa264a2012-03-05 13:15:19 -0800224 struct request_queue *q = blkg->q;
Tejun Heo3c798392012-04-16 13:57:25 -0700225 struct blkcg *blkcg = blkg->blkcg;
Tejun Heo03aa264a2012-03-05 13:15:19 -0800226
227 lockdep_assert_held(q->queue_lock);
Tejun Heo9f13ef62012-03-05 13:15:21 -0800228 lockdep_assert_held(&blkcg->lock);
Tejun Heo03aa264a2012-03-05 13:15:19 -0800229
230 /* Something wrong if we are trying to remove same group twice */
Tejun Heoe8989fa2012-03-05 13:15:20 -0800231 WARN_ON_ONCE(list_empty(&blkg->q_node));
Tejun Heo9f13ef62012-03-05 13:15:21 -0800232 WARN_ON_ONCE(hlist_unhashed(&blkg->blkcg_node));
Tejun Heoe8989fa2012-03-05 13:15:20 -0800233 list_del_init(&blkg->q_node);
Tejun Heo9f13ef62012-03-05 13:15:21 -0800234 hlist_del_init_rcu(&blkg->blkcg_node);
Tejun Heo03aa264a2012-03-05 13:15:19 -0800235
Tejun Heo03aa264a2012-03-05 13:15:19 -0800236 /*
237 * Put the reference taken at the time of creation so that when all
238 * queues are gone, group can be destroyed.
239 */
240 blkg_put(blkg);
241}
242
Tejun Heo9f13ef62012-03-05 13:15:21 -0800243/**
244 * blkg_destroy_all - destroy all blkgs associated with a request_queue
245 * @q: request_queue of interest
Tejun Heo9f13ef62012-03-05 13:15:21 -0800246 *
Tejun Heo3c96cb32012-04-13 13:11:34 -0700247 * Destroy all blkgs associated with @q.
Tejun Heo9f13ef62012-03-05 13:15:21 -0800248 */
Tejun Heo3c96cb32012-04-13 13:11:34 -0700249static void blkg_destroy_all(struct request_queue *q)
Tejun Heo03aa264a2012-03-05 13:15:19 -0800250{
Tejun Heo3c798392012-04-16 13:57:25 -0700251 struct blkcg_gq *blkg, *n;
Tejun Heo72e06c22012-03-05 13:15:00 -0800252
Tejun Heo6d18b002012-04-13 13:11:35 -0700253 lockdep_assert_held(q->queue_lock);
Tejun Heo72e06c22012-03-05 13:15:00 -0800254
Tejun Heo9f13ef62012-03-05 13:15:21 -0800255 list_for_each_entry_safe(blkg, n, &q->blkg_list, q_node) {
Tejun Heo3c798392012-04-16 13:57:25 -0700256 struct blkcg *blkcg = blkg->blkcg;
Tejun Heo72e06c22012-03-05 13:15:00 -0800257
Tejun Heo9f13ef62012-03-05 13:15:21 -0800258 spin_lock(&blkcg->lock);
259 blkg_destroy(blkg);
260 spin_unlock(&blkcg->lock);
Tejun Heo72e06c22012-03-05 13:15:00 -0800261 }
262}
263
Tejun Heo1adaf3d2012-03-05 13:15:15 -0800264static void blkg_rcu_free(struct rcu_head *rcu_head)
265{
Tejun Heo3c798392012-04-16 13:57:25 -0700266 blkg_free(container_of(rcu_head, struct blkcg_gq, rcu_head));
Tejun Heo1adaf3d2012-03-05 13:15:15 -0800267}
268
Tejun Heo3c798392012-04-16 13:57:25 -0700269void __blkg_release(struct blkcg_gq *blkg)
Tejun Heo1adaf3d2012-03-05 13:15:15 -0800270{
271 /* release the extra blkcg reference this blkg has been holding */
272 css_put(&blkg->blkcg->css);
273
274 /*
275 * A group is freed in rcu manner. But having an rcu lock does not
276 * mean that one can access all the fields of blkg and assume these
277 * are valid. For example, don't try to follow throtl_data and
278 * request queue links.
279 *
280 * Having a reference to blkg under an rcu allows acess to only
281 * values local to groups like group stats and group rate limits
282 */
283 call_rcu(&blkg->rcu_head, blkg_rcu_free);
284}
285EXPORT_SYMBOL_GPL(__blkg_release);
286
Tejun Heo3c798392012-04-16 13:57:25 -0700287static int blkcg_reset_stats(struct cgroup *cgroup, struct cftype *cftype,
288 u64 val)
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700289{
Tejun Heo3c798392012-04-16 13:57:25 -0700290 struct blkcg *blkcg = cgroup_to_blkcg(cgroup);
291 struct blkcg_gq *blkg;
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700292 struct hlist_node *n;
Tejun Heobc0d6502012-04-13 13:11:26 -0700293 int i;
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700294
Tejun Heobc0d6502012-04-13 13:11:26 -0700295 mutex_lock(&blkcg_pol_mutex);
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700296 spin_lock_irq(&blkcg->lock);
Tejun Heo997a0262012-03-08 10:53:58 -0800297
298 /*
299 * Note that stat reset is racy - it doesn't synchronize against
300 * stat updates. This is a debug feature which shouldn't exist
301 * anyway. If you get hit by a race, retry.
302 */
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700303 hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) {
Tejun Heo8bd435b2012-04-13 13:11:28 -0700304 for (i = 0; i < BLKCG_MAX_POLS; i++) {
Tejun Heo3c798392012-04-16 13:57:25 -0700305 struct blkcg_policy *pol = blkcg_policy[i];
Tejun Heo549d3aa2012-03-05 13:15:16 -0800306
Tejun Heoa2b16932012-04-13 13:11:33 -0700307 if (blkcg_policy_enabled(blkg->q, pol) &&
Tejun Heof9fcc2d2012-04-16 13:57:27 -0700308 pol->pd_reset_stats_fn)
309 pol->pd_reset_stats_fn(blkg);
Tejun Heobc0d6502012-04-13 13:11:26 -0700310 }
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700311 }
Vivek Goyalf0bdc8c2011-05-19 15:38:30 -0400312
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700313 spin_unlock_irq(&blkcg->lock);
Tejun Heobc0d6502012-04-13 13:11:26 -0700314 mutex_unlock(&blkcg_pol_mutex);
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700315 return 0;
316}
317
Tejun Heo3c798392012-04-16 13:57:25 -0700318static const char *blkg_dev_name(struct blkcg_gq *blkg)
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700319{
Tejun Heod3d32e692012-04-01 14:38:42 -0700320 /* some drivers (floppy) instantiate a queue w/o disk registered */
321 if (blkg->q->backing_dev_info.dev)
322 return dev_name(blkg->q->backing_dev_info.dev);
323 return NULL;
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700324}
325
Tejun Heod3d32e692012-04-01 14:38:42 -0700326/**
327 * blkcg_print_blkgs - helper for printing per-blkg data
328 * @sf: seq_file to print to
329 * @blkcg: blkcg of interest
330 * @prfill: fill function to print out a blkg
331 * @pol: policy in question
332 * @data: data to be passed to @prfill
333 * @show_total: to print out sum of prfill return values or not
334 *
335 * This function invokes @prfill on each blkg of @blkcg if pd for the
336 * policy specified by @pol exists. @prfill is invoked with @sf, the
337 * policy data and @data. If @show_total is %true, the sum of the return
338 * values from @prfill is printed with "Total" label at the end.
339 *
340 * This is to be used to construct print functions for
341 * cftype->read_seq_string method.
342 */
Tejun Heo3c798392012-04-16 13:57:25 -0700343void blkcg_print_blkgs(struct seq_file *sf, struct blkcg *blkcg,
Tejun Heof95a04a2012-04-16 13:57:26 -0700344 u64 (*prfill)(struct seq_file *,
345 struct blkg_policy_data *, int),
Tejun Heo3c798392012-04-16 13:57:25 -0700346 const struct blkcg_policy *pol, int data,
Tejun Heoec399342012-04-13 13:11:27 -0700347 bool show_total)
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400348{
Tejun Heo3c798392012-04-16 13:57:25 -0700349 struct blkcg_gq *blkg;
Tejun Heod3d32e692012-04-01 14:38:42 -0700350 struct hlist_node *n;
351 u64 total = 0;
352
353 spin_lock_irq(&blkcg->lock);
354 hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node)
Tejun Heoa2b16932012-04-13 13:11:33 -0700355 if (blkcg_policy_enabled(blkg->q, pol))
Tejun Heof95a04a2012-04-16 13:57:26 -0700356 total += prfill(sf, blkg->pd[pol->plid], data);
Tejun Heod3d32e692012-04-01 14:38:42 -0700357 spin_unlock_irq(&blkcg->lock);
358
359 if (show_total)
360 seq_printf(sf, "Total %llu\n", (unsigned long long)total);
361}
Tejun Heo829fdb52012-04-01 14:38:43 -0700362EXPORT_SYMBOL_GPL(blkcg_print_blkgs);
Tejun Heod3d32e692012-04-01 14:38:42 -0700363
364/**
365 * __blkg_prfill_u64 - prfill helper for a single u64 value
366 * @sf: seq_file to print to
Tejun Heof95a04a2012-04-16 13:57:26 -0700367 * @pd: policy private data of interest
Tejun Heod3d32e692012-04-01 14:38:42 -0700368 * @v: value to print
369 *
Tejun Heof95a04a2012-04-16 13:57:26 -0700370 * Print @v to @sf for the device assocaited with @pd.
Tejun Heod3d32e692012-04-01 14:38:42 -0700371 */
Tejun Heof95a04a2012-04-16 13:57:26 -0700372u64 __blkg_prfill_u64(struct seq_file *sf, struct blkg_policy_data *pd, u64 v)
Tejun Heod3d32e692012-04-01 14:38:42 -0700373{
Tejun Heof95a04a2012-04-16 13:57:26 -0700374 const char *dname = blkg_dev_name(pd->blkg);
Tejun Heod3d32e692012-04-01 14:38:42 -0700375
376 if (!dname)
377 return 0;
378
379 seq_printf(sf, "%s %llu\n", dname, (unsigned long long)v);
380 return v;
381}
Tejun Heo829fdb52012-04-01 14:38:43 -0700382EXPORT_SYMBOL_GPL(__blkg_prfill_u64);
Tejun Heod3d32e692012-04-01 14:38:42 -0700383
384/**
385 * __blkg_prfill_rwstat - prfill helper for a blkg_rwstat
386 * @sf: seq_file to print to
Tejun Heof95a04a2012-04-16 13:57:26 -0700387 * @pd: policy private data of interest
Tejun Heod3d32e692012-04-01 14:38:42 -0700388 * @rwstat: rwstat to print
389 *
Tejun Heof95a04a2012-04-16 13:57:26 -0700390 * Print @rwstat to @sf for the device assocaited with @pd.
Tejun Heod3d32e692012-04-01 14:38:42 -0700391 */
Tejun Heof95a04a2012-04-16 13:57:26 -0700392u64 __blkg_prfill_rwstat(struct seq_file *sf, struct blkg_policy_data *pd,
Tejun Heo829fdb52012-04-01 14:38:43 -0700393 const struct blkg_rwstat *rwstat)
Tejun Heod3d32e692012-04-01 14:38:42 -0700394{
395 static const char *rwstr[] = {
396 [BLKG_RWSTAT_READ] = "Read",
397 [BLKG_RWSTAT_WRITE] = "Write",
398 [BLKG_RWSTAT_SYNC] = "Sync",
399 [BLKG_RWSTAT_ASYNC] = "Async",
400 };
Tejun Heof95a04a2012-04-16 13:57:26 -0700401 const char *dname = blkg_dev_name(pd->blkg);
Tejun Heod3d32e692012-04-01 14:38:42 -0700402 u64 v;
403 int i;
404
405 if (!dname)
406 return 0;
407
408 for (i = 0; i < BLKG_RWSTAT_NR; i++)
409 seq_printf(sf, "%s %s %llu\n", dname, rwstr[i],
410 (unsigned long long)rwstat->cnt[i]);
411
412 v = rwstat->cnt[BLKG_RWSTAT_READ] + rwstat->cnt[BLKG_RWSTAT_WRITE];
413 seq_printf(sf, "%s Total %llu\n", dname, (unsigned long long)v);
414 return v;
415}
416
Tejun Heo5bc4afb12012-04-01 14:38:45 -0700417/**
418 * blkg_prfill_stat - prfill callback for blkg_stat
419 * @sf: seq_file to print to
Tejun Heof95a04a2012-04-16 13:57:26 -0700420 * @pd: policy private data of interest
421 * @off: offset to the blkg_stat in @pd
Tejun Heo5bc4afb12012-04-01 14:38:45 -0700422 *
423 * prfill callback for printing a blkg_stat.
424 */
Tejun Heof95a04a2012-04-16 13:57:26 -0700425u64 blkg_prfill_stat(struct seq_file *sf, struct blkg_policy_data *pd, int off)
Tejun Heod3d32e692012-04-01 14:38:42 -0700426{
Tejun Heof95a04a2012-04-16 13:57:26 -0700427 return __blkg_prfill_u64(sf, pd, blkg_stat_read((void *)pd + off));
Tejun Heod3d32e692012-04-01 14:38:42 -0700428}
Tejun Heo5bc4afb12012-04-01 14:38:45 -0700429EXPORT_SYMBOL_GPL(blkg_prfill_stat);
Tejun Heod3d32e692012-04-01 14:38:42 -0700430
Tejun Heo5bc4afb12012-04-01 14:38:45 -0700431/**
432 * blkg_prfill_rwstat - prfill callback for blkg_rwstat
433 * @sf: seq_file to print to
Tejun Heof95a04a2012-04-16 13:57:26 -0700434 * @pd: policy private data of interest
435 * @off: offset to the blkg_rwstat in @pd
Tejun Heo5bc4afb12012-04-01 14:38:45 -0700436 *
437 * prfill callback for printing a blkg_rwstat.
438 */
Tejun Heof95a04a2012-04-16 13:57:26 -0700439u64 blkg_prfill_rwstat(struct seq_file *sf, struct blkg_policy_data *pd,
440 int off)
Tejun Heod3d32e692012-04-01 14:38:42 -0700441{
Tejun Heof95a04a2012-04-16 13:57:26 -0700442 struct blkg_rwstat rwstat = blkg_rwstat_read((void *)pd + off);
Tejun Heod3d32e692012-04-01 14:38:42 -0700443
Tejun Heof95a04a2012-04-16 13:57:26 -0700444 return __blkg_prfill_rwstat(sf, pd, &rwstat);
Tejun Heod3d32e692012-04-01 14:38:42 -0700445}
Tejun Heo5bc4afb12012-04-01 14:38:45 -0700446EXPORT_SYMBOL_GPL(blkg_prfill_rwstat);
Tejun Heod3d32e692012-04-01 14:38:42 -0700447
Tejun Heo3a8b31d2012-04-01 14:38:43 -0700448/**
449 * blkg_conf_prep - parse and prepare for per-blkg config update
450 * @blkcg: target block cgroup
Tejun Heoda8b0662012-04-13 13:11:29 -0700451 * @pol: target policy
Tejun Heo3a8b31d2012-04-01 14:38:43 -0700452 * @input: input string
453 * @ctx: blkg_conf_ctx to be filled
454 *
455 * Parse per-blkg config update from @input and initialize @ctx with the
456 * result. @ctx->blkg points to the blkg to be updated and @ctx->v the new
Tejun Heoda8b0662012-04-13 13:11:29 -0700457 * value. This function returns with RCU read lock and queue lock held and
458 * must be paired with blkg_conf_finish().
Tejun Heo3a8b31d2012-04-01 14:38:43 -0700459 */
Tejun Heo3c798392012-04-16 13:57:25 -0700460int blkg_conf_prep(struct blkcg *blkcg, const struct blkcg_policy *pol,
461 const char *input, struct blkg_conf_ctx *ctx)
Tejun Heoda8b0662012-04-13 13:11:29 -0700462 __acquires(rcu) __acquires(disk->queue->queue_lock)
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800463{
Tejun Heo3a8b31d2012-04-01 14:38:43 -0700464 struct gendisk *disk;
Tejun Heo3c798392012-04-16 13:57:25 -0700465 struct blkcg_gq *blkg;
Tejun Heo726fa692012-04-01 14:38:43 -0700466 unsigned int major, minor;
467 unsigned long long v;
468 int part, ret;
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800469
Tejun Heo726fa692012-04-01 14:38:43 -0700470 if (sscanf(input, "%u:%u %llu", &major, &minor, &v) != 3)
471 return -EINVAL;
Tejun Heo3a8b31d2012-04-01 14:38:43 -0700472
Tejun Heo726fa692012-04-01 14:38:43 -0700473 disk = get_gendisk(MKDEV(major, minor), &part);
Tejun Heo4bfd4822012-03-05 13:15:08 -0800474 if (!disk || part)
Tejun Heo726fa692012-04-01 14:38:43 -0700475 return -EINVAL;
Tejun Heoe56da7e2012-03-05 13:15:07 -0800476
477 rcu_read_lock();
Tejun Heo4bfd4822012-03-05 13:15:08 -0800478 spin_lock_irq(disk->queue->queue_lock);
Tejun Heoda8b0662012-04-13 13:11:29 -0700479
Tejun Heoa2b16932012-04-13 13:11:33 -0700480 if (blkcg_policy_enabled(disk->queue, pol))
Tejun Heo3c96cb32012-04-13 13:11:34 -0700481 blkg = blkg_lookup_create(blkcg, disk->queue);
Tejun Heoa2b16932012-04-13 13:11:33 -0700482 else
483 blkg = ERR_PTR(-EINVAL);
Tejun Heoe56da7e2012-03-05 13:15:07 -0800484
Tejun Heo4bfd4822012-03-05 13:15:08 -0800485 if (IS_ERR(blkg)) {
486 ret = PTR_ERR(blkg);
Tejun Heo3a8b31d2012-04-01 14:38:43 -0700487 rcu_read_unlock();
Tejun Heoda8b0662012-04-13 13:11:29 -0700488 spin_unlock_irq(disk->queue->queue_lock);
Tejun Heo3a8b31d2012-04-01 14:38:43 -0700489 put_disk(disk);
490 /*
491 * If queue was bypassing, we should retry. Do so after a
492 * short msleep(). It isn't strictly necessary but queue
493 * can be bypassing for some time and it's always nice to
494 * avoid busy looping.
495 */
496 if (ret == -EBUSY) {
497 msleep(10);
498 ret = restart_syscall();
Vivek Goyal7702e8f2010-09-15 17:06:36 -0400499 }
Tejun Heo726fa692012-04-01 14:38:43 -0700500 return ret;
Vivek Goyal062a6442010-09-15 17:06:33 -0400501 }
Tejun Heoe56da7e2012-03-05 13:15:07 -0800502
Tejun Heo3a8b31d2012-04-01 14:38:43 -0700503 ctx->disk = disk;
504 ctx->blkg = blkg;
Tejun Heo726fa692012-04-01 14:38:43 -0700505 ctx->v = v;
506 return 0;
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800507}
Tejun Heo829fdb52012-04-01 14:38:43 -0700508EXPORT_SYMBOL_GPL(blkg_conf_prep);
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800509
Tejun Heo3a8b31d2012-04-01 14:38:43 -0700510/**
511 * blkg_conf_finish - finish up per-blkg config update
512 * @ctx: blkg_conf_ctx intiailized by blkg_conf_prep()
513 *
514 * Finish up after per-blkg config update. This function must be paired
515 * with blkg_conf_prep().
516 */
Tejun Heo829fdb52012-04-01 14:38:43 -0700517void blkg_conf_finish(struct blkg_conf_ctx *ctx)
Tejun Heoda8b0662012-04-13 13:11:29 -0700518 __releases(ctx->disk->queue->queue_lock) __releases(rcu)
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800519{
Tejun Heoda8b0662012-04-13 13:11:29 -0700520 spin_unlock_irq(ctx->disk->queue->queue_lock);
Tejun Heo3a8b31d2012-04-01 14:38:43 -0700521 rcu_read_unlock();
522 put_disk(ctx->disk);
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800523}
Tejun Heo829fdb52012-04-01 14:38:43 -0700524EXPORT_SYMBOL_GPL(blkg_conf_finish);
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800525
Tejun Heo3c798392012-04-16 13:57:25 -0700526struct cftype blkcg_files[] = {
Vivek Goyal31e4c282009-12-03 12:59:42 -0500527 {
Divyesh Shah84c124d2010-04-09 08:31:19 +0200528 .name = "reset_stats",
Tejun Heo3c798392012-04-16 13:57:25 -0700529 .write_u64 = blkcg_reset_stats,
Vivek Goyal22084192009-12-03 12:59:49 -0500530 },
Tejun Heo4baf6e32012-04-01 12:09:55 -0700531 { } /* terminate */
Vivek Goyal31e4c282009-12-03 12:59:42 -0500532};
533
Tejun Heo9f13ef62012-03-05 13:15:21 -0800534/**
Tejun Heo3c798392012-04-16 13:57:25 -0700535 * blkcg_pre_destroy - cgroup pre_destroy callback
Tejun Heo9f13ef62012-03-05 13:15:21 -0800536 * @cgroup: cgroup of interest
537 *
538 * This function is called when @cgroup is about to go away and responsible
539 * for shooting down all blkgs associated with @cgroup. blkgs should be
540 * removed while holding both q and blkcg locks. As blkcg lock is nested
541 * inside q lock, this function performs reverse double lock dancing.
542 *
543 * This is the blkcg counterpart of ioc_release_fn().
544 */
Tejun Heo3c798392012-04-16 13:57:25 -0700545static int blkcg_pre_destroy(struct cgroup *cgroup)
Vivek Goyal31e4c282009-12-03 12:59:42 -0500546{
Tejun Heo3c798392012-04-16 13:57:25 -0700547 struct blkcg *blkcg = cgroup_to_blkcg(cgroup);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500548
Tejun Heo9f13ef62012-03-05 13:15:21 -0800549 spin_lock_irq(&blkcg->lock);
Tejun Heo7ee9c562012-03-05 13:15:11 -0800550
Tejun Heo9f13ef62012-03-05 13:15:21 -0800551 while (!hlist_empty(&blkcg->blkg_list)) {
Tejun Heo3c798392012-04-16 13:57:25 -0700552 struct blkcg_gq *blkg = hlist_entry(blkcg->blkg_list.first,
553 struct blkcg_gq, blkcg_node);
Tejun Heoc875f4d2012-03-05 13:15:22 -0800554 struct request_queue *q = blkg->q;
Vivek Goyalb1c35762009-12-03 12:59:47 -0500555
Tejun Heo9f13ef62012-03-05 13:15:21 -0800556 if (spin_trylock(q->queue_lock)) {
557 blkg_destroy(blkg);
558 spin_unlock(q->queue_lock);
559 } else {
560 spin_unlock_irq(&blkcg->lock);
Tejun Heo9f13ef62012-03-05 13:15:21 -0800561 cpu_relax();
Dan Carpentera5567932012-03-29 20:57:08 +0200562 spin_lock_irq(&blkcg->lock);
Jens Axboe0f3942a2010-05-03 14:28:55 +0200563 }
Tejun Heo9f13ef62012-03-05 13:15:21 -0800564 }
Jens Axboe0f3942a2010-05-03 14:28:55 +0200565
Tejun Heo9f13ef62012-03-05 13:15:21 -0800566 spin_unlock_irq(&blkcg->lock);
Tejun Heo7ee9c562012-03-05 13:15:11 -0800567 return 0;
568}
569
Tejun Heo3c798392012-04-16 13:57:25 -0700570static void blkcg_destroy(struct cgroup *cgroup)
Tejun Heo7ee9c562012-03-05 13:15:11 -0800571{
Tejun Heo3c798392012-04-16 13:57:25 -0700572 struct blkcg *blkcg = cgroup_to_blkcg(cgroup);
Tejun Heo7ee9c562012-03-05 13:15:11 -0800573
Tejun Heo3c798392012-04-16 13:57:25 -0700574 if (blkcg != &blkcg_root)
Ben Blum67523c42010-03-10 15:22:11 -0800575 kfree(blkcg);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500576}
577
Tejun Heo3c798392012-04-16 13:57:25 -0700578static struct cgroup_subsys_state *blkcg_create(struct cgroup *cgroup)
Vivek Goyal31e4c282009-12-03 12:59:42 -0500579{
Tejun Heo9a9e8a22012-03-19 15:10:56 -0700580 static atomic64_t id_seq = ATOMIC64_INIT(0);
Tejun Heo3c798392012-04-16 13:57:25 -0700581 struct blkcg *blkcg;
Li Zefan03415092010-05-07 08:57:00 +0200582 struct cgroup *parent = cgroup->parent;
Vivek Goyal31e4c282009-12-03 12:59:42 -0500583
Li Zefan03415092010-05-07 08:57:00 +0200584 if (!parent) {
Tejun Heo3c798392012-04-16 13:57:25 -0700585 blkcg = &blkcg_root;
Vivek Goyal31e4c282009-12-03 12:59:42 -0500586 goto done;
587 }
588
Vivek Goyal31e4c282009-12-03 12:59:42 -0500589 blkcg = kzalloc(sizeof(*blkcg), GFP_KERNEL);
590 if (!blkcg)
591 return ERR_PTR(-ENOMEM);
592
Tejun Heo3381cb82012-04-01 14:38:44 -0700593 blkcg->cfq_weight = CFQ_WEIGHT_DEFAULT;
Tejun Heo9a9e8a22012-03-19 15:10:56 -0700594 blkcg->id = atomic64_inc_return(&id_seq); /* root is 0, start from 1 */
Vivek Goyal31e4c282009-12-03 12:59:42 -0500595done:
596 spin_lock_init(&blkcg->lock);
597 INIT_HLIST_HEAD(&blkcg->blkg_list);
598
599 return &blkcg->css;
600}
601
Tejun Heo5efd6112012-03-05 13:15:12 -0800602/**
603 * blkcg_init_queue - initialize blkcg part of request queue
604 * @q: request_queue to initialize
605 *
606 * Called from blk_alloc_queue_node(). Responsible for initializing blkcg
607 * part of new request_queue @q.
608 *
609 * RETURNS:
610 * 0 on success, -errno on failure.
611 */
612int blkcg_init_queue(struct request_queue *q)
613{
614 might_sleep();
615
Tejun Heo3c96cb32012-04-13 13:11:34 -0700616 return blk_throtl_init(q);
Tejun Heo5efd6112012-03-05 13:15:12 -0800617}
618
619/**
620 * blkcg_drain_queue - drain blkcg part of request_queue
621 * @q: request_queue to drain
622 *
623 * Called from blk_drain_queue(). Responsible for draining blkcg part.
624 */
625void blkcg_drain_queue(struct request_queue *q)
626{
627 lockdep_assert_held(q->queue_lock);
628
629 blk_throtl_drain(q);
630}
631
632/**
633 * blkcg_exit_queue - exit and release blkcg part of request_queue
634 * @q: request_queue being released
635 *
636 * Called from blk_release_queue(). Responsible for exiting blkcg part.
637 */
638void blkcg_exit_queue(struct request_queue *q)
639{
Tejun Heo6d18b002012-04-13 13:11:35 -0700640 spin_lock_irq(q->queue_lock);
Tejun Heo3c96cb32012-04-13 13:11:34 -0700641 blkg_destroy_all(q);
Tejun Heo6d18b002012-04-13 13:11:35 -0700642 spin_unlock_irq(q->queue_lock);
643
Tejun Heo5efd6112012-03-05 13:15:12 -0800644 blk_throtl_exit(q);
645}
646
Vivek Goyal31e4c282009-12-03 12:59:42 -0500647/*
648 * We cannot support shared io contexts, as we have no mean to support
649 * two tasks with the same ioc in two different groups without major rework
650 * of the main cic data structures. For now we allow a task to change
651 * its cgroup only if it's the only owner of its ioc.
652 */
Tejun Heo3c798392012-04-16 13:57:25 -0700653static int blkcg_can_attach(struct cgroup *cgrp, struct cgroup_taskset *tset)
Vivek Goyal31e4c282009-12-03 12:59:42 -0500654{
Tejun Heobb9d97b2011-12-12 18:12:21 -0800655 struct task_struct *task;
Vivek Goyal31e4c282009-12-03 12:59:42 -0500656 struct io_context *ioc;
657 int ret = 0;
658
659 /* task_lock() is needed to avoid races with exit_io_context() */
Tejun Heobb9d97b2011-12-12 18:12:21 -0800660 cgroup_taskset_for_each(task, cgrp, tset) {
661 task_lock(task);
662 ioc = task->io_context;
663 if (ioc && atomic_read(&ioc->nr_tasks) > 1)
664 ret = -EINVAL;
665 task_unlock(task);
666 if (ret)
667 break;
668 }
Vivek Goyal31e4c282009-12-03 12:59:42 -0500669 return ret;
670}
671
Tejun Heo676f7c82012-04-01 12:09:55 -0700672struct cgroup_subsys blkio_subsys = {
673 .name = "blkio",
Tejun Heo3c798392012-04-16 13:57:25 -0700674 .create = blkcg_create,
675 .can_attach = blkcg_can_attach,
676 .pre_destroy = blkcg_pre_destroy,
677 .destroy = blkcg_destroy,
Tejun Heo676f7c82012-04-01 12:09:55 -0700678 .subsys_id = blkio_subsys_id,
Tejun Heo3c798392012-04-16 13:57:25 -0700679 .base_cftypes = blkcg_files,
Tejun Heo676f7c82012-04-01 12:09:55 -0700680 .module = THIS_MODULE,
681};
682EXPORT_SYMBOL_GPL(blkio_subsys);
683
Tejun Heo8bd435b2012-04-13 13:11:28 -0700684/**
Tejun Heoa2b16932012-04-13 13:11:33 -0700685 * blkcg_activate_policy - activate a blkcg policy on a request_queue
686 * @q: request_queue of interest
687 * @pol: blkcg policy to activate
688 *
689 * Activate @pol on @q. Requires %GFP_KERNEL context. @q goes through
690 * bypass mode to populate its blkgs with policy_data for @pol.
691 *
692 * Activation happens with @q bypassed, so nobody would be accessing blkgs
693 * from IO path. Update of each blkg is protected by both queue and blkcg
694 * locks so that holding either lock and testing blkcg_policy_enabled() is
695 * always enough for dereferencing policy data.
696 *
697 * The caller is responsible for synchronizing [de]activations and policy
698 * [un]registerations. Returns 0 on success, -errno on failure.
699 */
700int blkcg_activate_policy(struct request_queue *q,
Tejun Heo3c798392012-04-16 13:57:25 -0700701 const struct blkcg_policy *pol)
Tejun Heoa2b16932012-04-13 13:11:33 -0700702{
703 LIST_HEAD(pds);
Tejun Heo3c798392012-04-16 13:57:25 -0700704 struct blkcg_gq *blkg;
Tejun Heoa2b16932012-04-13 13:11:33 -0700705 struct blkg_policy_data *pd, *n;
706 int cnt = 0, ret;
707
708 if (blkcg_policy_enabled(q, pol))
709 return 0;
710
711 blk_queue_bypass_start(q);
712
713 /* make sure the root blkg exists and count the existing blkgs */
714 spin_lock_irq(q->queue_lock);
715
716 rcu_read_lock();
Tejun Heo3c798392012-04-16 13:57:25 -0700717 blkg = __blkg_lookup_create(&blkcg_root, q);
Tejun Heoa2b16932012-04-13 13:11:33 -0700718 rcu_read_unlock();
719
720 if (IS_ERR(blkg)) {
721 ret = PTR_ERR(blkg);
722 goto out_unlock;
723 }
724 q->root_blkg = blkg;
725
726 list_for_each_entry(blkg, &q->blkg_list, q_node)
727 cnt++;
728
729 spin_unlock_irq(q->queue_lock);
730
731 /* allocate policy_data for all existing blkgs */
732 while (cnt--) {
Tejun Heof95a04a2012-04-16 13:57:26 -0700733 pd = kzalloc_node(pol->pd_size, GFP_KERNEL, q->node);
Tejun Heoa2b16932012-04-13 13:11:33 -0700734 if (!pd) {
735 ret = -ENOMEM;
736 goto out_free;
737 }
738 list_add_tail(&pd->alloc_node, &pds);
739 }
740
741 /*
742 * Install the allocated pds. With @q bypassing, no new blkg
743 * should have been created while the queue lock was dropped.
744 */
745 spin_lock_irq(q->queue_lock);
746
747 list_for_each_entry(blkg, &q->blkg_list, q_node) {
748 if (WARN_ON(list_empty(&pds))) {
749 /* umm... this shouldn't happen, just abort */
750 ret = -ENOMEM;
751 goto out_unlock;
752 }
753 pd = list_first_entry(&pds, struct blkg_policy_data, alloc_node);
754 list_del_init(&pd->alloc_node);
755
756 /* grab blkcg lock too while installing @pd on @blkg */
757 spin_lock(&blkg->blkcg->lock);
758
759 blkg->pd[pol->plid] = pd;
760 pd->blkg = blkg;
Tejun Heof9fcc2d2012-04-16 13:57:27 -0700761 pol->pd_init_fn(blkg);
Tejun Heoa2b16932012-04-13 13:11:33 -0700762
763 spin_unlock(&blkg->blkcg->lock);
764 }
765
766 __set_bit(pol->plid, q->blkcg_pols);
767 ret = 0;
768out_unlock:
769 spin_unlock_irq(q->queue_lock);
770out_free:
771 blk_queue_bypass_end(q);
772 list_for_each_entry_safe(pd, n, &pds, alloc_node)
773 kfree(pd);
774 return ret;
775}
776EXPORT_SYMBOL_GPL(blkcg_activate_policy);
777
778/**
779 * blkcg_deactivate_policy - deactivate a blkcg policy on a request_queue
780 * @q: request_queue of interest
781 * @pol: blkcg policy to deactivate
782 *
783 * Deactivate @pol on @q. Follows the same synchronization rules as
784 * blkcg_activate_policy().
785 */
786void blkcg_deactivate_policy(struct request_queue *q,
Tejun Heo3c798392012-04-16 13:57:25 -0700787 const struct blkcg_policy *pol)
Tejun Heoa2b16932012-04-13 13:11:33 -0700788{
Tejun Heo3c798392012-04-16 13:57:25 -0700789 struct blkcg_gq *blkg;
Tejun Heoa2b16932012-04-13 13:11:33 -0700790
791 if (!blkcg_policy_enabled(q, pol))
792 return;
793
794 blk_queue_bypass_start(q);
795 spin_lock_irq(q->queue_lock);
796
797 __clear_bit(pol->plid, q->blkcg_pols);
798
Tejun Heo6d18b002012-04-13 13:11:35 -0700799 /* if no policy is left, no need for blkgs - shoot them down */
800 if (bitmap_empty(q->blkcg_pols, BLKCG_MAX_POLS))
801 blkg_destroy_all(q);
802
Tejun Heoa2b16932012-04-13 13:11:33 -0700803 list_for_each_entry(blkg, &q->blkg_list, q_node) {
804 /* grab blkcg lock too while removing @pd from @blkg */
805 spin_lock(&blkg->blkcg->lock);
806
Tejun Heof9fcc2d2012-04-16 13:57:27 -0700807 if (pol->pd_exit_fn)
808 pol->pd_exit_fn(blkg);
Tejun Heoa2b16932012-04-13 13:11:33 -0700809
810 kfree(blkg->pd[pol->plid]);
811 blkg->pd[pol->plid] = NULL;
812
813 spin_unlock(&blkg->blkcg->lock);
814 }
815
816 spin_unlock_irq(q->queue_lock);
817 blk_queue_bypass_end(q);
818}
819EXPORT_SYMBOL_GPL(blkcg_deactivate_policy);
820
821/**
Tejun Heo3c798392012-04-16 13:57:25 -0700822 * blkcg_policy_register - register a blkcg policy
823 * @pol: blkcg policy to register
Tejun Heo8bd435b2012-04-13 13:11:28 -0700824 *
Tejun Heo3c798392012-04-16 13:57:25 -0700825 * Register @pol with blkcg core. Might sleep and @pol may be modified on
826 * successful registration. Returns 0 on success and -errno on failure.
Tejun Heo8bd435b2012-04-13 13:11:28 -0700827 */
Tejun Heo3c798392012-04-16 13:57:25 -0700828int blkcg_policy_register(struct blkcg_policy *pol)
Vivek Goyal3e252062009-12-04 10:36:42 -0500829{
Tejun Heo8bd435b2012-04-13 13:11:28 -0700830 int i, ret;
Tejun Heoe8989fa2012-03-05 13:15:20 -0800831
Tejun Heof95a04a2012-04-16 13:57:26 -0700832 if (WARN_ON(pol->pd_size < sizeof(struct blkg_policy_data)))
833 return -EINVAL;
834
Tejun Heobc0d6502012-04-13 13:11:26 -0700835 mutex_lock(&blkcg_pol_mutex);
836
Tejun Heo8bd435b2012-04-13 13:11:28 -0700837 /* find an empty slot */
838 ret = -ENOSPC;
839 for (i = 0; i < BLKCG_MAX_POLS; i++)
Tejun Heo3c798392012-04-16 13:57:25 -0700840 if (!blkcg_policy[i])
Tejun Heo8bd435b2012-04-13 13:11:28 -0700841 break;
842 if (i >= BLKCG_MAX_POLS)
843 goto out_unlock;
Tejun Heo035d10b2012-03-05 13:15:04 -0800844
Tejun Heo8bd435b2012-04-13 13:11:28 -0700845 /* register and update blkgs */
Tejun Heo3c798392012-04-16 13:57:25 -0700846 pol->plid = i;
847 blkcg_policy[i] = pol;
Tejun Heo8bd435b2012-04-13 13:11:28 -0700848
Tejun Heo8bd435b2012-04-13 13:11:28 -0700849 /* everything is in place, add intf files for the new policy */
Tejun Heo3c798392012-04-16 13:57:25 -0700850 if (pol->cftypes)
851 WARN_ON(cgroup_add_cftypes(&blkio_subsys, pol->cftypes));
Tejun Heo8bd435b2012-04-13 13:11:28 -0700852 ret = 0;
853out_unlock:
Tejun Heobc0d6502012-04-13 13:11:26 -0700854 mutex_unlock(&blkcg_pol_mutex);
Tejun Heo8bd435b2012-04-13 13:11:28 -0700855 return ret;
Vivek Goyal3e252062009-12-04 10:36:42 -0500856}
Tejun Heo3c798392012-04-16 13:57:25 -0700857EXPORT_SYMBOL_GPL(blkcg_policy_register);
Vivek Goyal3e252062009-12-04 10:36:42 -0500858
Tejun Heo8bd435b2012-04-13 13:11:28 -0700859/**
Tejun Heo3c798392012-04-16 13:57:25 -0700860 * blkcg_policy_unregister - unregister a blkcg policy
861 * @pol: blkcg policy to unregister
Tejun Heo8bd435b2012-04-13 13:11:28 -0700862 *
Tejun Heo3c798392012-04-16 13:57:25 -0700863 * Undo blkcg_policy_register(@pol). Might sleep.
Tejun Heo8bd435b2012-04-13 13:11:28 -0700864 */
Tejun Heo3c798392012-04-16 13:57:25 -0700865void blkcg_policy_unregister(struct blkcg_policy *pol)
Vivek Goyal3e252062009-12-04 10:36:42 -0500866{
Tejun Heobc0d6502012-04-13 13:11:26 -0700867 mutex_lock(&blkcg_pol_mutex);
868
Tejun Heo3c798392012-04-16 13:57:25 -0700869 if (WARN_ON(blkcg_policy[pol->plid] != pol))
Tejun Heo8bd435b2012-04-13 13:11:28 -0700870 goto out_unlock;
871
872 /* kill the intf files first */
Tejun Heo3c798392012-04-16 13:57:25 -0700873 if (pol->cftypes)
874 cgroup_rm_cftypes(&blkio_subsys, pol->cftypes);
Tejun Heo44ea53d2012-04-01 14:38:43 -0700875
Tejun Heo8bd435b2012-04-13 13:11:28 -0700876 /* unregister and update blkgs */
Tejun Heo3c798392012-04-16 13:57:25 -0700877 blkcg_policy[pol->plid] = NULL;
Tejun Heo8bd435b2012-04-13 13:11:28 -0700878out_unlock:
Tejun Heobc0d6502012-04-13 13:11:26 -0700879 mutex_unlock(&blkcg_pol_mutex);
Vivek Goyal3e252062009-12-04 10:36:42 -0500880}
Tejun Heo3c798392012-04-16 13:57:25 -0700881EXPORT_SYMBOL_GPL(blkcg_policy_unregister);