blob: 4b686ad08eaaad5384f7d0c6535b5b4154679c5f [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>
Vivek Goyal31e4c282009-12-03 12:59:42 -050018#include "blk-cgroup.h"
Vivek Goyal3e252062009-12-04 10:36:42 -050019
20static DEFINE_SPINLOCK(blkio_list_lock);
21static LIST_HEAD(blkio_list);
Vivek Goyalb1c35762009-12-03 12:59:47 -050022
Vivek Goyal31e4c282009-12-03 12:59:42 -050023struct blkio_cgroup blkio_root_cgroup = { .weight = 2*BLKIO_WEIGHT_DEFAULT };
Vivek Goyal9d6a9862009-12-04 10:36:41 -050024EXPORT_SYMBOL_GPL(blkio_root_cgroup);
25
Ben Blum67523c42010-03-10 15:22:11 -080026static struct cgroup_subsys_state *blkiocg_create(struct cgroup_subsys *,
27 struct cgroup *);
28static int blkiocg_can_attach(struct cgroup_subsys *, struct cgroup *,
29 struct task_struct *, bool);
30static void blkiocg_attach(struct cgroup_subsys *, struct cgroup *,
31 struct cgroup *, struct task_struct *, bool);
32static void blkiocg_destroy(struct cgroup_subsys *, struct cgroup *);
33static int blkiocg_populate(struct cgroup_subsys *, struct cgroup *);
34
35struct cgroup_subsys blkio_subsys = {
36 .name = "blkio",
37 .create = blkiocg_create,
38 .can_attach = blkiocg_can_attach,
39 .attach = blkiocg_attach,
40 .destroy = blkiocg_destroy,
41 .populate = blkiocg_populate,
42#ifdef CONFIG_BLK_CGROUP
43 /* note: blkio_subsys_id is otherwise defined in blk-cgroup.h */
44 .subsys_id = blkio_subsys_id,
45#endif
46 .use_id = 1,
47 .module = THIS_MODULE,
48};
49EXPORT_SYMBOL_GPL(blkio_subsys);
50
Vivek Goyal31e4c282009-12-03 12:59:42 -050051struct blkio_cgroup *cgroup_to_blkio_cgroup(struct cgroup *cgroup)
52{
53 return container_of(cgroup_subsys_state(cgroup, blkio_subsys_id),
54 struct blkio_cgroup, css);
55}
Vivek Goyal9d6a9862009-12-04 10:36:41 -050056EXPORT_SYMBOL_GPL(cgroup_to_blkio_cgroup);
Vivek Goyal31e4c282009-12-03 12:59:42 -050057
Vivek Goyal22084192009-12-03 12:59:49 -050058void blkiocg_update_blkio_group_stats(struct blkio_group *blkg,
59 unsigned long time, unsigned long sectors)
60{
61 blkg->time += time;
62 blkg->sectors += sectors;
63}
Vivek Goyal9d6a9862009-12-04 10:36:41 -050064EXPORT_SYMBOL_GPL(blkiocg_update_blkio_group_stats);
Vivek Goyal22084192009-12-03 12:59:49 -050065
Vivek Goyal31e4c282009-12-03 12:59:42 -050066void blkiocg_add_blkio_group(struct blkio_cgroup *blkcg,
Vivek Goyal22084192009-12-03 12:59:49 -050067 struct blkio_group *blkg, void *key, dev_t dev)
Vivek Goyal31e4c282009-12-03 12:59:42 -050068{
69 unsigned long flags;
70
71 spin_lock_irqsave(&blkcg->lock, flags);
72 rcu_assign_pointer(blkg->key, key);
Vivek Goyalb1c35762009-12-03 12:59:47 -050073 blkg->blkcg_id = css_id(&blkcg->css);
Vivek Goyal31e4c282009-12-03 12:59:42 -050074 hlist_add_head_rcu(&blkg->blkcg_node, &blkcg->blkg_list);
75 spin_unlock_irqrestore(&blkcg->lock, flags);
Vivek Goyal2868ef72009-12-03 12:59:48 -050076#ifdef CONFIG_DEBUG_BLK_CGROUP
77 /* Need to take css reference ? */
78 cgroup_path(blkcg->css.cgroup, blkg->path, sizeof(blkg->path));
79#endif
Vivek Goyal22084192009-12-03 12:59:49 -050080 blkg->dev = dev;
Vivek Goyal31e4c282009-12-03 12:59:42 -050081}
Vivek Goyal9d6a9862009-12-04 10:36:41 -050082EXPORT_SYMBOL_GPL(blkiocg_add_blkio_group);
Vivek Goyal31e4c282009-12-03 12:59:42 -050083
Vivek Goyalb1c35762009-12-03 12:59:47 -050084static void __blkiocg_del_blkio_group(struct blkio_group *blkg)
85{
86 hlist_del_init_rcu(&blkg->blkcg_node);
87 blkg->blkcg_id = 0;
88}
89
90/*
91 * returns 0 if blkio_group was still on cgroup list. Otherwise returns 1
92 * indicating that blk_group was unhashed by the time we got to it.
93 */
Vivek Goyal31e4c282009-12-03 12:59:42 -050094int blkiocg_del_blkio_group(struct blkio_group *blkg)
95{
Vivek Goyalb1c35762009-12-03 12:59:47 -050096 struct blkio_cgroup *blkcg;
97 unsigned long flags;
98 struct cgroup_subsys_state *css;
99 int ret = 1;
100
101 rcu_read_lock();
102 css = css_lookup(&blkio_subsys, blkg->blkcg_id);
103 if (!css)
104 goto out;
105
106 blkcg = container_of(css, struct blkio_cgroup, css);
107 spin_lock_irqsave(&blkcg->lock, flags);
108 if (!hlist_unhashed(&blkg->blkcg_node)) {
109 __blkiocg_del_blkio_group(blkg);
110 ret = 0;
111 }
112 spin_unlock_irqrestore(&blkcg->lock, flags);
113out:
114 rcu_read_unlock();
115 return ret;
Vivek Goyal31e4c282009-12-03 12:59:42 -0500116}
Vivek Goyal9d6a9862009-12-04 10:36:41 -0500117EXPORT_SYMBOL_GPL(blkiocg_del_blkio_group);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500118
119/* called under rcu_read_lock(). */
120struct blkio_group *blkiocg_lookup_group(struct blkio_cgroup *blkcg, void *key)
121{
122 struct blkio_group *blkg;
123 struct hlist_node *n;
124 void *__key;
125
126 hlist_for_each_entry_rcu(blkg, n, &blkcg->blkg_list, blkcg_node) {
127 __key = blkg->key;
128 if (__key == key)
129 return blkg;
130 }
131
132 return NULL;
133}
Vivek Goyal9d6a9862009-12-04 10:36:41 -0500134EXPORT_SYMBOL_GPL(blkiocg_lookup_group);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500135
136#define SHOW_FUNCTION(__VAR) \
137static u64 blkiocg_##__VAR##_read(struct cgroup *cgroup, \
138 struct cftype *cftype) \
139{ \
140 struct blkio_cgroup *blkcg; \
141 \
142 blkcg = cgroup_to_blkio_cgroup(cgroup); \
143 return (u64)blkcg->__VAR; \
144}
145
146SHOW_FUNCTION(weight);
147#undef SHOW_FUNCTION
148
149static int
150blkiocg_weight_write(struct cgroup *cgroup, struct cftype *cftype, u64 val)
151{
152 struct blkio_cgroup *blkcg;
Vivek Goyalf8d461d2009-12-03 12:59:52 -0500153 struct blkio_group *blkg;
154 struct hlist_node *n;
Vivek Goyal3e252062009-12-04 10:36:42 -0500155 struct blkio_policy_type *blkiop;
Vivek Goyal31e4c282009-12-03 12:59:42 -0500156
157 if (val < BLKIO_WEIGHT_MIN || val > BLKIO_WEIGHT_MAX)
158 return -EINVAL;
159
160 blkcg = cgroup_to_blkio_cgroup(cgroup);
Gui Jianfengbcf4dd42010-02-01 09:58:54 +0100161 spin_lock(&blkio_list_lock);
Vivek Goyalf8d461d2009-12-03 12:59:52 -0500162 spin_lock_irq(&blkcg->lock);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500163 blkcg->weight = (unsigned int)val;
Vivek Goyal3e252062009-12-04 10:36:42 -0500164 hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) {
Vivek Goyal3e252062009-12-04 10:36:42 -0500165 list_for_each_entry(blkiop, &blkio_list, list)
166 blkiop->ops.blkio_update_group_weight_fn(blkg,
167 blkcg->weight);
Vivek Goyal3e252062009-12-04 10:36:42 -0500168 }
Vivek Goyalf8d461d2009-12-03 12:59:52 -0500169 spin_unlock_irq(&blkcg->lock);
Gui Jianfengbcf4dd42010-02-01 09:58:54 +0100170 spin_unlock(&blkio_list_lock);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500171 return 0;
172}
173
Vivek Goyal22084192009-12-03 12:59:49 -0500174#define SHOW_FUNCTION_PER_GROUP(__VAR) \
175static int blkiocg_##__VAR##_read(struct cgroup *cgroup, \
176 struct cftype *cftype, struct seq_file *m) \
177{ \
178 struct blkio_cgroup *blkcg; \
179 struct blkio_group *blkg; \
180 struct hlist_node *n; \
181 \
182 if (!cgroup_lock_live_group(cgroup)) \
183 return -ENODEV; \
184 \
185 blkcg = cgroup_to_blkio_cgroup(cgroup); \
186 rcu_read_lock(); \
187 hlist_for_each_entry_rcu(blkg, n, &blkcg->blkg_list, blkcg_node) {\
188 if (blkg->dev) \
189 seq_printf(m, "%u:%u %lu\n", MAJOR(blkg->dev), \
190 MINOR(blkg->dev), blkg->__VAR); \
191 } \
192 rcu_read_unlock(); \
193 cgroup_unlock(); \
194 return 0; \
195}
196
197SHOW_FUNCTION_PER_GROUP(time);
198SHOW_FUNCTION_PER_GROUP(sectors);
199#ifdef CONFIG_DEBUG_BLK_CGROUP
200SHOW_FUNCTION_PER_GROUP(dequeue);
201#endif
202#undef SHOW_FUNCTION_PER_GROUP
203
204#ifdef CONFIG_DEBUG_BLK_CGROUP
205void blkiocg_update_blkio_group_dequeue_stats(struct blkio_group *blkg,
206 unsigned long dequeue)
207{
208 blkg->dequeue += dequeue;
209}
Vivek Goyal9d6a9862009-12-04 10:36:41 -0500210EXPORT_SYMBOL_GPL(blkiocg_update_blkio_group_dequeue_stats);
Vivek Goyal22084192009-12-03 12:59:49 -0500211#endif
212
Vivek Goyal31e4c282009-12-03 12:59:42 -0500213struct cftype blkio_files[] = {
214 {
215 .name = "weight",
216 .read_u64 = blkiocg_weight_read,
217 .write_u64 = blkiocg_weight_write,
218 },
Vivek Goyal22084192009-12-03 12:59:49 -0500219 {
220 .name = "time",
221 .read_seq_string = blkiocg_time_read,
222 },
223 {
224 .name = "sectors",
225 .read_seq_string = blkiocg_sectors_read,
226 },
227#ifdef CONFIG_DEBUG_BLK_CGROUP
228 {
229 .name = "dequeue",
230 .read_seq_string = blkiocg_dequeue_read,
231 },
232#endif
Vivek Goyal31e4c282009-12-03 12:59:42 -0500233};
234
235static int blkiocg_populate(struct cgroup_subsys *subsys, struct cgroup *cgroup)
236{
237 return cgroup_add_files(cgroup, subsys, blkio_files,
238 ARRAY_SIZE(blkio_files));
239}
240
241static void blkiocg_destroy(struct cgroup_subsys *subsys, struct cgroup *cgroup)
242{
243 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgroup);
Vivek Goyalb1c35762009-12-03 12:59:47 -0500244 unsigned long flags;
245 struct blkio_group *blkg;
246 void *key;
Vivek Goyal3e252062009-12-04 10:36:42 -0500247 struct blkio_policy_type *blkiop;
Vivek Goyal31e4c282009-12-03 12:59:42 -0500248
Vivek Goyalb1c35762009-12-03 12:59:47 -0500249 rcu_read_lock();
250remove_entry:
251 spin_lock_irqsave(&blkcg->lock, flags);
252
253 if (hlist_empty(&blkcg->blkg_list)) {
254 spin_unlock_irqrestore(&blkcg->lock, flags);
255 goto done;
256 }
257
258 blkg = hlist_entry(blkcg->blkg_list.first, struct blkio_group,
259 blkcg_node);
260 key = rcu_dereference(blkg->key);
261 __blkiocg_del_blkio_group(blkg);
262
263 spin_unlock_irqrestore(&blkcg->lock, flags);
264
265 /*
266 * This blkio_group is being unlinked as associated cgroup is going
267 * away. Let all the IO controlling policies know about this event.
268 *
269 * Currently this is static call to one io controlling policy. Once
270 * we have more policies in place, we need some dynamic registration
271 * of callback function.
272 */
Vivek Goyal3e252062009-12-04 10:36:42 -0500273 spin_lock(&blkio_list_lock);
274 list_for_each_entry(blkiop, &blkio_list, list)
275 blkiop->ops.blkio_unlink_group_fn(key, blkg);
276 spin_unlock(&blkio_list_lock);
Vivek Goyalb1c35762009-12-03 12:59:47 -0500277 goto remove_entry;
278done:
Vivek Goyal31e4c282009-12-03 12:59:42 -0500279 free_css_id(&blkio_subsys, &blkcg->css);
Vivek Goyalb1c35762009-12-03 12:59:47 -0500280 rcu_read_unlock();
Ben Blum67523c42010-03-10 15:22:11 -0800281 if (blkcg != &blkio_root_cgroup)
282 kfree(blkcg);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500283}
284
285static struct cgroup_subsys_state *
286blkiocg_create(struct cgroup_subsys *subsys, struct cgroup *cgroup)
287{
288 struct blkio_cgroup *blkcg, *parent_blkcg;
289
290 if (!cgroup->parent) {
291 blkcg = &blkio_root_cgroup;
292 goto done;
293 }
294
295 /* Currently we do not support hierarchy deeper than two level (0,1) */
296 parent_blkcg = cgroup_to_blkio_cgroup(cgroup->parent);
297 if (css_depth(&parent_blkcg->css) > 0)
298 return ERR_PTR(-EINVAL);
299
300 blkcg = kzalloc(sizeof(*blkcg), GFP_KERNEL);
301 if (!blkcg)
302 return ERR_PTR(-ENOMEM);
303
304 blkcg->weight = BLKIO_WEIGHT_DEFAULT;
305done:
306 spin_lock_init(&blkcg->lock);
307 INIT_HLIST_HEAD(&blkcg->blkg_list);
308
309 return &blkcg->css;
310}
311
312/*
313 * We cannot support shared io contexts, as we have no mean to support
314 * two tasks with the same ioc in two different groups without major rework
315 * of the main cic data structures. For now we allow a task to change
316 * its cgroup only if it's the only owner of its ioc.
317 */
318static int blkiocg_can_attach(struct cgroup_subsys *subsys,
319 struct cgroup *cgroup, struct task_struct *tsk,
320 bool threadgroup)
321{
322 struct io_context *ioc;
323 int ret = 0;
324
325 /* task_lock() is needed to avoid races with exit_io_context() */
326 task_lock(tsk);
327 ioc = tsk->io_context;
328 if (ioc && atomic_read(&ioc->nr_tasks) > 1)
329 ret = -EINVAL;
330 task_unlock(tsk);
331
332 return ret;
333}
334
335static void blkiocg_attach(struct cgroup_subsys *subsys, struct cgroup *cgroup,
336 struct cgroup *prev, struct task_struct *tsk,
337 bool threadgroup)
338{
339 struct io_context *ioc;
340
341 task_lock(tsk);
342 ioc = tsk->io_context;
343 if (ioc)
344 ioc->cgroup_changed = 1;
345 task_unlock(tsk);
346}
347
Vivek Goyal3e252062009-12-04 10:36:42 -0500348void blkio_policy_register(struct blkio_policy_type *blkiop)
349{
350 spin_lock(&blkio_list_lock);
351 list_add_tail(&blkiop->list, &blkio_list);
352 spin_unlock(&blkio_list_lock);
353}
354EXPORT_SYMBOL_GPL(blkio_policy_register);
355
356void blkio_policy_unregister(struct blkio_policy_type *blkiop)
357{
358 spin_lock(&blkio_list_lock);
359 list_del_init(&blkiop->list);
360 spin_unlock(&blkio_list_lock);
361}
362EXPORT_SYMBOL_GPL(blkio_policy_unregister);
Ben Blum67523c42010-03-10 15:22:11 -0800363
364static int __init init_cgroup_blkio(void)
365{
366 return cgroup_load_subsys(&blkio_subsys);
367}
368
369static void __exit exit_cgroup_blkio(void)
370{
371 cgroup_unload_subsys(&blkio_subsys);
372}
373
374module_init(init_cgroup_blkio);
375module_exit(exit_cgroup_blkio);
376MODULE_LICENSE("GPL");