blob: b06ca70354e30d9131550fc898cf0970e541b6a3 [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
Vivek Goyal062a6442010-09-15 17:06:33 -040040/* for encoding cft->private value on file */
41#define BLKIOFILE_PRIVATE(x, val) (((x) << 16) | (val))
42/* What policy owns the file, proportional or throttle */
43#define BLKIOFILE_POLICY(val) (((val) >> 16) & 0xffff)
44#define BLKIOFILE_ATTR(val) ((val) & 0xffff)
45
Ben Blum67523c42010-03-10 15:22:11 -080046struct cgroup_subsys blkio_subsys = {
47 .name = "blkio",
48 .create = blkiocg_create,
49 .can_attach = blkiocg_can_attach,
50 .attach = blkiocg_attach,
51 .destroy = blkiocg_destroy,
52 .populate = blkiocg_populate,
53#ifdef CONFIG_BLK_CGROUP
54 /* note: blkio_subsys_id is otherwise defined in blk-cgroup.h */
55 .subsys_id = blkio_subsys_id,
56#endif
57 .use_id = 1,
58 .module = THIS_MODULE,
59};
60EXPORT_SYMBOL_GPL(blkio_subsys);
61
Gui Jianfeng34d0f172010-04-13 16:05:49 +080062static inline void blkio_policy_insert_node(struct blkio_cgroup *blkcg,
63 struct blkio_policy_node *pn)
64{
65 list_add(&pn->node, &blkcg->policy_list);
66}
67
Vivek Goyal062a6442010-09-15 17:06:33 -040068static inline bool cftype_blkg_same_policy(struct cftype *cft,
69 struct blkio_group *blkg)
70{
71 enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
72
73 if (blkg->plid == plid)
74 return 1;
75
76 return 0;
77}
78
79/* Determines if policy node matches cgroup file being accessed */
80static inline bool pn_matches_cftype(struct cftype *cft,
81 struct blkio_policy_node *pn)
82{
83 enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
84 int fileid = BLKIOFILE_ATTR(cft->private);
85
86 return (plid == pn->plid && fileid == pn->fileid);
87}
88
Gui Jianfeng34d0f172010-04-13 16:05:49 +080089/* Must be called with blkcg->lock held */
90static inline void blkio_policy_delete_node(struct blkio_policy_node *pn)
91{
92 list_del(&pn->node);
93}
94
95/* Must be called with blkcg->lock held */
96static struct blkio_policy_node *
Vivek Goyal062a6442010-09-15 17:06:33 -040097blkio_policy_search_node(const struct blkio_cgroup *blkcg, dev_t dev,
98 enum blkio_policy_id plid, int fileid)
Gui Jianfeng34d0f172010-04-13 16:05:49 +080099{
100 struct blkio_policy_node *pn;
101
102 list_for_each_entry(pn, &blkcg->policy_list, node) {
Vivek Goyal062a6442010-09-15 17:06:33 -0400103 if (pn->dev == dev && pn->plid == plid && pn->fileid == fileid)
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800104 return pn;
105 }
106
107 return NULL;
108}
109
Vivek Goyal31e4c282009-12-03 12:59:42 -0500110struct blkio_cgroup *cgroup_to_blkio_cgroup(struct cgroup *cgroup)
111{
112 return container_of(cgroup_subsys_state(cgroup, blkio_subsys_id),
113 struct blkio_cgroup, css);
114}
Vivek Goyal9d6a9862009-12-04 10:36:41 -0500115EXPORT_SYMBOL_GPL(cgroup_to_blkio_cgroup);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500116
Vivek Goyal062a6442010-09-15 17:06:33 -0400117static inline void
118blkio_update_group_weight(struct blkio_group *blkg, unsigned int weight)
119{
120 struct blkio_policy_type *blkiop;
121
122 list_for_each_entry(blkiop, &blkio_list, list) {
123 /* If this policy does not own the blkg, do not send updates */
124 if (blkiop->plid != blkg->plid)
125 continue;
126 if (blkiop->ops.blkio_update_group_weight_fn)
127 blkiop->ops.blkio_update_group_weight_fn(blkg, weight);
128 }
129}
130
Vivek Goyal4c9eefa2010-09-15 17:06:34 -0400131static inline void blkio_update_group_bps(struct blkio_group *blkg, u64 bps,
132 int fileid)
133{
134 struct blkio_policy_type *blkiop;
135
136 list_for_each_entry(blkiop, &blkio_list, list) {
137
138 /* If this policy does not own the blkg, do not send updates */
139 if (blkiop->plid != blkg->plid)
140 continue;
141
142 if (fileid == BLKIO_THROTL_read_bps_device
143 && blkiop->ops.blkio_update_group_read_bps_fn)
144 blkiop->ops.blkio_update_group_read_bps_fn(blkg, bps);
145
146 if (fileid == BLKIO_THROTL_write_bps_device
147 && blkiop->ops.blkio_update_group_write_bps_fn)
148 blkiop->ops.blkio_update_group_write_bps_fn(blkg, bps);
149 }
150}
151
Vivek Goyal7702e8f2010-09-15 17:06:36 -0400152static inline void blkio_update_group_iops(struct blkio_group *blkg,
153 unsigned int iops, int fileid)
154{
155 struct blkio_policy_type *blkiop;
156
157 list_for_each_entry(blkiop, &blkio_list, list) {
158
159 /* If this policy does not own the blkg, do not send updates */
160 if (blkiop->plid != blkg->plid)
161 continue;
162
163 if (fileid == BLKIO_THROTL_read_iops_device
164 && blkiop->ops.blkio_update_group_read_iops_fn)
165 blkiop->ops.blkio_update_group_read_iops_fn(blkg, iops);
166
167 if (fileid == BLKIO_THROTL_write_iops_device
168 && blkiop->ops.blkio_update_group_write_iops_fn)
169 blkiop->ops.blkio_update_group_write_iops_fn(blkg,iops);
170 }
171}
172
Divyesh Shah91952912010-04-01 15:01:41 -0700173/*
174 * Add to the appropriate stat variable depending on the request type.
175 * This should be called with the blkg->stats_lock held.
176 */
Divyesh Shah84c124d2010-04-09 08:31:19 +0200177static void blkio_add_stat(uint64_t *stat, uint64_t add, bool direction,
178 bool sync)
Divyesh Shah91952912010-04-01 15:01:41 -0700179{
Divyesh Shah84c124d2010-04-09 08:31:19 +0200180 if (direction)
181 stat[BLKIO_STAT_WRITE] += add;
Divyesh Shah91952912010-04-01 15:01:41 -0700182 else
Divyesh Shah84c124d2010-04-09 08:31:19 +0200183 stat[BLKIO_STAT_READ] += add;
184 if (sync)
185 stat[BLKIO_STAT_SYNC] += add;
Divyesh Shah91952912010-04-01 15:01:41 -0700186 else
Divyesh Shah84c124d2010-04-09 08:31:19 +0200187 stat[BLKIO_STAT_ASYNC] += add;
Divyesh Shah91952912010-04-01 15:01:41 -0700188}
189
Divyesh Shahcdc11842010-04-08 21:15:10 -0700190/*
191 * Decrements the appropriate stat variable if non-zero depending on the
192 * request type. Panics on value being zero.
193 * This should be called with the blkg->stats_lock held.
194 */
195static void blkio_check_and_dec_stat(uint64_t *stat, bool direction, bool sync)
196{
197 if (direction) {
198 BUG_ON(stat[BLKIO_STAT_WRITE] == 0);
199 stat[BLKIO_STAT_WRITE]--;
200 } else {
201 BUG_ON(stat[BLKIO_STAT_READ] == 0);
202 stat[BLKIO_STAT_READ]--;
203 }
204 if (sync) {
205 BUG_ON(stat[BLKIO_STAT_SYNC] == 0);
206 stat[BLKIO_STAT_SYNC]--;
207 } else {
208 BUG_ON(stat[BLKIO_STAT_ASYNC] == 0);
209 stat[BLKIO_STAT_ASYNC]--;
210 }
211}
212
213#ifdef CONFIG_DEBUG_BLK_CGROUP
Divyesh Shah812df482010-04-08 21:15:35 -0700214/* This should be called with the blkg->stats_lock held. */
215static void blkio_set_start_group_wait_time(struct blkio_group *blkg,
216 struct blkio_group *curr_blkg)
217{
218 if (blkio_blkg_waiting(&blkg->stats))
219 return;
220 if (blkg == curr_blkg)
221 return;
222 blkg->stats.start_group_wait_time = sched_clock();
223 blkio_mark_blkg_waiting(&blkg->stats);
224}
225
226/* This should be called with the blkg->stats_lock held. */
227static void blkio_update_group_wait_time(struct blkio_group_stats *stats)
228{
229 unsigned long long now;
230
231 if (!blkio_blkg_waiting(stats))
232 return;
233
234 now = sched_clock();
235 if (time_after64(now, stats->start_group_wait_time))
236 stats->group_wait_time += now - stats->start_group_wait_time;
237 blkio_clear_blkg_waiting(stats);
238}
239
240/* This should be called with the blkg->stats_lock held. */
241static void blkio_end_empty_time(struct blkio_group_stats *stats)
242{
243 unsigned long long now;
244
245 if (!blkio_blkg_empty(stats))
246 return;
247
248 now = sched_clock();
249 if (time_after64(now, stats->start_empty_time))
250 stats->empty_time += now - stats->start_empty_time;
251 blkio_clear_blkg_empty(stats);
252}
253
254void blkiocg_update_set_idle_time_stats(struct blkio_group *blkg)
255{
256 unsigned long flags;
257
258 spin_lock_irqsave(&blkg->stats_lock, flags);
259 BUG_ON(blkio_blkg_idling(&blkg->stats));
260 blkg->stats.start_idle_time = sched_clock();
261 blkio_mark_blkg_idling(&blkg->stats);
262 spin_unlock_irqrestore(&blkg->stats_lock, flags);
263}
264EXPORT_SYMBOL_GPL(blkiocg_update_set_idle_time_stats);
265
266void blkiocg_update_idle_time_stats(struct blkio_group *blkg)
267{
268 unsigned long flags;
269 unsigned long long now;
270 struct blkio_group_stats *stats;
271
272 spin_lock_irqsave(&blkg->stats_lock, flags);
273 stats = &blkg->stats;
274 if (blkio_blkg_idling(stats)) {
275 now = sched_clock();
276 if (time_after64(now, stats->start_idle_time))
277 stats->idle_time += now - stats->start_idle_time;
278 blkio_clear_blkg_idling(stats);
279 }
280 spin_unlock_irqrestore(&blkg->stats_lock, flags);
281}
282EXPORT_SYMBOL_GPL(blkiocg_update_idle_time_stats);
283
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200284void blkiocg_update_avg_queue_size_stats(struct blkio_group *blkg)
Divyesh Shahcdc11842010-04-08 21:15:10 -0700285{
286 unsigned long flags;
287 struct blkio_group_stats *stats;
288
289 spin_lock_irqsave(&blkg->stats_lock, flags);
290 stats = &blkg->stats;
291 stats->avg_queue_size_sum +=
292 stats->stat_arr[BLKIO_STAT_QUEUED][BLKIO_STAT_READ] +
293 stats->stat_arr[BLKIO_STAT_QUEUED][BLKIO_STAT_WRITE];
294 stats->avg_queue_size_samples++;
Divyesh Shah812df482010-04-08 21:15:35 -0700295 blkio_update_group_wait_time(stats);
Divyesh Shahcdc11842010-04-08 21:15:10 -0700296 spin_unlock_irqrestore(&blkg->stats_lock, flags);
297}
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200298EXPORT_SYMBOL_GPL(blkiocg_update_avg_queue_size_stats);
299
Vivek Goyale5ff0822010-04-26 19:25:11 +0200300void blkiocg_set_start_empty_time(struct blkio_group *blkg)
Divyesh Shah28baf442010-04-14 11:22:38 +0200301{
302 unsigned long flags;
303 struct blkio_group_stats *stats;
304
305 spin_lock_irqsave(&blkg->stats_lock, flags);
306 stats = &blkg->stats;
307
308 if (stats->stat_arr[BLKIO_STAT_QUEUED][BLKIO_STAT_READ] ||
309 stats->stat_arr[BLKIO_STAT_QUEUED][BLKIO_STAT_WRITE]) {
310 spin_unlock_irqrestore(&blkg->stats_lock, flags);
311 return;
312 }
313
314 /*
Vivek Goyale5ff0822010-04-26 19:25:11 +0200315 * group is already marked empty. This can happen if cfqq got new
316 * request in parent group and moved to this group while being added
317 * to service tree. Just ignore the event and move on.
Divyesh Shah28baf442010-04-14 11:22:38 +0200318 */
Vivek Goyale5ff0822010-04-26 19:25:11 +0200319 if(blkio_blkg_empty(stats)) {
320 spin_unlock_irqrestore(&blkg->stats_lock, flags);
321 return;
322 }
323
Divyesh Shah28baf442010-04-14 11:22:38 +0200324 stats->start_empty_time = sched_clock();
325 blkio_mark_blkg_empty(stats);
326 spin_unlock_irqrestore(&blkg->stats_lock, flags);
327}
328EXPORT_SYMBOL_GPL(blkiocg_set_start_empty_time);
329
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200330void blkiocg_update_dequeue_stats(struct blkio_group *blkg,
331 unsigned long dequeue)
332{
333 blkg->stats.dequeue += dequeue;
334}
335EXPORT_SYMBOL_GPL(blkiocg_update_dequeue_stats);
Divyesh Shah812df482010-04-08 21:15:35 -0700336#else
337static inline void blkio_set_start_group_wait_time(struct blkio_group *blkg,
338 struct blkio_group *curr_blkg) {}
339static inline void blkio_end_empty_time(struct blkio_group_stats *stats) {}
Divyesh Shahcdc11842010-04-08 21:15:10 -0700340#endif
341
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200342void blkiocg_update_io_add_stats(struct blkio_group *blkg,
Divyesh Shahcdc11842010-04-08 21:15:10 -0700343 struct blkio_group *curr_blkg, bool direction,
344 bool sync)
345{
346 unsigned long flags;
347
348 spin_lock_irqsave(&blkg->stats_lock, flags);
349 blkio_add_stat(blkg->stats.stat_arr[BLKIO_STAT_QUEUED], 1, direction,
350 sync);
Divyesh Shah812df482010-04-08 21:15:35 -0700351 blkio_end_empty_time(&blkg->stats);
352 blkio_set_start_group_wait_time(blkg, curr_blkg);
Divyesh Shahcdc11842010-04-08 21:15:10 -0700353 spin_unlock_irqrestore(&blkg->stats_lock, flags);
354}
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200355EXPORT_SYMBOL_GPL(blkiocg_update_io_add_stats);
Divyesh Shahcdc11842010-04-08 21:15:10 -0700356
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200357void blkiocg_update_io_remove_stats(struct blkio_group *blkg,
Divyesh Shahcdc11842010-04-08 21:15:10 -0700358 bool direction, bool sync)
359{
360 unsigned long flags;
361
362 spin_lock_irqsave(&blkg->stats_lock, flags);
363 blkio_check_and_dec_stat(blkg->stats.stat_arr[BLKIO_STAT_QUEUED],
364 direction, sync);
365 spin_unlock_irqrestore(&blkg->stats_lock, flags);
366}
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200367EXPORT_SYMBOL_GPL(blkiocg_update_io_remove_stats);
Divyesh Shahcdc11842010-04-08 21:15:10 -0700368
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700369void blkiocg_update_timeslice_used(struct blkio_group *blkg, unsigned long time)
Vivek Goyal22084192009-12-03 12:59:49 -0500370{
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700371 unsigned long flags;
372
373 spin_lock_irqsave(&blkg->stats_lock, flags);
374 blkg->stats.time += time;
375 spin_unlock_irqrestore(&blkg->stats_lock, flags);
Vivek Goyal22084192009-12-03 12:59:49 -0500376}
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700377EXPORT_SYMBOL_GPL(blkiocg_update_timeslice_used);
Vivek Goyal22084192009-12-03 12:59:49 -0500378
Divyesh Shah84c124d2010-04-09 08:31:19 +0200379void blkiocg_update_dispatch_stats(struct blkio_group *blkg,
380 uint64_t bytes, bool direction, bool sync)
Divyesh Shah91952912010-04-01 15:01:41 -0700381{
382 struct blkio_group_stats *stats;
383 unsigned long flags;
384
385 spin_lock_irqsave(&blkg->stats_lock, flags);
386 stats = &blkg->stats;
Divyesh Shah84c124d2010-04-09 08:31:19 +0200387 stats->sectors += bytes >> 9;
388 blkio_add_stat(stats->stat_arr[BLKIO_STAT_SERVICED], 1, direction,
389 sync);
390 blkio_add_stat(stats->stat_arr[BLKIO_STAT_SERVICE_BYTES], bytes,
391 direction, sync);
Divyesh Shah91952912010-04-01 15:01:41 -0700392 spin_unlock_irqrestore(&blkg->stats_lock, flags);
393}
Divyesh Shah84c124d2010-04-09 08:31:19 +0200394EXPORT_SYMBOL_GPL(blkiocg_update_dispatch_stats);
Divyesh Shah91952912010-04-01 15:01:41 -0700395
Divyesh Shah84c124d2010-04-09 08:31:19 +0200396void blkiocg_update_completion_stats(struct blkio_group *blkg,
397 uint64_t start_time, uint64_t io_start_time, bool direction, bool sync)
Divyesh Shah91952912010-04-01 15:01:41 -0700398{
399 struct blkio_group_stats *stats;
400 unsigned long flags;
401 unsigned long long now = sched_clock();
402
403 spin_lock_irqsave(&blkg->stats_lock, flags);
404 stats = &blkg->stats;
Divyesh Shah84c124d2010-04-09 08:31:19 +0200405 if (time_after64(now, io_start_time))
406 blkio_add_stat(stats->stat_arr[BLKIO_STAT_SERVICE_TIME],
407 now - io_start_time, direction, sync);
408 if (time_after64(io_start_time, start_time))
409 blkio_add_stat(stats->stat_arr[BLKIO_STAT_WAIT_TIME],
410 io_start_time - start_time, direction, sync);
Divyesh Shah91952912010-04-01 15:01:41 -0700411 spin_unlock_irqrestore(&blkg->stats_lock, flags);
412}
Divyesh Shah84c124d2010-04-09 08:31:19 +0200413EXPORT_SYMBOL_GPL(blkiocg_update_completion_stats);
Divyesh Shah91952912010-04-01 15:01:41 -0700414
Divyesh Shah812d4022010-04-08 21:14:23 -0700415void blkiocg_update_io_merged_stats(struct blkio_group *blkg, bool direction,
416 bool sync)
417{
418 unsigned long flags;
419
420 spin_lock_irqsave(&blkg->stats_lock, flags);
421 blkio_add_stat(blkg->stats.stat_arr[BLKIO_STAT_MERGED], 1, direction,
422 sync);
423 spin_unlock_irqrestore(&blkg->stats_lock, flags);
424}
425EXPORT_SYMBOL_GPL(blkiocg_update_io_merged_stats);
426
Vivek Goyal31e4c282009-12-03 12:59:42 -0500427void blkiocg_add_blkio_group(struct blkio_cgroup *blkcg,
Vivek Goyal062a6442010-09-15 17:06:33 -0400428 struct blkio_group *blkg, void *key, dev_t dev,
429 enum blkio_policy_id plid)
Vivek Goyal31e4c282009-12-03 12:59:42 -0500430{
431 unsigned long flags;
432
433 spin_lock_irqsave(&blkcg->lock, flags);
Divyesh Shah8d2a91f2010-04-16 08:10:51 +0200434 spin_lock_init(&blkg->stats_lock);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500435 rcu_assign_pointer(blkg->key, key);
Vivek Goyalb1c35762009-12-03 12:59:47 -0500436 blkg->blkcg_id = css_id(&blkcg->css);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500437 hlist_add_head_rcu(&blkg->blkcg_node, &blkcg->blkg_list);
Vivek Goyal062a6442010-09-15 17:06:33 -0400438 blkg->plid = plid;
Vivek Goyal31e4c282009-12-03 12:59:42 -0500439 spin_unlock_irqrestore(&blkcg->lock, flags);
Vivek Goyal2868ef72009-12-03 12:59:48 -0500440 /* Need to take css reference ? */
441 cgroup_path(blkcg->css.cgroup, blkg->path, sizeof(blkg->path));
Vivek Goyal22084192009-12-03 12:59:49 -0500442 blkg->dev = dev;
Vivek Goyal31e4c282009-12-03 12:59:42 -0500443}
Vivek Goyal9d6a9862009-12-04 10:36:41 -0500444EXPORT_SYMBOL_GPL(blkiocg_add_blkio_group);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500445
Vivek Goyalb1c35762009-12-03 12:59:47 -0500446static void __blkiocg_del_blkio_group(struct blkio_group *blkg)
447{
448 hlist_del_init_rcu(&blkg->blkcg_node);
449 blkg->blkcg_id = 0;
450}
451
452/*
453 * returns 0 if blkio_group was still on cgroup list. Otherwise returns 1
454 * indicating that blk_group was unhashed by the time we got to it.
455 */
Vivek Goyal31e4c282009-12-03 12:59:42 -0500456int blkiocg_del_blkio_group(struct blkio_group *blkg)
457{
Vivek Goyalb1c35762009-12-03 12:59:47 -0500458 struct blkio_cgroup *blkcg;
459 unsigned long flags;
460 struct cgroup_subsys_state *css;
461 int ret = 1;
462
463 rcu_read_lock();
464 css = css_lookup(&blkio_subsys, blkg->blkcg_id);
Jens Axboe0f3942a2010-05-03 14:28:55 +0200465 if (css) {
466 blkcg = container_of(css, struct blkio_cgroup, css);
467 spin_lock_irqsave(&blkcg->lock, flags);
468 if (!hlist_unhashed(&blkg->blkcg_node)) {
469 __blkiocg_del_blkio_group(blkg);
470 ret = 0;
471 }
472 spin_unlock_irqrestore(&blkcg->lock, flags);
Vivek Goyalb1c35762009-12-03 12:59:47 -0500473 }
Jens Axboe0f3942a2010-05-03 14:28:55 +0200474
Vivek Goyalb1c35762009-12-03 12:59:47 -0500475 rcu_read_unlock();
476 return ret;
Vivek Goyal31e4c282009-12-03 12:59:42 -0500477}
Vivek Goyal9d6a9862009-12-04 10:36:41 -0500478EXPORT_SYMBOL_GPL(blkiocg_del_blkio_group);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500479
480/* called under rcu_read_lock(). */
481struct blkio_group *blkiocg_lookup_group(struct blkio_cgroup *blkcg, void *key)
482{
483 struct blkio_group *blkg;
484 struct hlist_node *n;
485 void *__key;
486
487 hlist_for_each_entry_rcu(blkg, n, &blkcg->blkg_list, blkcg_node) {
488 __key = blkg->key;
489 if (__key == key)
490 return blkg;
491 }
492
493 return NULL;
494}
Vivek Goyal9d6a9862009-12-04 10:36:41 -0500495EXPORT_SYMBOL_GPL(blkiocg_lookup_group);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500496
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700497static int
Divyesh Shah84c124d2010-04-09 08:31:19 +0200498blkiocg_reset_stats(struct cgroup *cgroup, struct cftype *cftype, u64 val)
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700499{
500 struct blkio_cgroup *blkcg;
501 struct blkio_group *blkg;
Divyesh Shah812df482010-04-08 21:15:35 -0700502 struct blkio_group_stats *stats;
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700503 struct hlist_node *n;
Divyesh Shahcdc11842010-04-08 21:15:10 -0700504 uint64_t queued[BLKIO_STAT_TOTAL];
505 int i;
Divyesh Shah812df482010-04-08 21:15:35 -0700506#ifdef CONFIG_DEBUG_BLK_CGROUP
507 bool idling, waiting, empty;
508 unsigned long long now = sched_clock();
509#endif
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700510
511 blkcg = cgroup_to_blkio_cgroup(cgroup);
512 spin_lock_irq(&blkcg->lock);
513 hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) {
514 spin_lock(&blkg->stats_lock);
Divyesh Shah812df482010-04-08 21:15:35 -0700515 stats = &blkg->stats;
516#ifdef CONFIG_DEBUG_BLK_CGROUP
517 idling = blkio_blkg_idling(stats);
518 waiting = blkio_blkg_waiting(stats);
519 empty = blkio_blkg_empty(stats);
520#endif
Divyesh Shahcdc11842010-04-08 21:15:10 -0700521 for (i = 0; i < BLKIO_STAT_TOTAL; i++)
Divyesh Shah812df482010-04-08 21:15:35 -0700522 queued[i] = stats->stat_arr[BLKIO_STAT_QUEUED][i];
523 memset(stats, 0, sizeof(struct blkio_group_stats));
Divyesh Shahcdc11842010-04-08 21:15:10 -0700524 for (i = 0; i < BLKIO_STAT_TOTAL; i++)
Divyesh Shah812df482010-04-08 21:15:35 -0700525 stats->stat_arr[BLKIO_STAT_QUEUED][i] = queued[i];
526#ifdef CONFIG_DEBUG_BLK_CGROUP
527 if (idling) {
528 blkio_mark_blkg_idling(stats);
529 stats->start_idle_time = now;
530 }
531 if (waiting) {
532 blkio_mark_blkg_waiting(stats);
533 stats->start_group_wait_time = now;
534 }
535 if (empty) {
536 blkio_mark_blkg_empty(stats);
537 stats->start_empty_time = now;
538 }
539#endif
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700540 spin_unlock(&blkg->stats_lock);
541 }
542 spin_unlock_irq(&blkcg->lock);
543 return 0;
544}
545
Divyesh Shah84c124d2010-04-09 08:31:19 +0200546static void blkio_get_key_name(enum stat_sub_type type, dev_t dev, char *str,
547 int chars_left, bool diskname_only)
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700548{
Divyesh Shah84c124d2010-04-09 08:31:19 +0200549 snprintf(str, chars_left, "%d:%d", MAJOR(dev), MINOR(dev));
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700550 chars_left -= strlen(str);
551 if (chars_left <= 0) {
552 printk(KERN_WARNING
553 "Possibly incorrect cgroup stat display format");
554 return;
555 }
Divyesh Shah84c124d2010-04-09 08:31:19 +0200556 if (diskname_only)
557 return;
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700558 switch (type) {
Divyesh Shah84c124d2010-04-09 08:31:19 +0200559 case BLKIO_STAT_READ:
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700560 strlcat(str, " Read", chars_left);
561 break;
Divyesh Shah84c124d2010-04-09 08:31:19 +0200562 case BLKIO_STAT_WRITE:
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700563 strlcat(str, " Write", chars_left);
564 break;
Divyesh Shah84c124d2010-04-09 08:31:19 +0200565 case BLKIO_STAT_SYNC:
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700566 strlcat(str, " Sync", chars_left);
567 break;
Divyesh Shah84c124d2010-04-09 08:31:19 +0200568 case BLKIO_STAT_ASYNC:
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700569 strlcat(str, " Async", chars_left);
570 break;
Divyesh Shah84c124d2010-04-09 08:31:19 +0200571 case BLKIO_STAT_TOTAL:
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700572 strlcat(str, " Total", chars_left);
573 break;
574 default:
575 strlcat(str, " Invalid", chars_left);
576 }
577}
578
Divyesh Shah84c124d2010-04-09 08:31:19 +0200579static uint64_t blkio_fill_stat(char *str, int chars_left, uint64_t val,
580 struct cgroup_map_cb *cb, dev_t dev)
581{
582 blkio_get_key_name(0, dev, str, chars_left, true);
583 cb->fill(cb, str, val);
584 return val;
585}
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700586
Divyesh Shah84c124d2010-04-09 08:31:19 +0200587/* This should be called with blkg->stats_lock held */
588static uint64_t blkio_get_stat(struct blkio_group *blkg,
589 struct cgroup_map_cb *cb, dev_t dev, enum stat_type type)
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700590{
591 uint64_t disk_total;
592 char key_str[MAX_KEY_LEN];
Divyesh Shah84c124d2010-04-09 08:31:19 +0200593 enum stat_sub_type sub_type;
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700594
Divyesh Shah84c124d2010-04-09 08:31:19 +0200595 if (type == BLKIO_STAT_TIME)
596 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
597 blkg->stats.time, cb, dev);
598 if (type == BLKIO_STAT_SECTORS)
599 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
600 blkg->stats.sectors, cb, dev);
601#ifdef CONFIG_DEBUG_BLK_CGROUP
Divyesh Shahcdc11842010-04-08 21:15:10 -0700602 if (type == BLKIO_STAT_AVG_QUEUE_SIZE) {
603 uint64_t sum = blkg->stats.avg_queue_size_sum;
604 uint64_t samples = blkg->stats.avg_queue_size_samples;
605 if (samples)
606 do_div(sum, samples);
607 else
608 sum = 0;
609 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1, sum, cb, dev);
610 }
Divyesh Shah812df482010-04-08 21:15:35 -0700611 if (type == BLKIO_STAT_GROUP_WAIT_TIME)
612 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
613 blkg->stats.group_wait_time, cb, dev);
614 if (type == BLKIO_STAT_IDLE_TIME)
615 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
616 blkg->stats.idle_time, cb, dev);
617 if (type == BLKIO_STAT_EMPTY_TIME)
618 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
619 blkg->stats.empty_time, cb, dev);
Divyesh Shah84c124d2010-04-09 08:31:19 +0200620 if (type == BLKIO_STAT_DEQUEUE)
621 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
622 blkg->stats.dequeue, cb, dev);
623#endif
624
625 for (sub_type = BLKIO_STAT_READ; sub_type < BLKIO_STAT_TOTAL;
626 sub_type++) {
627 blkio_get_key_name(sub_type, dev, key_str, MAX_KEY_LEN, false);
628 cb->fill(cb, key_str, blkg->stats.stat_arr[type][sub_type]);
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700629 }
Divyesh Shah84c124d2010-04-09 08:31:19 +0200630 disk_total = blkg->stats.stat_arr[type][BLKIO_STAT_READ] +
631 blkg->stats.stat_arr[type][BLKIO_STAT_WRITE];
632 blkio_get_key_name(BLKIO_STAT_TOTAL, dev, key_str, MAX_KEY_LEN, false);
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700633 cb->fill(cb, key_str, disk_total);
634 return disk_total;
635}
636
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800637static int blkio_check_dev_num(dev_t dev)
638{
639 int part = 0;
640 struct gendisk *disk;
641
642 disk = get_gendisk(dev, &part);
643 if (!disk || part)
644 return -ENODEV;
645
646 return 0;
647}
648
649static int blkio_policy_parse_and_set(char *buf,
Vivek Goyal062a6442010-09-15 17:06:33 -0400650 struct blkio_policy_node *newpn, enum blkio_policy_id plid, int fileid)
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800651{
652 char *s[4], *p, *major_s = NULL, *minor_s = NULL;
653 int ret;
Vivek Goyal7702e8f2010-09-15 17:06:36 -0400654 unsigned long major, minor, temp, iops;
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800655 int i = 0;
656 dev_t dev;
Vivek Goyal4c9eefa2010-09-15 17:06:34 -0400657 u64 bps;
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800658
659 memset(s, 0, sizeof(s));
660
661 while ((p = strsep(&buf, " ")) != NULL) {
662 if (!*p)
663 continue;
664
665 s[i++] = p;
666
667 /* Prevent from inputing too many things */
668 if (i == 3)
669 break;
670 }
671
672 if (i != 2)
673 return -EINVAL;
674
675 p = strsep(&s[0], ":");
676 if (p != NULL)
677 major_s = p;
678 else
679 return -EINVAL;
680
681 minor_s = s[0];
682 if (!minor_s)
683 return -EINVAL;
684
685 ret = strict_strtoul(major_s, 10, &major);
686 if (ret)
687 return -EINVAL;
688
689 ret = strict_strtoul(minor_s, 10, &minor);
690 if (ret)
691 return -EINVAL;
692
693 dev = MKDEV(major, minor);
694
695 ret = blkio_check_dev_num(dev);
696 if (ret)
697 return ret;
698
699 newpn->dev = dev;
700
701 if (s[1] == NULL)
702 return -EINVAL;
703
Vivek Goyal062a6442010-09-15 17:06:33 -0400704 switch (plid) {
705 case BLKIO_POLICY_PROP:
706 ret = strict_strtoul(s[1], 10, &temp);
707 if (ret || (temp < BLKIO_WEIGHT_MIN && temp > 0) ||
708 temp > BLKIO_WEIGHT_MAX)
709 return -EINVAL;
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800710
Vivek Goyal062a6442010-09-15 17:06:33 -0400711 newpn->plid = plid;
712 newpn->fileid = fileid;
Vivek Goyal4c9eefa2010-09-15 17:06:34 -0400713 newpn->val.weight = temp;
714 break;
715 case BLKIO_POLICY_THROTL:
Vivek Goyal7702e8f2010-09-15 17:06:36 -0400716 switch(fileid) {
717 case BLKIO_THROTL_read_bps_device:
718 case BLKIO_THROTL_write_bps_device:
719 ret = strict_strtoull(s[1], 10, &bps);
720 if (ret)
721 return -EINVAL;
Vivek Goyal4c9eefa2010-09-15 17:06:34 -0400722
Vivek Goyal7702e8f2010-09-15 17:06:36 -0400723 newpn->plid = plid;
724 newpn->fileid = fileid;
725 newpn->val.bps = bps;
726 break;
727 case BLKIO_THROTL_read_iops_device:
728 case BLKIO_THROTL_write_iops_device:
729 ret = strict_strtoul(s[1], 10, &iops);
730 if (ret)
731 return -EINVAL;
732
733 newpn->plid = plid;
734 newpn->fileid = fileid;
735 newpn->val.iops = iops;
736 break;
737 }
Vivek Goyal062a6442010-09-15 17:06:33 -0400738 break;
739 default:
740 BUG();
741 }
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800742
743 return 0;
744}
745
746unsigned int blkcg_get_weight(struct blkio_cgroup *blkcg,
747 dev_t dev)
748{
749 struct blkio_policy_node *pn;
750
Vivek Goyal062a6442010-09-15 17:06:33 -0400751 pn = blkio_policy_search_node(blkcg, dev, BLKIO_POLICY_PROP,
752 BLKIO_PROP_weight_device);
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800753 if (pn)
Vivek Goyal4c9eefa2010-09-15 17:06:34 -0400754 return pn->val.weight;
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800755 else
756 return blkcg->weight;
757}
758EXPORT_SYMBOL_GPL(blkcg_get_weight);
759
Vivek Goyal4c9eefa2010-09-15 17:06:34 -0400760uint64_t blkcg_get_read_bps(struct blkio_cgroup *blkcg, dev_t dev)
761{
762 struct blkio_policy_node *pn;
763
764 pn = blkio_policy_search_node(blkcg, dev, BLKIO_POLICY_THROTL,
765 BLKIO_THROTL_read_bps_device);
766 if (pn)
767 return pn->val.bps;
768 else
769 return -1;
770}
771
772uint64_t blkcg_get_write_bps(struct blkio_cgroup *blkcg, dev_t dev)
773{
774 struct blkio_policy_node *pn;
775 pn = blkio_policy_search_node(blkcg, dev, BLKIO_POLICY_THROTL,
776 BLKIO_THROTL_write_bps_device);
777 if (pn)
778 return pn->val.bps;
779 else
780 return -1;
781}
782
Vivek Goyal7702e8f2010-09-15 17:06:36 -0400783unsigned int blkcg_get_read_iops(struct blkio_cgroup *blkcg, dev_t dev)
784{
785 struct blkio_policy_node *pn;
786
787 pn = blkio_policy_search_node(blkcg, dev, BLKIO_POLICY_THROTL,
788 BLKIO_THROTL_read_iops_device);
789 if (pn)
790 return pn->val.iops;
791 else
792 return -1;
793}
794
795unsigned int blkcg_get_write_iops(struct blkio_cgroup *blkcg, dev_t dev)
796{
797 struct blkio_policy_node *pn;
798 pn = blkio_policy_search_node(blkcg, dev, BLKIO_POLICY_THROTL,
799 BLKIO_THROTL_write_iops_device);
800 if (pn)
801 return pn->val.iops;
802 else
803 return -1;
804}
805
Vivek Goyal062a6442010-09-15 17:06:33 -0400806/* Checks whether user asked for deleting a policy rule */
807static bool blkio_delete_rule_command(struct blkio_policy_node *pn)
808{
809 switch(pn->plid) {
810 case BLKIO_POLICY_PROP:
Vivek Goyal4c9eefa2010-09-15 17:06:34 -0400811 if (pn->val.weight == 0)
812 return 1;
813 break;
814 case BLKIO_POLICY_THROTL:
Vivek Goyal7702e8f2010-09-15 17:06:36 -0400815 switch(pn->fileid) {
816 case BLKIO_THROTL_read_bps_device:
817 case BLKIO_THROTL_write_bps_device:
818 if (pn->val.bps == 0)
819 return 1;
820 break;
821 case BLKIO_THROTL_read_iops_device:
822 case BLKIO_THROTL_write_iops_device:
823 if (pn->val.iops == 0)
824 return 1;
825 }
Vivek Goyal062a6442010-09-15 17:06:33 -0400826 break;
827 default:
828 BUG();
829 }
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800830
Vivek Goyal062a6442010-09-15 17:06:33 -0400831 return 0;
832}
833
834static void blkio_update_policy_rule(struct blkio_policy_node *oldpn,
835 struct blkio_policy_node *newpn)
836{
837 switch(oldpn->plid) {
838 case BLKIO_POLICY_PROP:
Vivek Goyal4c9eefa2010-09-15 17:06:34 -0400839 oldpn->val.weight = newpn->val.weight;
840 break;
841 case BLKIO_POLICY_THROTL:
Vivek Goyal7702e8f2010-09-15 17:06:36 -0400842 switch(newpn->fileid) {
843 case BLKIO_THROTL_read_bps_device:
844 case BLKIO_THROTL_write_bps_device:
845 oldpn->val.bps = newpn->val.bps;
846 break;
847 case BLKIO_THROTL_read_iops_device:
848 case BLKIO_THROTL_write_iops_device:
849 oldpn->val.iops = newpn->val.iops;
850 }
Vivek Goyal062a6442010-09-15 17:06:33 -0400851 break;
852 default:
853 BUG();
854 }
855}
856
857/*
858 * Some rules/values in blkg have changed. Propogate those to respective
859 * policies.
860 */
861static void blkio_update_blkg_policy(struct blkio_cgroup *blkcg,
862 struct blkio_group *blkg, struct blkio_policy_node *pn)
863{
Vivek Goyal7702e8f2010-09-15 17:06:36 -0400864 unsigned int weight, iops;
Vivek Goyal4c9eefa2010-09-15 17:06:34 -0400865 u64 bps;
Vivek Goyal062a6442010-09-15 17:06:33 -0400866
867 switch(pn->plid) {
868 case BLKIO_POLICY_PROP:
Vivek Goyal4c9eefa2010-09-15 17:06:34 -0400869 weight = pn->val.weight ? pn->val.weight :
Vivek Goyal062a6442010-09-15 17:06:33 -0400870 blkcg->weight;
871 blkio_update_group_weight(blkg, weight);
872 break;
Vivek Goyal4c9eefa2010-09-15 17:06:34 -0400873 case BLKIO_POLICY_THROTL:
874 switch(pn->fileid) {
875 case BLKIO_THROTL_read_bps_device:
876 case BLKIO_THROTL_write_bps_device:
877 bps = pn->val.bps ? pn->val.bps : (-1);
878 blkio_update_group_bps(blkg, bps, pn->fileid);
879 break;
Vivek Goyal7702e8f2010-09-15 17:06:36 -0400880 case BLKIO_THROTL_read_iops_device:
881 case BLKIO_THROTL_write_iops_device:
882 iops = pn->val.iops ? pn->val.iops : (-1);
883 blkio_update_group_iops(blkg, iops, pn->fileid);
884 break;
Vivek Goyal4c9eefa2010-09-15 17:06:34 -0400885 }
886 break;
Vivek Goyal062a6442010-09-15 17:06:33 -0400887 default:
888 BUG();
889 }
890}
891
892/*
893 * A policy node rule has been updated. Propogate this update to all the
894 * block groups which might be affected by this update.
895 */
896static void blkio_update_policy_node_blkg(struct blkio_cgroup *blkcg,
897 struct blkio_policy_node *pn)
898{
899 struct blkio_group *blkg;
900 struct hlist_node *n;
901
902 spin_lock(&blkio_list_lock);
903 spin_lock_irq(&blkcg->lock);
904
905 hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) {
906 if (pn->dev != blkg->dev || pn->plid != blkg->plid)
907 continue;
908 blkio_update_blkg_policy(blkcg, blkg, pn);
909 }
910
911 spin_unlock_irq(&blkcg->lock);
912 spin_unlock(&blkio_list_lock);
913}
914
915static int blkiocg_file_write(struct cgroup *cgrp, struct cftype *cft,
916 const char *buffer)
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800917{
918 int ret = 0;
919 char *buf;
920 struct blkio_policy_node *newpn, *pn;
921 struct blkio_cgroup *blkcg;
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800922 int keep_newpn = 0;
Vivek Goyal062a6442010-09-15 17:06:33 -0400923 enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
924 int fileid = BLKIOFILE_ATTR(cft->private);
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800925
926 buf = kstrdup(buffer, GFP_KERNEL);
927 if (!buf)
928 return -ENOMEM;
929
930 newpn = kzalloc(sizeof(*newpn), GFP_KERNEL);
931 if (!newpn) {
932 ret = -ENOMEM;
933 goto free_buf;
934 }
935
Vivek Goyal062a6442010-09-15 17:06:33 -0400936 ret = blkio_policy_parse_and_set(buf, newpn, plid, fileid);
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800937 if (ret)
938 goto free_newpn;
939
940 blkcg = cgroup_to_blkio_cgroup(cgrp);
941
942 spin_lock_irq(&blkcg->lock);
943
Vivek Goyal062a6442010-09-15 17:06:33 -0400944 pn = blkio_policy_search_node(blkcg, newpn->dev, plid, fileid);
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800945 if (!pn) {
Vivek Goyal062a6442010-09-15 17:06:33 -0400946 if (!blkio_delete_rule_command(newpn)) {
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800947 blkio_policy_insert_node(blkcg, newpn);
948 keep_newpn = 1;
949 }
950 spin_unlock_irq(&blkcg->lock);
951 goto update_io_group;
952 }
953
Vivek Goyal062a6442010-09-15 17:06:33 -0400954 if (blkio_delete_rule_command(newpn)) {
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800955 blkio_policy_delete_node(pn);
956 spin_unlock_irq(&blkcg->lock);
957 goto update_io_group;
958 }
959 spin_unlock_irq(&blkcg->lock);
960
Vivek Goyal062a6442010-09-15 17:06:33 -0400961 blkio_update_policy_rule(pn, newpn);
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800962
963update_io_group:
Vivek Goyal062a6442010-09-15 17:06:33 -0400964 blkio_update_policy_node_blkg(blkcg, newpn);
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800965
966free_newpn:
967 if (!keep_newpn)
968 kfree(newpn);
969free_buf:
970 kfree(buf);
971 return ret;
972}
973
Vivek Goyal062a6442010-09-15 17:06:33 -0400974static void
975blkio_print_policy_node(struct seq_file *m, struct blkio_policy_node *pn)
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800976{
Vivek Goyal062a6442010-09-15 17:06:33 -0400977 switch(pn->plid) {
978 case BLKIO_POLICY_PROP:
979 if (pn->fileid == BLKIO_PROP_weight_device)
980 seq_printf(m, "%u:%u\t%u\n", MAJOR(pn->dev),
Vivek Goyal4c9eefa2010-09-15 17:06:34 -0400981 MINOR(pn->dev), pn->val.weight);
982 break;
983 case BLKIO_POLICY_THROTL:
Vivek Goyal7702e8f2010-09-15 17:06:36 -0400984 switch(pn->fileid) {
985 case BLKIO_THROTL_read_bps_device:
986 case BLKIO_THROTL_write_bps_device:
Vivek Goyal4c9eefa2010-09-15 17:06:34 -0400987 seq_printf(m, "%u:%u\t%llu\n", MAJOR(pn->dev),
988 MINOR(pn->dev), pn->val.bps);
Vivek Goyal7702e8f2010-09-15 17:06:36 -0400989 break;
990 case BLKIO_THROTL_read_iops_device:
991 case BLKIO_THROTL_write_iops_device:
992 seq_printf(m, "%u:%u\t%u\n", MAJOR(pn->dev),
993 MINOR(pn->dev), pn->val.iops);
994 break;
995 }
Vivek Goyal062a6442010-09-15 17:06:33 -0400996 break;
997 default:
998 BUG();
999 }
1000}
1001
1002/* cgroup files which read their data from policy nodes end up here */
1003static void blkio_read_policy_node_files(struct cftype *cft,
1004 struct blkio_cgroup *blkcg, struct seq_file *m)
1005{
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001006 struct blkio_policy_node *pn;
1007
Jens Axboe0f3942a2010-05-03 14:28:55 +02001008 if (!list_empty(&blkcg->policy_list)) {
1009 spin_lock_irq(&blkcg->lock);
1010 list_for_each_entry(pn, &blkcg->policy_list, node) {
Vivek Goyal062a6442010-09-15 17:06:33 -04001011 if (!pn_matches_cftype(cft, pn))
1012 continue;
1013 blkio_print_policy_node(m, pn);
Jens Axboe0f3942a2010-05-03 14:28:55 +02001014 }
1015 spin_unlock_irq(&blkcg->lock);
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001016 }
Vivek Goyal062a6442010-09-15 17:06:33 -04001017}
1018
1019static int blkiocg_file_read(struct cgroup *cgrp, struct cftype *cft,
1020 struct seq_file *m)
1021{
1022 struct blkio_cgroup *blkcg;
1023 enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
1024 int name = BLKIOFILE_ATTR(cft->private);
1025
1026 blkcg = cgroup_to_blkio_cgroup(cgrp);
1027
1028 switch(plid) {
1029 case BLKIO_POLICY_PROP:
1030 switch(name) {
1031 case BLKIO_PROP_weight_device:
1032 blkio_read_policy_node_files(cft, blkcg, m);
1033 return 0;
1034 default:
1035 BUG();
1036 }
1037 break;
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001038 case BLKIO_POLICY_THROTL:
1039 switch(name){
1040 case BLKIO_THROTL_read_bps_device:
1041 case BLKIO_THROTL_write_bps_device:
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001042 case BLKIO_THROTL_read_iops_device:
1043 case BLKIO_THROTL_write_iops_device:
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001044 blkio_read_policy_node_files(cft, blkcg, m);
1045 return 0;
1046 default:
1047 BUG();
1048 }
1049 break;
Vivek Goyal062a6442010-09-15 17:06:33 -04001050 default:
1051 BUG();
1052 }
1053
1054 return 0;
1055}
1056
1057static int blkio_read_blkg_stats(struct blkio_cgroup *blkcg,
1058 struct cftype *cft, struct cgroup_map_cb *cb, enum stat_type type,
1059 bool show_total)
1060{
1061 struct blkio_group *blkg;
1062 struct hlist_node *n;
1063 uint64_t cgroup_total = 0;
1064
1065 rcu_read_lock();
1066 hlist_for_each_entry_rcu(blkg, n, &blkcg->blkg_list, blkcg_node) {
1067 if (blkg->dev) {
1068 if (!cftype_blkg_same_policy(cft, blkg))
1069 continue;
1070 spin_lock_irq(&blkg->stats_lock);
1071 cgroup_total += blkio_get_stat(blkg, cb, blkg->dev,
1072 type);
1073 spin_unlock_irq(&blkg->stats_lock);
1074 }
1075 }
1076 if (show_total)
1077 cb->fill(cb, "Total", cgroup_total);
1078 rcu_read_unlock();
1079 return 0;
1080}
1081
1082/* All map kind of cgroup file get serviced by this function */
1083static int blkiocg_file_read_map(struct cgroup *cgrp, struct cftype *cft,
1084 struct cgroup_map_cb *cb)
1085{
1086 struct blkio_cgroup *blkcg;
1087 enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
1088 int name = BLKIOFILE_ATTR(cft->private);
1089
1090 blkcg = cgroup_to_blkio_cgroup(cgrp);
1091
1092 switch(plid) {
1093 case BLKIO_POLICY_PROP:
1094 switch(name) {
1095 case BLKIO_PROP_time:
1096 return blkio_read_blkg_stats(blkcg, cft, cb,
1097 BLKIO_STAT_TIME, 0);
1098 case BLKIO_PROP_sectors:
1099 return blkio_read_blkg_stats(blkcg, cft, cb,
1100 BLKIO_STAT_SECTORS, 0);
1101 case BLKIO_PROP_io_service_bytes:
1102 return blkio_read_blkg_stats(blkcg, cft, cb,
1103 BLKIO_STAT_SERVICE_BYTES, 1);
1104 case BLKIO_PROP_io_serviced:
1105 return blkio_read_blkg_stats(blkcg, cft, cb,
1106 BLKIO_STAT_SERVICED, 1);
1107 case BLKIO_PROP_io_service_time:
1108 return blkio_read_blkg_stats(blkcg, cft, cb,
1109 BLKIO_STAT_SERVICE_TIME, 1);
1110 case BLKIO_PROP_io_wait_time:
1111 return blkio_read_blkg_stats(blkcg, cft, cb,
1112 BLKIO_STAT_WAIT_TIME, 1);
1113 case BLKIO_PROP_io_merged:
1114 return blkio_read_blkg_stats(blkcg, cft, cb,
1115 BLKIO_STAT_MERGED, 1);
1116 case BLKIO_PROP_io_queued:
1117 return blkio_read_blkg_stats(blkcg, cft, cb,
1118 BLKIO_STAT_QUEUED, 1);
1119#ifdef CONFIG_DEBUG_BLK_CGROUP
1120 case BLKIO_PROP_dequeue:
1121 return blkio_read_blkg_stats(blkcg, cft, cb,
1122 BLKIO_STAT_DEQUEUE, 0);
1123 case BLKIO_PROP_avg_queue_size:
1124 return blkio_read_blkg_stats(blkcg, cft, cb,
1125 BLKIO_STAT_AVG_QUEUE_SIZE, 0);
1126 case BLKIO_PROP_group_wait_time:
1127 return blkio_read_blkg_stats(blkcg, cft, cb,
1128 BLKIO_STAT_GROUP_WAIT_TIME, 0);
1129 case BLKIO_PROP_idle_time:
1130 return blkio_read_blkg_stats(blkcg, cft, cb,
1131 BLKIO_STAT_IDLE_TIME, 0);
1132 case BLKIO_PROP_empty_time:
1133 return blkio_read_blkg_stats(blkcg, cft, cb,
1134 BLKIO_STAT_EMPTY_TIME, 0);
1135#endif
1136 default:
1137 BUG();
1138 }
1139 break;
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001140 case BLKIO_POLICY_THROTL:
1141 switch(name){
1142 case BLKIO_THROTL_io_service_bytes:
1143 return blkio_read_blkg_stats(blkcg, cft, cb,
1144 BLKIO_STAT_SERVICE_BYTES, 1);
1145 case BLKIO_THROTL_io_serviced:
1146 return blkio_read_blkg_stats(blkcg, cft, cb,
1147 BLKIO_STAT_SERVICED, 1);
1148 default:
1149 BUG();
1150 }
1151 break;
Vivek Goyal062a6442010-09-15 17:06:33 -04001152 default:
1153 BUG();
1154 }
1155
1156 return 0;
1157}
1158
1159static int blkio_weight_write(struct blkio_cgroup *blkcg, u64 val)
1160{
1161 struct blkio_group *blkg;
1162 struct hlist_node *n;
1163 struct blkio_policy_node *pn;
1164
1165 if (val < BLKIO_WEIGHT_MIN || val > BLKIO_WEIGHT_MAX)
1166 return -EINVAL;
1167
1168 spin_lock(&blkio_list_lock);
1169 spin_lock_irq(&blkcg->lock);
1170 blkcg->weight = (unsigned int)val;
1171
1172 hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) {
1173 pn = blkio_policy_search_node(blkcg, blkg->dev,
1174 BLKIO_POLICY_PROP, BLKIO_PROP_weight_device);
1175 if (pn)
1176 continue;
1177
1178 blkio_update_group_weight(blkg, blkcg->weight);
1179 }
1180 spin_unlock_irq(&blkcg->lock);
1181 spin_unlock(&blkio_list_lock);
1182 return 0;
1183}
1184
1185static u64 blkiocg_file_read_u64 (struct cgroup *cgrp, struct cftype *cft) {
1186 struct blkio_cgroup *blkcg;
1187 enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
1188 int name = BLKIOFILE_ATTR(cft->private);
1189
1190 blkcg = cgroup_to_blkio_cgroup(cgrp);
1191
1192 switch(plid) {
1193 case BLKIO_POLICY_PROP:
1194 switch(name) {
1195 case BLKIO_PROP_weight:
1196 return (u64)blkcg->weight;
1197 }
1198 break;
1199 default:
1200 BUG();
1201 }
1202 return 0;
1203}
1204
1205static int
1206blkiocg_file_write_u64(struct cgroup *cgrp, struct cftype *cft, u64 val)
1207{
1208 struct blkio_cgroup *blkcg;
1209 enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
1210 int name = BLKIOFILE_ATTR(cft->private);
1211
1212 blkcg = cgroup_to_blkio_cgroup(cgrp);
1213
1214 switch(plid) {
1215 case BLKIO_POLICY_PROP:
1216 switch(name) {
1217 case BLKIO_PROP_weight:
1218 return blkio_weight_write(blkcg, val);
1219 }
1220 break;
1221 default:
1222 BUG();
1223 }
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001224
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001225 return 0;
1226}
1227
Vivek Goyal31e4c282009-12-03 12:59:42 -05001228struct cftype blkio_files[] = {
1229 {
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001230 .name = "weight_device",
Vivek Goyal062a6442010-09-15 17:06:33 -04001231 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1232 BLKIO_PROP_weight_device),
1233 .read_seq_string = blkiocg_file_read,
1234 .write_string = blkiocg_file_write,
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001235 .max_write_len = 256,
1236 },
1237 {
Vivek Goyal31e4c282009-12-03 12:59:42 -05001238 .name = "weight",
Vivek Goyal062a6442010-09-15 17:06:33 -04001239 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1240 BLKIO_PROP_weight),
1241 .read_u64 = blkiocg_file_read_u64,
1242 .write_u64 = blkiocg_file_write_u64,
Vivek Goyal31e4c282009-12-03 12:59:42 -05001243 },
Vivek Goyal22084192009-12-03 12:59:49 -05001244 {
Vivek Goyal13f98252010-10-01 14:49:41 +02001245 .name = "time",
1246 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1247 BLKIO_PROP_time),
1248 .read_map = blkiocg_file_read_map,
1249 },
1250 {
1251 .name = "sectors",
1252 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1253 BLKIO_PROP_sectors),
1254 .read_map = blkiocg_file_read_map,
1255 },
1256 {
1257 .name = "io_service_bytes",
1258 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1259 BLKIO_PROP_io_service_bytes),
1260 .read_map = blkiocg_file_read_map,
1261 },
1262 {
1263 .name = "io_serviced",
1264 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1265 BLKIO_PROP_io_serviced),
1266 .read_map = blkiocg_file_read_map,
1267 },
1268 {
1269 .name = "io_service_time",
1270 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1271 BLKIO_PROP_io_service_time),
1272 .read_map = blkiocg_file_read_map,
1273 },
1274 {
1275 .name = "io_wait_time",
1276 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1277 BLKIO_PROP_io_wait_time),
1278 .read_map = blkiocg_file_read_map,
1279 },
1280 {
1281 .name = "io_merged",
1282 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1283 BLKIO_PROP_io_merged),
1284 .read_map = blkiocg_file_read_map,
1285 },
1286 {
1287 .name = "io_queued",
1288 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1289 BLKIO_PROP_io_queued),
1290 .read_map = blkiocg_file_read_map,
1291 },
1292 {
1293 .name = "reset_stats",
1294 .write_u64 = blkiocg_reset_stats,
1295 },
1296#ifdef CONFIG_BLK_DEV_THROTTLING
1297 {
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001298 .name = "throttle.read_bps_device",
1299 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1300 BLKIO_THROTL_read_bps_device),
1301 .read_seq_string = blkiocg_file_read,
1302 .write_string = blkiocg_file_write,
1303 .max_write_len = 256,
1304 },
1305
1306 {
1307 .name = "throttle.write_bps_device",
1308 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1309 BLKIO_THROTL_write_bps_device),
1310 .read_seq_string = blkiocg_file_read,
1311 .write_string = blkiocg_file_write,
1312 .max_write_len = 256,
1313 },
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001314
1315 {
1316 .name = "throttle.read_iops_device",
1317 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1318 BLKIO_THROTL_read_iops_device),
1319 .read_seq_string = blkiocg_file_read,
1320 .write_string = blkiocg_file_write,
1321 .max_write_len = 256,
1322 },
1323
1324 {
1325 .name = "throttle.write_iops_device",
1326 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1327 BLKIO_THROTL_write_iops_device),
1328 .read_seq_string = blkiocg_file_read,
1329 .write_string = blkiocg_file_write,
1330 .max_write_len = 256,
1331 },
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001332 {
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001333 .name = "throttle.io_service_bytes",
1334 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1335 BLKIO_THROTL_io_service_bytes),
1336 .read_map = blkiocg_file_read_map,
1337 },
1338 {
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001339 .name = "throttle.io_serviced",
1340 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1341 BLKIO_THROTL_io_serviced),
1342 .read_map = blkiocg_file_read_map,
1343 },
Vivek Goyal13f98252010-10-01 14:49:41 +02001344#endif /* CONFIG_BLK_DEV_THROTTLING */
1345
Vivek Goyal22084192009-12-03 12:59:49 -05001346#ifdef CONFIG_DEBUG_BLK_CGROUP
Divyesh Shahcdc11842010-04-08 21:15:10 -07001347 {
1348 .name = "avg_queue_size",
Vivek Goyal062a6442010-09-15 17:06:33 -04001349 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1350 BLKIO_PROP_avg_queue_size),
1351 .read_map = blkiocg_file_read_map,
Divyesh Shahcdc11842010-04-08 21:15:10 -07001352 },
1353 {
Divyesh Shah812df482010-04-08 21:15:35 -07001354 .name = "group_wait_time",
Vivek Goyal062a6442010-09-15 17:06:33 -04001355 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1356 BLKIO_PROP_group_wait_time),
1357 .read_map = blkiocg_file_read_map,
Divyesh Shah812df482010-04-08 21:15:35 -07001358 },
1359 {
1360 .name = "idle_time",
Vivek Goyal062a6442010-09-15 17:06:33 -04001361 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1362 BLKIO_PROP_idle_time),
1363 .read_map = blkiocg_file_read_map,
Divyesh Shah812df482010-04-08 21:15:35 -07001364 },
1365 {
1366 .name = "empty_time",
Vivek Goyal062a6442010-09-15 17:06:33 -04001367 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1368 BLKIO_PROP_empty_time),
1369 .read_map = blkiocg_file_read_map,
Divyesh Shah812df482010-04-08 21:15:35 -07001370 },
1371 {
Vivek Goyal22084192009-12-03 12:59:49 -05001372 .name = "dequeue",
Vivek Goyal062a6442010-09-15 17:06:33 -04001373 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1374 BLKIO_PROP_dequeue),
1375 .read_map = blkiocg_file_read_map,
Divyesh Shahcdc11842010-04-08 21:15:10 -07001376 },
Vivek Goyal22084192009-12-03 12:59:49 -05001377#endif
Vivek Goyal31e4c282009-12-03 12:59:42 -05001378};
1379
1380static int blkiocg_populate(struct cgroup_subsys *subsys, struct cgroup *cgroup)
1381{
1382 return cgroup_add_files(cgroup, subsys, blkio_files,
1383 ARRAY_SIZE(blkio_files));
1384}
1385
1386static void blkiocg_destroy(struct cgroup_subsys *subsys, struct cgroup *cgroup)
1387{
1388 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgroup);
Vivek Goyalb1c35762009-12-03 12:59:47 -05001389 unsigned long flags;
1390 struct blkio_group *blkg;
1391 void *key;
Vivek Goyal3e252062009-12-04 10:36:42 -05001392 struct blkio_policy_type *blkiop;
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001393 struct blkio_policy_node *pn, *pntmp;
Vivek Goyal31e4c282009-12-03 12:59:42 -05001394
Vivek Goyalb1c35762009-12-03 12:59:47 -05001395 rcu_read_lock();
Jens Axboe0f3942a2010-05-03 14:28:55 +02001396 do {
1397 spin_lock_irqsave(&blkcg->lock, flags);
Vivek Goyalb1c35762009-12-03 12:59:47 -05001398
Jens Axboe0f3942a2010-05-03 14:28:55 +02001399 if (hlist_empty(&blkcg->blkg_list)) {
1400 spin_unlock_irqrestore(&blkcg->lock, flags);
1401 break;
1402 }
1403
1404 blkg = hlist_entry(blkcg->blkg_list.first, struct blkio_group,
1405 blkcg_node);
1406 key = rcu_dereference(blkg->key);
1407 __blkiocg_del_blkio_group(blkg);
1408
Vivek Goyalb1c35762009-12-03 12:59:47 -05001409 spin_unlock_irqrestore(&blkcg->lock, flags);
Vivek Goyalb1c35762009-12-03 12:59:47 -05001410
Jens Axboe0f3942a2010-05-03 14:28:55 +02001411 /*
1412 * This blkio_group is being unlinked as associated cgroup is
1413 * going away. Let all the IO controlling policies know about
Vivek Goyal61014e92010-10-01 14:49:44 +02001414 * this event.
Jens Axboe0f3942a2010-05-03 14:28:55 +02001415 */
1416 spin_lock(&blkio_list_lock);
Vivek Goyal61014e92010-10-01 14:49:44 +02001417 list_for_each_entry(blkiop, &blkio_list, list) {
1418 if (blkiop->plid != blkg->plid)
1419 continue;
Jens Axboe0f3942a2010-05-03 14:28:55 +02001420 blkiop->ops.blkio_unlink_group_fn(key, blkg);
Vivek Goyal61014e92010-10-01 14:49:44 +02001421 }
Jens Axboe0f3942a2010-05-03 14:28:55 +02001422 spin_unlock(&blkio_list_lock);
1423 } while (1);
Vivek Goyalb1c35762009-12-03 12:59:47 -05001424
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001425 list_for_each_entry_safe(pn, pntmp, &blkcg->policy_list, node) {
1426 blkio_policy_delete_node(pn);
1427 kfree(pn);
1428 }
Jens Axboe0f3942a2010-05-03 14:28:55 +02001429
Vivek Goyal31e4c282009-12-03 12:59:42 -05001430 free_css_id(&blkio_subsys, &blkcg->css);
Vivek Goyalb1c35762009-12-03 12:59:47 -05001431 rcu_read_unlock();
Ben Blum67523c42010-03-10 15:22:11 -08001432 if (blkcg != &blkio_root_cgroup)
1433 kfree(blkcg);
Vivek Goyal31e4c282009-12-03 12:59:42 -05001434}
1435
1436static struct cgroup_subsys_state *
1437blkiocg_create(struct cgroup_subsys *subsys, struct cgroup *cgroup)
1438{
Li Zefan03415092010-05-07 08:57:00 +02001439 struct blkio_cgroup *blkcg;
1440 struct cgroup *parent = cgroup->parent;
Vivek Goyal31e4c282009-12-03 12:59:42 -05001441
Li Zefan03415092010-05-07 08:57:00 +02001442 if (!parent) {
Vivek Goyal31e4c282009-12-03 12:59:42 -05001443 blkcg = &blkio_root_cgroup;
1444 goto done;
1445 }
1446
1447 /* Currently we do not support hierarchy deeper than two level (0,1) */
Li Zefan03415092010-05-07 08:57:00 +02001448 if (parent != cgroup->top_cgroup)
Vivek Goyal31e4c282009-12-03 12:59:42 -05001449 return ERR_PTR(-EINVAL);
1450
1451 blkcg = kzalloc(sizeof(*blkcg), GFP_KERNEL);
1452 if (!blkcg)
1453 return ERR_PTR(-ENOMEM);
1454
1455 blkcg->weight = BLKIO_WEIGHT_DEFAULT;
1456done:
1457 spin_lock_init(&blkcg->lock);
1458 INIT_HLIST_HEAD(&blkcg->blkg_list);
1459
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001460 INIT_LIST_HEAD(&blkcg->policy_list);
Vivek Goyal31e4c282009-12-03 12:59:42 -05001461 return &blkcg->css;
1462}
1463
1464/*
1465 * We cannot support shared io contexts, as we have no mean to support
1466 * two tasks with the same ioc in two different groups without major rework
1467 * of the main cic data structures. For now we allow a task to change
1468 * its cgroup only if it's the only owner of its ioc.
1469 */
1470static int blkiocg_can_attach(struct cgroup_subsys *subsys,
1471 struct cgroup *cgroup, struct task_struct *tsk,
1472 bool threadgroup)
1473{
1474 struct io_context *ioc;
1475 int ret = 0;
1476
1477 /* task_lock() is needed to avoid races with exit_io_context() */
1478 task_lock(tsk);
1479 ioc = tsk->io_context;
1480 if (ioc && atomic_read(&ioc->nr_tasks) > 1)
1481 ret = -EINVAL;
1482 task_unlock(tsk);
1483
1484 return ret;
1485}
1486
1487static void blkiocg_attach(struct cgroup_subsys *subsys, struct cgroup *cgroup,
1488 struct cgroup *prev, struct task_struct *tsk,
1489 bool threadgroup)
1490{
1491 struct io_context *ioc;
1492
1493 task_lock(tsk);
1494 ioc = tsk->io_context;
1495 if (ioc)
1496 ioc->cgroup_changed = 1;
1497 task_unlock(tsk);
1498}
1499
Vivek Goyal3e252062009-12-04 10:36:42 -05001500void blkio_policy_register(struct blkio_policy_type *blkiop)
1501{
1502 spin_lock(&blkio_list_lock);
1503 list_add_tail(&blkiop->list, &blkio_list);
1504 spin_unlock(&blkio_list_lock);
1505}
1506EXPORT_SYMBOL_GPL(blkio_policy_register);
1507
1508void blkio_policy_unregister(struct blkio_policy_type *blkiop)
1509{
1510 spin_lock(&blkio_list_lock);
1511 list_del_init(&blkiop->list);
1512 spin_unlock(&blkio_list_lock);
1513}
1514EXPORT_SYMBOL_GPL(blkio_policy_unregister);
Ben Blum67523c42010-03-10 15:22:11 -08001515
1516static int __init init_cgroup_blkio(void)
1517{
1518 return cgroup_load_subsys(&blkio_subsys);
1519}
1520
1521static void __exit exit_cgroup_blkio(void)
1522{
1523 cgroup_unload_subsys(&blkio_subsys);
1524}
1525
1526module_init(init_cgroup_blkio);
1527module_exit(exit_cgroup_blkio);
1528MODULE_LICENSE("GPL");