blob: c85d74cae200275b168f5291770073ad3540347f [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
Vivek Goyal31e4c282009-12-03 12:59:42 -050026struct blkio_cgroup *cgroup_to_blkio_cgroup(struct cgroup *cgroup)
27{
28 return container_of(cgroup_subsys_state(cgroup, blkio_subsys_id),
29 struct blkio_cgroup, css);
30}
Vivek Goyal9d6a9862009-12-04 10:36:41 -050031EXPORT_SYMBOL_GPL(cgroup_to_blkio_cgroup);
Vivek Goyal31e4c282009-12-03 12:59:42 -050032
Vivek Goyal22084192009-12-03 12:59:49 -050033void blkiocg_update_blkio_group_stats(struct blkio_group *blkg,
34 unsigned long time, unsigned long sectors)
35{
36 blkg->time += time;
37 blkg->sectors += sectors;
38}
Vivek Goyal9d6a9862009-12-04 10:36:41 -050039EXPORT_SYMBOL_GPL(blkiocg_update_blkio_group_stats);
Vivek Goyal22084192009-12-03 12:59:49 -050040
Vivek Goyal31e4c282009-12-03 12:59:42 -050041void blkiocg_add_blkio_group(struct blkio_cgroup *blkcg,
Vivek Goyal22084192009-12-03 12:59:49 -050042 struct blkio_group *blkg, void *key, dev_t dev)
Vivek Goyal31e4c282009-12-03 12:59:42 -050043{
44 unsigned long flags;
45
46 spin_lock_irqsave(&blkcg->lock, flags);
47 rcu_assign_pointer(blkg->key, key);
Vivek Goyalb1c35762009-12-03 12:59:47 -050048 blkg->blkcg_id = css_id(&blkcg->css);
Vivek Goyal31e4c282009-12-03 12:59:42 -050049 hlist_add_head_rcu(&blkg->blkcg_node, &blkcg->blkg_list);
50 spin_unlock_irqrestore(&blkcg->lock, flags);
Vivek Goyal2868ef72009-12-03 12:59:48 -050051#ifdef CONFIG_DEBUG_BLK_CGROUP
52 /* Need to take css reference ? */
53 cgroup_path(blkcg->css.cgroup, blkg->path, sizeof(blkg->path));
54#endif
Vivek Goyal22084192009-12-03 12:59:49 -050055 blkg->dev = dev;
Vivek Goyal31e4c282009-12-03 12:59:42 -050056}
Vivek Goyal9d6a9862009-12-04 10:36:41 -050057EXPORT_SYMBOL_GPL(blkiocg_add_blkio_group);
Vivek Goyal31e4c282009-12-03 12:59:42 -050058
Vivek Goyalb1c35762009-12-03 12:59:47 -050059static void __blkiocg_del_blkio_group(struct blkio_group *blkg)
60{
61 hlist_del_init_rcu(&blkg->blkcg_node);
62 blkg->blkcg_id = 0;
63}
64
65/*
66 * returns 0 if blkio_group was still on cgroup list. Otherwise returns 1
67 * indicating that blk_group was unhashed by the time we got to it.
68 */
Vivek Goyal31e4c282009-12-03 12:59:42 -050069int blkiocg_del_blkio_group(struct blkio_group *blkg)
70{
Vivek Goyalb1c35762009-12-03 12:59:47 -050071 struct blkio_cgroup *blkcg;
72 unsigned long flags;
73 struct cgroup_subsys_state *css;
74 int ret = 1;
75
76 rcu_read_lock();
77 css = css_lookup(&blkio_subsys, blkg->blkcg_id);
78 if (!css)
79 goto out;
80
81 blkcg = container_of(css, struct blkio_cgroup, css);
82 spin_lock_irqsave(&blkcg->lock, flags);
83 if (!hlist_unhashed(&blkg->blkcg_node)) {
84 __blkiocg_del_blkio_group(blkg);
85 ret = 0;
86 }
87 spin_unlock_irqrestore(&blkcg->lock, flags);
88out:
89 rcu_read_unlock();
90 return ret;
Vivek Goyal31e4c282009-12-03 12:59:42 -050091}
Vivek Goyal9d6a9862009-12-04 10:36:41 -050092EXPORT_SYMBOL_GPL(blkiocg_del_blkio_group);
Vivek Goyal31e4c282009-12-03 12:59:42 -050093
94/* called under rcu_read_lock(). */
95struct blkio_group *blkiocg_lookup_group(struct blkio_cgroup *blkcg, void *key)
96{
97 struct blkio_group *blkg;
98 struct hlist_node *n;
99 void *__key;
100
101 hlist_for_each_entry_rcu(blkg, n, &blkcg->blkg_list, blkcg_node) {
102 __key = blkg->key;
103 if (__key == key)
104 return blkg;
105 }
106
107 return NULL;
108}
Vivek Goyal9d6a9862009-12-04 10:36:41 -0500109EXPORT_SYMBOL_GPL(blkiocg_lookup_group);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500110
111#define SHOW_FUNCTION(__VAR) \
112static u64 blkiocg_##__VAR##_read(struct cgroup *cgroup, \
113 struct cftype *cftype) \
114{ \
115 struct blkio_cgroup *blkcg; \
116 \
117 blkcg = cgroup_to_blkio_cgroup(cgroup); \
118 return (u64)blkcg->__VAR; \
119}
120
121SHOW_FUNCTION(weight);
122#undef SHOW_FUNCTION
123
124static int
125blkiocg_weight_write(struct cgroup *cgroup, struct cftype *cftype, u64 val)
126{
127 struct blkio_cgroup *blkcg;
Vivek Goyalf8d461d2009-12-03 12:59:52 -0500128 struct blkio_group *blkg;
129 struct hlist_node *n;
Vivek Goyal3e252062009-12-04 10:36:42 -0500130 struct blkio_policy_type *blkiop;
Vivek Goyal31e4c282009-12-03 12:59:42 -0500131
132 if (val < BLKIO_WEIGHT_MIN || val > BLKIO_WEIGHT_MAX)
133 return -EINVAL;
134
135 blkcg = cgroup_to_blkio_cgroup(cgroup);
Gui Jianfengbcf4dd42010-02-01 09:58:54 +0100136 spin_lock(&blkio_list_lock);
Vivek Goyalf8d461d2009-12-03 12:59:52 -0500137 spin_lock_irq(&blkcg->lock);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500138 blkcg->weight = (unsigned int)val;
Vivek Goyal3e252062009-12-04 10:36:42 -0500139 hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) {
Vivek Goyal3e252062009-12-04 10:36:42 -0500140 list_for_each_entry(blkiop, &blkio_list, list)
141 blkiop->ops.blkio_update_group_weight_fn(blkg,
142 blkcg->weight);
Vivek Goyal3e252062009-12-04 10:36:42 -0500143 }
Vivek Goyalf8d461d2009-12-03 12:59:52 -0500144 spin_unlock_irq(&blkcg->lock);
Gui Jianfengbcf4dd42010-02-01 09:58:54 +0100145 spin_unlock(&blkio_list_lock);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500146 return 0;
147}
148
Vivek Goyal22084192009-12-03 12:59:49 -0500149#define SHOW_FUNCTION_PER_GROUP(__VAR) \
150static int blkiocg_##__VAR##_read(struct cgroup *cgroup, \
151 struct cftype *cftype, struct seq_file *m) \
152{ \
153 struct blkio_cgroup *blkcg; \
154 struct blkio_group *blkg; \
155 struct hlist_node *n; \
156 \
157 if (!cgroup_lock_live_group(cgroup)) \
158 return -ENODEV; \
159 \
160 blkcg = cgroup_to_blkio_cgroup(cgroup); \
161 rcu_read_lock(); \
162 hlist_for_each_entry_rcu(blkg, n, &blkcg->blkg_list, blkcg_node) {\
163 if (blkg->dev) \
164 seq_printf(m, "%u:%u %lu\n", MAJOR(blkg->dev), \
165 MINOR(blkg->dev), blkg->__VAR); \
166 } \
167 rcu_read_unlock(); \
168 cgroup_unlock(); \
169 return 0; \
170}
171
172SHOW_FUNCTION_PER_GROUP(time);
173SHOW_FUNCTION_PER_GROUP(sectors);
174#ifdef CONFIG_DEBUG_BLK_CGROUP
175SHOW_FUNCTION_PER_GROUP(dequeue);
176#endif
177#undef SHOW_FUNCTION_PER_GROUP
178
179#ifdef CONFIG_DEBUG_BLK_CGROUP
180void blkiocg_update_blkio_group_dequeue_stats(struct blkio_group *blkg,
181 unsigned long dequeue)
182{
183 blkg->dequeue += dequeue;
184}
Vivek Goyal9d6a9862009-12-04 10:36:41 -0500185EXPORT_SYMBOL_GPL(blkiocg_update_blkio_group_dequeue_stats);
Vivek Goyal22084192009-12-03 12:59:49 -0500186#endif
187
Vivek Goyal31e4c282009-12-03 12:59:42 -0500188struct cftype blkio_files[] = {
189 {
190 .name = "weight",
191 .read_u64 = blkiocg_weight_read,
192 .write_u64 = blkiocg_weight_write,
193 },
Vivek Goyal22084192009-12-03 12:59:49 -0500194 {
195 .name = "time",
196 .read_seq_string = blkiocg_time_read,
197 },
198 {
199 .name = "sectors",
200 .read_seq_string = blkiocg_sectors_read,
201 },
202#ifdef CONFIG_DEBUG_BLK_CGROUP
203 {
204 .name = "dequeue",
205 .read_seq_string = blkiocg_dequeue_read,
206 },
207#endif
Vivek Goyal31e4c282009-12-03 12:59:42 -0500208};
209
210static int blkiocg_populate(struct cgroup_subsys *subsys, struct cgroup *cgroup)
211{
212 return cgroup_add_files(cgroup, subsys, blkio_files,
213 ARRAY_SIZE(blkio_files));
214}
215
216static void blkiocg_destroy(struct cgroup_subsys *subsys, struct cgroup *cgroup)
217{
218 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgroup);
Vivek Goyalb1c35762009-12-03 12:59:47 -0500219 unsigned long flags;
220 struct blkio_group *blkg;
221 void *key;
Vivek Goyal3e252062009-12-04 10:36:42 -0500222 struct blkio_policy_type *blkiop;
Vivek Goyal31e4c282009-12-03 12:59:42 -0500223
Vivek Goyalb1c35762009-12-03 12:59:47 -0500224 rcu_read_lock();
225remove_entry:
226 spin_lock_irqsave(&blkcg->lock, flags);
227
228 if (hlist_empty(&blkcg->blkg_list)) {
229 spin_unlock_irqrestore(&blkcg->lock, flags);
230 goto done;
231 }
232
233 blkg = hlist_entry(blkcg->blkg_list.first, struct blkio_group,
234 blkcg_node);
235 key = rcu_dereference(blkg->key);
236 __blkiocg_del_blkio_group(blkg);
237
238 spin_unlock_irqrestore(&blkcg->lock, flags);
239
240 /*
241 * This blkio_group is being unlinked as associated cgroup is going
242 * away. Let all the IO controlling policies know about this event.
243 *
244 * Currently this is static call to one io controlling policy. Once
245 * we have more policies in place, we need some dynamic registration
246 * of callback function.
247 */
Vivek Goyal3e252062009-12-04 10:36:42 -0500248 spin_lock(&blkio_list_lock);
249 list_for_each_entry(blkiop, &blkio_list, list)
250 blkiop->ops.blkio_unlink_group_fn(key, blkg);
251 spin_unlock(&blkio_list_lock);
Vivek Goyalb1c35762009-12-03 12:59:47 -0500252 goto remove_entry;
253done:
Vivek Goyal31e4c282009-12-03 12:59:42 -0500254 free_css_id(&blkio_subsys, &blkcg->css);
Vivek Goyalb1c35762009-12-03 12:59:47 -0500255 rcu_read_unlock();
Vivek Goyal31e4c282009-12-03 12:59:42 -0500256 kfree(blkcg);
257}
258
259static struct cgroup_subsys_state *
260blkiocg_create(struct cgroup_subsys *subsys, struct cgroup *cgroup)
261{
262 struct blkio_cgroup *blkcg, *parent_blkcg;
263
264 if (!cgroup->parent) {
265 blkcg = &blkio_root_cgroup;
266 goto done;
267 }
268
269 /* Currently we do not support hierarchy deeper than two level (0,1) */
270 parent_blkcg = cgroup_to_blkio_cgroup(cgroup->parent);
271 if (css_depth(&parent_blkcg->css) > 0)
272 return ERR_PTR(-EINVAL);
273
274 blkcg = kzalloc(sizeof(*blkcg), GFP_KERNEL);
275 if (!blkcg)
276 return ERR_PTR(-ENOMEM);
277
278 blkcg->weight = BLKIO_WEIGHT_DEFAULT;
279done:
280 spin_lock_init(&blkcg->lock);
281 INIT_HLIST_HEAD(&blkcg->blkg_list);
282
283 return &blkcg->css;
284}
285
286/*
287 * We cannot support shared io contexts, as we have no mean to support
288 * two tasks with the same ioc in two different groups without major rework
289 * of the main cic data structures. For now we allow a task to change
290 * its cgroup only if it's the only owner of its ioc.
291 */
292static int blkiocg_can_attach(struct cgroup_subsys *subsys,
293 struct cgroup *cgroup, struct task_struct *tsk,
294 bool threadgroup)
295{
296 struct io_context *ioc;
297 int ret = 0;
298
299 /* task_lock() is needed to avoid races with exit_io_context() */
300 task_lock(tsk);
301 ioc = tsk->io_context;
302 if (ioc && atomic_read(&ioc->nr_tasks) > 1)
303 ret = -EINVAL;
304 task_unlock(tsk);
305
306 return ret;
307}
308
309static void blkiocg_attach(struct cgroup_subsys *subsys, struct cgroup *cgroup,
310 struct cgroup *prev, struct task_struct *tsk,
311 bool threadgroup)
312{
313 struct io_context *ioc;
314
315 task_lock(tsk);
316 ioc = tsk->io_context;
317 if (ioc)
318 ioc->cgroup_changed = 1;
319 task_unlock(tsk);
320}
321
322struct cgroup_subsys blkio_subsys = {
323 .name = "blkio",
324 .create = blkiocg_create,
325 .can_attach = blkiocg_can_attach,
326 .attach = blkiocg_attach,
327 .destroy = blkiocg_destroy,
328 .populate = blkiocg_populate,
329 .subsys_id = blkio_subsys_id,
330 .use_id = 1,
331};
Vivek Goyal3e252062009-12-04 10:36:42 -0500332
333void blkio_policy_register(struct blkio_policy_type *blkiop)
334{
335 spin_lock(&blkio_list_lock);
336 list_add_tail(&blkiop->list, &blkio_list);
337 spin_unlock(&blkio_list_lock);
338}
339EXPORT_SYMBOL_GPL(blkio_policy_register);
340
341void blkio_policy_unregister(struct blkio_policy_type *blkiop)
342{
343 spin_lock(&blkio_list_lock);
344 list_del_init(&blkiop->list);
345 spin_unlock(&blkio_list_lock);
346}
347EXPORT_SYMBOL_GPL(blkio_policy_unregister);