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 <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"
20 static DEFINE_SPINLOCK(blkio_list_lock
);
21 static LIST_HEAD(blkio_list
);
23 struct blkio_cgroup blkio_root_cgroup
= { .weight
= 2*BLKIO_WEIGHT_DEFAULT
};
24 EXPORT_SYMBOL_GPL(blkio_root_cgroup
);
26 struct blkio_cgroup
*cgroup_to_blkio_cgroup(struct cgroup
*cgroup
)
28 return container_of(cgroup_subsys_state(cgroup
, blkio_subsys_id
),
29 struct blkio_cgroup
, css
);
31 EXPORT_SYMBOL_GPL(cgroup_to_blkio_cgroup
);
33 void blkiocg_update_blkio_group_stats(struct blkio_group
*blkg
,
34 unsigned long time
, unsigned long sectors
)
37 blkg
->sectors
+= sectors
;
39 EXPORT_SYMBOL_GPL(blkiocg_update_blkio_group_stats
);
41 void blkiocg_add_blkio_group(struct blkio_cgroup
*blkcg
,
42 struct blkio_group
*blkg
, void *key
, dev_t dev
)
46 spin_lock_irqsave(&blkcg
->lock
, flags
);
47 rcu_assign_pointer(blkg
->key
, key
);
48 blkg
->blkcg_id
= css_id(&blkcg
->css
);
49 hlist_add_head_rcu(&blkg
->blkcg_node
, &blkcg
->blkg_list
);
50 spin_unlock_irqrestore(&blkcg
->lock
, flags
);
51 #ifdef CONFIG_DEBUG_BLK_CGROUP
52 /* Need to take css reference ? */
53 cgroup_path(blkcg
->css
.cgroup
, blkg
->path
, sizeof(blkg
->path
));
57 EXPORT_SYMBOL_GPL(blkiocg_add_blkio_group
);
59 static void __blkiocg_del_blkio_group(struct blkio_group
*blkg
)
61 hlist_del_init_rcu(&blkg
->blkcg_node
);
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.
69 int blkiocg_del_blkio_group(struct blkio_group
*blkg
)
71 struct blkio_cgroup
*blkcg
;
73 struct cgroup_subsys_state
*css
;
77 css
= css_lookup(&blkio_subsys
, blkg
->blkcg_id
);
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
);
87 spin_unlock_irqrestore(&blkcg
->lock
, flags
);
92 EXPORT_SYMBOL_GPL(blkiocg_del_blkio_group
);
94 /* called under rcu_read_lock(). */
95 struct blkio_group
*blkiocg_lookup_group(struct blkio_cgroup
*blkcg
, void *key
)
97 struct blkio_group
*blkg
;
101 hlist_for_each_entry_rcu(blkg
, n
, &blkcg
->blkg_list
, blkcg_node
) {
109 EXPORT_SYMBOL_GPL(blkiocg_lookup_group
);
111 #define SHOW_FUNCTION(__VAR) \
112 static u64 blkiocg_##__VAR##_read(struct cgroup *cgroup, \
113 struct cftype *cftype) \
115 struct blkio_cgroup *blkcg; \
117 blkcg = cgroup_to_blkio_cgroup(cgroup); \
118 return (u64)blkcg->__VAR; \
121 SHOW_FUNCTION(weight
);
125 blkiocg_weight_write(struct cgroup
*cgroup
, struct cftype
*cftype
, u64 val
)
127 struct blkio_cgroup
*blkcg
;
128 struct blkio_group
*blkg
;
129 struct hlist_node
*n
;
130 struct blkio_policy_type
*blkiop
;
132 if (val
< BLKIO_WEIGHT_MIN
|| val
> BLKIO_WEIGHT_MAX
)
135 blkcg
= cgroup_to_blkio_cgroup(cgroup
);
136 spin_lock(&blkio_list_lock
);
137 spin_lock_irq(&blkcg
->lock
);
138 blkcg
->weight
= (unsigned int)val
;
139 hlist_for_each_entry(blkg
, n
, &blkcg
->blkg_list
, blkcg_node
) {
140 list_for_each_entry(blkiop
, &blkio_list
, list
)
141 blkiop
->ops
.blkio_update_group_weight_fn(blkg
,
144 spin_unlock_irq(&blkcg
->lock
);
145 spin_unlock(&blkio_list_lock
);
149 #define SHOW_FUNCTION_PER_GROUP(__VAR) \
150 static int blkiocg_##__VAR##_read(struct cgroup *cgroup, \
151 struct cftype *cftype, struct seq_file *m) \
153 struct blkio_cgroup *blkcg; \
154 struct blkio_group *blkg; \
155 struct hlist_node *n; \
157 if (!cgroup_lock_live_group(cgroup)) \
160 blkcg = cgroup_to_blkio_cgroup(cgroup); \
162 hlist_for_each_entry_rcu(blkg, n, &blkcg->blkg_list, blkcg_node) {\
164 seq_printf(m, "%u:%u %lu\n", MAJOR(blkg->dev), \
165 MINOR(blkg->dev), blkg->__VAR); \
172 SHOW_FUNCTION_PER_GROUP(time
);
173 SHOW_FUNCTION_PER_GROUP(sectors
);
174 #ifdef CONFIG_DEBUG_BLK_CGROUP
175 SHOW_FUNCTION_PER_GROUP(dequeue
);
177 #undef SHOW_FUNCTION_PER_GROUP
179 #ifdef CONFIG_DEBUG_BLK_CGROUP
180 void blkiocg_update_blkio_group_dequeue_stats(struct blkio_group
*blkg
,
181 unsigned long dequeue
)
183 blkg
->dequeue
+= dequeue
;
185 EXPORT_SYMBOL_GPL(blkiocg_update_blkio_group_dequeue_stats
);
188 struct cftype blkio_files
[] = {
191 .read_u64
= blkiocg_weight_read
,
192 .write_u64
= blkiocg_weight_write
,
196 .read_seq_string
= blkiocg_time_read
,
200 .read_seq_string
= blkiocg_sectors_read
,
202 #ifdef CONFIG_DEBUG_BLK_CGROUP
205 .read_seq_string
= blkiocg_dequeue_read
,
210 static int blkiocg_populate(struct cgroup_subsys
*subsys
, struct cgroup
*cgroup
)
212 return cgroup_add_files(cgroup
, subsys
, blkio_files
,
213 ARRAY_SIZE(blkio_files
));
216 static void blkiocg_destroy(struct cgroup_subsys
*subsys
, struct cgroup
*cgroup
)
218 struct blkio_cgroup
*blkcg
= cgroup_to_blkio_cgroup(cgroup
);
220 struct blkio_group
*blkg
;
222 struct blkio_policy_type
*blkiop
;
226 spin_lock_irqsave(&blkcg
->lock
, flags
);
228 if (hlist_empty(&blkcg
->blkg_list
)) {
229 spin_unlock_irqrestore(&blkcg
->lock
, flags
);
233 blkg
= hlist_entry(blkcg
->blkg_list
.first
, struct blkio_group
,
235 key
= rcu_dereference(blkg
->key
);
236 __blkiocg_del_blkio_group(blkg
);
238 spin_unlock_irqrestore(&blkcg
->lock
, flags
);
241 * This blkio_group is being unlinked as associated cgroup is going
242 * away. Let all the IO controlling policies know about this event.
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.
248 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
);
254 free_css_id(&blkio_subsys
, &blkcg
->css
);
259 static struct cgroup_subsys_state
*
260 blkiocg_create(struct cgroup_subsys
*subsys
, struct cgroup
*cgroup
)
262 struct blkio_cgroup
*blkcg
, *parent_blkcg
;
264 if (!cgroup
->parent
) {
265 blkcg
= &blkio_root_cgroup
;
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
);
274 blkcg
= kzalloc(sizeof(*blkcg
), GFP_KERNEL
);
276 return ERR_PTR(-ENOMEM
);
278 blkcg
->weight
= BLKIO_WEIGHT_DEFAULT
;
280 spin_lock_init(&blkcg
->lock
);
281 INIT_HLIST_HEAD(&blkcg
->blkg_list
);
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.
292 static int blkiocg_can_attach(struct cgroup_subsys
*subsys
,
293 struct cgroup
*cgroup
, struct task_struct
*tsk
,
296 struct io_context
*ioc
;
299 /* task_lock() is needed to avoid races with exit_io_context() */
301 ioc
= tsk
->io_context
;
302 if (ioc
&& atomic_read(&ioc
->nr_tasks
) > 1)
309 static void blkiocg_attach(struct cgroup_subsys
*subsys
, struct cgroup
*cgroup
,
310 struct cgroup
*prev
, struct task_struct
*tsk
,
313 struct io_context
*ioc
;
316 ioc
= tsk
->io_context
;
318 ioc
->cgroup_changed
= 1;
322 struct cgroup_subsys blkio_subsys
= {
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
,
333 void blkio_policy_register(struct blkio_policy_type
*blkiop
)
335 spin_lock(&blkio_list_lock
);
336 list_add_tail(&blkiop
->list
, &blkio_list
);
337 spin_unlock(&blkio_list_lock
);
339 EXPORT_SYMBOL_GPL(blkio_policy_register
);
341 void blkio_policy_unregister(struct blkio_policy_type
*blkiop
)
343 spin_lock(&blkio_list_lock
);
344 list_del_init(&blkiop
->list
);
345 spin_unlock(&blkio_list_lock
);
347 EXPORT_SYMBOL_GPL(blkio_policy_unregister
);