blob: c0e239b8455ec019abe5084fc7a31e9315a3e3f1 [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
Tejun Heo3381cb82012-04-01 14:38:44 -070033struct blkio_cgroup blkio_root_cgroup = { .cfq_weight = 2 * CFQ_WEIGHT_DEFAULT };
Vivek Goyal9d6a9862009-12-04 10:36:41 -050034EXPORT_SYMBOL_GPL(blkio_root_cgroup);
35
Tejun Heo035d10b2012-03-05 13:15:04 -080036static struct blkio_policy_type *blkio_policy[BLKIO_NR_POLICIES];
37
Vivek Goyal31e4c282009-12-03 12:59:42 -050038struct blkio_cgroup *cgroup_to_blkio_cgroup(struct cgroup *cgroup)
39{
40 return container_of(cgroup_subsys_state(cgroup, blkio_subsys_id),
41 struct blkio_cgroup, css);
42}
Vivek Goyal9d6a9862009-12-04 10:36:41 -050043EXPORT_SYMBOL_GPL(cgroup_to_blkio_cgroup);
Vivek Goyal31e4c282009-12-03 12:59:42 -050044
Tejun Heo4f85cb92012-03-05 13:15:28 -080045static struct blkio_cgroup *task_blkio_cgroup(struct task_struct *tsk)
Vivek Goyal70087dc2011-05-16 15:24:08 +020046{
47 return container_of(task_subsys_state(tsk, blkio_subsys_id),
48 struct blkio_cgroup, css);
49}
Tejun Heo4f85cb92012-03-05 13:15:28 -080050
51struct blkio_cgroup *bio_blkio_cgroup(struct bio *bio)
52{
53 if (bio && bio->bi_css)
54 return container_of(bio->bi_css, struct blkio_cgroup, css);
55 return task_blkio_cgroup(current);
56}
57EXPORT_SYMBOL_GPL(bio_blkio_cgroup);
Vivek Goyal70087dc2011-05-16 15:24:08 +020058
Tejun Heo03814112012-03-05 13:15:14 -080059/**
60 * blkg_free - free a blkg
61 * @blkg: blkg to free
62 *
63 * Free @blkg which may be partially allocated.
64 */
65static void blkg_free(struct blkio_group *blkg)
66{
Tejun Heoe8989fa2012-03-05 13:15:20 -080067 int i;
Tejun Heo549d3aa2012-03-05 13:15:16 -080068
69 if (!blkg)
70 return;
71
Tejun Heoe8989fa2012-03-05 13:15:20 -080072 for (i = 0; i < BLKIO_NR_POLICIES; i++) {
Tejun Heo9ade5ea2012-04-01 14:38:44 -070073 struct blkio_policy_type *pol = blkio_policy[i];
Tejun Heoe8989fa2012-03-05 13:15:20 -080074 struct blkg_policy_data *pd = blkg->pd[i];
75
Tejun Heo9ade5ea2012-04-01 14:38:44 -070076 if (!pd)
77 continue;
78
79 if (pol && pol->ops.blkio_exit_group_fn)
80 pol->ops.blkio_exit_group_fn(blkg);
81
Tejun Heo9ade5ea2012-04-01 14:38:44 -070082 kfree(pd);
Tejun Heo03814112012-03-05 13:15:14 -080083 }
Tejun Heoe8989fa2012-03-05 13:15:20 -080084
Tejun Heo549d3aa2012-03-05 13:15:16 -080085 kfree(blkg);
Tejun Heo03814112012-03-05 13:15:14 -080086}
87
88/**
89 * blkg_alloc - allocate a blkg
90 * @blkcg: block cgroup the new blkg is associated with
91 * @q: request_queue the new blkg is associated with
Tejun Heo03814112012-03-05 13:15:14 -080092 *
Tejun Heoe8989fa2012-03-05 13:15:20 -080093 * Allocate a new blkg assocating @blkcg and @q.
Tejun Heo03814112012-03-05 13:15:14 -080094 */
95static struct blkio_group *blkg_alloc(struct blkio_cgroup *blkcg,
Tejun Heoe8989fa2012-03-05 13:15:20 -080096 struct request_queue *q)
Tejun Heo03814112012-03-05 13:15:14 -080097{
98 struct blkio_group *blkg;
Tejun Heoe8989fa2012-03-05 13:15:20 -080099 int i;
Tejun Heo03814112012-03-05 13:15:14 -0800100
101 /* alloc and init base part */
102 blkg = kzalloc_node(sizeof(*blkg), GFP_ATOMIC, q->node);
103 if (!blkg)
104 return NULL;
105
Tejun Heoc875f4d2012-03-05 13:15:22 -0800106 blkg->q = q;
Tejun Heoe8989fa2012-03-05 13:15:20 -0800107 INIT_LIST_HEAD(&blkg->q_node);
Tejun Heo03814112012-03-05 13:15:14 -0800108 blkg->blkcg = blkcg;
Tejun Heo1adaf3d2012-03-05 13:15:15 -0800109 blkg->refcnt = 1;
Tejun Heo03814112012-03-05 13:15:14 -0800110 cgroup_path(blkcg->css.cgroup, blkg->path, sizeof(blkg->path));
111
Tejun Heoe8989fa2012-03-05 13:15:20 -0800112 for (i = 0; i < BLKIO_NR_POLICIES; i++) {
113 struct blkio_policy_type *pol = blkio_policy[i];
114 struct blkg_policy_data *pd;
Tejun Heo03814112012-03-05 13:15:14 -0800115
Tejun Heoe8989fa2012-03-05 13:15:20 -0800116 if (!pol)
117 continue;
Tejun Heo549d3aa2012-03-05 13:15:16 -0800118
Tejun Heoe8989fa2012-03-05 13:15:20 -0800119 /* alloc per-policy data and attach it to blkg */
120 pd = kzalloc_node(sizeof(*pd) + pol->pdata_size, GFP_ATOMIC,
121 q->node);
122 if (!pd) {
123 blkg_free(blkg);
124 return NULL;
125 }
Tejun Heo549d3aa2012-03-05 13:15:16 -0800126
Tejun Heoe8989fa2012-03-05 13:15:20 -0800127 blkg->pd[i] = pd;
128 pd->blkg = blkg;
Tejun Heo03814112012-03-05 13:15:14 -0800129 }
130
Tejun Heo549d3aa2012-03-05 13:15:16 -0800131 /* invoke per-policy init */
Tejun Heoe8989fa2012-03-05 13:15:20 -0800132 for (i = 0; i < BLKIO_NR_POLICIES; i++) {
133 struct blkio_policy_type *pol = blkio_policy[i];
134
135 if (pol)
136 pol->ops.blkio_init_group_fn(blkg);
137 }
138
Tejun Heo03814112012-03-05 13:15:14 -0800139 return blkg;
140}
141
Tejun Heocd1604f2012-03-05 13:15:06 -0800142struct blkio_group *blkg_lookup_create(struct blkio_cgroup *blkcg,
143 struct request_queue *q,
Tejun Heocd1604f2012-03-05 13:15:06 -0800144 bool for_root)
145 __releases(q->queue_lock) __acquires(q->queue_lock)
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400146{
Vivek Goyal1cd9e032012-03-08 10:53:56 -0800147 struct blkio_group *blkg;
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400148
Tejun Heocd1604f2012-03-05 13:15:06 -0800149 WARN_ON_ONCE(!rcu_read_lock_held());
150 lockdep_assert_held(q->queue_lock);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500151
Tejun Heocd1604f2012-03-05 13:15:06 -0800152 /*
153 * This could be the first entry point of blkcg implementation and
154 * we shouldn't allow anything to go through for a bypassing queue.
155 * The following can be removed if blkg lookup is guaranteed to
156 * fail on a bypassing queue.
157 */
158 if (unlikely(blk_queue_bypass(q)) && !for_root)
159 return ERR_PTR(blk_queue_dead(q) ? -EINVAL : -EBUSY);
160
Tejun Heoe8989fa2012-03-05 13:15:20 -0800161 blkg = blkg_lookup(blkcg, q);
Tejun Heocd1604f2012-03-05 13:15:06 -0800162 if (blkg)
163 return blkg;
164
Tejun Heo7ee9c562012-03-05 13:15:11 -0800165 /* blkg holds a reference to blkcg */
Tejun Heocd1604f2012-03-05 13:15:06 -0800166 if (!css_tryget(&blkcg->css))
167 return ERR_PTR(-EINVAL);
168
169 /*
170 * Allocate and initialize.
Tejun Heocd1604f2012-03-05 13:15:06 -0800171 */
Vivek Goyal1cd9e032012-03-08 10:53:56 -0800172 blkg = blkg_alloc(blkcg, q);
Tejun Heocd1604f2012-03-05 13:15:06 -0800173
174 /* did alloc fail? */
Vivek Goyal1cd9e032012-03-08 10:53:56 -0800175 if (unlikely(!blkg)) {
Tejun Heocd1604f2012-03-05 13:15:06 -0800176 blkg = ERR_PTR(-ENOMEM);
177 goto out;
178 }
179
180 /* insert */
181 spin_lock(&blkcg->lock);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500182 hlist_add_head_rcu(&blkg->blkcg_node, &blkcg->blkg_list);
Tejun Heoe8989fa2012-03-05 13:15:20 -0800183 list_add(&blkg->q_node, &q->blkg_list);
Tejun Heocd1604f2012-03-05 13:15:06 -0800184 spin_unlock(&blkcg->lock);
185out:
Tejun Heocd1604f2012-03-05 13:15:06 -0800186 return blkg;
Vivek Goyal31e4c282009-12-03 12:59:42 -0500187}
Tejun Heocd1604f2012-03-05 13:15:06 -0800188EXPORT_SYMBOL_GPL(blkg_lookup_create);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500189
Vivek Goyal31e4c282009-12-03 12:59:42 -0500190/* called under rcu_read_lock(). */
Tejun Heocd1604f2012-03-05 13:15:06 -0800191struct blkio_group *blkg_lookup(struct blkio_cgroup *blkcg,
Tejun Heoe8989fa2012-03-05 13:15:20 -0800192 struct request_queue *q)
Vivek Goyal31e4c282009-12-03 12:59:42 -0500193{
194 struct blkio_group *blkg;
195 struct hlist_node *n;
Vivek Goyal31e4c282009-12-03 12:59:42 -0500196
Tejun Heoca32aef2012-03-05 13:15:03 -0800197 hlist_for_each_entry_rcu(blkg, n, &blkcg->blkg_list, blkcg_node)
Tejun Heoe8989fa2012-03-05 13:15:20 -0800198 if (blkg->q == q)
Vivek Goyal31e4c282009-12-03 12:59:42 -0500199 return blkg;
Vivek Goyal31e4c282009-12-03 12:59:42 -0500200 return NULL;
201}
Tejun Heocd1604f2012-03-05 13:15:06 -0800202EXPORT_SYMBOL_GPL(blkg_lookup);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500203
Tejun Heoe8989fa2012-03-05 13:15:20 -0800204static void blkg_destroy(struct blkio_group *blkg)
Tejun Heo72e06c22012-03-05 13:15:00 -0800205{
Tejun Heo03aa2642012-03-05 13:15:19 -0800206 struct request_queue *q = blkg->q;
Tejun Heo9f13ef62012-03-05 13:15:21 -0800207 struct blkio_cgroup *blkcg = blkg->blkcg;
Tejun Heo03aa2642012-03-05 13:15:19 -0800208
209 lockdep_assert_held(q->queue_lock);
Tejun Heo9f13ef62012-03-05 13:15:21 -0800210 lockdep_assert_held(&blkcg->lock);
Tejun Heo03aa2642012-03-05 13:15:19 -0800211
212 /* Something wrong if we are trying to remove same group twice */
Tejun Heoe8989fa2012-03-05 13:15:20 -0800213 WARN_ON_ONCE(list_empty(&blkg->q_node));
Tejun Heo9f13ef62012-03-05 13:15:21 -0800214 WARN_ON_ONCE(hlist_unhashed(&blkg->blkcg_node));
Tejun Heoe8989fa2012-03-05 13:15:20 -0800215 list_del_init(&blkg->q_node);
Tejun Heo9f13ef62012-03-05 13:15:21 -0800216 hlist_del_init_rcu(&blkg->blkcg_node);
Tejun Heo03aa2642012-03-05 13:15:19 -0800217
Tejun Heo03aa2642012-03-05 13:15:19 -0800218 /*
219 * Put the reference taken at the time of creation so that when all
220 * queues are gone, group can be destroyed.
221 */
222 blkg_put(blkg);
223}
224
Tejun Heoe8989fa2012-03-05 13:15:20 -0800225/*
226 * XXX: This updates blkg policy data in-place for root blkg, which is
227 * necessary across elevator switch and policy registration as root blkgs
228 * aren't shot down. This broken and racy implementation is temporary.
229 * Eventually, blkg shoot down will be replaced by proper in-place update.
230 */
231void update_root_blkg_pd(struct request_queue *q, enum blkio_policy_id plid)
232{
233 struct blkio_policy_type *pol = blkio_policy[plid];
234 struct blkio_group *blkg = blkg_lookup(&blkio_root_cgroup, q);
235 struct blkg_policy_data *pd;
236
237 if (!blkg)
238 return;
239
240 kfree(blkg->pd[plid]);
241 blkg->pd[plid] = NULL;
242
243 if (!pol)
244 return;
245
246 pd = kzalloc(sizeof(*pd) + pol->pdata_size, GFP_KERNEL);
247 WARN_ON_ONCE(!pd);
248
Tejun Heoe8989fa2012-03-05 13:15:20 -0800249 blkg->pd[plid] = pd;
250 pd->blkg = blkg;
251 pol->ops.blkio_init_group_fn(blkg);
252}
253EXPORT_SYMBOL_GPL(update_root_blkg_pd);
254
Tejun Heo9f13ef62012-03-05 13:15:21 -0800255/**
256 * blkg_destroy_all - destroy all blkgs associated with a request_queue
257 * @q: request_queue of interest
258 * @destroy_root: whether to destroy root blkg or not
259 *
260 * Destroy blkgs associated with @q. If @destroy_root is %true, all are
261 * destroyed; otherwise, root blkg is left alone.
262 */
Tejun Heoe8989fa2012-03-05 13:15:20 -0800263void blkg_destroy_all(struct request_queue *q, bool destroy_root)
Tejun Heo03aa2642012-03-05 13:15:19 -0800264{
265 struct blkio_group *blkg, *n;
Tejun Heo72e06c22012-03-05 13:15:00 -0800266
Tejun Heo9f13ef62012-03-05 13:15:21 -0800267 spin_lock_irq(q->queue_lock);
Tejun Heo72e06c22012-03-05 13:15:00 -0800268
Tejun Heo9f13ef62012-03-05 13:15:21 -0800269 list_for_each_entry_safe(blkg, n, &q->blkg_list, q_node) {
270 struct blkio_cgroup *blkcg = blkg->blkcg;
Tejun Heo72e06c22012-03-05 13:15:00 -0800271
Tejun Heo9f13ef62012-03-05 13:15:21 -0800272 /* skip root? */
273 if (!destroy_root && blkg->blkcg == &blkio_root_cgroup)
274 continue;
Tejun Heo03aa2642012-03-05 13:15:19 -0800275
Tejun Heo9f13ef62012-03-05 13:15:21 -0800276 spin_lock(&blkcg->lock);
277 blkg_destroy(blkg);
278 spin_unlock(&blkcg->lock);
Tejun Heo72e06c22012-03-05 13:15:00 -0800279 }
Tejun Heo9f13ef62012-03-05 13:15:21 -0800280
281 spin_unlock_irq(q->queue_lock);
Tejun Heo72e06c22012-03-05 13:15:00 -0800282}
Tejun Heo03aa2642012-03-05 13:15:19 -0800283EXPORT_SYMBOL_GPL(blkg_destroy_all);
Tejun Heo72e06c22012-03-05 13:15:00 -0800284
Tejun Heo1adaf3d2012-03-05 13:15:15 -0800285static void blkg_rcu_free(struct rcu_head *rcu_head)
286{
287 blkg_free(container_of(rcu_head, struct blkio_group, rcu_head));
288}
289
290void __blkg_release(struct blkio_group *blkg)
291{
292 /* release the extra blkcg reference this blkg has been holding */
293 css_put(&blkg->blkcg->css);
294
295 /*
296 * A group is freed in rcu manner. But having an rcu lock does not
297 * mean that one can access all the fields of blkg and assume these
298 * are valid. For example, don't try to follow throtl_data and
299 * request queue links.
300 *
301 * Having a reference to blkg under an rcu allows acess to only
302 * values local to groups like group stats and group rate limits
303 */
304 call_rcu(&blkg->rcu_head, blkg_rcu_free);
305}
306EXPORT_SYMBOL_GPL(__blkg_release);
307
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700308static int
Divyesh Shah84c124d2010-04-09 08:31:19 +0200309blkiocg_reset_stats(struct cgroup *cgroup, struct cftype *cftype, u64 val)
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700310{
Tejun Heo997a0262012-03-08 10:53:58 -0800311 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgroup);
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700312 struct blkio_group *blkg;
313 struct hlist_node *n;
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700314
Tejun Heoe8989fa2012-03-05 13:15:20 -0800315 spin_lock(&blkio_list_lock);
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700316 spin_lock_irq(&blkcg->lock);
Tejun Heo997a0262012-03-08 10:53:58 -0800317
318 /*
319 * Note that stat reset is racy - it doesn't synchronize against
320 * stat updates. This is a debug feature which shouldn't exist
321 * anyway. If you get hit by a race, retry.
322 */
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700323 hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) {
Tejun Heoe8989fa2012-03-05 13:15:20 -0800324 struct blkio_policy_type *pol;
Tejun Heo549d3aa2012-03-05 13:15:16 -0800325
Tejun Heo8a3d2612012-04-01 14:38:44 -0700326 list_for_each_entry(pol, &blkio_list, list)
Tejun Heo9ade5ea2012-04-01 14:38:44 -0700327 if (pol->ops.blkio_reset_group_stats_fn)
328 pol->ops.blkio_reset_group_stats_fn(blkg);
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700329 }
Vivek Goyalf0bdc8c2011-05-19 15:38:30 -0400330
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700331 spin_unlock_irq(&blkcg->lock);
Tejun Heoe8989fa2012-03-05 13:15:20 -0800332 spin_unlock(&blkio_list_lock);
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700333 return 0;
334}
335
Tejun Heod3d32e62012-04-01 14:38:42 -0700336static const char *blkg_dev_name(struct blkio_group *blkg)
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700337{
Tejun Heod3d32e62012-04-01 14:38:42 -0700338 /* some drivers (floppy) instantiate a queue w/o disk registered */
339 if (blkg->q->backing_dev_info.dev)
340 return dev_name(blkg->q->backing_dev_info.dev);
341 return NULL;
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700342}
343
Tejun Heod3d32e62012-04-01 14:38:42 -0700344/**
345 * blkcg_print_blkgs - helper for printing per-blkg data
346 * @sf: seq_file to print to
347 * @blkcg: blkcg of interest
348 * @prfill: fill function to print out a blkg
349 * @pol: policy in question
350 * @data: data to be passed to @prfill
351 * @show_total: to print out sum of prfill return values or not
352 *
353 * This function invokes @prfill on each blkg of @blkcg if pd for the
354 * policy specified by @pol exists. @prfill is invoked with @sf, the
355 * policy data and @data. If @show_total is %true, the sum of the return
356 * values from @prfill is printed with "Total" label at the end.
357 *
358 * This is to be used to construct print functions for
359 * cftype->read_seq_string method.
360 */
Tejun Heo829fdb52012-04-01 14:38:43 -0700361void blkcg_print_blkgs(struct seq_file *sf, struct blkio_cgroup *blkcg,
362 u64 (*prfill)(struct seq_file *, struct blkg_policy_data *, int),
363 int pol, int data, bool show_total)
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400364{
Tejun Heod3d32e62012-04-01 14:38:42 -0700365 struct blkio_group *blkg;
366 struct hlist_node *n;
367 u64 total = 0;
368
369 spin_lock_irq(&blkcg->lock);
370 hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node)
371 if (blkg->pd[pol])
372 total += prfill(sf, blkg->pd[pol], data);
373 spin_unlock_irq(&blkcg->lock);
374
375 if (show_total)
376 seq_printf(sf, "Total %llu\n", (unsigned long long)total);
377}
Tejun Heo829fdb52012-04-01 14:38:43 -0700378EXPORT_SYMBOL_GPL(blkcg_print_blkgs);
Tejun Heod3d32e62012-04-01 14:38:42 -0700379
380/**
381 * __blkg_prfill_u64 - prfill helper for a single u64 value
382 * @sf: seq_file to print to
383 * @pd: policy data of interest
384 * @v: value to print
385 *
386 * Print @v to @sf for the device assocaited with @pd.
387 */
Tejun Heo829fdb52012-04-01 14:38:43 -0700388u64 __blkg_prfill_u64(struct seq_file *sf, struct blkg_policy_data *pd, u64 v)
Tejun Heod3d32e62012-04-01 14:38:42 -0700389{
390 const char *dname = blkg_dev_name(pd->blkg);
391
392 if (!dname)
393 return 0;
394
395 seq_printf(sf, "%s %llu\n", dname, (unsigned long long)v);
396 return v;
397}
Tejun Heo829fdb52012-04-01 14:38:43 -0700398EXPORT_SYMBOL_GPL(__blkg_prfill_u64);
Tejun Heod3d32e62012-04-01 14:38:42 -0700399
400/**
401 * __blkg_prfill_rwstat - prfill helper for a blkg_rwstat
402 * @sf: seq_file to print to
403 * @pd: policy data of interest
404 * @rwstat: rwstat to print
405 *
406 * Print @rwstat to @sf for the device assocaited with @pd.
407 */
Tejun Heo829fdb52012-04-01 14:38:43 -0700408u64 __blkg_prfill_rwstat(struct seq_file *sf, struct blkg_policy_data *pd,
409 const struct blkg_rwstat *rwstat)
Tejun Heod3d32e62012-04-01 14:38:42 -0700410{
411 static const char *rwstr[] = {
412 [BLKG_RWSTAT_READ] = "Read",
413 [BLKG_RWSTAT_WRITE] = "Write",
414 [BLKG_RWSTAT_SYNC] = "Sync",
415 [BLKG_RWSTAT_ASYNC] = "Async",
416 };
417 const char *dname = blkg_dev_name(pd->blkg);
418 u64 v;
419 int i;
420
421 if (!dname)
422 return 0;
423
424 for (i = 0; i < BLKG_RWSTAT_NR; i++)
425 seq_printf(sf, "%s %s %llu\n", dname, rwstr[i],
426 (unsigned long long)rwstat->cnt[i]);
427
428 v = rwstat->cnt[BLKG_RWSTAT_READ] + rwstat->cnt[BLKG_RWSTAT_WRITE];
429 seq_printf(sf, "%s Total %llu\n", dname, (unsigned long long)v);
430 return v;
431}
432
433static u64 blkg_prfill_stat(struct seq_file *sf, struct blkg_policy_data *pd,
434 int off)
435{
436 return __blkg_prfill_u64(sf, pd,
Tejun Heo155fead2012-04-01 14:38:44 -0700437 blkg_stat_read((void *)pd->pdata + off));
Tejun Heod3d32e62012-04-01 14:38:42 -0700438}
439
440static u64 blkg_prfill_rwstat(struct seq_file *sf, struct blkg_policy_data *pd,
441 int off)
442{
Tejun Heo155fead2012-04-01 14:38:44 -0700443 struct blkg_rwstat rwstat = blkg_rwstat_read((void *)pd->pdata + off);
Tejun Heod3d32e62012-04-01 14:38:42 -0700444
445 return __blkg_prfill_rwstat(sf, pd, &rwstat);
446}
447
448/* print blkg_stat specified by BLKCG_STAT_PRIV() */
Tejun Heo829fdb52012-04-01 14:38:43 -0700449int blkcg_print_stat(struct cgroup *cgrp, struct cftype *cft,
450 struct seq_file *sf)
Tejun Heod3d32e62012-04-01 14:38:42 -0700451{
452 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgrp);
453
454 blkcg_print_blkgs(sf, blkcg, blkg_prfill_stat,
455 BLKCG_STAT_POL(cft->private),
456 BLKCG_STAT_OFF(cft->private), false);
457 return 0;
458}
Tejun Heo829fdb52012-04-01 14:38:43 -0700459EXPORT_SYMBOL_GPL(blkcg_print_stat);
Tejun Heod3d32e62012-04-01 14:38:42 -0700460
461/* print blkg_rwstat specified by BLKCG_STAT_PRIV() */
Tejun Heo829fdb52012-04-01 14:38:43 -0700462int blkcg_print_rwstat(struct cgroup *cgrp, struct cftype *cft,
463 struct seq_file *sf)
Tejun Heod3d32e62012-04-01 14:38:42 -0700464{
465 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgrp);
466
467 blkcg_print_blkgs(sf, blkcg, blkg_prfill_rwstat,
468 BLKCG_STAT_POL(cft->private),
469 BLKCG_STAT_OFF(cft->private), true);
470 return 0;
471}
Tejun Heo829fdb52012-04-01 14:38:43 -0700472EXPORT_SYMBOL_GPL(blkcg_print_rwstat);
Tejun Heod3d32e62012-04-01 14:38:42 -0700473
Tejun Heo3a8b31d2012-04-01 14:38:43 -0700474/**
475 * blkg_conf_prep - parse and prepare for per-blkg config update
476 * @blkcg: target block cgroup
477 * @input: input string
478 * @ctx: blkg_conf_ctx to be filled
479 *
480 * Parse per-blkg config update from @input and initialize @ctx with the
481 * result. @ctx->blkg points to the blkg to be updated and @ctx->v the new
482 * value. This function returns with RCU read locked and must be paired
483 * with blkg_conf_finish().
484 */
Tejun Heo829fdb52012-04-01 14:38:43 -0700485int blkg_conf_prep(struct blkio_cgroup *blkcg, const char *input,
486 struct blkg_conf_ctx *ctx)
Tejun Heo3a8b31d2012-04-01 14:38:43 -0700487 __acquires(rcu)
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800488{
Tejun Heo3a8b31d2012-04-01 14:38:43 -0700489 struct gendisk *disk;
490 struct blkio_group *blkg;
Tejun Heo726fa692012-04-01 14:38:43 -0700491 unsigned int major, minor;
492 unsigned long long v;
493 int part, ret;
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800494
Tejun Heo726fa692012-04-01 14:38:43 -0700495 if (sscanf(input, "%u:%u %llu", &major, &minor, &v) != 3)
496 return -EINVAL;
Tejun Heo3a8b31d2012-04-01 14:38:43 -0700497
Tejun Heo726fa692012-04-01 14:38:43 -0700498 disk = get_gendisk(MKDEV(major, minor), &part);
Tejun Heo4bfd4822012-03-05 13:15:08 -0800499 if (!disk || part)
Tejun Heo726fa692012-04-01 14:38:43 -0700500 return -EINVAL;
Tejun Heoe56da7e2012-03-05 13:15:07 -0800501
502 rcu_read_lock();
503
Tejun Heo4bfd4822012-03-05 13:15:08 -0800504 spin_lock_irq(disk->queue->queue_lock);
Tejun Heoaaec55a2012-04-01 14:38:42 -0700505 blkg = blkg_lookup_create(blkcg, disk->queue, false);
Tejun Heo4bfd4822012-03-05 13:15:08 -0800506 spin_unlock_irq(disk->queue->queue_lock);
Tejun Heoe56da7e2012-03-05 13:15:07 -0800507
Tejun Heo4bfd4822012-03-05 13:15:08 -0800508 if (IS_ERR(blkg)) {
509 ret = PTR_ERR(blkg);
Tejun Heo3a8b31d2012-04-01 14:38:43 -0700510 rcu_read_unlock();
511 put_disk(disk);
512 /*
513 * If queue was bypassing, we should retry. Do so after a
514 * short msleep(). It isn't strictly necessary but queue
515 * can be bypassing for some time and it's always nice to
516 * avoid busy looping.
517 */
518 if (ret == -EBUSY) {
519 msleep(10);
520 ret = restart_syscall();
Vivek Goyal7702e8f2010-09-15 17:06:36 -0400521 }
Tejun Heo726fa692012-04-01 14:38:43 -0700522 return ret;
Vivek Goyal062a6442010-09-15 17:06:33 -0400523 }
Tejun Heoe56da7e2012-03-05 13:15:07 -0800524
Tejun Heo3a8b31d2012-04-01 14:38:43 -0700525 ctx->disk = disk;
526 ctx->blkg = blkg;
Tejun Heo726fa692012-04-01 14:38:43 -0700527 ctx->v = v;
528 return 0;
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800529}
Tejun Heo829fdb52012-04-01 14:38:43 -0700530EXPORT_SYMBOL_GPL(blkg_conf_prep);
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800531
Tejun Heo3a8b31d2012-04-01 14:38:43 -0700532/**
533 * blkg_conf_finish - finish up per-blkg config update
534 * @ctx: blkg_conf_ctx intiailized by blkg_conf_prep()
535 *
536 * Finish up after per-blkg config update. This function must be paired
537 * with blkg_conf_prep().
538 */
Tejun Heo829fdb52012-04-01 14:38:43 -0700539void blkg_conf_finish(struct blkg_conf_ctx *ctx)
Tejun Heo3a8b31d2012-04-01 14:38:43 -0700540 __releases(rcu)
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800541{
Tejun Heo3a8b31d2012-04-01 14:38:43 -0700542 rcu_read_unlock();
543 put_disk(ctx->disk);
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800544}
Tejun Heo829fdb52012-04-01 14:38:43 -0700545EXPORT_SYMBOL_GPL(blkg_conf_finish);
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800546
Vivek Goyal31e4c282009-12-03 12:59:42 -0500547struct cftype blkio_files[] = {
548 {
Divyesh Shah84c124d2010-04-09 08:31:19 +0200549 .name = "reset_stats",
550 .write_u64 = blkiocg_reset_stats,
Vivek Goyal22084192009-12-03 12:59:49 -0500551 },
Tejun Heo4baf6e32012-04-01 12:09:55 -0700552 { } /* terminate */
Vivek Goyal31e4c282009-12-03 12:59:42 -0500553};
554
Tejun Heo9f13ef62012-03-05 13:15:21 -0800555/**
556 * blkiocg_pre_destroy - cgroup pre_destroy callback
Tejun Heo9f13ef62012-03-05 13:15:21 -0800557 * @cgroup: cgroup of interest
558 *
559 * This function is called when @cgroup is about to go away and responsible
560 * for shooting down all blkgs associated with @cgroup. blkgs should be
561 * removed while holding both q and blkcg locks. As blkcg lock is nested
562 * inside q lock, this function performs reverse double lock dancing.
563 *
564 * This is the blkcg counterpart of ioc_release_fn().
565 */
Tejun Heo959d8512012-04-01 12:30:01 -0700566static int blkiocg_pre_destroy(struct cgroup *cgroup)
Vivek Goyal31e4c282009-12-03 12:59:42 -0500567{
568 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgroup);
569
Tejun Heo9f13ef62012-03-05 13:15:21 -0800570 spin_lock_irq(&blkcg->lock);
Tejun Heo7ee9c562012-03-05 13:15:11 -0800571
Tejun Heo9f13ef62012-03-05 13:15:21 -0800572 while (!hlist_empty(&blkcg->blkg_list)) {
573 struct blkio_group *blkg = hlist_entry(blkcg->blkg_list.first,
574 struct blkio_group, blkcg_node);
Tejun Heoc875f4d2012-03-05 13:15:22 -0800575 struct request_queue *q = blkg->q;
Vivek Goyalb1c35762009-12-03 12:59:47 -0500576
Tejun Heo9f13ef62012-03-05 13:15:21 -0800577 if (spin_trylock(q->queue_lock)) {
578 blkg_destroy(blkg);
579 spin_unlock(q->queue_lock);
580 } else {
581 spin_unlock_irq(&blkcg->lock);
Tejun Heo9f13ef62012-03-05 13:15:21 -0800582 cpu_relax();
Dan Carpentera5567932012-03-29 20:57:08 +0200583 spin_lock_irq(&blkcg->lock);
Jens Axboe0f3942a2010-05-03 14:28:55 +0200584 }
Tejun Heo9f13ef62012-03-05 13:15:21 -0800585 }
Jens Axboe0f3942a2010-05-03 14:28:55 +0200586
Tejun Heo9f13ef62012-03-05 13:15:21 -0800587 spin_unlock_irq(&blkcg->lock);
Tejun Heo7ee9c562012-03-05 13:15:11 -0800588 return 0;
589}
590
Li Zefan761b3ef2012-01-31 13:47:36 +0800591static void blkiocg_destroy(struct cgroup *cgroup)
Tejun Heo7ee9c562012-03-05 13:15:11 -0800592{
593 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgroup);
594
Ben Blum67523c42010-03-10 15:22:11 -0800595 if (blkcg != &blkio_root_cgroup)
596 kfree(blkcg);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500597}
598
Li Zefan761b3ef2012-01-31 13:47:36 +0800599static struct cgroup_subsys_state *blkiocg_create(struct cgroup *cgroup)
Vivek Goyal31e4c282009-12-03 12:59:42 -0500600{
Tejun Heo9a9e8a22012-03-19 15:10:56 -0700601 static atomic64_t id_seq = ATOMIC64_INIT(0);
Li Zefan03415092010-05-07 08:57:00 +0200602 struct blkio_cgroup *blkcg;
603 struct cgroup *parent = cgroup->parent;
Vivek Goyal31e4c282009-12-03 12:59:42 -0500604
Li Zefan03415092010-05-07 08:57:00 +0200605 if (!parent) {
Vivek Goyal31e4c282009-12-03 12:59:42 -0500606 blkcg = &blkio_root_cgroup;
607 goto done;
608 }
609
Vivek Goyal31e4c282009-12-03 12:59:42 -0500610 blkcg = kzalloc(sizeof(*blkcg), GFP_KERNEL);
611 if (!blkcg)
612 return ERR_PTR(-ENOMEM);
613
Tejun Heo3381cb82012-04-01 14:38:44 -0700614 blkcg->cfq_weight = CFQ_WEIGHT_DEFAULT;
Tejun Heo9a9e8a22012-03-19 15:10:56 -0700615 blkcg->id = atomic64_inc_return(&id_seq); /* root is 0, start from 1 */
Vivek Goyal31e4c282009-12-03 12:59:42 -0500616done:
617 spin_lock_init(&blkcg->lock);
618 INIT_HLIST_HEAD(&blkcg->blkg_list);
619
620 return &blkcg->css;
621}
622
Tejun Heo5efd6112012-03-05 13:15:12 -0800623/**
624 * blkcg_init_queue - initialize blkcg part of request queue
625 * @q: request_queue to initialize
626 *
627 * Called from blk_alloc_queue_node(). Responsible for initializing blkcg
628 * part of new request_queue @q.
629 *
630 * RETURNS:
631 * 0 on success, -errno on failure.
632 */
633int blkcg_init_queue(struct request_queue *q)
634{
Tejun Heo923adde2012-03-05 13:15:13 -0800635 int ret;
636
Tejun Heo5efd6112012-03-05 13:15:12 -0800637 might_sleep();
638
Tejun Heo923adde2012-03-05 13:15:13 -0800639 ret = blk_throtl_init(q);
640 if (ret)
641 return ret;
642
643 mutex_lock(&all_q_mutex);
644 INIT_LIST_HEAD(&q->all_q_node);
645 list_add_tail(&q->all_q_node, &all_q_list);
646 mutex_unlock(&all_q_mutex);
647
648 return 0;
Tejun Heo5efd6112012-03-05 13:15:12 -0800649}
650
651/**
652 * blkcg_drain_queue - drain blkcg part of request_queue
653 * @q: request_queue to drain
654 *
655 * Called from blk_drain_queue(). Responsible for draining blkcg part.
656 */
657void blkcg_drain_queue(struct request_queue *q)
658{
659 lockdep_assert_held(q->queue_lock);
660
661 blk_throtl_drain(q);
662}
663
664/**
665 * blkcg_exit_queue - exit and release blkcg part of request_queue
666 * @q: request_queue being released
667 *
668 * Called from blk_release_queue(). Responsible for exiting blkcg part.
669 */
670void blkcg_exit_queue(struct request_queue *q)
671{
Tejun Heo923adde2012-03-05 13:15:13 -0800672 mutex_lock(&all_q_mutex);
673 list_del_init(&q->all_q_node);
674 mutex_unlock(&all_q_mutex);
675
Tejun Heoe8989fa2012-03-05 13:15:20 -0800676 blkg_destroy_all(q, true);
677
Tejun Heo5efd6112012-03-05 13:15:12 -0800678 blk_throtl_exit(q);
679}
680
Vivek Goyal31e4c282009-12-03 12:59:42 -0500681/*
682 * We cannot support shared io contexts, as we have no mean to support
683 * two tasks with the same ioc in two different groups without major rework
684 * of the main cic data structures. For now we allow a task to change
685 * its cgroup only if it's the only owner of its ioc.
686 */
Li Zefan761b3ef2012-01-31 13:47:36 +0800687static int blkiocg_can_attach(struct cgroup *cgrp, struct cgroup_taskset *tset)
Vivek Goyal31e4c282009-12-03 12:59:42 -0500688{
Tejun Heobb9d97b2011-12-12 18:12:21 -0800689 struct task_struct *task;
Vivek Goyal31e4c282009-12-03 12:59:42 -0500690 struct io_context *ioc;
691 int ret = 0;
692
693 /* task_lock() is needed to avoid races with exit_io_context() */
Tejun Heobb9d97b2011-12-12 18:12:21 -0800694 cgroup_taskset_for_each(task, cgrp, tset) {
695 task_lock(task);
696 ioc = task->io_context;
697 if (ioc && atomic_read(&ioc->nr_tasks) > 1)
698 ret = -EINVAL;
699 task_unlock(task);
700 if (ret)
701 break;
702 }
Vivek Goyal31e4c282009-12-03 12:59:42 -0500703 return ret;
704}
705
Tejun Heo923adde2012-03-05 13:15:13 -0800706static void blkcg_bypass_start(void)
707 __acquires(&all_q_mutex)
708{
709 struct request_queue *q;
710
711 mutex_lock(&all_q_mutex);
712
713 list_for_each_entry(q, &all_q_list, all_q_node) {
714 blk_queue_bypass_start(q);
Tejun Heoe8989fa2012-03-05 13:15:20 -0800715 blkg_destroy_all(q, false);
Tejun Heo923adde2012-03-05 13:15:13 -0800716 }
717}
718
719static void blkcg_bypass_end(void)
720 __releases(&all_q_mutex)
721{
722 struct request_queue *q;
723
724 list_for_each_entry(q, &all_q_list, all_q_node)
725 blk_queue_bypass_end(q);
726
727 mutex_unlock(&all_q_mutex);
728}
729
Tejun Heo676f7c82012-04-01 12:09:55 -0700730struct cgroup_subsys blkio_subsys = {
731 .name = "blkio",
732 .create = blkiocg_create,
733 .can_attach = blkiocg_can_attach,
Tejun Heo959d8512012-04-01 12:30:01 -0700734 .pre_destroy = blkiocg_pre_destroy,
Tejun Heo676f7c82012-04-01 12:09:55 -0700735 .destroy = blkiocg_destroy,
Tejun Heo676f7c82012-04-01 12:09:55 -0700736 .subsys_id = blkio_subsys_id,
Tejun Heo4baf6e32012-04-01 12:09:55 -0700737 .base_cftypes = blkio_files,
Tejun Heo676f7c82012-04-01 12:09:55 -0700738 .module = THIS_MODULE,
739};
740EXPORT_SYMBOL_GPL(blkio_subsys);
741
Vivek Goyal3e252062009-12-04 10:36:42 -0500742void blkio_policy_register(struct blkio_policy_type *blkiop)
743{
Tejun Heoe8989fa2012-03-05 13:15:20 -0800744 struct request_queue *q;
745
Tejun Heo923adde2012-03-05 13:15:13 -0800746 blkcg_bypass_start();
Vivek Goyal3e252062009-12-04 10:36:42 -0500747 spin_lock(&blkio_list_lock);
Tejun Heo035d10b2012-03-05 13:15:04 -0800748
749 BUG_ON(blkio_policy[blkiop->plid]);
750 blkio_policy[blkiop->plid] = blkiop;
Vivek Goyal3e252062009-12-04 10:36:42 -0500751 list_add_tail(&blkiop->list, &blkio_list);
Tejun Heo035d10b2012-03-05 13:15:04 -0800752
Vivek Goyal3e252062009-12-04 10:36:42 -0500753 spin_unlock(&blkio_list_lock);
Tejun Heoe8989fa2012-03-05 13:15:20 -0800754 list_for_each_entry(q, &all_q_list, all_q_node)
755 update_root_blkg_pd(q, blkiop->plid);
Tejun Heo923adde2012-03-05 13:15:13 -0800756 blkcg_bypass_end();
Tejun Heo44ea53d2012-04-01 14:38:43 -0700757
758 if (blkiop->cftypes)
759 WARN_ON(cgroup_add_cftypes(&blkio_subsys, blkiop->cftypes));
Vivek Goyal3e252062009-12-04 10:36:42 -0500760}
761EXPORT_SYMBOL_GPL(blkio_policy_register);
762
763void blkio_policy_unregister(struct blkio_policy_type *blkiop)
764{
Tejun Heoe8989fa2012-03-05 13:15:20 -0800765 struct request_queue *q;
766
Tejun Heo44ea53d2012-04-01 14:38:43 -0700767 if (blkiop->cftypes)
768 cgroup_rm_cftypes(&blkio_subsys, blkiop->cftypes);
769
Tejun Heo923adde2012-03-05 13:15:13 -0800770 blkcg_bypass_start();
Vivek Goyal3e252062009-12-04 10:36:42 -0500771 spin_lock(&blkio_list_lock);
Tejun Heo035d10b2012-03-05 13:15:04 -0800772
773 BUG_ON(blkio_policy[blkiop->plid] != blkiop);
774 blkio_policy[blkiop->plid] = NULL;
Vivek Goyal3e252062009-12-04 10:36:42 -0500775 list_del_init(&blkiop->list);
Tejun Heo035d10b2012-03-05 13:15:04 -0800776
Vivek Goyal3e252062009-12-04 10:36:42 -0500777 spin_unlock(&blkio_list_lock);
Tejun Heoe8989fa2012-03-05 13:15:20 -0800778 list_for_each_entry(q, &all_q_list, all_q_node)
779 update_root_blkg_pd(q, blkiop->plid);
Tejun Heo923adde2012-03-05 13:15:13 -0800780 blkcg_bypass_end();
Vivek Goyal3e252062009-12-04 10:36:42 -0500781}
782EXPORT_SYMBOL_GPL(blkio_policy_unregister);