blob: aae8c930a6f8a479d8e1d15b0b0baf0628fde1f0 [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
Divyesh Shah91952912010-04-01 15:01:41 -0700152/*
153 * Add to the appropriate stat variable depending on the request type.
154 * This should be called with the blkg->stats_lock held.
155 */
Divyesh Shah84c124d2010-04-09 08:31:19 +0200156static void blkio_add_stat(uint64_t *stat, uint64_t add, bool direction,
157 bool sync)
Divyesh Shah91952912010-04-01 15:01:41 -0700158{
Divyesh Shah84c124d2010-04-09 08:31:19 +0200159 if (direction)
160 stat[BLKIO_STAT_WRITE] += add;
Divyesh Shah91952912010-04-01 15:01:41 -0700161 else
Divyesh Shah84c124d2010-04-09 08:31:19 +0200162 stat[BLKIO_STAT_READ] += add;
163 if (sync)
164 stat[BLKIO_STAT_SYNC] += add;
Divyesh Shah91952912010-04-01 15:01:41 -0700165 else
Divyesh Shah84c124d2010-04-09 08:31:19 +0200166 stat[BLKIO_STAT_ASYNC] += add;
Divyesh Shah91952912010-04-01 15:01:41 -0700167}
168
Divyesh Shahcdc11842010-04-08 21:15:10 -0700169/*
170 * Decrements the appropriate stat variable if non-zero depending on the
171 * request type. Panics on value being zero.
172 * This should be called with the blkg->stats_lock held.
173 */
174static void blkio_check_and_dec_stat(uint64_t *stat, bool direction, bool sync)
175{
176 if (direction) {
177 BUG_ON(stat[BLKIO_STAT_WRITE] == 0);
178 stat[BLKIO_STAT_WRITE]--;
179 } else {
180 BUG_ON(stat[BLKIO_STAT_READ] == 0);
181 stat[BLKIO_STAT_READ]--;
182 }
183 if (sync) {
184 BUG_ON(stat[BLKIO_STAT_SYNC] == 0);
185 stat[BLKIO_STAT_SYNC]--;
186 } else {
187 BUG_ON(stat[BLKIO_STAT_ASYNC] == 0);
188 stat[BLKIO_STAT_ASYNC]--;
189 }
190}
191
192#ifdef CONFIG_DEBUG_BLK_CGROUP
Divyesh Shah812df482010-04-08 21:15:35 -0700193/* This should be called with the blkg->stats_lock held. */
194static void blkio_set_start_group_wait_time(struct blkio_group *blkg,
195 struct blkio_group *curr_blkg)
196{
197 if (blkio_blkg_waiting(&blkg->stats))
198 return;
199 if (blkg == curr_blkg)
200 return;
201 blkg->stats.start_group_wait_time = sched_clock();
202 blkio_mark_blkg_waiting(&blkg->stats);
203}
204
205/* This should be called with the blkg->stats_lock held. */
206static void blkio_update_group_wait_time(struct blkio_group_stats *stats)
207{
208 unsigned long long now;
209
210 if (!blkio_blkg_waiting(stats))
211 return;
212
213 now = sched_clock();
214 if (time_after64(now, stats->start_group_wait_time))
215 stats->group_wait_time += now - stats->start_group_wait_time;
216 blkio_clear_blkg_waiting(stats);
217}
218
219/* This should be called with the blkg->stats_lock held. */
220static void blkio_end_empty_time(struct blkio_group_stats *stats)
221{
222 unsigned long long now;
223
224 if (!blkio_blkg_empty(stats))
225 return;
226
227 now = sched_clock();
228 if (time_after64(now, stats->start_empty_time))
229 stats->empty_time += now - stats->start_empty_time;
230 blkio_clear_blkg_empty(stats);
231}
232
233void blkiocg_update_set_idle_time_stats(struct blkio_group *blkg)
234{
235 unsigned long flags;
236
237 spin_lock_irqsave(&blkg->stats_lock, flags);
238 BUG_ON(blkio_blkg_idling(&blkg->stats));
239 blkg->stats.start_idle_time = sched_clock();
240 blkio_mark_blkg_idling(&blkg->stats);
241 spin_unlock_irqrestore(&blkg->stats_lock, flags);
242}
243EXPORT_SYMBOL_GPL(blkiocg_update_set_idle_time_stats);
244
245void blkiocg_update_idle_time_stats(struct blkio_group *blkg)
246{
247 unsigned long flags;
248 unsigned long long now;
249 struct blkio_group_stats *stats;
250
251 spin_lock_irqsave(&blkg->stats_lock, flags);
252 stats = &blkg->stats;
253 if (blkio_blkg_idling(stats)) {
254 now = sched_clock();
255 if (time_after64(now, stats->start_idle_time))
256 stats->idle_time += now - stats->start_idle_time;
257 blkio_clear_blkg_idling(stats);
258 }
259 spin_unlock_irqrestore(&blkg->stats_lock, flags);
260}
261EXPORT_SYMBOL_GPL(blkiocg_update_idle_time_stats);
262
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200263void blkiocg_update_avg_queue_size_stats(struct blkio_group *blkg)
Divyesh Shahcdc11842010-04-08 21:15:10 -0700264{
265 unsigned long flags;
266 struct blkio_group_stats *stats;
267
268 spin_lock_irqsave(&blkg->stats_lock, flags);
269 stats = &blkg->stats;
270 stats->avg_queue_size_sum +=
271 stats->stat_arr[BLKIO_STAT_QUEUED][BLKIO_STAT_READ] +
272 stats->stat_arr[BLKIO_STAT_QUEUED][BLKIO_STAT_WRITE];
273 stats->avg_queue_size_samples++;
Divyesh Shah812df482010-04-08 21:15:35 -0700274 blkio_update_group_wait_time(stats);
Divyesh Shahcdc11842010-04-08 21:15:10 -0700275 spin_unlock_irqrestore(&blkg->stats_lock, flags);
276}
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200277EXPORT_SYMBOL_GPL(blkiocg_update_avg_queue_size_stats);
278
Vivek Goyale5ff0822010-04-26 19:25:11 +0200279void blkiocg_set_start_empty_time(struct blkio_group *blkg)
Divyesh Shah28baf442010-04-14 11:22:38 +0200280{
281 unsigned long flags;
282 struct blkio_group_stats *stats;
283
284 spin_lock_irqsave(&blkg->stats_lock, flags);
285 stats = &blkg->stats;
286
287 if (stats->stat_arr[BLKIO_STAT_QUEUED][BLKIO_STAT_READ] ||
288 stats->stat_arr[BLKIO_STAT_QUEUED][BLKIO_STAT_WRITE]) {
289 spin_unlock_irqrestore(&blkg->stats_lock, flags);
290 return;
291 }
292
293 /*
Vivek Goyale5ff0822010-04-26 19:25:11 +0200294 * group is already marked empty. This can happen if cfqq got new
295 * request in parent group and moved to this group while being added
296 * to service tree. Just ignore the event and move on.
Divyesh Shah28baf442010-04-14 11:22:38 +0200297 */
Vivek Goyale5ff0822010-04-26 19:25:11 +0200298 if(blkio_blkg_empty(stats)) {
299 spin_unlock_irqrestore(&blkg->stats_lock, flags);
300 return;
301 }
302
Divyesh Shah28baf442010-04-14 11:22:38 +0200303 stats->start_empty_time = sched_clock();
304 blkio_mark_blkg_empty(stats);
305 spin_unlock_irqrestore(&blkg->stats_lock, flags);
306}
307EXPORT_SYMBOL_GPL(blkiocg_set_start_empty_time);
308
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200309void blkiocg_update_dequeue_stats(struct blkio_group *blkg,
310 unsigned long dequeue)
311{
312 blkg->stats.dequeue += dequeue;
313}
314EXPORT_SYMBOL_GPL(blkiocg_update_dequeue_stats);
Divyesh Shah812df482010-04-08 21:15:35 -0700315#else
316static inline void blkio_set_start_group_wait_time(struct blkio_group *blkg,
317 struct blkio_group *curr_blkg) {}
318static inline void blkio_end_empty_time(struct blkio_group_stats *stats) {}
Divyesh Shahcdc11842010-04-08 21:15:10 -0700319#endif
320
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200321void blkiocg_update_io_add_stats(struct blkio_group *blkg,
Divyesh Shahcdc11842010-04-08 21:15:10 -0700322 struct blkio_group *curr_blkg, bool direction,
323 bool sync)
324{
325 unsigned long flags;
326
327 spin_lock_irqsave(&blkg->stats_lock, flags);
328 blkio_add_stat(blkg->stats.stat_arr[BLKIO_STAT_QUEUED], 1, direction,
329 sync);
Divyesh Shah812df482010-04-08 21:15:35 -0700330 blkio_end_empty_time(&blkg->stats);
331 blkio_set_start_group_wait_time(blkg, curr_blkg);
Divyesh Shahcdc11842010-04-08 21:15:10 -0700332 spin_unlock_irqrestore(&blkg->stats_lock, flags);
333}
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200334EXPORT_SYMBOL_GPL(blkiocg_update_io_add_stats);
Divyesh Shahcdc11842010-04-08 21:15:10 -0700335
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200336void blkiocg_update_io_remove_stats(struct blkio_group *blkg,
Divyesh Shahcdc11842010-04-08 21:15:10 -0700337 bool direction, bool sync)
338{
339 unsigned long flags;
340
341 spin_lock_irqsave(&blkg->stats_lock, flags);
342 blkio_check_and_dec_stat(blkg->stats.stat_arr[BLKIO_STAT_QUEUED],
343 direction, sync);
344 spin_unlock_irqrestore(&blkg->stats_lock, flags);
345}
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200346EXPORT_SYMBOL_GPL(blkiocg_update_io_remove_stats);
Divyesh Shahcdc11842010-04-08 21:15:10 -0700347
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700348void blkiocg_update_timeslice_used(struct blkio_group *blkg, unsigned long time)
Vivek Goyal22084192009-12-03 12:59:49 -0500349{
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700350 unsigned long flags;
351
352 spin_lock_irqsave(&blkg->stats_lock, flags);
353 blkg->stats.time += time;
354 spin_unlock_irqrestore(&blkg->stats_lock, flags);
Vivek Goyal22084192009-12-03 12:59:49 -0500355}
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700356EXPORT_SYMBOL_GPL(blkiocg_update_timeslice_used);
Vivek Goyal22084192009-12-03 12:59:49 -0500357
Divyesh Shah84c124d2010-04-09 08:31:19 +0200358void blkiocg_update_dispatch_stats(struct blkio_group *blkg,
359 uint64_t bytes, bool direction, bool sync)
Divyesh Shah91952912010-04-01 15:01:41 -0700360{
361 struct blkio_group_stats *stats;
362 unsigned long flags;
363
364 spin_lock_irqsave(&blkg->stats_lock, flags);
365 stats = &blkg->stats;
Divyesh Shah84c124d2010-04-09 08:31:19 +0200366 stats->sectors += bytes >> 9;
367 blkio_add_stat(stats->stat_arr[BLKIO_STAT_SERVICED], 1, direction,
368 sync);
369 blkio_add_stat(stats->stat_arr[BLKIO_STAT_SERVICE_BYTES], bytes,
370 direction, sync);
Divyesh Shah91952912010-04-01 15:01:41 -0700371 spin_unlock_irqrestore(&blkg->stats_lock, flags);
372}
Divyesh Shah84c124d2010-04-09 08:31:19 +0200373EXPORT_SYMBOL_GPL(blkiocg_update_dispatch_stats);
Divyesh Shah91952912010-04-01 15:01:41 -0700374
Divyesh Shah84c124d2010-04-09 08:31:19 +0200375void blkiocg_update_completion_stats(struct blkio_group *blkg,
376 uint64_t start_time, uint64_t io_start_time, bool direction, bool sync)
Divyesh Shah91952912010-04-01 15:01:41 -0700377{
378 struct blkio_group_stats *stats;
379 unsigned long flags;
380 unsigned long long now = sched_clock();
381
382 spin_lock_irqsave(&blkg->stats_lock, flags);
383 stats = &blkg->stats;
Divyesh Shah84c124d2010-04-09 08:31:19 +0200384 if (time_after64(now, io_start_time))
385 blkio_add_stat(stats->stat_arr[BLKIO_STAT_SERVICE_TIME],
386 now - io_start_time, direction, sync);
387 if (time_after64(io_start_time, start_time))
388 blkio_add_stat(stats->stat_arr[BLKIO_STAT_WAIT_TIME],
389 io_start_time - start_time, direction, sync);
Divyesh Shah91952912010-04-01 15:01:41 -0700390 spin_unlock_irqrestore(&blkg->stats_lock, flags);
391}
Divyesh Shah84c124d2010-04-09 08:31:19 +0200392EXPORT_SYMBOL_GPL(blkiocg_update_completion_stats);
Divyesh Shah91952912010-04-01 15:01:41 -0700393
Divyesh Shah812d4022010-04-08 21:14:23 -0700394void blkiocg_update_io_merged_stats(struct blkio_group *blkg, bool direction,
395 bool sync)
396{
397 unsigned long flags;
398
399 spin_lock_irqsave(&blkg->stats_lock, flags);
400 blkio_add_stat(blkg->stats.stat_arr[BLKIO_STAT_MERGED], 1, direction,
401 sync);
402 spin_unlock_irqrestore(&blkg->stats_lock, flags);
403}
404EXPORT_SYMBOL_GPL(blkiocg_update_io_merged_stats);
405
Vivek Goyal31e4c282009-12-03 12:59:42 -0500406void blkiocg_add_blkio_group(struct blkio_cgroup *blkcg,
Vivek Goyal062a6442010-09-15 17:06:33 -0400407 struct blkio_group *blkg, void *key, dev_t dev,
408 enum blkio_policy_id plid)
Vivek Goyal31e4c282009-12-03 12:59:42 -0500409{
410 unsigned long flags;
411
412 spin_lock_irqsave(&blkcg->lock, flags);
Divyesh Shah8d2a91f2010-04-16 08:10:51 +0200413 spin_lock_init(&blkg->stats_lock);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500414 rcu_assign_pointer(blkg->key, key);
Vivek Goyalb1c35762009-12-03 12:59:47 -0500415 blkg->blkcg_id = css_id(&blkcg->css);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500416 hlist_add_head_rcu(&blkg->blkcg_node, &blkcg->blkg_list);
Vivek Goyal062a6442010-09-15 17:06:33 -0400417 blkg->plid = plid;
Vivek Goyal31e4c282009-12-03 12:59:42 -0500418 spin_unlock_irqrestore(&blkcg->lock, flags);
Vivek Goyal2868ef72009-12-03 12:59:48 -0500419 /* Need to take css reference ? */
420 cgroup_path(blkcg->css.cgroup, blkg->path, sizeof(blkg->path));
Vivek Goyal22084192009-12-03 12:59:49 -0500421 blkg->dev = dev;
Vivek Goyal31e4c282009-12-03 12:59:42 -0500422}
Vivek Goyal9d6a9862009-12-04 10:36:41 -0500423EXPORT_SYMBOL_GPL(blkiocg_add_blkio_group);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500424
Vivek Goyalb1c35762009-12-03 12:59:47 -0500425static void __blkiocg_del_blkio_group(struct blkio_group *blkg)
426{
427 hlist_del_init_rcu(&blkg->blkcg_node);
428 blkg->blkcg_id = 0;
429}
430
431/*
432 * returns 0 if blkio_group was still on cgroup list. Otherwise returns 1
433 * indicating that blk_group was unhashed by the time we got to it.
434 */
Vivek Goyal31e4c282009-12-03 12:59:42 -0500435int blkiocg_del_blkio_group(struct blkio_group *blkg)
436{
Vivek Goyalb1c35762009-12-03 12:59:47 -0500437 struct blkio_cgroup *blkcg;
438 unsigned long flags;
439 struct cgroup_subsys_state *css;
440 int ret = 1;
441
442 rcu_read_lock();
443 css = css_lookup(&blkio_subsys, blkg->blkcg_id);
Jens Axboe0f3942a2010-05-03 14:28:55 +0200444 if (css) {
445 blkcg = container_of(css, struct blkio_cgroup, css);
446 spin_lock_irqsave(&blkcg->lock, flags);
447 if (!hlist_unhashed(&blkg->blkcg_node)) {
448 __blkiocg_del_blkio_group(blkg);
449 ret = 0;
450 }
451 spin_unlock_irqrestore(&blkcg->lock, flags);
Vivek Goyalb1c35762009-12-03 12:59:47 -0500452 }
Jens Axboe0f3942a2010-05-03 14:28:55 +0200453
Vivek Goyalb1c35762009-12-03 12:59:47 -0500454 rcu_read_unlock();
455 return ret;
Vivek Goyal31e4c282009-12-03 12:59:42 -0500456}
Vivek Goyal9d6a9862009-12-04 10:36:41 -0500457EXPORT_SYMBOL_GPL(blkiocg_del_blkio_group);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500458
459/* called under rcu_read_lock(). */
460struct blkio_group *blkiocg_lookup_group(struct blkio_cgroup *blkcg, void *key)
461{
462 struct blkio_group *blkg;
463 struct hlist_node *n;
464 void *__key;
465
466 hlist_for_each_entry_rcu(blkg, n, &blkcg->blkg_list, blkcg_node) {
467 __key = blkg->key;
468 if (__key == key)
469 return blkg;
470 }
471
472 return NULL;
473}
Vivek Goyal9d6a9862009-12-04 10:36:41 -0500474EXPORT_SYMBOL_GPL(blkiocg_lookup_group);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500475
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700476static int
Divyesh Shah84c124d2010-04-09 08:31:19 +0200477blkiocg_reset_stats(struct cgroup *cgroup, struct cftype *cftype, u64 val)
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700478{
479 struct blkio_cgroup *blkcg;
480 struct blkio_group *blkg;
Divyesh Shah812df482010-04-08 21:15:35 -0700481 struct blkio_group_stats *stats;
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700482 struct hlist_node *n;
Divyesh Shahcdc11842010-04-08 21:15:10 -0700483 uint64_t queued[BLKIO_STAT_TOTAL];
484 int i;
Divyesh Shah812df482010-04-08 21:15:35 -0700485#ifdef CONFIG_DEBUG_BLK_CGROUP
486 bool idling, waiting, empty;
487 unsigned long long now = sched_clock();
488#endif
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700489
490 blkcg = cgroup_to_blkio_cgroup(cgroup);
491 spin_lock_irq(&blkcg->lock);
492 hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) {
493 spin_lock(&blkg->stats_lock);
Divyesh Shah812df482010-04-08 21:15:35 -0700494 stats = &blkg->stats;
495#ifdef CONFIG_DEBUG_BLK_CGROUP
496 idling = blkio_blkg_idling(stats);
497 waiting = blkio_blkg_waiting(stats);
498 empty = blkio_blkg_empty(stats);
499#endif
Divyesh Shahcdc11842010-04-08 21:15:10 -0700500 for (i = 0; i < BLKIO_STAT_TOTAL; i++)
Divyesh Shah812df482010-04-08 21:15:35 -0700501 queued[i] = stats->stat_arr[BLKIO_STAT_QUEUED][i];
502 memset(stats, 0, sizeof(struct blkio_group_stats));
Divyesh Shahcdc11842010-04-08 21:15:10 -0700503 for (i = 0; i < BLKIO_STAT_TOTAL; i++)
Divyesh Shah812df482010-04-08 21:15:35 -0700504 stats->stat_arr[BLKIO_STAT_QUEUED][i] = queued[i];
505#ifdef CONFIG_DEBUG_BLK_CGROUP
506 if (idling) {
507 blkio_mark_blkg_idling(stats);
508 stats->start_idle_time = now;
509 }
510 if (waiting) {
511 blkio_mark_blkg_waiting(stats);
512 stats->start_group_wait_time = now;
513 }
514 if (empty) {
515 blkio_mark_blkg_empty(stats);
516 stats->start_empty_time = now;
517 }
518#endif
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700519 spin_unlock(&blkg->stats_lock);
520 }
521 spin_unlock_irq(&blkcg->lock);
522 return 0;
523}
524
Divyesh Shah84c124d2010-04-09 08:31:19 +0200525static void blkio_get_key_name(enum stat_sub_type type, dev_t dev, char *str,
526 int chars_left, bool diskname_only)
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700527{
Divyesh Shah84c124d2010-04-09 08:31:19 +0200528 snprintf(str, chars_left, "%d:%d", MAJOR(dev), MINOR(dev));
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700529 chars_left -= strlen(str);
530 if (chars_left <= 0) {
531 printk(KERN_WARNING
532 "Possibly incorrect cgroup stat display format");
533 return;
534 }
Divyesh Shah84c124d2010-04-09 08:31:19 +0200535 if (diskname_only)
536 return;
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700537 switch (type) {
Divyesh Shah84c124d2010-04-09 08:31:19 +0200538 case BLKIO_STAT_READ:
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700539 strlcat(str, " Read", chars_left);
540 break;
Divyesh Shah84c124d2010-04-09 08:31:19 +0200541 case BLKIO_STAT_WRITE:
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700542 strlcat(str, " Write", chars_left);
543 break;
Divyesh Shah84c124d2010-04-09 08:31:19 +0200544 case BLKIO_STAT_SYNC:
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700545 strlcat(str, " Sync", chars_left);
546 break;
Divyesh Shah84c124d2010-04-09 08:31:19 +0200547 case BLKIO_STAT_ASYNC:
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700548 strlcat(str, " Async", chars_left);
549 break;
Divyesh Shah84c124d2010-04-09 08:31:19 +0200550 case BLKIO_STAT_TOTAL:
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700551 strlcat(str, " Total", chars_left);
552 break;
553 default:
554 strlcat(str, " Invalid", chars_left);
555 }
556}
557
Divyesh Shah84c124d2010-04-09 08:31:19 +0200558static uint64_t blkio_fill_stat(char *str, int chars_left, uint64_t val,
559 struct cgroup_map_cb *cb, dev_t dev)
560{
561 blkio_get_key_name(0, dev, str, chars_left, true);
562 cb->fill(cb, str, val);
563 return val;
564}
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700565
Divyesh Shah84c124d2010-04-09 08:31:19 +0200566/* This should be called with blkg->stats_lock held */
567static uint64_t blkio_get_stat(struct blkio_group *blkg,
568 struct cgroup_map_cb *cb, dev_t dev, enum stat_type type)
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700569{
570 uint64_t disk_total;
571 char key_str[MAX_KEY_LEN];
Divyesh Shah84c124d2010-04-09 08:31:19 +0200572 enum stat_sub_type sub_type;
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700573
Divyesh Shah84c124d2010-04-09 08:31:19 +0200574 if (type == BLKIO_STAT_TIME)
575 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
576 blkg->stats.time, cb, dev);
577 if (type == BLKIO_STAT_SECTORS)
578 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
579 blkg->stats.sectors, cb, dev);
580#ifdef CONFIG_DEBUG_BLK_CGROUP
Divyesh Shahcdc11842010-04-08 21:15:10 -0700581 if (type == BLKIO_STAT_AVG_QUEUE_SIZE) {
582 uint64_t sum = blkg->stats.avg_queue_size_sum;
583 uint64_t samples = blkg->stats.avg_queue_size_samples;
584 if (samples)
585 do_div(sum, samples);
586 else
587 sum = 0;
588 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1, sum, cb, dev);
589 }
Divyesh Shah812df482010-04-08 21:15:35 -0700590 if (type == BLKIO_STAT_GROUP_WAIT_TIME)
591 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
592 blkg->stats.group_wait_time, cb, dev);
593 if (type == BLKIO_STAT_IDLE_TIME)
594 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
595 blkg->stats.idle_time, cb, dev);
596 if (type == BLKIO_STAT_EMPTY_TIME)
597 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
598 blkg->stats.empty_time, cb, dev);
Divyesh Shah84c124d2010-04-09 08:31:19 +0200599 if (type == BLKIO_STAT_DEQUEUE)
600 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
601 blkg->stats.dequeue, cb, dev);
602#endif
603
604 for (sub_type = BLKIO_STAT_READ; sub_type < BLKIO_STAT_TOTAL;
605 sub_type++) {
606 blkio_get_key_name(sub_type, dev, key_str, MAX_KEY_LEN, false);
607 cb->fill(cb, key_str, blkg->stats.stat_arr[type][sub_type]);
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700608 }
Divyesh Shah84c124d2010-04-09 08:31:19 +0200609 disk_total = blkg->stats.stat_arr[type][BLKIO_STAT_READ] +
610 blkg->stats.stat_arr[type][BLKIO_STAT_WRITE];
611 blkio_get_key_name(BLKIO_STAT_TOTAL, dev, key_str, MAX_KEY_LEN, false);
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700612 cb->fill(cb, key_str, disk_total);
613 return disk_total;
614}
615
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800616static int blkio_check_dev_num(dev_t dev)
617{
618 int part = 0;
619 struct gendisk *disk;
620
621 disk = get_gendisk(dev, &part);
622 if (!disk || part)
623 return -ENODEV;
624
625 return 0;
626}
627
628static int blkio_policy_parse_and_set(char *buf,
Vivek Goyal062a6442010-09-15 17:06:33 -0400629 struct blkio_policy_node *newpn, enum blkio_policy_id plid, int fileid)
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800630{
631 char *s[4], *p, *major_s = NULL, *minor_s = NULL;
632 int ret;
633 unsigned long major, minor, temp;
634 int i = 0;
635 dev_t dev;
Vivek Goyal4c9eefa2010-09-15 17:06:34 -0400636 u64 bps;
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800637
638 memset(s, 0, sizeof(s));
639
640 while ((p = strsep(&buf, " ")) != NULL) {
641 if (!*p)
642 continue;
643
644 s[i++] = p;
645
646 /* Prevent from inputing too many things */
647 if (i == 3)
648 break;
649 }
650
651 if (i != 2)
652 return -EINVAL;
653
654 p = strsep(&s[0], ":");
655 if (p != NULL)
656 major_s = p;
657 else
658 return -EINVAL;
659
660 minor_s = s[0];
661 if (!minor_s)
662 return -EINVAL;
663
664 ret = strict_strtoul(major_s, 10, &major);
665 if (ret)
666 return -EINVAL;
667
668 ret = strict_strtoul(minor_s, 10, &minor);
669 if (ret)
670 return -EINVAL;
671
672 dev = MKDEV(major, minor);
673
674 ret = blkio_check_dev_num(dev);
675 if (ret)
676 return ret;
677
678 newpn->dev = dev;
679
680 if (s[1] == NULL)
681 return -EINVAL;
682
Vivek Goyal062a6442010-09-15 17:06:33 -0400683 switch (plid) {
684 case BLKIO_POLICY_PROP:
685 ret = strict_strtoul(s[1], 10, &temp);
686 if (ret || (temp < BLKIO_WEIGHT_MIN && temp > 0) ||
687 temp > BLKIO_WEIGHT_MAX)
688 return -EINVAL;
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800689
Vivek Goyal062a6442010-09-15 17:06:33 -0400690 newpn->plid = plid;
691 newpn->fileid = fileid;
Vivek Goyal4c9eefa2010-09-15 17:06:34 -0400692 newpn->val.weight = temp;
693 break;
694 case BLKIO_POLICY_THROTL:
695 ret = strict_strtoull(s[1], 10, &bps);
696 if (ret)
697 return -EINVAL;
698
699 newpn->plid = plid;
700 newpn->fileid = fileid;
701 newpn->val.bps = bps;
Vivek Goyal062a6442010-09-15 17:06:33 -0400702 break;
703 default:
704 BUG();
705 }
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800706
707 return 0;
708}
709
710unsigned int blkcg_get_weight(struct blkio_cgroup *blkcg,
711 dev_t dev)
712{
713 struct blkio_policy_node *pn;
714
Vivek Goyal062a6442010-09-15 17:06:33 -0400715 pn = blkio_policy_search_node(blkcg, dev, BLKIO_POLICY_PROP,
716 BLKIO_PROP_weight_device);
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800717 if (pn)
Vivek Goyal4c9eefa2010-09-15 17:06:34 -0400718 return pn->val.weight;
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800719 else
720 return blkcg->weight;
721}
722EXPORT_SYMBOL_GPL(blkcg_get_weight);
723
Vivek Goyal4c9eefa2010-09-15 17:06:34 -0400724uint64_t blkcg_get_read_bps(struct blkio_cgroup *blkcg, dev_t dev)
725{
726 struct blkio_policy_node *pn;
727
728 pn = blkio_policy_search_node(blkcg, dev, BLKIO_POLICY_THROTL,
729 BLKIO_THROTL_read_bps_device);
730 if (pn)
731 return pn->val.bps;
732 else
733 return -1;
734}
735
736uint64_t blkcg_get_write_bps(struct blkio_cgroup *blkcg, dev_t dev)
737{
738 struct blkio_policy_node *pn;
739 pn = blkio_policy_search_node(blkcg, dev, BLKIO_POLICY_THROTL,
740 BLKIO_THROTL_write_bps_device);
741 if (pn)
742 return pn->val.bps;
743 else
744 return -1;
745}
746
Vivek Goyal062a6442010-09-15 17:06:33 -0400747/* Checks whether user asked for deleting a policy rule */
748static bool blkio_delete_rule_command(struct blkio_policy_node *pn)
749{
750 switch(pn->plid) {
751 case BLKIO_POLICY_PROP:
Vivek Goyal4c9eefa2010-09-15 17:06:34 -0400752 if (pn->val.weight == 0)
753 return 1;
754 break;
755 case BLKIO_POLICY_THROTL:
756 if (pn->val.bps == 0)
Vivek Goyal062a6442010-09-15 17:06:33 -0400757 return 1;
758 break;
759 default:
760 BUG();
761 }
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800762
Vivek Goyal062a6442010-09-15 17:06:33 -0400763 return 0;
764}
765
766static void blkio_update_policy_rule(struct blkio_policy_node *oldpn,
767 struct blkio_policy_node *newpn)
768{
769 switch(oldpn->plid) {
770 case BLKIO_POLICY_PROP:
Vivek Goyal4c9eefa2010-09-15 17:06:34 -0400771 oldpn->val.weight = newpn->val.weight;
772 break;
773 case BLKIO_POLICY_THROTL:
774 oldpn->val.bps = newpn->val.bps;
Vivek Goyal062a6442010-09-15 17:06:33 -0400775 break;
776 default:
777 BUG();
778 }
779}
780
781/*
782 * Some rules/values in blkg have changed. Propogate those to respective
783 * policies.
784 */
785static void blkio_update_blkg_policy(struct blkio_cgroup *blkcg,
786 struct blkio_group *blkg, struct blkio_policy_node *pn)
787{
788 unsigned int weight;
Vivek Goyal4c9eefa2010-09-15 17:06:34 -0400789 u64 bps;
Vivek Goyal062a6442010-09-15 17:06:33 -0400790
791 switch(pn->plid) {
792 case BLKIO_POLICY_PROP:
Vivek Goyal4c9eefa2010-09-15 17:06:34 -0400793 weight = pn->val.weight ? pn->val.weight :
Vivek Goyal062a6442010-09-15 17:06:33 -0400794 blkcg->weight;
795 blkio_update_group_weight(blkg, weight);
796 break;
Vivek Goyal4c9eefa2010-09-15 17:06:34 -0400797 case BLKIO_POLICY_THROTL:
798 switch(pn->fileid) {
799 case BLKIO_THROTL_read_bps_device:
800 case BLKIO_THROTL_write_bps_device:
801 bps = pn->val.bps ? pn->val.bps : (-1);
802 blkio_update_group_bps(blkg, bps, pn->fileid);
803 break;
804 }
805 break;
Vivek Goyal062a6442010-09-15 17:06:33 -0400806 default:
807 BUG();
808 }
809}
810
811/*
812 * A policy node rule has been updated. Propogate this update to all the
813 * block groups which might be affected by this update.
814 */
815static void blkio_update_policy_node_blkg(struct blkio_cgroup *blkcg,
816 struct blkio_policy_node *pn)
817{
818 struct blkio_group *blkg;
819 struct hlist_node *n;
820
821 spin_lock(&blkio_list_lock);
822 spin_lock_irq(&blkcg->lock);
823
824 hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) {
825 if (pn->dev != blkg->dev || pn->plid != blkg->plid)
826 continue;
827 blkio_update_blkg_policy(blkcg, blkg, pn);
828 }
829
830 spin_unlock_irq(&blkcg->lock);
831 spin_unlock(&blkio_list_lock);
832}
833
834static int blkiocg_file_write(struct cgroup *cgrp, struct cftype *cft,
835 const char *buffer)
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800836{
837 int ret = 0;
838 char *buf;
839 struct blkio_policy_node *newpn, *pn;
840 struct blkio_cgroup *blkcg;
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800841 int keep_newpn = 0;
Vivek Goyal062a6442010-09-15 17:06:33 -0400842 enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
843 int fileid = BLKIOFILE_ATTR(cft->private);
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800844
845 buf = kstrdup(buffer, GFP_KERNEL);
846 if (!buf)
847 return -ENOMEM;
848
849 newpn = kzalloc(sizeof(*newpn), GFP_KERNEL);
850 if (!newpn) {
851 ret = -ENOMEM;
852 goto free_buf;
853 }
854
Vivek Goyal062a6442010-09-15 17:06:33 -0400855 ret = blkio_policy_parse_and_set(buf, newpn, plid, fileid);
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800856 if (ret)
857 goto free_newpn;
858
859 blkcg = cgroup_to_blkio_cgroup(cgrp);
860
861 spin_lock_irq(&blkcg->lock);
862
Vivek Goyal062a6442010-09-15 17:06:33 -0400863 pn = blkio_policy_search_node(blkcg, newpn->dev, plid, fileid);
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800864 if (!pn) {
Vivek Goyal062a6442010-09-15 17:06:33 -0400865 if (!blkio_delete_rule_command(newpn)) {
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800866 blkio_policy_insert_node(blkcg, newpn);
867 keep_newpn = 1;
868 }
869 spin_unlock_irq(&blkcg->lock);
870 goto update_io_group;
871 }
872
Vivek Goyal062a6442010-09-15 17:06:33 -0400873 if (blkio_delete_rule_command(newpn)) {
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800874 blkio_policy_delete_node(pn);
875 spin_unlock_irq(&blkcg->lock);
876 goto update_io_group;
877 }
878 spin_unlock_irq(&blkcg->lock);
879
Vivek Goyal062a6442010-09-15 17:06:33 -0400880 blkio_update_policy_rule(pn, newpn);
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800881
882update_io_group:
Vivek Goyal062a6442010-09-15 17:06:33 -0400883 blkio_update_policy_node_blkg(blkcg, newpn);
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800884
885free_newpn:
886 if (!keep_newpn)
887 kfree(newpn);
888free_buf:
889 kfree(buf);
890 return ret;
891}
892
Vivek Goyal062a6442010-09-15 17:06:33 -0400893static void
894blkio_print_policy_node(struct seq_file *m, struct blkio_policy_node *pn)
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800895{
Vivek Goyal062a6442010-09-15 17:06:33 -0400896 switch(pn->plid) {
897 case BLKIO_POLICY_PROP:
898 if (pn->fileid == BLKIO_PROP_weight_device)
899 seq_printf(m, "%u:%u\t%u\n", MAJOR(pn->dev),
Vivek Goyal4c9eefa2010-09-15 17:06:34 -0400900 MINOR(pn->dev), pn->val.weight);
901 break;
902 case BLKIO_POLICY_THROTL:
903 if (pn->fileid == BLKIO_THROTL_read_bps_device)
904 seq_printf(m, "%u:%u\t%llu\n", MAJOR(pn->dev),
905 MINOR(pn->dev), pn->val.bps);
906 else if (pn->fileid == BLKIO_THROTL_write_bps_device)
907 seq_printf(m, "%u:%u\t%llu\n", MAJOR(pn->dev),
908 MINOR(pn->dev), pn->val.bps);
909 else
910 BUG();
Vivek Goyal062a6442010-09-15 17:06:33 -0400911 break;
912 default:
913 BUG();
914 }
915}
916
917/* cgroup files which read their data from policy nodes end up here */
918static void blkio_read_policy_node_files(struct cftype *cft,
919 struct blkio_cgroup *blkcg, struct seq_file *m)
920{
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800921 struct blkio_policy_node *pn;
922
Jens Axboe0f3942a2010-05-03 14:28:55 +0200923 if (!list_empty(&blkcg->policy_list)) {
924 spin_lock_irq(&blkcg->lock);
925 list_for_each_entry(pn, &blkcg->policy_list, node) {
Vivek Goyal062a6442010-09-15 17:06:33 -0400926 if (!pn_matches_cftype(cft, pn))
927 continue;
928 blkio_print_policy_node(m, pn);
Jens Axboe0f3942a2010-05-03 14:28:55 +0200929 }
930 spin_unlock_irq(&blkcg->lock);
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800931 }
Vivek Goyal062a6442010-09-15 17:06:33 -0400932}
933
934static int blkiocg_file_read(struct cgroup *cgrp, struct cftype *cft,
935 struct seq_file *m)
936{
937 struct blkio_cgroup *blkcg;
938 enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
939 int name = BLKIOFILE_ATTR(cft->private);
940
941 blkcg = cgroup_to_blkio_cgroup(cgrp);
942
943 switch(plid) {
944 case BLKIO_POLICY_PROP:
945 switch(name) {
946 case BLKIO_PROP_weight_device:
947 blkio_read_policy_node_files(cft, blkcg, m);
948 return 0;
949 default:
950 BUG();
951 }
952 break;
Vivek Goyal4c9eefa2010-09-15 17:06:34 -0400953 case BLKIO_POLICY_THROTL:
954 switch(name){
955 case BLKIO_THROTL_read_bps_device:
956 case BLKIO_THROTL_write_bps_device:
957 blkio_read_policy_node_files(cft, blkcg, m);
958 return 0;
959 default:
960 BUG();
961 }
962 break;
Vivek Goyal062a6442010-09-15 17:06:33 -0400963 default:
964 BUG();
965 }
966
967 return 0;
968}
969
970static int blkio_read_blkg_stats(struct blkio_cgroup *blkcg,
971 struct cftype *cft, struct cgroup_map_cb *cb, enum stat_type type,
972 bool show_total)
973{
974 struct blkio_group *blkg;
975 struct hlist_node *n;
976 uint64_t cgroup_total = 0;
977
978 rcu_read_lock();
979 hlist_for_each_entry_rcu(blkg, n, &blkcg->blkg_list, blkcg_node) {
980 if (blkg->dev) {
981 if (!cftype_blkg_same_policy(cft, blkg))
982 continue;
983 spin_lock_irq(&blkg->stats_lock);
984 cgroup_total += blkio_get_stat(blkg, cb, blkg->dev,
985 type);
986 spin_unlock_irq(&blkg->stats_lock);
987 }
988 }
989 if (show_total)
990 cb->fill(cb, "Total", cgroup_total);
991 rcu_read_unlock();
992 return 0;
993}
994
995/* All map kind of cgroup file get serviced by this function */
996static int blkiocg_file_read_map(struct cgroup *cgrp, struct cftype *cft,
997 struct cgroup_map_cb *cb)
998{
999 struct blkio_cgroup *blkcg;
1000 enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
1001 int name = BLKIOFILE_ATTR(cft->private);
1002
1003 blkcg = cgroup_to_blkio_cgroup(cgrp);
1004
1005 switch(plid) {
1006 case BLKIO_POLICY_PROP:
1007 switch(name) {
1008 case BLKIO_PROP_time:
1009 return blkio_read_blkg_stats(blkcg, cft, cb,
1010 BLKIO_STAT_TIME, 0);
1011 case BLKIO_PROP_sectors:
1012 return blkio_read_blkg_stats(blkcg, cft, cb,
1013 BLKIO_STAT_SECTORS, 0);
1014 case BLKIO_PROP_io_service_bytes:
1015 return blkio_read_blkg_stats(blkcg, cft, cb,
1016 BLKIO_STAT_SERVICE_BYTES, 1);
1017 case BLKIO_PROP_io_serviced:
1018 return blkio_read_blkg_stats(blkcg, cft, cb,
1019 BLKIO_STAT_SERVICED, 1);
1020 case BLKIO_PROP_io_service_time:
1021 return blkio_read_blkg_stats(blkcg, cft, cb,
1022 BLKIO_STAT_SERVICE_TIME, 1);
1023 case BLKIO_PROP_io_wait_time:
1024 return blkio_read_blkg_stats(blkcg, cft, cb,
1025 BLKIO_STAT_WAIT_TIME, 1);
1026 case BLKIO_PROP_io_merged:
1027 return blkio_read_blkg_stats(blkcg, cft, cb,
1028 BLKIO_STAT_MERGED, 1);
1029 case BLKIO_PROP_io_queued:
1030 return blkio_read_blkg_stats(blkcg, cft, cb,
1031 BLKIO_STAT_QUEUED, 1);
1032#ifdef CONFIG_DEBUG_BLK_CGROUP
1033 case BLKIO_PROP_dequeue:
1034 return blkio_read_blkg_stats(blkcg, cft, cb,
1035 BLKIO_STAT_DEQUEUE, 0);
1036 case BLKIO_PROP_avg_queue_size:
1037 return blkio_read_blkg_stats(blkcg, cft, cb,
1038 BLKIO_STAT_AVG_QUEUE_SIZE, 0);
1039 case BLKIO_PROP_group_wait_time:
1040 return blkio_read_blkg_stats(blkcg, cft, cb,
1041 BLKIO_STAT_GROUP_WAIT_TIME, 0);
1042 case BLKIO_PROP_idle_time:
1043 return blkio_read_blkg_stats(blkcg, cft, cb,
1044 BLKIO_STAT_IDLE_TIME, 0);
1045 case BLKIO_PROP_empty_time:
1046 return blkio_read_blkg_stats(blkcg, cft, cb,
1047 BLKIO_STAT_EMPTY_TIME, 0);
1048#endif
1049 default:
1050 BUG();
1051 }
1052 break;
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001053 case BLKIO_POLICY_THROTL:
1054 switch(name){
1055 case BLKIO_THROTL_io_service_bytes:
1056 return blkio_read_blkg_stats(blkcg, cft, cb,
1057 BLKIO_STAT_SERVICE_BYTES, 1);
1058 case BLKIO_THROTL_io_serviced:
1059 return blkio_read_blkg_stats(blkcg, cft, cb,
1060 BLKIO_STAT_SERVICED, 1);
1061 default:
1062 BUG();
1063 }
1064 break;
Vivek Goyal062a6442010-09-15 17:06:33 -04001065 default:
1066 BUG();
1067 }
1068
1069 return 0;
1070}
1071
1072static int blkio_weight_write(struct blkio_cgroup *blkcg, u64 val)
1073{
1074 struct blkio_group *blkg;
1075 struct hlist_node *n;
1076 struct blkio_policy_node *pn;
1077
1078 if (val < BLKIO_WEIGHT_MIN || val > BLKIO_WEIGHT_MAX)
1079 return -EINVAL;
1080
1081 spin_lock(&blkio_list_lock);
1082 spin_lock_irq(&blkcg->lock);
1083 blkcg->weight = (unsigned int)val;
1084
1085 hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) {
1086 pn = blkio_policy_search_node(blkcg, blkg->dev,
1087 BLKIO_POLICY_PROP, BLKIO_PROP_weight_device);
1088 if (pn)
1089 continue;
1090
1091 blkio_update_group_weight(blkg, blkcg->weight);
1092 }
1093 spin_unlock_irq(&blkcg->lock);
1094 spin_unlock(&blkio_list_lock);
1095 return 0;
1096}
1097
1098static u64 blkiocg_file_read_u64 (struct cgroup *cgrp, struct cftype *cft) {
1099 struct blkio_cgroup *blkcg;
1100 enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
1101 int name = BLKIOFILE_ATTR(cft->private);
1102
1103 blkcg = cgroup_to_blkio_cgroup(cgrp);
1104
1105 switch(plid) {
1106 case BLKIO_POLICY_PROP:
1107 switch(name) {
1108 case BLKIO_PROP_weight:
1109 return (u64)blkcg->weight;
1110 }
1111 break;
1112 default:
1113 BUG();
1114 }
1115 return 0;
1116}
1117
1118static int
1119blkiocg_file_write_u64(struct cgroup *cgrp, struct cftype *cft, u64 val)
1120{
1121 struct blkio_cgroup *blkcg;
1122 enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
1123 int name = BLKIOFILE_ATTR(cft->private);
1124
1125 blkcg = cgroup_to_blkio_cgroup(cgrp);
1126
1127 switch(plid) {
1128 case BLKIO_POLICY_PROP:
1129 switch(name) {
1130 case BLKIO_PROP_weight:
1131 return blkio_weight_write(blkcg, val);
1132 }
1133 break;
1134 default:
1135 BUG();
1136 }
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001137
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001138 return 0;
1139}
1140
Vivek Goyal31e4c282009-12-03 12:59:42 -05001141struct cftype blkio_files[] = {
1142 {
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001143 .name = "weight_device",
Vivek Goyal062a6442010-09-15 17:06:33 -04001144 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1145 BLKIO_PROP_weight_device),
1146 .read_seq_string = blkiocg_file_read,
1147 .write_string = blkiocg_file_write,
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001148 .max_write_len = 256,
1149 },
1150 {
Vivek Goyal31e4c282009-12-03 12:59:42 -05001151 .name = "weight",
Vivek Goyal062a6442010-09-15 17:06:33 -04001152 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1153 BLKIO_PROP_weight),
1154 .read_u64 = blkiocg_file_read_u64,
1155 .write_u64 = blkiocg_file_write_u64,
Vivek Goyal31e4c282009-12-03 12:59:42 -05001156 },
Vivek Goyal22084192009-12-03 12:59:49 -05001157 {
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001158 .name = "throttle.read_bps_device",
1159 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1160 BLKIO_THROTL_read_bps_device),
1161 .read_seq_string = blkiocg_file_read,
1162 .write_string = blkiocg_file_write,
1163 .max_write_len = 256,
1164 },
1165
1166 {
1167 .name = "throttle.write_bps_device",
1168 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1169 BLKIO_THROTL_write_bps_device),
1170 .read_seq_string = blkiocg_file_read,
1171 .write_string = blkiocg_file_write,
1172 .max_write_len = 256,
1173 },
1174 {
Vivek Goyal22084192009-12-03 12:59:49 -05001175 .name = "time",
Vivek Goyal062a6442010-09-15 17:06:33 -04001176 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1177 BLKIO_PROP_time),
1178 .read_map = blkiocg_file_read_map,
Vivek Goyal22084192009-12-03 12:59:49 -05001179 },
1180 {
1181 .name = "sectors",
Vivek Goyal062a6442010-09-15 17:06:33 -04001182 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1183 BLKIO_PROP_sectors),
1184 .read_map = blkiocg_file_read_map,
Divyesh Shah303a3ac2010-04-01 15:01:24 -07001185 },
1186 {
1187 .name = "io_service_bytes",
Vivek Goyal062a6442010-09-15 17:06:33 -04001188 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1189 BLKIO_PROP_io_service_bytes),
1190 .read_map = blkiocg_file_read_map,
Divyesh Shah303a3ac2010-04-01 15:01:24 -07001191 },
1192 {
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001193 .name = "throttle.io_service_bytes",
1194 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1195 BLKIO_THROTL_io_service_bytes),
1196 .read_map = blkiocg_file_read_map,
1197 },
1198 {
Divyesh Shah303a3ac2010-04-01 15:01:24 -07001199 .name = "io_serviced",
Vivek Goyal062a6442010-09-15 17:06:33 -04001200 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1201 BLKIO_PROP_io_serviced),
1202 .read_map = blkiocg_file_read_map,
Divyesh Shah303a3ac2010-04-01 15:01:24 -07001203 },
1204 {
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001205 .name = "throttle.io_serviced",
1206 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1207 BLKIO_THROTL_io_serviced),
1208 .read_map = blkiocg_file_read_map,
1209 },
1210 {
Divyesh Shah303a3ac2010-04-01 15:01:24 -07001211 .name = "io_service_time",
Vivek Goyal062a6442010-09-15 17:06:33 -04001212 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1213 BLKIO_PROP_io_service_time),
1214 .read_map = blkiocg_file_read_map,
Divyesh Shah303a3ac2010-04-01 15:01:24 -07001215 },
1216 {
1217 .name = "io_wait_time",
Vivek Goyal062a6442010-09-15 17:06:33 -04001218 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1219 BLKIO_PROP_io_wait_time),
1220 .read_map = blkiocg_file_read_map,
Divyesh Shah84c124d2010-04-09 08:31:19 +02001221 },
1222 {
Divyesh Shah812d4022010-04-08 21:14:23 -07001223 .name = "io_merged",
Vivek Goyal062a6442010-09-15 17:06:33 -04001224 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1225 BLKIO_PROP_io_merged),
1226 .read_map = blkiocg_file_read_map,
Divyesh Shah812d4022010-04-08 21:14:23 -07001227 },
1228 {
Divyesh Shahcdc11842010-04-08 21:15:10 -07001229 .name = "io_queued",
Vivek Goyal062a6442010-09-15 17:06:33 -04001230 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1231 BLKIO_PROP_io_queued),
1232 .read_map = blkiocg_file_read_map,
Divyesh Shahcdc11842010-04-08 21:15:10 -07001233 },
1234 {
Divyesh Shah84c124d2010-04-09 08:31:19 +02001235 .name = "reset_stats",
1236 .write_u64 = blkiocg_reset_stats,
Vivek Goyal22084192009-12-03 12:59:49 -05001237 },
1238#ifdef CONFIG_DEBUG_BLK_CGROUP
Divyesh Shahcdc11842010-04-08 21:15:10 -07001239 {
1240 .name = "avg_queue_size",
Vivek Goyal062a6442010-09-15 17:06:33 -04001241 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1242 BLKIO_PROP_avg_queue_size),
1243 .read_map = blkiocg_file_read_map,
Divyesh Shahcdc11842010-04-08 21:15:10 -07001244 },
1245 {
Divyesh Shah812df482010-04-08 21:15:35 -07001246 .name = "group_wait_time",
Vivek Goyal062a6442010-09-15 17:06:33 -04001247 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1248 BLKIO_PROP_group_wait_time),
1249 .read_map = blkiocg_file_read_map,
Divyesh Shah812df482010-04-08 21:15:35 -07001250 },
1251 {
1252 .name = "idle_time",
Vivek Goyal062a6442010-09-15 17:06:33 -04001253 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1254 BLKIO_PROP_idle_time),
1255 .read_map = blkiocg_file_read_map,
Divyesh Shah812df482010-04-08 21:15:35 -07001256 },
1257 {
1258 .name = "empty_time",
Vivek Goyal062a6442010-09-15 17:06:33 -04001259 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1260 BLKIO_PROP_empty_time),
1261 .read_map = blkiocg_file_read_map,
Divyesh Shah812df482010-04-08 21:15:35 -07001262 },
1263 {
Vivek Goyal22084192009-12-03 12:59:49 -05001264 .name = "dequeue",
Vivek Goyal062a6442010-09-15 17:06:33 -04001265 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1266 BLKIO_PROP_dequeue),
1267 .read_map = blkiocg_file_read_map,
Divyesh Shahcdc11842010-04-08 21:15:10 -07001268 },
Vivek Goyal22084192009-12-03 12:59:49 -05001269#endif
Vivek Goyal31e4c282009-12-03 12:59:42 -05001270};
1271
1272static int blkiocg_populate(struct cgroup_subsys *subsys, struct cgroup *cgroup)
1273{
1274 return cgroup_add_files(cgroup, subsys, blkio_files,
1275 ARRAY_SIZE(blkio_files));
1276}
1277
1278static void blkiocg_destroy(struct cgroup_subsys *subsys, struct cgroup *cgroup)
1279{
1280 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgroup);
Vivek Goyalb1c35762009-12-03 12:59:47 -05001281 unsigned long flags;
1282 struct blkio_group *blkg;
1283 void *key;
Vivek Goyal3e252062009-12-04 10:36:42 -05001284 struct blkio_policy_type *blkiop;
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001285 struct blkio_policy_node *pn, *pntmp;
Vivek Goyal31e4c282009-12-03 12:59:42 -05001286
Vivek Goyalb1c35762009-12-03 12:59:47 -05001287 rcu_read_lock();
Jens Axboe0f3942a2010-05-03 14:28:55 +02001288 do {
1289 spin_lock_irqsave(&blkcg->lock, flags);
Vivek Goyalb1c35762009-12-03 12:59:47 -05001290
Jens Axboe0f3942a2010-05-03 14:28:55 +02001291 if (hlist_empty(&blkcg->blkg_list)) {
1292 spin_unlock_irqrestore(&blkcg->lock, flags);
1293 break;
1294 }
1295
1296 blkg = hlist_entry(blkcg->blkg_list.first, struct blkio_group,
1297 blkcg_node);
1298 key = rcu_dereference(blkg->key);
1299 __blkiocg_del_blkio_group(blkg);
1300
Vivek Goyalb1c35762009-12-03 12:59:47 -05001301 spin_unlock_irqrestore(&blkcg->lock, flags);
Vivek Goyalb1c35762009-12-03 12:59:47 -05001302
Jens Axboe0f3942a2010-05-03 14:28:55 +02001303 /*
1304 * This blkio_group is being unlinked as associated cgroup is
1305 * going away. Let all the IO controlling policies know about
1306 * this event. Currently this is static call to one io
1307 * controlling policy. Once we have more policies in place, we
1308 * need some dynamic registration of callback function.
1309 */
1310 spin_lock(&blkio_list_lock);
1311 list_for_each_entry(blkiop, &blkio_list, list)
1312 blkiop->ops.blkio_unlink_group_fn(key, blkg);
1313 spin_unlock(&blkio_list_lock);
1314 } while (1);
Vivek Goyalb1c35762009-12-03 12:59:47 -05001315
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001316 list_for_each_entry_safe(pn, pntmp, &blkcg->policy_list, node) {
1317 blkio_policy_delete_node(pn);
1318 kfree(pn);
1319 }
Jens Axboe0f3942a2010-05-03 14:28:55 +02001320
Vivek Goyal31e4c282009-12-03 12:59:42 -05001321 free_css_id(&blkio_subsys, &blkcg->css);
Vivek Goyalb1c35762009-12-03 12:59:47 -05001322 rcu_read_unlock();
Ben Blum67523c42010-03-10 15:22:11 -08001323 if (blkcg != &blkio_root_cgroup)
1324 kfree(blkcg);
Vivek Goyal31e4c282009-12-03 12:59:42 -05001325}
1326
1327static struct cgroup_subsys_state *
1328blkiocg_create(struct cgroup_subsys *subsys, struct cgroup *cgroup)
1329{
Li Zefan03415092010-05-07 08:57:00 +02001330 struct blkio_cgroup *blkcg;
1331 struct cgroup *parent = cgroup->parent;
Vivek Goyal31e4c282009-12-03 12:59:42 -05001332
Li Zefan03415092010-05-07 08:57:00 +02001333 if (!parent) {
Vivek Goyal31e4c282009-12-03 12:59:42 -05001334 blkcg = &blkio_root_cgroup;
1335 goto done;
1336 }
1337
1338 /* Currently we do not support hierarchy deeper than two level (0,1) */
Li Zefan03415092010-05-07 08:57:00 +02001339 if (parent != cgroup->top_cgroup)
Vivek Goyal31e4c282009-12-03 12:59:42 -05001340 return ERR_PTR(-EINVAL);
1341
1342 blkcg = kzalloc(sizeof(*blkcg), GFP_KERNEL);
1343 if (!blkcg)
1344 return ERR_PTR(-ENOMEM);
1345
1346 blkcg->weight = BLKIO_WEIGHT_DEFAULT;
1347done:
1348 spin_lock_init(&blkcg->lock);
1349 INIT_HLIST_HEAD(&blkcg->blkg_list);
1350
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001351 INIT_LIST_HEAD(&blkcg->policy_list);
Vivek Goyal31e4c282009-12-03 12:59:42 -05001352 return &blkcg->css;
1353}
1354
1355/*
1356 * We cannot support shared io contexts, as we have no mean to support
1357 * two tasks with the same ioc in two different groups without major rework
1358 * of the main cic data structures. For now we allow a task to change
1359 * its cgroup only if it's the only owner of its ioc.
1360 */
1361static int blkiocg_can_attach(struct cgroup_subsys *subsys,
1362 struct cgroup *cgroup, struct task_struct *tsk,
1363 bool threadgroup)
1364{
1365 struct io_context *ioc;
1366 int ret = 0;
1367
1368 /* task_lock() is needed to avoid races with exit_io_context() */
1369 task_lock(tsk);
1370 ioc = tsk->io_context;
1371 if (ioc && atomic_read(&ioc->nr_tasks) > 1)
1372 ret = -EINVAL;
1373 task_unlock(tsk);
1374
1375 return ret;
1376}
1377
1378static void blkiocg_attach(struct cgroup_subsys *subsys, struct cgroup *cgroup,
1379 struct cgroup *prev, struct task_struct *tsk,
1380 bool threadgroup)
1381{
1382 struct io_context *ioc;
1383
1384 task_lock(tsk);
1385 ioc = tsk->io_context;
1386 if (ioc)
1387 ioc->cgroup_changed = 1;
1388 task_unlock(tsk);
1389}
1390
Vivek Goyal3e252062009-12-04 10:36:42 -05001391void blkio_policy_register(struct blkio_policy_type *blkiop)
1392{
1393 spin_lock(&blkio_list_lock);
1394 list_add_tail(&blkiop->list, &blkio_list);
1395 spin_unlock(&blkio_list_lock);
1396}
1397EXPORT_SYMBOL_GPL(blkio_policy_register);
1398
1399void blkio_policy_unregister(struct blkio_policy_type *blkiop)
1400{
1401 spin_lock(&blkio_list_lock);
1402 list_del_init(&blkiop->list);
1403 spin_unlock(&blkio_list_lock);
1404}
1405EXPORT_SYMBOL_GPL(blkio_policy_unregister);
Ben Blum67523c42010-03-10 15:22:11 -08001406
1407static int __init init_cgroup_blkio(void)
1408{
1409 return cgroup_load_subsys(&blkio_subsys);
1410}
1411
1412static void __exit exit_cgroup_blkio(void)
1413{
1414 cgroup_unload_subsys(&blkio_subsys);
1415}
1416
1417module_init(init_cgroup_blkio);
1418module_exit(exit_cgroup_blkio);
1419MODULE_LICENSE("GPL");