blob: 2cc682b860ead9d15350620c595bfbb30f4a533d [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/seq_file.h>
15#include <linux/kdev_t.h>
Vivek Goyal9d6a9862009-12-04 10:36:41 -050016#include <linux/module.h>
Stephen Rothwellaccee782009-12-07 19:29:39 +110017#include <linux/err.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/slab.h>
Vivek Goyal31e4c282009-12-03 12:59:42 -050019#include "blk-cgroup.h"
Vivek Goyal3e252062009-12-04 10:36:42 -050020
21static DEFINE_SPINLOCK(blkio_list_lock);
22static LIST_HEAD(blkio_list);
Vivek Goyalb1c35762009-12-03 12:59:47 -050023
Vivek Goyal31e4c282009-12-03 12:59:42 -050024struct blkio_cgroup blkio_root_cgroup = { .weight = 2*BLKIO_WEIGHT_DEFAULT };
Vivek Goyal9d6a9862009-12-04 10:36:41 -050025EXPORT_SYMBOL_GPL(blkio_root_cgroup);
26
Ben Blum67523c42010-03-10 15:22:11 -080027static struct cgroup_subsys_state *blkiocg_create(struct cgroup_subsys *,
28 struct cgroup *);
29static int blkiocg_can_attach(struct cgroup_subsys *, struct cgroup *,
30 struct task_struct *, bool);
31static void blkiocg_attach(struct cgroup_subsys *, struct cgroup *,
32 struct cgroup *, struct task_struct *, bool);
33static void blkiocg_destroy(struct cgroup_subsys *, struct cgroup *);
34static int blkiocg_populate(struct cgroup_subsys *, struct cgroup *);
35
36struct cgroup_subsys blkio_subsys = {
37 .name = "blkio",
38 .create = blkiocg_create,
39 .can_attach = blkiocg_can_attach,
40 .attach = blkiocg_attach,
41 .destroy = blkiocg_destroy,
42 .populate = blkiocg_populate,
43#ifdef CONFIG_BLK_CGROUP
44 /* note: blkio_subsys_id is otherwise defined in blk-cgroup.h */
45 .subsys_id = blkio_subsys_id,
46#endif
47 .use_id = 1,
48 .module = THIS_MODULE,
49};
50EXPORT_SYMBOL_GPL(blkio_subsys);
51
Vivek Goyal31e4c282009-12-03 12:59:42 -050052struct blkio_cgroup *cgroup_to_blkio_cgroup(struct cgroup *cgroup)
53{
54 return container_of(cgroup_subsys_state(cgroup, blkio_subsys_id),
55 struct blkio_cgroup, css);
56}
Vivek Goyal9d6a9862009-12-04 10:36:41 -050057EXPORT_SYMBOL_GPL(cgroup_to_blkio_cgroup);
Vivek Goyal31e4c282009-12-03 12:59:42 -050058
Vivek Goyal22084192009-12-03 12:59:49 -050059void blkiocg_update_blkio_group_stats(struct blkio_group *blkg,
60 unsigned long time, unsigned long sectors)
61{
62 blkg->time += time;
63 blkg->sectors += sectors;
64}
Vivek Goyal9d6a9862009-12-04 10:36:41 -050065EXPORT_SYMBOL_GPL(blkiocg_update_blkio_group_stats);
Vivek Goyal22084192009-12-03 12:59:49 -050066
Vivek Goyal31e4c282009-12-03 12:59:42 -050067void blkiocg_add_blkio_group(struct blkio_cgroup *blkcg,
Vivek Goyal22084192009-12-03 12:59:49 -050068 struct blkio_group *blkg, void *key, dev_t dev)
Vivek Goyal31e4c282009-12-03 12:59:42 -050069{
70 unsigned long flags;
71
72 spin_lock_irqsave(&blkcg->lock, flags);
73 rcu_assign_pointer(blkg->key, key);
Vivek Goyalb1c35762009-12-03 12:59:47 -050074 blkg->blkcg_id = css_id(&blkcg->css);
Vivek Goyal31e4c282009-12-03 12:59:42 -050075 hlist_add_head_rcu(&blkg->blkcg_node, &blkcg->blkg_list);
76 spin_unlock_irqrestore(&blkcg->lock, flags);
Vivek Goyal2868ef72009-12-03 12:59:48 -050077#ifdef CONFIG_DEBUG_BLK_CGROUP
78 /* Need to take css reference ? */
79 cgroup_path(blkcg->css.cgroup, blkg->path, sizeof(blkg->path));
80#endif
Vivek Goyal22084192009-12-03 12:59:49 -050081 blkg->dev = dev;
Vivek Goyal31e4c282009-12-03 12:59:42 -050082}
Vivek Goyal9d6a9862009-12-04 10:36:41 -050083EXPORT_SYMBOL_GPL(blkiocg_add_blkio_group);
Vivek Goyal31e4c282009-12-03 12:59:42 -050084
Vivek Goyalb1c35762009-12-03 12:59:47 -050085static void __blkiocg_del_blkio_group(struct blkio_group *blkg)
86{
87 hlist_del_init_rcu(&blkg->blkcg_node);
88 blkg->blkcg_id = 0;
89}
90
91/*
92 * returns 0 if blkio_group was still on cgroup list. Otherwise returns 1
93 * indicating that blk_group was unhashed by the time we got to it.
94 */
Vivek Goyal31e4c282009-12-03 12:59:42 -050095int blkiocg_del_blkio_group(struct blkio_group *blkg)
96{
Vivek Goyalb1c35762009-12-03 12:59:47 -050097 struct blkio_cgroup *blkcg;
98 unsigned long flags;
99 struct cgroup_subsys_state *css;
100 int ret = 1;
101
102 rcu_read_lock();
103 css = css_lookup(&blkio_subsys, blkg->blkcg_id);
104 if (!css)
105 goto out;
106
107 blkcg = container_of(css, struct blkio_cgroup, css);
108 spin_lock_irqsave(&blkcg->lock, flags);
109 if (!hlist_unhashed(&blkg->blkcg_node)) {
110 __blkiocg_del_blkio_group(blkg);
111 ret = 0;
112 }
113 spin_unlock_irqrestore(&blkcg->lock, flags);
114out:
115 rcu_read_unlock();
116 return ret;
Vivek Goyal31e4c282009-12-03 12:59:42 -0500117}
Vivek Goyal9d6a9862009-12-04 10:36:41 -0500118EXPORT_SYMBOL_GPL(blkiocg_del_blkio_group);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500119
120/* called under rcu_read_lock(). */
121struct blkio_group *blkiocg_lookup_group(struct blkio_cgroup *blkcg, void *key)
122{
123 struct blkio_group *blkg;
124 struct hlist_node *n;
125 void *__key;
126
127 hlist_for_each_entry_rcu(blkg, n, &blkcg->blkg_list, blkcg_node) {
128 __key = blkg->key;
129 if (__key == key)
130 return blkg;
131 }
132
133 return NULL;
134}
Vivek Goyal9d6a9862009-12-04 10:36:41 -0500135EXPORT_SYMBOL_GPL(blkiocg_lookup_group);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500136
137#define SHOW_FUNCTION(__VAR) \
138static u64 blkiocg_##__VAR##_read(struct cgroup *cgroup, \
139 struct cftype *cftype) \
140{ \
141 struct blkio_cgroup *blkcg; \
142 \
143 blkcg = cgroup_to_blkio_cgroup(cgroup); \
144 return (u64)blkcg->__VAR; \
145}
146
147SHOW_FUNCTION(weight);
148#undef SHOW_FUNCTION
149
150static int
151blkiocg_weight_write(struct cgroup *cgroup, struct cftype *cftype, u64 val)
152{
153 struct blkio_cgroup *blkcg;
Vivek Goyalf8d461d2009-12-03 12:59:52 -0500154 struct blkio_group *blkg;
155 struct hlist_node *n;
Vivek Goyal3e252062009-12-04 10:36:42 -0500156 struct blkio_policy_type *blkiop;
Vivek Goyal31e4c282009-12-03 12:59:42 -0500157
158 if (val < BLKIO_WEIGHT_MIN || val > BLKIO_WEIGHT_MAX)
159 return -EINVAL;
160
161 blkcg = cgroup_to_blkio_cgroup(cgroup);
Gui Jianfengbcf4dd42010-02-01 09:58:54 +0100162 spin_lock(&blkio_list_lock);
Vivek Goyalf8d461d2009-12-03 12:59:52 -0500163 spin_lock_irq(&blkcg->lock);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500164 blkcg->weight = (unsigned int)val;
Vivek Goyal3e252062009-12-04 10:36:42 -0500165 hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) {
Vivek Goyal3e252062009-12-04 10:36:42 -0500166 list_for_each_entry(blkiop, &blkio_list, list)
167 blkiop->ops.blkio_update_group_weight_fn(blkg,
168 blkcg->weight);
Vivek Goyal3e252062009-12-04 10:36:42 -0500169 }
Vivek Goyalf8d461d2009-12-03 12:59:52 -0500170 spin_unlock_irq(&blkcg->lock);
Gui Jianfengbcf4dd42010-02-01 09:58:54 +0100171 spin_unlock(&blkio_list_lock);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500172 return 0;
173}
174
Vivek Goyal22084192009-12-03 12:59:49 -0500175#define SHOW_FUNCTION_PER_GROUP(__VAR) \
176static int blkiocg_##__VAR##_read(struct cgroup *cgroup, \
177 struct cftype *cftype, struct seq_file *m) \
178{ \
179 struct blkio_cgroup *blkcg; \
180 struct blkio_group *blkg; \
181 struct hlist_node *n; \
182 \
183 if (!cgroup_lock_live_group(cgroup)) \
184 return -ENODEV; \
185 \
186 blkcg = cgroup_to_blkio_cgroup(cgroup); \
187 rcu_read_lock(); \
188 hlist_for_each_entry_rcu(blkg, n, &blkcg->blkg_list, blkcg_node) {\
189 if (blkg->dev) \
190 seq_printf(m, "%u:%u %lu\n", MAJOR(blkg->dev), \
191 MINOR(blkg->dev), blkg->__VAR); \
192 } \
193 rcu_read_unlock(); \
194 cgroup_unlock(); \
195 return 0; \
196}
197
198SHOW_FUNCTION_PER_GROUP(time);
199SHOW_FUNCTION_PER_GROUP(sectors);
200#ifdef CONFIG_DEBUG_BLK_CGROUP
201SHOW_FUNCTION_PER_GROUP(dequeue);
202#endif
203#undef SHOW_FUNCTION_PER_GROUP
204
205#ifdef CONFIG_DEBUG_BLK_CGROUP
206void blkiocg_update_blkio_group_dequeue_stats(struct blkio_group *blkg,
207 unsigned long dequeue)
208{
209 blkg->dequeue += dequeue;
210}
Vivek Goyal9d6a9862009-12-04 10:36:41 -0500211EXPORT_SYMBOL_GPL(blkiocg_update_blkio_group_dequeue_stats);
Vivek Goyal22084192009-12-03 12:59:49 -0500212#endif
213
Vivek Goyal31e4c282009-12-03 12:59:42 -0500214struct cftype blkio_files[] = {
215 {
216 .name = "weight",
217 .read_u64 = blkiocg_weight_read,
218 .write_u64 = blkiocg_weight_write,
219 },
Vivek Goyal22084192009-12-03 12:59:49 -0500220 {
221 .name = "time",
222 .read_seq_string = blkiocg_time_read,
223 },
224 {
225 .name = "sectors",
226 .read_seq_string = blkiocg_sectors_read,
227 },
228#ifdef CONFIG_DEBUG_BLK_CGROUP
229 {
230 .name = "dequeue",
231 .read_seq_string = blkiocg_dequeue_read,
232 },
233#endif
Vivek Goyal31e4c282009-12-03 12:59:42 -0500234};
235
236static int blkiocg_populate(struct cgroup_subsys *subsys, struct cgroup *cgroup)
237{
238 return cgroup_add_files(cgroup, subsys, blkio_files,
239 ARRAY_SIZE(blkio_files));
240}
241
242static void blkiocg_destroy(struct cgroup_subsys *subsys, struct cgroup *cgroup)
243{
244 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgroup);
Vivek Goyalb1c35762009-12-03 12:59:47 -0500245 unsigned long flags;
246 struct blkio_group *blkg;
247 void *key;
Vivek Goyal3e252062009-12-04 10:36:42 -0500248 struct blkio_policy_type *blkiop;
Vivek Goyal31e4c282009-12-03 12:59:42 -0500249
Vivek Goyalb1c35762009-12-03 12:59:47 -0500250 rcu_read_lock();
251remove_entry:
252 spin_lock_irqsave(&blkcg->lock, flags);
253
254 if (hlist_empty(&blkcg->blkg_list)) {
255 spin_unlock_irqrestore(&blkcg->lock, flags);
256 goto done;
257 }
258
259 blkg = hlist_entry(blkcg->blkg_list.first, struct blkio_group,
260 blkcg_node);
261 key = rcu_dereference(blkg->key);
262 __blkiocg_del_blkio_group(blkg);
263
264 spin_unlock_irqrestore(&blkcg->lock, flags);
265
266 /*
267 * This blkio_group is being unlinked as associated cgroup is going
268 * away. Let all the IO controlling policies know about this event.
269 *
270 * Currently this is static call to one io controlling policy. Once
271 * we have more policies in place, we need some dynamic registration
272 * of callback function.
273 */
Vivek Goyal3e252062009-12-04 10:36:42 -0500274 spin_lock(&blkio_list_lock);
275 list_for_each_entry(blkiop, &blkio_list, list)
276 blkiop->ops.blkio_unlink_group_fn(key, blkg);
277 spin_unlock(&blkio_list_lock);
Vivek Goyalb1c35762009-12-03 12:59:47 -0500278 goto remove_entry;
279done:
Vivek Goyal31e4c282009-12-03 12:59:42 -0500280 free_css_id(&blkio_subsys, &blkcg->css);
Vivek Goyalb1c35762009-12-03 12:59:47 -0500281 rcu_read_unlock();
Ben Blum67523c42010-03-10 15:22:11 -0800282 if (blkcg != &blkio_root_cgroup)
283 kfree(blkcg);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500284}
285
286static struct cgroup_subsys_state *
287blkiocg_create(struct cgroup_subsys *subsys, struct cgroup *cgroup)
288{
Li Zefan03415092010-05-07 08:57:00 +0200289 struct blkio_cgroup *blkcg;
290 struct cgroup *parent = cgroup->parent;
Vivek Goyal31e4c282009-12-03 12:59:42 -0500291
Li Zefan03415092010-05-07 08:57:00 +0200292 if (!parent) {
Vivek Goyal31e4c282009-12-03 12:59:42 -0500293 blkcg = &blkio_root_cgroup;
294 goto done;
295 }
296
297 /* Currently we do not support hierarchy deeper than two level (0,1) */
Li Zefan03415092010-05-07 08:57:00 +0200298 if (parent != cgroup->top_cgroup)
Vivek Goyal31e4c282009-12-03 12:59:42 -0500299 return ERR_PTR(-EINVAL);
300
301 blkcg = kzalloc(sizeof(*blkcg), GFP_KERNEL);
302 if (!blkcg)
303 return ERR_PTR(-ENOMEM);
304
305 blkcg->weight = BLKIO_WEIGHT_DEFAULT;
306done:
307 spin_lock_init(&blkcg->lock);
308 INIT_HLIST_HEAD(&blkcg->blkg_list);
309
310 return &blkcg->css;
311}
312
313/*
314 * We cannot support shared io contexts, as we have no mean to support
315 * two tasks with the same ioc in two different groups without major rework
316 * of the main cic data structures. For now we allow a task to change
317 * its cgroup only if it's the only owner of its ioc.
318 */
319static int blkiocg_can_attach(struct cgroup_subsys *subsys,
320 struct cgroup *cgroup, struct task_struct *tsk,
321 bool threadgroup)
322{
323 struct io_context *ioc;
324 int ret = 0;
325
326 /* task_lock() is needed to avoid races with exit_io_context() */
327 task_lock(tsk);
328 ioc = tsk->io_context;
329 if (ioc && atomic_read(&ioc->nr_tasks) > 1)
330 ret = -EINVAL;
331 task_unlock(tsk);
332
333 return ret;
334}
335
336static void blkiocg_attach(struct cgroup_subsys *subsys, struct cgroup *cgroup,
337 struct cgroup *prev, struct task_struct *tsk,
338 bool threadgroup)
339{
340 struct io_context *ioc;
341
342 task_lock(tsk);
343 ioc = tsk->io_context;
344 if (ioc)
345 ioc->cgroup_changed = 1;
346 task_unlock(tsk);
347}
348
Vivek Goyal3e252062009-12-04 10:36:42 -0500349void blkio_policy_register(struct blkio_policy_type *blkiop)
350{
351 spin_lock(&blkio_list_lock);
352 list_add_tail(&blkiop->list, &blkio_list);
353 spin_unlock(&blkio_list_lock);
354}
355EXPORT_SYMBOL_GPL(blkio_policy_register);
356
357void blkio_policy_unregister(struct blkio_policy_type *blkiop)
358{
359 spin_lock(&blkio_list_lock);
360 list_del_init(&blkiop->list);
361 spin_unlock(&blkio_list_lock);
362}
363EXPORT_SYMBOL_GPL(blkio_policy_unregister);
Ben Blum67523c42010-03-10 15:22:11 -0800364
365static int __init init_cgroup_blkio(void)
366{
367 return cgroup_load_subsys(&blkio_subsys);
368}
369
370static void __exit exit_cgroup_blkio(void)
371{
372 cgroup_unload_subsys(&blkio_subsys);
373}
374
375module_init(init_cgroup_blkio);
376module_exit(exit_cgroup_blkio);
377MODULE_LICENSE("GPL");