]> bbs.cooldavid.org Git - net-next-2.6.git/blame - block/blk-cgroup.c
be2net: Adding PCI SRIOV support
[net-next-2.6.git] / block / blk-cgroup.c
CommitLineData
31e4c28d
VG
1/*
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>
22084190
VG
14#include <linux/seq_file.h>
15#include <linux/kdev_t.h>
9d6a986c 16#include <linux/module.h>
accee785 17#include <linux/err.h>
31e4c28d 18#include "blk-cgroup.h"
3e252066
VG
19
20static DEFINE_SPINLOCK(blkio_list_lock);
21static LIST_HEAD(blkio_list);
b1c35769 22
31e4c28d 23struct blkio_cgroup blkio_root_cgroup = { .weight = 2*BLKIO_WEIGHT_DEFAULT };
9d6a986c
VG
24EXPORT_SYMBOL_GPL(blkio_root_cgroup);
25
67523c48
BB
26static 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
31e4c28d
VG
51struct 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}
9d6a986c 56EXPORT_SYMBOL_GPL(cgroup_to_blkio_cgroup);
31e4c28d 57
22084190
VG
58void 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}
9d6a986c 64EXPORT_SYMBOL_GPL(blkiocg_update_blkio_group_stats);
22084190 65
31e4c28d 66void blkiocg_add_blkio_group(struct blkio_cgroup *blkcg,
22084190 67 struct blkio_group *blkg, void *key, dev_t dev)
31e4c28d
VG
68{
69 unsigned long flags;
70
71 spin_lock_irqsave(&blkcg->lock, flags);
72 rcu_assign_pointer(blkg->key, key);
b1c35769 73 blkg->blkcg_id = css_id(&blkcg->css);
31e4c28d
VG
74 hlist_add_head_rcu(&blkg->blkcg_node, &blkcg->blkg_list);
75 spin_unlock_irqrestore(&blkcg->lock, flags);
2868ef7b
VG
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
22084190 80 blkg->dev = dev;
31e4c28d 81}
9d6a986c 82EXPORT_SYMBOL_GPL(blkiocg_add_blkio_group);
31e4c28d 83
b1c35769
VG
84static 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 */
31e4c28d
VG
94int blkiocg_del_blkio_group(struct blkio_group *blkg)
95{
b1c35769
VG
96 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;
31e4c28d 116}
9d6a986c 117EXPORT_SYMBOL_GPL(blkiocg_del_blkio_group);
31e4c28d
VG
118
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}
9d6a986c 134EXPORT_SYMBOL_GPL(blkiocg_lookup_group);
31e4c28d
VG
135
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;
f8d461d6
VG
153 struct blkio_group *blkg;
154 struct hlist_node *n;
3e252066 155 struct blkio_policy_type *blkiop;
31e4c28d
VG
156
157 if (val < BLKIO_WEIGHT_MIN || val > BLKIO_WEIGHT_MAX)
158 return -EINVAL;
159
160 blkcg = cgroup_to_blkio_cgroup(cgroup);
bcf4dd43 161 spin_lock(&blkio_list_lock);
f8d461d6 162 spin_lock_irq(&blkcg->lock);
31e4c28d 163 blkcg->weight = (unsigned int)val;
3e252066 164 hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) {
3e252066
VG
165 list_for_each_entry(blkiop, &blkio_list, list)
166 blkiop->ops.blkio_update_group_weight_fn(blkg,
167 blkcg->weight);
3e252066 168 }
f8d461d6 169 spin_unlock_irq(&blkcg->lock);
bcf4dd43 170 spin_unlock(&blkio_list_lock);
31e4c28d
VG
171 return 0;
172}
173
22084190
VG
174#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}
9d6a986c 210EXPORT_SYMBOL_GPL(blkiocg_update_blkio_group_dequeue_stats);
22084190
VG
211#endif
212
31e4c28d
VG
213struct cftype blkio_files[] = {
214 {
215 .name = "weight",
216 .read_u64 = blkiocg_weight_read,
217 .write_u64 = blkiocg_weight_write,
218 },
22084190
VG
219 {
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
31e4c28d
VG
233};
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);
b1c35769
VG
244 unsigned long flags;
245 struct blkio_group *blkg;
246 void *key;
3e252066 247 struct blkio_policy_type *blkiop;
b1c35769
VG
248
249 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);
31e4c28d 262
b1c35769
VG
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 */
3e252066
VG
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);
b1c35769
VG
277 goto remove_entry;
278done:
31e4c28d 279 free_css_id(&blkio_subsys, &blkcg->css);
b1c35769 280 rcu_read_unlock();
67523c48
BB
281 if (blkcg != &blkio_root_cgroup)
282 kfree(blkcg);
31e4c28d
VG
283}
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
3e252066
VG
348void 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);
67523c48
BB
363
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");