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 "blk-cgroup.h"
16 extern void cfq_unlink_blkio_group(void *, struct blkio_group *);
18 struct blkio_cgroup blkio_root_cgroup = { .weight = 2*BLKIO_WEIGHT_DEFAULT };
20 struct blkio_cgroup *cgroup_to_blkio_cgroup(struct cgroup *cgroup)
22 return container_of(cgroup_subsys_state(cgroup, blkio_subsys_id),
23 struct blkio_cgroup, css);
26 void blkiocg_add_blkio_group(struct blkio_cgroup *blkcg,
27 struct blkio_group *blkg, void *key)
31 spin_lock_irqsave(&blkcg->lock, flags);
32 rcu_assign_pointer(blkg->key, key);
33 blkg->blkcg_id = css_id(&blkcg->css);
34 hlist_add_head_rcu(&blkg->blkcg_node, &blkcg->blkg_list);
35 spin_unlock_irqrestore(&blkcg->lock, flags);
38 static void __blkiocg_del_blkio_group(struct blkio_group *blkg)
40 hlist_del_init_rcu(&blkg->blkcg_node);
45 * returns 0 if blkio_group was still on cgroup list. Otherwise returns 1
46 * indicating that blk_group was unhashed by the time we got to it.
48 int blkiocg_del_blkio_group(struct blkio_group *blkg)
50 struct blkio_cgroup *blkcg;
52 struct cgroup_subsys_state *css;
56 css = css_lookup(&blkio_subsys, blkg->blkcg_id);
60 blkcg = container_of(css, struct blkio_cgroup, css);
61 spin_lock_irqsave(&blkcg->lock, flags);
62 if (!hlist_unhashed(&blkg->blkcg_node)) {
63 __blkiocg_del_blkio_group(blkg);
66 spin_unlock_irqrestore(&blkcg->lock, flags);
72 /* called under rcu_read_lock(). */
73 struct blkio_group *blkiocg_lookup_group(struct blkio_cgroup *blkcg, void *key)
75 struct blkio_group *blkg;
79 hlist_for_each_entry_rcu(blkg, n, &blkcg->blkg_list, blkcg_node) {
88 #define SHOW_FUNCTION(__VAR) \
89 static u64 blkiocg_##__VAR##_read(struct cgroup *cgroup, \
90 struct cftype *cftype) \
92 struct blkio_cgroup *blkcg; \
94 blkcg = cgroup_to_blkio_cgroup(cgroup); \
95 return (u64)blkcg->__VAR; \
98 SHOW_FUNCTION(weight);
102 blkiocg_weight_write(struct cgroup *cgroup, struct cftype *cftype, u64 val)
104 struct blkio_cgroup *blkcg;
106 if (val < BLKIO_WEIGHT_MIN || val > BLKIO_WEIGHT_MAX)
109 blkcg = cgroup_to_blkio_cgroup(cgroup);
110 blkcg->weight = (unsigned int)val;
114 struct cftype blkio_files[] = {
117 .read_u64 = blkiocg_weight_read,
118 .write_u64 = blkiocg_weight_write,
122 static int blkiocg_populate(struct cgroup_subsys *subsys, struct cgroup *cgroup)
124 return cgroup_add_files(cgroup, subsys, blkio_files,
125 ARRAY_SIZE(blkio_files));
128 static void blkiocg_destroy(struct cgroup_subsys *subsys, struct cgroup *cgroup)
130 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgroup);
132 struct blkio_group *blkg;
137 spin_lock_irqsave(&blkcg->lock, flags);
139 if (hlist_empty(&blkcg->blkg_list)) {
140 spin_unlock_irqrestore(&blkcg->lock, flags);
144 blkg = hlist_entry(blkcg->blkg_list.first, struct blkio_group,
146 key = rcu_dereference(blkg->key);
147 __blkiocg_del_blkio_group(blkg);
149 spin_unlock_irqrestore(&blkcg->lock, flags);
152 * This blkio_group is being unlinked as associated cgroup is going
153 * away. Let all the IO controlling policies know about this event.
155 * Currently this is static call to one io controlling policy. Once
156 * we have more policies in place, we need some dynamic registration
157 * of callback function.
159 cfq_unlink_blkio_group(key, blkg);
162 free_css_id(&blkio_subsys, &blkcg->css);
167 static struct cgroup_subsys_state *
168 blkiocg_create(struct cgroup_subsys *subsys, struct cgroup *cgroup)
170 struct blkio_cgroup *blkcg, *parent_blkcg;
172 if (!cgroup->parent) {
173 blkcg = &blkio_root_cgroup;
177 /* Currently we do not support hierarchy deeper than two level (0,1) */
178 parent_blkcg = cgroup_to_blkio_cgroup(cgroup->parent);
179 if (css_depth(&parent_blkcg->css) > 0)
180 return ERR_PTR(-EINVAL);
182 blkcg = kzalloc(sizeof(*blkcg), GFP_KERNEL);
184 return ERR_PTR(-ENOMEM);
186 blkcg->weight = BLKIO_WEIGHT_DEFAULT;
188 spin_lock_init(&blkcg->lock);
189 INIT_HLIST_HEAD(&blkcg->blkg_list);
195 * We cannot support shared io contexts, as we have no mean to support
196 * two tasks with the same ioc in two different groups without major rework
197 * of the main cic data structures. For now we allow a task to change
198 * its cgroup only if it's the only owner of its ioc.
200 static int blkiocg_can_attach(struct cgroup_subsys *subsys,
201 struct cgroup *cgroup, struct task_struct *tsk,
204 struct io_context *ioc;
207 /* task_lock() is needed to avoid races with exit_io_context() */
209 ioc = tsk->io_context;
210 if (ioc && atomic_read(&ioc->nr_tasks) > 1)
217 static void blkiocg_attach(struct cgroup_subsys *subsys, struct cgroup *cgroup,
218 struct cgroup *prev, struct task_struct *tsk,
221 struct io_context *ioc;
224 ioc = tsk->io_context;
226 ioc->cgroup_changed = 1;
230 struct cgroup_subsys blkio_subsys = {
232 .create = blkiocg_create,
233 .can_attach = blkiocg_can_attach,
234 .attach = blkiocg_attach,
235 .destroy = blkiocg_destroy,
236 .populate = blkiocg_populate,
237 .subsys_id = blkio_subsys_id,