blob: 96248d2578db347a4e3df5aa94644e92b596a7a8 [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 Heo15974992012-06-04 20:40:52 -070094 * @gfp_mask: allocation mask to use
Tejun Heo03814112012-03-05 13:15:14 -080095 *
Tejun Heoe8989fa2012-03-05 13:15:20 -080096 * Allocate a new blkg assocating @blkcg and @q.
Tejun Heo03814112012-03-05 13:15:14 -080097 */
Tejun Heo15974992012-06-04 20:40:52 -070098static struct blkcg_gq *blkg_alloc(struct blkcg *blkcg, struct request_queue *q,
99 gfp_t gfp_mask)
Tejun Heo03814112012-03-05 13:15:14 -0800100{
Tejun Heo3c798392012-04-16 13:57:25 -0700101 struct blkcg_gq *blkg;
Tejun Heoe8989fa2012-03-05 13:15:20 -0800102 int i;
Tejun Heo03814112012-03-05 13:15:14 -0800103
104 /* alloc and init base part */
Tejun Heo15974992012-06-04 20:40:52 -0700105 blkg = kzalloc_node(sizeof(*blkg), gfp_mask, q->node);
Tejun Heo03814112012-03-05 13:15:14 -0800106 if (!blkg)
107 return NULL;
108
Tejun Heoc875f4d2012-03-05 13:15:22 -0800109 blkg->q = q;
Tejun Heoe8989fa2012-03-05 13:15:20 -0800110 INIT_LIST_HEAD(&blkg->q_node);
Tejun Heo03814112012-03-05 13:15:14 -0800111 blkg->blkcg = blkcg;
Tejun Heo1adaf3d2012-03-05 13:15:15 -0800112 blkg->refcnt = 1;
Tejun Heo03814112012-03-05 13:15:14 -0800113
Tejun Heo8bd435b2012-04-13 13:11:28 -0700114 for (i = 0; i < BLKCG_MAX_POLS; i++) {
Tejun Heo3c798392012-04-16 13:57:25 -0700115 struct blkcg_policy *pol = blkcg_policy[i];
Tejun Heoe8989fa2012-03-05 13:15:20 -0800116 struct blkg_policy_data *pd;
Tejun Heo03814112012-03-05 13:15:14 -0800117
Tejun Heoa2b16932012-04-13 13:11:33 -0700118 if (!blkcg_policy_enabled(q, pol))
Tejun Heoe8989fa2012-03-05 13:15:20 -0800119 continue;
Tejun Heo549d3aa2012-03-05 13:15:16 -0800120
Tejun Heoe8989fa2012-03-05 13:15:20 -0800121 /* alloc per-policy data and attach it to blkg */
Tejun Heo15974992012-06-04 20:40:52 -0700122 pd = kzalloc_node(pol->pd_size, gfp_mask, q->node);
Tejun Heoe8989fa2012-03-05 13:15:20 -0800123 if (!pd) {
124 blkg_free(blkg);
125 return NULL;
126 }
Tejun Heo549d3aa2012-03-05 13:15:16 -0800127
Tejun Heoe8989fa2012-03-05 13:15:20 -0800128 blkg->pd[i] = pd;
129 pd->blkg = blkg;
Tejun Heo03814112012-03-05 13:15:14 -0800130
Tejun Heo9b2ea862012-06-04 15:21:00 +0900131 /* invoke per-policy init */
Tejun Heoa2b16932012-04-13 13:11:33 -0700132 if (blkcg_policy_enabled(blkg->q, pol))
Tejun Heof9fcc2d2012-04-16 13:57:27 -0700133 pol->pd_init_fn(blkg);
Tejun Heoe8989fa2012-03-05 13:15:20 -0800134 }
135
Tejun Heo03814112012-03-05 13:15:14 -0800136 return blkg;
137}
138
Tejun Heo3c798392012-04-16 13:57:25 -0700139static struct blkcg_gq *__blkg_lookup(struct blkcg *blkcg,
140 struct request_queue *q)
Tejun Heo80fd9972012-04-13 14:50:53 -0700141{
Tejun Heo3c798392012-04-16 13:57:25 -0700142 struct blkcg_gq *blkg;
Tejun Heo80fd9972012-04-13 14:50:53 -0700143
Tejun Heoa6371202012-04-19 16:29:24 -0700144 blkg = rcu_dereference(blkcg->blkg_hint);
145 if (blkg && blkg->q == q)
146 return blkg;
147
148 /*
149 * Hint didn't match. Look up from the radix tree. Note that we
150 * may not be holding queue_lock and thus are not sure whether
151 * @blkg from blkg_tree has already been removed or not, so we
152 * can't update hint to the lookup result. Leave it to the caller.
153 */
154 blkg = radix_tree_lookup(&blkcg->blkg_tree, q->id);
155 if (blkg && blkg->q == q)
156 return blkg;
157
Tejun Heo80fd9972012-04-13 14:50:53 -0700158 return NULL;
159}
160
161/**
162 * blkg_lookup - lookup blkg for the specified blkcg - q pair
163 * @blkcg: blkcg of interest
164 * @q: request_queue of interest
165 *
166 * Lookup blkg for the @blkcg - @q pair. This function should be called
167 * under RCU read lock and is guaranteed to return %NULL if @q is bypassing
168 * - see blk_queue_bypass_start() for details.
169 */
Tejun Heo3c798392012-04-16 13:57:25 -0700170struct blkcg_gq *blkg_lookup(struct blkcg *blkcg, struct request_queue *q)
Tejun Heo80fd9972012-04-13 14:50:53 -0700171{
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 Heo15974992012-06-04 20:40:52 -0700180/*
181 * If @new_blkg is %NULL, this function tries to allocate a new one as
182 * necessary using %GFP_ATOMIC. @new_blkg is always consumed on return.
183 */
Tejun Heo3c798392012-04-16 13:57:25 -0700184static struct blkcg_gq *__blkg_lookup_create(struct blkcg *blkcg,
Tejun Heo15974992012-06-04 20:40:52 -0700185 struct request_queue *q,
186 struct blkcg_gq *new_blkg)
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400187{
Tejun Heo3c798392012-04-16 13:57:25 -0700188 struct blkcg_gq *blkg;
Tejun Heo496fb782012-04-19 16:29:23 -0700189 int ret;
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400190
Tejun Heocd1604f2012-03-05 13:15:06 -0800191 WARN_ON_ONCE(!rcu_read_lock_held());
192 lockdep_assert_held(q->queue_lock);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500193
Tejun Heoa6371202012-04-19 16:29:24 -0700194 /* lookup and update hint on success, see __blkg_lookup() for details */
Tejun Heo80fd9972012-04-13 14:50:53 -0700195 blkg = __blkg_lookup(blkcg, q);
Tejun Heoa6371202012-04-19 16:29:24 -0700196 if (blkg) {
197 rcu_assign_pointer(blkcg->blkg_hint, blkg);
Tejun Heo15974992012-06-04 20:40:52 -0700198 goto out_free;
Tejun Heoa6371202012-04-19 16:29:24 -0700199 }
Tejun Heocd1604f2012-03-05 13:15:06 -0800200
Tejun Heo7ee9c562012-03-05 13:15:11 -0800201 /* blkg holds a reference to blkcg */
Tejun Heo15974992012-06-04 20:40:52 -0700202 if (!css_tryget(&blkcg->css)) {
203 blkg = ERR_PTR(-EINVAL);
204 goto out_free;
205 }
Tejun Heocd1604f2012-03-05 13:15:06 -0800206
Tejun Heo496fb782012-04-19 16:29:23 -0700207 /* allocate */
Tejun Heo15974992012-06-04 20:40:52 -0700208 if (!new_blkg) {
209 new_blkg = blkg_alloc(blkcg, q, GFP_ATOMIC);
210 if (unlikely(!new_blkg)) {
211 blkg = ERR_PTR(-ENOMEM);
212 goto out_put;
213 }
214 }
215 blkg = new_blkg;
Tejun Heocd1604f2012-03-05 13:15:06 -0800216
217 /* insert */
Tejun Heoa6371202012-04-19 16:29:24 -0700218 spin_lock(&blkcg->lock);
219 ret = radix_tree_insert(&blkcg->blkg_tree, q->id, blkg);
220 if (likely(!ret)) {
221 hlist_add_head_rcu(&blkg->blkcg_node, &blkcg->blkg_list);
222 list_add(&blkg->q_node, &q->blkg_list);
223 }
224 spin_unlock(&blkcg->lock);
225
Tejun Heoa6371202012-04-19 16:29:24 -0700226 if (!ret)
227 return blkg;
Tejun Heo15974992012-06-04 20:40:52 -0700228
229 blkg = ERR_PTR(ret);
230out_put:
Tejun Heo496fb782012-04-19 16:29:23 -0700231 css_put(&blkcg->css);
Tejun Heo15974992012-06-04 20:40:52 -0700232out_free:
233 blkg_free(new_blkg);
234 return blkg;
Vivek Goyal31e4c282009-12-03 12:59:42 -0500235}
Tejun Heo3c96cb32012-04-13 13:11:34 -0700236
Tejun Heo3c798392012-04-16 13:57:25 -0700237struct blkcg_gq *blkg_lookup_create(struct blkcg *blkcg,
238 struct request_queue *q)
Tejun Heo3c96cb32012-04-13 13:11:34 -0700239{
240 /*
241 * This could be the first entry point of blkcg implementation and
242 * we shouldn't allow anything to go through for a bypassing queue.
243 */
244 if (unlikely(blk_queue_bypass(q)))
245 return ERR_PTR(blk_queue_dead(q) ? -EINVAL : -EBUSY);
Tejun Heo15974992012-06-04 20:40:52 -0700246 return __blkg_lookup_create(blkcg, q, NULL);
Tejun Heo3c96cb32012-04-13 13:11:34 -0700247}
Tejun Heocd1604f2012-03-05 13:15:06 -0800248EXPORT_SYMBOL_GPL(blkg_lookup_create);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500249
Tejun Heo3c798392012-04-16 13:57:25 -0700250static void blkg_destroy(struct blkcg_gq *blkg)
Tejun Heo72e06c22012-03-05 13:15:00 -0800251{
Tejun Heo3c798392012-04-16 13:57:25 -0700252 struct blkcg *blkcg = blkg->blkcg;
Tejun Heo03aa2642012-03-05 13:15:19 -0800253
Tejun Heo27e1f9d2012-06-05 13:36:44 +0200254 lockdep_assert_held(blkg->q->queue_lock);
Tejun Heo9f13ef62012-03-05 13:15:21 -0800255 lockdep_assert_held(&blkcg->lock);
Tejun Heo03aa2642012-03-05 13:15:19 -0800256
257 /* Something wrong if we are trying to remove same group twice */
Tejun Heoe8989fa2012-03-05 13:15:20 -0800258 WARN_ON_ONCE(list_empty(&blkg->q_node));
Tejun Heo9f13ef62012-03-05 13:15:21 -0800259 WARN_ON_ONCE(hlist_unhashed(&blkg->blkcg_node));
Tejun Heoa6371202012-04-19 16:29:24 -0700260
261 radix_tree_delete(&blkcg->blkg_tree, blkg->q->id);
Tejun Heoe8989fa2012-03-05 13:15:20 -0800262 list_del_init(&blkg->q_node);
Tejun Heo9f13ef62012-03-05 13:15:21 -0800263 hlist_del_init_rcu(&blkg->blkcg_node);
Tejun Heo03aa2642012-03-05 13:15:19 -0800264
Tejun Heo03aa2642012-03-05 13:15:19 -0800265 /*
Tejun Heoa6371202012-04-19 16:29:24 -0700266 * Both setting lookup hint to and clearing it from @blkg are done
267 * under queue_lock. If it's not pointing to @blkg now, it never
268 * will. Hint assignment itself can race safely.
269 */
270 if (rcu_dereference_raw(blkcg->blkg_hint) == blkg)
271 rcu_assign_pointer(blkcg->blkg_hint, NULL);
272
273 /*
Tejun Heo03aa2642012-03-05 13:15:19 -0800274 * Put the reference taken at the time of creation so that when all
275 * queues are gone, group can be destroyed.
276 */
277 blkg_put(blkg);
278}
279
Tejun Heo9f13ef62012-03-05 13:15:21 -0800280/**
281 * blkg_destroy_all - destroy all blkgs associated with a request_queue
282 * @q: request_queue of interest
Tejun Heo9f13ef62012-03-05 13:15:21 -0800283 *
Tejun Heo3c96cb32012-04-13 13:11:34 -0700284 * Destroy all blkgs associated with @q.
Tejun Heo9f13ef62012-03-05 13:15:21 -0800285 */
Tejun Heo3c96cb32012-04-13 13:11:34 -0700286static void blkg_destroy_all(struct request_queue *q)
Tejun Heo03aa2642012-03-05 13:15:19 -0800287{
Tejun Heo3c798392012-04-16 13:57:25 -0700288 struct blkcg_gq *blkg, *n;
Tejun Heo72e06c22012-03-05 13:15:00 -0800289
Tejun Heo6d18b002012-04-13 13:11:35 -0700290 lockdep_assert_held(q->queue_lock);
Tejun Heo72e06c22012-03-05 13:15:00 -0800291
Tejun Heo9f13ef62012-03-05 13:15:21 -0800292 list_for_each_entry_safe(blkg, n, &q->blkg_list, q_node) {
Tejun Heo3c798392012-04-16 13:57:25 -0700293 struct blkcg *blkcg = blkg->blkcg;
Tejun Heo72e06c22012-03-05 13:15:00 -0800294
Tejun Heo9f13ef62012-03-05 13:15:21 -0800295 spin_lock(&blkcg->lock);
296 blkg_destroy(blkg);
297 spin_unlock(&blkcg->lock);
Tejun Heo72e06c22012-03-05 13:15:00 -0800298 }
299}
300
Tejun Heo1adaf3d2012-03-05 13:15:15 -0800301static void blkg_rcu_free(struct rcu_head *rcu_head)
302{
Tejun Heo3c798392012-04-16 13:57:25 -0700303 blkg_free(container_of(rcu_head, struct blkcg_gq, rcu_head));
Tejun Heo1adaf3d2012-03-05 13:15:15 -0800304}
305
Tejun Heo3c798392012-04-16 13:57:25 -0700306void __blkg_release(struct blkcg_gq *blkg)
Tejun Heo1adaf3d2012-03-05 13:15:15 -0800307{
308 /* release the extra blkcg reference this blkg has been holding */
309 css_put(&blkg->blkcg->css);
310
311 /*
312 * A group is freed in rcu manner. But having an rcu lock does not
313 * mean that one can access all the fields of blkg and assume these
314 * are valid. For example, don't try to follow throtl_data and
315 * request queue links.
316 *
317 * Having a reference to blkg under an rcu allows acess to only
318 * values local to groups like group stats and group rate limits
319 */
320 call_rcu(&blkg->rcu_head, blkg_rcu_free);
321}
322EXPORT_SYMBOL_GPL(__blkg_release);
323
Tejun Heo3c798392012-04-16 13:57:25 -0700324static int blkcg_reset_stats(struct cgroup *cgroup, struct cftype *cftype,
325 u64 val)
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700326{
Tejun Heo3c798392012-04-16 13:57:25 -0700327 struct blkcg *blkcg = cgroup_to_blkcg(cgroup);
328 struct blkcg_gq *blkg;
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700329 struct hlist_node *n;
Tejun Heobc0d6502012-04-13 13:11:26 -0700330 int i;
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700331
Tejun Heobc0d6502012-04-13 13:11:26 -0700332 mutex_lock(&blkcg_pol_mutex);
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700333 spin_lock_irq(&blkcg->lock);
Tejun Heo997a0262012-03-08 10:53:58 -0800334
335 /*
336 * Note that stat reset is racy - it doesn't synchronize against
337 * stat updates. This is a debug feature which shouldn't exist
338 * anyway. If you get hit by a race, retry.
339 */
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700340 hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) {
Tejun Heo8bd435b2012-04-13 13:11:28 -0700341 for (i = 0; i < BLKCG_MAX_POLS; i++) {
Tejun Heo3c798392012-04-16 13:57:25 -0700342 struct blkcg_policy *pol = blkcg_policy[i];
Tejun Heo549d3aa2012-03-05 13:15:16 -0800343
Tejun Heoa2b16932012-04-13 13:11:33 -0700344 if (blkcg_policy_enabled(blkg->q, pol) &&
Tejun Heof9fcc2d2012-04-16 13:57:27 -0700345 pol->pd_reset_stats_fn)
346 pol->pd_reset_stats_fn(blkg);
Tejun Heobc0d6502012-04-13 13:11:26 -0700347 }
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700348 }
Vivek Goyalf0bdc8c2011-05-19 15:38:30 -0400349
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700350 spin_unlock_irq(&blkcg->lock);
Tejun Heobc0d6502012-04-13 13:11:26 -0700351 mutex_unlock(&blkcg_pol_mutex);
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700352 return 0;
353}
354
Tejun Heo3c798392012-04-16 13:57:25 -0700355static const char *blkg_dev_name(struct blkcg_gq *blkg)
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700356{
Tejun Heod3d32e62012-04-01 14:38:42 -0700357 /* some drivers (floppy) instantiate a queue w/o disk registered */
358 if (blkg->q->backing_dev_info.dev)
359 return dev_name(blkg->q->backing_dev_info.dev);
360 return NULL;
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700361}
362
Tejun Heod3d32e62012-04-01 14:38:42 -0700363/**
364 * blkcg_print_blkgs - helper for printing per-blkg data
365 * @sf: seq_file to print to
366 * @blkcg: blkcg of interest
367 * @prfill: fill function to print out a blkg
368 * @pol: policy in question
369 * @data: data to be passed to @prfill
370 * @show_total: to print out sum of prfill return values or not
371 *
372 * This function invokes @prfill on each blkg of @blkcg if pd for the
373 * policy specified by @pol exists. @prfill is invoked with @sf, the
374 * policy data and @data. If @show_total is %true, the sum of the return
375 * values from @prfill is printed with "Total" label at the end.
376 *
377 * This is to be used to construct print functions for
378 * cftype->read_seq_string method.
379 */
Tejun Heo3c798392012-04-16 13:57:25 -0700380void blkcg_print_blkgs(struct seq_file *sf, struct blkcg *blkcg,
Tejun Heof95a04a2012-04-16 13:57:26 -0700381 u64 (*prfill)(struct seq_file *,
382 struct blkg_policy_data *, int),
Tejun Heo3c798392012-04-16 13:57:25 -0700383 const struct blkcg_policy *pol, int data,
Tejun Heoec399342012-04-13 13:11:27 -0700384 bool show_total)
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400385{
Tejun Heo3c798392012-04-16 13:57:25 -0700386 struct blkcg_gq *blkg;
Tejun Heod3d32e62012-04-01 14:38:42 -0700387 struct hlist_node *n;
388 u64 total = 0;
389
390 spin_lock_irq(&blkcg->lock);
391 hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node)
Tejun Heoa2b16932012-04-13 13:11:33 -0700392 if (blkcg_policy_enabled(blkg->q, pol))
Tejun Heof95a04a2012-04-16 13:57:26 -0700393 total += prfill(sf, blkg->pd[pol->plid], data);
Tejun Heod3d32e62012-04-01 14:38:42 -0700394 spin_unlock_irq(&blkcg->lock);
395
396 if (show_total)
397 seq_printf(sf, "Total %llu\n", (unsigned long long)total);
398}
Tejun Heo829fdb52012-04-01 14:38:43 -0700399EXPORT_SYMBOL_GPL(blkcg_print_blkgs);
Tejun Heod3d32e62012-04-01 14:38:42 -0700400
401/**
402 * __blkg_prfill_u64 - prfill helper for a single u64 value
403 * @sf: seq_file to print to
Tejun Heof95a04a2012-04-16 13:57:26 -0700404 * @pd: policy private data of interest
Tejun Heod3d32e62012-04-01 14:38:42 -0700405 * @v: value to print
406 *
Tejun Heof95a04a2012-04-16 13:57:26 -0700407 * Print @v to @sf for the device assocaited with @pd.
Tejun Heod3d32e62012-04-01 14:38:42 -0700408 */
Tejun Heof95a04a2012-04-16 13:57:26 -0700409u64 __blkg_prfill_u64(struct seq_file *sf, struct blkg_policy_data *pd, u64 v)
Tejun Heod3d32e62012-04-01 14:38:42 -0700410{
Tejun Heof95a04a2012-04-16 13:57:26 -0700411 const char *dname = blkg_dev_name(pd->blkg);
Tejun Heod3d32e62012-04-01 14:38:42 -0700412
413 if (!dname)
414 return 0;
415
416 seq_printf(sf, "%s %llu\n", dname, (unsigned long long)v);
417 return v;
418}
Tejun Heo829fdb52012-04-01 14:38:43 -0700419EXPORT_SYMBOL_GPL(__blkg_prfill_u64);
Tejun Heod3d32e62012-04-01 14:38:42 -0700420
421/**
422 * __blkg_prfill_rwstat - prfill helper for a blkg_rwstat
423 * @sf: seq_file to print to
Tejun Heof95a04a2012-04-16 13:57:26 -0700424 * @pd: policy private data of interest
Tejun Heod3d32e62012-04-01 14:38:42 -0700425 * @rwstat: rwstat to print
426 *
Tejun Heof95a04a2012-04-16 13:57:26 -0700427 * Print @rwstat to @sf for the device assocaited with @pd.
Tejun Heod3d32e62012-04-01 14:38:42 -0700428 */
Tejun Heof95a04a2012-04-16 13:57:26 -0700429u64 __blkg_prfill_rwstat(struct seq_file *sf, struct blkg_policy_data *pd,
Tejun Heo829fdb52012-04-01 14:38:43 -0700430 const struct blkg_rwstat *rwstat)
Tejun Heod3d32e62012-04-01 14:38:42 -0700431{
432 static const char *rwstr[] = {
433 [BLKG_RWSTAT_READ] = "Read",
434 [BLKG_RWSTAT_WRITE] = "Write",
435 [BLKG_RWSTAT_SYNC] = "Sync",
436 [BLKG_RWSTAT_ASYNC] = "Async",
437 };
Tejun Heof95a04a2012-04-16 13:57:26 -0700438 const char *dname = blkg_dev_name(pd->blkg);
Tejun Heod3d32e62012-04-01 14:38:42 -0700439 u64 v;
440 int i;
441
442 if (!dname)
443 return 0;
444
445 for (i = 0; i < BLKG_RWSTAT_NR; i++)
446 seq_printf(sf, "%s %s %llu\n", dname, rwstr[i],
447 (unsigned long long)rwstat->cnt[i]);
448
449 v = rwstat->cnt[BLKG_RWSTAT_READ] + rwstat->cnt[BLKG_RWSTAT_WRITE];
450 seq_printf(sf, "%s Total %llu\n", dname, (unsigned long long)v);
451 return v;
452}
453
Tejun Heo5bc4afb12012-04-01 14:38:45 -0700454/**
455 * blkg_prfill_stat - prfill callback for blkg_stat
456 * @sf: seq_file to print to
Tejun Heof95a04a2012-04-16 13:57:26 -0700457 * @pd: policy private data of interest
458 * @off: offset to the blkg_stat in @pd
Tejun Heo5bc4afb12012-04-01 14:38:45 -0700459 *
460 * prfill callback for printing a blkg_stat.
461 */
Tejun Heof95a04a2012-04-16 13:57:26 -0700462u64 blkg_prfill_stat(struct seq_file *sf, struct blkg_policy_data *pd, int off)
Tejun Heod3d32e62012-04-01 14:38:42 -0700463{
Tejun Heof95a04a2012-04-16 13:57:26 -0700464 return __blkg_prfill_u64(sf, pd, blkg_stat_read((void *)pd + off));
Tejun Heod3d32e62012-04-01 14:38:42 -0700465}
Tejun Heo5bc4afb12012-04-01 14:38:45 -0700466EXPORT_SYMBOL_GPL(blkg_prfill_stat);
Tejun Heod3d32e62012-04-01 14:38:42 -0700467
Tejun Heo5bc4afb12012-04-01 14:38:45 -0700468/**
469 * blkg_prfill_rwstat - prfill callback for blkg_rwstat
470 * @sf: seq_file to print to
Tejun Heof95a04a2012-04-16 13:57:26 -0700471 * @pd: policy private data of interest
472 * @off: offset to the blkg_rwstat in @pd
Tejun Heo5bc4afb12012-04-01 14:38:45 -0700473 *
474 * prfill callback for printing a blkg_rwstat.
475 */
Tejun Heof95a04a2012-04-16 13:57:26 -0700476u64 blkg_prfill_rwstat(struct seq_file *sf, struct blkg_policy_data *pd,
477 int off)
Tejun Heod3d32e62012-04-01 14:38:42 -0700478{
Tejun Heof95a04a2012-04-16 13:57:26 -0700479 struct blkg_rwstat rwstat = blkg_rwstat_read((void *)pd + off);
Tejun Heod3d32e62012-04-01 14:38:42 -0700480
Tejun Heof95a04a2012-04-16 13:57:26 -0700481 return __blkg_prfill_rwstat(sf, pd, &rwstat);
Tejun Heod3d32e62012-04-01 14:38:42 -0700482}
Tejun Heo5bc4afb12012-04-01 14:38:45 -0700483EXPORT_SYMBOL_GPL(blkg_prfill_rwstat);
Tejun Heod3d32e62012-04-01 14:38:42 -0700484
Tejun Heo3a8b31d2012-04-01 14:38:43 -0700485/**
486 * blkg_conf_prep - parse and prepare for per-blkg config update
487 * @blkcg: target block cgroup
Tejun Heoda8b0662012-04-13 13:11:29 -0700488 * @pol: target policy
Tejun Heo3a8b31d2012-04-01 14:38:43 -0700489 * @input: input string
490 * @ctx: blkg_conf_ctx to be filled
491 *
492 * Parse per-blkg config update from @input and initialize @ctx with the
493 * result. @ctx->blkg points to the blkg to be updated and @ctx->v the new
Tejun Heoda8b0662012-04-13 13:11:29 -0700494 * value. This function returns with RCU read lock and queue lock held and
495 * must be paired with blkg_conf_finish().
Tejun Heo3a8b31d2012-04-01 14:38:43 -0700496 */
Tejun Heo3c798392012-04-16 13:57:25 -0700497int blkg_conf_prep(struct blkcg *blkcg, const struct blkcg_policy *pol,
498 const char *input, struct blkg_conf_ctx *ctx)
Tejun Heoda8b0662012-04-13 13:11:29 -0700499 __acquires(rcu) __acquires(disk->queue->queue_lock)
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800500{
Tejun Heo3a8b31d2012-04-01 14:38:43 -0700501 struct gendisk *disk;
Tejun Heo3c798392012-04-16 13:57:25 -0700502 struct blkcg_gq *blkg;
Tejun Heo726fa692012-04-01 14:38:43 -0700503 unsigned int major, minor;
504 unsigned long long v;
505 int part, ret;
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800506
Tejun Heo726fa692012-04-01 14:38:43 -0700507 if (sscanf(input, "%u:%u %llu", &major, &minor, &v) != 3)
508 return -EINVAL;
Tejun Heo3a8b31d2012-04-01 14:38:43 -0700509
Tejun Heo726fa692012-04-01 14:38:43 -0700510 disk = get_gendisk(MKDEV(major, minor), &part);
Tejun Heo4bfd4822012-03-05 13:15:08 -0800511 if (!disk || part)
Tejun Heo726fa692012-04-01 14:38:43 -0700512 return -EINVAL;
Tejun Heoe56da7e2012-03-05 13:15:07 -0800513
514 rcu_read_lock();
Tejun Heo4bfd4822012-03-05 13:15:08 -0800515 spin_lock_irq(disk->queue->queue_lock);
Tejun Heoda8b0662012-04-13 13:11:29 -0700516
Tejun Heoa2b16932012-04-13 13:11:33 -0700517 if (blkcg_policy_enabled(disk->queue, pol))
Tejun Heo3c96cb32012-04-13 13:11:34 -0700518 blkg = blkg_lookup_create(blkcg, disk->queue);
Tejun Heoa2b16932012-04-13 13:11:33 -0700519 else
520 blkg = ERR_PTR(-EINVAL);
Tejun Heoe56da7e2012-03-05 13:15:07 -0800521
Tejun Heo4bfd4822012-03-05 13:15:08 -0800522 if (IS_ERR(blkg)) {
523 ret = PTR_ERR(blkg);
Tejun Heo3a8b31d2012-04-01 14:38:43 -0700524 rcu_read_unlock();
Tejun Heoda8b0662012-04-13 13:11:29 -0700525 spin_unlock_irq(disk->queue->queue_lock);
Tejun Heo3a8b31d2012-04-01 14:38:43 -0700526 put_disk(disk);
527 /*
528 * If queue was bypassing, we should retry. Do so after a
529 * short msleep(). It isn't strictly necessary but queue
530 * can be bypassing for some time and it's always nice to
531 * avoid busy looping.
532 */
533 if (ret == -EBUSY) {
534 msleep(10);
535 ret = restart_syscall();
Vivek Goyal7702e8f2010-09-15 17:06:36 -0400536 }
Tejun Heo726fa692012-04-01 14:38:43 -0700537 return ret;
Vivek Goyal062a6442010-09-15 17:06:33 -0400538 }
Tejun Heoe56da7e2012-03-05 13:15:07 -0800539
Tejun Heo3a8b31d2012-04-01 14:38:43 -0700540 ctx->disk = disk;
541 ctx->blkg = blkg;
Tejun Heo726fa692012-04-01 14:38:43 -0700542 ctx->v = v;
543 return 0;
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800544}
Tejun Heo829fdb52012-04-01 14:38:43 -0700545EXPORT_SYMBOL_GPL(blkg_conf_prep);
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800546
Tejun Heo3a8b31d2012-04-01 14:38:43 -0700547/**
548 * blkg_conf_finish - finish up per-blkg config update
549 * @ctx: blkg_conf_ctx intiailized by blkg_conf_prep()
550 *
551 * Finish up after per-blkg config update. This function must be paired
552 * with blkg_conf_prep().
553 */
Tejun Heo829fdb52012-04-01 14:38:43 -0700554void blkg_conf_finish(struct blkg_conf_ctx *ctx)
Tejun Heoda8b0662012-04-13 13:11:29 -0700555 __releases(ctx->disk->queue->queue_lock) __releases(rcu)
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800556{
Tejun Heoda8b0662012-04-13 13:11:29 -0700557 spin_unlock_irq(ctx->disk->queue->queue_lock);
Tejun Heo3a8b31d2012-04-01 14:38:43 -0700558 rcu_read_unlock();
559 put_disk(ctx->disk);
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800560}
Tejun Heo829fdb52012-04-01 14:38:43 -0700561EXPORT_SYMBOL_GPL(blkg_conf_finish);
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800562
Tejun Heo3c798392012-04-16 13:57:25 -0700563struct cftype blkcg_files[] = {
Vivek Goyal31e4c282009-12-03 12:59:42 -0500564 {
Divyesh Shah84c124d2010-04-09 08:31:19 +0200565 .name = "reset_stats",
Tejun Heo3c798392012-04-16 13:57:25 -0700566 .write_u64 = blkcg_reset_stats,
Vivek Goyal22084192009-12-03 12:59:49 -0500567 },
Tejun Heo4baf6e32012-04-01 12:09:55 -0700568 { } /* terminate */
Vivek Goyal31e4c282009-12-03 12:59:42 -0500569};
570
Tejun Heo9f13ef62012-03-05 13:15:21 -0800571/**
Tejun Heo3c798392012-04-16 13:57:25 -0700572 * blkcg_pre_destroy - cgroup pre_destroy callback
Tejun Heo9f13ef62012-03-05 13:15:21 -0800573 * @cgroup: cgroup of interest
574 *
575 * This function is called when @cgroup is about to go away and responsible
576 * for shooting down all blkgs associated with @cgroup. blkgs should be
577 * removed while holding both q and blkcg locks. As blkcg lock is nested
578 * inside q lock, this function performs reverse double lock dancing.
579 *
580 * This is the blkcg counterpart of ioc_release_fn().
581 */
Tejun Heo3c798392012-04-16 13:57:25 -0700582static int blkcg_pre_destroy(struct cgroup *cgroup)
Vivek Goyal31e4c282009-12-03 12:59:42 -0500583{
Tejun Heo3c798392012-04-16 13:57:25 -0700584 struct blkcg *blkcg = cgroup_to_blkcg(cgroup);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500585
Tejun Heo9f13ef62012-03-05 13:15:21 -0800586 spin_lock_irq(&blkcg->lock);
Tejun Heo7ee9c562012-03-05 13:15:11 -0800587
Tejun Heo9f13ef62012-03-05 13:15:21 -0800588 while (!hlist_empty(&blkcg->blkg_list)) {
Tejun Heo3c798392012-04-16 13:57:25 -0700589 struct blkcg_gq *blkg = hlist_entry(blkcg->blkg_list.first,
590 struct blkcg_gq, blkcg_node);
Tejun Heoc875f4d2012-03-05 13:15:22 -0800591 struct request_queue *q = blkg->q;
Vivek Goyalb1c35762009-12-03 12:59:47 -0500592
Tejun Heo9f13ef62012-03-05 13:15:21 -0800593 if (spin_trylock(q->queue_lock)) {
594 blkg_destroy(blkg);
595 spin_unlock(q->queue_lock);
596 } else {
597 spin_unlock_irq(&blkcg->lock);
Tejun Heo9f13ef62012-03-05 13:15:21 -0800598 cpu_relax();
Dan Carpentera5567932012-03-29 20:57:08 +0200599 spin_lock_irq(&blkcg->lock);
Jens Axboe0f3942a2010-05-03 14:28:55 +0200600 }
Tejun Heo9f13ef62012-03-05 13:15:21 -0800601 }
Jens Axboe0f3942a2010-05-03 14:28:55 +0200602
Tejun Heo9f13ef62012-03-05 13:15:21 -0800603 spin_unlock_irq(&blkcg->lock);
Tejun Heo7ee9c562012-03-05 13:15:11 -0800604 return 0;
605}
606
Tejun Heo3c798392012-04-16 13:57:25 -0700607static void blkcg_destroy(struct cgroup *cgroup)
Tejun Heo7ee9c562012-03-05 13:15:11 -0800608{
Tejun Heo3c798392012-04-16 13:57:25 -0700609 struct blkcg *blkcg = cgroup_to_blkcg(cgroup);
Tejun Heo7ee9c562012-03-05 13:15:11 -0800610
Tejun Heo3c798392012-04-16 13:57:25 -0700611 if (blkcg != &blkcg_root)
Ben Blum67523c42010-03-10 15:22:11 -0800612 kfree(blkcg);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500613}
614
Tejun Heo3c798392012-04-16 13:57:25 -0700615static struct cgroup_subsys_state *blkcg_create(struct cgroup *cgroup)
Vivek Goyal31e4c282009-12-03 12:59:42 -0500616{
Tejun Heo9a9e8a22012-03-19 15:10:56 -0700617 static atomic64_t id_seq = ATOMIC64_INIT(0);
Tejun Heo3c798392012-04-16 13:57:25 -0700618 struct blkcg *blkcg;
Li Zefan03415092010-05-07 08:57:00 +0200619 struct cgroup *parent = cgroup->parent;
Vivek Goyal31e4c282009-12-03 12:59:42 -0500620
Li Zefan03415092010-05-07 08:57:00 +0200621 if (!parent) {
Tejun Heo3c798392012-04-16 13:57:25 -0700622 blkcg = &blkcg_root;
Vivek Goyal31e4c282009-12-03 12:59:42 -0500623 goto done;
624 }
625
Vivek Goyal31e4c282009-12-03 12:59:42 -0500626 blkcg = kzalloc(sizeof(*blkcg), GFP_KERNEL);
627 if (!blkcg)
628 return ERR_PTR(-ENOMEM);
629
Tejun Heo3381cb82012-04-01 14:38:44 -0700630 blkcg->cfq_weight = CFQ_WEIGHT_DEFAULT;
Tejun Heo9a9e8a22012-03-19 15:10:56 -0700631 blkcg->id = atomic64_inc_return(&id_seq); /* root is 0, start from 1 */
Vivek Goyal31e4c282009-12-03 12:59:42 -0500632done:
633 spin_lock_init(&blkcg->lock);
Tejun Heoa6371202012-04-19 16:29:24 -0700634 INIT_RADIX_TREE(&blkcg->blkg_tree, GFP_ATOMIC);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500635 INIT_HLIST_HEAD(&blkcg->blkg_list);
636
637 return &blkcg->css;
638}
639
Tejun Heo5efd6112012-03-05 13:15:12 -0800640/**
641 * blkcg_init_queue - initialize blkcg part of request queue
642 * @q: request_queue to initialize
643 *
644 * Called from blk_alloc_queue_node(). Responsible for initializing blkcg
645 * part of new request_queue @q.
646 *
647 * RETURNS:
648 * 0 on success, -errno on failure.
649 */
650int blkcg_init_queue(struct request_queue *q)
651{
652 might_sleep();
653
Tejun Heo3c96cb32012-04-13 13:11:34 -0700654 return blk_throtl_init(q);
Tejun Heo5efd6112012-03-05 13:15:12 -0800655}
656
657/**
658 * blkcg_drain_queue - drain blkcg part of request_queue
659 * @q: request_queue to drain
660 *
661 * Called from blk_drain_queue(). Responsible for draining blkcg part.
662 */
663void blkcg_drain_queue(struct request_queue *q)
664{
665 lockdep_assert_held(q->queue_lock);
666
667 blk_throtl_drain(q);
668}
669
670/**
671 * blkcg_exit_queue - exit and release blkcg part of request_queue
672 * @q: request_queue being released
673 *
674 * Called from blk_release_queue(). Responsible for exiting blkcg part.
675 */
676void blkcg_exit_queue(struct request_queue *q)
677{
Tejun Heo6d18b002012-04-13 13:11:35 -0700678 spin_lock_irq(q->queue_lock);
Tejun Heo3c96cb32012-04-13 13:11:34 -0700679 blkg_destroy_all(q);
Tejun Heo6d18b002012-04-13 13:11:35 -0700680 spin_unlock_irq(q->queue_lock);
681
Tejun Heo5efd6112012-03-05 13:15:12 -0800682 blk_throtl_exit(q);
683}
684
Vivek Goyal31e4c282009-12-03 12:59:42 -0500685/*
686 * We cannot support shared io contexts, as we have no mean to support
687 * two tasks with the same ioc in two different groups without major rework
688 * of the main cic data structures. For now we allow a task to change
689 * its cgroup only if it's the only owner of its ioc.
690 */
Tejun Heo3c798392012-04-16 13:57:25 -0700691static int blkcg_can_attach(struct cgroup *cgrp, struct cgroup_taskset *tset)
Vivek Goyal31e4c282009-12-03 12:59:42 -0500692{
Tejun Heobb9d97b2011-12-12 18:12:21 -0800693 struct task_struct *task;
Vivek Goyal31e4c282009-12-03 12:59:42 -0500694 struct io_context *ioc;
695 int ret = 0;
696
697 /* task_lock() is needed to avoid races with exit_io_context() */
Tejun Heobb9d97b2011-12-12 18:12:21 -0800698 cgroup_taskset_for_each(task, cgrp, tset) {
699 task_lock(task);
700 ioc = task->io_context;
701 if (ioc && atomic_read(&ioc->nr_tasks) > 1)
702 ret = -EINVAL;
703 task_unlock(task);
704 if (ret)
705 break;
706 }
Vivek Goyal31e4c282009-12-03 12:59:42 -0500707 return ret;
708}
709
Tejun Heo676f7c82012-04-01 12:09:55 -0700710struct cgroup_subsys blkio_subsys = {
711 .name = "blkio",
Tejun Heo3c798392012-04-16 13:57:25 -0700712 .create = blkcg_create,
713 .can_attach = blkcg_can_attach,
714 .pre_destroy = blkcg_pre_destroy,
715 .destroy = blkcg_destroy,
Tejun Heo676f7c82012-04-01 12:09:55 -0700716 .subsys_id = blkio_subsys_id,
Tejun Heo3c798392012-04-16 13:57:25 -0700717 .base_cftypes = blkcg_files,
Tejun Heo676f7c82012-04-01 12:09:55 -0700718 .module = THIS_MODULE,
719};
720EXPORT_SYMBOL_GPL(blkio_subsys);
721
Tejun Heo8bd435b2012-04-13 13:11:28 -0700722/**
Tejun Heoa2b16932012-04-13 13:11:33 -0700723 * blkcg_activate_policy - activate a blkcg policy on a request_queue
724 * @q: request_queue of interest
725 * @pol: blkcg policy to activate
726 *
727 * Activate @pol on @q. Requires %GFP_KERNEL context. @q goes through
728 * bypass mode to populate its blkgs with policy_data for @pol.
729 *
730 * Activation happens with @q bypassed, so nobody would be accessing blkgs
731 * from IO path. Update of each blkg is protected by both queue and blkcg
732 * locks so that holding either lock and testing blkcg_policy_enabled() is
733 * always enough for dereferencing policy data.
734 *
735 * The caller is responsible for synchronizing [de]activations and policy
736 * [un]registerations. Returns 0 on success, -errno on failure.
737 */
738int blkcg_activate_policy(struct request_queue *q,
Tejun Heo3c798392012-04-16 13:57:25 -0700739 const struct blkcg_policy *pol)
Tejun Heoa2b16932012-04-13 13:11:33 -0700740{
741 LIST_HEAD(pds);
Tejun Heo3c798392012-04-16 13:57:25 -0700742 struct blkcg_gq *blkg;
Tejun Heoa2b16932012-04-13 13:11:33 -0700743 struct blkg_policy_data *pd, *n;
744 int cnt = 0, ret;
Tejun Heo15974992012-06-04 20:40:52 -0700745 bool preloaded;
Tejun Heoa2b16932012-04-13 13:11:33 -0700746
747 if (blkcg_policy_enabled(q, pol))
748 return 0;
749
Tejun Heo15974992012-06-04 20:40:52 -0700750 /* preallocations for root blkg */
751 blkg = blkg_alloc(&blkcg_root, q, GFP_KERNEL);
752 if (!blkg)
753 return -ENOMEM;
754
755 preloaded = !radix_tree_preload(GFP_KERNEL);
756
Tejun Heoa2b16932012-04-13 13:11:33 -0700757 blk_queue_bypass_start(q);
758
759 /* make sure the root blkg exists and count the existing blkgs */
760 spin_lock_irq(q->queue_lock);
761
762 rcu_read_lock();
Tejun Heo15974992012-06-04 20:40:52 -0700763 blkg = __blkg_lookup_create(&blkcg_root, q, blkg);
Tejun Heoa2b16932012-04-13 13:11:33 -0700764 rcu_read_unlock();
765
Tejun Heo15974992012-06-04 20:40:52 -0700766 if (preloaded)
767 radix_tree_preload_end();
768
Tejun Heoa2b16932012-04-13 13:11:33 -0700769 if (IS_ERR(blkg)) {
770 ret = PTR_ERR(blkg);
771 goto out_unlock;
772 }
773 q->root_blkg = blkg;
774
775 list_for_each_entry(blkg, &q->blkg_list, q_node)
776 cnt++;
777
778 spin_unlock_irq(q->queue_lock);
779
780 /* allocate policy_data for all existing blkgs */
781 while (cnt--) {
Tejun Heof95a04a2012-04-16 13:57:26 -0700782 pd = kzalloc_node(pol->pd_size, GFP_KERNEL, q->node);
Tejun Heoa2b16932012-04-13 13:11:33 -0700783 if (!pd) {
784 ret = -ENOMEM;
785 goto out_free;
786 }
787 list_add_tail(&pd->alloc_node, &pds);
788 }
789
790 /*
791 * Install the allocated pds. With @q bypassing, no new blkg
792 * should have been created while the queue lock was dropped.
793 */
794 spin_lock_irq(q->queue_lock);
795
796 list_for_each_entry(blkg, &q->blkg_list, q_node) {
797 if (WARN_ON(list_empty(&pds))) {
798 /* umm... this shouldn't happen, just abort */
799 ret = -ENOMEM;
800 goto out_unlock;
801 }
802 pd = list_first_entry(&pds, struct blkg_policy_data, alloc_node);
803 list_del_init(&pd->alloc_node);
804
805 /* grab blkcg lock too while installing @pd on @blkg */
806 spin_lock(&blkg->blkcg->lock);
807
808 blkg->pd[pol->plid] = pd;
809 pd->blkg = blkg;
Tejun Heof9fcc2d2012-04-16 13:57:27 -0700810 pol->pd_init_fn(blkg);
Tejun Heoa2b16932012-04-13 13:11:33 -0700811
812 spin_unlock(&blkg->blkcg->lock);
813 }
814
815 __set_bit(pol->plid, q->blkcg_pols);
816 ret = 0;
817out_unlock:
818 spin_unlock_irq(q->queue_lock);
819out_free:
820 blk_queue_bypass_end(q);
821 list_for_each_entry_safe(pd, n, &pds, alloc_node)
822 kfree(pd);
823 return ret;
824}
825EXPORT_SYMBOL_GPL(blkcg_activate_policy);
826
827/**
828 * blkcg_deactivate_policy - deactivate a blkcg policy on a request_queue
829 * @q: request_queue of interest
830 * @pol: blkcg policy to deactivate
831 *
832 * Deactivate @pol on @q. Follows the same synchronization rules as
833 * blkcg_activate_policy().
834 */
835void blkcg_deactivate_policy(struct request_queue *q,
Tejun Heo3c798392012-04-16 13:57:25 -0700836 const struct blkcg_policy *pol)
Tejun Heoa2b16932012-04-13 13:11:33 -0700837{
Tejun Heo3c798392012-04-16 13:57:25 -0700838 struct blkcg_gq *blkg;
Tejun Heoa2b16932012-04-13 13:11:33 -0700839
840 if (!blkcg_policy_enabled(q, pol))
841 return;
842
843 blk_queue_bypass_start(q);
844 spin_lock_irq(q->queue_lock);
845
846 __clear_bit(pol->plid, q->blkcg_pols);
847
Tejun Heo6d18b002012-04-13 13:11:35 -0700848 /* if no policy is left, no need for blkgs - shoot them down */
849 if (bitmap_empty(q->blkcg_pols, BLKCG_MAX_POLS))
850 blkg_destroy_all(q);
851
Tejun Heoa2b16932012-04-13 13:11:33 -0700852 list_for_each_entry(blkg, &q->blkg_list, q_node) {
853 /* grab blkcg lock too while removing @pd from @blkg */
854 spin_lock(&blkg->blkcg->lock);
855
Tejun Heof9fcc2d2012-04-16 13:57:27 -0700856 if (pol->pd_exit_fn)
857 pol->pd_exit_fn(blkg);
Tejun Heoa2b16932012-04-13 13:11:33 -0700858
859 kfree(blkg->pd[pol->plid]);
860 blkg->pd[pol->plid] = NULL;
861
862 spin_unlock(&blkg->blkcg->lock);
863 }
864
865 spin_unlock_irq(q->queue_lock);
866 blk_queue_bypass_end(q);
867}
868EXPORT_SYMBOL_GPL(blkcg_deactivate_policy);
869
870/**
Tejun Heo3c798392012-04-16 13:57:25 -0700871 * blkcg_policy_register - register a blkcg policy
872 * @pol: blkcg policy to register
Tejun Heo8bd435b2012-04-13 13:11:28 -0700873 *
Tejun Heo3c798392012-04-16 13:57:25 -0700874 * Register @pol with blkcg core. Might sleep and @pol may be modified on
875 * successful registration. Returns 0 on success and -errno on failure.
Tejun Heo8bd435b2012-04-13 13:11:28 -0700876 */
Tejun Heo3c798392012-04-16 13:57:25 -0700877int blkcg_policy_register(struct blkcg_policy *pol)
Vivek Goyal3e252062009-12-04 10:36:42 -0500878{
Tejun Heo8bd435b2012-04-13 13:11:28 -0700879 int i, ret;
Tejun Heoe8989fa2012-03-05 13:15:20 -0800880
Tejun Heof95a04a2012-04-16 13:57:26 -0700881 if (WARN_ON(pol->pd_size < sizeof(struct blkg_policy_data)))
882 return -EINVAL;
883
Tejun Heobc0d6502012-04-13 13:11:26 -0700884 mutex_lock(&blkcg_pol_mutex);
885
Tejun Heo8bd435b2012-04-13 13:11:28 -0700886 /* find an empty slot */
887 ret = -ENOSPC;
888 for (i = 0; i < BLKCG_MAX_POLS; i++)
Tejun Heo3c798392012-04-16 13:57:25 -0700889 if (!blkcg_policy[i])
Tejun Heo8bd435b2012-04-13 13:11:28 -0700890 break;
891 if (i >= BLKCG_MAX_POLS)
892 goto out_unlock;
Tejun Heo035d10b2012-03-05 13:15:04 -0800893
Tejun Heo8bd435b2012-04-13 13:11:28 -0700894 /* register and update blkgs */
Tejun Heo3c798392012-04-16 13:57:25 -0700895 pol->plid = i;
896 blkcg_policy[i] = pol;
Tejun Heo8bd435b2012-04-13 13:11:28 -0700897
Tejun Heo8bd435b2012-04-13 13:11:28 -0700898 /* everything is in place, add intf files for the new policy */
Tejun Heo3c798392012-04-16 13:57:25 -0700899 if (pol->cftypes)
900 WARN_ON(cgroup_add_cftypes(&blkio_subsys, pol->cftypes));
Tejun Heo8bd435b2012-04-13 13:11:28 -0700901 ret = 0;
902out_unlock:
Tejun Heobc0d6502012-04-13 13:11:26 -0700903 mutex_unlock(&blkcg_pol_mutex);
Tejun Heo8bd435b2012-04-13 13:11:28 -0700904 return ret;
Vivek Goyal3e252062009-12-04 10:36:42 -0500905}
Tejun Heo3c798392012-04-16 13:57:25 -0700906EXPORT_SYMBOL_GPL(blkcg_policy_register);
Vivek Goyal3e252062009-12-04 10:36:42 -0500907
Tejun Heo8bd435b2012-04-13 13:11:28 -0700908/**
Tejun Heo3c798392012-04-16 13:57:25 -0700909 * blkcg_policy_unregister - unregister a blkcg policy
910 * @pol: blkcg policy to unregister
Tejun Heo8bd435b2012-04-13 13:11:28 -0700911 *
Tejun Heo3c798392012-04-16 13:57:25 -0700912 * Undo blkcg_policy_register(@pol). Might sleep.
Tejun Heo8bd435b2012-04-13 13:11:28 -0700913 */
Tejun Heo3c798392012-04-16 13:57:25 -0700914void blkcg_policy_unregister(struct blkcg_policy *pol)
Vivek Goyal3e252062009-12-04 10:36:42 -0500915{
Tejun Heobc0d6502012-04-13 13:11:26 -0700916 mutex_lock(&blkcg_pol_mutex);
917
Tejun Heo3c798392012-04-16 13:57:25 -0700918 if (WARN_ON(blkcg_policy[pol->plid] != pol))
Tejun Heo8bd435b2012-04-13 13:11:28 -0700919 goto out_unlock;
920
921 /* kill the intf files first */
Tejun Heo3c798392012-04-16 13:57:25 -0700922 if (pol->cftypes)
923 cgroup_rm_cftypes(&blkio_subsys, pol->cftypes);
Tejun Heo44ea53d2012-04-01 14:38:43 -0700924
Tejun Heo8bd435b2012-04-13 13:11:28 -0700925 /* unregister and update blkgs */
Tejun Heo3c798392012-04-16 13:57:25 -0700926 blkcg_policy[pol->plid] = NULL;
Tejun Heo8bd435b2012-04-13 13:11:28 -0700927out_unlock:
Tejun Heobc0d6502012-04-13 13:11:26 -0700928 mutex_unlock(&blkcg_pol_mutex);
Vivek Goyal3e252062009-12-04 10:36:42 -0500929}
Tejun Heo3c798392012-04-16 13:57:25 -0700930EXPORT_SYMBOL_GPL(blkcg_policy_unregister);