blob: 10e1df9da46ee6ae7c0da74a567ae54726094fff [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 Heoe71357e2013-01-09 08:05:10 -080029struct blkcg blkcg_root = { .cfq_weight = 2 * CFQ_WEIGHT_DEFAULT,
30 .cfq_leaf_weight = 2 * CFQ_WEIGHT_DEFAULT, };
Tejun Heo3c798392012-04-16 13:57:25 -070031EXPORT_SYMBOL_GPL(blkcg_root);
Vivek Goyal9d6a9862009-12-04 10:36:41 -050032
Tejun Heo3c798392012-04-16 13:57:25 -070033static struct blkcg_policy *blkcg_policy[BLKCG_MAX_POLS];
Tejun Heo035d10b2012-03-05 13:15:04 -080034
Tejun Heoa2b16932012-04-13 13:11:33 -070035static bool blkcg_policy_enabled(struct request_queue *q,
Tejun Heo3c798392012-04-16 13:57:25 -070036 const struct blkcg_policy *pol)
Tejun Heoa2b16932012-04-13 13:11:33 -070037{
38 return pol && test_bit(pol->plid, q->blkcg_pols);
39}
40
Tejun Heo03814112012-03-05 13:15:14 -080041/**
42 * blkg_free - free a blkg
43 * @blkg: blkg to free
44 *
45 * Free @blkg which may be partially allocated.
46 */
Tejun Heo3c798392012-04-16 13:57:25 -070047static void blkg_free(struct blkcg_gq *blkg)
Tejun Heo03814112012-03-05 13:15:14 -080048{
Tejun Heoe8989fa2012-03-05 13:15:20 -080049 int i;
Tejun Heo549d3aa2012-03-05 13:15:16 -080050
51 if (!blkg)
52 return;
53
Tejun Heo8bd435b2012-04-13 13:11:28 -070054 for (i = 0; i < BLKCG_MAX_POLS; i++) {
Tejun Heo3c798392012-04-16 13:57:25 -070055 struct blkcg_policy *pol = blkcg_policy[i];
Tejun Heoe8989fa2012-03-05 13:15:20 -080056 struct blkg_policy_data *pd = blkg->pd[i];
57
Tejun Heo9ade5ea2012-04-01 14:38:44 -070058 if (!pd)
59 continue;
60
Tejun Heof9fcc2d2012-04-16 13:57:27 -070061 if (pol && pol->pd_exit_fn)
62 pol->pd_exit_fn(blkg);
Tejun Heo9ade5ea2012-04-01 14:38:44 -070063
Tejun Heo9ade5ea2012-04-01 14:38:44 -070064 kfree(pd);
Tejun Heo03814112012-03-05 13:15:14 -080065 }
Tejun Heoe8989fa2012-03-05 13:15:20 -080066
Tejun Heoa0516612012-06-26 15:05:44 -070067 blk_exit_rl(&blkg->rl);
Tejun Heo549d3aa2012-03-05 13:15:16 -080068 kfree(blkg);
Tejun Heo03814112012-03-05 13:15:14 -080069}
70
71/**
72 * blkg_alloc - allocate a blkg
73 * @blkcg: block cgroup the new blkg is associated with
74 * @q: request_queue the new blkg is associated with
Tejun Heo15974992012-06-04 20:40:52 -070075 * @gfp_mask: allocation mask to use
Tejun Heo03814112012-03-05 13:15:14 -080076 *
Tejun Heoe8989fa2012-03-05 13:15:20 -080077 * Allocate a new blkg assocating @blkcg and @q.
Tejun Heo03814112012-03-05 13:15:14 -080078 */
Tejun Heo15974992012-06-04 20:40:52 -070079static struct blkcg_gq *blkg_alloc(struct blkcg *blkcg, struct request_queue *q,
80 gfp_t gfp_mask)
Tejun Heo03814112012-03-05 13:15:14 -080081{
Tejun Heo3c798392012-04-16 13:57:25 -070082 struct blkcg_gq *blkg;
Tejun Heoe8989fa2012-03-05 13:15:20 -080083 int i;
Tejun Heo03814112012-03-05 13:15:14 -080084
85 /* alloc and init base part */
Tejun Heo15974992012-06-04 20:40:52 -070086 blkg = kzalloc_node(sizeof(*blkg), gfp_mask, q->node);
Tejun Heo03814112012-03-05 13:15:14 -080087 if (!blkg)
88 return NULL;
89
Tejun Heoc875f4d2012-03-05 13:15:22 -080090 blkg->q = q;
Tejun Heoe8989fa2012-03-05 13:15:20 -080091 INIT_LIST_HEAD(&blkg->q_node);
Tejun Heo03814112012-03-05 13:15:14 -080092 blkg->blkcg = blkcg;
Tejun Heo1adaf3d2012-03-05 13:15:15 -080093 blkg->refcnt = 1;
Tejun Heo03814112012-03-05 13:15:14 -080094
Tejun Heoa0516612012-06-26 15:05:44 -070095 /* root blkg uses @q->root_rl, init rl only for !root blkgs */
96 if (blkcg != &blkcg_root) {
97 if (blk_init_rl(&blkg->rl, q, gfp_mask))
98 goto err_free;
99 blkg->rl.blkg = blkg;
100 }
101
Tejun Heo8bd435b2012-04-13 13:11:28 -0700102 for (i = 0; i < BLKCG_MAX_POLS; i++) {
Tejun Heo3c798392012-04-16 13:57:25 -0700103 struct blkcg_policy *pol = blkcg_policy[i];
Tejun Heoe8989fa2012-03-05 13:15:20 -0800104 struct blkg_policy_data *pd;
Tejun Heo03814112012-03-05 13:15:14 -0800105
Tejun Heoa2b16932012-04-13 13:11:33 -0700106 if (!blkcg_policy_enabled(q, pol))
Tejun Heoe8989fa2012-03-05 13:15:20 -0800107 continue;
Tejun Heo549d3aa2012-03-05 13:15:16 -0800108
Tejun Heoe8989fa2012-03-05 13:15:20 -0800109 /* alloc per-policy data and attach it to blkg */
Tejun Heo15974992012-06-04 20:40:52 -0700110 pd = kzalloc_node(pol->pd_size, gfp_mask, q->node);
Tejun Heoa0516612012-06-26 15:05:44 -0700111 if (!pd)
112 goto err_free;
Tejun Heo549d3aa2012-03-05 13:15:16 -0800113
Tejun Heoe8989fa2012-03-05 13:15:20 -0800114 blkg->pd[i] = pd;
115 pd->blkg = blkg;
Tejun Heo03814112012-03-05 13:15:14 -0800116
Tejun Heo9b2ea862012-06-04 15:21:00 +0900117 /* invoke per-policy init */
Tejun Heo356d2e52013-01-09 08:05:10 -0800118 if (pol->pd_init_fn)
Tejun Heof9fcc2d2012-04-16 13:57:27 -0700119 pol->pd_init_fn(blkg);
Tejun Heoe8989fa2012-03-05 13:15:20 -0800120 }
121
Tejun Heo03814112012-03-05 13:15:14 -0800122 return blkg;
Tejun Heoa0516612012-06-26 15:05:44 -0700123
124err_free:
125 blkg_free(blkg);
126 return NULL;
Tejun Heo03814112012-03-05 13:15:14 -0800127}
128
Tejun Heo3c798392012-04-16 13:57:25 -0700129static struct blkcg_gq *__blkg_lookup(struct blkcg *blkcg,
Tejun Heo86cde6b2013-01-09 08:05:10 -0800130 struct request_queue *q, bool update_hint)
Tejun Heo80fd9972012-04-13 14:50:53 -0700131{
Tejun Heo3c798392012-04-16 13:57:25 -0700132 struct blkcg_gq *blkg;
Tejun Heo80fd9972012-04-13 14:50:53 -0700133
Tejun Heoa6371202012-04-19 16:29:24 -0700134 blkg = rcu_dereference(blkcg->blkg_hint);
135 if (blkg && blkg->q == q)
136 return blkg;
137
138 /*
Tejun Heo86cde6b2013-01-09 08:05:10 -0800139 * Hint didn't match. Look up from the radix tree. Note that the
140 * hint can only be updated under queue_lock as otherwise @blkg
141 * could have already been removed from blkg_tree. The caller is
142 * responsible for grabbing queue_lock if @update_hint.
Tejun Heoa6371202012-04-19 16:29:24 -0700143 */
144 blkg = radix_tree_lookup(&blkcg->blkg_tree, q->id);
Tejun Heo86cde6b2013-01-09 08:05:10 -0800145 if (blkg && blkg->q == q) {
146 if (update_hint) {
147 lockdep_assert_held(q->queue_lock);
148 rcu_assign_pointer(blkcg->blkg_hint, blkg);
149 }
Tejun Heoa6371202012-04-19 16:29:24 -0700150 return blkg;
Tejun Heo86cde6b2013-01-09 08:05:10 -0800151 }
Tejun Heoa6371202012-04-19 16:29:24 -0700152
Tejun Heo80fd9972012-04-13 14:50:53 -0700153 return NULL;
154}
155
156/**
157 * blkg_lookup - lookup blkg for the specified blkcg - q pair
158 * @blkcg: blkcg of interest
159 * @q: request_queue of interest
160 *
161 * Lookup blkg for the @blkcg - @q pair. This function should be called
162 * under RCU read lock and is guaranteed to return %NULL if @q is bypassing
163 * - see blk_queue_bypass_start() for details.
164 */
Tejun Heo3c798392012-04-16 13:57:25 -0700165struct blkcg_gq *blkg_lookup(struct blkcg *blkcg, struct request_queue *q)
Tejun Heo80fd9972012-04-13 14:50:53 -0700166{
167 WARN_ON_ONCE(!rcu_read_lock_held());
168
169 if (unlikely(blk_queue_bypass(q)))
170 return NULL;
Tejun Heo86cde6b2013-01-09 08:05:10 -0800171 return __blkg_lookup(blkcg, q, false);
Tejun Heo80fd9972012-04-13 14:50:53 -0700172}
173EXPORT_SYMBOL_GPL(blkg_lookup);
174
Tejun Heo15974992012-06-04 20:40:52 -0700175/*
176 * If @new_blkg is %NULL, this function tries to allocate a new one as
177 * necessary using %GFP_ATOMIC. @new_blkg is always consumed on return.
178 */
Tejun Heo86cde6b2013-01-09 08:05:10 -0800179static struct blkcg_gq *blkg_create(struct blkcg *blkcg,
180 struct request_queue *q,
181 struct blkcg_gq *new_blkg)
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400182{
Tejun Heo3c798392012-04-16 13:57:25 -0700183 struct blkcg_gq *blkg;
Tejun Heo496fb782012-04-19 16:29:23 -0700184 int ret;
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 Heo7ee9c562012-03-05 13:15:11 -0800189 /* blkg holds a reference to blkcg */
Tejun Heo15974992012-06-04 20:40:52 -0700190 if (!css_tryget(&blkcg->css)) {
Tejun Heo93e6d5d2013-01-09 08:05:10 -0800191 ret = -EINVAL;
192 goto err_free_blkg;
Tejun Heo15974992012-06-04 20:40:52 -0700193 }
Tejun Heocd1604f2012-03-05 13:15:06 -0800194
Tejun Heo496fb782012-04-19 16:29:23 -0700195 /* allocate */
Tejun Heo15974992012-06-04 20:40:52 -0700196 if (!new_blkg) {
197 new_blkg = blkg_alloc(blkcg, q, GFP_ATOMIC);
198 if (unlikely(!new_blkg)) {
Tejun Heo93e6d5d2013-01-09 08:05:10 -0800199 ret = -ENOMEM;
200 goto err_put_css;
Tejun Heo15974992012-06-04 20:40:52 -0700201 }
202 }
203 blkg = new_blkg;
Tejun Heocd1604f2012-03-05 13:15:06 -0800204
Tejun Heo3c547862013-01-09 08:05:10 -0800205 /* link parent and insert */
206 if (blkcg_parent(blkcg)) {
207 blkg->parent = __blkg_lookup(blkcg_parent(blkcg), q, false);
208 if (WARN_ON_ONCE(!blkg->parent)) {
209 blkg = ERR_PTR(-EINVAL);
210 goto err_put_css;
211 }
212 blkg_get(blkg->parent);
213 }
214
Tejun Heoa6371202012-04-19 16:29:24 -0700215 spin_lock(&blkcg->lock);
216 ret = radix_tree_insert(&blkcg->blkg_tree, q->id, blkg);
217 if (likely(!ret)) {
218 hlist_add_head_rcu(&blkg->blkcg_node, &blkcg->blkg_list);
219 list_add(&blkg->q_node, &q->blkg_list);
220 }
221 spin_unlock(&blkcg->lock);
222
Tejun Heoa6371202012-04-19 16:29:24 -0700223 if (!ret)
224 return blkg;
Tejun Heo15974992012-06-04 20:40:52 -0700225
Tejun Heo3c547862013-01-09 08:05:10 -0800226 /* @blkg failed fully initialized, use the usual release path */
227 blkg_put(blkg);
228 return ERR_PTR(ret);
229
Tejun Heo93e6d5d2013-01-09 08:05:10 -0800230err_put_css:
Tejun Heo496fb782012-04-19 16:29:23 -0700231 css_put(&blkcg->css);
Tejun Heo93e6d5d2013-01-09 08:05:10 -0800232err_free_blkg:
Tejun Heo15974992012-06-04 20:40:52 -0700233 blkg_free(new_blkg);
Tejun Heo93e6d5d2013-01-09 08:05:10 -0800234 return ERR_PTR(ret);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500235}
Tejun Heo3c96cb32012-04-13 13:11:34 -0700236
Tejun Heo86cde6b2013-01-09 08:05:10 -0800237/**
238 * blkg_lookup_create - lookup blkg, try to create one if not there
239 * @blkcg: blkcg of interest
240 * @q: request_queue of interest
241 *
242 * Lookup blkg for the @blkcg - @q pair. If it doesn't exist, try to
Tejun Heo3c547862013-01-09 08:05:10 -0800243 * create one. blkg creation is performed recursively from blkcg_root such
244 * that all non-root blkg's have access to the parent blkg. This function
245 * should be called under RCU read lock and @q->queue_lock.
Tejun Heo86cde6b2013-01-09 08:05:10 -0800246 *
247 * Returns pointer to the looked up or created blkg on success, ERR_PTR()
248 * value on error. If @q is dead, returns ERR_PTR(-EINVAL). If @q is not
249 * dead and bypassing, returns ERR_PTR(-EBUSY).
250 */
Tejun Heo3c798392012-04-16 13:57:25 -0700251struct blkcg_gq *blkg_lookup_create(struct blkcg *blkcg,
252 struct request_queue *q)
Tejun Heo3c96cb32012-04-13 13:11:34 -0700253{
Tejun Heo86cde6b2013-01-09 08:05:10 -0800254 struct blkcg_gq *blkg;
255
256 WARN_ON_ONCE(!rcu_read_lock_held());
257 lockdep_assert_held(q->queue_lock);
258
Tejun Heo3c96cb32012-04-13 13:11:34 -0700259 /*
260 * This could be the first entry point of blkcg implementation and
261 * we shouldn't allow anything to go through for a bypassing queue.
262 */
263 if (unlikely(blk_queue_bypass(q)))
Bart Van Assche3f3299d2012-11-28 13:42:38 +0100264 return ERR_PTR(blk_queue_dying(q) ? -EINVAL : -EBUSY);
Tejun Heo86cde6b2013-01-09 08:05:10 -0800265
266 blkg = __blkg_lookup(blkcg, q, true);
267 if (blkg)
268 return blkg;
269
Tejun Heo3c547862013-01-09 08:05:10 -0800270 /*
271 * Create blkgs walking down from blkcg_root to @blkcg, so that all
272 * non-root blkgs have access to their parents.
273 */
274 while (true) {
275 struct blkcg *pos = blkcg;
276 struct blkcg *parent = blkcg_parent(blkcg);
277
278 while (parent && !__blkg_lookup(parent, q, false)) {
279 pos = parent;
280 parent = blkcg_parent(parent);
281 }
282
283 blkg = blkg_create(pos, q, NULL);
284 if (pos == blkcg || IS_ERR(blkg))
285 return blkg;
286 }
Tejun Heo3c96cb32012-04-13 13:11:34 -0700287}
Tejun Heocd1604f2012-03-05 13:15:06 -0800288EXPORT_SYMBOL_GPL(blkg_lookup_create);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500289
Tejun Heo3c798392012-04-16 13:57:25 -0700290static void blkg_destroy(struct blkcg_gq *blkg)
Tejun Heo72e06c22012-03-05 13:15:00 -0800291{
Tejun Heo3c798392012-04-16 13:57:25 -0700292 struct blkcg *blkcg = blkg->blkcg;
Tejun Heo03aa2642012-03-05 13:15:19 -0800293
Tejun Heo27e1f9d2012-06-05 13:36:44 +0200294 lockdep_assert_held(blkg->q->queue_lock);
Tejun Heo9f13ef62012-03-05 13:15:21 -0800295 lockdep_assert_held(&blkcg->lock);
Tejun Heo03aa2642012-03-05 13:15:19 -0800296
297 /* Something wrong if we are trying to remove same group twice */
Tejun Heoe8989fa2012-03-05 13:15:20 -0800298 WARN_ON_ONCE(list_empty(&blkg->q_node));
Tejun Heo9f13ef62012-03-05 13:15:21 -0800299 WARN_ON_ONCE(hlist_unhashed(&blkg->blkcg_node));
Tejun Heoa6371202012-04-19 16:29:24 -0700300
301 radix_tree_delete(&blkcg->blkg_tree, blkg->q->id);
Tejun Heoe8989fa2012-03-05 13:15:20 -0800302 list_del_init(&blkg->q_node);
Tejun Heo9f13ef62012-03-05 13:15:21 -0800303 hlist_del_init_rcu(&blkg->blkcg_node);
Tejun Heo03aa2642012-03-05 13:15:19 -0800304
Tejun Heo03aa2642012-03-05 13:15:19 -0800305 /*
Tejun Heoa6371202012-04-19 16:29:24 -0700306 * Both setting lookup hint to and clearing it from @blkg are done
307 * under queue_lock. If it's not pointing to @blkg now, it never
308 * will. Hint assignment itself can race safely.
309 */
310 if (rcu_dereference_raw(blkcg->blkg_hint) == blkg)
311 rcu_assign_pointer(blkcg->blkg_hint, NULL);
312
313 /*
Tejun Heo03aa2642012-03-05 13:15:19 -0800314 * Put the reference taken at the time of creation so that when all
315 * queues are gone, group can be destroyed.
316 */
317 blkg_put(blkg);
318}
319
Tejun Heo9f13ef62012-03-05 13:15:21 -0800320/**
321 * blkg_destroy_all - destroy all blkgs associated with a request_queue
322 * @q: request_queue of interest
Tejun Heo9f13ef62012-03-05 13:15:21 -0800323 *
Tejun Heo3c96cb32012-04-13 13:11:34 -0700324 * Destroy all blkgs associated with @q.
Tejun Heo9f13ef62012-03-05 13:15:21 -0800325 */
Tejun Heo3c96cb32012-04-13 13:11:34 -0700326static void blkg_destroy_all(struct request_queue *q)
Tejun Heo03aa2642012-03-05 13:15:19 -0800327{
Tejun Heo3c798392012-04-16 13:57:25 -0700328 struct blkcg_gq *blkg, *n;
Tejun Heo72e06c22012-03-05 13:15:00 -0800329
Tejun Heo6d18b002012-04-13 13:11:35 -0700330 lockdep_assert_held(q->queue_lock);
Tejun Heo72e06c22012-03-05 13:15:00 -0800331
Tejun Heo9f13ef62012-03-05 13:15:21 -0800332 list_for_each_entry_safe(blkg, n, &q->blkg_list, q_node) {
Tejun Heo3c798392012-04-16 13:57:25 -0700333 struct blkcg *blkcg = blkg->blkcg;
Tejun Heo72e06c22012-03-05 13:15:00 -0800334
Tejun Heo9f13ef62012-03-05 13:15:21 -0800335 spin_lock(&blkcg->lock);
336 blkg_destroy(blkg);
337 spin_unlock(&blkcg->lock);
Tejun Heo72e06c22012-03-05 13:15:00 -0800338 }
Jun'ichi Nomura65635cb2012-10-17 17:45:36 +0900339
340 /*
341 * root blkg is destroyed. Just clear the pointer since
342 * root_rl does not take reference on root blkg.
343 */
344 q->root_blkg = NULL;
345 q->root_rl.blkg = NULL;
Tejun Heo72e06c22012-03-05 13:15:00 -0800346}
347
Tejun Heo1adaf3d2012-03-05 13:15:15 -0800348static void blkg_rcu_free(struct rcu_head *rcu_head)
349{
Tejun Heo3c798392012-04-16 13:57:25 -0700350 blkg_free(container_of(rcu_head, struct blkcg_gq, rcu_head));
Tejun Heo1adaf3d2012-03-05 13:15:15 -0800351}
352
Tejun Heo3c798392012-04-16 13:57:25 -0700353void __blkg_release(struct blkcg_gq *blkg)
Tejun Heo1adaf3d2012-03-05 13:15:15 -0800354{
Tejun Heo3c547862013-01-09 08:05:10 -0800355 /* release the blkcg and parent blkg refs this blkg has been holding */
Tejun Heo1adaf3d2012-03-05 13:15:15 -0800356 css_put(&blkg->blkcg->css);
Tejun Heo3c547862013-01-09 08:05:10 -0800357 if (blkg->parent)
358 blkg_put(blkg->parent);
Tejun Heo1adaf3d2012-03-05 13:15:15 -0800359
360 /*
361 * A group is freed in rcu manner. But having an rcu lock does not
362 * mean that one can access all the fields of blkg and assume these
363 * are valid. For example, don't try to follow throtl_data and
364 * request queue links.
365 *
366 * Having a reference to blkg under an rcu allows acess to only
367 * values local to groups like group stats and group rate limits
368 */
369 call_rcu(&blkg->rcu_head, blkg_rcu_free);
370}
371EXPORT_SYMBOL_GPL(__blkg_release);
372
Tejun Heoa0516612012-06-26 15:05:44 -0700373/*
374 * The next function used by blk_queue_for_each_rl(). It's a bit tricky
375 * because the root blkg uses @q->root_rl instead of its own rl.
376 */
377struct request_list *__blk_queue_next_rl(struct request_list *rl,
378 struct request_queue *q)
379{
380 struct list_head *ent;
381 struct blkcg_gq *blkg;
382
383 /*
384 * Determine the current blkg list_head. The first entry is
385 * root_rl which is off @q->blkg_list and mapped to the head.
386 */
387 if (rl == &q->root_rl) {
388 ent = &q->blkg_list;
Jun'ichi Nomura65c77fd2012-10-22 10:15:37 +0900389 /* There are no more block groups, hence no request lists */
390 if (list_empty(ent))
391 return NULL;
Tejun Heoa0516612012-06-26 15:05:44 -0700392 } else {
393 blkg = container_of(rl, struct blkcg_gq, rl);
394 ent = &blkg->q_node;
395 }
396
397 /* walk to the next list_head, skip root blkcg */
398 ent = ent->next;
399 if (ent == &q->root_blkg->q_node)
400 ent = ent->next;
401 if (ent == &q->blkg_list)
402 return NULL;
403
404 blkg = container_of(ent, struct blkcg_gq, q_node);
405 return &blkg->rl;
406}
407
Tejun Heo3c798392012-04-16 13:57:25 -0700408static int blkcg_reset_stats(struct cgroup *cgroup, struct cftype *cftype,
409 u64 val)
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700410{
Tejun Heo3c798392012-04-16 13:57:25 -0700411 struct blkcg *blkcg = cgroup_to_blkcg(cgroup);
412 struct blkcg_gq *blkg;
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700413 struct hlist_node *n;
Tejun Heobc0d6502012-04-13 13:11:26 -0700414 int i;
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700415
Tejun Heobc0d6502012-04-13 13:11:26 -0700416 mutex_lock(&blkcg_pol_mutex);
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700417 spin_lock_irq(&blkcg->lock);
Tejun Heo997a0262012-03-08 10:53:58 -0800418
419 /*
420 * Note that stat reset is racy - it doesn't synchronize against
421 * stat updates. This is a debug feature which shouldn't exist
422 * anyway. If you get hit by a race, retry.
423 */
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700424 hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) {
Tejun Heo8bd435b2012-04-13 13:11:28 -0700425 for (i = 0; i < BLKCG_MAX_POLS; i++) {
Tejun Heo3c798392012-04-16 13:57:25 -0700426 struct blkcg_policy *pol = blkcg_policy[i];
Tejun Heo549d3aa2012-03-05 13:15:16 -0800427
Tejun Heoa2b16932012-04-13 13:11:33 -0700428 if (blkcg_policy_enabled(blkg->q, pol) &&
Tejun Heof9fcc2d2012-04-16 13:57:27 -0700429 pol->pd_reset_stats_fn)
430 pol->pd_reset_stats_fn(blkg);
Tejun Heobc0d6502012-04-13 13:11:26 -0700431 }
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700432 }
Vivek Goyalf0bdc8c2011-05-19 15:38:30 -0400433
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700434 spin_unlock_irq(&blkcg->lock);
Tejun Heobc0d6502012-04-13 13:11:26 -0700435 mutex_unlock(&blkcg_pol_mutex);
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700436 return 0;
437}
438
Tejun Heo3c798392012-04-16 13:57:25 -0700439static const char *blkg_dev_name(struct blkcg_gq *blkg)
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700440{
Tejun Heod3d32e62012-04-01 14:38:42 -0700441 /* some drivers (floppy) instantiate a queue w/o disk registered */
442 if (blkg->q->backing_dev_info.dev)
443 return dev_name(blkg->q->backing_dev_info.dev);
444 return NULL;
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700445}
446
Tejun Heod3d32e62012-04-01 14:38:42 -0700447/**
448 * blkcg_print_blkgs - helper for printing per-blkg data
449 * @sf: seq_file to print to
450 * @blkcg: blkcg of interest
451 * @prfill: fill function to print out a blkg
452 * @pol: policy in question
453 * @data: data to be passed to @prfill
454 * @show_total: to print out sum of prfill return values or not
455 *
456 * This function invokes @prfill on each blkg of @blkcg if pd for the
457 * policy specified by @pol exists. @prfill is invoked with @sf, the
458 * policy data and @data. If @show_total is %true, the sum of the return
459 * values from @prfill is printed with "Total" label at the end.
460 *
461 * This is to be used to construct print functions for
462 * cftype->read_seq_string method.
463 */
Tejun Heo3c798392012-04-16 13:57:25 -0700464void blkcg_print_blkgs(struct seq_file *sf, struct blkcg *blkcg,
Tejun Heof95a04a2012-04-16 13:57:26 -0700465 u64 (*prfill)(struct seq_file *,
466 struct blkg_policy_data *, int),
Tejun Heo3c798392012-04-16 13:57:25 -0700467 const struct blkcg_policy *pol, int data,
Tejun Heoec399342012-04-13 13:11:27 -0700468 bool show_total)
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400469{
Tejun Heo3c798392012-04-16 13:57:25 -0700470 struct blkcg_gq *blkg;
Tejun Heod3d32e62012-04-01 14:38:42 -0700471 struct hlist_node *n;
472 u64 total = 0;
473
474 spin_lock_irq(&blkcg->lock);
475 hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node)
Tejun Heoa2b16932012-04-13 13:11:33 -0700476 if (blkcg_policy_enabled(blkg->q, pol))
Tejun Heof95a04a2012-04-16 13:57:26 -0700477 total += prfill(sf, blkg->pd[pol->plid], data);
Tejun Heod3d32e62012-04-01 14:38:42 -0700478 spin_unlock_irq(&blkcg->lock);
479
480 if (show_total)
481 seq_printf(sf, "Total %llu\n", (unsigned long long)total);
482}
Tejun Heo829fdb52012-04-01 14:38:43 -0700483EXPORT_SYMBOL_GPL(blkcg_print_blkgs);
Tejun Heod3d32e62012-04-01 14:38:42 -0700484
485/**
486 * __blkg_prfill_u64 - prfill helper for a single u64 value
487 * @sf: seq_file to print to
Tejun Heof95a04a2012-04-16 13:57:26 -0700488 * @pd: policy private data of interest
Tejun Heod3d32e62012-04-01 14:38:42 -0700489 * @v: value to print
490 *
Tejun Heof95a04a2012-04-16 13:57:26 -0700491 * Print @v to @sf for the device assocaited with @pd.
Tejun Heod3d32e62012-04-01 14:38:42 -0700492 */
Tejun Heof95a04a2012-04-16 13:57:26 -0700493u64 __blkg_prfill_u64(struct seq_file *sf, struct blkg_policy_data *pd, u64 v)
Tejun Heod3d32e62012-04-01 14:38:42 -0700494{
Tejun Heof95a04a2012-04-16 13:57:26 -0700495 const char *dname = blkg_dev_name(pd->blkg);
Tejun Heod3d32e62012-04-01 14:38:42 -0700496
497 if (!dname)
498 return 0;
499
500 seq_printf(sf, "%s %llu\n", dname, (unsigned long long)v);
501 return v;
502}
Tejun Heo829fdb52012-04-01 14:38:43 -0700503EXPORT_SYMBOL_GPL(__blkg_prfill_u64);
Tejun Heod3d32e62012-04-01 14:38:42 -0700504
505/**
506 * __blkg_prfill_rwstat - prfill helper for a blkg_rwstat
507 * @sf: seq_file to print to
Tejun Heof95a04a2012-04-16 13:57:26 -0700508 * @pd: policy private data of interest
Tejun Heod3d32e62012-04-01 14:38:42 -0700509 * @rwstat: rwstat to print
510 *
Tejun Heof95a04a2012-04-16 13:57:26 -0700511 * Print @rwstat to @sf for the device assocaited with @pd.
Tejun Heod3d32e62012-04-01 14:38:42 -0700512 */
Tejun Heof95a04a2012-04-16 13:57:26 -0700513u64 __blkg_prfill_rwstat(struct seq_file *sf, struct blkg_policy_data *pd,
Tejun Heo829fdb52012-04-01 14:38:43 -0700514 const struct blkg_rwstat *rwstat)
Tejun Heod3d32e62012-04-01 14:38:42 -0700515{
516 static const char *rwstr[] = {
517 [BLKG_RWSTAT_READ] = "Read",
518 [BLKG_RWSTAT_WRITE] = "Write",
519 [BLKG_RWSTAT_SYNC] = "Sync",
520 [BLKG_RWSTAT_ASYNC] = "Async",
521 };
Tejun Heof95a04a2012-04-16 13:57:26 -0700522 const char *dname = blkg_dev_name(pd->blkg);
Tejun Heod3d32e62012-04-01 14:38:42 -0700523 u64 v;
524 int i;
525
526 if (!dname)
527 return 0;
528
529 for (i = 0; i < BLKG_RWSTAT_NR; i++)
530 seq_printf(sf, "%s %s %llu\n", dname, rwstr[i],
531 (unsigned long long)rwstat->cnt[i]);
532
533 v = rwstat->cnt[BLKG_RWSTAT_READ] + rwstat->cnt[BLKG_RWSTAT_WRITE];
534 seq_printf(sf, "%s Total %llu\n", dname, (unsigned long long)v);
535 return v;
536}
537
Tejun Heo5bc4afb12012-04-01 14:38:45 -0700538/**
539 * blkg_prfill_stat - prfill callback for blkg_stat
540 * @sf: seq_file to print to
Tejun Heof95a04a2012-04-16 13:57:26 -0700541 * @pd: policy private data of interest
542 * @off: offset to the blkg_stat in @pd
Tejun Heo5bc4afb12012-04-01 14:38:45 -0700543 *
544 * prfill callback for printing a blkg_stat.
545 */
Tejun Heof95a04a2012-04-16 13:57:26 -0700546u64 blkg_prfill_stat(struct seq_file *sf, struct blkg_policy_data *pd, int off)
Tejun Heod3d32e62012-04-01 14:38:42 -0700547{
Tejun Heof95a04a2012-04-16 13:57:26 -0700548 return __blkg_prfill_u64(sf, pd, blkg_stat_read((void *)pd + off));
Tejun Heod3d32e62012-04-01 14:38:42 -0700549}
Tejun Heo5bc4afb12012-04-01 14:38:45 -0700550EXPORT_SYMBOL_GPL(blkg_prfill_stat);
Tejun Heod3d32e62012-04-01 14:38:42 -0700551
Tejun Heo5bc4afb12012-04-01 14:38:45 -0700552/**
553 * blkg_prfill_rwstat - prfill callback for blkg_rwstat
554 * @sf: seq_file to print to
Tejun Heof95a04a2012-04-16 13:57:26 -0700555 * @pd: policy private data of interest
556 * @off: offset to the blkg_rwstat in @pd
Tejun Heo5bc4afb12012-04-01 14:38:45 -0700557 *
558 * prfill callback for printing a blkg_rwstat.
559 */
Tejun Heof95a04a2012-04-16 13:57:26 -0700560u64 blkg_prfill_rwstat(struct seq_file *sf, struct blkg_policy_data *pd,
561 int off)
Tejun Heod3d32e62012-04-01 14:38:42 -0700562{
Tejun Heof95a04a2012-04-16 13:57:26 -0700563 struct blkg_rwstat rwstat = blkg_rwstat_read((void *)pd + off);
Tejun Heod3d32e62012-04-01 14:38:42 -0700564
Tejun Heof95a04a2012-04-16 13:57:26 -0700565 return __blkg_prfill_rwstat(sf, pd, &rwstat);
Tejun Heod3d32e62012-04-01 14:38:42 -0700566}
Tejun Heo5bc4afb12012-04-01 14:38:45 -0700567EXPORT_SYMBOL_GPL(blkg_prfill_rwstat);
Tejun Heod3d32e62012-04-01 14:38:42 -0700568
Tejun Heo3a8b31d2012-04-01 14:38:43 -0700569/**
570 * blkg_conf_prep - parse and prepare for per-blkg config update
571 * @blkcg: target block cgroup
Tejun Heoda8b0662012-04-13 13:11:29 -0700572 * @pol: target policy
Tejun Heo3a8b31d2012-04-01 14:38:43 -0700573 * @input: input string
574 * @ctx: blkg_conf_ctx to be filled
575 *
576 * Parse per-blkg config update from @input and initialize @ctx with the
577 * result. @ctx->blkg points to the blkg to be updated and @ctx->v the new
Tejun Heoda8b0662012-04-13 13:11:29 -0700578 * value. This function returns with RCU read lock and queue lock held and
579 * must be paired with blkg_conf_finish().
Tejun Heo3a8b31d2012-04-01 14:38:43 -0700580 */
Tejun Heo3c798392012-04-16 13:57:25 -0700581int blkg_conf_prep(struct blkcg *blkcg, const struct blkcg_policy *pol,
582 const char *input, struct blkg_conf_ctx *ctx)
Tejun Heoda8b0662012-04-13 13:11:29 -0700583 __acquires(rcu) __acquires(disk->queue->queue_lock)
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800584{
Tejun Heo3a8b31d2012-04-01 14:38:43 -0700585 struct gendisk *disk;
Tejun Heo3c798392012-04-16 13:57:25 -0700586 struct blkcg_gq *blkg;
Tejun Heo726fa692012-04-01 14:38:43 -0700587 unsigned int major, minor;
588 unsigned long long v;
589 int part, ret;
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800590
Tejun Heo726fa692012-04-01 14:38:43 -0700591 if (sscanf(input, "%u:%u %llu", &major, &minor, &v) != 3)
592 return -EINVAL;
Tejun Heo3a8b31d2012-04-01 14:38:43 -0700593
Tejun Heo726fa692012-04-01 14:38:43 -0700594 disk = get_gendisk(MKDEV(major, minor), &part);
Tejun Heo4bfd4822012-03-05 13:15:08 -0800595 if (!disk || part)
Tejun Heo726fa692012-04-01 14:38:43 -0700596 return -EINVAL;
Tejun Heoe56da7e2012-03-05 13:15:07 -0800597
598 rcu_read_lock();
Tejun Heo4bfd4822012-03-05 13:15:08 -0800599 spin_lock_irq(disk->queue->queue_lock);
Tejun Heoda8b0662012-04-13 13:11:29 -0700600
Tejun Heoa2b16932012-04-13 13:11:33 -0700601 if (blkcg_policy_enabled(disk->queue, pol))
Tejun Heo3c96cb32012-04-13 13:11:34 -0700602 blkg = blkg_lookup_create(blkcg, disk->queue);
Tejun Heoa2b16932012-04-13 13:11:33 -0700603 else
604 blkg = ERR_PTR(-EINVAL);
Tejun Heoe56da7e2012-03-05 13:15:07 -0800605
Tejun Heo4bfd4822012-03-05 13:15:08 -0800606 if (IS_ERR(blkg)) {
607 ret = PTR_ERR(blkg);
Tejun Heo3a8b31d2012-04-01 14:38:43 -0700608 rcu_read_unlock();
Tejun Heoda8b0662012-04-13 13:11:29 -0700609 spin_unlock_irq(disk->queue->queue_lock);
Tejun Heo3a8b31d2012-04-01 14:38:43 -0700610 put_disk(disk);
611 /*
612 * If queue was bypassing, we should retry. Do so after a
613 * short msleep(). It isn't strictly necessary but queue
614 * can be bypassing for some time and it's always nice to
615 * avoid busy looping.
616 */
617 if (ret == -EBUSY) {
618 msleep(10);
619 ret = restart_syscall();
Vivek Goyal7702e8f2010-09-15 17:06:36 -0400620 }
Tejun Heo726fa692012-04-01 14:38:43 -0700621 return ret;
Vivek Goyal062a6442010-09-15 17:06:33 -0400622 }
Tejun Heoe56da7e2012-03-05 13:15:07 -0800623
Tejun Heo3a8b31d2012-04-01 14:38:43 -0700624 ctx->disk = disk;
625 ctx->blkg = blkg;
Tejun Heo726fa692012-04-01 14:38:43 -0700626 ctx->v = v;
627 return 0;
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800628}
Tejun Heo829fdb52012-04-01 14:38:43 -0700629EXPORT_SYMBOL_GPL(blkg_conf_prep);
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800630
Tejun Heo3a8b31d2012-04-01 14:38:43 -0700631/**
632 * blkg_conf_finish - finish up per-blkg config update
633 * @ctx: blkg_conf_ctx intiailized by blkg_conf_prep()
634 *
635 * Finish up after per-blkg config update. This function must be paired
636 * with blkg_conf_prep().
637 */
Tejun Heo829fdb52012-04-01 14:38:43 -0700638void blkg_conf_finish(struct blkg_conf_ctx *ctx)
Tejun Heoda8b0662012-04-13 13:11:29 -0700639 __releases(ctx->disk->queue->queue_lock) __releases(rcu)
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800640{
Tejun Heoda8b0662012-04-13 13:11:29 -0700641 spin_unlock_irq(ctx->disk->queue->queue_lock);
Tejun Heo3a8b31d2012-04-01 14:38:43 -0700642 rcu_read_unlock();
643 put_disk(ctx->disk);
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800644}
Tejun Heo829fdb52012-04-01 14:38:43 -0700645EXPORT_SYMBOL_GPL(blkg_conf_finish);
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800646
Tejun Heo3c798392012-04-16 13:57:25 -0700647struct cftype blkcg_files[] = {
Vivek Goyal31e4c282009-12-03 12:59:42 -0500648 {
Divyesh Shah84c124d2010-04-09 08:31:19 +0200649 .name = "reset_stats",
Tejun Heo3c798392012-04-16 13:57:25 -0700650 .write_u64 = blkcg_reset_stats,
Vivek Goyal22084192009-12-03 12:59:49 -0500651 },
Tejun Heo4baf6e32012-04-01 12:09:55 -0700652 { } /* terminate */
Vivek Goyal31e4c282009-12-03 12:59:42 -0500653};
654
Tejun Heo9f13ef62012-03-05 13:15:21 -0800655/**
Tejun Heo92fb9742012-11-19 08:13:38 -0800656 * blkcg_css_offline - cgroup css_offline callback
Tejun Heo9f13ef62012-03-05 13:15:21 -0800657 * @cgroup: cgroup of interest
658 *
659 * This function is called when @cgroup is about to go away and responsible
660 * for shooting down all blkgs associated with @cgroup. blkgs should be
661 * removed while holding both q and blkcg locks. As blkcg lock is nested
662 * inside q lock, this function performs reverse double lock dancing.
663 *
664 * This is the blkcg counterpart of ioc_release_fn().
665 */
Tejun Heo92fb9742012-11-19 08:13:38 -0800666static void blkcg_css_offline(struct cgroup *cgroup)
Vivek Goyal31e4c282009-12-03 12:59:42 -0500667{
Tejun Heo3c798392012-04-16 13:57:25 -0700668 struct blkcg *blkcg = cgroup_to_blkcg(cgroup);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500669
Tejun Heo9f13ef62012-03-05 13:15:21 -0800670 spin_lock_irq(&blkcg->lock);
Tejun Heo7ee9c562012-03-05 13:15:11 -0800671
Tejun Heo9f13ef62012-03-05 13:15:21 -0800672 while (!hlist_empty(&blkcg->blkg_list)) {
Tejun Heo3c798392012-04-16 13:57:25 -0700673 struct blkcg_gq *blkg = hlist_entry(blkcg->blkg_list.first,
674 struct blkcg_gq, blkcg_node);
Tejun Heoc875f4d2012-03-05 13:15:22 -0800675 struct request_queue *q = blkg->q;
Vivek Goyalb1c35762009-12-03 12:59:47 -0500676
Tejun Heo9f13ef62012-03-05 13:15:21 -0800677 if (spin_trylock(q->queue_lock)) {
678 blkg_destroy(blkg);
679 spin_unlock(q->queue_lock);
680 } else {
681 spin_unlock_irq(&blkcg->lock);
Tejun Heo9f13ef62012-03-05 13:15:21 -0800682 cpu_relax();
Dan Carpentera5567932012-03-29 20:57:08 +0200683 spin_lock_irq(&blkcg->lock);
Jens Axboe0f3942a2010-05-03 14:28:55 +0200684 }
Tejun Heo9f13ef62012-03-05 13:15:21 -0800685 }
Jens Axboe0f3942a2010-05-03 14:28:55 +0200686
Tejun Heo9f13ef62012-03-05 13:15:21 -0800687 spin_unlock_irq(&blkcg->lock);
Tejun Heo7ee9c562012-03-05 13:15:11 -0800688}
689
Tejun Heo92fb9742012-11-19 08:13:38 -0800690static void blkcg_css_free(struct cgroup *cgroup)
Tejun Heo7ee9c562012-03-05 13:15:11 -0800691{
Tejun Heo3c798392012-04-16 13:57:25 -0700692 struct blkcg *blkcg = cgroup_to_blkcg(cgroup);
Tejun Heo7ee9c562012-03-05 13:15:11 -0800693
Tejun Heo3c798392012-04-16 13:57:25 -0700694 if (blkcg != &blkcg_root)
Ben Blum67523c42010-03-10 15:22:11 -0800695 kfree(blkcg);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500696}
697
Tejun Heo92fb9742012-11-19 08:13:38 -0800698static struct cgroup_subsys_state *blkcg_css_alloc(struct cgroup *cgroup)
Vivek Goyal31e4c282009-12-03 12:59:42 -0500699{
Tejun Heo9a9e8a22012-03-19 15:10:56 -0700700 static atomic64_t id_seq = ATOMIC64_INIT(0);
Tejun Heo3c798392012-04-16 13:57:25 -0700701 struct blkcg *blkcg;
Li Zefan03415092010-05-07 08:57:00 +0200702 struct cgroup *parent = cgroup->parent;
Vivek Goyal31e4c282009-12-03 12:59:42 -0500703
Li Zefan03415092010-05-07 08:57:00 +0200704 if (!parent) {
Tejun Heo3c798392012-04-16 13:57:25 -0700705 blkcg = &blkcg_root;
Vivek Goyal31e4c282009-12-03 12:59:42 -0500706 goto done;
707 }
708
Vivek Goyal31e4c282009-12-03 12:59:42 -0500709 blkcg = kzalloc(sizeof(*blkcg), GFP_KERNEL);
710 if (!blkcg)
711 return ERR_PTR(-ENOMEM);
712
Tejun Heo3381cb82012-04-01 14:38:44 -0700713 blkcg->cfq_weight = CFQ_WEIGHT_DEFAULT;
Tejun Heoe71357e2013-01-09 08:05:10 -0800714 blkcg->cfq_leaf_weight = CFQ_WEIGHT_DEFAULT;
Tejun Heo9a9e8a22012-03-19 15:10:56 -0700715 blkcg->id = atomic64_inc_return(&id_seq); /* root is 0, start from 1 */
Vivek Goyal31e4c282009-12-03 12:59:42 -0500716done:
717 spin_lock_init(&blkcg->lock);
Tejun Heoa6371202012-04-19 16:29:24 -0700718 INIT_RADIX_TREE(&blkcg->blkg_tree, GFP_ATOMIC);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500719 INIT_HLIST_HEAD(&blkcg->blkg_list);
720
721 return &blkcg->css;
722}
723
Tejun Heo5efd6112012-03-05 13:15:12 -0800724/**
725 * blkcg_init_queue - initialize blkcg part of request queue
726 * @q: request_queue to initialize
727 *
728 * Called from blk_alloc_queue_node(). Responsible for initializing blkcg
729 * part of new request_queue @q.
730 *
731 * RETURNS:
732 * 0 on success, -errno on failure.
733 */
734int blkcg_init_queue(struct request_queue *q)
735{
736 might_sleep();
737
Tejun Heo3c96cb32012-04-13 13:11:34 -0700738 return blk_throtl_init(q);
Tejun Heo5efd6112012-03-05 13:15:12 -0800739}
740
741/**
742 * blkcg_drain_queue - drain blkcg part of request_queue
743 * @q: request_queue to drain
744 *
745 * Called from blk_drain_queue(). Responsible for draining blkcg part.
746 */
747void blkcg_drain_queue(struct request_queue *q)
748{
749 lockdep_assert_held(q->queue_lock);
750
751 blk_throtl_drain(q);
752}
753
754/**
755 * blkcg_exit_queue - exit and release blkcg part of request_queue
756 * @q: request_queue being released
757 *
758 * Called from blk_release_queue(). Responsible for exiting blkcg part.
759 */
760void blkcg_exit_queue(struct request_queue *q)
761{
Tejun Heo6d18b002012-04-13 13:11:35 -0700762 spin_lock_irq(q->queue_lock);
Tejun Heo3c96cb32012-04-13 13:11:34 -0700763 blkg_destroy_all(q);
Tejun Heo6d18b002012-04-13 13:11:35 -0700764 spin_unlock_irq(q->queue_lock);
765
Tejun Heo5efd6112012-03-05 13:15:12 -0800766 blk_throtl_exit(q);
767}
768
Vivek Goyal31e4c282009-12-03 12:59:42 -0500769/*
770 * We cannot support shared io contexts, as we have no mean to support
771 * two tasks with the same ioc in two different groups without major rework
772 * of the main cic data structures. For now we allow a task to change
773 * its cgroup only if it's the only owner of its ioc.
774 */
Tejun Heo3c798392012-04-16 13:57:25 -0700775static int blkcg_can_attach(struct cgroup *cgrp, struct cgroup_taskset *tset)
Vivek Goyal31e4c282009-12-03 12:59:42 -0500776{
Tejun Heobb9d97b2011-12-12 18:12:21 -0800777 struct task_struct *task;
Vivek Goyal31e4c282009-12-03 12:59:42 -0500778 struct io_context *ioc;
779 int ret = 0;
780
781 /* task_lock() is needed to avoid races with exit_io_context() */
Tejun Heobb9d97b2011-12-12 18:12:21 -0800782 cgroup_taskset_for_each(task, cgrp, tset) {
783 task_lock(task);
784 ioc = task->io_context;
785 if (ioc && atomic_read(&ioc->nr_tasks) > 1)
786 ret = -EINVAL;
787 task_unlock(task);
788 if (ret)
789 break;
790 }
Vivek Goyal31e4c282009-12-03 12:59:42 -0500791 return ret;
792}
793
Tejun Heo676f7c82012-04-01 12:09:55 -0700794struct cgroup_subsys blkio_subsys = {
795 .name = "blkio",
Tejun Heo92fb9742012-11-19 08:13:38 -0800796 .css_alloc = blkcg_css_alloc,
797 .css_offline = blkcg_css_offline,
798 .css_free = blkcg_css_free,
Tejun Heo3c798392012-04-16 13:57:25 -0700799 .can_attach = blkcg_can_attach,
Tejun Heo676f7c82012-04-01 12:09:55 -0700800 .subsys_id = blkio_subsys_id,
Tejun Heo3c798392012-04-16 13:57:25 -0700801 .base_cftypes = blkcg_files,
Tejun Heo676f7c82012-04-01 12:09:55 -0700802 .module = THIS_MODULE,
Tejun Heo8c7f6ed2012-09-13 12:20:58 -0700803
804 /*
805 * blkio subsystem is utterly broken in terms of hierarchy support.
806 * It treats all cgroups equally regardless of where they're
807 * located in the hierarchy - all cgroups are treated as if they're
808 * right below the root. Fix it and remove the following.
809 */
810 .broken_hierarchy = true,
Tejun Heo676f7c82012-04-01 12:09:55 -0700811};
812EXPORT_SYMBOL_GPL(blkio_subsys);
813
Tejun Heo8bd435b2012-04-13 13:11:28 -0700814/**
Tejun Heoa2b16932012-04-13 13:11:33 -0700815 * blkcg_activate_policy - activate a blkcg policy on a request_queue
816 * @q: request_queue of interest
817 * @pol: blkcg policy to activate
818 *
819 * Activate @pol on @q. Requires %GFP_KERNEL context. @q goes through
820 * bypass mode to populate its blkgs with policy_data for @pol.
821 *
822 * Activation happens with @q bypassed, so nobody would be accessing blkgs
823 * from IO path. Update of each blkg is protected by both queue and blkcg
824 * locks so that holding either lock and testing blkcg_policy_enabled() is
825 * always enough for dereferencing policy data.
826 *
827 * The caller is responsible for synchronizing [de]activations and policy
828 * [un]registerations. Returns 0 on success, -errno on failure.
829 */
830int blkcg_activate_policy(struct request_queue *q,
Tejun Heo3c798392012-04-16 13:57:25 -0700831 const struct blkcg_policy *pol)
Tejun Heoa2b16932012-04-13 13:11:33 -0700832{
833 LIST_HEAD(pds);
Tejun Heo86cde6b2013-01-09 08:05:10 -0800834 struct blkcg_gq *blkg, *new_blkg;
Tejun Heoa2b16932012-04-13 13:11:33 -0700835 struct blkg_policy_data *pd, *n;
836 int cnt = 0, ret;
Tejun Heo15974992012-06-04 20:40:52 -0700837 bool preloaded;
Tejun Heoa2b16932012-04-13 13:11:33 -0700838
839 if (blkcg_policy_enabled(q, pol))
840 return 0;
841
Tejun Heo15974992012-06-04 20:40:52 -0700842 /* preallocations for root blkg */
Tejun Heo86cde6b2013-01-09 08:05:10 -0800843 new_blkg = blkg_alloc(&blkcg_root, q, GFP_KERNEL);
844 if (!new_blkg)
Tejun Heo15974992012-06-04 20:40:52 -0700845 return -ENOMEM;
846
847 preloaded = !radix_tree_preload(GFP_KERNEL);
848
Tejun Heoa2b16932012-04-13 13:11:33 -0700849 blk_queue_bypass_start(q);
850
Tejun Heo86cde6b2013-01-09 08:05:10 -0800851 /*
852 * Make sure the root blkg exists and count the existing blkgs. As
853 * @q is bypassing at this point, blkg_lookup_create() can't be
854 * used. Open code it.
855 */
Tejun Heoa2b16932012-04-13 13:11:33 -0700856 spin_lock_irq(q->queue_lock);
857
858 rcu_read_lock();
Tejun Heo86cde6b2013-01-09 08:05:10 -0800859 blkg = __blkg_lookup(&blkcg_root, q, false);
860 if (blkg)
861 blkg_free(new_blkg);
862 else
863 blkg = blkg_create(&blkcg_root, q, new_blkg);
Tejun Heoa2b16932012-04-13 13:11:33 -0700864 rcu_read_unlock();
865
Tejun Heo15974992012-06-04 20:40:52 -0700866 if (preloaded)
867 radix_tree_preload_end();
868
Tejun Heoa2b16932012-04-13 13:11:33 -0700869 if (IS_ERR(blkg)) {
870 ret = PTR_ERR(blkg);
871 goto out_unlock;
872 }
873 q->root_blkg = blkg;
Tejun Heoa0516612012-06-26 15:05:44 -0700874 q->root_rl.blkg = blkg;
Tejun Heoa2b16932012-04-13 13:11:33 -0700875
876 list_for_each_entry(blkg, &q->blkg_list, q_node)
877 cnt++;
878
879 spin_unlock_irq(q->queue_lock);
880
881 /* allocate policy_data for all existing blkgs */
882 while (cnt--) {
Tejun Heof95a04a2012-04-16 13:57:26 -0700883 pd = kzalloc_node(pol->pd_size, GFP_KERNEL, q->node);
Tejun Heoa2b16932012-04-13 13:11:33 -0700884 if (!pd) {
885 ret = -ENOMEM;
886 goto out_free;
887 }
888 list_add_tail(&pd->alloc_node, &pds);
889 }
890
891 /*
892 * Install the allocated pds. With @q bypassing, no new blkg
893 * should have been created while the queue lock was dropped.
894 */
895 spin_lock_irq(q->queue_lock);
896
897 list_for_each_entry(blkg, &q->blkg_list, q_node) {
898 if (WARN_ON(list_empty(&pds))) {
899 /* umm... this shouldn't happen, just abort */
900 ret = -ENOMEM;
901 goto out_unlock;
902 }
903 pd = list_first_entry(&pds, struct blkg_policy_data, alloc_node);
904 list_del_init(&pd->alloc_node);
905
906 /* grab blkcg lock too while installing @pd on @blkg */
907 spin_lock(&blkg->blkcg->lock);
908
909 blkg->pd[pol->plid] = pd;
910 pd->blkg = blkg;
Tejun Heof9fcc2d2012-04-16 13:57:27 -0700911 pol->pd_init_fn(blkg);
Tejun Heoa2b16932012-04-13 13:11:33 -0700912
913 spin_unlock(&blkg->blkcg->lock);
914 }
915
916 __set_bit(pol->plid, q->blkcg_pols);
917 ret = 0;
918out_unlock:
919 spin_unlock_irq(q->queue_lock);
920out_free:
921 blk_queue_bypass_end(q);
922 list_for_each_entry_safe(pd, n, &pds, alloc_node)
923 kfree(pd);
924 return ret;
925}
926EXPORT_SYMBOL_GPL(blkcg_activate_policy);
927
928/**
929 * blkcg_deactivate_policy - deactivate a blkcg policy on a request_queue
930 * @q: request_queue of interest
931 * @pol: blkcg policy to deactivate
932 *
933 * Deactivate @pol on @q. Follows the same synchronization rules as
934 * blkcg_activate_policy().
935 */
936void blkcg_deactivate_policy(struct request_queue *q,
Tejun Heo3c798392012-04-16 13:57:25 -0700937 const struct blkcg_policy *pol)
Tejun Heoa2b16932012-04-13 13:11:33 -0700938{
Tejun Heo3c798392012-04-16 13:57:25 -0700939 struct blkcg_gq *blkg;
Tejun Heoa2b16932012-04-13 13:11:33 -0700940
941 if (!blkcg_policy_enabled(q, pol))
942 return;
943
944 blk_queue_bypass_start(q);
945 spin_lock_irq(q->queue_lock);
946
947 __clear_bit(pol->plid, q->blkcg_pols);
948
Tejun Heo6d18b002012-04-13 13:11:35 -0700949 /* if no policy is left, no need for blkgs - shoot them down */
950 if (bitmap_empty(q->blkcg_pols, BLKCG_MAX_POLS))
951 blkg_destroy_all(q);
952
Tejun Heoa2b16932012-04-13 13:11:33 -0700953 list_for_each_entry(blkg, &q->blkg_list, q_node) {
954 /* grab blkcg lock too while removing @pd from @blkg */
955 spin_lock(&blkg->blkcg->lock);
956
Tejun Heof9fcc2d2012-04-16 13:57:27 -0700957 if (pol->pd_exit_fn)
958 pol->pd_exit_fn(blkg);
Tejun Heoa2b16932012-04-13 13:11:33 -0700959
960 kfree(blkg->pd[pol->plid]);
961 blkg->pd[pol->plid] = NULL;
962
963 spin_unlock(&blkg->blkcg->lock);
964 }
965
966 spin_unlock_irq(q->queue_lock);
967 blk_queue_bypass_end(q);
968}
969EXPORT_SYMBOL_GPL(blkcg_deactivate_policy);
970
971/**
Tejun Heo3c798392012-04-16 13:57:25 -0700972 * blkcg_policy_register - register a blkcg policy
973 * @pol: blkcg policy to register
Tejun Heo8bd435b2012-04-13 13:11:28 -0700974 *
Tejun Heo3c798392012-04-16 13:57:25 -0700975 * Register @pol with blkcg core. Might sleep and @pol may be modified on
976 * successful registration. Returns 0 on success and -errno on failure.
Tejun Heo8bd435b2012-04-13 13:11:28 -0700977 */
Tejun Heo3c798392012-04-16 13:57:25 -0700978int blkcg_policy_register(struct blkcg_policy *pol)
Vivek Goyal3e252062009-12-04 10:36:42 -0500979{
Tejun Heo8bd435b2012-04-13 13:11:28 -0700980 int i, ret;
Tejun Heoe8989fa2012-03-05 13:15:20 -0800981
Tejun Heof95a04a2012-04-16 13:57:26 -0700982 if (WARN_ON(pol->pd_size < sizeof(struct blkg_policy_data)))
983 return -EINVAL;
984
Tejun Heobc0d6502012-04-13 13:11:26 -0700985 mutex_lock(&blkcg_pol_mutex);
986
Tejun Heo8bd435b2012-04-13 13:11:28 -0700987 /* find an empty slot */
988 ret = -ENOSPC;
989 for (i = 0; i < BLKCG_MAX_POLS; i++)
Tejun Heo3c798392012-04-16 13:57:25 -0700990 if (!blkcg_policy[i])
Tejun Heo8bd435b2012-04-13 13:11:28 -0700991 break;
992 if (i >= BLKCG_MAX_POLS)
993 goto out_unlock;
Tejun Heo035d10b2012-03-05 13:15:04 -0800994
Tejun Heo8bd435b2012-04-13 13:11:28 -0700995 /* register and update blkgs */
Tejun Heo3c798392012-04-16 13:57:25 -0700996 pol->plid = i;
997 blkcg_policy[i] = pol;
Tejun Heo8bd435b2012-04-13 13:11:28 -0700998
Tejun Heo8bd435b2012-04-13 13:11:28 -0700999 /* everything is in place, add intf files for the new policy */
Tejun Heo3c798392012-04-16 13:57:25 -07001000 if (pol->cftypes)
1001 WARN_ON(cgroup_add_cftypes(&blkio_subsys, pol->cftypes));
Tejun Heo8bd435b2012-04-13 13:11:28 -07001002 ret = 0;
1003out_unlock:
Tejun Heobc0d6502012-04-13 13:11:26 -07001004 mutex_unlock(&blkcg_pol_mutex);
Tejun Heo8bd435b2012-04-13 13:11:28 -07001005 return ret;
Vivek Goyal3e252062009-12-04 10:36:42 -05001006}
Tejun Heo3c798392012-04-16 13:57:25 -07001007EXPORT_SYMBOL_GPL(blkcg_policy_register);
Vivek Goyal3e252062009-12-04 10:36:42 -05001008
Tejun Heo8bd435b2012-04-13 13:11:28 -07001009/**
Tejun Heo3c798392012-04-16 13:57:25 -07001010 * blkcg_policy_unregister - unregister a blkcg policy
1011 * @pol: blkcg policy to unregister
Tejun Heo8bd435b2012-04-13 13:11:28 -07001012 *
Tejun Heo3c798392012-04-16 13:57:25 -07001013 * Undo blkcg_policy_register(@pol). Might sleep.
Tejun Heo8bd435b2012-04-13 13:11:28 -07001014 */
Tejun Heo3c798392012-04-16 13:57:25 -07001015void blkcg_policy_unregister(struct blkcg_policy *pol)
Vivek Goyal3e252062009-12-04 10:36:42 -05001016{
Tejun Heobc0d6502012-04-13 13:11:26 -07001017 mutex_lock(&blkcg_pol_mutex);
1018
Tejun Heo3c798392012-04-16 13:57:25 -07001019 if (WARN_ON(blkcg_policy[pol->plid] != pol))
Tejun Heo8bd435b2012-04-13 13:11:28 -07001020 goto out_unlock;
1021
1022 /* kill the intf files first */
Tejun Heo3c798392012-04-16 13:57:25 -07001023 if (pol->cftypes)
1024 cgroup_rm_cftypes(&blkio_subsys, pol->cftypes);
Tejun Heo44ea53d2012-04-01 14:38:43 -07001025
Tejun Heo8bd435b2012-04-13 13:11:28 -07001026 /* unregister and update blkgs */
Tejun Heo3c798392012-04-16 13:57:25 -07001027 blkcg_policy[pol->plid] = NULL;
Tejun Heo8bd435b2012-04-13 13:11:28 -07001028out_unlock:
Tejun Heobc0d6502012-04-13 13:11:26 -07001029 mutex_unlock(&blkcg_pol_mutex);
Vivek Goyal3e252062009-12-04 10:36:42 -05001030}
Tejun Heo3c798392012-04-16 13:57:25 -07001031EXPORT_SYMBOL_GPL(blkcg_policy_unregister);