blob: 83930f65016ab2666b4ca3f04d15508bce966cb5 [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
Divyesh Shah28baf442010-04-14 11:22:38 +0200216void blkiocg_set_start_empty_time(struct blkio_group *blkg, bool ignore)
217{
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 /*
231 * If ignore is set, we do not panic on the empty flag being set
232 * already. This is to avoid cases where there are superfluous timeslice
233 * complete events (for eg., forced_dispatch in CFQ) when no IOs are
234 * served which could result in triggering the empty check incorrectly.
235 */
236 BUG_ON(!ignore && blkio_blkg_empty(stats));
237 stats->start_empty_time = sched_clock();
238 blkio_mark_blkg_empty(stats);
239 spin_unlock_irqrestore(&blkg->stats_lock, flags);
240}
241EXPORT_SYMBOL_GPL(blkiocg_set_start_empty_time);
242
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200243void blkiocg_update_dequeue_stats(struct blkio_group *blkg,
244 unsigned long dequeue)
245{
246 blkg->stats.dequeue += dequeue;
247}
248EXPORT_SYMBOL_GPL(blkiocg_update_dequeue_stats);
Divyesh Shah812df482010-04-08 21:15:35 -0700249#else
250static inline void blkio_set_start_group_wait_time(struct blkio_group *blkg,
251 struct blkio_group *curr_blkg) {}
252static inline void blkio_end_empty_time(struct blkio_group_stats *stats) {}
Divyesh Shahcdc11842010-04-08 21:15:10 -0700253#endif
254
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200255void blkiocg_update_io_add_stats(struct blkio_group *blkg,
Divyesh Shahcdc11842010-04-08 21:15:10 -0700256 struct blkio_group *curr_blkg, bool direction,
257 bool sync)
258{
259 unsigned long flags;
260
261 spin_lock_irqsave(&blkg->stats_lock, flags);
262 blkio_add_stat(blkg->stats.stat_arr[BLKIO_STAT_QUEUED], 1, direction,
263 sync);
Divyesh Shah812df482010-04-08 21:15:35 -0700264 blkio_end_empty_time(&blkg->stats);
265 blkio_set_start_group_wait_time(blkg, curr_blkg);
Divyesh Shahcdc11842010-04-08 21:15:10 -0700266 spin_unlock_irqrestore(&blkg->stats_lock, flags);
267}
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200268EXPORT_SYMBOL_GPL(blkiocg_update_io_add_stats);
Divyesh Shahcdc11842010-04-08 21:15:10 -0700269
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200270void blkiocg_update_io_remove_stats(struct blkio_group *blkg,
Divyesh Shahcdc11842010-04-08 21:15:10 -0700271 bool direction, bool sync)
272{
273 unsigned long flags;
274
275 spin_lock_irqsave(&blkg->stats_lock, flags);
276 blkio_check_and_dec_stat(blkg->stats.stat_arr[BLKIO_STAT_QUEUED],
277 direction, sync);
278 spin_unlock_irqrestore(&blkg->stats_lock, flags);
279}
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200280EXPORT_SYMBOL_GPL(blkiocg_update_io_remove_stats);
Divyesh Shahcdc11842010-04-08 21:15:10 -0700281
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700282void blkiocg_update_timeslice_used(struct blkio_group *blkg, unsigned long time)
Vivek Goyal22084192009-12-03 12:59:49 -0500283{
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700284 unsigned long flags;
285
286 spin_lock_irqsave(&blkg->stats_lock, flags);
287 blkg->stats.time += time;
288 spin_unlock_irqrestore(&blkg->stats_lock, flags);
Vivek Goyal22084192009-12-03 12:59:49 -0500289}
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700290EXPORT_SYMBOL_GPL(blkiocg_update_timeslice_used);
Vivek Goyal22084192009-12-03 12:59:49 -0500291
Divyesh Shah84c124d2010-04-09 08:31:19 +0200292void blkiocg_update_dispatch_stats(struct blkio_group *blkg,
293 uint64_t bytes, bool direction, bool sync)
Divyesh Shah91952912010-04-01 15:01:41 -0700294{
295 struct blkio_group_stats *stats;
296 unsigned long flags;
297
298 spin_lock_irqsave(&blkg->stats_lock, flags);
299 stats = &blkg->stats;
Divyesh Shah84c124d2010-04-09 08:31:19 +0200300 stats->sectors += bytes >> 9;
301 blkio_add_stat(stats->stat_arr[BLKIO_STAT_SERVICED], 1, direction,
302 sync);
303 blkio_add_stat(stats->stat_arr[BLKIO_STAT_SERVICE_BYTES], bytes,
304 direction, sync);
Divyesh Shah91952912010-04-01 15:01:41 -0700305 spin_unlock_irqrestore(&blkg->stats_lock, flags);
306}
Divyesh Shah84c124d2010-04-09 08:31:19 +0200307EXPORT_SYMBOL_GPL(blkiocg_update_dispatch_stats);
Divyesh Shah91952912010-04-01 15:01:41 -0700308
Divyesh Shah84c124d2010-04-09 08:31:19 +0200309void blkiocg_update_completion_stats(struct blkio_group *blkg,
310 uint64_t start_time, uint64_t io_start_time, bool direction, bool sync)
Divyesh Shah91952912010-04-01 15:01:41 -0700311{
312 struct blkio_group_stats *stats;
313 unsigned long flags;
314 unsigned long long now = sched_clock();
315
316 spin_lock_irqsave(&blkg->stats_lock, flags);
317 stats = &blkg->stats;
Divyesh Shah84c124d2010-04-09 08:31:19 +0200318 if (time_after64(now, io_start_time))
319 blkio_add_stat(stats->stat_arr[BLKIO_STAT_SERVICE_TIME],
320 now - io_start_time, direction, sync);
321 if (time_after64(io_start_time, start_time))
322 blkio_add_stat(stats->stat_arr[BLKIO_STAT_WAIT_TIME],
323 io_start_time - start_time, direction, sync);
Divyesh Shah91952912010-04-01 15:01:41 -0700324 spin_unlock_irqrestore(&blkg->stats_lock, flags);
325}
Divyesh Shah84c124d2010-04-09 08:31:19 +0200326EXPORT_SYMBOL_GPL(blkiocg_update_completion_stats);
Divyesh Shah91952912010-04-01 15:01:41 -0700327
Divyesh Shah812d4022010-04-08 21:14:23 -0700328void blkiocg_update_io_merged_stats(struct blkio_group *blkg, bool direction,
329 bool sync)
330{
331 unsigned long flags;
332
333 spin_lock_irqsave(&blkg->stats_lock, flags);
334 blkio_add_stat(blkg->stats.stat_arr[BLKIO_STAT_MERGED], 1, direction,
335 sync);
336 spin_unlock_irqrestore(&blkg->stats_lock, flags);
337}
338EXPORT_SYMBOL_GPL(blkiocg_update_io_merged_stats);
339
Vivek Goyal31e4c282009-12-03 12:59:42 -0500340void blkiocg_add_blkio_group(struct blkio_cgroup *blkcg,
Vivek Goyal22084192009-12-03 12:59:49 -0500341 struct blkio_group *blkg, void *key, dev_t dev)
Vivek Goyal31e4c282009-12-03 12:59:42 -0500342{
343 unsigned long flags;
344
345 spin_lock_irqsave(&blkcg->lock, flags);
Divyesh Shah8d2a91f2010-04-16 08:10:51 +0200346 spin_lock_init(&blkg->stats_lock);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500347 rcu_assign_pointer(blkg->key, key);
Vivek Goyalb1c35762009-12-03 12:59:47 -0500348 blkg->blkcg_id = css_id(&blkcg->css);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500349 hlist_add_head_rcu(&blkg->blkcg_node, &blkcg->blkg_list);
350 spin_unlock_irqrestore(&blkcg->lock, flags);
Vivek Goyal2868ef72009-12-03 12:59:48 -0500351#ifdef CONFIG_DEBUG_BLK_CGROUP
352 /* Need to take css reference ? */
353 cgroup_path(blkcg->css.cgroup, blkg->path, sizeof(blkg->path));
354#endif
Vivek Goyal22084192009-12-03 12:59:49 -0500355 blkg->dev = dev;
Vivek Goyal31e4c282009-12-03 12:59:42 -0500356}
Vivek Goyal9d6a9862009-12-04 10:36:41 -0500357EXPORT_SYMBOL_GPL(blkiocg_add_blkio_group);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500358
Vivek Goyalb1c35762009-12-03 12:59:47 -0500359static void __blkiocg_del_blkio_group(struct blkio_group *blkg)
360{
361 hlist_del_init_rcu(&blkg->blkcg_node);
362 blkg->blkcg_id = 0;
363}
364
365/*
366 * returns 0 if blkio_group was still on cgroup list. Otherwise returns 1
367 * indicating that blk_group was unhashed by the time we got to it.
368 */
Vivek Goyal31e4c282009-12-03 12:59:42 -0500369int blkiocg_del_blkio_group(struct blkio_group *blkg)
370{
Vivek Goyalb1c35762009-12-03 12:59:47 -0500371 struct blkio_cgroup *blkcg;
372 unsigned long flags;
373 struct cgroup_subsys_state *css;
374 int ret = 1;
375
376 rcu_read_lock();
377 css = css_lookup(&blkio_subsys, blkg->blkcg_id);
378 if (!css)
379 goto out;
380
381 blkcg = container_of(css, struct blkio_cgroup, css);
382 spin_lock_irqsave(&blkcg->lock, flags);
383 if (!hlist_unhashed(&blkg->blkcg_node)) {
384 __blkiocg_del_blkio_group(blkg);
385 ret = 0;
386 }
387 spin_unlock_irqrestore(&blkcg->lock, flags);
388out:
389 rcu_read_unlock();
390 return ret;
Vivek Goyal31e4c282009-12-03 12:59:42 -0500391}
Vivek Goyal9d6a9862009-12-04 10:36:41 -0500392EXPORT_SYMBOL_GPL(blkiocg_del_blkio_group);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500393
394/* called under rcu_read_lock(). */
395struct blkio_group *blkiocg_lookup_group(struct blkio_cgroup *blkcg, void *key)
396{
397 struct blkio_group *blkg;
398 struct hlist_node *n;
399 void *__key;
400
401 hlist_for_each_entry_rcu(blkg, n, &blkcg->blkg_list, blkcg_node) {
402 __key = blkg->key;
403 if (__key == key)
404 return blkg;
405 }
406
407 return NULL;
408}
Vivek Goyal9d6a9862009-12-04 10:36:41 -0500409EXPORT_SYMBOL_GPL(blkiocg_lookup_group);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500410
411#define SHOW_FUNCTION(__VAR) \
412static u64 blkiocg_##__VAR##_read(struct cgroup *cgroup, \
413 struct cftype *cftype) \
414{ \
415 struct blkio_cgroup *blkcg; \
416 \
417 blkcg = cgroup_to_blkio_cgroup(cgroup); \
418 return (u64)blkcg->__VAR; \
419}
420
421SHOW_FUNCTION(weight);
422#undef SHOW_FUNCTION
423
424static int
425blkiocg_weight_write(struct cgroup *cgroup, struct cftype *cftype, u64 val)
426{
427 struct blkio_cgroup *blkcg;
Vivek Goyalf8d461d2009-12-03 12:59:52 -0500428 struct blkio_group *blkg;
429 struct hlist_node *n;
Vivek Goyal3e252062009-12-04 10:36:42 -0500430 struct blkio_policy_type *blkiop;
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800431 struct blkio_policy_node *pn;
Vivek Goyal31e4c282009-12-03 12:59:42 -0500432
433 if (val < BLKIO_WEIGHT_MIN || val > BLKIO_WEIGHT_MAX)
434 return -EINVAL;
435
436 blkcg = cgroup_to_blkio_cgroup(cgroup);
Gui Jianfengbcf4dd42010-02-01 09:58:54 +0100437 spin_lock(&blkio_list_lock);
Vivek Goyalf8d461d2009-12-03 12:59:52 -0500438 spin_lock_irq(&blkcg->lock);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500439 blkcg->weight = (unsigned int)val;
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800440
Vivek Goyal3e252062009-12-04 10:36:42 -0500441 hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) {
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800442 pn = blkio_policy_search_node(blkcg, blkg->dev);
443
444 if (pn)
445 continue;
446
Vivek Goyal3e252062009-12-04 10:36:42 -0500447 list_for_each_entry(blkiop, &blkio_list, list)
448 blkiop->ops.blkio_update_group_weight_fn(blkg,
449 blkcg->weight);
Vivek Goyal3e252062009-12-04 10:36:42 -0500450 }
Vivek Goyalf8d461d2009-12-03 12:59:52 -0500451 spin_unlock_irq(&blkcg->lock);
Gui Jianfengbcf4dd42010-02-01 09:58:54 +0100452 spin_unlock(&blkio_list_lock);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500453 return 0;
454}
455
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700456static int
Divyesh Shah84c124d2010-04-09 08:31:19 +0200457blkiocg_reset_stats(struct cgroup *cgroup, struct cftype *cftype, u64 val)
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700458{
459 struct blkio_cgroup *blkcg;
460 struct blkio_group *blkg;
Divyesh Shah812df482010-04-08 21:15:35 -0700461 struct blkio_group_stats *stats;
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700462 struct hlist_node *n;
Divyesh Shahcdc11842010-04-08 21:15:10 -0700463 uint64_t queued[BLKIO_STAT_TOTAL];
464 int i;
Divyesh Shah812df482010-04-08 21:15:35 -0700465#ifdef CONFIG_DEBUG_BLK_CGROUP
466 bool idling, waiting, empty;
467 unsigned long long now = sched_clock();
468#endif
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700469
470 blkcg = cgroup_to_blkio_cgroup(cgroup);
471 spin_lock_irq(&blkcg->lock);
472 hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) {
473 spin_lock(&blkg->stats_lock);
Divyesh Shah812df482010-04-08 21:15:35 -0700474 stats = &blkg->stats;
475#ifdef CONFIG_DEBUG_BLK_CGROUP
476 idling = blkio_blkg_idling(stats);
477 waiting = blkio_blkg_waiting(stats);
478 empty = blkio_blkg_empty(stats);
479#endif
Divyesh Shahcdc11842010-04-08 21:15:10 -0700480 for (i = 0; i < BLKIO_STAT_TOTAL; i++)
Divyesh Shah812df482010-04-08 21:15:35 -0700481 queued[i] = stats->stat_arr[BLKIO_STAT_QUEUED][i];
482 memset(stats, 0, sizeof(struct blkio_group_stats));
Divyesh Shahcdc11842010-04-08 21:15:10 -0700483 for (i = 0; i < BLKIO_STAT_TOTAL; i++)
Divyesh Shah812df482010-04-08 21:15:35 -0700484 stats->stat_arr[BLKIO_STAT_QUEUED][i] = queued[i];
485#ifdef CONFIG_DEBUG_BLK_CGROUP
486 if (idling) {
487 blkio_mark_blkg_idling(stats);
488 stats->start_idle_time = now;
489 }
490 if (waiting) {
491 blkio_mark_blkg_waiting(stats);
492 stats->start_group_wait_time = now;
493 }
494 if (empty) {
495 blkio_mark_blkg_empty(stats);
496 stats->start_empty_time = now;
497 }
498#endif
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700499 spin_unlock(&blkg->stats_lock);
500 }
501 spin_unlock_irq(&blkcg->lock);
502 return 0;
503}
504
Divyesh Shah84c124d2010-04-09 08:31:19 +0200505static void blkio_get_key_name(enum stat_sub_type type, dev_t dev, char *str,
506 int chars_left, bool diskname_only)
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700507{
Divyesh Shah84c124d2010-04-09 08:31:19 +0200508 snprintf(str, chars_left, "%d:%d", MAJOR(dev), MINOR(dev));
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700509 chars_left -= strlen(str);
510 if (chars_left <= 0) {
511 printk(KERN_WARNING
512 "Possibly incorrect cgroup stat display format");
513 return;
514 }
Divyesh Shah84c124d2010-04-09 08:31:19 +0200515 if (diskname_only)
516 return;
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700517 switch (type) {
Divyesh Shah84c124d2010-04-09 08:31:19 +0200518 case BLKIO_STAT_READ:
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700519 strlcat(str, " Read", chars_left);
520 break;
Divyesh Shah84c124d2010-04-09 08:31:19 +0200521 case BLKIO_STAT_WRITE:
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700522 strlcat(str, " Write", chars_left);
523 break;
Divyesh Shah84c124d2010-04-09 08:31:19 +0200524 case BLKIO_STAT_SYNC:
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700525 strlcat(str, " Sync", chars_left);
526 break;
Divyesh Shah84c124d2010-04-09 08:31:19 +0200527 case BLKIO_STAT_ASYNC:
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700528 strlcat(str, " Async", chars_left);
529 break;
Divyesh Shah84c124d2010-04-09 08:31:19 +0200530 case BLKIO_STAT_TOTAL:
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700531 strlcat(str, " Total", chars_left);
532 break;
533 default:
534 strlcat(str, " Invalid", chars_left);
535 }
536}
537
Divyesh Shah84c124d2010-04-09 08:31:19 +0200538static uint64_t blkio_fill_stat(char *str, int chars_left, uint64_t val,
539 struct cgroup_map_cb *cb, dev_t dev)
540{
541 blkio_get_key_name(0, dev, str, chars_left, true);
542 cb->fill(cb, str, val);
543 return val;
544}
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700545
Divyesh Shah84c124d2010-04-09 08:31:19 +0200546/* This should be called with blkg->stats_lock held */
547static uint64_t blkio_get_stat(struct blkio_group *blkg,
548 struct cgroup_map_cb *cb, dev_t dev, enum stat_type type)
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700549{
550 uint64_t disk_total;
551 char key_str[MAX_KEY_LEN];
Divyesh Shah84c124d2010-04-09 08:31:19 +0200552 enum stat_sub_type sub_type;
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700553
Divyesh Shah84c124d2010-04-09 08:31:19 +0200554 if (type == BLKIO_STAT_TIME)
555 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
556 blkg->stats.time, cb, dev);
557 if (type == BLKIO_STAT_SECTORS)
558 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
559 blkg->stats.sectors, cb, dev);
560#ifdef CONFIG_DEBUG_BLK_CGROUP
Divyesh Shahcdc11842010-04-08 21:15:10 -0700561 if (type == BLKIO_STAT_AVG_QUEUE_SIZE) {
562 uint64_t sum = blkg->stats.avg_queue_size_sum;
563 uint64_t samples = blkg->stats.avg_queue_size_samples;
564 if (samples)
565 do_div(sum, samples);
566 else
567 sum = 0;
568 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1, sum, cb, dev);
569 }
Divyesh Shah812df482010-04-08 21:15:35 -0700570 if (type == BLKIO_STAT_GROUP_WAIT_TIME)
571 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
572 blkg->stats.group_wait_time, cb, dev);
573 if (type == BLKIO_STAT_IDLE_TIME)
574 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
575 blkg->stats.idle_time, cb, dev);
576 if (type == BLKIO_STAT_EMPTY_TIME)
577 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
578 blkg->stats.empty_time, cb, dev);
Divyesh Shah84c124d2010-04-09 08:31:19 +0200579 if (type == BLKIO_STAT_DEQUEUE)
580 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
581 blkg->stats.dequeue, cb, dev);
582#endif
583
584 for (sub_type = BLKIO_STAT_READ; sub_type < BLKIO_STAT_TOTAL;
585 sub_type++) {
586 blkio_get_key_name(sub_type, dev, key_str, MAX_KEY_LEN, false);
587 cb->fill(cb, key_str, blkg->stats.stat_arr[type][sub_type]);
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700588 }
Divyesh Shah84c124d2010-04-09 08:31:19 +0200589 disk_total = blkg->stats.stat_arr[type][BLKIO_STAT_READ] +
590 blkg->stats.stat_arr[type][BLKIO_STAT_WRITE];
591 blkio_get_key_name(BLKIO_STAT_TOTAL, dev, key_str, MAX_KEY_LEN, false);
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700592 cb->fill(cb, key_str, disk_total);
593 return disk_total;
594}
595
Divyesh Shah84c124d2010-04-09 08:31:19 +0200596#define SHOW_FUNCTION_PER_GROUP(__VAR, type, show_total) \
Vivek Goyal22084192009-12-03 12:59:49 -0500597static int blkiocg_##__VAR##_read(struct cgroup *cgroup, \
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700598 struct cftype *cftype, struct cgroup_map_cb *cb) \
Vivek Goyal22084192009-12-03 12:59:49 -0500599{ \
600 struct blkio_cgroup *blkcg; \
601 struct blkio_group *blkg; \
602 struct hlist_node *n; \
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700603 uint64_t cgroup_total = 0; \
Vivek Goyal22084192009-12-03 12:59:49 -0500604 \
605 if (!cgroup_lock_live_group(cgroup)) \
606 return -ENODEV; \
607 \
608 blkcg = cgroup_to_blkio_cgroup(cgroup); \
609 rcu_read_lock(); \
610 hlist_for_each_entry_rcu(blkg, n, &blkcg->blkg_list, blkcg_node) {\
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700611 if (blkg->dev) { \
612 spin_lock_irq(&blkg->stats_lock); \
Divyesh Shah84c124d2010-04-09 08:31:19 +0200613 cgroup_total += blkio_get_stat(blkg, cb, \
614 blkg->dev, type); \
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700615 spin_unlock_irq(&blkg->stats_lock); \
616 } \
Vivek Goyal22084192009-12-03 12:59:49 -0500617 } \
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700618 if (show_total) \
619 cb->fill(cb, "Total", cgroup_total); \
Vivek Goyal22084192009-12-03 12:59:49 -0500620 rcu_read_unlock(); \
621 cgroup_unlock(); \
622 return 0; \
623}
624
Divyesh Shah84c124d2010-04-09 08:31:19 +0200625SHOW_FUNCTION_PER_GROUP(time, BLKIO_STAT_TIME, 0);
626SHOW_FUNCTION_PER_GROUP(sectors, BLKIO_STAT_SECTORS, 0);
627SHOW_FUNCTION_PER_GROUP(io_service_bytes, BLKIO_STAT_SERVICE_BYTES, 1);
628SHOW_FUNCTION_PER_GROUP(io_serviced, BLKIO_STAT_SERVICED, 1);
629SHOW_FUNCTION_PER_GROUP(io_service_time, BLKIO_STAT_SERVICE_TIME, 1);
630SHOW_FUNCTION_PER_GROUP(io_wait_time, BLKIO_STAT_WAIT_TIME, 1);
Divyesh Shah812d4022010-04-08 21:14:23 -0700631SHOW_FUNCTION_PER_GROUP(io_merged, BLKIO_STAT_MERGED, 1);
Divyesh Shahcdc11842010-04-08 21:15:10 -0700632SHOW_FUNCTION_PER_GROUP(io_queued, BLKIO_STAT_QUEUED, 1);
Vivek Goyal22084192009-12-03 12:59:49 -0500633#ifdef CONFIG_DEBUG_BLK_CGROUP
Divyesh Shah84c124d2010-04-09 08:31:19 +0200634SHOW_FUNCTION_PER_GROUP(dequeue, BLKIO_STAT_DEQUEUE, 0);
Divyesh Shahcdc11842010-04-08 21:15:10 -0700635SHOW_FUNCTION_PER_GROUP(avg_queue_size, BLKIO_STAT_AVG_QUEUE_SIZE, 0);
Divyesh Shah812df482010-04-08 21:15:35 -0700636SHOW_FUNCTION_PER_GROUP(group_wait_time, BLKIO_STAT_GROUP_WAIT_TIME, 0);
637SHOW_FUNCTION_PER_GROUP(idle_time, BLKIO_STAT_IDLE_TIME, 0);
638SHOW_FUNCTION_PER_GROUP(empty_time, BLKIO_STAT_EMPTY_TIME, 0);
Vivek Goyal22084192009-12-03 12:59:49 -0500639#endif
640#undef SHOW_FUNCTION_PER_GROUP
641
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800642static int blkio_check_dev_num(dev_t dev)
643{
644 int part = 0;
645 struct gendisk *disk;
646
647 disk = get_gendisk(dev, &part);
648 if (!disk || part)
649 return -ENODEV;
650
651 return 0;
652}
653
654static int blkio_policy_parse_and_set(char *buf,
655 struct blkio_policy_node *newpn)
656{
657 char *s[4], *p, *major_s = NULL, *minor_s = NULL;
658 int ret;
659 unsigned long major, minor, temp;
660 int i = 0;
661 dev_t dev;
662
663 memset(s, 0, sizeof(s));
664
665 while ((p = strsep(&buf, " ")) != NULL) {
666 if (!*p)
667 continue;
668
669 s[i++] = p;
670
671 /* Prevent from inputing too many things */
672 if (i == 3)
673 break;
674 }
675
676 if (i != 2)
677 return -EINVAL;
678
679 p = strsep(&s[0], ":");
680 if (p != NULL)
681 major_s = p;
682 else
683 return -EINVAL;
684
685 minor_s = s[0];
686 if (!minor_s)
687 return -EINVAL;
688
689 ret = strict_strtoul(major_s, 10, &major);
690 if (ret)
691 return -EINVAL;
692
693 ret = strict_strtoul(minor_s, 10, &minor);
694 if (ret)
695 return -EINVAL;
696
697 dev = MKDEV(major, minor);
698
699 ret = blkio_check_dev_num(dev);
700 if (ret)
701 return ret;
702
703 newpn->dev = dev;
704
705 if (s[1] == NULL)
706 return -EINVAL;
707
708 ret = strict_strtoul(s[1], 10, &temp);
709 if (ret || (temp < BLKIO_WEIGHT_MIN && temp > 0) ||
710 temp > BLKIO_WEIGHT_MAX)
711 return -EINVAL;
712
713 newpn->weight = temp;
714
715 return 0;
716}
717
718unsigned int blkcg_get_weight(struct blkio_cgroup *blkcg,
719 dev_t dev)
720{
721 struct blkio_policy_node *pn;
722
723 pn = blkio_policy_search_node(blkcg, dev);
724 if (pn)
725 return pn->weight;
726 else
727 return blkcg->weight;
728}
729EXPORT_SYMBOL_GPL(blkcg_get_weight);
730
731
732static int blkiocg_weight_device_write(struct cgroup *cgrp, struct cftype *cft,
733 const char *buffer)
734{
735 int ret = 0;
736 char *buf;
737 struct blkio_policy_node *newpn, *pn;
738 struct blkio_cgroup *blkcg;
739 struct blkio_group *blkg;
740 int keep_newpn = 0;
741 struct hlist_node *n;
742 struct blkio_policy_type *blkiop;
743
744 buf = kstrdup(buffer, GFP_KERNEL);
745 if (!buf)
746 return -ENOMEM;
747
748 newpn = kzalloc(sizeof(*newpn), GFP_KERNEL);
749 if (!newpn) {
750 ret = -ENOMEM;
751 goto free_buf;
752 }
753
754 ret = blkio_policy_parse_and_set(buf, newpn);
755 if (ret)
756 goto free_newpn;
757
758 blkcg = cgroup_to_blkio_cgroup(cgrp);
759
760 spin_lock_irq(&blkcg->lock);
761
762 pn = blkio_policy_search_node(blkcg, newpn->dev);
763 if (!pn) {
764 if (newpn->weight != 0) {
765 blkio_policy_insert_node(blkcg, newpn);
766 keep_newpn = 1;
767 }
768 spin_unlock_irq(&blkcg->lock);
769 goto update_io_group;
770 }
771
772 if (newpn->weight == 0) {
773 /* weight == 0 means deleteing a specific weight */
774 blkio_policy_delete_node(pn);
775 spin_unlock_irq(&blkcg->lock);
776 goto update_io_group;
777 }
778 spin_unlock_irq(&blkcg->lock);
779
780 pn->weight = newpn->weight;
781
782update_io_group:
783 /* update weight for each cfqg */
784 spin_lock(&blkio_list_lock);
785 spin_lock_irq(&blkcg->lock);
786
787 hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) {
788 if (newpn->dev == blkg->dev) {
789 list_for_each_entry(blkiop, &blkio_list, list)
790 blkiop->ops.blkio_update_group_weight_fn(blkg,
791 newpn->weight ?
792 newpn->weight :
793 blkcg->weight);
794 }
795 }
796
797 spin_unlock_irq(&blkcg->lock);
798 spin_unlock(&blkio_list_lock);
799
800free_newpn:
801 if (!keep_newpn)
802 kfree(newpn);
803free_buf:
804 kfree(buf);
805 return ret;
806}
807
808static int blkiocg_weight_device_read(struct cgroup *cgrp, struct cftype *cft,
809 struct seq_file *m)
810{
811 struct blkio_cgroup *blkcg;
812 struct blkio_policy_node *pn;
813
814 seq_printf(m, "dev\tweight\n");
815
816 blkcg = cgroup_to_blkio_cgroup(cgrp);
817 if (list_empty(&blkcg->policy_list))
818 goto out;
819
820 spin_lock_irq(&blkcg->lock);
821 list_for_each_entry(pn, &blkcg->policy_list, node) {
822 seq_printf(m, "%u:%u\t%u\n", MAJOR(pn->dev),
823 MINOR(pn->dev), pn->weight);
824 }
825 spin_unlock_irq(&blkcg->lock);
826
827out:
828 return 0;
829}
830
Vivek Goyal31e4c282009-12-03 12:59:42 -0500831struct cftype blkio_files[] = {
832 {
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800833 .name = "weight_device",
834 .read_seq_string = blkiocg_weight_device_read,
835 .write_string = blkiocg_weight_device_write,
836 .max_write_len = 256,
837 },
838 {
Vivek Goyal31e4c282009-12-03 12:59:42 -0500839 .name = "weight",
840 .read_u64 = blkiocg_weight_read,
841 .write_u64 = blkiocg_weight_write,
842 },
Vivek Goyal22084192009-12-03 12:59:49 -0500843 {
844 .name = "time",
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700845 .read_map = blkiocg_time_read,
Vivek Goyal22084192009-12-03 12:59:49 -0500846 },
847 {
848 .name = "sectors",
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700849 .read_map = blkiocg_sectors_read,
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700850 },
851 {
852 .name = "io_service_bytes",
853 .read_map = blkiocg_io_service_bytes_read,
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700854 },
855 {
856 .name = "io_serviced",
857 .read_map = blkiocg_io_serviced_read,
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700858 },
859 {
860 .name = "io_service_time",
861 .read_map = blkiocg_io_service_time_read,
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700862 },
863 {
864 .name = "io_wait_time",
865 .read_map = blkiocg_io_wait_time_read,
Divyesh Shah84c124d2010-04-09 08:31:19 +0200866 },
867 {
Divyesh Shah812d4022010-04-08 21:14:23 -0700868 .name = "io_merged",
869 .read_map = blkiocg_io_merged_read,
870 },
871 {
Divyesh Shahcdc11842010-04-08 21:15:10 -0700872 .name = "io_queued",
873 .read_map = blkiocg_io_queued_read,
874 },
875 {
Divyesh Shah84c124d2010-04-09 08:31:19 +0200876 .name = "reset_stats",
877 .write_u64 = blkiocg_reset_stats,
Vivek Goyal22084192009-12-03 12:59:49 -0500878 },
879#ifdef CONFIG_DEBUG_BLK_CGROUP
Divyesh Shahcdc11842010-04-08 21:15:10 -0700880 {
881 .name = "avg_queue_size",
882 .read_map = blkiocg_avg_queue_size_read,
883 },
884 {
Divyesh Shah812df482010-04-08 21:15:35 -0700885 .name = "group_wait_time",
886 .read_map = blkiocg_group_wait_time_read,
887 },
888 {
889 .name = "idle_time",
890 .read_map = blkiocg_idle_time_read,
891 },
892 {
893 .name = "empty_time",
894 .read_map = blkiocg_empty_time_read,
895 },
896 {
Vivek Goyal22084192009-12-03 12:59:49 -0500897 .name = "dequeue",
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700898 .read_map = blkiocg_dequeue_read,
Divyesh Shahcdc11842010-04-08 21:15:10 -0700899 },
Vivek Goyal22084192009-12-03 12:59:49 -0500900#endif
Vivek Goyal31e4c282009-12-03 12:59:42 -0500901};
902
903static int blkiocg_populate(struct cgroup_subsys *subsys, struct cgroup *cgroup)
904{
905 return cgroup_add_files(cgroup, subsys, blkio_files,
906 ARRAY_SIZE(blkio_files));
907}
908
909static void blkiocg_destroy(struct cgroup_subsys *subsys, struct cgroup *cgroup)
910{
911 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgroup);
Vivek Goyalb1c35762009-12-03 12:59:47 -0500912 unsigned long flags;
913 struct blkio_group *blkg;
914 void *key;
Vivek Goyal3e252062009-12-04 10:36:42 -0500915 struct blkio_policy_type *blkiop;
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800916 struct blkio_policy_node *pn, *pntmp;
Vivek Goyal31e4c282009-12-03 12:59:42 -0500917
Vivek Goyalb1c35762009-12-03 12:59:47 -0500918 rcu_read_lock();
919remove_entry:
920 spin_lock_irqsave(&blkcg->lock, flags);
921
922 if (hlist_empty(&blkcg->blkg_list)) {
923 spin_unlock_irqrestore(&blkcg->lock, flags);
924 goto done;
925 }
926
927 blkg = hlist_entry(blkcg->blkg_list.first, struct blkio_group,
928 blkcg_node);
929 key = rcu_dereference(blkg->key);
930 __blkiocg_del_blkio_group(blkg);
931
932 spin_unlock_irqrestore(&blkcg->lock, flags);
933
934 /*
935 * This blkio_group is being unlinked as associated cgroup is going
936 * away. Let all the IO controlling policies know about this event.
937 *
938 * Currently this is static call to one io controlling policy. Once
939 * we have more policies in place, we need some dynamic registration
940 * of callback function.
941 */
Vivek Goyal3e252062009-12-04 10:36:42 -0500942 spin_lock(&blkio_list_lock);
943 list_for_each_entry(blkiop, &blkio_list, list)
944 blkiop->ops.blkio_unlink_group_fn(key, blkg);
945 spin_unlock(&blkio_list_lock);
Vivek Goyalb1c35762009-12-03 12:59:47 -0500946 goto remove_entry;
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800947
Vivek Goyalb1c35762009-12-03 12:59:47 -0500948done:
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800949 list_for_each_entry_safe(pn, pntmp, &blkcg->policy_list, node) {
950 blkio_policy_delete_node(pn);
951 kfree(pn);
952 }
Vivek Goyal31e4c282009-12-03 12:59:42 -0500953 free_css_id(&blkio_subsys, &blkcg->css);
Vivek Goyalb1c35762009-12-03 12:59:47 -0500954 rcu_read_unlock();
Ben Blum67523c42010-03-10 15:22:11 -0800955 if (blkcg != &blkio_root_cgroup)
956 kfree(blkcg);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500957}
958
959static struct cgroup_subsys_state *
960blkiocg_create(struct cgroup_subsys *subsys, struct cgroup *cgroup)
961{
962 struct blkio_cgroup *blkcg, *parent_blkcg;
963
964 if (!cgroup->parent) {
965 blkcg = &blkio_root_cgroup;
966 goto done;
967 }
968
969 /* Currently we do not support hierarchy deeper than two level (0,1) */
970 parent_blkcg = cgroup_to_blkio_cgroup(cgroup->parent);
971 if (css_depth(&parent_blkcg->css) > 0)
972 return ERR_PTR(-EINVAL);
973
974 blkcg = kzalloc(sizeof(*blkcg), GFP_KERNEL);
975 if (!blkcg)
976 return ERR_PTR(-ENOMEM);
977
978 blkcg->weight = BLKIO_WEIGHT_DEFAULT;
979done:
980 spin_lock_init(&blkcg->lock);
981 INIT_HLIST_HEAD(&blkcg->blkg_list);
982
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800983 INIT_LIST_HEAD(&blkcg->policy_list);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500984 return &blkcg->css;
985}
986
987/*
988 * We cannot support shared io contexts, as we have no mean to support
989 * two tasks with the same ioc in two different groups without major rework
990 * of the main cic data structures. For now we allow a task to change
991 * its cgroup only if it's the only owner of its ioc.
992 */
993static int blkiocg_can_attach(struct cgroup_subsys *subsys,
994 struct cgroup *cgroup, struct task_struct *tsk,
995 bool threadgroup)
996{
997 struct io_context *ioc;
998 int ret = 0;
999
1000 /* task_lock() is needed to avoid races with exit_io_context() */
1001 task_lock(tsk);
1002 ioc = tsk->io_context;
1003 if (ioc && atomic_read(&ioc->nr_tasks) > 1)
1004 ret = -EINVAL;
1005 task_unlock(tsk);
1006
1007 return ret;
1008}
1009
1010static void blkiocg_attach(struct cgroup_subsys *subsys, struct cgroup *cgroup,
1011 struct cgroup *prev, struct task_struct *tsk,
1012 bool threadgroup)
1013{
1014 struct io_context *ioc;
1015
1016 task_lock(tsk);
1017 ioc = tsk->io_context;
1018 if (ioc)
1019 ioc->cgroup_changed = 1;
1020 task_unlock(tsk);
1021}
1022
Vivek Goyal3e252062009-12-04 10:36:42 -05001023void blkio_policy_register(struct blkio_policy_type *blkiop)
1024{
1025 spin_lock(&blkio_list_lock);
1026 list_add_tail(&blkiop->list, &blkio_list);
1027 spin_unlock(&blkio_list_lock);
1028}
1029EXPORT_SYMBOL_GPL(blkio_policy_register);
1030
1031void blkio_policy_unregister(struct blkio_policy_type *blkiop)
1032{
1033 spin_lock(&blkio_list_lock);
1034 list_del_init(&blkiop->list);
1035 spin_unlock(&blkio_list_lock);
1036}
1037EXPORT_SYMBOL_GPL(blkio_policy_unregister);
Ben Blum67523c42010-03-10 15:22:11 -08001038
1039static int __init init_cgroup_blkio(void)
1040{
1041 return cgroup_load_subsys(&blkio_subsys);
1042}
1043
1044static void __exit exit_cgroup_blkio(void)
1045{
1046 cgroup_unload_subsys(&blkio_subsys);
1047}
1048
1049module_init(init_cgroup_blkio);
1050module_exit(exit_cgroup_blkio);
1051MODULE_LICENSE("GPL");