Merge branch 'irq-fixes-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git...
[sfrench/cifs-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, unsigned long sectors)
60 {
61         blkg->time += time;
62         blkg->sectors += sectors;
63 }
64 EXPORT_SYMBOL_GPL(blkiocg_update_blkio_group_stats);
65
66 void blkiocg_add_blkio_group(struct blkio_cgroup *blkcg,
67                         struct blkio_group *blkg, void *key, dev_t dev)
68 {
69         unsigned long flags;
70
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;
81 }
82 EXPORT_SYMBOL_GPL(blkiocg_add_blkio_group);
83
84 static 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  */
94 int blkiocg_del_blkio_group(struct blkio_group *blkg)
95 {
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);
113 out:
114         rcu_read_unlock();
115         return ret;
116 }
117 EXPORT_SYMBOL_GPL(blkiocg_del_blkio_group);
118
119 /* called under rcu_read_lock(). */
120 struct 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 }
134 EXPORT_SYMBOL_GPL(blkiocg_lookup_group);
135
136 #define SHOW_FUNCTION(__VAR)                                            \
137 static 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
146 SHOW_FUNCTION(weight);
147 #undef SHOW_FUNCTION
148
149 static int
150 blkiocg_weight_write(struct cgroup *cgroup, struct cftype *cftype, u64 val)
151 {
152         struct blkio_cgroup *blkcg;
153         struct blkio_group *blkg;
154         struct hlist_node *n;
155         struct blkio_policy_type *blkiop;
156
157         if (val < BLKIO_WEIGHT_MIN || val > BLKIO_WEIGHT_MAX)
158                 return -EINVAL;
159
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);
168         }
169         spin_unlock_irq(&blkcg->lock);
170         spin_unlock(&blkio_list_lock);
171         return 0;
172 }
173
174 #define SHOW_FUNCTION_PER_GROUP(__VAR)                                  \
175 static 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
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
203
204 #ifdef CONFIG_DEBUG_BLK_CGROUP
205 void blkiocg_update_blkio_group_dequeue_stats(struct blkio_group *blkg,
206                         unsigned long dequeue)
207 {
208         blkg->dequeue += dequeue;
209 }
210 EXPORT_SYMBOL_GPL(blkiocg_update_blkio_group_dequeue_stats);
211 #endif
212
213 struct cftype blkio_files[] = {
214         {
215                 .name = "weight",
216                 .read_u64 = blkiocg_weight_read,
217                 .write_u64 = blkiocg_weight_write,
218         },
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
233 };
234
235 static 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
241 static void blkiocg_destroy(struct cgroup_subsys *subsys, struct cgroup *cgroup)
242 {
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;
248
249         rcu_read_lock();
250 remove_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          */
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);
283 }
284
285 static struct cgroup_subsys_state *
286 blkiocg_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;
305 done:
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  */
318 static 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
335 static 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
348 void 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 }
354 EXPORT_SYMBOL_GPL(blkio_policy_register);
355
356 void 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 }
362 EXPORT_SYMBOL_GPL(blkio_policy_unregister);
363
364 static int __init init_cgroup_blkio(void)
365 {
366         return cgroup_load_subsys(&blkio_subsys);
367 }
368
369 static void __exit exit_cgroup_blkio(void)
370 {
371         cgroup_unload_subsys(&blkio_subsys);
372 }
373
374 module_init(init_cgroup_blkio);
375 module_exit(exit_cgroup_blkio);
376 MODULE_LICENSE("GPL");