blob: cfdda44f4a0b6cbcef1b85508e241f36cec664ab [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
Vivek Goyal3e252062009-12-04 10:36:42 -050027static DEFINE_SPINLOCK(blkio_list_lock);
28static LIST_HEAD(blkio_list);
Vivek Goyalb1c35762009-12-03 12:59:47 -050029
Tejun Heo923adde2012-03-05 13:15:13 -080030static DEFINE_MUTEX(all_q_mutex);
31static LIST_HEAD(all_q_list);
32
Vivek Goyal1cd9e032012-03-08 10:53:56 -080033/* List of groups pending per cpu stats allocation */
34static DEFINE_SPINLOCK(alloc_list_lock);
35static LIST_HEAD(alloc_list);
36
37static void blkio_stat_alloc_fn(struct work_struct *);
38static DECLARE_DELAYED_WORK(blkio_stat_alloc_work, blkio_stat_alloc_fn);
39
Vivek Goyal31e4c282009-12-03 12:59:42 -050040struct blkio_cgroup blkio_root_cgroup = { .weight = 2*BLKIO_WEIGHT_DEFAULT };
Vivek Goyal9d6a9862009-12-04 10:36:41 -050041EXPORT_SYMBOL_GPL(blkio_root_cgroup);
42
Tejun Heo035d10b2012-03-05 13:15:04 -080043static struct blkio_policy_type *blkio_policy[BLKIO_NR_POLICIES];
44
Vivek Goyal31e4c282009-12-03 12:59:42 -050045struct blkio_cgroup *cgroup_to_blkio_cgroup(struct cgroup *cgroup)
46{
47 return container_of(cgroup_subsys_state(cgroup, blkio_subsys_id),
48 struct blkio_cgroup, css);
49}
Vivek Goyal9d6a9862009-12-04 10:36:41 -050050EXPORT_SYMBOL_GPL(cgroup_to_blkio_cgroup);
Vivek Goyal31e4c282009-12-03 12:59:42 -050051
Tejun Heo4f85cb92012-03-05 13:15:28 -080052static struct blkio_cgroup *task_blkio_cgroup(struct task_struct *tsk)
Vivek Goyal70087dc2011-05-16 15:24:08 +020053{
54 return container_of(task_subsys_state(tsk, blkio_subsys_id),
55 struct blkio_cgroup, css);
56}
Tejun Heo4f85cb92012-03-05 13:15:28 -080057
58struct blkio_cgroup *bio_blkio_cgroup(struct bio *bio)
59{
60 if (bio && bio->bi_css)
61 return container_of(bio->bi_css, struct blkio_cgroup, css);
62 return task_blkio_cgroup(current);
63}
64EXPORT_SYMBOL_GPL(bio_blkio_cgroup);
Vivek Goyal70087dc2011-05-16 15:24:08 +020065
Vivek Goyal1cd9e032012-03-08 10:53:56 -080066/*
67 * Worker for allocating per cpu stat for blk groups. This is scheduled on
68 * the system_nrt_wq once there are some groups on the alloc_list waiting
69 * for allocation.
70 */
71static void blkio_stat_alloc_fn(struct work_struct *work)
72{
73 static void *pcpu_stats[BLKIO_NR_POLICIES];
74 struct delayed_work *dwork = to_delayed_work(work);
75 struct blkio_group *blkg;
76 int i;
77 bool empty = false;
78
79alloc_stats:
80 for (i = 0; i < BLKIO_NR_POLICIES; i++) {
81 if (pcpu_stats[i] != NULL)
82 continue;
83
84 pcpu_stats[i] = alloc_percpu(struct blkio_group_stats_cpu);
85
86 /* Allocation failed. Try again after some time. */
87 if (pcpu_stats[i] == NULL) {
88 queue_delayed_work(system_nrt_wq, dwork,
89 msecs_to_jiffies(10));
90 return;
91 }
92 }
93
94 spin_lock_irq(&blkio_list_lock);
95 spin_lock(&alloc_list_lock);
96
97 /* cgroup got deleted or queue exited. */
98 if (!list_empty(&alloc_list)) {
99 blkg = list_first_entry(&alloc_list, struct blkio_group,
100 alloc_node);
101 for (i = 0; i < BLKIO_NR_POLICIES; i++) {
102 struct blkg_policy_data *pd = blkg->pd[i];
103
104 if (blkio_policy[i] && pd && !pd->stats_cpu)
105 swap(pd->stats_cpu, pcpu_stats[i]);
106 }
107
108 list_del_init(&blkg->alloc_node);
109 }
110
111 empty = list_empty(&alloc_list);
112
113 spin_unlock(&alloc_list_lock);
114 spin_unlock_irq(&blkio_list_lock);
115
116 if (!empty)
117 goto alloc_stats;
118}
119
Tejun Heo03814112012-03-05 13:15:14 -0800120/**
121 * blkg_free - free a blkg
122 * @blkg: blkg to free
123 *
124 * Free @blkg which may be partially allocated.
125 */
126static void blkg_free(struct blkio_group *blkg)
127{
Tejun Heoe8989fa2012-03-05 13:15:20 -0800128 int i;
Tejun Heo549d3aa2012-03-05 13:15:16 -0800129
130 if (!blkg)
131 return;
132
Tejun Heoe8989fa2012-03-05 13:15:20 -0800133 for (i = 0; i < BLKIO_NR_POLICIES; i++) {
Tejun Heo9ade5ea2012-04-01 14:38:44 -0700134 struct blkio_policy_type *pol = blkio_policy[i];
Tejun Heoe8989fa2012-03-05 13:15:20 -0800135 struct blkg_policy_data *pd = blkg->pd[i];
136
Tejun Heo9ade5ea2012-04-01 14:38:44 -0700137 if (!pd)
138 continue;
139
140 if (pol && pol->ops.blkio_exit_group_fn)
141 pol->ops.blkio_exit_group_fn(blkg);
142
143 free_percpu(pd->stats_cpu);
144 kfree(pd);
Tejun Heo03814112012-03-05 13:15:14 -0800145 }
Tejun Heoe8989fa2012-03-05 13:15:20 -0800146
Tejun Heo549d3aa2012-03-05 13:15:16 -0800147 kfree(blkg);
Tejun Heo03814112012-03-05 13:15:14 -0800148}
149
150/**
151 * blkg_alloc - allocate a blkg
152 * @blkcg: block cgroup the new blkg is associated with
153 * @q: request_queue the new blkg is associated with
Tejun Heo03814112012-03-05 13:15:14 -0800154 *
Tejun Heoe8989fa2012-03-05 13:15:20 -0800155 * Allocate a new blkg assocating @blkcg and @q.
Tejun Heo03814112012-03-05 13:15:14 -0800156 */
157static struct blkio_group *blkg_alloc(struct blkio_cgroup *blkcg,
Tejun Heoe8989fa2012-03-05 13:15:20 -0800158 struct request_queue *q)
Tejun Heo03814112012-03-05 13:15:14 -0800159{
160 struct blkio_group *blkg;
Tejun Heoe8989fa2012-03-05 13:15:20 -0800161 int i;
Tejun Heo03814112012-03-05 13:15:14 -0800162
163 /* alloc and init base part */
164 blkg = kzalloc_node(sizeof(*blkg), GFP_ATOMIC, q->node);
165 if (!blkg)
166 return NULL;
167
Tejun Heoc875f4d2012-03-05 13:15:22 -0800168 blkg->q = q;
Tejun Heoe8989fa2012-03-05 13:15:20 -0800169 INIT_LIST_HEAD(&blkg->q_node);
Vivek Goyal1cd9e032012-03-08 10:53:56 -0800170 INIT_LIST_HEAD(&blkg->alloc_node);
Tejun Heo03814112012-03-05 13:15:14 -0800171 blkg->blkcg = blkcg;
Tejun Heo1adaf3d2012-03-05 13:15:15 -0800172 blkg->refcnt = 1;
Tejun Heo03814112012-03-05 13:15:14 -0800173 cgroup_path(blkcg->css.cgroup, blkg->path, sizeof(blkg->path));
174
Tejun Heoe8989fa2012-03-05 13:15:20 -0800175 for (i = 0; i < BLKIO_NR_POLICIES; i++) {
176 struct blkio_policy_type *pol = blkio_policy[i];
177 struct blkg_policy_data *pd;
Tejun Heo03814112012-03-05 13:15:14 -0800178
Tejun Heoe8989fa2012-03-05 13:15:20 -0800179 if (!pol)
180 continue;
Tejun Heo549d3aa2012-03-05 13:15:16 -0800181
Tejun Heoe8989fa2012-03-05 13:15:20 -0800182 /* alloc per-policy data and attach it to blkg */
183 pd = kzalloc_node(sizeof(*pd) + pol->pdata_size, GFP_ATOMIC,
184 q->node);
185 if (!pd) {
186 blkg_free(blkg);
187 return NULL;
188 }
Tejun Heo549d3aa2012-03-05 13:15:16 -0800189
Tejun Heoe8989fa2012-03-05 13:15:20 -0800190 blkg->pd[i] = pd;
191 pd->blkg = blkg;
Tejun Heo03814112012-03-05 13:15:14 -0800192 }
193
Tejun Heo549d3aa2012-03-05 13:15:16 -0800194 /* invoke per-policy init */
Tejun Heoe8989fa2012-03-05 13:15:20 -0800195 for (i = 0; i < BLKIO_NR_POLICIES; i++) {
196 struct blkio_policy_type *pol = blkio_policy[i];
197
198 if (pol)
199 pol->ops.blkio_init_group_fn(blkg);
200 }
201
Tejun Heo03814112012-03-05 13:15:14 -0800202 return blkg;
203}
204
Tejun Heocd1604f2012-03-05 13:15:06 -0800205struct blkio_group *blkg_lookup_create(struct blkio_cgroup *blkcg,
206 struct request_queue *q,
Tejun Heocd1604f2012-03-05 13:15:06 -0800207 bool for_root)
208 __releases(q->queue_lock) __acquires(q->queue_lock)
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400209{
Vivek Goyal1cd9e032012-03-08 10:53:56 -0800210 struct blkio_group *blkg;
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400211
Tejun Heocd1604f2012-03-05 13:15:06 -0800212 WARN_ON_ONCE(!rcu_read_lock_held());
213 lockdep_assert_held(q->queue_lock);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500214
Tejun Heocd1604f2012-03-05 13:15:06 -0800215 /*
216 * This could be the first entry point of blkcg implementation and
217 * we shouldn't allow anything to go through for a bypassing queue.
218 * The following can be removed if blkg lookup is guaranteed to
219 * fail on a bypassing queue.
220 */
221 if (unlikely(blk_queue_bypass(q)) && !for_root)
222 return ERR_PTR(blk_queue_dead(q) ? -EINVAL : -EBUSY);
223
Tejun Heoe8989fa2012-03-05 13:15:20 -0800224 blkg = blkg_lookup(blkcg, q);
Tejun Heocd1604f2012-03-05 13:15:06 -0800225 if (blkg)
226 return blkg;
227
Tejun Heo7ee9c562012-03-05 13:15:11 -0800228 /* blkg holds a reference to blkcg */
Tejun Heocd1604f2012-03-05 13:15:06 -0800229 if (!css_tryget(&blkcg->css))
230 return ERR_PTR(-EINVAL);
231
232 /*
233 * Allocate and initialize.
Tejun Heocd1604f2012-03-05 13:15:06 -0800234 */
Vivek Goyal1cd9e032012-03-08 10:53:56 -0800235 blkg = blkg_alloc(blkcg, q);
Tejun Heocd1604f2012-03-05 13:15:06 -0800236
237 /* did alloc fail? */
Vivek Goyal1cd9e032012-03-08 10:53:56 -0800238 if (unlikely(!blkg)) {
Tejun Heocd1604f2012-03-05 13:15:06 -0800239 blkg = ERR_PTR(-ENOMEM);
240 goto out;
241 }
242
243 /* insert */
244 spin_lock(&blkcg->lock);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500245 hlist_add_head_rcu(&blkg->blkcg_node, &blkcg->blkg_list);
Tejun Heoe8989fa2012-03-05 13:15:20 -0800246 list_add(&blkg->q_node, &q->blkg_list);
Tejun Heocd1604f2012-03-05 13:15:06 -0800247 spin_unlock(&blkcg->lock);
Vivek Goyal1cd9e032012-03-08 10:53:56 -0800248
249 spin_lock(&alloc_list_lock);
250 list_add(&blkg->alloc_node, &alloc_list);
251 /* Queue per cpu stat allocation from worker thread. */
252 queue_delayed_work(system_nrt_wq, &blkio_stat_alloc_work, 0);
253 spin_unlock(&alloc_list_lock);
Tejun Heocd1604f2012-03-05 13:15:06 -0800254out:
Tejun Heocd1604f2012-03-05 13:15:06 -0800255 return blkg;
Vivek Goyal31e4c282009-12-03 12:59:42 -0500256}
Tejun Heocd1604f2012-03-05 13:15:06 -0800257EXPORT_SYMBOL_GPL(blkg_lookup_create);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500258
Vivek Goyal31e4c282009-12-03 12:59:42 -0500259/* called under rcu_read_lock(). */
Tejun Heocd1604f2012-03-05 13:15:06 -0800260struct blkio_group *blkg_lookup(struct blkio_cgroup *blkcg,
Tejun Heoe8989fa2012-03-05 13:15:20 -0800261 struct request_queue *q)
Vivek Goyal31e4c282009-12-03 12:59:42 -0500262{
263 struct blkio_group *blkg;
264 struct hlist_node *n;
Vivek Goyal31e4c282009-12-03 12:59:42 -0500265
Tejun Heoca32aef2012-03-05 13:15:03 -0800266 hlist_for_each_entry_rcu(blkg, n, &blkcg->blkg_list, blkcg_node)
Tejun Heoe8989fa2012-03-05 13:15:20 -0800267 if (blkg->q == q)
Vivek Goyal31e4c282009-12-03 12:59:42 -0500268 return blkg;
Vivek Goyal31e4c282009-12-03 12:59:42 -0500269 return NULL;
270}
Tejun Heocd1604f2012-03-05 13:15:06 -0800271EXPORT_SYMBOL_GPL(blkg_lookup);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500272
Tejun Heoe8989fa2012-03-05 13:15:20 -0800273static void blkg_destroy(struct blkio_group *blkg)
Tejun Heo72e06c22012-03-05 13:15:00 -0800274{
Tejun Heo03aa2642012-03-05 13:15:19 -0800275 struct request_queue *q = blkg->q;
Tejun Heo9f13ef62012-03-05 13:15:21 -0800276 struct blkio_cgroup *blkcg = blkg->blkcg;
Tejun Heo03aa2642012-03-05 13:15:19 -0800277
278 lockdep_assert_held(q->queue_lock);
Tejun Heo9f13ef62012-03-05 13:15:21 -0800279 lockdep_assert_held(&blkcg->lock);
Tejun Heo03aa2642012-03-05 13:15:19 -0800280
281 /* Something wrong if we are trying to remove same group twice */
Tejun Heoe8989fa2012-03-05 13:15:20 -0800282 WARN_ON_ONCE(list_empty(&blkg->q_node));
Tejun Heo9f13ef62012-03-05 13:15:21 -0800283 WARN_ON_ONCE(hlist_unhashed(&blkg->blkcg_node));
Tejun Heoe8989fa2012-03-05 13:15:20 -0800284 list_del_init(&blkg->q_node);
Tejun Heo9f13ef62012-03-05 13:15:21 -0800285 hlist_del_init_rcu(&blkg->blkcg_node);
Tejun Heo03aa2642012-03-05 13:15:19 -0800286
Vivek Goyal1cd9e032012-03-08 10:53:56 -0800287 spin_lock(&alloc_list_lock);
288 list_del_init(&blkg->alloc_node);
289 spin_unlock(&alloc_list_lock);
290
Tejun Heo03aa2642012-03-05 13:15:19 -0800291 /*
292 * Put the reference taken at the time of creation so that when all
293 * queues are gone, group can be destroyed.
294 */
295 blkg_put(blkg);
296}
297
Tejun Heoe8989fa2012-03-05 13:15:20 -0800298/*
299 * XXX: This updates blkg policy data in-place for root blkg, which is
300 * necessary across elevator switch and policy registration as root blkgs
301 * aren't shot down. This broken and racy implementation is temporary.
302 * Eventually, blkg shoot down will be replaced by proper in-place update.
303 */
304void update_root_blkg_pd(struct request_queue *q, enum blkio_policy_id plid)
305{
306 struct blkio_policy_type *pol = blkio_policy[plid];
307 struct blkio_group *blkg = blkg_lookup(&blkio_root_cgroup, q);
308 struct blkg_policy_data *pd;
309
310 if (!blkg)
311 return;
312
313 kfree(blkg->pd[plid]);
314 blkg->pd[plid] = NULL;
315
316 if (!pol)
317 return;
318
319 pd = kzalloc(sizeof(*pd) + pol->pdata_size, GFP_KERNEL);
320 WARN_ON_ONCE(!pd);
321
322 pd->stats_cpu = alloc_percpu(struct blkio_group_stats_cpu);
323 WARN_ON_ONCE(!pd->stats_cpu);
324
325 blkg->pd[plid] = pd;
326 pd->blkg = blkg;
327 pol->ops.blkio_init_group_fn(blkg);
328}
329EXPORT_SYMBOL_GPL(update_root_blkg_pd);
330
Tejun Heo9f13ef62012-03-05 13:15:21 -0800331/**
332 * blkg_destroy_all - destroy all blkgs associated with a request_queue
333 * @q: request_queue of interest
334 * @destroy_root: whether to destroy root blkg or not
335 *
336 * Destroy blkgs associated with @q. If @destroy_root is %true, all are
337 * destroyed; otherwise, root blkg is left alone.
338 */
Tejun Heoe8989fa2012-03-05 13:15:20 -0800339void blkg_destroy_all(struct request_queue *q, bool destroy_root)
Tejun Heo03aa2642012-03-05 13:15:19 -0800340{
341 struct blkio_group *blkg, *n;
Tejun Heo72e06c22012-03-05 13:15:00 -0800342
Tejun Heo9f13ef62012-03-05 13:15:21 -0800343 spin_lock_irq(q->queue_lock);
Tejun Heo72e06c22012-03-05 13:15:00 -0800344
Tejun Heo9f13ef62012-03-05 13:15:21 -0800345 list_for_each_entry_safe(blkg, n, &q->blkg_list, q_node) {
346 struct blkio_cgroup *blkcg = blkg->blkcg;
Tejun Heo72e06c22012-03-05 13:15:00 -0800347
Tejun Heo9f13ef62012-03-05 13:15:21 -0800348 /* skip root? */
349 if (!destroy_root && blkg->blkcg == &blkio_root_cgroup)
350 continue;
Tejun Heo03aa2642012-03-05 13:15:19 -0800351
Tejun Heo9f13ef62012-03-05 13:15:21 -0800352 spin_lock(&blkcg->lock);
353 blkg_destroy(blkg);
354 spin_unlock(&blkcg->lock);
Tejun Heo72e06c22012-03-05 13:15:00 -0800355 }
Tejun Heo9f13ef62012-03-05 13:15:21 -0800356
357 spin_unlock_irq(q->queue_lock);
Tejun Heo72e06c22012-03-05 13:15:00 -0800358}
Tejun Heo03aa2642012-03-05 13:15:19 -0800359EXPORT_SYMBOL_GPL(blkg_destroy_all);
Tejun Heo72e06c22012-03-05 13:15:00 -0800360
Tejun Heo1adaf3d2012-03-05 13:15:15 -0800361static void blkg_rcu_free(struct rcu_head *rcu_head)
362{
363 blkg_free(container_of(rcu_head, struct blkio_group, rcu_head));
364}
365
366void __blkg_release(struct blkio_group *blkg)
367{
368 /* release the extra blkcg reference this blkg has been holding */
369 css_put(&blkg->blkcg->css);
370
371 /*
372 * A group is freed in rcu manner. But having an rcu lock does not
373 * mean that one can access all the fields of blkg and assume these
374 * are valid. For example, don't try to follow throtl_data and
375 * request queue links.
376 *
377 * Having a reference to blkg under an rcu allows acess to only
378 * values local to groups like group stats and group rate limits
379 */
380 call_rcu(&blkg->rcu_head, blkg_rcu_free);
381}
382EXPORT_SYMBOL_GPL(__blkg_release);
383
Tejun Heoc1768262012-03-05 13:15:17 -0800384static void blkio_reset_stats_cpu(struct blkio_group *blkg, int plid)
Vivek Goyalf0bdc8c2011-05-19 15:38:30 -0400385{
Tejun Heoc1768262012-03-05 13:15:17 -0800386 struct blkg_policy_data *pd = blkg->pd[plid];
Tejun Heo997a0262012-03-08 10:53:58 -0800387 int cpu;
Vivek Goyal1cd9e032012-03-08 10:53:56 -0800388
389 if (pd->stats_cpu == NULL)
390 return;
Tejun Heo997a0262012-03-08 10:53:58 -0800391
392 for_each_possible_cpu(cpu) {
393 struct blkio_group_stats_cpu *sc =
394 per_cpu_ptr(pd->stats_cpu, cpu);
395
Tejun Heoedcb0722012-04-01 14:38:42 -0700396 blkg_rwstat_reset(&sc->service_bytes);
397 blkg_rwstat_reset(&sc->serviced);
Vivek Goyalf0bdc8c2011-05-19 15:38:30 -0400398 }
399}
400
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700401static int
Divyesh Shah84c124d2010-04-09 08:31:19 +0200402blkiocg_reset_stats(struct cgroup *cgroup, struct cftype *cftype, u64 val)
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700403{
Tejun Heo997a0262012-03-08 10:53:58 -0800404 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgroup);
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700405 struct blkio_group *blkg;
406 struct hlist_node *n;
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700407
Tejun Heoe8989fa2012-03-05 13:15:20 -0800408 spin_lock(&blkio_list_lock);
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700409 spin_lock_irq(&blkcg->lock);
Tejun Heo997a0262012-03-08 10:53:58 -0800410
411 /*
412 * Note that stat reset is racy - it doesn't synchronize against
413 * stat updates. This is a debug feature which shouldn't exist
414 * anyway. If you get hit by a race, retry.
415 */
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700416 hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) {
Tejun Heoe8989fa2012-03-05 13:15:20 -0800417 struct blkio_policy_type *pol;
Tejun Heo549d3aa2012-03-05 13:15:16 -0800418
Tejun Heoe8989fa2012-03-05 13:15:20 -0800419 list_for_each_entry(pol, &blkio_list, list) {
Tejun Heoe8989fa2012-03-05 13:15:20 -0800420 blkio_reset_stats_cpu(blkg, pol->plid);
Tejun Heo9ade5ea2012-04-01 14:38:44 -0700421
422 if (pol->ops.blkio_reset_group_stats_fn)
423 pol->ops.blkio_reset_group_stats_fn(blkg);
Tejun Heoe8989fa2012-03-05 13:15:20 -0800424 }
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700425 }
Vivek Goyalf0bdc8c2011-05-19 15:38:30 -0400426
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700427 spin_unlock_irq(&blkcg->lock);
Tejun Heoe8989fa2012-03-05 13:15:20 -0800428 spin_unlock(&blkio_list_lock);
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700429 return 0;
430}
431
Tejun Heod3d32e62012-04-01 14:38:42 -0700432static const char *blkg_dev_name(struct blkio_group *blkg)
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700433{
Tejun Heod3d32e62012-04-01 14:38:42 -0700434 /* some drivers (floppy) instantiate a queue w/o disk registered */
435 if (blkg->q->backing_dev_info.dev)
436 return dev_name(blkg->q->backing_dev_info.dev);
437 return NULL;
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700438}
439
Tejun Heod3d32e62012-04-01 14:38:42 -0700440/**
441 * blkcg_print_blkgs - helper for printing per-blkg data
442 * @sf: seq_file to print to
443 * @blkcg: blkcg of interest
444 * @prfill: fill function to print out a blkg
445 * @pol: policy in question
446 * @data: data to be passed to @prfill
447 * @show_total: to print out sum of prfill return values or not
448 *
449 * This function invokes @prfill on each blkg of @blkcg if pd for the
450 * policy specified by @pol exists. @prfill is invoked with @sf, the
451 * policy data and @data. If @show_total is %true, the sum of the return
452 * values from @prfill is printed with "Total" label at the end.
453 *
454 * This is to be used to construct print functions for
455 * cftype->read_seq_string method.
456 */
Tejun Heo829fdb52012-04-01 14:38:43 -0700457void blkcg_print_blkgs(struct seq_file *sf, struct blkio_cgroup *blkcg,
458 u64 (*prfill)(struct seq_file *, struct blkg_policy_data *, int),
459 int pol, int data, bool show_total)
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400460{
Tejun Heod3d32e62012-04-01 14:38:42 -0700461 struct blkio_group *blkg;
462 struct hlist_node *n;
463 u64 total = 0;
464
465 spin_lock_irq(&blkcg->lock);
466 hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node)
467 if (blkg->pd[pol])
468 total += prfill(sf, blkg->pd[pol], data);
469 spin_unlock_irq(&blkcg->lock);
470
471 if (show_total)
472 seq_printf(sf, "Total %llu\n", (unsigned long long)total);
473}
Tejun Heo829fdb52012-04-01 14:38:43 -0700474EXPORT_SYMBOL_GPL(blkcg_print_blkgs);
Tejun Heod3d32e62012-04-01 14:38:42 -0700475
476/**
477 * __blkg_prfill_u64 - prfill helper for a single u64 value
478 * @sf: seq_file to print to
479 * @pd: policy data of interest
480 * @v: value to print
481 *
482 * Print @v to @sf for the device assocaited with @pd.
483 */
Tejun Heo829fdb52012-04-01 14:38:43 -0700484u64 __blkg_prfill_u64(struct seq_file *sf, struct blkg_policy_data *pd, u64 v)
Tejun Heod3d32e62012-04-01 14:38:42 -0700485{
486 const char *dname = blkg_dev_name(pd->blkg);
487
488 if (!dname)
489 return 0;
490
491 seq_printf(sf, "%s %llu\n", dname, (unsigned long long)v);
492 return v;
493}
Tejun Heo829fdb52012-04-01 14:38:43 -0700494EXPORT_SYMBOL_GPL(__blkg_prfill_u64);
Tejun Heod3d32e62012-04-01 14:38:42 -0700495
496/**
497 * __blkg_prfill_rwstat - prfill helper for a blkg_rwstat
498 * @sf: seq_file to print to
499 * @pd: policy data of interest
500 * @rwstat: rwstat to print
501 *
502 * Print @rwstat to @sf for the device assocaited with @pd.
503 */
Tejun Heo829fdb52012-04-01 14:38:43 -0700504u64 __blkg_prfill_rwstat(struct seq_file *sf, struct blkg_policy_data *pd,
505 const struct blkg_rwstat *rwstat)
Tejun Heod3d32e62012-04-01 14:38:42 -0700506{
507 static const char *rwstr[] = {
508 [BLKG_RWSTAT_READ] = "Read",
509 [BLKG_RWSTAT_WRITE] = "Write",
510 [BLKG_RWSTAT_SYNC] = "Sync",
511 [BLKG_RWSTAT_ASYNC] = "Async",
512 };
513 const char *dname = blkg_dev_name(pd->blkg);
514 u64 v;
515 int i;
516
517 if (!dname)
518 return 0;
519
520 for (i = 0; i < BLKG_RWSTAT_NR; i++)
521 seq_printf(sf, "%s %s %llu\n", dname, rwstr[i],
522 (unsigned long long)rwstat->cnt[i]);
523
524 v = rwstat->cnt[BLKG_RWSTAT_READ] + rwstat->cnt[BLKG_RWSTAT_WRITE];
525 seq_printf(sf, "%s Total %llu\n", dname, (unsigned long long)v);
526 return v;
527}
528
529static u64 blkg_prfill_stat(struct seq_file *sf, struct blkg_policy_data *pd,
530 int off)
531{
532 return __blkg_prfill_u64(sf, pd,
Tejun Heo155fead2012-04-01 14:38:44 -0700533 blkg_stat_read((void *)pd->pdata + off));
Tejun Heod3d32e62012-04-01 14:38:42 -0700534}
535
536static u64 blkg_prfill_rwstat(struct seq_file *sf, struct blkg_policy_data *pd,
537 int off)
538{
Tejun Heo155fead2012-04-01 14:38:44 -0700539 struct blkg_rwstat rwstat = blkg_rwstat_read((void *)pd->pdata + off);
Tejun Heod3d32e62012-04-01 14:38:42 -0700540
541 return __blkg_prfill_rwstat(sf, pd, &rwstat);
542}
543
544/* print blkg_stat specified by BLKCG_STAT_PRIV() */
Tejun Heo829fdb52012-04-01 14:38:43 -0700545int blkcg_print_stat(struct cgroup *cgrp, struct cftype *cft,
546 struct seq_file *sf)
Tejun Heod3d32e62012-04-01 14:38:42 -0700547{
548 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgrp);
549
550 blkcg_print_blkgs(sf, blkcg, blkg_prfill_stat,
551 BLKCG_STAT_POL(cft->private),
552 BLKCG_STAT_OFF(cft->private), false);
553 return 0;
554}
Tejun Heo829fdb52012-04-01 14:38:43 -0700555EXPORT_SYMBOL_GPL(blkcg_print_stat);
Tejun Heod3d32e62012-04-01 14:38:42 -0700556
557/* print blkg_rwstat specified by BLKCG_STAT_PRIV() */
Tejun Heo829fdb52012-04-01 14:38:43 -0700558int blkcg_print_rwstat(struct cgroup *cgrp, struct cftype *cft,
559 struct seq_file *sf)
Tejun Heod3d32e62012-04-01 14:38:42 -0700560{
561 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgrp);
562
563 blkcg_print_blkgs(sf, blkcg, blkg_prfill_rwstat,
564 BLKCG_STAT_POL(cft->private),
565 BLKCG_STAT_OFF(cft->private), true);
566 return 0;
567}
Tejun Heo829fdb52012-04-01 14:38:43 -0700568EXPORT_SYMBOL_GPL(blkcg_print_rwstat);
Tejun Heod3d32e62012-04-01 14:38:42 -0700569
Tejun Heo3a8b31d2012-04-01 14:38:43 -0700570/**
571 * blkg_conf_prep - parse and prepare for per-blkg config update
572 * @blkcg: target block cgroup
573 * @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
578 * value. This function returns with RCU read locked and must be paired
579 * with blkg_conf_finish().
580 */
Tejun Heo829fdb52012-04-01 14:38:43 -0700581int blkg_conf_prep(struct blkio_cgroup *blkcg, const char *input,
582 struct blkg_conf_ctx *ctx)
Tejun Heo3a8b31d2012-04-01 14:38:43 -0700583 __acquires(rcu)
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800584{
Tejun Heo3a8b31d2012-04-01 14:38:43 -0700585 struct gendisk *disk;
586 struct blkio_group *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();
599
Tejun Heo4bfd4822012-03-05 13:15:08 -0800600 spin_lock_irq(disk->queue->queue_lock);
Tejun Heoaaec55a2012-04-01 14:38:42 -0700601 blkg = blkg_lookup_create(blkcg, disk->queue, false);
Tejun Heo4bfd4822012-03-05 13:15:08 -0800602 spin_unlock_irq(disk->queue->queue_lock);
Tejun Heoe56da7e2012-03-05 13:15:07 -0800603
Tejun Heo4bfd4822012-03-05 13:15:08 -0800604 if (IS_ERR(blkg)) {
605 ret = PTR_ERR(blkg);
Tejun Heo3a8b31d2012-04-01 14:38:43 -0700606 rcu_read_unlock();
607 put_disk(disk);
608 /*
609 * If queue was bypassing, we should retry. Do so after a
610 * short msleep(). It isn't strictly necessary but queue
611 * can be bypassing for some time and it's always nice to
612 * avoid busy looping.
613 */
614 if (ret == -EBUSY) {
615 msleep(10);
616 ret = restart_syscall();
Vivek Goyal7702e8f2010-09-15 17:06:36 -0400617 }
Tejun Heo726fa692012-04-01 14:38:43 -0700618 return ret;
Vivek Goyal062a6442010-09-15 17:06:33 -0400619 }
Tejun Heoe56da7e2012-03-05 13:15:07 -0800620
Tejun Heo3a8b31d2012-04-01 14:38:43 -0700621 ctx->disk = disk;
622 ctx->blkg = blkg;
Tejun Heo726fa692012-04-01 14:38:43 -0700623 ctx->v = v;
624 return 0;
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800625}
Tejun Heo829fdb52012-04-01 14:38:43 -0700626EXPORT_SYMBOL_GPL(blkg_conf_prep);
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800627
Tejun Heo3a8b31d2012-04-01 14:38:43 -0700628/**
629 * blkg_conf_finish - finish up per-blkg config update
630 * @ctx: blkg_conf_ctx intiailized by blkg_conf_prep()
631 *
632 * Finish up after per-blkg config update. This function must be paired
633 * with blkg_conf_prep().
634 */
Tejun Heo829fdb52012-04-01 14:38:43 -0700635void blkg_conf_finish(struct blkg_conf_ctx *ctx)
Tejun Heo3a8b31d2012-04-01 14:38:43 -0700636 __releases(rcu)
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800637{
Tejun Heo3a8b31d2012-04-01 14:38:43 -0700638 rcu_read_unlock();
639 put_disk(ctx->disk);
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800640}
Tejun Heo829fdb52012-04-01 14:38:43 -0700641EXPORT_SYMBOL_GPL(blkg_conf_finish);
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800642
Vivek Goyal31e4c282009-12-03 12:59:42 -0500643struct cftype blkio_files[] = {
644 {
Divyesh Shah84c124d2010-04-09 08:31:19 +0200645 .name = "reset_stats",
646 .write_u64 = blkiocg_reset_stats,
Vivek Goyal22084192009-12-03 12:59:49 -0500647 },
Tejun Heo4baf6e32012-04-01 12:09:55 -0700648 { } /* terminate */
Vivek Goyal31e4c282009-12-03 12:59:42 -0500649};
650
Tejun Heo9f13ef62012-03-05 13:15:21 -0800651/**
652 * blkiocg_pre_destroy - cgroup pre_destroy callback
Tejun Heo9f13ef62012-03-05 13:15:21 -0800653 * @cgroup: cgroup of interest
654 *
655 * This function is called when @cgroup is about to go away and responsible
656 * for shooting down all blkgs associated with @cgroup. blkgs should be
657 * removed while holding both q and blkcg locks. As blkcg lock is nested
658 * inside q lock, this function performs reverse double lock dancing.
659 *
660 * This is the blkcg counterpart of ioc_release_fn().
661 */
Tejun Heo959d8512012-04-01 12:30:01 -0700662static int blkiocg_pre_destroy(struct cgroup *cgroup)
Vivek Goyal31e4c282009-12-03 12:59:42 -0500663{
664 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgroup);
665
Tejun Heo9f13ef62012-03-05 13:15:21 -0800666 spin_lock_irq(&blkcg->lock);
Tejun Heo7ee9c562012-03-05 13:15:11 -0800667
Tejun Heo9f13ef62012-03-05 13:15:21 -0800668 while (!hlist_empty(&blkcg->blkg_list)) {
669 struct blkio_group *blkg = hlist_entry(blkcg->blkg_list.first,
670 struct blkio_group, blkcg_node);
Tejun Heoc875f4d2012-03-05 13:15:22 -0800671 struct request_queue *q = blkg->q;
Vivek Goyalb1c35762009-12-03 12:59:47 -0500672
Tejun Heo9f13ef62012-03-05 13:15:21 -0800673 if (spin_trylock(q->queue_lock)) {
674 blkg_destroy(blkg);
675 spin_unlock(q->queue_lock);
676 } else {
677 spin_unlock_irq(&blkcg->lock);
Tejun Heo9f13ef62012-03-05 13:15:21 -0800678 cpu_relax();
Dan Carpentera5567932012-03-29 20:57:08 +0200679 spin_lock_irq(&blkcg->lock);
Jens Axboe0f3942a2010-05-03 14:28:55 +0200680 }
Tejun Heo9f13ef62012-03-05 13:15:21 -0800681 }
Jens Axboe0f3942a2010-05-03 14:28:55 +0200682
Tejun Heo9f13ef62012-03-05 13:15:21 -0800683 spin_unlock_irq(&blkcg->lock);
Tejun Heo7ee9c562012-03-05 13:15:11 -0800684 return 0;
685}
686
Li Zefan761b3ef2012-01-31 13:47:36 +0800687static void blkiocg_destroy(struct cgroup *cgroup)
Tejun Heo7ee9c562012-03-05 13:15:11 -0800688{
689 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgroup);
690
Ben Blum67523c42010-03-10 15:22:11 -0800691 if (blkcg != &blkio_root_cgroup)
692 kfree(blkcg);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500693}
694
Li Zefan761b3ef2012-01-31 13:47:36 +0800695static struct cgroup_subsys_state *blkiocg_create(struct cgroup *cgroup)
Vivek Goyal31e4c282009-12-03 12:59:42 -0500696{
Tejun Heo9a9e8a22012-03-19 15:10:56 -0700697 static atomic64_t id_seq = ATOMIC64_INIT(0);
Li Zefan03415092010-05-07 08:57:00 +0200698 struct blkio_cgroup *blkcg;
699 struct cgroup *parent = cgroup->parent;
Vivek Goyal31e4c282009-12-03 12:59:42 -0500700
Li Zefan03415092010-05-07 08:57:00 +0200701 if (!parent) {
Vivek Goyal31e4c282009-12-03 12:59:42 -0500702 blkcg = &blkio_root_cgroup;
703 goto done;
704 }
705
Vivek Goyal31e4c282009-12-03 12:59:42 -0500706 blkcg = kzalloc(sizeof(*blkcg), GFP_KERNEL);
707 if (!blkcg)
708 return ERR_PTR(-ENOMEM);
709
710 blkcg->weight = BLKIO_WEIGHT_DEFAULT;
Tejun Heo9a9e8a22012-03-19 15:10:56 -0700711 blkcg->id = atomic64_inc_return(&id_seq); /* root is 0, start from 1 */
Vivek Goyal31e4c282009-12-03 12:59:42 -0500712done:
713 spin_lock_init(&blkcg->lock);
714 INIT_HLIST_HEAD(&blkcg->blkg_list);
715
716 return &blkcg->css;
717}
718
Tejun Heo5efd6112012-03-05 13:15:12 -0800719/**
720 * blkcg_init_queue - initialize blkcg part of request queue
721 * @q: request_queue to initialize
722 *
723 * Called from blk_alloc_queue_node(). Responsible for initializing blkcg
724 * part of new request_queue @q.
725 *
726 * RETURNS:
727 * 0 on success, -errno on failure.
728 */
729int blkcg_init_queue(struct request_queue *q)
730{
Tejun Heo923adde2012-03-05 13:15:13 -0800731 int ret;
732
Tejun Heo5efd6112012-03-05 13:15:12 -0800733 might_sleep();
734
Tejun Heo923adde2012-03-05 13:15:13 -0800735 ret = blk_throtl_init(q);
736 if (ret)
737 return ret;
738
739 mutex_lock(&all_q_mutex);
740 INIT_LIST_HEAD(&q->all_q_node);
741 list_add_tail(&q->all_q_node, &all_q_list);
742 mutex_unlock(&all_q_mutex);
743
744 return 0;
Tejun Heo5efd6112012-03-05 13:15:12 -0800745}
746
747/**
748 * blkcg_drain_queue - drain blkcg part of request_queue
749 * @q: request_queue to drain
750 *
751 * Called from blk_drain_queue(). Responsible for draining blkcg part.
752 */
753void blkcg_drain_queue(struct request_queue *q)
754{
755 lockdep_assert_held(q->queue_lock);
756
757 blk_throtl_drain(q);
758}
759
760/**
761 * blkcg_exit_queue - exit and release blkcg part of request_queue
762 * @q: request_queue being released
763 *
764 * Called from blk_release_queue(). Responsible for exiting blkcg part.
765 */
766void blkcg_exit_queue(struct request_queue *q)
767{
Tejun Heo923adde2012-03-05 13:15:13 -0800768 mutex_lock(&all_q_mutex);
769 list_del_init(&q->all_q_node);
770 mutex_unlock(&all_q_mutex);
771
Tejun Heoe8989fa2012-03-05 13:15:20 -0800772 blkg_destroy_all(q, true);
773
Tejun Heo5efd6112012-03-05 13:15:12 -0800774 blk_throtl_exit(q);
775}
776
Vivek Goyal31e4c282009-12-03 12:59:42 -0500777/*
778 * We cannot support shared io contexts, as we have no mean to support
779 * two tasks with the same ioc in two different groups without major rework
780 * of the main cic data structures. For now we allow a task to change
781 * its cgroup only if it's the only owner of its ioc.
782 */
Li Zefan761b3ef2012-01-31 13:47:36 +0800783static int blkiocg_can_attach(struct cgroup *cgrp, struct cgroup_taskset *tset)
Vivek Goyal31e4c282009-12-03 12:59:42 -0500784{
Tejun Heobb9d97b2011-12-12 18:12:21 -0800785 struct task_struct *task;
Vivek Goyal31e4c282009-12-03 12:59:42 -0500786 struct io_context *ioc;
787 int ret = 0;
788
789 /* task_lock() is needed to avoid races with exit_io_context() */
Tejun Heobb9d97b2011-12-12 18:12:21 -0800790 cgroup_taskset_for_each(task, cgrp, tset) {
791 task_lock(task);
792 ioc = task->io_context;
793 if (ioc && atomic_read(&ioc->nr_tasks) > 1)
794 ret = -EINVAL;
795 task_unlock(task);
796 if (ret)
797 break;
798 }
Vivek Goyal31e4c282009-12-03 12:59:42 -0500799 return ret;
800}
801
Tejun Heo923adde2012-03-05 13:15:13 -0800802static void blkcg_bypass_start(void)
803 __acquires(&all_q_mutex)
804{
805 struct request_queue *q;
806
807 mutex_lock(&all_q_mutex);
808
809 list_for_each_entry(q, &all_q_list, all_q_node) {
810 blk_queue_bypass_start(q);
Tejun Heoe8989fa2012-03-05 13:15:20 -0800811 blkg_destroy_all(q, false);
Tejun Heo923adde2012-03-05 13:15:13 -0800812 }
813}
814
815static void blkcg_bypass_end(void)
816 __releases(&all_q_mutex)
817{
818 struct request_queue *q;
819
820 list_for_each_entry(q, &all_q_list, all_q_node)
821 blk_queue_bypass_end(q);
822
823 mutex_unlock(&all_q_mutex);
824}
825
Tejun Heo676f7c82012-04-01 12:09:55 -0700826struct cgroup_subsys blkio_subsys = {
827 .name = "blkio",
828 .create = blkiocg_create,
829 .can_attach = blkiocg_can_attach,
Tejun Heo959d8512012-04-01 12:30:01 -0700830 .pre_destroy = blkiocg_pre_destroy,
Tejun Heo676f7c82012-04-01 12:09:55 -0700831 .destroy = blkiocg_destroy,
Tejun Heo676f7c82012-04-01 12:09:55 -0700832 .subsys_id = blkio_subsys_id,
Tejun Heo4baf6e32012-04-01 12:09:55 -0700833 .base_cftypes = blkio_files,
Tejun Heo676f7c82012-04-01 12:09:55 -0700834 .module = THIS_MODULE,
835};
836EXPORT_SYMBOL_GPL(blkio_subsys);
837
Vivek Goyal3e252062009-12-04 10:36:42 -0500838void blkio_policy_register(struct blkio_policy_type *blkiop)
839{
Tejun Heoe8989fa2012-03-05 13:15:20 -0800840 struct request_queue *q;
841
Tejun Heo923adde2012-03-05 13:15:13 -0800842 blkcg_bypass_start();
Vivek Goyal3e252062009-12-04 10:36:42 -0500843 spin_lock(&blkio_list_lock);
Tejun Heo035d10b2012-03-05 13:15:04 -0800844
845 BUG_ON(blkio_policy[blkiop->plid]);
846 blkio_policy[blkiop->plid] = blkiop;
Vivek Goyal3e252062009-12-04 10:36:42 -0500847 list_add_tail(&blkiop->list, &blkio_list);
Tejun Heo035d10b2012-03-05 13:15:04 -0800848
Vivek Goyal3e252062009-12-04 10:36:42 -0500849 spin_unlock(&blkio_list_lock);
Tejun Heoe8989fa2012-03-05 13:15:20 -0800850 list_for_each_entry(q, &all_q_list, all_q_node)
851 update_root_blkg_pd(q, blkiop->plid);
Tejun Heo923adde2012-03-05 13:15:13 -0800852 blkcg_bypass_end();
Tejun Heo44ea53d2012-04-01 14:38:43 -0700853
854 if (blkiop->cftypes)
855 WARN_ON(cgroup_add_cftypes(&blkio_subsys, blkiop->cftypes));
Vivek Goyal3e252062009-12-04 10:36:42 -0500856}
857EXPORT_SYMBOL_GPL(blkio_policy_register);
858
859void blkio_policy_unregister(struct blkio_policy_type *blkiop)
860{
Tejun Heoe8989fa2012-03-05 13:15:20 -0800861 struct request_queue *q;
862
Tejun Heo44ea53d2012-04-01 14:38:43 -0700863 if (blkiop->cftypes)
864 cgroup_rm_cftypes(&blkio_subsys, blkiop->cftypes);
865
Tejun Heo923adde2012-03-05 13:15:13 -0800866 blkcg_bypass_start();
Vivek Goyal3e252062009-12-04 10:36:42 -0500867 spin_lock(&blkio_list_lock);
Tejun Heo035d10b2012-03-05 13:15:04 -0800868
869 BUG_ON(blkio_policy[blkiop->plid] != blkiop);
870 blkio_policy[blkiop->plid] = NULL;
Vivek Goyal3e252062009-12-04 10:36:42 -0500871 list_del_init(&blkiop->list);
Tejun Heo035d10b2012-03-05 13:15:04 -0800872
Vivek Goyal3e252062009-12-04 10:36:42 -0500873 spin_unlock(&blkio_list_lock);
Tejun Heoe8989fa2012-03-05 13:15:20 -0800874 list_for_each_entry(q, &all_q_list, all_q_node)
875 update_root_blkg_pd(q, blkiop->plid);
Tejun Heo923adde2012-03-05 13:15:13 -0800876 blkcg_bypass_end();
Vivek Goyal3e252062009-12-04 10:36:42 -0500877}
878EXPORT_SYMBOL_GPL(blkio_policy_unregister);