blob: af42efbb0c1d8681cd9d657a4a4598912f1dc740 [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>
Divyesh Shah91952912010-04-01 15:01:41 -070018#include <linux/blkdev.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090019#include <linux/slab.h>
Vivek Goyal31e4c282009-12-03 12:59:42 -050020#include "blk-cgroup.h"
Gui Jianfeng34d0f172010-04-13 16:05:49 +080021#include <linux/genhd.h>
Vivek Goyal3e252062009-12-04 10:36:42 -050022
Divyesh Shah84c124d2010-04-09 08:31:19 +020023#define MAX_KEY_LEN 100
24
Vivek Goyal3e252062009-12-04 10:36:42 -050025static DEFINE_SPINLOCK(blkio_list_lock);
26static LIST_HEAD(blkio_list);
Vivek Goyalb1c35762009-12-03 12:59:47 -050027
Vivek Goyal31e4c282009-12-03 12:59:42 -050028struct blkio_cgroup blkio_root_cgroup = { .weight = 2*BLKIO_WEIGHT_DEFAULT };
Vivek Goyal9d6a9862009-12-04 10:36:41 -050029EXPORT_SYMBOL_GPL(blkio_root_cgroup);
30
Ben Blum67523c42010-03-10 15:22:11 -080031static struct cgroup_subsys_state *blkiocg_create(struct cgroup_subsys *,
32 struct cgroup *);
33static int blkiocg_can_attach(struct cgroup_subsys *, struct cgroup *,
34 struct task_struct *, bool);
35static void blkiocg_attach(struct cgroup_subsys *, struct cgroup *,
36 struct cgroup *, struct task_struct *, bool);
37static void blkiocg_destroy(struct cgroup_subsys *, struct cgroup *);
38static int blkiocg_populate(struct cgroup_subsys *, struct cgroup *);
39
40struct cgroup_subsys blkio_subsys = {
41 .name = "blkio",
42 .create = blkiocg_create,
43 .can_attach = blkiocg_can_attach,
44 .attach = blkiocg_attach,
45 .destroy = blkiocg_destroy,
46 .populate = blkiocg_populate,
47#ifdef CONFIG_BLK_CGROUP
48 /* note: blkio_subsys_id is otherwise defined in blk-cgroup.h */
49 .subsys_id = blkio_subsys_id,
50#endif
51 .use_id = 1,
52 .module = THIS_MODULE,
53};
54EXPORT_SYMBOL_GPL(blkio_subsys);
55
Gui Jianfeng34d0f172010-04-13 16:05:49 +080056static inline void blkio_policy_insert_node(struct blkio_cgroup *blkcg,
57 struct blkio_policy_node *pn)
58{
59 list_add(&pn->node, &blkcg->policy_list);
60}
61
62/* Must be called with blkcg->lock held */
63static inline void blkio_policy_delete_node(struct blkio_policy_node *pn)
64{
65 list_del(&pn->node);
66}
67
68/* Must be called with blkcg->lock held */
69static struct blkio_policy_node *
70blkio_policy_search_node(const struct blkio_cgroup *blkcg, dev_t dev)
71{
72 struct blkio_policy_node *pn;
73
74 list_for_each_entry(pn, &blkcg->policy_list, node) {
75 if (pn->dev == dev)
76 return pn;
77 }
78
79 return NULL;
80}
81
Vivek Goyal31e4c282009-12-03 12:59:42 -050082struct blkio_cgroup *cgroup_to_blkio_cgroup(struct cgroup *cgroup)
83{
84 return container_of(cgroup_subsys_state(cgroup, blkio_subsys_id),
85 struct blkio_cgroup, css);
86}
Vivek Goyal9d6a9862009-12-04 10:36:41 -050087EXPORT_SYMBOL_GPL(cgroup_to_blkio_cgroup);
Vivek Goyal31e4c282009-12-03 12:59:42 -050088
Divyesh Shah91952912010-04-01 15:01:41 -070089/*
90 * Add to the appropriate stat variable depending on the request type.
91 * This should be called with the blkg->stats_lock held.
92 */
Divyesh Shah84c124d2010-04-09 08:31:19 +020093static void blkio_add_stat(uint64_t *stat, uint64_t add, bool direction,
94 bool sync)
Divyesh Shah91952912010-04-01 15:01:41 -070095{
Divyesh Shah84c124d2010-04-09 08:31:19 +020096 if (direction)
97 stat[BLKIO_STAT_WRITE] += add;
Divyesh Shah91952912010-04-01 15:01:41 -070098 else
Divyesh Shah84c124d2010-04-09 08:31:19 +020099 stat[BLKIO_STAT_READ] += add;
100 if (sync)
101 stat[BLKIO_STAT_SYNC] += add;
Divyesh Shah91952912010-04-01 15:01:41 -0700102 else
Divyesh Shah84c124d2010-04-09 08:31:19 +0200103 stat[BLKIO_STAT_ASYNC] += add;
Divyesh Shah91952912010-04-01 15:01:41 -0700104}
105
Divyesh Shahcdc11842010-04-08 21:15:10 -0700106/*
107 * Decrements the appropriate stat variable if non-zero depending on the
108 * request type. Panics on value being zero.
109 * This should be called with the blkg->stats_lock held.
110 */
111static void blkio_check_and_dec_stat(uint64_t *stat, bool direction, bool sync)
112{
113 if (direction) {
114 BUG_ON(stat[BLKIO_STAT_WRITE] == 0);
115 stat[BLKIO_STAT_WRITE]--;
116 } else {
117 BUG_ON(stat[BLKIO_STAT_READ] == 0);
118 stat[BLKIO_STAT_READ]--;
119 }
120 if (sync) {
121 BUG_ON(stat[BLKIO_STAT_SYNC] == 0);
122 stat[BLKIO_STAT_SYNC]--;
123 } else {
124 BUG_ON(stat[BLKIO_STAT_ASYNC] == 0);
125 stat[BLKIO_STAT_ASYNC]--;
126 }
127}
128
129#ifdef CONFIG_DEBUG_BLK_CGROUP
Divyesh Shah812df482010-04-08 21:15:35 -0700130/* This should be called with the blkg->stats_lock held. */
131static void blkio_set_start_group_wait_time(struct blkio_group *blkg,
132 struct blkio_group *curr_blkg)
133{
134 if (blkio_blkg_waiting(&blkg->stats))
135 return;
136 if (blkg == curr_blkg)
137 return;
138 blkg->stats.start_group_wait_time = sched_clock();
139 blkio_mark_blkg_waiting(&blkg->stats);
140}
141
142/* This should be called with the blkg->stats_lock held. */
143static void blkio_update_group_wait_time(struct blkio_group_stats *stats)
144{
145 unsigned long long now;
146
147 if (!blkio_blkg_waiting(stats))
148 return;
149
150 now = sched_clock();
151 if (time_after64(now, stats->start_group_wait_time))
152 stats->group_wait_time += now - stats->start_group_wait_time;
153 blkio_clear_blkg_waiting(stats);
154}
155
156/* This should be called with the blkg->stats_lock held. */
157static void blkio_end_empty_time(struct blkio_group_stats *stats)
158{
159 unsigned long long now;
160
161 if (!blkio_blkg_empty(stats))
162 return;
163
164 now = sched_clock();
165 if (time_after64(now, stats->start_empty_time))
166 stats->empty_time += now - stats->start_empty_time;
167 blkio_clear_blkg_empty(stats);
168}
169
170void blkiocg_update_set_idle_time_stats(struct blkio_group *blkg)
171{
172 unsigned long flags;
173
174 spin_lock_irqsave(&blkg->stats_lock, flags);
175 BUG_ON(blkio_blkg_idling(&blkg->stats));
176 blkg->stats.start_idle_time = sched_clock();
177 blkio_mark_blkg_idling(&blkg->stats);
178 spin_unlock_irqrestore(&blkg->stats_lock, flags);
179}
180EXPORT_SYMBOL_GPL(blkiocg_update_set_idle_time_stats);
181
182void blkiocg_update_idle_time_stats(struct blkio_group *blkg)
183{
184 unsigned long flags;
185 unsigned long long now;
186 struct blkio_group_stats *stats;
187
188 spin_lock_irqsave(&blkg->stats_lock, flags);
189 stats = &blkg->stats;
190 if (blkio_blkg_idling(stats)) {
191 now = sched_clock();
192 if (time_after64(now, stats->start_idle_time))
193 stats->idle_time += now - stats->start_idle_time;
194 blkio_clear_blkg_idling(stats);
195 }
196 spin_unlock_irqrestore(&blkg->stats_lock, flags);
197}
198EXPORT_SYMBOL_GPL(blkiocg_update_idle_time_stats);
199
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200200void blkiocg_update_avg_queue_size_stats(struct blkio_group *blkg)
Divyesh Shahcdc11842010-04-08 21:15:10 -0700201{
202 unsigned long flags;
203 struct blkio_group_stats *stats;
204
205 spin_lock_irqsave(&blkg->stats_lock, flags);
206 stats = &blkg->stats;
207 stats->avg_queue_size_sum +=
208 stats->stat_arr[BLKIO_STAT_QUEUED][BLKIO_STAT_READ] +
209 stats->stat_arr[BLKIO_STAT_QUEUED][BLKIO_STAT_WRITE];
210 stats->avg_queue_size_samples++;
Divyesh Shah812df482010-04-08 21:15:35 -0700211 blkio_update_group_wait_time(stats);
Divyesh Shahcdc11842010-04-08 21:15:10 -0700212 spin_unlock_irqrestore(&blkg->stats_lock, flags);
213}
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200214EXPORT_SYMBOL_GPL(blkiocg_update_avg_queue_size_stats);
215
Vivek Goyale5ff0822010-04-26 19:25:11 +0200216void blkiocg_set_start_empty_time(struct blkio_group *blkg)
Divyesh Shah28baf442010-04-14 11:22:38 +0200217{
218 unsigned long flags;
219 struct blkio_group_stats *stats;
220
221 spin_lock_irqsave(&blkg->stats_lock, flags);
222 stats = &blkg->stats;
223
224 if (stats->stat_arr[BLKIO_STAT_QUEUED][BLKIO_STAT_READ] ||
225 stats->stat_arr[BLKIO_STAT_QUEUED][BLKIO_STAT_WRITE]) {
226 spin_unlock_irqrestore(&blkg->stats_lock, flags);
227 return;
228 }
229
230 /*
Vivek Goyale5ff0822010-04-26 19:25:11 +0200231 * group is already marked empty. This can happen if cfqq got new
232 * request in parent group and moved to this group while being added
233 * to service tree. Just ignore the event and move on.
Divyesh Shah28baf442010-04-14 11:22:38 +0200234 */
Vivek Goyale5ff0822010-04-26 19:25:11 +0200235 if(blkio_blkg_empty(stats)) {
236 spin_unlock_irqrestore(&blkg->stats_lock, flags);
237 return;
238 }
239
Divyesh Shah28baf442010-04-14 11:22:38 +0200240 stats->start_empty_time = sched_clock();
241 blkio_mark_blkg_empty(stats);
242 spin_unlock_irqrestore(&blkg->stats_lock, flags);
243}
244EXPORT_SYMBOL_GPL(blkiocg_set_start_empty_time);
245
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200246void blkiocg_update_dequeue_stats(struct blkio_group *blkg,
247 unsigned long dequeue)
248{
249 blkg->stats.dequeue += dequeue;
250}
251EXPORT_SYMBOL_GPL(blkiocg_update_dequeue_stats);
Divyesh Shah812df482010-04-08 21:15:35 -0700252#else
253static inline void blkio_set_start_group_wait_time(struct blkio_group *blkg,
254 struct blkio_group *curr_blkg) {}
255static inline void blkio_end_empty_time(struct blkio_group_stats *stats) {}
Divyesh Shahcdc11842010-04-08 21:15:10 -0700256#endif
257
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200258void blkiocg_update_io_add_stats(struct blkio_group *blkg,
Divyesh Shahcdc11842010-04-08 21:15:10 -0700259 struct blkio_group *curr_blkg, bool direction,
260 bool sync)
261{
262 unsigned long flags;
263
264 spin_lock_irqsave(&blkg->stats_lock, flags);
265 blkio_add_stat(blkg->stats.stat_arr[BLKIO_STAT_QUEUED], 1, direction,
266 sync);
Divyesh Shah812df482010-04-08 21:15:35 -0700267 blkio_end_empty_time(&blkg->stats);
268 blkio_set_start_group_wait_time(blkg, curr_blkg);
Divyesh Shahcdc11842010-04-08 21:15:10 -0700269 spin_unlock_irqrestore(&blkg->stats_lock, flags);
270}
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200271EXPORT_SYMBOL_GPL(blkiocg_update_io_add_stats);
Divyesh Shahcdc11842010-04-08 21:15:10 -0700272
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200273void blkiocg_update_io_remove_stats(struct blkio_group *blkg,
Divyesh Shahcdc11842010-04-08 21:15:10 -0700274 bool direction, bool sync)
275{
276 unsigned long flags;
277
278 spin_lock_irqsave(&blkg->stats_lock, flags);
279 blkio_check_and_dec_stat(blkg->stats.stat_arr[BLKIO_STAT_QUEUED],
280 direction, sync);
281 spin_unlock_irqrestore(&blkg->stats_lock, flags);
282}
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200283EXPORT_SYMBOL_GPL(blkiocg_update_io_remove_stats);
Divyesh Shahcdc11842010-04-08 21:15:10 -0700284
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700285void blkiocg_update_timeslice_used(struct blkio_group *blkg, unsigned long time)
Vivek Goyal22084192009-12-03 12:59:49 -0500286{
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700287 unsigned long flags;
288
289 spin_lock_irqsave(&blkg->stats_lock, flags);
290 blkg->stats.time += time;
291 spin_unlock_irqrestore(&blkg->stats_lock, flags);
Vivek Goyal22084192009-12-03 12:59:49 -0500292}
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700293EXPORT_SYMBOL_GPL(blkiocg_update_timeslice_used);
Vivek Goyal22084192009-12-03 12:59:49 -0500294
Divyesh Shah84c124d2010-04-09 08:31:19 +0200295void blkiocg_update_dispatch_stats(struct blkio_group *blkg,
296 uint64_t bytes, bool direction, bool sync)
Divyesh Shah91952912010-04-01 15:01:41 -0700297{
298 struct blkio_group_stats *stats;
299 unsigned long flags;
300
301 spin_lock_irqsave(&blkg->stats_lock, flags);
302 stats = &blkg->stats;
Divyesh Shah84c124d2010-04-09 08:31:19 +0200303 stats->sectors += bytes >> 9;
304 blkio_add_stat(stats->stat_arr[BLKIO_STAT_SERVICED], 1, direction,
305 sync);
306 blkio_add_stat(stats->stat_arr[BLKIO_STAT_SERVICE_BYTES], bytes,
307 direction, sync);
Divyesh Shah91952912010-04-01 15:01:41 -0700308 spin_unlock_irqrestore(&blkg->stats_lock, flags);
309}
Divyesh Shah84c124d2010-04-09 08:31:19 +0200310EXPORT_SYMBOL_GPL(blkiocg_update_dispatch_stats);
Divyesh Shah91952912010-04-01 15:01:41 -0700311
Divyesh Shah84c124d2010-04-09 08:31:19 +0200312void blkiocg_update_completion_stats(struct blkio_group *blkg,
313 uint64_t start_time, uint64_t io_start_time, bool direction, bool sync)
Divyesh Shah91952912010-04-01 15:01:41 -0700314{
315 struct blkio_group_stats *stats;
316 unsigned long flags;
317 unsigned long long now = sched_clock();
318
319 spin_lock_irqsave(&blkg->stats_lock, flags);
320 stats = &blkg->stats;
Divyesh Shah84c124d2010-04-09 08:31:19 +0200321 if (time_after64(now, io_start_time))
322 blkio_add_stat(stats->stat_arr[BLKIO_STAT_SERVICE_TIME],
323 now - io_start_time, direction, sync);
324 if (time_after64(io_start_time, start_time))
325 blkio_add_stat(stats->stat_arr[BLKIO_STAT_WAIT_TIME],
326 io_start_time - start_time, direction, sync);
Divyesh Shah91952912010-04-01 15:01:41 -0700327 spin_unlock_irqrestore(&blkg->stats_lock, flags);
328}
Divyesh Shah84c124d2010-04-09 08:31:19 +0200329EXPORT_SYMBOL_GPL(blkiocg_update_completion_stats);
Divyesh Shah91952912010-04-01 15:01:41 -0700330
Divyesh Shah812d4022010-04-08 21:14:23 -0700331void blkiocg_update_io_merged_stats(struct blkio_group *blkg, bool direction,
332 bool sync)
333{
334 unsigned long flags;
335
336 spin_lock_irqsave(&blkg->stats_lock, flags);
337 blkio_add_stat(blkg->stats.stat_arr[BLKIO_STAT_MERGED], 1, direction,
338 sync);
339 spin_unlock_irqrestore(&blkg->stats_lock, flags);
340}
341EXPORT_SYMBOL_GPL(blkiocg_update_io_merged_stats);
342
Vivek Goyal31e4c282009-12-03 12:59:42 -0500343void blkiocg_add_blkio_group(struct blkio_cgroup *blkcg,
Vivek Goyal22084192009-12-03 12:59:49 -0500344 struct blkio_group *blkg, void *key, dev_t dev)
Vivek Goyal31e4c282009-12-03 12:59:42 -0500345{
346 unsigned long flags;
347
348 spin_lock_irqsave(&blkcg->lock, flags);
Divyesh Shah8d2a91f2010-04-16 08:10:51 +0200349 spin_lock_init(&blkg->stats_lock);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500350 rcu_assign_pointer(blkg->key, key);
Vivek Goyalb1c35762009-12-03 12:59:47 -0500351 blkg->blkcg_id = css_id(&blkcg->css);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500352 hlist_add_head_rcu(&blkg->blkcg_node, &blkcg->blkg_list);
353 spin_unlock_irqrestore(&blkcg->lock, flags);
Vivek Goyal2868ef72009-12-03 12:59:48 -0500354#ifdef CONFIG_DEBUG_BLK_CGROUP
355 /* Need to take css reference ? */
356 cgroup_path(blkcg->css.cgroup, blkg->path, sizeof(blkg->path));
357#endif
Vivek Goyal22084192009-12-03 12:59:49 -0500358 blkg->dev = dev;
Vivek Goyal31e4c282009-12-03 12:59:42 -0500359}
Vivek Goyal9d6a9862009-12-04 10:36:41 -0500360EXPORT_SYMBOL_GPL(blkiocg_add_blkio_group);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500361
Vivek Goyalb1c35762009-12-03 12:59:47 -0500362static void __blkiocg_del_blkio_group(struct blkio_group *blkg)
363{
364 hlist_del_init_rcu(&blkg->blkcg_node);
365 blkg->blkcg_id = 0;
366}
367
368/*
369 * returns 0 if blkio_group was still on cgroup list. Otherwise returns 1
370 * indicating that blk_group was unhashed by the time we got to it.
371 */
Vivek Goyal31e4c282009-12-03 12:59:42 -0500372int blkiocg_del_blkio_group(struct blkio_group *blkg)
373{
Vivek Goyalb1c35762009-12-03 12:59:47 -0500374 struct blkio_cgroup *blkcg;
375 unsigned long flags;
376 struct cgroup_subsys_state *css;
377 int ret = 1;
378
379 rcu_read_lock();
380 css = css_lookup(&blkio_subsys, blkg->blkcg_id);
381 if (!css)
382 goto out;
383
384 blkcg = container_of(css, struct blkio_cgroup, css);
385 spin_lock_irqsave(&blkcg->lock, flags);
386 if (!hlist_unhashed(&blkg->blkcg_node)) {
387 __blkiocg_del_blkio_group(blkg);
388 ret = 0;
389 }
390 spin_unlock_irqrestore(&blkcg->lock, flags);
391out:
392 rcu_read_unlock();
393 return ret;
Vivek Goyal31e4c282009-12-03 12:59:42 -0500394}
Vivek Goyal9d6a9862009-12-04 10:36:41 -0500395EXPORT_SYMBOL_GPL(blkiocg_del_blkio_group);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500396
397/* called under rcu_read_lock(). */
398struct blkio_group *blkiocg_lookup_group(struct blkio_cgroup *blkcg, void *key)
399{
400 struct blkio_group *blkg;
401 struct hlist_node *n;
402 void *__key;
403
404 hlist_for_each_entry_rcu(blkg, n, &blkcg->blkg_list, blkcg_node) {
405 __key = blkg->key;
406 if (__key == key)
407 return blkg;
408 }
409
410 return NULL;
411}
Vivek Goyal9d6a9862009-12-04 10:36:41 -0500412EXPORT_SYMBOL_GPL(blkiocg_lookup_group);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500413
414#define SHOW_FUNCTION(__VAR) \
415static u64 blkiocg_##__VAR##_read(struct cgroup *cgroup, \
416 struct cftype *cftype) \
417{ \
418 struct blkio_cgroup *blkcg; \
419 \
420 blkcg = cgroup_to_blkio_cgroup(cgroup); \
421 return (u64)blkcg->__VAR; \
422}
423
424SHOW_FUNCTION(weight);
425#undef SHOW_FUNCTION
426
427static int
428blkiocg_weight_write(struct cgroup *cgroup, struct cftype *cftype, u64 val)
429{
430 struct blkio_cgroup *blkcg;
Vivek Goyalf8d461d2009-12-03 12:59:52 -0500431 struct blkio_group *blkg;
432 struct hlist_node *n;
Vivek Goyal3e252062009-12-04 10:36:42 -0500433 struct blkio_policy_type *blkiop;
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800434 struct blkio_policy_node *pn;
Vivek Goyal31e4c282009-12-03 12:59:42 -0500435
436 if (val < BLKIO_WEIGHT_MIN || val > BLKIO_WEIGHT_MAX)
437 return -EINVAL;
438
439 blkcg = cgroup_to_blkio_cgroup(cgroup);
Gui Jianfengbcf4dd42010-02-01 09:58:54 +0100440 spin_lock(&blkio_list_lock);
Vivek Goyalf8d461d2009-12-03 12:59:52 -0500441 spin_lock_irq(&blkcg->lock);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500442 blkcg->weight = (unsigned int)val;
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800443
Vivek Goyal3e252062009-12-04 10:36:42 -0500444 hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) {
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800445 pn = blkio_policy_search_node(blkcg, blkg->dev);
446
447 if (pn)
448 continue;
449
Vivek Goyal3e252062009-12-04 10:36:42 -0500450 list_for_each_entry(blkiop, &blkio_list, list)
451 blkiop->ops.blkio_update_group_weight_fn(blkg,
452 blkcg->weight);
Vivek Goyal3e252062009-12-04 10:36:42 -0500453 }
Vivek Goyalf8d461d2009-12-03 12:59:52 -0500454 spin_unlock_irq(&blkcg->lock);
Gui Jianfengbcf4dd42010-02-01 09:58:54 +0100455 spin_unlock(&blkio_list_lock);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500456 return 0;
457}
458
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700459static int
Divyesh Shah84c124d2010-04-09 08:31:19 +0200460blkiocg_reset_stats(struct cgroup *cgroup, struct cftype *cftype, u64 val)
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700461{
462 struct blkio_cgroup *blkcg;
463 struct blkio_group *blkg;
Divyesh Shah812df482010-04-08 21:15:35 -0700464 struct blkio_group_stats *stats;
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700465 struct hlist_node *n;
Divyesh Shahcdc11842010-04-08 21:15:10 -0700466 uint64_t queued[BLKIO_STAT_TOTAL];
467 int i;
Divyesh Shah812df482010-04-08 21:15:35 -0700468#ifdef CONFIG_DEBUG_BLK_CGROUP
469 bool idling, waiting, empty;
470 unsigned long long now = sched_clock();
471#endif
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700472
473 blkcg = cgroup_to_blkio_cgroup(cgroup);
474 spin_lock_irq(&blkcg->lock);
475 hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) {
476 spin_lock(&blkg->stats_lock);
Divyesh Shah812df482010-04-08 21:15:35 -0700477 stats = &blkg->stats;
478#ifdef CONFIG_DEBUG_BLK_CGROUP
479 idling = blkio_blkg_idling(stats);
480 waiting = blkio_blkg_waiting(stats);
481 empty = blkio_blkg_empty(stats);
482#endif
Divyesh Shahcdc11842010-04-08 21:15:10 -0700483 for (i = 0; i < BLKIO_STAT_TOTAL; i++)
Divyesh Shah812df482010-04-08 21:15:35 -0700484 queued[i] = stats->stat_arr[BLKIO_STAT_QUEUED][i];
485 memset(stats, 0, sizeof(struct blkio_group_stats));
Divyesh Shahcdc11842010-04-08 21:15:10 -0700486 for (i = 0; i < BLKIO_STAT_TOTAL; i++)
Divyesh Shah812df482010-04-08 21:15:35 -0700487 stats->stat_arr[BLKIO_STAT_QUEUED][i] = queued[i];
488#ifdef CONFIG_DEBUG_BLK_CGROUP
489 if (idling) {
490 blkio_mark_blkg_idling(stats);
491 stats->start_idle_time = now;
492 }
493 if (waiting) {
494 blkio_mark_blkg_waiting(stats);
495 stats->start_group_wait_time = now;
496 }
497 if (empty) {
498 blkio_mark_blkg_empty(stats);
499 stats->start_empty_time = now;
500 }
501#endif
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700502 spin_unlock(&blkg->stats_lock);
503 }
504 spin_unlock_irq(&blkcg->lock);
505 return 0;
506}
507
Divyesh Shah84c124d2010-04-09 08:31:19 +0200508static void blkio_get_key_name(enum stat_sub_type type, dev_t dev, char *str,
509 int chars_left, bool diskname_only)
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700510{
Divyesh Shah84c124d2010-04-09 08:31:19 +0200511 snprintf(str, chars_left, "%d:%d", MAJOR(dev), MINOR(dev));
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700512 chars_left -= strlen(str);
513 if (chars_left <= 0) {
514 printk(KERN_WARNING
515 "Possibly incorrect cgroup stat display format");
516 return;
517 }
Divyesh Shah84c124d2010-04-09 08:31:19 +0200518 if (diskname_only)
519 return;
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700520 switch (type) {
Divyesh Shah84c124d2010-04-09 08:31:19 +0200521 case BLKIO_STAT_READ:
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700522 strlcat(str, " Read", chars_left);
523 break;
Divyesh Shah84c124d2010-04-09 08:31:19 +0200524 case BLKIO_STAT_WRITE:
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700525 strlcat(str, " Write", chars_left);
526 break;
Divyesh Shah84c124d2010-04-09 08:31:19 +0200527 case BLKIO_STAT_SYNC:
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700528 strlcat(str, " Sync", chars_left);
529 break;
Divyesh Shah84c124d2010-04-09 08:31:19 +0200530 case BLKIO_STAT_ASYNC:
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700531 strlcat(str, " Async", chars_left);
532 break;
Divyesh Shah84c124d2010-04-09 08:31:19 +0200533 case BLKIO_STAT_TOTAL:
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700534 strlcat(str, " Total", chars_left);
535 break;
536 default:
537 strlcat(str, " Invalid", chars_left);
538 }
539}
540
Divyesh Shah84c124d2010-04-09 08:31:19 +0200541static uint64_t blkio_fill_stat(char *str, int chars_left, uint64_t val,
542 struct cgroup_map_cb *cb, dev_t dev)
543{
544 blkio_get_key_name(0, dev, str, chars_left, true);
545 cb->fill(cb, str, val);
546 return val;
547}
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700548
Divyesh Shah84c124d2010-04-09 08:31:19 +0200549/* This should be called with blkg->stats_lock held */
550static uint64_t blkio_get_stat(struct blkio_group *blkg,
551 struct cgroup_map_cb *cb, dev_t dev, enum stat_type type)
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700552{
553 uint64_t disk_total;
554 char key_str[MAX_KEY_LEN];
Divyesh Shah84c124d2010-04-09 08:31:19 +0200555 enum stat_sub_type sub_type;
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700556
Divyesh Shah84c124d2010-04-09 08:31:19 +0200557 if (type == BLKIO_STAT_TIME)
558 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
559 blkg->stats.time, cb, dev);
560 if (type == BLKIO_STAT_SECTORS)
561 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
562 blkg->stats.sectors, cb, dev);
563#ifdef CONFIG_DEBUG_BLK_CGROUP
Divyesh Shahcdc11842010-04-08 21:15:10 -0700564 if (type == BLKIO_STAT_AVG_QUEUE_SIZE) {
565 uint64_t sum = blkg->stats.avg_queue_size_sum;
566 uint64_t samples = blkg->stats.avg_queue_size_samples;
567 if (samples)
568 do_div(sum, samples);
569 else
570 sum = 0;
571 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1, sum, cb, dev);
572 }
Divyesh Shah812df482010-04-08 21:15:35 -0700573 if (type == BLKIO_STAT_GROUP_WAIT_TIME)
574 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
575 blkg->stats.group_wait_time, cb, dev);
576 if (type == BLKIO_STAT_IDLE_TIME)
577 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
578 blkg->stats.idle_time, cb, dev);
579 if (type == BLKIO_STAT_EMPTY_TIME)
580 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
581 blkg->stats.empty_time, cb, dev);
Divyesh Shah84c124d2010-04-09 08:31:19 +0200582 if (type == BLKIO_STAT_DEQUEUE)
583 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
584 blkg->stats.dequeue, cb, dev);
585#endif
586
587 for (sub_type = BLKIO_STAT_READ; sub_type < BLKIO_STAT_TOTAL;
588 sub_type++) {
589 blkio_get_key_name(sub_type, dev, key_str, MAX_KEY_LEN, false);
590 cb->fill(cb, key_str, blkg->stats.stat_arr[type][sub_type]);
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700591 }
Divyesh Shah84c124d2010-04-09 08:31:19 +0200592 disk_total = blkg->stats.stat_arr[type][BLKIO_STAT_READ] +
593 blkg->stats.stat_arr[type][BLKIO_STAT_WRITE];
594 blkio_get_key_name(BLKIO_STAT_TOTAL, dev, key_str, MAX_KEY_LEN, false);
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700595 cb->fill(cb, key_str, disk_total);
596 return disk_total;
597}
598
Divyesh Shah84c124d2010-04-09 08:31:19 +0200599#define SHOW_FUNCTION_PER_GROUP(__VAR, type, show_total) \
Vivek Goyal22084192009-12-03 12:59:49 -0500600static int blkiocg_##__VAR##_read(struct cgroup *cgroup, \
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700601 struct cftype *cftype, struct cgroup_map_cb *cb) \
Vivek Goyal22084192009-12-03 12:59:49 -0500602{ \
603 struct blkio_cgroup *blkcg; \
604 struct blkio_group *blkg; \
605 struct hlist_node *n; \
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700606 uint64_t cgroup_total = 0; \
Vivek Goyal22084192009-12-03 12:59:49 -0500607 \
608 if (!cgroup_lock_live_group(cgroup)) \
609 return -ENODEV; \
610 \
611 blkcg = cgroup_to_blkio_cgroup(cgroup); \
612 rcu_read_lock(); \
613 hlist_for_each_entry_rcu(blkg, n, &blkcg->blkg_list, blkcg_node) {\
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700614 if (blkg->dev) { \
615 spin_lock_irq(&blkg->stats_lock); \
Divyesh Shah84c124d2010-04-09 08:31:19 +0200616 cgroup_total += blkio_get_stat(blkg, cb, \
617 blkg->dev, type); \
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700618 spin_unlock_irq(&blkg->stats_lock); \
619 } \
Vivek Goyal22084192009-12-03 12:59:49 -0500620 } \
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700621 if (show_total) \
622 cb->fill(cb, "Total", cgroup_total); \
Vivek Goyal22084192009-12-03 12:59:49 -0500623 rcu_read_unlock(); \
624 cgroup_unlock(); \
625 return 0; \
626}
627
Divyesh Shah84c124d2010-04-09 08:31:19 +0200628SHOW_FUNCTION_PER_GROUP(time, BLKIO_STAT_TIME, 0);
629SHOW_FUNCTION_PER_GROUP(sectors, BLKIO_STAT_SECTORS, 0);
630SHOW_FUNCTION_PER_GROUP(io_service_bytes, BLKIO_STAT_SERVICE_BYTES, 1);
631SHOW_FUNCTION_PER_GROUP(io_serviced, BLKIO_STAT_SERVICED, 1);
632SHOW_FUNCTION_PER_GROUP(io_service_time, BLKIO_STAT_SERVICE_TIME, 1);
633SHOW_FUNCTION_PER_GROUP(io_wait_time, BLKIO_STAT_WAIT_TIME, 1);
Divyesh Shah812d4022010-04-08 21:14:23 -0700634SHOW_FUNCTION_PER_GROUP(io_merged, BLKIO_STAT_MERGED, 1);
Divyesh Shahcdc11842010-04-08 21:15:10 -0700635SHOW_FUNCTION_PER_GROUP(io_queued, BLKIO_STAT_QUEUED, 1);
Vivek Goyal22084192009-12-03 12:59:49 -0500636#ifdef CONFIG_DEBUG_BLK_CGROUP
Divyesh Shah84c124d2010-04-09 08:31:19 +0200637SHOW_FUNCTION_PER_GROUP(dequeue, BLKIO_STAT_DEQUEUE, 0);
Divyesh Shahcdc11842010-04-08 21:15:10 -0700638SHOW_FUNCTION_PER_GROUP(avg_queue_size, BLKIO_STAT_AVG_QUEUE_SIZE, 0);
Divyesh Shah812df482010-04-08 21:15:35 -0700639SHOW_FUNCTION_PER_GROUP(group_wait_time, BLKIO_STAT_GROUP_WAIT_TIME, 0);
640SHOW_FUNCTION_PER_GROUP(idle_time, BLKIO_STAT_IDLE_TIME, 0);
641SHOW_FUNCTION_PER_GROUP(empty_time, BLKIO_STAT_EMPTY_TIME, 0);
Vivek Goyal22084192009-12-03 12:59:49 -0500642#endif
643#undef SHOW_FUNCTION_PER_GROUP
644
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800645static int blkio_check_dev_num(dev_t dev)
646{
647 int part = 0;
648 struct gendisk *disk;
649
650 disk = get_gendisk(dev, &part);
651 if (!disk || part)
652 return -ENODEV;
653
654 return 0;
655}
656
657static int blkio_policy_parse_and_set(char *buf,
658 struct blkio_policy_node *newpn)
659{
660 char *s[4], *p, *major_s = NULL, *minor_s = NULL;
661 int ret;
662 unsigned long major, minor, temp;
663 int i = 0;
664 dev_t dev;
665
666 memset(s, 0, sizeof(s));
667
668 while ((p = strsep(&buf, " ")) != NULL) {
669 if (!*p)
670 continue;
671
672 s[i++] = p;
673
674 /* Prevent from inputing too many things */
675 if (i == 3)
676 break;
677 }
678
679 if (i != 2)
680 return -EINVAL;
681
682 p = strsep(&s[0], ":");
683 if (p != NULL)
684 major_s = p;
685 else
686 return -EINVAL;
687
688 minor_s = s[0];
689 if (!minor_s)
690 return -EINVAL;
691
692 ret = strict_strtoul(major_s, 10, &major);
693 if (ret)
694 return -EINVAL;
695
696 ret = strict_strtoul(minor_s, 10, &minor);
697 if (ret)
698 return -EINVAL;
699
700 dev = MKDEV(major, minor);
701
702 ret = blkio_check_dev_num(dev);
703 if (ret)
704 return ret;
705
706 newpn->dev = dev;
707
708 if (s[1] == NULL)
709 return -EINVAL;
710
711 ret = strict_strtoul(s[1], 10, &temp);
712 if (ret || (temp < BLKIO_WEIGHT_MIN && temp > 0) ||
713 temp > BLKIO_WEIGHT_MAX)
714 return -EINVAL;
715
716 newpn->weight = temp;
717
718 return 0;
719}
720
721unsigned int blkcg_get_weight(struct blkio_cgroup *blkcg,
722 dev_t dev)
723{
724 struct blkio_policy_node *pn;
725
726 pn = blkio_policy_search_node(blkcg, dev);
727 if (pn)
728 return pn->weight;
729 else
730 return blkcg->weight;
731}
732EXPORT_SYMBOL_GPL(blkcg_get_weight);
733
734
735static int blkiocg_weight_device_write(struct cgroup *cgrp, struct cftype *cft,
736 const char *buffer)
737{
738 int ret = 0;
739 char *buf;
740 struct blkio_policy_node *newpn, *pn;
741 struct blkio_cgroup *blkcg;
742 struct blkio_group *blkg;
743 int keep_newpn = 0;
744 struct hlist_node *n;
745 struct blkio_policy_type *blkiop;
746
747 buf = kstrdup(buffer, GFP_KERNEL);
748 if (!buf)
749 return -ENOMEM;
750
751 newpn = kzalloc(sizeof(*newpn), GFP_KERNEL);
752 if (!newpn) {
753 ret = -ENOMEM;
754 goto free_buf;
755 }
756
757 ret = blkio_policy_parse_and_set(buf, newpn);
758 if (ret)
759 goto free_newpn;
760
761 blkcg = cgroup_to_blkio_cgroup(cgrp);
762
763 spin_lock_irq(&blkcg->lock);
764
765 pn = blkio_policy_search_node(blkcg, newpn->dev);
766 if (!pn) {
767 if (newpn->weight != 0) {
768 blkio_policy_insert_node(blkcg, newpn);
769 keep_newpn = 1;
770 }
771 spin_unlock_irq(&blkcg->lock);
772 goto update_io_group;
773 }
774
775 if (newpn->weight == 0) {
776 /* weight == 0 means deleteing a specific weight */
777 blkio_policy_delete_node(pn);
778 spin_unlock_irq(&blkcg->lock);
779 goto update_io_group;
780 }
781 spin_unlock_irq(&blkcg->lock);
782
783 pn->weight = newpn->weight;
784
785update_io_group:
786 /* update weight for each cfqg */
787 spin_lock(&blkio_list_lock);
788 spin_lock_irq(&blkcg->lock);
789
790 hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) {
791 if (newpn->dev == blkg->dev) {
792 list_for_each_entry(blkiop, &blkio_list, list)
793 blkiop->ops.blkio_update_group_weight_fn(blkg,
794 newpn->weight ?
795 newpn->weight :
796 blkcg->weight);
797 }
798 }
799
800 spin_unlock_irq(&blkcg->lock);
801 spin_unlock(&blkio_list_lock);
802
803free_newpn:
804 if (!keep_newpn)
805 kfree(newpn);
806free_buf:
807 kfree(buf);
808 return ret;
809}
810
811static int blkiocg_weight_device_read(struct cgroup *cgrp, struct cftype *cft,
812 struct seq_file *m)
813{
814 struct blkio_cgroup *blkcg;
815 struct blkio_policy_node *pn;
816
817 seq_printf(m, "dev\tweight\n");
818
819 blkcg = cgroup_to_blkio_cgroup(cgrp);
820 if (list_empty(&blkcg->policy_list))
821 goto out;
822
823 spin_lock_irq(&blkcg->lock);
824 list_for_each_entry(pn, &blkcg->policy_list, node) {
825 seq_printf(m, "%u:%u\t%u\n", MAJOR(pn->dev),
826 MINOR(pn->dev), pn->weight);
827 }
828 spin_unlock_irq(&blkcg->lock);
829
830out:
831 return 0;
832}
833
Vivek Goyal31e4c282009-12-03 12:59:42 -0500834struct cftype blkio_files[] = {
835 {
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800836 .name = "weight_device",
837 .read_seq_string = blkiocg_weight_device_read,
838 .write_string = blkiocg_weight_device_write,
839 .max_write_len = 256,
840 },
841 {
Vivek Goyal31e4c282009-12-03 12:59:42 -0500842 .name = "weight",
843 .read_u64 = blkiocg_weight_read,
844 .write_u64 = blkiocg_weight_write,
845 },
Vivek Goyal22084192009-12-03 12:59:49 -0500846 {
847 .name = "time",
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700848 .read_map = blkiocg_time_read,
Vivek Goyal22084192009-12-03 12:59:49 -0500849 },
850 {
851 .name = "sectors",
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700852 .read_map = blkiocg_sectors_read,
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700853 },
854 {
855 .name = "io_service_bytes",
856 .read_map = blkiocg_io_service_bytes_read,
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700857 },
858 {
859 .name = "io_serviced",
860 .read_map = blkiocg_io_serviced_read,
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700861 },
862 {
863 .name = "io_service_time",
864 .read_map = blkiocg_io_service_time_read,
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700865 },
866 {
867 .name = "io_wait_time",
868 .read_map = blkiocg_io_wait_time_read,
Divyesh Shah84c124d2010-04-09 08:31:19 +0200869 },
870 {
Divyesh Shah812d4022010-04-08 21:14:23 -0700871 .name = "io_merged",
872 .read_map = blkiocg_io_merged_read,
873 },
874 {
Divyesh Shahcdc11842010-04-08 21:15:10 -0700875 .name = "io_queued",
876 .read_map = blkiocg_io_queued_read,
877 },
878 {
Divyesh Shah84c124d2010-04-09 08:31:19 +0200879 .name = "reset_stats",
880 .write_u64 = blkiocg_reset_stats,
Vivek Goyal22084192009-12-03 12:59:49 -0500881 },
882#ifdef CONFIG_DEBUG_BLK_CGROUP
Divyesh Shahcdc11842010-04-08 21:15:10 -0700883 {
884 .name = "avg_queue_size",
885 .read_map = blkiocg_avg_queue_size_read,
886 },
887 {
Divyesh Shah812df482010-04-08 21:15:35 -0700888 .name = "group_wait_time",
889 .read_map = blkiocg_group_wait_time_read,
890 },
891 {
892 .name = "idle_time",
893 .read_map = blkiocg_idle_time_read,
894 },
895 {
896 .name = "empty_time",
897 .read_map = blkiocg_empty_time_read,
898 },
899 {
Vivek Goyal22084192009-12-03 12:59:49 -0500900 .name = "dequeue",
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700901 .read_map = blkiocg_dequeue_read,
Divyesh Shahcdc11842010-04-08 21:15:10 -0700902 },
Vivek Goyal22084192009-12-03 12:59:49 -0500903#endif
Vivek Goyal31e4c282009-12-03 12:59:42 -0500904};
905
906static int blkiocg_populate(struct cgroup_subsys *subsys, struct cgroup *cgroup)
907{
908 return cgroup_add_files(cgroup, subsys, blkio_files,
909 ARRAY_SIZE(blkio_files));
910}
911
912static void blkiocg_destroy(struct cgroup_subsys *subsys, struct cgroup *cgroup)
913{
914 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgroup);
Vivek Goyalb1c35762009-12-03 12:59:47 -0500915 unsigned long flags;
916 struct blkio_group *blkg;
917 void *key;
Vivek Goyal3e252062009-12-04 10:36:42 -0500918 struct blkio_policy_type *blkiop;
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800919 struct blkio_policy_node *pn, *pntmp;
Vivek Goyal31e4c282009-12-03 12:59:42 -0500920
Vivek Goyalb1c35762009-12-03 12:59:47 -0500921 rcu_read_lock();
922remove_entry:
923 spin_lock_irqsave(&blkcg->lock, flags);
924
925 if (hlist_empty(&blkcg->blkg_list)) {
926 spin_unlock_irqrestore(&blkcg->lock, flags);
927 goto done;
928 }
929
930 blkg = hlist_entry(blkcg->blkg_list.first, struct blkio_group,
931 blkcg_node);
932 key = rcu_dereference(blkg->key);
933 __blkiocg_del_blkio_group(blkg);
934
935 spin_unlock_irqrestore(&blkcg->lock, flags);
936
937 /*
938 * This blkio_group is being unlinked as associated cgroup is going
939 * away. Let all the IO controlling policies know about this event.
940 *
941 * Currently this is static call to one io controlling policy. Once
942 * we have more policies in place, we need some dynamic registration
943 * of callback function.
944 */
Vivek Goyal3e252062009-12-04 10:36:42 -0500945 spin_lock(&blkio_list_lock);
946 list_for_each_entry(blkiop, &blkio_list, list)
947 blkiop->ops.blkio_unlink_group_fn(key, blkg);
948 spin_unlock(&blkio_list_lock);
Vivek Goyalb1c35762009-12-03 12:59:47 -0500949 goto remove_entry;
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800950
Vivek Goyalb1c35762009-12-03 12:59:47 -0500951done:
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800952 list_for_each_entry_safe(pn, pntmp, &blkcg->policy_list, node) {
953 blkio_policy_delete_node(pn);
954 kfree(pn);
955 }
Vivek Goyal31e4c282009-12-03 12:59:42 -0500956 free_css_id(&blkio_subsys, &blkcg->css);
Vivek Goyalb1c35762009-12-03 12:59:47 -0500957 rcu_read_unlock();
Ben Blum67523c42010-03-10 15:22:11 -0800958 if (blkcg != &blkio_root_cgroup)
959 kfree(blkcg);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500960}
961
962static struct cgroup_subsys_state *
963blkiocg_create(struct cgroup_subsys *subsys, struct cgroup *cgroup)
964{
965 struct blkio_cgroup *blkcg, *parent_blkcg;
966
967 if (!cgroup->parent) {
968 blkcg = &blkio_root_cgroup;
969 goto done;
970 }
971
972 /* Currently we do not support hierarchy deeper than two level (0,1) */
973 parent_blkcg = cgroup_to_blkio_cgroup(cgroup->parent);
974 if (css_depth(&parent_blkcg->css) > 0)
975 return ERR_PTR(-EINVAL);
976
977 blkcg = kzalloc(sizeof(*blkcg), GFP_KERNEL);
978 if (!blkcg)
979 return ERR_PTR(-ENOMEM);
980
981 blkcg->weight = BLKIO_WEIGHT_DEFAULT;
982done:
983 spin_lock_init(&blkcg->lock);
984 INIT_HLIST_HEAD(&blkcg->blkg_list);
985
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800986 INIT_LIST_HEAD(&blkcg->policy_list);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500987 return &blkcg->css;
988}
989
990/*
991 * We cannot support shared io contexts, as we have no mean to support
992 * two tasks with the same ioc in two different groups without major rework
993 * of the main cic data structures. For now we allow a task to change
994 * its cgroup only if it's the only owner of its ioc.
995 */
996static int blkiocg_can_attach(struct cgroup_subsys *subsys,
997 struct cgroup *cgroup, struct task_struct *tsk,
998 bool threadgroup)
999{
1000 struct io_context *ioc;
1001 int ret = 0;
1002
1003 /* task_lock() is needed to avoid races with exit_io_context() */
1004 task_lock(tsk);
1005 ioc = tsk->io_context;
1006 if (ioc && atomic_read(&ioc->nr_tasks) > 1)
1007 ret = -EINVAL;
1008 task_unlock(tsk);
1009
1010 return ret;
1011}
1012
1013static void blkiocg_attach(struct cgroup_subsys *subsys, struct cgroup *cgroup,
1014 struct cgroup *prev, struct task_struct *tsk,
1015 bool threadgroup)
1016{
1017 struct io_context *ioc;
1018
1019 task_lock(tsk);
1020 ioc = tsk->io_context;
1021 if (ioc)
1022 ioc->cgroup_changed = 1;
1023 task_unlock(tsk);
1024}
1025
Vivek Goyal3e252062009-12-04 10:36:42 -05001026void blkio_policy_register(struct blkio_policy_type *blkiop)
1027{
1028 spin_lock(&blkio_list_lock);
1029 list_add_tail(&blkiop->list, &blkio_list);
1030 spin_unlock(&blkio_list_lock);
1031}
1032EXPORT_SYMBOL_GPL(blkio_policy_register);
1033
1034void blkio_policy_unregister(struct blkio_policy_type *blkiop)
1035{
1036 spin_lock(&blkio_list_lock);
1037 list_del_init(&blkiop->list);
1038 spin_unlock(&blkio_list_lock);
1039}
1040EXPORT_SYMBOL_GPL(blkio_policy_unregister);
Ben Blum67523c42010-03-10 15:22:11 -08001041
1042static int __init init_cgroup_blkio(void)
1043{
1044 return cgroup_load_subsys(&blkio_subsys);
1045}
1046
1047static void __exit exit_cgroup_blkio(void)
1048{
1049 cgroup_unload_subsys(&blkio_subsys);
1050}
1051
1052module_init(init_cgroup_blkio);
1053module_exit(exit_cgroup_blkio);
1054MODULE_LICENSE("GPL");