blkio: Remove per-cfqq nr_sectors as we'll be passing
[linux-2.6.git] / block / blk-cgroup.c
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>
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"
19
20 static DEFINE_SPINLOCK(blkio_list_lock);
21 static LIST_HEAD(blkio_list);
22
23 struct blkio_cgroup blkio_root_cgroup = { .weight = 2*BLKIO_WEIGHT_DEFAULT };
24 EXPORT_SYMBOL_GPL(blkio_root_cgroup);
25
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 *);
34
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,
48 };
49 EXPORT_SYMBOL_GPL(blkio_subsys);
50
51 struct 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 }
56 EXPORT_SYMBOL_GPL(cgroup_to_blkio_cgroup);
57
58 void blkiocg_update_blkio_group_stats(struct blkio_group *blkg,
59                                                 unsigned long time)
60 {
61         blkg->time += time;
62 }
63 EXPORT_SYMBOL_GPL(blkiocg_update_blkio_group_stats);
64
65 void blkiocg_add_blkio_group(struct blkio_cgroup *blkcg,
66                         struct blkio_group *blkg, void *key, dev_t dev)
67 {
68         unsigned long flags;
69
70         spin_lock_irqsave(&blkcg->lock, flags);
71         rcu_assign_pointer(blkg->key, key);
72         blkg->blkcg_id = css_id(&blkcg->css);
73         hlist_add_head_rcu(&blkg->blkcg_node, &blkcg->blkg_list);
74         spin_unlock_irqrestore(&blkcg->lock, flags);
75 #ifdef CONFIG_DEBUG_BLK_CGROUP
76         /* Need to take css reference ? */
77         cgroup_path(blkcg->css.cgroup, blkg->path, sizeof(blkg->path));
78 #endif
79         blkg->dev = dev;
80 }
81 EXPORT_SYMBOL_GPL(blkiocg_add_blkio_group);
82
83 static void __blkiocg_del_blkio_group(struct blkio_group *blkg)
84 {
85         hlist_del_init_rcu(&blkg->blkcg_node);
86         blkg->blkcg_id = 0;
87 }
88
89 /*
90  * returns 0 if blkio_group was still on cgroup list. Otherwise returns 1
91  * indicating that blk_group was unhashed by the time we got to it.
92  */
93 int blkiocg_del_blkio_group(struct blkio_group *blkg)
94 {
95         struct blkio_cgroup *blkcg;
96         unsigned long flags;
97         struct cgroup_subsys_state *css;
98         int ret = 1;
99
100         rcu_read_lock();
101         css = css_lookup(&blkio_subsys, blkg->blkcg_id);
102         if (!css)
103                 goto out;
104
105         blkcg = container_of(css, struct blkio_cgroup, css);
106         spin_lock_irqsave(&blkcg->lock, flags);
107         if (!hlist_unhashed(&blkg->blkcg_node)) {
108                 __blkiocg_del_blkio_group(blkg);
109                 ret = 0;
110         }
111         spin_unlock_irqrestore(&blkcg->lock, flags);
112 out:
113         rcu_read_unlock();
114         return ret;
115 }
116 EXPORT_SYMBOL_GPL(blkiocg_del_blkio_group);
117
118 /* called under rcu_read_lock(). */
119 struct blkio_group *blkiocg_lookup_group(struct blkio_cgroup *blkcg, void *key)
120 {
121         struct blkio_group *blkg;
122         struct hlist_node *n;
123         void *__key;
124
125         hlist_for_each_entry_rcu(blkg, n, &blkcg->blkg_list, blkcg_node) {
126                 __key = blkg->key;
127                 if (__key == key)
128                         return blkg;
129         }
130
131         return NULL;
132 }
133 EXPORT_SYMBOL_GPL(blkiocg_lookup_group);
134
135 #define SHOW_FUNCTION(__VAR)                                            \
136 static u64 blkiocg_##__VAR##_read(struct cgroup *cgroup,                \
137                                        struct cftype *cftype)           \
138 {                                                                       \
139         struct blkio_cgroup *blkcg;                                     \
140                                                                         \
141         blkcg = cgroup_to_blkio_cgroup(cgroup);                         \
142         return (u64)blkcg->__VAR;                                       \
143 }
144
145 SHOW_FUNCTION(weight);
146 #undef SHOW_FUNCTION
147
148 static int
149 blkiocg_weight_write(struct cgroup *cgroup, struct cftype *cftype, u64 val)
150 {
151         struct blkio_cgroup *blkcg;
152         struct blkio_group *blkg;
153         struct hlist_node *n;
154         struct blkio_policy_type *blkiop;
155
156         if (val < BLKIO_WEIGHT_MIN || val > BLKIO_WEIGHT_MAX)
157                 return -EINVAL;
158
159         blkcg = cgroup_to_blkio_cgroup(cgroup);
160         spin_lock(&blkio_list_lock);
161         spin_lock_irq(&blkcg->lock);
162         blkcg->weight = (unsigned int)val;
163         hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) {
164                 list_for_each_entry(blkiop, &blkio_list, list)
165                         blkiop->ops.blkio_update_group_weight_fn(blkg,
166                                         blkcg->weight);
167         }
168         spin_unlock_irq(&blkcg->lock);
169         spin_unlock(&blkio_list_lock);
170         return 0;
171 }
172
173 #define SHOW_FUNCTION_PER_GROUP(__VAR)                                  \
174 static int blkiocg_##__VAR##_read(struct cgroup *cgroup,                \
175                         struct cftype *cftype, struct seq_file *m)      \
176 {                                                                       \
177         struct blkio_cgroup *blkcg;                                     \
178         struct blkio_group *blkg;                                       \
179         struct hlist_node *n;                                           \
180                                                                         \
181         if (!cgroup_lock_live_group(cgroup))                            \
182                 return -ENODEV;                                         \
183                                                                         \
184         blkcg = cgroup_to_blkio_cgroup(cgroup);                         \
185         rcu_read_lock();                                                \
186         hlist_for_each_entry_rcu(blkg, n, &blkcg->blkg_list, blkcg_node) {\
187                 if (blkg->dev)                                          \
188                         seq_printf(m, "%u:%u %lu\n", MAJOR(blkg->dev),  \
189                                  MINOR(blkg->dev), blkg->__VAR);        \
190         }                                                               \
191         rcu_read_unlock();                                              \
192         cgroup_unlock();                                                \
193         return 0;                                                       \
194 }
195
196 SHOW_FUNCTION_PER_GROUP(time);
197 SHOW_FUNCTION_PER_GROUP(sectors);
198 #ifdef CONFIG_DEBUG_BLK_CGROUP
199 SHOW_FUNCTION_PER_GROUP(dequeue);
200 #endif
201 #undef SHOW_FUNCTION_PER_GROUP
202
203 #ifdef CONFIG_DEBUG_BLK_CGROUP
204 void blkiocg_update_blkio_group_dequeue_stats(struct blkio_group *blkg,
205                         unsigned long dequeue)
206 {
207         blkg->dequeue += dequeue;
208 }
209 EXPORT_SYMBOL_GPL(blkiocg_update_blkio_group_dequeue_stats);
210 #endif
211
212 struct cftype blkio_files[] = {
213         {
214                 .name = "weight",
215                 .read_u64 = blkiocg_weight_read,
216                 .write_u64 = blkiocg_weight_write,
217         },
218         {
219                 .name = "time",
220                 .read_seq_string = blkiocg_time_read,
221         },
222         {
223                 .name = "sectors",
224                 .read_seq_string = blkiocg_sectors_read,
225         },
226 #ifdef CONFIG_DEBUG_BLK_CGROUP
227        {
228                 .name = "dequeue",
229                 .read_seq_string = blkiocg_dequeue_read,
230        },
231 #endif
232 };
233
234 static int blkiocg_populate(struct cgroup_subsys *subsys, struct cgroup *cgroup)
235 {
236         return cgroup_add_files(cgroup, subsys, blkio_files,
237                                 ARRAY_SIZE(blkio_files));
238 }
239
240 static void blkiocg_destroy(struct cgroup_subsys *subsys, struct cgroup *cgroup)
241 {
242         struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgroup);
243         unsigned long flags;
244         struct blkio_group *blkg;
245         void *key;
246         struct blkio_policy_type *blkiop;
247
248         rcu_read_lock();
249 remove_entry:
250         spin_lock_irqsave(&blkcg->lock, flags);
251
252         if (hlist_empty(&blkcg->blkg_list)) {
253                 spin_unlock_irqrestore(&blkcg->lock, flags);
254                 goto done;
255         }
256
257         blkg = hlist_entry(blkcg->blkg_list.first, struct blkio_group,
258                                 blkcg_node);
259         key = rcu_dereference(blkg->key);
260         __blkiocg_del_blkio_group(blkg);
261
262         spin_unlock_irqrestore(&blkcg->lock, flags);
263
264         /*
265          * This blkio_group is being unlinked as associated cgroup is going
266          * away. Let all the IO controlling policies know about this event.
267          *
268          * Currently this is static call to one io controlling policy. Once
269          * we have more policies in place, we need some dynamic registration
270          * of callback function.
271          */
272         spin_lock(&blkio_list_lock);
273         list_for_each_entry(blkiop, &blkio_list, list)
274                 blkiop->ops.blkio_unlink_group_fn(key, blkg);
275         spin_unlock(&blkio_list_lock);
276         goto remove_entry;
277 done:
278         free_css_id(&blkio_subsys, &blkcg->css);
279         rcu_read_unlock();
280         if (blkcg != &blkio_root_cgroup)
281                 kfree(blkcg);
282 }
283
284 static struct cgroup_subsys_state *
285 blkiocg_create(struct cgroup_subsys *subsys, struct cgroup *cgroup)
286 {
287         struct blkio_cgroup *blkcg, *parent_blkcg;
288
289         if (!cgroup->parent) {
290                 blkcg = &blkio_root_cgroup;
291                 goto done;
292         }
293
294         /* Currently we do not support hierarchy deeper than two level (0,1) */
295         parent_blkcg = cgroup_to_blkio_cgroup(cgroup->parent);
296         if (css_depth(&parent_blkcg->css) > 0)
297                 return ERR_PTR(-EINVAL);
298
299         blkcg = kzalloc(sizeof(*blkcg), GFP_KERNEL);
300         if (!blkcg)
301                 return ERR_PTR(-ENOMEM);
302
303         blkcg->weight = BLKIO_WEIGHT_DEFAULT;
304 done:
305         spin_lock_init(&blkcg->lock);
306         INIT_HLIST_HEAD(&blkcg->blkg_list);
307
308         return &blkcg->css;
309 }
310
311 /*
312  * We cannot support shared io contexts, as we have no mean to support
313  * two tasks with the same ioc in two different groups without major rework
314  * of the main cic data structures.  For now we allow a task to change
315  * its cgroup only if it's the only owner of its ioc.
316  */
317 static int blkiocg_can_attach(struct cgroup_subsys *subsys,
318                                 struct cgroup *cgroup, struct task_struct *tsk,
319                                 bool threadgroup)
320 {
321         struct io_context *ioc;
322         int ret = 0;
323
324         /* task_lock() is needed to avoid races with exit_io_context() */
325         task_lock(tsk);
326         ioc = tsk->io_context;
327         if (ioc && atomic_read(&ioc->nr_tasks) > 1)
328                 ret = -EINVAL;
329         task_unlock(tsk);
330
331         return ret;
332 }
333
334 static void blkiocg_attach(struct cgroup_subsys *subsys, struct cgroup *cgroup,
335                                 struct cgroup *prev, struct task_struct *tsk,
336                                 bool threadgroup)
337 {
338         struct io_context *ioc;
339
340         task_lock(tsk);
341         ioc = tsk->io_context;
342         if (ioc)
343                 ioc->cgroup_changed = 1;
344         task_unlock(tsk);
345 }
346
347 void blkio_policy_register(struct blkio_policy_type *blkiop)
348 {
349         spin_lock(&blkio_list_lock);
350         list_add_tail(&blkiop->list, &blkio_list);
351         spin_unlock(&blkio_list_lock);
352 }
353 EXPORT_SYMBOL_GPL(blkio_policy_register);
354
355 void blkio_policy_unregister(struct blkio_policy_type *blkiop)
356 {
357         spin_lock(&blkio_list_lock);
358         list_del_init(&blkiop->list);
359         spin_unlock(&blkio_list_lock);
360 }
361 EXPORT_SYMBOL_GPL(blkio_policy_unregister);
362
363 static int __init init_cgroup_blkio(void)
364 {
365         return cgroup_load_subsys(&blkio_subsys);
366 }
367
368 static void __exit exit_cgroup_blkio(void)
369 {
370         cgroup_unload_subsys(&blkio_subsys);
371 }
372
373 module_init(init_cgroup_blkio);
374 module_exit(exit_cgroup_blkio);
375 MODULE_LICENSE("GPL");