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 bool blkiocg_css_tryget(struct blkio_cgroup
*blkcg
)
28 if (!css_tryget(&blkcg
->css
))
32 EXPORT_SYMBOL_GPL(blkiocg_css_tryget
);
34 void blkiocg_css_put(struct blkio_cgroup
*blkcg
)
38 EXPORT_SYMBOL_GPL(blkiocg_css_put
);
40 struct blkio_cgroup
*cgroup_to_blkio_cgroup(struct cgroup
*cgroup
)
42 return container_of(cgroup_subsys_state(cgroup
, blkio_subsys_id
),
43 struct blkio_cgroup
, css
);
45 EXPORT_SYMBOL_GPL(cgroup_to_blkio_cgroup
);
47 void blkiocg_update_blkio_group_stats(struct blkio_group
*blkg
,
48 unsigned long time
, unsigned long sectors
)
51 blkg
->sectors
+= sectors
;
53 EXPORT_SYMBOL_GPL(blkiocg_update_blkio_group_stats
);
55 void blkiocg_add_blkio_group(struct blkio_cgroup
*blkcg
,
56 struct blkio_group
*blkg
, void *key
, dev_t dev
)
60 spin_lock_irqsave(&blkcg
->lock
, flags
);
61 rcu_assign_pointer(blkg
->key
, key
);
62 blkg
->blkcg_id
= css_id(&blkcg
->css
);
63 hlist_add_head_rcu(&blkg
->blkcg_node
, &blkcg
->blkg_list
);
64 spin_unlock_irqrestore(&blkcg
->lock
, flags
);
65 #ifdef CONFIG_DEBUG_BLK_CGROUP
66 /* Need to take css reference ? */
67 cgroup_path(blkcg
->css
.cgroup
, blkg
->path
, sizeof(blkg
->path
));
71 EXPORT_SYMBOL_GPL(blkiocg_add_blkio_group
);
73 static void __blkiocg_del_blkio_group(struct blkio_group
*blkg
)
75 hlist_del_init_rcu(&blkg
->blkcg_node
);
80 * returns 0 if blkio_group was still on cgroup list. Otherwise returns 1
81 * indicating that blk_group was unhashed by the time we got to it.
83 int blkiocg_del_blkio_group(struct blkio_group
*blkg
)
85 struct blkio_cgroup
*blkcg
;
87 struct cgroup_subsys_state
*css
;
91 css
= css_lookup(&blkio_subsys
, blkg
->blkcg_id
);
95 blkcg
= container_of(css
, struct blkio_cgroup
, css
);
96 spin_lock_irqsave(&blkcg
->lock
, flags
);
97 if (!hlist_unhashed(&blkg
->blkcg_node
)) {
98 __blkiocg_del_blkio_group(blkg
);
101 spin_unlock_irqrestore(&blkcg
->lock
, flags
);
106 EXPORT_SYMBOL_GPL(blkiocg_del_blkio_group
);
108 /* called under rcu_read_lock(). */
109 struct blkio_group
*blkiocg_lookup_group(struct blkio_cgroup
*blkcg
, void *key
)
111 struct blkio_group
*blkg
;
112 struct hlist_node
*n
;
115 hlist_for_each_entry_rcu(blkg
, n
, &blkcg
->blkg_list
, blkcg_node
) {
123 EXPORT_SYMBOL_GPL(blkiocg_lookup_group
);
125 #define SHOW_FUNCTION(__VAR) \
126 static u64 blkiocg_##__VAR##_read(struct cgroup *cgroup, \
127 struct cftype *cftype) \
129 struct blkio_cgroup *blkcg; \
131 blkcg = cgroup_to_blkio_cgroup(cgroup); \
132 return (u64)blkcg->__VAR; \
135 SHOW_FUNCTION(weight
);
139 blkiocg_weight_write(struct cgroup
*cgroup
, struct cftype
*cftype
, u64 val
)
141 struct blkio_cgroup
*blkcg
;
142 struct blkio_group
*blkg
;
143 struct hlist_node
*n
;
144 struct blkio_policy_type
*blkiop
;
146 if (val
< BLKIO_WEIGHT_MIN
|| val
> BLKIO_WEIGHT_MAX
)
149 blkcg
= cgroup_to_blkio_cgroup(cgroup
);
150 spin_lock_irq(&blkcg
->lock
);
151 blkcg
->weight
= (unsigned int)val
;
152 hlist_for_each_entry(blkg
, n
, &blkcg
->blkg_list
, blkcg_node
) {
153 spin_lock(&blkio_list_lock
);
154 list_for_each_entry(blkiop
, &blkio_list
, list
)
155 blkiop
->ops
.blkio_update_group_weight_fn(blkg
,
157 spin_unlock(&blkio_list_lock
);
159 spin_unlock_irq(&blkcg
->lock
);
163 #define SHOW_FUNCTION_PER_GROUP(__VAR) \
164 static int blkiocg_##__VAR##_read(struct cgroup *cgroup, \
165 struct cftype *cftype, struct seq_file *m) \
167 struct blkio_cgroup *blkcg; \
168 struct blkio_group *blkg; \
169 struct hlist_node *n; \
171 if (!cgroup_lock_live_group(cgroup)) \
174 blkcg = cgroup_to_blkio_cgroup(cgroup); \
176 hlist_for_each_entry_rcu(blkg, n, &blkcg->blkg_list, blkcg_node) {\
178 seq_printf(m, "%u:%u %lu\n", MAJOR(blkg->dev), \
179 MINOR(blkg->dev), blkg->__VAR); \
186 SHOW_FUNCTION_PER_GROUP(time
);
187 SHOW_FUNCTION_PER_GROUP(sectors
);
188 #ifdef CONFIG_DEBUG_BLK_CGROUP
189 SHOW_FUNCTION_PER_GROUP(dequeue
);
191 #undef SHOW_FUNCTION_PER_GROUP
193 #ifdef CONFIG_DEBUG_BLK_CGROUP
194 void blkiocg_update_blkio_group_dequeue_stats(struct blkio_group
*blkg
,
195 unsigned long dequeue
)
197 blkg
->dequeue
+= dequeue
;
199 EXPORT_SYMBOL_GPL(blkiocg_update_blkio_group_dequeue_stats
);
202 struct cftype blkio_files
[] = {
205 .read_u64
= blkiocg_weight_read
,
206 .write_u64
= blkiocg_weight_write
,
210 .read_seq_string
= blkiocg_time_read
,
214 .read_seq_string
= blkiocg_sectors_read
,
216 #ifdef CONFIG_DEBUG_BLK_CGROUP
219 .read_seq_string
= blkiocg_dequeue_read
,
224 static int blkiocg_populate(struct cgroup_subsys
*subsys
, struct cgroup
*cgroup
)
226 return cgroup_add_files(cgroup
, subsys
, blkio_files
,
227 ARRAY_SIZE(blkio_files
));
230 static void blkiocg_destroy(struct cgroup_subsys
*subsys
, struct cgroup
*cgroup
)
232 struct blkio_cgroup
*blkcg
= cgroup_to_blkio_cgroup(cgroup
);
234 struct blkio_group
*blkg
;
236 struct blkio_policy_type
*blkiop
;
240 spin_lock_irqsave(&blkcg
->lock
, flags
);
242 if (hlist_empty(&blkcg
->blkg_list
)) {
243 spin_unlock_irqrestore(&blkcg
->lock
, flags
);
247 blkg
= hlist_entry(blkcg
->blkg_list
.first
, struct blkio_group
,
249 key
= rcu_dereference(blkg
->key
);
250 __blkiocg_del_blkio_group(blkg
);
252 spin_unlock_irqrestore(&blkcg
->lock
, flags
);
255 * This blkio_group is being unlinked as associated cgroup is going
256 * away. Let all the IO controlling policies know about this event.
258 * Currently this is static call to one io controlling policy. Once
259 * we have more policies in place, we need some dynamic registration
260 * of callback function.
262 spin_lock(&blkio_list_lock
);
263 list_for_each_entry(blkiop
, &blkio_list
, list
)
264 blkiop
->ops
.blkio_unlink_group_fn(key
, blkg
);
265 spin_unlock(&blkio_list_lock
);
268 free_css_id(&blkio_subsys
, &blkcg
->css
);
273 static struct cgroup_subsys_state
*
274 blkiocg_create(struct cgroup_subsys
*subsys
, struct cgroup
*cgroup
)
276 struct blkio_cgroup
*blkcg
, *parent_blkcg
;
278 if (!cgroup
->parent
) {
279 blkcg
= &blkio_root_cgroup
;
283 /* Currently we do not support hierarchy deeper than two level (0,1) */
284 parent_blkcg
= cgroup_to_blkio_cgroup(cgroup
->parent
);
285 if (css_depth(&parent_blkcg
->css
) > 0)
286 return ERR_PTR(-EINVAL
);
288 blkcg
= kzalloc(sizeof(*blkcg
), GFP_KERNEL
);
290 return ERR_PTR(-ENOMEM
);
292 blkcg
->weight
= BLKIO_WEIGHT_DEFAULT
;
294 spin_lock_init(&blkcg
->lock
);
295 INIT_HLIST_HEAD(&blkcg
->blkg_list
);
301 * We cannot support shared io contexts, as we have no mean to support
302 * two tasks with the same ioc in two different groups without major rework
303 * of the main cic data structures. For now we allow a task to change
304 * its cgroup only if it's the only owner of its ioc.
306 static int blkiocg_can_attach(struct cgroup_subsys
*subsys
,
307 struct cgroup
*cgroup
, struct task_struct
*tsk
,
310 struct io_context
*ioc
;
313 /* task_lock() is needed to avoid races with exit_io_context() */
315 ioc
= tsk
->io_context
;
316 if (ioc
&& atomic_read(&ioc
->nr_tasks
) > 1)
323 static void blkiocg_attach(struct cgroup_subsys
*subsys
, struct cgroup
*cgroup
,
324 struct cgroup
*prev
, struct task_struct
*tsk
,
327 struct io_context
*ioc
;
330 ioc
= tsk
->io_context
;
332 ioc
->cgroup_changed
= 1;
336 struct cgroup_subsys blkio_subsys
= {
338 .create
= blkiocg_create
,
339 .can_attach
= blkiocg_can_attach
,
340 .attach
= blkiocg_attach
,
341 .destroy
= blkiocg_destroy
,
342 .populate
= blkiocg_populate
,
343 .subsys_id
= blkio_subsys_id
,
347 void blkio_policy_register(struct blkio_policy_type
*blkiop
)
349 spin_lock(&blkio_list_lock
);
350 list_add_tail(&blkiop
->list
, &blkio_list
);
351 spin_unlock(&blkio_list_lock
);
353 EXPORT_SYMBOL_GPL(blkio_policy_register
);
355 void blkio_policy_unregister(struct blkio_policy_type
*blkiop
)
357 spin_lock(&blkio_list_lock
);
358 list_del_init(&blkiop
->list
);
359 spin_unlock(&blkio_list_lock
);
361 EXPORT_SYMBOL_GPL(blkio_policy_unregister
);