blob: 80c1261a7d38308e20d95412d476f0f8d509ca99 [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 Shah84c124d2010-04-09 08:31:19 +020089void blkio_group_init(struct blkio_group *blkg)
90{
91 spin_lock_init(&blkg->stats_lock);
92}
93EXPORT_SYMBOL_GPL(blkio_group_init);
94
Divyesh Shah91952912010-04-01 15:01:41 -070095/*
96 * Add to the appropriate stat variable depending on the request type.
97 * This should be called with the blkg->stats_lock held.
98 */
Divyesh Shah84c124d2010-04-09 08:31:19 +020099static void blkio_add_stat(uint64_t *stat, uint64_t add, bool direction,
100 bool sync)
Divyesh Shah91952912010-04-01 15:01:41 -0700101{
Divyesh Shah84c124d2010-04-09 08:31:19 +0200102 if (direction)
103 stat[BLKIO_STAT_WRITE] += add;
Divyesh Shah91952912010-04-01 15:01:41 -0700104 else
Divyesh Shah84c124d2010-04-09 08:31:19 +0200105 stat[BLKIO_STAT_READ] += add;
106 if (sync)
107 stat[BLKIO_STAT_SYNC] += add;
Divyesh Shah91952912010-04-01 15:01:41 -0700108 else
Divyesh Shah84c124d2010-04-09 08:31:19 +0200109 stat[BLKIO_STAT_ASYNC] += add;
Divyesh Shah91952912010-04-01 15:01:41 -0700110}
111
Divyesh Shahcdc11842010-04-08 21:15:10 -0700112/*
113 * Decrements the appropriate stat variable if non-zero depending on the
114 * request type. Panics on value being zero.
115 * This should be called with the blkg->stats_lock held.
116 */
117static void blkio_check_and_dec_stat(uint64_t *stat, bool direction, bool sync)
118{
119 if (direction) {
120 BUG_ON(stat[BLKIO_STAT_WRITE] == 0);
121 stat[BLKIO_STAT_WRITE]--;
122 } else {
123 BUG_ON(stat[BLKIO_STAT_READ] == 0);
124 stat[BLKIO_STAT_READ]--;
125 }
126 if (sync) {
127 BUG_ON(stat[BLKIO_STAT_SYNC] == 0);
128 stat[BLKIO_STAT_SYNC]--;
129 } else {
130 BUG_ON(stat[BLKIO_STAT_ASYNC] == 0);
131 stat[BLKIO_STAT_ASYNC]--;
132 }
133}
134
135#ifdef CONFIG_DEBUG_BLK_CGROUP
Divyesh Shah812df482010-04-08 21:15:35 -0700136/* This should be called with the blkg->stats_lock held. */
137static void blkio_set_start_group_wait_time(struct blkio_group *blkg,
138 struct blkio_group *curr_blkg)
139{
140 if (blkio_blkg_waiting(&blkg->stats))
141 return;
142 if (blkg == curr_blkg)
143 return;
144 blkg->stats.start_group_wait_time = sched_clock();
145 blkio_mark_blkg_waiting(&blkg->stats);
146}
147
148/* This should be called with the blkg->stats_lock held. */
149static void blkio_update_group_wait_time(struct blkio_group_stats *stats)
150{
151 unsigned long long now;
152
153 if (!blkio_blkg_waiting(stats))
154 return;
155
156 now = sched_clock();
157 if (time_after64(now, stats->start_group_wait_time))
158 stats->group_wait_time += now - stats->start_group_wait_time;
159 blkio_clear_blkg_waiting(stats);
160}
161
162/* This should be called with the blkg->stats_lock held. */
163static void blkio_end_empty_time(struct blkio_group_stats *stats)
164{
165 unsigned long long now;
166
167 if (!blkio_blkg_empty(stats))
168 return;
169
170 now = sched_clock();
171 if (time_after64(now, stats->start_empty_time))
172 stats->empty_time += now - stats->start_empty_time;
173 blkio_clear_blkg_empty(stats);
174}
175
176void blkiocg_update_set_idle_time_stats(struct blkio_group *blkg)
177{
178 unsigned long flags;
179
180 spin_lock_irqsave(&blkg->stats_lock, flags);
181 BUG_ON(blkio_blkg_idling(&blkg->stats));
182 blkg->stats.start_idle_time = sched_clock();
183 blkio_mark_blkg_idling(&blkg->stats);
184 spin_unlock_irqrestore(&blkg->stats_lock, flags);
185}
186EXPORT_SYMBOL_GPL(blkiocg_update_set_idle_time_stats);
187
188void blkiocg_update_idle_time_stats(struct blkio_group *blkg)
189{
190 unsigned long flags;
191 unsigned long long now;
192 struct blkio_group_stats *stats;
193
194 spin_lock_irqsave(&blkg->stats_lock, flags);
195 stats = &blkg->stats;
196 if (blkio_blkg_idling(stats)) {
197 now = sched_clock();
198 if (time_after64(now, stats->start_idle_time))
199 stats->idle_time += now - stats->start_idle_time;
200 blkio_clear_blkg_idling(stats);
201 }
202 spin_unlock_irqrestore(&blkg->stats_lock, flags);
203}
204EXPORT_SYMBOL_GPL(blkiocg_update_idle_time_stats);
205
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200206void blkiocg_update_avg_queue_size_stats(struct blkio_group *blkg)
Divyesh Shahcdc11842010-04-08 21:15:10 -0700207{
208 unsigned long flags;
209 struct blkio_group_stats *stats;
210
211 spin_lock_irqsave(&blkg->stats_lock, flags);
212 stats = &blkg->stats;
213 stats->avg_queue_size_sum +=
214 stats->stat_arr[BLKIO_STAT_QUEUED][BLKIO_STAT_READ] +
215 stats->stat_arr[BLKIO_STAT_QUEUED][BLKIO_STAT_WRITE];
216 stats->avg_queue_size_samples++;
Divyesh Shah812df482010-04-08 21:15:35 -0700217 blkio_update_group_wait_time(stats);
Divyesh Shahcdc11842010-04-08 21:15:10 -0700218 spin_unlock_irqrestore(&blkg->stats_lock, flags);
219}
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200220EXPORT_SYMBOL_GPL(blkiocg_update_avg_queue_size_stats);
221
Divyesh Shah28baf442010-04-14 11:22:38 +0200222void blkiocg_set_start_empty_time(struct blkio_group *blkg, bool ignore)
223{
224 unsigned long flags;
225 struct blkio_group_stats *stats;
226
227 spin_lock_irqsave(&blkg->stats_lock, flags);
228 stats = &blkg->stats;
229
230 if (stats->stat_arr[BLKIO_STAT_QUEUED][BLKIO_STAT_READ] ||
231 stats->stat_arr[BLKIO_STAT_QUEUED][BLKIO_STAT_WRITE]) {
232 spin_unlock_irqrestore(&blkg->stats_lock, flags);
233 return;
234 }
235
236 /*
237 * If ignore is set, we do not panic on the empty flag being set
238 * already. This is to avoid cases where there are superfluous timeslice
239 * complete events (for eg., forced_dispatch in CFQ) when no IOs are
240 * served which could result in triggering the empty check incorrectly.
241 */
242 BUG_ON(!ignore && blkio_blkg_empty(stats));
243 stats->start_empty_time = sched_clock();
244 blkio_mark_blkg_empty(stats);
245 spin_unlock_irqrestore(&blkg->stats_lock, flags);
246}
247EXPORT_SYMBOL_GPL(blkiocg_set_start_empty_time);
248
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200249void blkiocg_update_dequeue_stats(struct blkio_group *blkg,
250 unsigned long dequeue)
251{
252 blkg->stats.dequeue += dequeue;
253}
254EXPORT_SYMBOL_GPL(blkiocg_update_dequeue_stats);
Divyesh Shah812df482010-04-08 21:15:35 -0700255#else
256static inline void blkio_set_start_group_wait_time(struct blkio_group *blkg,
257 struct blkio_group *curr_blkg) {}
258static inline void blkio_end_empty_time(struct blkio_group_stats *stats) {}
Divyesh Shahcdc11842010-04-08 21:15:10 -0700259#endif
260
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200261void blkiocg_update_io_add_stats(struct blkio_group *blkg,
Divyesh Shahcdc11842010-04-08 21:15:10 -0700262 struct blkio_group *curr_blkg, bool direction,
263 bool sync)
264{
265 unsigned long flags;
266
267 spin_lock_irqsave(&blkg->stats_lock, flags);
268 blkio_add_stat(blkg->stats.stat_arr[BLKIO_STAT_QUEUED], 1, direction,
269 sync);
Divyesh Shah812df482010-04-08 21:15:35 -0700270 blkio_end_empty_time(&blkg->stats);
271 blkio_set_start_group_wait_time(blkg, curr_blkg);
Divyesh Shahcdc11842010-04-08 21:15:10 -0700272 spin_unlock_irqrestore(&blkg->stats_lock, flags);
273}
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200274EXPORT_SYMBOL_GPL(blkiocg_update_io_add_stats);
Divyesh Shahcdc11842010-04-08 21:15:10 -0700275
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200276void blkiocg_update_io_remove_stats(struct blkio_group *blkg,
Divyesh Shahcdc11842010-04-08 21:15:10 -0700277 bool direction, bool sync)
278{
279 unsigned long flags;
280
281 spin_lock_irqsave(&blkg->stats_lock, flags);
282 blkio_check_and_dec_stat(blkg->stats.stat_arr[BLKIO_STAT_QUEUED],
283 direction, sync);
284 spin_unlock_irqrestore(&blkg->stats_lock, flags);
285}
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200286EXPORT_SYMBOL_GPL(blkiocg_update_io_remove_stats);
Divyesh Shahcdc11842010-04-08 21:15:10 -0700287
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700288void blkiocg_update_timeslice_used(struct blkio_group *blkg, unsigned long time)
Vivek Goyal22084192009-12-03 12:59:49 -0500289{
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700290 unsigned long flags;
291
292 spin_lock_irqsave(&blkg->stats_lock, flags);
293 blkg->stats.time += time;
294 spin_unlock_irqrestore(&blkg->stats_lock, flags);
Vivek Goyal22084192009-12-03 12:59:49 -0500295}
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700296EXPORT_SYMBOL_GPL(blkiocg_update_timeslice_used);
Vivek Goyal22084192009-12-03 12:59:49 -0500297
Divyesh Shah84c124d2010-04-09 08:31:19 +0200298void blkiocg_update_dispatch_stats(struct blkio_group *blkg,
299 uint64_t bytes, bool direction, bool sync)
Divyesh Shah91952912010-04-01 15:01:41 -0700300{
301 struct blkio_group_stats *stats;
302 unsigned long flags;
303
304 spin_lock_irqsave(&blkg->stats_lock, flags);
305 stats = &blkg->stats;
Divyesh Shah84c124d2010-04-09 08:31:19 +0200306 stats->sectors += bytes >> 9;
307 blkio_add_stat(stats->stat_arr[BLKIO_STAT_SERVICED], 1, direction,
308 sync);
309 blkio_add_stat(stats->stat_arr[BLKIO_STAT_SERVICE_BYTES], bytes,
310 direction, sync);
Divyesh Shah91952912010-04-01 15:01:41 -0700311 spin_unlock_irqrestore(&blkg->stats_lock, flags);
312}
Divyesh Shah84c124d2010-04-09 08:31:19 +0200313EXPORT_SYMBOL_GPL(blkiocg_update_dispatch_stats);
Divyesh Shah91952912010-04-01 15:01:41 -0700314
Divyesh Shah84c124d2010-04-09 08:31:19 +0200315void blkiocg_update_completion_stats(struct blkio_group *blkg,
316 uint64_t start_time, uint64_t io_start_time, bool direction, bool sync)
Divyesh Shah91952912010-04-01 15:01:41 -0700317{
318 struct blkio_group_stats *stats;
319 unsigned long flags;
320 unsigned long long now = sched_clock();
321
322 spin_lock_irqsave(&blkg->stats_lock, flags);
323 stats = &blkg->stats;
Divyesh Shah84c124d2010-04-09 08:31:19 +0200324 if (time_after64(now, io_start_time))
325 blkio_add_stat(stats->stat_arr[BLKIO_STAT_SERVICE_TIME],
326 now - io_start_time, direction, sync);
327 if (time_after64(io_start_time, start_time))
328 blkio_add_stat(stats->stat_arr[BLKIO_STAT_WAIT_TIME],
329 io_start_time - start_time, direction, sync);
Divyesh Shah91952912010-04-01 15:01:41 -0700330 spin_unlock_irqrestore(&blkg->stats_lock, flags);
331}
Divyesh Shah84c124d2010-04-09 08:31:19 +0200332EXPORT_SYMBOL_GPL(blkiocg_update_completion_stats);
Divyesh Shah91952912010-04-01 15:01:41 -0700333
Divyesh Shah812d4022010-04-08 21:14:23 -0700334void blkiocg_update_io_merged_stats(struct blkio_group *blkg, bool direction,
335 bool sync)
336{
337 unsigned long flags;
338
339 spin_lock_irqsave(&blkg->stats_lock, flags);
340 blkio_add_stat(blkg->stats.stat_arr[BLKIO_STAT_MERGED], 1, direction,
341 sync);
342 spin_unlock_irqrestore(&blkg->stats_lock, flags);
343}
344EXPORT_SYMBOL_GPL(blkiocg_update_io_merged_stats);
345
Vivek Goyal31e4c282009-12-03 12:59:42 -0500346void blkiocg_add_blkio_group(struct blkio_cgroup *blkcg,
Vivek Goyal22084192009-12-03 12:59:49 -0500347 struct blkio_group *blkg, void *key, dev_t dev)
Vivek Goyal31e4c282009-12-03 12:59:42 -0500348{
349 unsigned long flags;
350
351 spin_lock_irqsave(&blkcg->lock, flags);
352 rcu_assign_pointer(blkg->key, key);
Vivek Goyalb1c35762009-12-03 12:59:47 -0500353 blkg->blkcg_id = css_id(&blkcg->css);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500354 hlist_add_head_rcu(&blkg->blkcg_node, &blkcg->blkg_list);
355 spin_unlock_irqrestore(&blkcg->lock, flags);
Vivek Goyal2868ef72009-12-03 12:59:48 -0500356#ifdef CONFIG_DEBUG_BLK_CGROUP
357 /* Need to take css reference ? */
358 cgroup_path(blkcg->css.cgroup, blkg->path, sizeof(blkg->path));
359#endif
Vivek Goyal22084192009-12-03 12:59:49 -0500360 blkg->dev = dev;
Vivek Goyal31e4c282009-12-03 12:59:42 -0500361}
Vivek Goyal9d6a9862009-12-04 10:36:41 -0500362EXPORT_SYMBOL_GPL(blkiocg_add_blkio_group);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500363
Vivek Goyalb1c35762009-12-03 12:59:47 -0500364static void __blkiocg_del_blkio_group(struct blkio_group *blkg)
365{
366 hlist_del_init_rcu(&blkg->blkcg_node);
367 blkg->blkcg_id = 0;
368}
369
370/*
371 * returns 0 if blkio_group was still on cgroup list. Otherwise returns 1
372 * indicating that blk_group was unhashed by the time we got to it.
373 */
Vivek Goyal31e4c282009-12-03 12:59:42 -0500374int blkiocg_del_blkio_group(struct blkio_group *blkg)
375{
Vivek Goyalb1c35762009-12-03 12:59:47 -0500376 struct blkio_cgroup *blkcg;
377 unsigned long flags;
378 struct cgroup_subsys_state *css;
379 int ret = 1;
380
381 rcu_read_lock();
382 css = css_lookup(&blkio_subsys, blkg->blkcg_id);
383 if (!css)
384 goto out;
385
386 blkcg = container_of(css, struct blkio_cgroup, css);
387 spin_lock_irqsave(&blkcg->lock, flags);
388 if (!hlist_unhashed(&blkg->blkcg_node)) {
389 __blkiocg_del_blkio_group(blkg);
390 ret = 0;
391 }
392 spin_unlock_irqrestore(&blkcg->lock, flags);
393out:
394 rcu_read_unlock();
395 return ret;
Vivek Goyal31e4c282009-12-03 12:59:42 -0500396}
Vivek Goyal9d6a9862009-12-04 10:36:41 -0500397EXPORT_SYMBOL_GPL(blkiocg_del_blkio_group);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500398
399/* called under rcu_read_lock(). */
400struct blkio_group *blkiocg_lookup_group(struct blkio_cgroup *blkcg, void *key)
401{
402 struct blkio_group *blkg;
403 struct hlist_node *n;
404 void *__key;
405
406 hlist_for_each_entry_rcu(blkg, n, &blkcg->blkg_list, blkcg_node) {
407 __key = blkg->key;
408 if (__key == key)
409 return blkg;
410 }
411
412 return NULL;
413}
Vivek Goyal9d6a9862009-12-04 10:36:41 -0500414EXPORT_SYMBOL_GPL(blkiocg_lookup_group);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500415
416#define SHOW_FUNCTION(__VAR) \
417static u64 blkiocg_##__VAR##_read(struct cgroup *cgroup, \
418 struct cftype *cftype) \
419{ \
420 struct blkio_cgroup *blkcg; \
421 \
422 blkcg = cgroup_to_blkio_cgroup(cgroup); \
423 return (u64)blkcg->__VAR; \
424}
425
426SHOW_FUNCTION(weight);
427#undef SHOW_FUNCTION
428
429static int
430blkiocg_weight_write(struct cgroup *cgroup, struct cftype *cftype, u64 val)
431{
432 struct blkio_cgroup *blkcg;
Vivek Goyalf8d461d2009-12-03 12:59:52 -0500433 struct blkio_group *blkg;
434 struct hlist_node *n;
Vivek Goyal3e252062009-12-04 10:36:42 -0500435 struct blkio_policy_type *blkiop;
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800436 struct blkio_policy_node *pn;
Vivek Goyal31e4c282009-12-03 12:59:42 -0500437
438 if (val < BLKIO_WEIGHT_MIN || val > BLKIO_WEIGHT_MAX)
439 return -EINVAL;
440
441 blkcg = cgroup_to_blkio_cgroup(cgroup);
Gui Jianfengbcf4dd42010-02-01 09:58:54 +0100442 spin_lock(&blkio_list_lock);
Vivek Goyalf8d461d2009-12-03 12:59:52 -0500443 spin_lock_irq(&blkcg->lock);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500444 blkcg->weight = (unsigned int)val;
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800445
Vivek Goyal3e252062009-12-04 10:36:42 -0500446 hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) {
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800447 pn = blkio_policy_search_node(blkcg, blkg->dev);
448
449 if (pn)
450 continue;
451
Vivek Goyal3e252062009-12-04 10:36:42 -0500452 list_for_each_entry(blkiop, &blkio_list, list)
453 blkiop->ops.blkio_update_group_weight_fn(blkg,
454 blkcg->weight);
Vivek Goyal3e252062009-12-04 10:36:42 -0500455 }
Vivek Goyalf8d461d2009-12-03 12:59:52 -0500456 spin_unlock_irq(&blkcg->lock);
Gui Jianfengbcf4dd42010-02-01 09:58:54 +0100457 spin_unlock(&blkio_list_lock);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500458 return 0;
459}
460
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700461static int
Divyesh Shah84c124d2010-04-09 08:31:19 +0200462blkiocg_reset_stats(struct cgroup *cgroup, struct cftype *cftype, u64 val)
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700463{
464 struct blkio_cgroup *blkcg;
465 struct blkio_group *blkg;
Divyesh Shah812df482010-04-08 21:15:35 -0700466 struct blkio_group_stats *stats;
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700467 struct hlist_node *n;
Divyesh Shahcdc11842010-04-08 21:15:10 -0700468 uint64_t queued[BLKIO_STAT_TOTAL];
469 int i;
Divyesh Shah812df482010-04-08 21:15:35 -0700470#ifdef CONFIG_DEBUG_BLK_CGROUP
471 bool idling, waiting, empty;
472 unsigned long long now = sched_clock();
473#endif
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700474
475 blkcg = cgroup_to_blkio_cgroup(cgroup);
476 spin_lock_irq(&blkcg->lock);
477 hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) {
478 spin_lock(&blkg->stats_lock);
Divyesh Shah812df482010-04-08 21:15:35 -0700479 stats = &blkg->stats;
480#ifdef CONFIG_DEBUG_BLK_CGROUP
481 idling = blkio_blkg_idling(stats);
482 waiting = blkio_blkg_waiting(stats);
483 empty = blkio_blkg_empty(stats);
484#endif
Divyesh Shahcdc11842010-04-08 21:15:10 -0700485 for (i = 0; i < BLKIO_STAT_TOTAL; i++)
Divyesh Shah812df482010-04-08 21:15:35 -0700486 queued[i] = stats->stat_arr[BLKIO_STAT_QUEUED][i];
487 memset(stats, 0, sizeof(struct blkio_group_stats));
Divyesh Shahcdc11842010-04-08 21:15:10 -0700488 for (i = 0; i < BLKIO_STAT_TOTAL; i++)
Divyesh Shah812df482010-04-08 21:15:35 -0700489 stats->stat_arr[BLKIO_STAT_QUEUED][i] = queued[i];
490#ifdef CONFIG_DEBUG_BLK_CGROUP
491 if (idling) {
492 blkio_mark_blkg_idling(stats);
493 stats->start_idle_time = now;
494 }
495 if (waiting) {
496 blkio_mark_blkg_waiting(stats);
497 stats->start_group_wait_time = now;
498 }
499 if (empty) {
500 blkio_mark_blkg_empty(stats);
501 stats->start_empty_time = now;
502 }
503#endif
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700504 spin_unlock(&blkg->stats_lock);
505 }
506 spin_unlock_irq(&blkcg->lock);
507 return 0;
508}
509
Divyesh Shah84c124d2010-04-09 08:31:19 +0200510static void blkio_get_key_name(enum stat_sub_type type, dev_t dev, char *str,
511 int chars_left, bool diskname_only)
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700512{
Divyesh Shah84c124d2010-04-09 08:31:19 +0200513 snprintf(str, chars_left, "%d:%d", MAJOR(dev), MINOR(dev));
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700514 chars_left -= strlen(str);
515 if (chars_left <= 0) {
516 printk(KERN_WARNING
517 "Possibly incorrect cgroup stat display format");
518 return;
519 }
Divyesh Shah84c124d2010-04-09 08:31:19 +0200520 if (diskname_only)
521 return;
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700522 switch (type) {
Divyesh Shah84c124d2010-04-09 08:31:19 +0200523 case BLKIO_STAT_READ:
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700524 strlcat(str, " Read", chars_left);
525 break;
Divyesh Shah84c124d2010-04-09 08:31:19 +0200526 case BLKIO_STAT_WRITE:
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700527 strlcat(str, " Write", chars_left);
528 break;
Divyesh Shah84c124d2010-04-09 08:31:19 +0200529 case BLKIO_STAT_SYNC:
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700530 strlcat(str, " Sync", chars_left);
531 break;
Divyesh Shah84c124d2010-04-09 08:31:19 +0200532 case BLKIO_STAT_ASYNC:
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700533 strlcat(str, " Async", chars_left);
534 break;
Divyesh Shah84c124d2010-04-09 08:31:19 +0200535 case BLKIO_STAT_TOTAL:
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700536 strlcat(str, " Total", chars_left);
537 break;
538 default:
539 strlcat(str, " Invalid", chars_left);
540 }
541}
542
Divyesh Shah84c124d2010-04-09 08:31:19 +0200543static uint64_t blkio_fill_stat(char *str, int chars_left, uint64_t val,
544 struct cgroup_map_cb *cb, dev_t dev)
545{
546 blkio_get_key_name(0, dev, str, chars_left, true);
547 cb->fill(cb, str, val);
548 return val;
549}
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700550
Divyesh Shah84c124d2010-04-09 08:31:19 +0200551/* This should be called with blkg->stats_lock held */
552static uint64_t blkio_get_stat(struct blkio_group *blkg,
553 struct cgroup_map_cb *cb, dev_t dev, enum stat_type type)
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700554{
555 uint64_t disk_total;
556 char key_str[MAX_KEY_LEN];
Divyesh Shah84c124d2010-04-09 08:31:19 +0200557 enum stat_sub_type sub_type;
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700558
Divyesh Shah84c124d2010-04-09 08:31:19 +0200559 if (type == BLKIO_STAT_TIME)
560 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
561 blkg->stats.time, cb, dev);
562 if (type == BLKIO_STAT_SECTORS)
563 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
564 blkg->stats.sectors, cb, dev);
565#ifdef CONFIG_DEBUG_BLK_CGROUP
Divyesh Shahcdc11842010-04-08 21:15:10 -0700566 if (type == BLKIO_STAT_AVG_QUEUE_SIZE) {
567 uint64_t sum = blkg->stats.avg_queue_size_sum;
568 uint64_t samples = blkg->stats.avg_queue_size_samples;
569 if (samples)
570 do_div(sum, samples);
571 else
572 sum = 0;
573 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1, sum, cb, dev);
574 }
Divyesh Shah812df482010-04-08 21:15:35 -0700575 if (type == BLKIO_STAT_GROUP_WAIT_TIME)
576 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
577 blkg->stats.group_wait_time, cb, dev);
578 if (type == BLKIO_STAT_IDLE_TIME)
579 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
580 blkg->stats.idle_time, cb, dev);
581 if (type == BLKIO_STAT_EMPTY_TIME)
582 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
583 blkg->stats.empty_time, cb, dev);
Divyesh Shah84c124d2010-04-09 08:31:19 +0200584 if (type == BLKIO_STAT_DEQUEUE)
585 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
586 blkg->stats.dequeue, cb, dev);
587#endif
588
589 for (sub_type = BLKIO_STAT_READ; sub_type < BLKIO_STAT_TOTAL;
590 sub_type++) {
591 blkio_get_key_name(sub_type, dev, key_str, MAX_KEY_LEN, false);
592 cb->fill(cb, key_str, blkg->stats.stat_arr[type][sub_type]);
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700593 }
Divyesh Shah84c124d2010-04-09 08:31:19 +0200594 disk_total = blkg->stats.stat_arr[type][BLKIO_STAT_READ] +
595 blkg->stats.stat_arr[type][BLKIO_STAT_WRITE];
596 blkio_get_key_name(BLKIO_STAT_TOTAL, dev, key_str, MAX_KEY_LEN, false);
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700597 cb->fill(cb, key_str, disk_total);
598 return disk_total;
599}
600
Divyesh Shah84c124d2010-04-09 08:31:19 +0200601#define SHOW_FUNCTION_PER_GROUP(__VAR, type, show_total) \
Vivek Goyal22084192009-12-03 12:59:49 -0500602static int blkiocg_##__VAR##_read(struct cgroup *cgroup, \
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700603 struct cftype *cftype, struct cgroup_map_cb *cb) \
Vivek Goyal22084192009-12-03 12:59:49 -0500604{ \
605 struct blkio_cgroup *blkcg; \
606 struct blkio_group *blkg; \
607 struct hlist_node *n; \
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700608 uint64_t cgroup_total = 0; \
Vivek Goyal22084192009-12-03 12:59:49 -0500609 \
610 if (!cgroup_lock_live_group(cgroup)) \
611 return -ENODEV; \
612 \
613 blkcg = cgroup_to_blkio_cgroup(cgroup); \
614 rcu_read_lock(); \
615 hlist_for_each_entry_rcu(blkg, n, &blkcg->blkg_list, blkcg_node) {\
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700616 if (blkg->dev) { \
617 spin_lock_irq(&blkg->stats_lock); \
Divyesh Shah84c124d2010-04-09 08:31:19 +0200618 cgroup_total += blkio_get_stat(blkg, cb, \
619 blkg->dev, type); \
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700620 spin_unlock_irq(&blkg->stats_lock); \
621 } \
Vivek Goyal22084192009-12-03 12:59:49 -0500622 } \
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700623 if (show_total) \
624 cb->fill(cb, "Total", cgroup_total); \
Vivek Goyal22084192009-12-03 12:59:49 -0500625 rcu_read_unlock(); \
626 cgroup_unlock(); \
627 return 0; \
628}
629
Divyesh Shah84c124d2010-04-09 08:31:19 +0200630SHOW_FUNCTION_PER_GROUP(time, BLKIO_STAT_TIME, 0);
631SHOW_FUNCTION_PER_GROUP(sectors, BLKIO_STAT_SECTORS, 0);
632SHOW_FUNCTION_PER_GROUP(io_service_bytes, BLKIO_STAT_SERVICE_BYTES, 1);
633SHOW_FUNCTION_PER_GROUP(io_serviced, BLKIO_STAT_SERVICED, 1);
634SHOW_FUNCTION_PER_GROUP(io_service_time, BLKIO_STAT_SERVICE_TIME, 1);
635SHOW_FUNCTION_PER_GROUP(io_wait_time, BLKIO_STAT_WAIT_TIME, 1);
Divyesh Shah812d4022010-04-08 21:14:23 -0700636SHOW_FUNCTION_PER_GROUP(io_merged, BLKIO_STAT_MERGED, 1);
Divyesh Shahcdc11842010-04-08 21:15:10 -0700637SHOW_FUNCTION_PER_GROUP(io_queued, BLKIO_STAT_QUEUED, 1);
Vivek Goyal22084192009-12-03 12:59:49 -0500638#ifdef CONFIG_DEBUG_BLK_CGROUP
Divyesh Shah84c124d2010-04-09 08:31:19 +0200639SHOW_FUNCTION_PER_GROUP(dequeue, BLKIO_STAT_DEQUEUE, 0);
Divyesh Shahcdc11842010-04-08 21:15:10 -0700640SHOW_FUNCTION_PER_GROUP(avg_queue_size, BLKIO_STAT_AVG_QUEUE_SIZE, 0);
Divyesh Shah812df482010-04-08 21:15:35 -0700641SHOW_FUNCTION_PER_GROUP(group_wait_time, BLKIO_STAT_GROUP_WAIT_TIME, 0);
642SHOW_FUNCTION_PER_GROUP(idle_time, BLKIO_STAT_IDLE_TIME, 0);
643SHOW_FUNCTION_PER_GROUP(empty_time, BLKIO_STAT_EMPTY_TIME, 0);
Vivek Goyal22084192009-12-03 12:59:49 -0500644#endif
645#undef SHOW_FUNCTION_PER_GROUP
646
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800647static int blkio_check_dev_num(dev_t dev)
648{
649 int part = 0;
650 struct gendisk *disk;
651
652 disk = get_gendisk(dev, &part);
653 if (!disk || part)
654 return -ENODEV;
655
656 return 0;
657}
658
659static int blkio_policy_parse_and_set(char *buf,
660 struct blkio_policy_node *newpn)
661{
662 char *s[4], *p, *major_s = NULL, *minor_s = NULL;
663 int ret;
664 unsigned long major, minor, temp;
665 int i = 0;
666 dev_t dev;
667
668 memset(s, 0, sizeof(s));
669
670 while ((p = strsep(&buf, " ")) != NULL) {
671 if (!*p)
672 continue;
673
674 s[i++] = p;
675
676 /* Prevent from inputing too many things */
677 if (i == 3)
678 break;
679 }
680
681 if (i != 2)
682 return -EINVAL;
683
684 p = strsep(&s[0], ":");
685 if (p != NULL)
686 major_s = p;
687 else
688 return -EINVAL;
689
690 minor_s = s[0];
691 if (!minor_s)
692 return -EINVAL;
693
694 ret = strict_strtoul(major_s, 10, &major);
695 if (ret)
696 return -EINVAL;
697
698 ret = strict_strtoul(minor_s, 10, &minor);
699 if (ret)
700 return -EINVAL;
701
702 dev = MKDEV(major, minor);
703
704 ret = blkio_check_dev_num(dev);
705 if (ret)
706 return ret;
707
708 newpn->dev = dev;
709
710 if (s[1] == NULL)
711 return -EINVAL;
712
713 ret = strict_strtoul(s[1], 10, &temp);
714 if (ret || (temp < BLKIO_WEIGHT_MIN && temp > 0) ||
715 temp > BLKIO_WEIGHT_MAX)
716 return -EINVAL;
717
718 newpn->weight = temp;
719
720 return 0;
721}
722
723unsigned int blkcg_get_weight(struct blkio_cgroup *blkcg,
724 dev_t dev)
725{
726 struct blkio_policy_node *pn;
727
728 pn = blkio_policy_search_node(blkcg, dev);
729 if (pn)
730 return pn->weight;
731 else
732 return blkcg->weight;
733}
734EXPORT_SYMBOL_GPL(blkcg_get_weight);
735
736
737static int blkiocg_weight_device_write(struct cgroup *cgrp, struct cftype *cft,
738 const char *buffer)
739{
740 int ret = 0;
741 char *buf;
742 struct blkio_policy_node *newpn, *pn;
743 struct blkio_cgroup *blkcg;
744 struct blkio_group *blkg;
745 int keep_newpn = 0;
746 struct hlist_node *n;
747 struct blkio_policy_type *blkiop;
748
749 buf = kstrdup(buffer, GFP_KERNEL);
750 if (!buf)
751 return -ENOMEM;
752
753 newpn = kzalloc(sizeof(*newpn), GFP_KERNEL);
754 if (!newpn) {
755 ret = -ENOMEM;
756 goto free_buf;
757 }
758
759 ret = blkio_policy_parse_and_set(buf, newpn);
760 if (ret)
761 goto free_newpn;
762
763 blkcg = cgroup_to_blkio_cgroup(cgrp);
764
765 spin_lock_irq(&blkcg->lock);
766
767 pn = blkio_policy_search_node(blkcg, newpn->dev);
768 if (!pn) {
769 if (newpn->weight != 0) {
770 blkio_policy_insert_node(blkcg, newpn);
771 keep_newpn = 1;
772 }
773 spin_unlock_irq(&blkcg->lock);
774 goto update_io_group;
775 }
776
777 if (newpn->weight == 0) {
778 /* weight == 0 means deleteing a specific weight */
779 blkio_policy_delete_node(pn);
780 spin_unlock_irq(&blkcg->lock);
781 goto update_io_group;
782 }
783 spin_unlock_irq(&blkcg->lock);
784
785 pn->weight = newpn->weight;
786
787update_io_group:
788 /* update weight for each cfqg */
789 spin_lock(&blkio_list_lock);
790 spin_lock_irq(&blkcg->lock);
791
792 hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) {
793 if (newpn->dev == blkg->dev) {
794 list_for_each_entry(blkiop, &blkio_list, list)
795 blkiop->ops.blkio_update_group_weight_fn(blkg,
796 newpn->weight ?
797 newpn->weight :
798 blkcg->weight);
799 }
800 }
801
802 spin_unlock_irq(&blkcg->lock);
803 spin_unlock(&blkio_list_lock);
804
805free_newpn:
806 if (!keep_newpn)
807 kfree(newpn);
808free_buf:
809 kfree(buf);
810 return ret;
811}
812
813static int blkiocg_weight_device_read(struct cgroup *cgrp, struct cftype *cft,
814 struct seq_file *m)
815{
816 struct blkio_cgroup *blkcg;
817 struct blkio_policy_node *pn;
818
819 seq_printf(m, "dev\tweight\n");
820
821 blkcg = cgroup_to_blkio_cgroup(cgrp);
822 if (list_empty(&blkcg->policy_list))
823 goto out;
824
825 spin_lock_irq(&blkcg->lock);
826 list_for_each_entry(pn, &blkcg->policy_list, node) {
827 seq_printf(m, "%u:%u\t%u\n", MAJOR(pn->dev),
828 MINOR(pn->dev), pn->weight);
829 }
830 spin_unlock_irq(&blkcg->lock);
831
832out:
833 return 0;
834}
835
Vivek Goyal31e4c282009-12-03 12:59:42 -0500836struct cftype blkio_files[] = {
837 {
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800838 .name = "weight_device",
839 .read_seq_string = blkiocg_weight_device_read,
840 .write_string = blkiocg_weight_device_write,
841 .max_write_len = 256,
842 },
843 {
Vivek Goyal31e4c282009-12-03 12:59:42 -0500844 .name = "weight",
845 .read_u64 = blkiocg_weight_read,
846 .write_u64 = blkiocg_weight_write,
847 },
Vivek Goyal22084192009-12-03 12:59:49 -0500848 {
849 .name = "time",
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700850 .read_map = blkiocg_time_read,
Vivek Goyal22084192009-12-03 12:59:49 -0500851 },
852 {
853 .name = "sectors",
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700854 .read_map = blkiocg_sectors_read,
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700855 },
856 {
857 .name = "io_service_bytes",
858 .read_map = blkiocg_io_service_bytes_read,
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700859 },
860 {
861 .name = "io_serviced",
862 .read_map = blkiocg_io_serviced_read,
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700863 },
864 {
865 .name = "io_service_time",
866 .read_map = blkiocg_io_service_time_read,
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700867 },
868 {
869 .name = "io_wait_time",
870 .read_map = blkiocg_io_wait_time_read,
Divyesh Shah84c124d2010-04-09 08:31:19 +0200871 },
872 {
Divyesh Shah812d4022010-04-08 21:14:23 -0700873 .name = "io_merged",
874 .read_map = blkiocg_io_merged_read,
875 },
876 {
Divyesh Shahcdc11842010-04-08 21:15:10 -0700877 .name = "io_queued",
878 .read_map = blkiocg_io_queued_read,
879 },
880 {
Divyesh Shah84c124d2010-04-09 08:31:19 +0200881 .name = "reset_stats",
882 .write_u64 = blkiocg_reset_stats,
Vivek Goyal22084192009-12-03 12:59:49 -0500883 },
884#ifdef CONFIG_DEBUG_BLK_CGROUP
Divyesh Shahcdc11842010-04-08 21:15:10 -0700885 {
886 .name = "avg_queue_size",
887 .read_map = blkiocg_avg_queue_size_read,
888 },
889 {
Divyesh Shah812df482010-04-08 21:15:35 -0700890 .name = "group_wait_time",
891 .read_map = blkiocg_group_wait_time_read,
892 },
893 {
894 .name = "idle_time",
895 .read_map = blkiocg_idle_time_read,
896 },
897 {
898 .name = "empty_time",
899 .read_map = blkiocg_empty_time_read,
900 },
901 {
Vivek Goyal22084192009-12-03 12:59:49 -0500902 .name = "dequeue",
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700903 .read_map = blkiocg_dequeue_read,
Divyesh Shahcdc11842010-04-08 21:15:10 -0700904 },
Vivek Goyal22084192009-12-03 12:59:49 -0500905#endif
Vivek Goyal31e4c282009-12-03 12:59:42 -0500906};
907
908static int blkiocg_populate(struct cgroup_subsys *subsys, struct cgroup *cgroup)
909{
910 return cgroup_add_files(cgroup, subsys, blkio_files,
911 ARRAY_SIZE(blkio_files));
912}
913
914static void blkiocg_destroy(struct cgroup_subsys *subsys, struct cgroup *cgroup)
915{
916 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgroup);
Vivek Goyalb1c35762009-12-03 12:59:47 -0500917 unsigned long flags;
918 struct blkio_group *blkg;
919 void *key;
Vivek Goyal3e252062009-12-04 10:36:42 -0500920 struct blkio_policy_type *blkiop;
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800921 struct blkio_policy_node *pn, *pntmp;
Vivek Goyal31e4c282009-12-03 12:59:42 -0500922
Vivek Goyalb1c35762009-12-03 12:59:47 -0500923 rcu_read_lock();
924remove_entry:
925 spin_lock_irqsave(&blkcg->lock, flags);
926
927 if (hlist_empty(&blkcg->blkg_list)) {
928 spin_unlock_irqrestore(&blkcg->lock, flags);
929 goto done;
930 }
931
932 blkg = hlist_entry(blkcg->blkg_list.first, struct blkio_group,
933 blkcg_node);
934 key = rcu_dereference(blkg->key);
935 __blkiocg_del_blkio_group(blkg);
936
937 spin_unlock_irqrestore(&blkcg->lock, flags);
938
939 /*
940 * This blkio_group is being unlinked as associated cgroup is going
941 * away. Let all the IO controlling policies know about this event.
942 *
943 * Currently this is static call to one io controlling policy. Once
944 * we have more policies in place, we need some dynamic registration
945 * of callback function.
946 */
Vivek Goyal3e252062009-12-04 10:36:42 -0500947 spin_lock(&blkio_list_lock);
948 list_for_each_entry(blkiop, &blkio_list, list)
949 blkiop->ops.blkio_unlink_group_fn(key, blkg);
950 spin_unlock(&blkio_list_lock);
Vivek Goyalb1c35762009-12-03 12:59:47 -0500951 goto remove_entry;
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800952
Vivek Goyalb1c35762009-12-03 12:59:47 -0500953done:
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800954 list_for_each_entry_safe(pn, pntmp, &blkcg->policy_list, node) {
955 blkio_policy_delete_node(pn);
956 kfree(pn);
957 }
Vivek Goyal31e4c282009-12-03 12:59:42 -0500958 free_css_id(&blkio_subsys, &blkcg->css);
Vivek Goyalb1c35762009-12-03 12:59:47 -0500959 rcu_read_unlock();
Ben Blum67523c42010-03-10 15:22:11 -0800960 if (blkcg != &blkio_root_cgroup)
961 kfree(blkcg);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500962}
963
964static struct cgroup_subsys_state *
965blkiocg_create(struct cgroup_subsys *subsys, struct cgroup *cgroup)
966{
967 struct blkio_cgroup *blkcg, *parent_blkcg;
968
969 if (!cgroup->parent) {
970 blkcg = &blkio_root_cgroup;
971 goto done;
972 }
973
974 /* Currently we do not support hierarchy deeper than two level (0,1) */
975 parent_blkcg = cgroup_to_blkio_cgroup(cgroup->parent);
976 if (css_depth(&parent_blkcg->css) > 0)
977 return ERR_PTR(-EINVAL);
978
979 blkcg = kzalloc(sizeof(*blkcg), GFP_KERNEL);
980 if (!blkcg)
981 return ERR_PTR(-ENOMEM);
982
983 blkcg->weight = BLKIO_WEIGHT_DEFAULT;
984done:
985 spin_lock_init(&blkcg->lock);
986 INIT_HLIST_HEAD(&blkcg->blkg_list);
987
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800988 INIT_LIST_HEAD(&blkcg->policy_list);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500989 return &blkcg->css;
990}
991
992/*
993 * We cannot support shared io contexts, as we have no mean to support
994 * two tasks with the same ioc in two different groups without major rework
995 * of the main cic data structures. For now we allow a task to change
996 * its cgroup only if it's the only owner of its ioc.
997 */
998static int blkiocg_can_attach(struct cgroup_subsys *subsys,
999 struct cgroup *cgroup, struct task_struct *tsk,
1000 bool threadgroup)
1001{
1002 struct io_context *ioc;
1003 int ret = 0;
1004
1005 /* task_lock() is needed to avoid races with exit_io_context() */
1006 task_lock(tsk);
1007 ioc = tsk->io_context;
1008 if (ioc && atomic_read(&ioc->nr_tasks) > 1)
1009 ret = -EINVAL;
1010 task_unlock(tsk);
1011
1012 return ret;
1013}
1014
1015static void blkiocg_attach(struct cgroup_subsys *subsys, struct cgroup *cgroup,
1016 struct cgroup *prev, struct task_struct *tsk,
1017 bool threadgroup)
1018{
1019 struct io_context *ioc;
1020
1021 task_lock(tsk);
1022 ioc = tsk->io_context;
1023 if (ioc)
1024 ioc->cgroup_changed = 1;
1025 task_unlock(tsk);
1026}
1027
Vivek Goyal3e252062009-12-04 10:36:42 -05001028void blkio_policy_register(struct blkio_policy_type *blkiop)
1029{
1030 spin_lock(&blkio_list_lock);
1031 list_add_tail(&blkiop->list, &blkio_list);
1032 spin_unlock(&blkio_list_lock);
1033}
1034EXPORT_SYMBOL_GPL(blkio_policy_register);
1035
1036void blkio_policy_unregister(struct blkio_policy_type *blkiop)
1037{
1038 spin_lock(&blkio_list_lock);
1039 list_del_init(&blkiop->list);
1040 spin_unlock(&blkio_list_lock);
1041}
1042EXPORT_SYMBOL_GPL(blkio_policy_unregister);
Ben Blum67523c42010-03-10 15:22:11 -08001043
1044static int __init init_cgroup_blkio(void)
1045{
1046 return cgroup_load_subsys(&blkio_subsys);
1047}
1048
1049static void __exit exit_cgroup_blkio(void)
1050{
1051 cgroup_unload_subsys(&blkio_subsys);
1052}
1053
1054module_init(init_cgroup_blkio);
1055module_exit(exit_cgroup_blkio);
1056MODULE_LICENSE("GPL");