net/intel: remove trailing space in messages
[linux-2.6/libata-dev.git] / block / blk-cgroup.c
blob4b686ad08eaaad5384f7d0c6535b5b4154679c5f
1 /*
2 * Common Block IO controller cgroup interface
4 * Based on ideas and code from CFQ, CFS and BFQ:
5 * Copyright (C) 2003 Jens Axboe <axboe@kernel.dk>
7 * Copyright (C) 2008 Fabio Checconi <fabio@gandalf.sssup.it>
8 * Paolo Valente <paolo.valente@unimore.it>
10 * Copyright (C) 2009 Vivek Goyal <vgoyal@redhat.com>
11 * Nauman Rafique <nauman@google.com>
13 #include <linux/ioprio.h>
14 #include <linux/seq_file.h>
15 #include <linux/kdev_t.h>
16 #include <linux/module.h>
17 #include <linux/err.h>
18 #include "blk-cgroup.h"
20 static DEFINE_SPINLOCK(blkio_list_lock);
21 static LIST_HEAD(blkio_list);
23 struct blkio_cgroup blkio_root_cgroup = { .weight = 2*BLKIO_WEIGHT_DEFAULT };
24 EXPORT_SYMBOL_GPL(blkio_root_cgroup);
26 static struct cgroup_subsys_state *blkiocg_create(struct cgroup_subsys *,
27 struct cgroup *);
28 static int blkiocg_can_attach(struct cgroup_subsys *, struct cgroup *,
29 struct task_struct *, bool);
30 static void blkiocg_attach(struct cgroup_subsys *, struct cgroup *,
31 struct cgroup *, struct task_struct *, bool);
32 static void blkiocg_destroy(struct cgroup_subsys *, struct cgroup *);
33 static int blkiocg_populate(struct cgroup_subsys *, struct cgroup *);
35 struct 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,
49 EXPORT_SYMBOL_GPL(blkio_subsys);
51 struct blkio_cgroup *cgroup_to_blkio_cgroup(struct cgroup *cgroup)
53 return container_of(cgroup_subsys_state(cgroup, blkio_subsys_id),
54 struct blkio_cgroup, css);
56 EXPORT_SYMBOL_GPL(cgroup_to_blkio_cgroup);
58 void blkiocg_update_blkio_group_stats(struct blkio_group *blkg,
59 unsigned long time, unsigned long sectors)
61 blkg->time += time;
62 blkg->sectors += sectors;
64 EXPORT_SYMBOL_GPL(blkiocg_update_blkio_group_stats);
66 void blkiocg_add_blkio_group(struct blkio_cgroup *blkcg,
67 struct blkio_group *blkg, void *key, dev_t dev)
69 unsigned long flags;
71 spin_lock_irqsave(&blkcg->lock, flags);
72 rcu_assign_pointer(blkg->key, key);
73 blkg->blkcg_id = css_id(&blkcg->css);
74 hlist_add_head_rcu(&blkg->blkcg_node, &blkcg->blkg_list);
75 spin_unlock_irqrestore(&blkcg->lock, flags);
76 #ifdef CONFIG_DEBUG_BLK_CGROUP
77 /* Need to take css reference ? */
78 cgroup_path(blkcg->css.cgroup, blkg->path, sizeof(blkg->path));
79 #endif
80 blkg->dev = dev;
82 EXPORT_SYMBOL_GPL(blkiocg_add_blkio_group);
84 static void __blkiocg_del_blkio_group(struct blkio_group *blkg)
86 hlist_del_init_rcu(&blkg->blkcg_node);
87 blkg->blkcg_id = 0;
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.
94 int blkiocg_del_blkio_group(struct blkio_group *blkg)
96 struct blkio_cgroup *blkcg;
97 unsigned long flags;
98 struct cgroup_subsys_state *css;
99 int ret = 1;
101 rcu_read_lock();
102 css = css_lookup(&blkio_subsys, blkg->blkcg_id);
103 if (!css)
104 goto out;
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;
112 spin_unlock_irqrestore(&blkcg->lock, flags);
113 out:
114 rcu_read_unlock();
115 return ret;
117 EXPORT_SYMBOL_GPL(blkiocg_del_blkio_group);
119 /* called under rcu_read_lock(). */
120 struct blkio_group *blkiocg_lookup_group(struct blkio_cgroup *blkcg, void *key)
122 struct blkio_group *blkg;
123 struct hlist_node *n;
124 void *__key;
126 hlist_for_each_entry_rcu(blkg, n, &blkcg->blkg_list, blkcg_node) {
127 __key = blkg->key;
128 if (__key == key)
129 return blkg;
132 return NULL;
134 EXPORT_SYMBOL_GPL(blkiocg_lookup_group);
136 #define SHOW_FUNCTION(__VAR) \
137 static u64 blkiocg_##__VAR##_read(struct cgroup *cgroup, \
138 struct cftype *cftype) \
140 struct blkio_cgroup *blkcg; \
142 blkcg = cgroup_to_blkio_cgroup(cgroup); \
143 return (u64)blkcg->__VAR; \
146 SHOW_FUNCTION(weight);
147 #undef SHOW_FUNCTION
149 static int
150 blkiocg_weight_write(struct cgroup *cgroup, struct cftype *cftype, u64 val)
152 struct blkio_cgroup *blkcg;
153 struct blkio_group *blkg;
154 struct hlist_node *n;
155 struct blkio_policy_type *blkiop;
157 if (val < BLKIO_WEIGHT_MIN || val > BLKIO_WEIGHT_MAX)
158 return -EINVAL;
160 blkcg = cgroup_to_blkio_cgroup(cgroup);
161 spin_lock(&blkio_list_lock);
162 spin_lock_irq(&blkcg->lock);
163 blkcg->weight = (unsigned int)val;
164 hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) {
165 list_for_each_entry(blkiop, &blkio_list, list)
166 blkiop->ops.blkio_update_group_weight_fn(blkg,
167 blkcg->weight);
169 spin_unlock_irq(&blkcg->lock);
170 spin_unlock(&blkio_list_lock);
171 return 0;
174 #define SHOW_FUNCTION_PER_GROUP(__VAR) \
175 static int blkiocg_##__VAR##_read(struct cgroup *cgroup, \
176 struct cftype *cftype, struct seq_file *m) \
178 struct blkio_cgroup *blkcg; \
179 struct blkio_group *blkg; \
180 struct hlist_node *n; \
182 if (!cgroup_lock_live_group(cgroup)) \
183 return -ENODEV; \
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); \
192 rcu_read_unlock(); \
193 cgroup_unlock(); \
194 return 0; \
197 SHOW_FUNCTION_PER_GROUP(time);
198 SHOW_FUNCTION_PER_GROUP(sectors);
199 #ifdef CONFIG_DEBUG_BLK_CGROUP
200 SHOW_FUNCTION_PER_GROUP(dequeue);
201 #endif
202 #undef SHOW_FUNCTION_PER_GROUP
204 #ifdef CONFIG_DEBUG_BLK_CGROUP
205 void blkiocg_update_blkio_group_dequeue_stats(struct blkio_group *blkg,
206 unsigned long dequeue)
208 blkg->dequeue += dequeue;
210 EXPORT_SYMBOL_GPL(blkiocg_update_blkio_group_dequeue_stats);
211 #endif
213 struct cftype blkio_files[] = {
215 .name = "weight",
216 .read_u64 = blkiocg_weight_read,
217 .write_u64 = blkiocg_weight_write,
220 .name = "time",
221 .read_seq_string = blkiocg_time_read,
224 .name = "sectors",
225 .read_seq_string = blkiocg_sectors_read,
227 #ifdef CONFIG_DEBUG_BLK_CGROUP
229 .name = "dequeue",
230 .read_seq_string = blkiocg_dequeue_read,
232 #endif
235 static int blkiocg_populate(struct cgroup_subsys *subsys, struct cgroup *cgroup)
237 return cgroup_add_files(cgroup, subsys, blkio_files,
238 ARRAY_SIZE(blkio_files));
241 static void blkiocg_destroy(struct cgroup_subsys *subsys, struct cgroup *cgroup)
243 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgroup);
244 unsigned long flags;
245 struct blkio_group *blkg;
246 void *key;
247 struct blkio_policy_type *blkiop;
249 rcu_read_lock();
250 remove_entry:
251 spin_lock_irqsave(&blkcg->lock, flags);
253 if (hlist_empty(&blkcg->blkg_list)) {
254 spin_unlock_irqrestore(&blkcg->lock, flags);
255 goto done;
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);
263 spin_unlock_irqrestore(&blkcg->lock, flags);
266 * This blkio_group is being unlinked as associated cgroup is going
267 * away. Let all the IO controlling policies know about this event.
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.
273 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);
277 goto remove_entry;
278 done:
279 free_css_id(&blkio_subsys, &blkcg->css);
280 rcu_read_unlock();
281 if (blkcg != &blkio_root_cgroup)
282 kfree(blkcg);
285 static struct cgroup_subsys_state *
286 blkiocg_create(struct cgroup_subsys *subsys, struct cgroup *cgroup)
288 struct blkio_cgroup *blkcg, *parent_blkcg;
290 if (!cgroup->parent) {
291 blkcg = &blkio_root_cgroup;
292 goto done;
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);
300 blkcg = kzalloc(sizeof(*blkcg), GFP_KERNEL);
301 if (!blkcg)
302 return ERR_PTR(-ENOMEM);
304 blkcg->weight = BLKIO_WEIGHT_DEFAULT;
305 done:
306 spin_lock_init(&blkcg->lock);
307 INIT_HLIST_HEAD(&blkcg->blkg_list);
309 return &blkcg->css;
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.
318 static int blkiocg_can_attach(struct cgroup_subsys *subsys,
319 struct cgroup *cgroup, struct task_struct *tsk,
320 bool threadgroup)
322 struct io_context *ioc;
323 int ret = 0;
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);
332 return ret;
335 static void blkiocg_attach(struct cgroup_subsys *subsys, struct cgroup *cgroup,
336 struct cgroup *prev, struct task_struct *tsk,
337 bool threadgroup)
339 struct io_context *ioc;
341 task_lock(tsk);
342 ioc = tsk->io_context;
343 if (ioc)
344 ioc->cgroup_changed = 1;
345 task_unlock(tsk);
348 void blkio_policy_register(struct blkio_policy_type *blkiop)
350 spin_lock(&blkio_list_lock);
351 list_add_tail(&blkiop->list, &blkio_list);
352 spin_unlock(&blkio_list_lock);
354 EXPORT_SYMBOL_GPL(blkio_policy_register);
356 void blkio_policy_unregister(struct blkio_policy_type *blkiop)
358 spin_lock(&blkio_list_lock);
359 list_del_init(&blkiop->list);
360 spin_unlock(&blkio_list_lock);
362 EXPORT_SYMBOL_GPL(blkio_policy_unregister);
364 static int __init init_cgroup_blkio(void)
366 return cgroup_load_subsys(&blkio_subsys);
369 static void __exit exit_cgroup_blkio(void)
371 cgroup_unload_subsys(&blkio_subsys);
374 module_init(init_cgroup_blkio);
375 module_exit(exit_cgroup_blkio);
376 MODULE_LICENSE("GPL");