blob: 7762987fdf9ec403267acceeaf22ca270e48cd41 [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
Divyesh Shah91952912010-04-01 15:01:41 -0700131/*
132 * Add to the appropriate stat variable depending on the request type.
133 * This should be called with the blkg->stats_lock held.
134 */
Divyesh Shah84c124d2010-04-09 08:31:19 +0200135static void blkio_add_stat(uint64_t *stat, uint64_t add, bool direction,
136 bool sync)
Divyesh Shah91952912010-04-01 15:01:41 -0700137{
Divyesh Shah84c124d2010-04-09 08:31:19 +0200138 if (direction)
139 stat[BLKIO_STAT_WRITE] += add;
Divyesh Shah91952912010-04-01 15:01:41 -0700140 else
Divyesh Shah84c124d2010-04-09 08:31:19 +0200141 stat[BLKIO_STAT_READ] += add;
142 if (sync)
143 stat[BLKIO_STAT_SYNC] += add;
Divyesh Shah91952912010-04-01 15:01:41 -0700144 else
Divyesh Shah84c124d2010-04-09 08:31:19 +0200145 stat[BLKIO_STAT_ASYNC] += add;
Divyesh Shah91952912010-04-01 15:01:41 -0700146}
147
Divyesh Shahcdc11842010-04-08 21:15:10 -0700148/*
149 * Decrements the appropriate stat variable if non-zero depending on the
150 * request type. Panics on value being zero.
151 * This should be called with the blkg->stats_lock held.
152 */
153static void blkio_check_and_dec_stat(uint64_t *stat, bool direction, bool sync)
154{
155 if (direction) {
156 BUG_ON(stat[BLKIO_STAT_WRITE] == 0);
157 stat[BLKIO_STAT_WRITE]--;
158 } else {
159 BUG_ON(stat[BLKIO_STAT_READ] == 0);
160 stat[BLKIO_STAT_READ]--;
161 }
162 if (sync) {
163 BUG_ON(stat[BLKIO_STAT_SYNC] == 0);
164 stat[BLKIO_STAT_SYNC]--;
165 } else {
166 BUG_ON(stat[BLKIO_STAT_ASYNC] == 0);
167 stat[BLKIO_STAT_ASYNC]--;
168 }
169}
170
171#ifdef CONFIG_DEBUG_BLK_CGROUP
Divyesh Shah812df482010-04-08 21:15:35 -0700172/* This should be called with the blkg->stats_lock held. */
173static void blkio_set_start_group_wait_time(struct blkio_group *blkg,
174 struct blkio_group *curr_blkg)
175{
176 if (blkio_blkg_waiting(&blkg->stats))
177 return;
178 if (blkg == curr_blkg)
179 return;
180 blkg->stats.start_group_wait_time = sched_clock();
181 blkio_mark_blkg_waiting(&blkg->stats);
182}
183
184/* This should be called with the blkg->stats_lock held. */
185static void blkio_update_group_wait_time(struct blkio_group_stats *stats)
186{
187 unsigned long long now;
188
189 if (!blkio_blkg_waiting(stats))
190 return;
191
192 now = sched_clock();
193 if (time_after64(now, stats->start_group_wait_time))
194 stats->group_wait_time += now - stats->start_group_wait_time;
195 blkio_clear_blkg_waiting(stats);
196}
197
198/* This should be called with the blkg->stats_lock held. */
199static void blkio_end_empty_time(struct blkio_group_stats *stats)
200{
201 unsigned long long now;
202
203 if (!blkio_blkg_empty(stats))
204 return;
205
206 now = sched_clock();
207 if (time_after64(now, stats->start_empty_time))
208 stats->empty_time += now - stats->start_empty_time;
209 blkio_clear_blkg_empty(stats);
210}
211
212void blkiocg_update_set_idle_time_stats(struct blkio_group *blkg)
213{
214 unsigned long flags;
215
216 spin_lock_irqsave(&blkg->stats_lock, flags);
217 BUG_ON(blkio_blkg_idling(&blkg->stats));
218 blkg->stats.start_idle_time = sched_clock();
219 blkio_mark_blkg_idling(&blkg->stats);
220 spin_unlock_irqrestore(&blkg->stats_lock, flags);
221}
222EXPORT_SYMBOL_GPL(blkiocg_update_set_idle_time_stats);
223
224void blkiocg_update_idle_time_stats(struct blkio_group *blkg)
225{
226 unsigned long flags;
227 unsigned long long now;
228 struct blkio_group_stats *stats;
229
230 spin_lock_irqsave(&blkg->stats_lock, flags);
231 stats = &blkg->stats;
232 if (blkio_blkg_idling(stats)) {
233 now = sched_clock();
234 if (time_after64(now, stats->start_idle_time))
235 stats->idle_time += now - stats->start_idle_time;
236 blkio_clear_blkg_idling(stats);
237 }
238 spin_unlock_irqrestore(&blkg->stats_lock, flags);
239}
240EXPORT_SYMBOL_GPL(blkiocg_update_idle_time_stats);
241
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200242void blkiocg_update_avg_queue_size_stats(struct blkio_group *blkg)
Divyesh Shahcdc11842010-04-08 21:15:10 -0700243{
244 unsigned long flags;
245 struct blkio_group_stats *stats;
246
247 spin_lock_irqsave(&blkg->stats_lock, flags);
248 stats = &blkg->stats;
249 stats->avg_queue_size_sum +=
250 stats->stat_arr[BLKIO_STAT_QUEUED][BLKIO_STAT_READ] +
251 stats->stat_arr[BLKIO_STAT_QUEUED][BLKIO_STAT_WRITE];
252 stats->avg_queue_size_samples++;
Divyesh Shah812df482010-04-08 21:15:35 -0700253 blkio_update_group_wait_time(stats);
Divyesh Shahcdc11842010-04-08 21:15:10 -0700254 spin_unlock_irqrestore(&blkg->stats_lock, flags);
255}
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200256EXPORT_SYMBOL_GPL(blkiocg_update_avg_queue_size_stats);
257
Vivek Goyale5ff0822010-04-26 19:25:11 +0200258void blkiocg_set_start_empty_time(struct blkio_group *blkg)
Divyesh Shah28baf442010-04-14 11:22:38 +0200259{
260 unsigned long flags;
261 struct blkio_group_stats *stats;
262
263 spin_lock_irqsave(&blkg->stats_lock, flags);
264 stats = &blkg->stats;
265
266 if (stats->stat_arr[BLKIO_STAT_QUEUED][BLKIO_STAT_READ] ||
267 stats->stat_arr[BLKIO_STAT_QUEUED][BLKIO_STAT_WRITE]) {
268 spin_unlock_irqrestore(&blkg->stats_lock, flags);
269 return;
270 }
271
272 /*
Vivek Goyale5ff0822010-04-26 19:25:11 +0200273 * group is already marked empty. This can happen if cfqq got new
274 * request in parent group and moved to this group while being added
275 * to service tree. Just ignore the event and move on.
Divyesh Shah28baf442010-04-14 11:22:38 +0200276 */
Vivek Goyale5ff0822010-04-26 19:25:11 +0200277 if(blkio_blkg_empty(stats)) {
278 spin_unlock_irqrestore(&blkg->stats_lock, flags);
279 return;
280 }
281
Divyesh Shah28baf442010-04-14 11:22:38 +0200282 stats->start_empty_time = sched_clock();
283 blkio_mark_blkg_empty(stats);
284 spin_unlock_irqrestore(&blkg->stats_lock, flags);
285}
286EXPORT_SYMBOL_GPL(blkiocg_set_start_empty_time);
287
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200288void blkiocg_update_dequeue_stats(struct blkio_group *blkg,
289 unsigned long dequeue)
290{
291 blkg->stats.dequeue += dequeue;
292}
293EXPORT_SYMBOL_GPL(blkiocg_update_dequeue_stats);
Divyesh Shah812df482010-04-08 21:15:35 -0700294#else
295static inline void blkio_set_start_group_wait_time(struct blkio_group *blkg,
296 struct blkio_group *curr_blkg) {}
297static inline void blkio_end_empty_time(struct blkio_group_stats *stats) {}
Divyesh Shahcdc11842010-04-08 21:15:10 -0700298#endif
299
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200300void blkiocg_update_io_add_stats(struct blkio_group *blkg,
Divyesh Shahcdc11842010-04-08 21:15:10 -0700301 struct blkio_group *curr_blkg, bool direction,
302 bool sync)
303{
304 unsigned long flags;
305
306 spin_lock_irqsave(&blkg->stats_lock, flags);
307 blkio_add_stat(blkg->stats.stat_arr[BLKIO_STAT_QUEUED], 1, direction,
308 sync);
Divyesh Shah812df482010-04-08 21:15:35 -0700309 blkio_end_empty_time(&blkg->stats);
310 blkio_set_start_group_wait_time(blkg, curr_blkg);
Divyesh Shahcdc11842010-04-08 21:15:10 -0700311 spin_unlock_irqrestore(&blkg->stats_lock, flags);
312}
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200313EXPORT_SYMBOL_GPL(blkiocg_update_io_add_stats);
Divyesh Shahcdc11842010-04-08 21:15:10 -0700314
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200315void blkiocg_update_io_remove_stats(struct blkio_group *blkg,
Divyesh Shahcdc11842010-04-08 21:15:10 -0700316 bool direction, bool sync)
317{
318 unsigned long flags;
319
320 spin_lock_irqsave(&blkg->stats_lock, flags);
321 blkio_check_and_dec_stat(blkg->stats.stat_arr[BLKIO_STAT_QUEUED],
322 direction, sync);
323 spin_unlock_irqrestore(&blkg->stats_lock, flags);
324}
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200325EXPORT_SYMBOL_GPL(blkiocg_update_io_remove_stats);
Divyesh Shahcdc11842010-04-08 21:15:10 -0700326
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700327void blkiocg_update_timeslice_used(struct blkio_group *blkg, unsigned long time)
Vivek Goyal22084192009-12-03 12:59:49 -0500328{
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700329 unsigned long flags;
330
331 spin_lock_irqsave(&blkg->stats_lock, flags);
332 blkg->stats.time += time;
333 spin_unlock_irqrestore(&blkg->stats_lock, flags);
Vivek Goyal22084192009-12-03 12:59:49 -0500334}
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700335EXPORT_SYMBOL_GPL(blkiocg_update_timeslice_used);
Vivek Goyal22084192009-12-03 12:59:49 -0500336
Divyesh Shah84c124d2010-04-09 08:31:19 +0200337void blkiocg_update_dispatch_stats(struct blkio_group *blkg,
338 uint64_t bytes, bool direction, bool sync)
Divyesh Shah91952912010-04-01 15:01:41 -0700339{
340 struct blkio_group_stats *stats;
341 unsigned long flags;
342
343 spin_lock_irqsave(&blkg->stats_lock, flags);
344 stats = &blkg->stats;
Divyesh Shah84c124d2010-04-09 08:31:19 +0200345 stats->sectors += bytes >> 9;
346 blkio_add_stat(stats->stat_arr[BLKIO_STAT_SERVICED], 1, direction,
347 sync);
348 blkio_add_stat(stats->stat_arr[BLKIO_STAT_SERVICE_BYTES], bytes,
349 direction, sync);
Divyesh Shah91952912010-04-01 15:01:41 -0700350 spin_unlock_irqrestore(&blkg->stats_lock, flags);
351}
Divyesh Shah84c124d2010-04-09 08:31:19 +0200352EXPORT_SYMBOL_GPL(blkiocg_update_dispatch_stats);
Divyesh Shah91952912010-04-01 15:01:41 -0700353
Divyesh Shah84c124d2010-04-09 08:31:19 +0200354void blkiocg_update_completion_stats(struct blkio_group *blkg,
355 uint64_t start_time, uint64_t io_start_time, bool direction, bool sync)
Divyesh Shah91952912010-04-01 15:01:41 -0700356{
357 struct blkio_group_stats *stats;
358 unsigned long flags;
359 unsigned long long now = sched_clock();
360
361 spin_lock_irqsave(&blkg->stats_lock, flags);
362 stats = &blkg->stats;
Divyesh Shah84c124d2010-04-09 08:31:19 +0200363 if (time_after64(now, io_start_time))
364 blkio_add_stat(stats->stat_arr[BLKIO_STAT_SERVICE_TIME],
365 now - io_start_time, direction, sync);
366 if (time_after64(io_start_time, start_time))
367 blkio_add_stat(stats->stat_arr[BLKIO_STAT_WAIT_TIME],
368 io_start_time - start_time, direction, sync);
Divyesh Shah91952912010-04-01 15:01:41 -0700369 spin_unlock_irqrestore(&blkg->stats_lock, flags);
370}
Divyesh Shah84c124d2010-04-09 08:31:19 +0200371EXPORT_SYMBOL_GPL(blkiocg_update_completion_stats);
Divyesh Shah91952912010-04-01 15:01:41 -0700372
Divyesh Shah812d4022010-04-08 21:14:23 -0700373void blkiocg_update_io_merged_stats(struct blkio_group *blkg, bool direction,
374 bool sync)
375{
376 unsigned long flags;
377
378 spin_lock_irqsave(&blkg->stats_lock, flags);
379 blkio_add_stat(blkg->stats.stat_arr[BLKIO_STAT_MERGED], 1, direction,
380 sync);
381 spin_unlock_irqrestore(&blkg->stats_lock, flags);
382}
383EXPORT_SYMBOL_GPL(blkiocg_update_io_merged_stats);
384
Vivek Goyal31e4c282009-12-03 12:59:42 -0500385void blkiocg_add_blkio_group(struct blkio_cgroup *blkcg,
Vivek Goyal062a6442010-09-15 17:06:33 -0400386 struct blkio_group *blkg, void *key, dev_t dev,
387 enum blkio_policy_id plid)
Vivek Goyal31e4c282009-12-03 12:59:42 -0500388{
389 unsigned long flags;
390
391 spin_lock_irqsave(&blkcg->lock, flags);
Divyesh Shah8d2a91f2010-04-16 08:10:51 +0200392 spin_lock_init(&blkg->stats_lock);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500393 rcu_assign_pointer(blkg->key, key);
Vivek Goyalb1c35762009-12-03 12:59:47 -0500394 blkg->blkcg_id = css_id(&blkcg->css);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500395 hlist_add_head_rcu(&blkg->blkcg_node, &blkcg->blkg_list);
Vivek Goyal062a6442010-09-15 17:06:33 -0400396 blkg->plid = plid;
Vivek Goyal31e4c282009-12-03 12:59:42 -0500397 spin_unlock_irqrestore(&blkcg->lock, flags);
Vivek Goyal2868ef72009-12-03 12:59:48 -0500398 /* Need to take css reference ? */
399 cgroup_path(blkcg->css.cgroup, blkg->path, sizeof(blkg->path));
Vivek Goyal22084192009-12-03 12:59:49 -0500400 blkg->dev = dev;
Vivek Goyal31e4c282009-12-03 12:59:42 -0500401}
Vivek Goyal9d6a9862009-12-04 10:36:41 -0500402EXPORT_SYMBOL_GPL(blkiocg_add_blkio_group);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500403
Vivek Goyalb1c35762009-12-03 12:59:47 -0500404static void __blkiocg_del_blkio_group(struct blkio_group *blkg)
405{
406 hlist_del_init_rcu(&blkg->blkcg_node);
407 blkg->blkcg_id = 0;
408}
409
410/*
411 * returns 0 if blkio_group was still on cgroup list. Otherwise returns 1
412 * indicating that blk_group was unhashed by the time we got to it.
413 */
Vivek Goyal31e4c282009-12-03 12:59:42 -0500414int blkiocg_del_blkio_group(struct blkio_group *blkg)
415{
Vivek Goyalb1c35762009-12-03 12:59:47 -0500416 struct blkio_cgroup *blkcg;
417 unsigned long flags;
418 struct cgroup_subsys_state *css;
419 int ret = 1;
420
421 rcu_read_lock();
422 css = css_lookup(&blkio_subsys, blkg->blkcg_id);
Jens Axboe0f3942a2010-05-03 14:28:55 +0200423 if (css) {
424 blkcg = container_of(css, struct blkio_cgroup, css);
425 spin_lock_irqsave(&blkcg->lock, flags);
426 if (!hlist_unhashed(&blkg->blkcg_node)) {
427 __blkiocg_del_blkio_group(blkg);
428 ret = 0;
429 }
430 spin_unlock_irqrestore(&blkcg->lock, flags);
Vivek Goyalb1c35762009-12-03 12:59:47 -0500431 }
Jens Axboe0f3942a2010-05-03 14:28:55 +0200432
Vivek Goyalb1c35762009-12-03 12:59:47 -0500433 rcu_read_unlock();
434 return ret;
Vivek Goyal31e4c282009-12-03 12:59:42 -0500435}
Vivek Goyal9d6a9862009-12-04 10:36:41 -0500436EXPORT_SYMBOL_GPL(blkiocg_del_blkio_group);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500437
438/* called under rcu_read_lock(). */
439struct blkio_group *blkiocg_lookup_group(struct blkio_cgroup *blkcg, void *key)
440{
441 struct blkio_group *blkg;
442 struct hlist_node *n;
443 void *__key;
444
445 hlist_for_each_entry_rcu(blkg, n, &blkcg->blkg_list, blkcg_node) {
446 __key = blkg->key;
447 if (__key == key)
448 return blkg;
449 }
450
451 return NULL;
452}
Vivek Goyal9d6a9862009-12-04 10:36:41 -0500453EXPORT_SYMBOL_GPL(blkiocg_lookup_group);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500454
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700455static int
Divyesh Shah84c124d2010-04-09 08:31:19 +0200456blkiocg_reset_stats(struct cgroup *cgroup, struct cftype *cftype, u64 val)
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700457{
458 struct blkio_cgroup *blkcg;
459 struct blkio_group *blkg;
Divyesh Shah812df482010-04-08 21:15:35 -0700460 struct blkio_group_stats *stats;
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700461 struct hlist_node *n;
Divyesh Shahcdc11842010-04-08 21:15:10 -0700462 uint64_t queued[BLKIO_STAT_TOTAL];
463 int i;
Divyesh Shah812df482010-04-08 21:15:35 -0700464#ifdef CONFIG_DEBUG_BLK_CGROUP
465 bool idling, waiting, empty;
466 unsigned long long now = sched_clock();
467#endif
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700468
469 blkcg = cgroup_to_blkio_cgroup(cgroup);
470 spin_lock_irq(&blkcg->lock);
471 hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) {
472 spin_lock(&blkg->stats_lock);
Divyesh Shah812df482010-04-08 21:15:35 -0700473 stats = &blkg->stats;
474#ifdef CONFIG_DEBUG_BLK_CGROUP
475 idling = blkio_blkg_idling(stats);
476 waiting = blkio_blkg_waiting(stats);
477 empty = blkio_blkg_empty(stats);
478#endif
Divyesh Shahcdc11842010-04-08 21:15:10 -0700479 for (i = 0; i < BLKIO_STAT_TOTAL; i++)
Divyesh Shah812df482010-04-08 21:15:35 -0700480 queued[i] = stats->stat_arr[BLKIO_STAT_QUEUED][i];
481 memset(stats, 0, sizeof(struct blkio_group_stats));
Divyesh Shahcdc11842010-04-08 21:15:10 -0700482 for (i = 0; i < BLKIO_STAT_TOTAL; i++)
Divyesh Shah812df482010-04-08 21:15:35 -0700483 stats->stat_arr[BLKIO_STAT_QUEUED][i] = queued[i];
484#ifdef CONFIG_DEBUG_BLK_CGROUP
485 if (idling) {
486 blkio_mark_blkg_idling(stats);
487 stats->start_idle_time = now;
488 }
489 if (waiting) {
490 blkio_mark_blkg_waiting(stats);
491 stats->start_group_wait_time = now;
492 }
493 if (empty) {
494 blkio_mark_blkg_empty(stats);
495 stats->start_empty_time = now;
496 }
497#endif
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700498 spin_unlock(&blkg->stats_lock);
499 }
500 spin_unlock_irq(&blkcg->lock);
501 return 0;
502}
503
Divyesh Shah84c124d2010-04-09 08:31:19 +0200504static void blkio_get_key_name(enum stat_sub_type type, dev_t dev, char *str,
505 int chars_left, bool diskname_only)
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700506{
Divyesh Shah84c124d2010-04-09 08:31:19 +0200507 snprintf(str, chars_left, "%d:%d", MAJOR(dev), MINOR(dev));
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700508 chars_left -= strlen(str);
509 if (chars_left <= 0) {
510 printk(KERN_WARNING
511 "Possibly incorrect cgroup stat display format");
512 return;
513 }
Divyesh Shah84c124d2010-04-09 08:31:19 +0200514 if (diskname_only)
515 return;
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700516 switch (type) {
Divyesh Shah84c124d2010-04-09 08:31:19 +0200517 case BLKIO_STAT_READ:
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700518 strlcat(str, " Read", chars_left);
519 break;
Divyesh Shah84c124d2010-04-09 08:31:19 +0200520 case BLKIO_STAT_WRITE:
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700521 strlcat(str, " Write", chars_left);
522 break;
Divyesh Shah84c124d2010-04-09 08:31:19 +0200523 case BLKIO_STAT_SYNC:
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700524 strlcat(str, " Sync", chars_left);
525 break;
Divyesh Shah84c124d2010-04-09 08:31:19 +0200526 case BLKIO_STAT_ASYNC:
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700527 strlcat(str, " Async", chars_left);
528 break;
Divyesh Shah84c124d2010-04-09 08:31:19 +0200529 case BLKIO_STAT_TOTAL:
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700530 strlcat(str, " Total", chars_left);
531 break;
532 default:
533 strlcat(str, " Invalid", chars_left);
534 }
535}
536
Divyesh Shah84c124d2010-04-09 08:31:19 +0200537static uint64_t blkio_fill_stat(char *str, int chars_left, uint64_t val,
538 struct cgroup_map_cb *cb, dev_t dev)
539{
540 blkio_get_key_name(0, dev, str, chars_left, true);
541 cb->fill(cb, str, val);
542 return val;
543}
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700544
Divyesh Shah84c124d2010-04-09 08:31:19 +0200545/* This should be called with blkg->stats_lock held */
546static uint64_t blkio_get_stat(struct blkio_group *blkg,
547 struct cgroup_map_cb *cb, dev_t dev, enum stat_type type)
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700548{
549 uint64_t disk_total;
550 char key_str[MAX_KEY_LEN];
Divyesh Shah84c124d2010-04-09 08:31:19 +0200551 enum stat_sub_type sub_type;
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700552
Divyesh Shah84c124d2010-04-09 08:31:19 +0200553 if (type == BLKIO_STAT_TIME)
554 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
555 blkg->stats.time, cb, dev);
556 if (type == BLKIO_STAT_SECTORS)
557 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
558 blkg->stats.sectors, cb, dev);
559#ifdef CONFIG_DEBUG_BLK_CGROUP
Divyesh Shahcdc11842010-04-08 21:15:10 -0700560 if (type == BLKIO_STAT_AVG_QUEUE_SIZE) {
561 uint64_t sum = blkg->stats.avg_queue_size_sum;
562 uint64_t samples = blkg->stats.avg_queue_size_samples;
563 if (samples)
564 do_div(sum, samples);
565 else
566 sum = 0;
567 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1, sum, cb, dev);
568 }
Divyesh Shah812df482010-04-08 21:15:35 -0700569 if (type == BLKIO_STAT_GROUP_WAIT_TIME)
570 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
571 blkg->stats.group_wait_time, cb, dev);
572 if (type == BLKIO_STAT_IDLE_TIME)
573 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
574 blkg->stats.idle_time, cb, dev);
575 if (type == BLKIO_STAT_EMPTY_TIME)
576 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
577 blkg->stats.empty_time, cb, dev);
Divyesh Shah84c124d2010-04-09 08:31:19 +0200578 if (type == BLKIO_STAT_DEQUEUE)
579 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
580 blkg->stats.dequeue, cb, dev);
581#endif
582
583 for (sub_type = BLKIO_STAT_READ; sub_type < BLKIO_STAT_TOTAL;
584 sub_type++) {
585 blkio_get_key_name(sub_type, dev, key_str, MAX_KEY_LEN, false);
586 cb->fill(cb, key_str, blkg->stats.stat_arr[type][sub_type]);
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700587 }
Divyesh Shah84c124d2010-04-09 08:31:19 +0200588 disk_total = blkg->stats.stat_arr[type][BLKIO_STAT_READ] +
589 blkg->stats.stat_arr[type][BLKIO_STAT_WRITE];
590 blkio_get_key_name(BLKIO_STAT_TOTAL, dev, key_str, MAX_KEY_LEN, false);
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700591 cb->fill(cb, key_str, disk_total);
592 return disk_total;
593}
594
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800595static int blkio_check_dev_num(dev_t dev)
596{
597 int part = 0;
598 struct gendisk *disk;
599
600 disk = get_gendisk(dev, &part);
601 if (!disk || part)
602 return -ENODEV;
603
604 return 0;
605}
606
607static int blkio_policy_parse_and_set(char *buf,
Vivek Goyal062a6442010-09-15 17:06:33 -0400608 struct blkio_policy_node *newpn, enum blkio_policy_id plid, int fileid)
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800609{
610 char *s[4], *p, *major_s = NULL, *minor_s = NULL;
611 int ret;
612 unsigned long major, minor, temp;
613 int i = 0;
614 dev_t dev;
615
616 memset(s, 0, sizeof(s));
617
618 while ((p = strsep(&buf, " ")) != NULL) {
619 if (!*p)
620 continue;
621
622 s[i++] = p;
623
624 /* Prevent from inputing too many things */
625 if (i == 3)
626 break;
627 }
628
629 if (i != 2)
630 return -EINVAL;
631
632 p = strsep(&s[0], ":");
633 if (p != NULL)
634 major_s = p;
635 else
636 return -EINVAL;
637
638 minor_s = s[0];
639 if (!minor_s)
640 return -EINVAL;
641
642 ret = strict_strtoul(major_s, 10, &major);
643 if (ret)
644 return -EINVAL;
645
646 ret = strict_strtoul(minor_s, 10, &minor);
647 if (ret)
648 return -EINVAL;
649
650 dev = MKDEV(major, minor);
651
652 ret = blkio_check_dev_num(dev);
653 if (ret)
654 return ret;
655
656 newpn->dev = dev;
657
658 if (s[1] == NULL)
659 return -EINVAL;
660
Vivek Goyal062a6442010-09-15 17:06:33 -0400661 switch (plid) {
662 case BLKIO_POLICY_PROP:
663 ret = strict_strtoul(s[1], 10, &temp);
664 if (ret || (temp < BLKIO_WEIGHT_MIN && temp > 0) ||
665 temp > BLKIO_WEIGHT_MAX)
666 return -EINVAL;
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800667
Vivek Goyal062a6442010-09-15 17:06:33 -0400668 newpn->plid = plid;
669 newpn->fileid = fileid;
670 newpn->weight = temp;
671 break;
672 default:
673 BUG();
674 }
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800675
676 return 0;
677}
678
679unsigned int blkcg_get_weight(struct blkio_cgroup *blkcg,
680 dev_t dev)
681{
682 struct blkio_policy_node *pn;
683
Vivek Goyal062a6442010-09-15 17:06:33 -0400684 pn = blkio_policy_search_node(blkcg, dev, BLKIO_POLICY_PROP,
685 BLKIO_PROP_weight_device);
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800686 if (pn)
687 return pn->weight;
688 else
689 return blkcg->weight;
690}
691EXPORT_SYMBOL_GPL(blkcg_get_weight);
692
Vivek Goyal062a6442010-09-15 17:06:33 -0400693/* Checks whether user asked for deleting a policy rule */
694static bool blkio_delete_rule_command(struct blkio_policy_node *pn)
695{
696 switch(pn->plid) {
697 case BLKIO_POLICY_PROP:
698 if (pn->weight == 0)
699 return 1;
700 break;
701 default:
702 BUG();
703 }
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800704
Vivek Goyal062a6442010-09-15 17:06:33 -0400705 return 0;
706}
707
708static void blkio_update_policy_rule(struct blkio_policy_node *oldpn,
709 struct blkio_policy_node *newpn)
710{
711 switch(oldpn->plid) {
712 case BLKIO_POLICY_PROP:
713 oldpn->weight = newpn->weight;
714 break;
715 default:
716 BUG();
717 }
718}
719
720/*
721 * Some rules/values in blkg have changed. Propogate those to respective
722 * policies.
723 */
724static void blkio_update_blkg_policy(struct blkio_cgroup *blkcg,
725 struct blkio_group *blkg, struct blkio_policy_node *pn)
726{
727 unsigned int weight;
728
729 switch(pn->plid) {
730 case BLKIO_POLICY_PROP:
731 weight = pn->weight ? pn->weight :
732 blkcg->weight;
733 blkio_update_group_weight(blkg, weight);
734 break;
735 default:
736 BUG();
737 }
738}
739
740/*
741 * A policy node rule has been updated. Propogate this update to all the
742 * block groups which might be affected by this update.
743 */
744static void blkio_update_policy_node_blkg(struct blkio_cgroup *blkcg,
745 struct blkio_policy_node *pn)
746{
747 struct blkio_group *blkg;
748 struct hlist_node *n;
749
750 spin_lock(&blkio_list_lock);
751 spin_lock_irq(&blkcg->lock);
752
753 hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) {
754 if (pn->dev != blkg->dev || pn->plid != blkg->plid)
755 continue;
756 blkio_update_blkg_policy(blkcg, blkg, pn);
757 }
758
759 spin_unlock_irq(&blkcg->lock);
760 spin_unlock(&blkio_list_lock);
761}
762
763static int blkiocg_file_write(struct cgroup *cgrp, struct cftype *cft,
764 const char *buffer)
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800765{
766 int ret = 0;
767 char *buf;
768 struct blkio_policy_node *newpn, *pn;
769 struct blkio_cgroup *blkcg;
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800770 int keep_newpn = 0;
Vivek Goyal062a6442010-09-15 17:06:33 -0400771 enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
772 int fileid = BLKIOFILE_ATTR(cft->private);
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800773
774 buf = kstrdup(buffer, GFP_KERNEL);
775 if (!buf)
776 return -ENOMEM;
777
778 newpn = kzalloc(sizeof(*newpn), GFP_KERNEL);
779 if (!newpn) {
780 ret = -ENOMEM;
781 goto free_buf;
782 }
783
Vivek Goyal062a6442010-09-15 17:06:33 -0400784 ret = blkio_policy_parse_and_set(buf, newpn, plid, fileid);
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800785 if (ret)
786 goto free_newpn;
787
788 blkcg = cgroup_to_blkio_cgroup(cgrp);
789
790 spin_lock_irq(&blkcg->lock);
791
Vivek Goyal062a6442010-09-15 17:06:33 -0400792 pn = blkio_policy_search_node(blkcg, newpn->dev, plid, fileid);
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800793 if (!pn) {
Vivek Goyal062a6442010-09-15 17:06:33 -0400794 if (!blkio_delete_rule_command(newpn)) {
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800795 blkio_policy_insert_node(blkcg, newpn);
796 keep_newpn = 1;
797 }
798 spin_unlock_irq(&blkcg->lock);
799 goto update_io_group;
800 }
801
Vivek Goyal062a6442010-09-15 17:06:33 -0400802 if (blkio_delete_rule_command(newpn)) {
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800803 blkio_policy_delete_node(pn);
804 spin_unlock_irq(&blkcg->lock);
805 goto update_io_group;
806 }
807 spin_unlock_irq(&blkcg->lock);
808
Vivek Goyal062a6442010-09-15 17:06:33 -0400809 blkio_update_policy_rule(pn, newpn);
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800810
811update_io_group:
Vivek Goyal062a6442010-09-15 17:06:33 -0400812 blkio_update_policy_node_blkg(blkcg, newpn);
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800813
814free_newpn:
815 if (!keep_newpn)
816 kfree(newpn);
817free_buf:
818 kfree(buf);
819 return ret;
820}
821
Vivek Goyal062a6442010-09-15 17:06:33 -0400822static void
823blkio_print_policy_node(struct seq_file *m, struct blkio_policy_node *pn)
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800824{
Vivek Goyal062a6442010-09-15 17:06:33 -0400825 switch(pn->plid) {
826 case BLKIO_POLICY_PROP:
827 if (pn->fileid == BLKIO_PROP_weight_device)
828 seq_printf(m, "%u:%u\t%u\n", MAJOR(pn->dev),
829 MINOR(pn->dev), pn->weight);
830 break;
831 default:
832 BUG();
833 }
834}
835
836/* cgroup files which read their data from policy nodes end up here */
837static void blkio_read_policy_node_files(struct cftype *cft,
838 struct blkio_cgroup *blkcg, struct seq_file *m)
839{
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800840 struct blkio_policy_node *pn;
841
Jens Axboe0f3942a2010-05-03 14:28:55 +0200842 if (!list_empty(&blkcg->policy_list)) {
843 spin_lock_irq(&blkcg->lock);
844 list_for_each_entry(pn, &blkcg->policy_list, node) {
Vivek Goyal062a6442010-09-15 17:06:33 -0400845 if (!pn_matches_cftype(cft, pn))
846 continue;
847 blkio_print_policy_node(m, pn);
Jens Axboe0f3942a2010-05-03 14:28:55 +0200848 }
849 spin_unlock_irq(&blkcg->lock);
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800850 }
Vivek Goyal062a6442010-09-15 17:06:33 -0400851}
852
853static int blkiocg_file_read(struct cgroup *cgrp, struct cftype *cft,
854 struct seq_file *m)
855{
856 struct blkio_cgroup *blkcg;
857 enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
858 int name = BLKIOFILE_ATTR(cft->private);
859
860 blkcg = cgroup_to_blkio_cgroup(cgrp);
861
862 switch(plid) {
863 case BLKIO_POLICY_PROP:
864 switch(name) {
865 case BLKIO_PROP_weight_device:
866 blkio_read_policy_node_files(cft, blkcg, m);
867 return 0;
868 default:
869 BUG();
870 }
871 break;
872 default:
873 BUG();
874 }
875
876 return 0;
877}
878
879static int blkio_read_blkg_stats(struct blkio_cgroup *blkcg,
880 struct cftype *cft, struct cgroup_map_cb *cb, enum stat_type type,
881 bool show_total)
882{
883 struct blkio_group *blkg;
884 struct hlist_node *n;
885 uint64_t cgroup_total = 0;
886
887 rcu_read_lock();
888 hlist_for_each_entry_rcu(blkg, n, &blkcg->blkg_list, blkcg_node) {
889 if (blkg->dev) {
890 if (!cftype_blkg_same_policy(cft, blkg))
891 continue;
892 spin_lock_irq(&blkg->stats_lock);
893 cgroup_total += blkio_get_stat(blkg, cb, blkg->dev,
894 type);
895 spin_unlock_irq(&blkg->stats_lock);
896 }
897 }
898 if (show_total)
899 cb->fill(cb, "Total", cgroup_total);
900 rcu_read_unlock();
901 return 0;
902}
903
904/* All map kind of cgroup file get serviced by this function */
905static int blkiocg_file_read_map(struct cgroup *cgrp, struct cftype *cft,
906 struct cgroup_map_cb *cb)
907{
908 struct blkio_cgroup *blkcg;
909 enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
910 int name = BLKIOFILE_ATTR(cft->private);
911
912 blkcg = cgroup_to_blkio_cgroup(cgrp);
913
914 switch(plid) {
915 case BLKIO_POLICY_PROP:
916 switch(name) {
917 case BLKIO_PROP_time:
918 return blkio_read_blkg_stats(blkcg, cft, cb,
919 BLKIO_STAT_TIME, 0);
920 case BLKIO_PROP_sectors:
921 return blkio_read_blkg_stats(blkcg, cft, cb,
922 BLKIO_STAT_SECTORS, 0);
923 case BLKIO_PROP_io_service_bytes:
924 return blkio_read_blkg_stats(blkcg, cft, cb,
925 BLKIO_STAT_SERVICE_BYTES, 1);
926 case BLKIO_PROP_io_serviced:
927 return blkio_read_blkg_stats(blkcg, cft, cb,
928 BLKIO_STAT_SERVICED, 1);
929 case BLKIO_PROP_io_service_time:
930 return blkio_read_blkg_stats(blkcg, cft, cb,
931 BLKIO_STAT_SERVICE_TIME, 1);
932 case BLKIO_PROP_io_wait_time:
933 return blkio_read_blkg_stats(blkcg, cft, cb,
934 BLKIO_STAT_WAIT_TIME, 1);
935 case BLKIO_PROP_io_merged:
936 return blkio_read_blkg_stats(blkcg, cft, cb,
937 BLKIO_STAT_MERGED, 1);
938 case BLKIO_PROP_io_queued:
939 return blkio_read_blkg_stats(blkcg, cft, cb,
940 BLKIO_STAT_QUEUED, 1);
941#ifdef CONFIG_DEBUG_BLK_CGROUP
942 case BLKIO_PROP_dequeue:
943 return blkio_read_blkg_stats(blkcg, cft, cb,
944 BLKIO_STAT_DEQUEUE, 0);
945 case BLKIO_PROP_avg_queue_size:
946 return blkio_read_blkg_stats(blkcg, cft, cb,
947 BLKIO_STAT_AVG_QUEUE_SIZE, 0);
948 case BLKIO_PROP_group_wait_time:
949 return blkio_read_blkg_stats(blkcg, cft, cb,
950 BLKIO_STAT_GROUP_WAIT_TIME, 0);
951 case BLKIO_PROP_idle_time:
952 return blkio_read_blkg_stats(blkcg, cft, cb,
953 BLKIO_STAT_IDLE_TIME, 0);
954 case BLKIO_PROP_empty_time:
955 return blkio_read_blkg_stats(blkcg, cft, cb,
956 BLKIO_STAT_EMPTY_TIME, 0);
957#endif
958 default:
959 BUG();
960 }
961 break;
962
963 default:
964 BUG();
965 }
966
967 return 0;
968}
969
970static int blkio_weight_write(struct blkio_cgroup *blkcg, u64 val)
971{
972 struct blkio_group *blkg;
973 struct hlist_node *n;
974 struct blkio_policy_node *pn;
975
976 if (val < BLKIO_WEIGHT_MIN || val > BLKIO_WEIGHT_MAX)
977 return -EINVAL;
978
979 spin_lock(&blkio_list_lock);
980 spin_lock_irq(&blkcg->lock);
981 blkcg->weight = (unsigned int)val;
982
983 hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) {
984 pn = blkio_policy_search_node(blkcg, blkg->dev,
985 BLKIO_POLICY_PROP, BLKIO_PROP_weight_device);
986 if (pn)
987 continue;
988
989 blkio_update_group_weight(blkg, blkcg->weight);
990 }
991 spin_unlock_irq(&blkcg->lock);
992 spin_unlock(&blkio_list_lock);
993 return 0;
994}
995
996static u64 blkiocg_file_read_u64 (struct cgroup *cgrp, struct cftype *cft) {
997 struct blkio_cgroup *blkcg;
998 enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
999 int name = BLKIOFILE_ATTR(cft->private);
1000
1001 blkcg = cgroup_to_blkio_cgroup(cgrp);
1002
1003 switch(plid) {
1004 case BLKIO_POLICY_PROP:
1005 switch(name) {
1006 case BLKIO_PROP_weight:
1007 return (u64)blkcg->weight;
1008 }
1009 break;
1010 default:
1011 BUG();
1012 }
1013 return 0;
1014}
1015
1016static int
1017blkiocg_file_write_u64(struct cgroup *cgrp, struct cftype *cft, u64 val)
1018{
1019 struct blkio_cgroup *blkcg;
1020 enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
1021 int name = BLKIOFILE_ATTR(cft->private);
1022
1023 blkcg = cgroup_to_blkio_cgroup(cgrp);
1024
1025 switch(plid) {
1026 case BLKIO_POLICY_PROP:
1027 switch(name) {
1028 case BLKIO_PROP_weight:
1029 return blkio_weight_write(blkcg, val);
1030 }
1031 break;
1032 default:
1033 BUG();
1034 }
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001035
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001036 return 0;
1037}
1038
Vivek Goyal31e4c282009-12-03 12:59:42 -05001039struct cftype blkio_files[] = {
1040 {
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001041 .name = "weight_device",
Vivek Goyal062a6442010-09-15 17:06:33 -04001042 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1043 BLKIO_PROP_weight_device),
1044 .read_seq_string = blkiocg_file_read,
1045 .write_string = blkiocg_file_write,
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001046 .max_write_len = 256,
1047 },
1048 {
Vivek Goyal31e4c282009-12-03 12:59:42 -05001049 .name = "weight",
Vivek Goyal062a6442010-09-15 17:06:33 -04001050 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1051 BLKIO_PROP_weight),
1052 .read_u64 = blkiocg_file_read_u64,
1053 .write_u64 = blkiocg_file_write_u64,
Vivek Goyal31e4c282009-12-03 12:59:42 -05001054 },
Vivek Goyal22084192009-12-03 12:59:49 -05001055 {
1056 .name = "time",
Vivek Goyal062a6442010-09-15 17:06:33 -04001057 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1058 BLKIO_PROP_time),
1059 .read_map = blkiocg_file_read_map,
Vivek Goyal22084192009-12-03 12:59:49 -05001060 },
1061 {
1062 .name = "sectors",
Vivek Goyal062a6442010-09-15 17:06:33 -04001063 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1064 BLKIO_PROP_sectors),
1065 .read_map = blkiocg_file_read_map,
Divyesh Shah303a3ac2010-04-01 15:01:24 -07001066 },
1067 {
1068 .name = "io_service_bytes",
Vivek Goyal062a6442010-09-15 17:06:33 -04001069 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1070 BLKIO_PROP_io_service_bytes),
1071 .read_map = blkiocg_file_read_map,
Divyesh Shah303a3ac2010-04-01 15:01:24 -07001072 },
1073 {
1074 .name = "io_serviced",
Vivek Goyal062a6442010-09-15 17:06:33 -04001075 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1076 BLKIO_PROP_io_serviced),
1077 .read_map = blkiocg_file_read_map,
Divyesh Shah303a3ac2010-04-01 15:01:24 -07001078 },
1079 {
1080 .name = "io_service_time",
Vivek Goyal062a6442010-09-15 17:06:33 -04001081 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1082 BLKIO_PROP_io_service_time),
1083 .read_map = blkiocg_file_read_map,
Divyesh Shah303a3ac2010-04-01 15:01:24 -07001084 },
1085 {
1086 .name = "io_wait_time",
Vivek Goyal062a6442010-09-15 17:06:33 -04001087 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1088 BLKIO_PROP_io_wait_time),
1089 .read_map = blkiocg_file_read_map,
Divyesh Shah84c124d2010-04-09 08:31:19 +02001090 },
1091 {
Divyesh Shah812d4022010-04-08 21:14:23 -07001092 .name = "io_merged",
Vivek Goyal062a6442010-09-15 17:06:33 -04001093 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1094 BLKIO_PROP_io_merged),
1095 .read_map = blkiocg_file_read_map,
Divyesh Shah812d4022010-04-08 21:14:23 -07001096 },
1097 {
Divyesh Shahcdc11842010-04-08 21:15:10 -07001098 .name = "io_queued",
Vivek Goyal062a6442010-09-15 17:06:33 -04001099 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1100 BLKIO_PROP_io_queued),
1101 .read_map = blkiocg_file_read_map,
Divyesh Shahcdc11842010-04-08 21:15:10 -07001102 },
1103 {
Divyesh Shah84c124d2010-04-09 08:31:19 +02001104 .name = "reset_stats",
1105 .write_u64 = blkiocg_reset_stats,
Vivek Goyal22084192009-12-03 12:59:49 -05001106 },
1107#ifdef CONFIG_DEBUG_BLK_CGROUP
Divyesh Shahcdc11842010-04-08 21:15:10 -07001108 {
1109 .name = "avg_queue_size",
Vivek Goyal062a6442010-09-15 17:06:33 -04001110 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1111 BLKIO_PROP_avg_queue_size),
1112 .read_map = blkiocg_file_read_map,
Divyesh Shahcdc11842010-04-08 21:15:10 -07001113 },
1114 {
Divyesh Shah812df482010-04-08 21:15:35 -07001115 .name = "group_wait_time",
Vivek Goyal062a6442010-09-15 17:06:33 -04001116 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1117 BLKIO_PROP_group_wait_time),
1118 .read_map = blkiocg_file_read_map,
Divyesh Shah812df482010-04-08 21:15:35 -07001119 },
1120 {
1121 .name = "idle_time",
Vivek Goyal062a6442010-09-15 17:06:33 -04001122 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1123 BLKIO_PROP_idle_time),
1124 .read_map = blkiocg_file_read_map,
Divyesh Shah812df482010-04-08 21:15:35 -07001125 },
1126 {
1127 .name = "empty_time",
Vivek Goyal062a6442010-09-15 17:06:33 -04001128 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1129 BLKIO_PROP_empty_time),
1130 .read_map = blkiocg_file_read_map,
Divyesh Shah812df482010-04-08 21:15:35 -07001131 },
1132 {
Vivek Goyal22084192009-12-03 12:59:49 -05001133 .name = "dequeue",
Vivek Goyal062a6442010-09-15 17:06:33 -04001134 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1135 BLKIO_PROP_dequeue),
1136 .read_map = blkiocg_file_read_map,
Divyesh Shahcdc11842010-04-08 21:15:10 -07001137 },
Vivek Goyal22084192009-12-03 12:59:49 -05001138#endif
Vivek Goyal31e4c282009-12-03 12:59:42 -05001139};
1140
1141static int blkiocg_populate(struct cgroup_subsys *subsys, struct cgroup *cgroup)
1142{
1143 return cgroup_add_files(cgroup, subsys, blkio_files,
1144 ARRAY_SIZE(blkio_files));
1145}
1146
1147static void blkiocg_destroy(struct cgroup_subsys *subsys, struct cgroup *cgroup)
1148{
1149 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgroup);
Vivek Goyalb1c35762009-12-03 12:59:47 -05001150 unsigned long flags;
1151 struct blkio_group *blkg;
1152 void *key;
Vivek Goyal3e252062009-12-04 10:36:42 -05001153 struct blkio_policy_type *blkiop;
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001154 struct blkio_policy_node *pn, *pntmp;
Vivek Goyal31e4c282009-12-03 12:59:42 -05001155
Vivek Goyalb1c35762009-12-03 12:59:47 -05001156 rcu_read_lock();
Jens Axboe0f3942a2010-05-03 14:28:55 +02001157 do {
1158 spin_lock_irqsave(&blkcg->lock, flags);
Vivek Goyalb1c35762009-12-03 12:59:47 -05001159
Jens Axboe0f3942a2010-05-03 14:28:55 +02001160 if (hlist_empty(&blkcg->blkg_list)) {
1161 spin_unlock_irqrestore(&blkcg->lock, flags);
1162 break;
1163 }
1164
1165 blkg = hlist_entry(blkcg->blkg_list.first, struct blkio_group,
1166 blkcg_node);
1167 key = rcu_dereference(blkg->key);
1168 __blkiocg_del_blkio_group(blkg);
1169
Vivek Goyalb1c35762009-12-03 12:59:47 -05001170 spin_unlock_irqrestore(&blkcg->lock, flags);
Vivek Goyalb1c35762009-12-03 12:59:47 -05001171
Jens Axboe0f3942a2010-05-03 14:28:55 +02001172 /*
1173 * This blkio_group is being unlinked as associated cgroup is
1174 * going away. Let all the IO controlling policies know about
1175 * this event. Currently this is static call to one io
1176 * controlling policy. Once we have more policies in place, we
1177 * need some dynamic registration of callback function.
1178 */
1179 spin_lock(&blkio_list_lock);
1180 list_for_each_entry(blkiop, &blkio_list, list)
1181 blkiop->ops.blkio_unlink_group_fn(key, blkg);
1182 spin_unlock(&blkio_list_lock);
1183 } while (1);
Vivek Goyalb1c35762009-12-03 12:59:47 -05001184
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001185 list_for_each_entry_safe(pn, pntmp, &blkcg->policy_list, node) {
1186 blkio_policy_delete_node(pn);
1187 kfree(pn);
1188 }
Jens Axboe0f3942a2010-05-03 14:28:55 +02001189
Vivek Goyal31e4c282009-12-03 12:59:42 -05001190 free_css_id(&blkio_subsys, &blkcg->css);
Vivek Goyalb1c35762009-12-03 12:59:47 -05001191 rcu_read_unlock();
Ben Blum67523c42010-03-10 15:22:11 -08001192 if (blkcg != &blkio_root_cgroup)
1193 kfree(blkcg);
Vivek Goyal31e4c282009-12-03 12:59:42 -05001194}
1195
1196static struct cgroup_subsys_state *
1197blkiocg_create(struct cgroup_subsys *subsys, struct cgroup *cgroup)
1198{
Li Zefan03415092010-05-07 08:57:00 +02001199 struct blkio_cgroup *blkcg;
1200 struct cgroup *parent = cgroup->parent;
Vivek Goyal31e4c282009-12-03 12:59:42 -05001201
Li Zefan03415092010-05-07 08:57:00 +02001202 if (!parent) {
Vivek Goyal31e4c282009-12-03 12:59:42 -05001203 blkcg = &blkio_root_cgroup;
1204 goto done;
1205 }
1206
1207 /* Currently we do not support hierarchy deeper than two level (0,1) */
Li Zefan03415092010-05-07 08:57:00 +02001208 if (parent != cgroup->top_cgroup)
Vivek Goyal31e4c282009-12-03 12:59:42 -05001209 return ERR_PTR(-EINVAL);
1210
1211 blkcg = kzalloc(sizeof(*blkcg), GFP_KERNEL);
1212 if (!blkcg)
1213 return ERR_PTR(-ENOMEM);
1214
1215 blkcg->weight = BLKIO_WEIGHT_DEFAULT;
1216done:
1217 spin_lock_init(&blkcg->lock);
1218 INIT_HLIST_HEAD(&blkcg->blkg_list);
1219
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001220 INIT_LIST_HEAD(&blkcg->policy_list);
Vivek Goyal31e4c282009-12-03 12:59:42 -05001221 return &blkcg->css;
1222}
1223
1224/*
1225 * We cannot support shared io contexts, as we have no mean to support
1226 * two tasks with the same ioc in two different groups without major rework
1227 * of the main cic data structures. For now we allow a task to change
1228 * its cgroup only if it's the only owner of its ioc.
1229 */
1230static int blkiocg_can_attach(struct cgroup_subsys *subsys,
1231 struct cgroup *cgroup, struct task_struct *tsk,
1232 bool threadgroup)
1233{
1234 struct io_context *ioc;
1235 int ret = 0;
1236
1237 /* task_lock() is needed to avoid races with exit_io_context() */
1238 task_lock(tsk);
1239 ioc = tsk->io_context;
1240 if (ioc && atomic_read(&ioc->nr_tasks) > 1)
1241 ret = -EINVAL;
1242 task_unlock(tsk);
1243
1244 return ret;
1245}
1246
1247static void blkiocg_attach(struct cgroup_subsys *subsys, struct cgroup *cgroup,
1248 struct cgroup *prev, struct task_struct *tsk,
1249 bool threadgroup)
1250{
1251 struct io_context *ioc;
1252
1253 task_lock(tsk);
1254 ioc = tsk->io_context;
1255 if (ioc)
1256 ioc->cgroup_changed = 1;
1257 task_unlock(tsk);
1258}
1259
Vivek Goyal3e252062009-12-04 10:36:42 -05001260void blkio_policy_register(struct blkio_policy_type *blkiop)
1261{
1262 spin_lock(&blkio_list_lock);
1263 list_add_tail(&blkiop->list, &blkio_list);
1264 spin_unlock(&blkio_list_lock);
1265}
1266EXPORT_SYMBOL_GPL(blkio_policy_register);
1267
1268void blkio_policy_unregister(struct blkio_policy_type *blkiop)
1269{
1270 spin_lock(&blkio_list_lock);
1271 list_del_init(&blkiop->list);
1272 spin_unlock(&blkio_list_lock);
1273}
1274EXPORT_SYMBOL_GPL(blkio_policy_unregister);
Ben Blum67523c42010-03-10 15:22:11 -08001275
1276static int __init init_cgroup_blkio(void)
1277{
1278 return cgroup_load_subsys(&blkio_subsys);
1279}
1280
1281static void __exit exit_cgroup_blkio(void)
1282{
1283 cgroup_unload_subsys(&blkio_subsys);
1284}
1285
1286module_init(init_cgroup_blkio);
1287module_exit(exit_cgroup_blkio);
1288MODULE_LICENSE("GPL");