blob: 34bfcefdd924c0c5e7f222c726ca5d30f05aed26 [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 Goyal70087dc2011-05-16 15:24:08 +0200117struct blkio_cgroup *task_blkio_cgroup(struct task_struct *tsk)
118{
119 return container_of(task_subsys_state(tsk, blkio_subsys_id),
120 struct blkio_cgroup, css);
121}
122EXPORT_SYMBOL_GPL(task_blkio_cgroup);
123
Vivek Goyal062a6442010-09-15 17:06:33 -0400124static inline void
125blkio_update_group_weight(struct blkio_group *blkg, unsigned int weight)
126{
127 struct blkio_policy_type *blkiop;
128
129 list_for_each_entry(blkiop, &blkio_list, list) {
130 /* If this policy does not own the blkg, do not send updates */
131 if (blkiop->plid != blkg->plid)
132 continue;
133 if (blkiop->ops.blkio_update_group_weight_fn)
Vivek Goyalfe071432010-10-01 14:49:49 +0200134 blkiop->ops.blkio_update_group_weight_fn(blkg->key,
135 blkg, weight);
Vivek Goyal062a6442010-09-15 17:06:33 -0400136 }
137}
138
Vivek Goyal4c9eefa2010-09-15 17:06:34 -0400139static inline void blkio_update_group_bps(struct blkio_group *blkg, u64 bps,
140 int fileid)
141{
142 struct blkio_policy_type *blkiop;
143
144 list_for_each_entry(blkiop, &blkio_list, list) {
145
146 /* If this policy does not own the blkg, do not send updates */
147 if (blkiop->plid != blkg->plid)
148 continue;
149
150 if (fileid == BLKIO_THROTL_read_bps_device
151 && blkiop->ops.blkio_update_group_read_bps_fn)
Vivek Goyalfe071432010-10-01 14:49:49 +0200152 blkiop->ops.blkio_update_group_read_bps_fn(blkg->key,
153 blkg, bps);
Vivek Goyal4c9eefa2010-09-15 17:06:34 -0400154
155 if (fileid == BLKIO_THROTL_write_bps_device
156 && blkiop->ops.blkio_update_group_write_bps_fn)
Vivek Goyalfe071432010-10-01 14:49:49 +0200157 blkiop->ops.blkio_update_group_write_bps_fn(blkg->key,
158 blkg, bps);
Vivek Goyal4c9eefa2010-09-15 17:06:34 -0400159 }
160}
161
Vivek Goyal7702e8f2010-09-15 17:06:36 -0400162static inline void blkio_update_group_iops(struct blkio_group *blkg,
163 unsigned int iops, int fileid)
164{
165 struct blkio_policy_type *blkiop;
166
167 list_for_each_entry(blkiop, &blkio_list, list) {
168
169 /* If this policy does not own the blkg, do not send updates */
170 if (blkiop->plid != blkg->plid)
171 continue;
172
173 if (fileid == BLKIO_THROTL_read_iops_device
174 && blkiop->ops.blkio_update_group_read_iops_fn)
Vivek Goyalfe071432010-10-01 14:49:49 +0200175 blkiop->ops.blkio_update_group_read_iops_fn(blkg->key,
176 blkg, iops);
Vivek Goyal7702e8f2010-09-15 17:06:36 -0400177
178 if (fileid == BLKIO_THROTL_write_iops_device
179 && blkiop->ops.blkio_update_group_write_iops_fn)
Vivek Goyalfe071432010-10-01 14:49:49 +0200180 blkiop->ops.blkio_update_group_write_iops_fn(blkg->key,
181 blkg,iops);
Vivek Goyal7702e8f2010-09-15 17:06:36 -0400182 }
183}
184
Divyesh Shah91952912010-04-01 15:01:41 -0700185/*
186 * Add to the appropriate stat variable depending on the request type.
187 * This should be called with the blkg->stats_lock held.
188 */
Divyesh Shah84c124d2010-04-09 08:31:19 +0200189static void blkio_add_stat(uint64_t *stat, uint64_t add, bool direction,
190 bool sync)
Divyesh Shah91952912010-04-01 15:01:41 -0700191{
Divyesh Shah84c124d2010-04-09 08:31:19 +0200192 if (direction)
193 stat[BLKIO_STAT_WRITE] += add;
Divyesh Shah91952912010-04-01 15:01:41 -0700194 else
Divyesh Shah84c124d2010-04-09 08:31:19 +0200195 stat[BLKIO_STAT_READ] += add;
196 if (sync)
197 stat[BLKIO_STAT_SYNC] += add;
Divyesh Shah91952912010-04-01 15:01:41 -0700198 else
Divyesh Shah84c124d2010-04-09 08:31:19 +0200199 stat[BLKIO_STAT_ASYNC] += add;
Divyesh Shah91952912010-04-01 15:01:41 -0700200}
201
Divyesh Shahcdc11842010-04-08 21:15:10 -0700202/*
203 * Decrements the appropriate stat variable if non-zero depending on the
204 * request type. Panics on value being zero.
205 * This should be called with the blkg->stats_lock held.
206 */
207static void blkio_check_and_dec_stat(uint64_t *stat, bool direction, bool sync)
208{
209 if (direction) {
210 BUG_ON(stat[BLKIO_STAT_WRITE] == 0);
211 stat[BLKIO_STAT_WRITE]--;
212 } else {
213 BUG_ON(stat[BLKIO_STAT_READ] == 0);
214 stat[BLKIO_STAT_READ]--;
215 }
216 if (sync) {
217 BUG_ON(stat[BLKIO_STAT_SYNC] == 0);
218 stat[BLKIO_STAT_SYNC]--;
219 } else {
220 BUG_ON(stat[BLKIO_STAT_ASYNC] == 0);
221 stat[BLKIO_STAT_ASYNC]--;
222 }
223}
224
225#ifdef CONFIG_DEBUG_BLK_CGROUP
Divyesh Shah812df482010-04-08 21:15:35 -0700226/* This should be called with the blkg->stats_lock held. */
227static void blkio_set_start_group_wait_time(struct blkio_group *blkg,
228 struct blkio_group *curr_blkg)
229{
230 if (blkio_blkg_waiting(&blkg->stats))
231 return;
232 if (blkg == curr_blkg)
233 return;
234 blkg->stats.start_group_wait_time = sched_clock();
235 blkio_mark_blkg_waiting(&blkg->stats);
236}
237
238/* This should be called with the blkg->stats_lock held. */
239static void blkio_update_group_wait_time(struct blkio_group_stats *stats)
240{
241 unsigned long long now;
242
243 if (!blkio_blkg_waiting(stats))
244 return;
245
246 now = sched_clock();
247 if (time_after64(now, stats->start_group_wait_time))
248 stats->group_wait_time += now - stats->start_group_wait_time;
249 blkio_clear_blkg_waiting(stats);
250}
251
252/* This should be called with the blkg->stats_lock held. */
253static void blkio_end_empty_time(struct blkio_group_stats *stats)
254{
255 unsigned long long now;
256
257 if (!blkio_blkg_empty(stats))
258 return;
259
260 now = sched_clock();
261 if (time_after64(now, stats->start_empty_time))
262 stats->empty_time += now - stats->start_empty_time;
263 blkio_clear_blkg_empty(stats);
264}
265
266void blkiocg_update_set_idle_time_stats(struct blkio_group *blkg)
267{
268 unsigned long flags;
269
270 spin_lock_irqsave(&blkg->stats_lock, flags);
271 BUG_ON(blkio_blkg_idling(&blkg->stats));
272 blkg->stats.start_idle_time = sched_clock();
273 blkio_mark_blkg_idling(&blkg->stats);
274 spin_unlock_irqrestore(&blkg->stats_lock, flags);
275}
276EXPORT_SYMBOL_GPL(blkiocg_update_set_idle_time_stats);
277
278void blkiocg_update_idle_time_stats(struct blkio_group *blkg)
279{
280 unsigned long flags;
281 unsigned long long now;
282 struct blkio_group_stats *stats;
283
284 spin_lock_irqsave(&blkg->stats_lock, flags);
285 stats = &blkg->stats;
286 if (blkio_blkg_idling(stats)) {
287 now = sched_clock();
288 if (time_after64(now, stats->start_idle_time))
289 stats->idle_time += now - stats->start_idle_time;
290 blkio_clear_blkg_idling(stats);
291 }
292 spin_unlock_irqrestore(&blkg->stats_lock, flags);
293}
294EXPORT_SYMBOL_GPL(blkiocg_update_idle_time_stats);
295
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200296void blkiocg_update_avg_queue_size_stats(struct blkio_group *blkg)
Divyesh Shahcdc11842010-04-08 21:15:10 -0700297{
298 unsigned long flags;
299 struct blkio_group_stats *stats;
300
301 spin_lock_irqsave(&blkg->stats_lock, flags);
302 stats = &blkg->stats;
303 stats->avg_queue_size_sum +=
304 stats->stat_arr[BLKIO_STAT_QUEUED][BLKIO_STAT_READ] +
305 stats->stat_arr[BLKIO_STAT_QUEUED][BLKIO_STAT_WRITE];
306 stats->avg_queue_size_samples++;
Divyesh Shah812df482010-04-08 21:15:35 -0700307 blkio_update_group_wait_time(stats);
Divyesh Shahcdc11842010-04-08 21:15:10 -0700308 spin_unlock_irqrestore(&blkg->stats_lock, flags);
309}
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200310EXPORT_SYMBOL_GPL(blkiocg_update_avg_queue_size_stats);
311
Vivek Goyale5ff0822010-04-26 19:25:11 +0200312void blkiocg_set_start_empty_time(struct blkio_group *blkg)
Divyesh Shah28baf442010-04-14 11:22:38 +0200313{
314 unsigned long flags;
315 struct blkio_group_stats *stats;
316
317 spin_lock_irqsave(&blkg->stats_lock, flags);
318 stats = &blkg->stats;
319
320 if (stats->stat_arr[BLKIO_STAT_QUEUED][BLKIO_STAT_READ] ||
321 stats->stat_arr[BLKIO_STAT_QUEUED][BLKIO_STAT_WRITE]) {
322 spin_unlock_irqrestore(&blkg->stats_lock, flags);
323 return;
324 }
325
326 /*
Vivek Goyale5ff0822010-04-26 19:25:11 +0200327 * group is already marked empty. This can happen if cfqq got new
328 * request in parent group and moved to this group while being added
329 * to service tree. Just ignore the event and move on.
Divyesh Shah28baf442010-04-14 11:22:38 +0200330 */
Vivek Goyale5ff0822010-04-26 19:25:11 +0200331 if(blkio_blkg_empty(stats)) {
332 spin_unlock_irqrestore(&blkg->stats_lock, flags);
333 return;
334 }
335
Divyesh Shah28baf442010-04-14 11:22:38 +0200336 stats->start_empty_time = sched_clock();
337 blkio_mark_blkg_empty(stats);
338 spin_unlock_irqrestore(&blkg->stats_lock, flags);
339}
340EXPORT_SYMBOL_GPL(blkiocg_set_start_empty_time);
341
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200342void blkiocg_update_dequeue_stats(struct blkio_group *blkg,
343 unsigned long dequeue)
344{
345 blkg->stats.dequeue += dequeue;
346}
347EXPORT_SYMBOL_GPL(blkiocg_update_dequeue_stats);
Divyesh Shah812df482010-04-08 21:15:35 -0700348#else
349static inline void blkio_set_start_group_wait_time(struct blkio_group *blkg,
350 struct blkio_group *curr_blkg) {}
351static inline void blkio_end_empty_time(struct blkio_group_stats *stats) {}
Divyesh Shahcdc11842010-04-08 21:15:10 -0700352#endif
353
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200354void blkiocg_update_io_add_stats(struct blkio_group *blkg,
Divyesh Shahcdc11842010-04-08 21:15:10 -0700355 struct blkio_group *curr_blkg, bool direction,
356 bool sync)
357{
358 unsigned long flags;
359
360 spin_lock_irqsave(&blkg->stats_lock, flags);
361 blkio_add_stat(blkg->stats.stat_arr[BLKIO_STAT_QUEUED], 1, direction,
362 sync);
Divyesh Shah812df482010-04-08 21:15:35 -0700363 blkio_end_empty_time(&blkg->stats);
364 blkio_set_start_group_wait_time(blkg, curr_blkg);
Divyesh Shahcdc11842010-04-08 21:15:10 -0700365 spin_unlock_irqrestore(&blkg->stats_lock, flags);
366}
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200367EXPORT_SYMBOL_GPL(blkiocg_update_io_add_stats);
Divyesh Shahcdc11842010-04-08 21:15:10 -0700368
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200369void blkiocg_update_io_remove_stats(struct blkio_group *blkg,
Divyesh Shahcdc11842010-04-08 21:15:10 -0700370 bool direction, bool sync)
371{
372 unsigned long flags;
373
374 spin_lock_irqsave(&blkg->stats_lock, flags);
375 blkio_check_and_dec_stat(blkg->stats.stat_arr[BLKIO_STAT_QUEUED],
376 direction, sync);
377 spin_unlock_irqrestore(&blkg->stats_lock, flags);
378}
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200379EXPORT_SYMBOL_GPL(blkiocg_update_io_remove_stats);
Divyesh Shahcdc11842010-04-08 21:15:10 -0700380
Justin TerAvest167400d2011-03-12 16:54:00 +0100381void blkiocg_update_timeslice_used(struct blkio_group *blkg, unsigned long time,
382 unsigned long unaccounted_time)
Vivek Goyal22084192009-12-03 12:59:49 -0500383{
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700384 unsigned long flags;
385
386 spin_lock_irqsave(&blkg->stats_lock, flags);
387 blkg->stats.time += time;
Vivek Goyala23e6862011-05-19 15:38:20 -0400388#ifdef CONFIG_DEBUG_BLK_CGROUP
Justin TerAvest167400d2011-03-12 16:54:00 +0100389 blkg->stats.unaccounted_time += unaccounted_time;
Vivek Goyala23e6862011-05-19 15:38:20 -0400390#endif
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700391 spin_unlock_irqrestore(&blkg->stats_lock, flags);
Vivek Goyal22084192009-12-03 12:59:49 -0500392}
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700393EXPORT_SYMBOL_GPL(blkiocg_update_timeslice_used);
Vivek Goyal22084192009-12-03 12:59:49 -0500394
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400395/*
396 * should be called under rcu read lock or queue lock to make sure blkg pointer
397 * is valid.
398 */
Divyesh Shah84c124d2010-04-09 08:31:19 +0200399void blkiocg_update_dispatch_stats(struct blkio_group *blkg,
400 uint64_t bytes, bool direction, bool sync)
Divyesh Shah91952912010-04-01 15:01:41 -0700401{
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400402 struct blkio_group_stats_cpu *stats_cpu;
Divyesh Shah91952912010-04-01 15:01:41 -0700403
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400404 stats_cpu = this_cpu_ptr(blkg->stats_cpu);
405
406 stats_cpu->sectors += bytes >> 9;
407 blkio_add_stat(stats_cpu->stat_arr_cpu[BLKIO_STAT_CPU_SERVICED],
408 1, direction, sync);
409 blkio_add_stat(stats_cpu->stat_arr_cpu[BLKIO_STAT_CPU_SERVICE_BYTES],
410 bytes, direction, sync);
Divyesh Shah91952912010-04-01 15:01:41 -0700411}
Divyesh Shah84c124d2010-04-09 08:31:19 +0200412EXPORT_SYMBOL_GPL(blkiocg_update_dispatch_stats);
Divyesh Shah91952912010-04-01 15:01:41 -0700413
Divyesh Shah84c124d2010-04-09 08:31:19 +0200414void blkiocg_update_completion_stats(struct blkio_group *blkg,
415 uint64_t start_time, uint64_t io_start_time, bool direction, bool sync)
Divyesh Shah91952912010-04-01 15:01:41 -0700416{
417 struct blkio_group_stats *stats;
418 unsigned long flags;
419 unsigned long long now = sched_clock();
420
421 spin_lock_irqsave(&blkg->stats_lock, flags);
422 stats = &blkg->stats;
Divyesh Shah84c124d2010-04-09 08:31:19 +0200423 if (time_after64(now, io_start_time))
424 blkio_add_stat(stats->stat_arr[BLKIO_STAT_SERVICE_TIME],
425 now - io_start_time, direction, sync);
426 if (time_after64(io_start_time, start_time))
427 blkio_add_stat(stats->stat_arr[BLKIO_STAT_WAIT_TIME],
428 io_start_time - start_time, direction, sync);
Divyesh Shah91952912010-04-01 15:01:41 -0700429 spin_unlock_irqrestore(&blkg->stats_lock, flags);
430}
Divyesh Shah84c124d2010-04-09 08:31:19 +0200431EXPORT_SYMBOL_GPL(blkiocg_update_completion_stats);
Divyesh Shah91952912010-04-01 15:01:41 -0700432
Divyesh Shah812d4022010-04-08 21:14:23 -0700433void blkiocg_update_io_merged_stats(struct blkio_group *blkg, bool direction,
434 bool sync)
435{
436 unsigned long flags;
437
438 spin_lock_irqsave(&blkg->stats_lock, flags);
439 blkio_add_stat(blkg->stats.stat_arr[BLKIO_STAT_MERGED], 1, direction,
440 sync);
441 spin_unlock_irqrestore(&blkg->stats_lock, flags);
442}
443EXPORT_SYMBOL_GPL(blkiocg_update_io_merged_stats);
444
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400445/*
446 * This function allocates the per cpu stats for blkio_group. Should be called
447 * from sleepable context as alloc_per_cpu() requires that.
448 */
449int blkio_alloc_blkg_stats(struct blkio_group *blkg)
450{
451 /* Allocate memory for per cpu stats */
452 blkg->stats_cpu = alloc_percpu(struct blkio_group_stats_cpu);
453 if (!blkg->stats_cpu)
454 return -ENOMEM;
455 return 0;
456}
457EXPORT_SYMBOL_GPL(blkio_alloc_blkg_stats);
458
Vivek Goyal31e4c282009-12-03 12:59:42 -0500459void blkiocg_add_blkio_group(struct blkio_cgroup *blkcg,
Vivek Goyal062a6442010-09-15 17:06:33 -0400460 struct blkio_group *blkg, void *key, dev_t dev,
461 enum blkio_policy_id plid)
Vivek Goyal31e4c282009-12-03 12:59:42 -0500462{
463 unsigned long flags;
464
465 spin_lock_irqsave(&blkcg->lock, flags);
Divyesh Shah8d2a91f2010-04-16 08:10:51 +0200466 spin_lock_init(&blkg->stats_lock);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500467 rcu_assign_pointer(blkg->key, key);
Vivek Goyalb1c35762009-12-03 12:59:47 -0500468 blkg->blkcg_id = css_id(&blkcg->css);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500469 hlist_add_head_rcu(&blkg->blkcg_node, &blkcg->blkg_list);
Vivek Goyal062a6442010-09-15 17:06:33 -0400470 blkg->plid = plid;
Vivek Goyal31e4c282009-12-03 12:59:42 -0500471 spin_unlock_irqrestore(&blkcg->lock, flags);
Vivek Goyal2868ef72009-12-03 12:59:48 -0500472 /* Need to take css reference ? */
473 cgroup_path(blkcg->css.cgroup, blkg->path, sizeof(blkg->path));
Vivek Goyal22084192009-12-03 12:59:49 -0500474 blkg->dev = dev;
Vivek Goyal31e4c282009-12-03 12:59:42 -0500475}
Vivek Goyal9d6a9862009-12-04 10:36:41 -0500476EXPORT_SYMBOL_GPL(blkiocg_add_blkio_group);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500477
Vivek Goyalb1c35762009-12-03 12:59:47 -0500478static void __blkiocg_del_blkio_group(struct blkio_group *blkg)
479{
480 hlist_del_init_rcu(&blkg->blkcg_node);
481 blkg->blkcg_id = 0;
482}
483
484/*
485 * returns 0 if blkio_group was still on cgroup list. Otherwise returns 1
486 * indicating that blk_group was unhashed by the time we got to it.
487 */
Vivek Goyal31e4c282009-12-03 12:59:42 -0500488int blkiocg_del_blkio_group(struct blkio_group *blkg)
489{
Vivek Goyalb1c35762009-12-03 12:59:47 -0500490 struct blkio_cgroup *blkcg;
491 unsigned long flags;
492 struct cgroup_subsys_state *css;
493 int ret = 1;
494
495 rcu_read_lock();
496 css = css_lookup(&blkio_subsys, blkg->blkcg_id);
Jens Axboe0f3942a2010-05-03 14:28:55 +0200497 if (css) {
498 blkcg = container_of(css, struct blkio_cgroup, css);
499 spin_lock_irqsave(&blkcg->lock, flags);
500 if (!hlist_unhashed(&blkg->blkcg_node)) {
501 __blkiocg_del_blkio_group(blkg);
502 ret = 0;
503 }
504 spin_unlock_irqrestore(&blkcg->lock, flags);
Vivek Goyalb1c35762009-12-03 12:59:47 -0500505 }
Jens Axboe0f3942a2010-05-03 14:28:55 +0200506
Vivek Goyalb1c35762009-12-03 12:59:47 -0500507 rcu_read_unlock();
508 return ret;
Vivek Goyal31e4c282009-12-03 12:59:42 -0500509}
Vivek Goyal9d6a9862009-12-04 10:36:41 -0500510EXPORT_SYMBOL_GPL(blkiocg_del_blkio_group);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500511
512/* called under rcu_read_lock(). */
513struct blkio_group *blkiocg_lookup_group(struct blkio_cgroup *blkcg, void *key)
514{
515 struct blkio_group *blkg;
516 struct hlist_node *n;
517 void *__key;
518
519 hlist_for_each_entry_rcu(blkg, n, &blkcg->blkg_list, blkcg_node) {
520 __key = blkg->key;
521 if (__key == key)
522 return blkg;
523 }
524
525 return NULL;
526}
Vivek Goyal9d6a9862009-12-04 10:36:41 -0500527EXPORT_SYMBOL_GPL(blkiocg_lookup_group);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500528
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700529static int
Divyesh Shah84c124d2010-04-09 08:31:19 +0200530blkiocg_reset_stats(struct cgroup *cgroup, struct cftype *cftype, u64 val)
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700531{
532 struct blkio_cgroup *blkcg;
533 struct blkio_group *blkg;
Divyesh Shah812df482010-04-08 21:15:35 -0700534 struct blkio_group_stats *stats;
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700535 struct hlist_node *n;
Divyesh Shahcdc11842010-04-08 21:15:10 -0700536 uint64_t queued[BLKIO_STAT_TOTAL];
537 int i;
Divyesh Shah812df482010-04-08 21:15:35 -0700538#ifdef CONFIG_DEBUG_BLK_CGROUP
539 bool idling, waiting, empty;
540 unsigned long long now = sched_clock();
541#endif
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700542
543 blkcg = cgroup_to_blkio_cgroup(cgroup);
544 spin_lock_irq(&blkcg->lock);
545 hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) {
546 spin_lock(&blkg->stats_lock);
Divyesh Shah812df482010-04-08 21:15:35 -0700547 stats = &blkg->stats;
548#ifdef CONFIG_DEBUG_BLK_CGROUP
549 idling = blkio_blkg_idling(stats);
550 waiting = blkio_blkg_waiting(stats);
551 empty = blkio_blkg_empty(stats);
552#endif
Divyesh Shahcdc11842010-04-08 21:15:10 -0700553 for (i = 0; i < BLKIO_STAT_TOTAL; i++)
Divyesh Shah812df482010-04-08 21:15:35 -0700554 queued[i] = stats->stat_arr[BLKIO_STAT_QUEUED][i];
555 memset(stats, 0, sizeof(struct blkio_group_stats));
Divyesh Shahcdc11842010-04-08 21:15:10 -0700556 for (i = 0; i < BLKIO_STAT_TOTAL; i++)
Divyesh Shah812df482010-04-08 21:15:35 -0700557 stats->stat_arr[BLKIO_STAT_QUEUED][i] = queued[i];
558#ifdef CONFIG_DEBUG_BLK_CGROUP
559 if (idling) {
560 blkio_mark_blkg_idling(stats);
561 stats->start_idle_time = now;
562 }
563 if (waiting) {
564 blkio_mark_blkg_waiting(stats);
565 stats->start_group_wait_time = now;
566 }
567 if (empty) {
568 blkio_mark_blkg_empty(stats);
569 stats->start_empty_time = now;
570 }
571#endif
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700572 spin_unlock(&blkg->stats_lock);
573 }
574 spin_unlock_irq(&blkcg->lock);
575 return 0;
576}
577
Divyesh Shah84c124d2010-04-09 08:31:19 +0200578static void blkio_get_key_name(enum stat_sub_type type, dev_t dev, char *str,
579 int chars_left, bool diskname_only)
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700580{
Divyesh Shah84c124d2010-04-09 08:31:19 +0200581 snprintf(str, chars_left, "%d:%d", MAJOR(dev), MINOR(dev));
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700582 chars_left -= strlen(str);
583 if (chars_left <= 0) {
584 printk(KERN_WARNING
585 "Possibly incorrect cgroup stat display format");
586 return;
587 }
Divyesh Shah84c124d2010-04-09 08:31:19 +0200588 if (diskname_only)
589 return;
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700590 switch (type) {
Divyesh Shah84c124d2010-04-09 08:31:19 +0200591 case BLKIO_STAT_READ:
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700592 strlcat(str, " Read", chars_left);
593 break;
Divyesh Shah84c124d2010-04-09 08:31:19 +0200594 case BLKIO_STAT_WRITE:
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700595 strlcat(str, " Write", chars_left);
596 break;
Divyesh Shah84c124d2010-04-09 08:31:19 +0200597 case BLKIO_STAT_SYNC:
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700598 strlcat(str, " Sync", chars_left);
599 break;
Divyesh Shah84c124d2010-04-09 08:31:19 +0200600 case BLKIO_STAT_ASYNC:
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700601 strlcat(str, " Async", chars_left);
602 break;
Divyesh Shah84c124d2010-04-09 08:31:19 +0200603 case BLKIO_STAT_TOTAL:
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700604 strlcat(str, " Total", chars_left);
605 break;
606 default:
607 strlcat(str, " Invalid", chars_left);
608 }
609}
610
Divyesh Shah84c124d2010-04-09 08:31:19 +0200611static uint64_t blkio_fill_stat(char *str, int chars_left, uint64_t val,
612 struct cgroup_map_cb *cb, dev_t dev)
613{
614 blkio_get_key_name(0, dev, str, chars_left, true);
615 cb->fill(cb, str, val);
616 return val;
617}
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700618
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400619
620static uint64_t blkio_read_stat_cpu(struct blkio_group *blkg,
621 enum stat_type_cpu type, enum stat_sub_type sub_type)
622{
623 int cpu;
624 struct blkio_group_stats_cpu *stats_cpu;
625 uint64_t val = 0;
626
627 for_each_possible_cpu(cpu) {
628 stats_cpu = per_cpu_ptr(blkg->stats_cpu, cpu);
629
630 if (type == BLKIO_STAT_CPU_SECTORS)
631 val += stats_cpu->sectors;
632 else
633 val += stats_cpu->stat_arr_cpu[type][sub_type];
634 }
635
636 return val;
637}
638
639static uint64_t blkio_get_stat_cpu(struct blkio_group *blkg,
640 struct cgroup_map_cb *cb, dev_t dev, enum stat_type_cpu type)
641{
642 uint64_t disk_total, val;
643 char key_str[MAX_KEY_LEN];
644 enum stat_sub_type sub_type;
645
646 if (type == BLKIO_STAT_CPU_SECTORS) {
647 val = blkio_read_stat_cpu(blkg, type, 0);
648 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1, val, cb, dev);
649 }
650
651 for (sub_type = BLKIO_STAT_READ; sub_type < BLKIO_STAT_TOTAL;
652 sub_type++) {
653 blkio_get_key_name(sub_type, dev, key_str, MAX_KEY_LEN, false);
654 val = blkio_read_stat_cpu(blkg, type, sub_type);
655 cb->fill(cb, key_str, val);
656 }
657
658 disk_total = blkio_read_stat_cpu(blkg, type, BLKIO_STAT_READ) +
659 blkio_read_stat_cpu(blkg, type, BLKIO_STAT_WRITE);
660
661 blkio_get_key_name(BLKIO_STAT_TOTAL, dev, key_str, MAX_KEY_LEN, false);
662 cb->fill(cb, key_str, disk_total);
663 return disk_total;
664}
665
Divyesh Shah84c124d2010-04-09 08:31:19 +0200666/* This should be called with blkg->stats_lock held */
667static uint64_t blkio_get_stat(struct blkio_group *blkg,
668 struct cgroup_map_cb *cb, dev_t dev, enum stat_type type)
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700669{
670 uint64_t disk_total;
671 char key_str[MAX_KEY_LEN];
Divyesh Shah84c124d2010-04-09 08:31:19 +0200672 enum stat_sub_type sub_type;
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700673
Divyesh Shah84c124d2010-04-09 08:31:19 +0200674 if (type == BLKIO_STAT_TIME)
675 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
676 blkg->stats.time, cb, dev);
Justin TerAvest9026e522011-03-22 21:26:54 +0100677#ifdef CONFIG_DEBUG_BLK_CGROUP
Justin TerAvest167400d2011-03-12 16:54:00 +0100678 if (type == BLKIO_STAT_UNACCOUNTED_TIME)
679 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
680 blkg->stats.unaccounted_time, cb, dev);
Divyesh Shahcdc11842010-04-08 21:15:10 -0700681 if (type == BLKIO_STAT_AVG_QUEUE_SIZE) {
682 uint64_t sum = blkg->stats.avg_queue_size_sum;
683 uint64_t samples = blkg->stats.avg_queue_size_samples;
684 if (samples)
685 do_div(sum, samples);
686 else
687 sum = 0;
688 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1, sum, cb, dev);
689 }
Divyesh Shah812df482010-04-08 21:15:35 -0700690 if (type == BLKIO_STAT_GROUP_WAIT_TIME)
691 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
692 blkg->stats.group_wait_time, cb, dev);
693 if (type == BLKIO_STAT_IDLE_TIME)
694 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
695 blkg->stats.idle_time, cb, dev);
696 if (type == BLKIO_STAT_EMPTY_TIME)
697 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
698 blkg->stats.empty_time, cb, dev);
Divyesh Shah84c124d2010-04-09 08:31:19 +0200699 if (type == BLKIO_STAT_DEQUEUE)
700 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
701 blkg->stats.dequeue, cb, dev);
702#endif
703
704 for (sub_type = BLKIO_STAT_READ; sub_type < BLKIO_STAT_TOTAL;
705 sub_type++) {
706 blkio_get_key_name(sub_type, dev, key_str, MAX_KEY_LEN, false);
707 cb->fill(cb, key_str, blkg->stats.stat_arr[type][sub_type]);
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700708 }
Divyesh Shah84c124d2010-04-09 08:31:19 +0200709 disk_total = blkg->stats.stat_arr[type][BLKIO_STAT_READ] +
710 blkg->stats.stat_arr[type][BLKIO_STAT_WRITE];
711 blkio_get_key_name(BLKIO_STAT_TOTAL, dev, key_str, MAX_KEY_LEN, false);
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700712 cb->fill(cb, key_str, disk_total);
713 return disk_total;
714}
715
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800716static int blkio_check_dev_num(dev_t dev)
717{
718 int part = 0;
719 struct gendisk *disk;
720
721 disk = get_gendisk(dev, &part);
722 if (!disk || part)
723 return -ENODEV;
724
725 return 0;
726}
727
728static int blkio_policy_parse_and_set(char *buf,
Vivek Goyal062a6442010-09-15 17:06:33 -0400729 struct blkio_policy_node *newpn, enum blkio_policy_id plid, int fileid)
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800730{
731 char *s[4], *p, *major_s = NULL, *minor_s = NULL;
732 int ret;
733 unsigned long major, minor, temp;
734 int i = 0;
735 dev_t dev;
Vivek Goyal9355aed2010-10-01 21:16:41 +0200736 u64 bps, iops;
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800737
738 memset(s, 0, sizeof(s));
739
740 while ((p = strsep(&buf, " ")) != NULL) {
741 if (!*p)
742 continue;
743
744 s[i++] = p;
745
746 /* Prevent from inputing too many things */
747 if (i == 3)
748 break;
749 }
750
751 if (i != 2)
752 return -EINVAL;
753
754 p = strsep(&s[0], ":");
755 if (p != NULL)
756 major_s = p;
757 else
758 return -EINVAL;
759
760 minor_s = s[0];
761 if (!minor_s)
762 return -EINVAL;
763
764 ret = strict_strtoul(major_s, 10, &major);
765 if (ret)
766 return -EINVAL;
767
768 ret = strict_strtoul(minor_s, 10, &minor);
769 if (ret)
770 return -EINVAL;
771
772 dev = MKDEV(major, minor);
773
774 ret = blkio_check_dev_num(dev);
775 if (ret)
776 return ret;
777
778 newpn->dev = dev;
779
780 if (s[1] == NULL)
781 return -EINVAL;
782
Vivek Goyal062a6442010-09-15 17:06:33 -0400783 switch (plid) {
784 case BLKIO_POLICY_PROP:
785 ret = strict_strtoul(s[1], 10, &temp);
786 if (ret || (temp < BLKIO_WEIGHT_MIN && temp > 0) ||
787 temp > BLKIO_WEIGHT_MAX)
788 return -EINVAL;
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800789
Vivek Goyal062a6442010-09-15 17:06:33 -0400790 newpn->plid = plid;
791 newpn->fileid = fileid;
Vivek Goyal4c9eefa2010-09-15 17:06:34 -0400792 newpn->val.weight = temp;
793 break;
794 case BLKIO_POLICY_THROTL:
Vivek Goyal7702e8f2010-09-15 17:06:36 -0400795 switch(fileid) {
796 case BLKIO_THROTL_read_bps_device:
797 case BLKIO_THROTL_write_bps_device:
798 ret = strict_strtoull(s[1], 10, &bps);
799 if (ret)
800 return -EINVAL;
Vivek Goyal4c9eefa2010-09-15 17:06:34 -0400801
Vivek Goyal7702e8f2010-09-15 17:06:36 -0400802 newpn->plid = plid;
803 newpn->fileid = fileid;
804 newpn->val.bps = bps;
805 break;
806 case BLKIO_THROTL_read_iops_device:
807 case BLKIO_THROTL_write_iops_device:
Vivek Goyal9355aed2010-10-01 21:16:41 +0200808 ret = strict_strtoull(s[1], 10, &iops);
Vivek Goyal7702e8f2010-09-15 17:06:36 -0400809 if (ret)
810 return -EINVAL;
811
Vivek Goyal9355aed2010-10-01 21:16:41 +0200812 if (iops > THROTL_IOPS_MAX)
813 return -EINVAL;
814
Vivek Goyal7702e8f2010-09-15 17:06:36 -0400815 newpn->plid = plid;
816 newpn->fileid = fileid;
Vivek Goyal9355aed2010-10-01 21:16:41 +0200817 newpn->val.iops = (unsigned int)iops;
Vivek Goyal7702e8f2010-09-15 17:06:36 -0400818 break;
819 }
Vivek Goyal062a6442010-09-15 17:06:33 -0400820 break;
821 default:
822 BUG();
823 }
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800824
825 return 0;
826}
827
828unsigned int blkcg_get_weight(struct blkio_cgroup *blkcg,
829 dev_t dev)
830{
831 struct blkio_policy_node *pn;
832
Vivek Goyal062a6442010-09-15 17:06:33 -0400833 pn = blkio_policy_search_node(blkcg, dev, BLKIO_POLICY_PROP,
834 BLKIO_PROP_weight_device);
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800835 if (pn)
Vivek Goyal4c9eefa2010-09-15 17:06:34 -0400836 return pn->val.weight;
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800837 else
838 return blkcg->weight;
839}
840EXPORT_SYMBOL_GPL(blkcg_get_weight);
841
Vivek Goyal4c9eefa2010-09-15 17:06:34 -0400842uint64_t blkcg_get_read_bps(struct blkio_cgroup *blkcg, dev_t dev)
843{
844 struct blkio_policy_node *pn;
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800845
Vivek Goyal4c9eefa2010-09-15 17:06:34 -0400846 pn = blkio_policy_search_node(blkcg, dev, BLKIO_POLICY_THROTL,
847 BLKIO_THROTL_read_bps_device);
848 if (pn)
849 return pn->val.bps;
850 else
851 return -1;
852}
853
854uint64_t blkcg_get_write_bps(struct blkio_cgroup *blkcg, dev_t dev)
855{
856 struct blkio_policy_node *pn;
857 pn = blkio_policy_search_node(blkcg, dev, BLKIO_POLICY_THROTL,
858 BLKIO_THROTL_write_bps_device);
859 if (pn)
860 return pn->val.bps;
861 else
862 return -1;
863}
864
Vivek Goyal7702e8f2010-09-15 17:06:36 -0400865unsigned int blkcg_get_read_iops(struct blkio_cgroup *blkcg, dev_t dev)
866{
867 struct blkio_policy_node *pn;
868
869 pn = blkio_policy_search_node(blkcg, dev, BLKIO_POLICY_THROTL,
870 BLKIO_THROTL_read_iops_device);
871 if (pn)
872 return pn->val.iops;
873 else
874 return -1;
875}
876
877unsigned int blkcg_get_write_iops(struct blkio_cgroup *blkcg, dev_t dev)
878{
879 struct blkio_policy_node *pn;
880 pn = blkio_policy_search_node(blkcg, dev, BLKIO_POLICY_THROTL,
881 BLKIO_THROTL_write_iops_device);
882 if (pn)
883 return pn->val.iops;
884 else
885 return -1;
886}
887
Vivek Goyal062a6442010-09-15 17:06:33 -0400888/* Checks whether user asked for deleting a policy rule */
889static bool blkio_delete_rule_command(struct blkio_policy_node *pn)
890{
891 switch(pn->plid) {
892 case BLKIO_POLICY_PROP:
Vivek Goyal4c9eefa2010-09-15 17:06:34 -0400893 if (pn->val.weight == 0)
894 return 1;
895 break;
896 case BLKIO_POLICY_THROTL:
Vivek Goyal7702e8f2010-09-15 17:06:36 -0400897 switch(pn->fileid) {
898 case BLKIO_THROTL_read_bps_device:
899 case BLKIO_THROTL_write_bps_device:
900 if (pn->val.bps == 0)
901 return 1;
902 break;
903 case BLKIO_THROTL_read_iops_device:
904 case BLKIO_THROTL_write_iops_device:
905 if (pn->val.iops == 0)
906 return 1;
907 }
Vivek Goyal062a6442010-09-15 17:06:33 -0400908 break;
909 default:
910 BUG();
911 }
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800912
Vivek Goyal062a6442010-09-15 17:06:33 -0400913 return 0;
914}
915
916static void blkio_update_policy_rule(struct blkio_policy_node *oldpn,
917 struct blkio_policy_node *newpn)
918{
919 switch(oldpn->plid) {
920 case BLKIO_POLICY_PROP:
Vivek Goyal4c9eefa2010-09-15 17:06:34 -0400921 oldpn->val.weight = newpn->val.weight;
922 break;
923 case BLKIO_POLICY_THROTL:
Vivek Goyal7702e8f2010-09-15 17:06:36 -0400924 switch(newpn->fileid) {
925 case BLKIO_THROTL_read_bps_device:
926 case BLKIO_THROTL_write_bps_device:
927 oldpn->val.bps = newpn->val.bps;
928 break;
929 case BLKIO_THROTL_read_iops_device:
930 case BLKIO_THROTL_write_iops_device:
931 oldpn->val.iops = newpn->val.iops;
932 }
Vivek Goyal062a6442010-09-15 17:06:33 -0400933 break;
934 default:
935 BUG();
936 }
937}
938
939/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300940 * Some rules/values in blkg have changed. Propagate those to respective
Vivek Goyal062a6442010-09-15 17:06:33 -0400941 * policies.
942 */
943static void blkio_update_blkg_policy(struct blkio_cgroup *blkcg,
944 struct blkio_group *blkg, struct blkio_policy_node *pn)
945{
Vivek Goyal7702e8f2010-09-15 17:06:36 -0400946 unsigned int weight, iops;
Vivek Goyal4c9eefa2010-09-15 17:06:34 -0400947 u64 bps;
Vivek Goyal062a6442010-09-15 17:06:33 -0400948
949 switch(pn->plid) {
950 case BLKIO_POLICY_PROP:
Vivek Goyal4c9eefa2010-09-15 17:06:34 -0400951 weight = pn->val.weight ? pn->val.weight :
Vivek Goyal062a6442010-09-15 17:06:33 -0400952 blkcg->weight;
953 blkio_update_group_weight(blkg, weight);
954 break;
Vivek Goyal4c9eefa2010-09-15 17:06:34 -0400955 case BLKIO_POLICY_THROTL:
956 switch(pn->fileid) {
957 case BLKIO_THROTL_read_bps_device:
958 case BLKIO_THROTL_write_bps_device:
959 bps = pn->val.bps ? pn->val.bps : (-1);
960 blkio_update_group_bps(blkg, bps, pn->fileid);
961 break;
Vivek Goyal7702e8f2010-09-15 17:06:36 -0400962 case BLKIO_THROTL_read_iops_device:
963 case BLKIO_THROTL_write_iops_device:
964 iops = pn->val.iops ? pn->val.iops : (-1);
965 blkio_update_group_iops(blkg, iops, pn->fileid);
966 break;
Vivek Goyal4c9eefa2010-09-15 17:06:34 -0400967 }
968 break;
Vivek Goyal062a6442010-09-15 17:06:33 -0400969 default:
970 BUG();
971 }
972}
973
974/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300975 * A policy node rule has been updated. Propagate this update to all the
Vivek Goyal062a6442010-09-15 17:06:33 -0400976 * block groups which might be affected by this update.
977 */
978static void blkio_update_policy_node_blkg(struct blkio_cgroup *blkcg,
979 struct blkio_policy_node *pn)
980{
981 struct blkio_group *blkg;
982 struct hlist_node *n;
983
984 spin_lock(&blkio_list_lock);
985 spin_lock_irq(&blkcg->lock);
986
987 hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) {
988 if (pn->dev != blkg->dev || pn->plid != blkg->plid)
989 continue;
990 blkio_update_blkg_policy(blkcg, blkg, pn);
991 }
992
993 spin_unlock_irq(&blkcg->lock);
994 spin_unlock(&blkio_list_lock);
995}
996
997static int blkiocg_file_write(struct cgroup *cgrp, struct cftype *cft,
998 const char *buffer)
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800999{
1000 int ret = 0;
1001 char *buf;
1002 struct blkio_policy_node *newpn, *pn;
1003 struct blkio_cgroup *blkcg;
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001004 int keep_newpn = 0;
Vivek Goyal062a6442010-09-15 17:06:33 -04001005 enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
1006 int fileid = BLKIOFILE_ATTR(cft->private);
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001007
1008 buf = kstrdup(buffer, GFP_KERNEL);
1009 if (!buf)
1010 return -ENOMEM;
1011
1012 newpn = kzalloc(sizeof(*newpn), GFP_KERNEL);
1013 if (!newpn) {
1014 ret = -ENOMEM;
1015 goto free_buf;
1016 }
1017
Vivek Goyal062a6442010-09-15 17:06:33 -04001018 ret = blkio_policy_parse_and_set(buf, newpn, plid, fileid);
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001019 if (ret)
1020 goto free_newpn;
1021
1022 blkcg = cgroup_to_blkio_cgroup(cgrp);
1023
1024 spin_lock_irq(&blkcg->lock);
1025
Vivek Goyal062a6442010-09-15 17:06:33 -04001026 pn = blkio_policy_search_node(blkcg, newpn->dev, plid, fileid);
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001027 if (!pn) {
Vivek Goyal062a6442010-09-15 17:06:33 -04001028 if (!blkio_delete_rule_command(newpn)) {
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001029 blkio_policy_insert_node(blkcg, newpn);
1030 keep_newpn = 1;
1031 }
1032 spin_unlock_irq(&blkcg->lock);
1033 goto update_io_group;
1034 }
1035
Vivek Goyal062a6442010-09-15 17:06:33 -04001036 if (blkio_delete_rule_command(newpn)) {
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001037 blkio_policy_delete_node(pn);
1038 spin_unlock_irq(&blkcg->lock);
1039 goto update_io_group;
1040 }
1041 spin_unlock_irq(&blkcg->lock);
1042
Vivek Goyal062a6442010-09-15 17:06:33 -04001043 blkio_update_policy_rule(pn, newpn);
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001044
1045update_io_group:
Vivek Goyal062a6442010-09-15 17:06:33 -04001046 blkio_update_policy_node_blkg(blkcg, newpn);
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001047
1048free_newpn:
1049 if (!keep_newpn)
1050 kfree(newpn);
1051free_buf:
1052 kfree(buf);
1053 return ret;
1054}
1055
Vivek Goyal062a6442010-09-15 17:06:33 -04001056static void
1057blkio_print_policy_node(struct seq_file *m, struct blkio_policy_node *pn)
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001058{
Vivek Goyal062a6442010-09-15 17:06:33 -04001059 switch(pn->plid) {
1060 case BLKIO_POLICY_PROP:
1061 if (pn->fileid == BLKIO_PROP_weight_device)
1062 seq_printf(m, "%u:%u\t%u\n", MAJOR(pn->dev),
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001063 MINOR(pn->dev), pn->val.weight);
1064 break;
1065 case BLKIO_POLICY_THROTL:
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001066 switch(pn->fileid) {
1067 case BLKIO_THROTL_read_bps_device:
1068 case BLKIO_THROTL_write_bps_device:
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001069 seq_printf(m, "%u:%u\t%llu\n", MAJOR(pn->dev),
1070 MINOR(pn->dev), pn->val.bps);
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001071 break;
1072 case BLKIO_THROTL_read_iops_device:
1073 case BLKIO_THROTL_write_iops_device:
1074 seq_printf(m, "%u:%u\t%u\n", MAJOR(pn->dev),
1075 MINOR(pn->dev), pn->val.iops);
1076 break;
1077 }
Vivek Goyal062a6442010-09-15 17:06:33 -04001078 break;
1079 default:
1080 BUG();
1081 }
1082}
1083
1084/* cgroup files which read their data from policy nodes end up here */
1085static void blkio_read_policy_node_files(struct cftype *cft,
1086 struct blkio_cgroup *blkcg, struct seq_file *m)
1087{
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001088 struct blkio_policy_node *pn;
1089
Jens Axboe0f3942a2010-05-03 14:28:55 +02001090 if (!list_empty(&blkcg->policy_list)) {
1091 spin_lock_irq(&blkcg->lock);
1092 list_for_each_entry(pn, &blkcg->policy_list, node) {
Vivek Goyal062a6442010-09-15 17:06:33 -04001093 if (!pn_matches_cftype(cft, pn))
1094 continue;
1095 blkio_print_policy_node(m, pn);
Jens Axboe0f3942a2010-05-03 14:28:55 +02001096 }
1097 spin_unlock_irq(&blkcg->lock);
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001098 }
Vivek Goyal062a6442010-09-15 17:06:33 -04001099}
1100
1101static int blkiocg_file_read(struct cgroup *cgrp, struct cftype *cft,
1102 struct seq_file *m)
1103{
1104 struct blkio_cgroup *blkcg;
1105 enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
1106 int name = BLKIOFILE_ATTR(cft->private);
1107
1108 blkcg = cgroup_to_blkio_cgroup(cgrp);
1109
1110 switch(plid) {
1111 case BLKIO_POLICY_PROP:
1112 switch(name) {
1113 case BLKIO_PROP_weight_device:
1114 blkio_read_policy_node_files(cft, blkcg, m);
1115 return 0;
1116 default:
1117 BUG();
1118 }
1119 break;
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001120 case BLKIO_POLICY_THROTL:
1121 switch(name){
1122 case BLKIO_THROTL_read_bps_device:
1123 case BLKIO_THROTL_write_bps_device:
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001124 case BLKIO_THROTL_read_iops_device:
1125 case BLKIO_THROTL_write_iops_device:
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001126 blkio_read_policy_node_files(cft, blkcg, m);
1127 return 0;
1128 default:
1129 BUG();
1130 }
1131 break;
Vivek Goyal062a6442010-09-15 17:06:33 -04001132 default:
1133 BUG();
1134 }
1135
1136 return 0;
1137}
1138
1139static int blkio_read_blkg_stats(struct blkio_cgroup *blkcg,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001140 struct cftype *cft, struct cgroup_map_cb *cb,
1141 enum stat_type type, bool show_total, bool pcpu)
Vivek Goyal062a6442010-09-15 17:06:33 -04001142{
1143 struct blkio_group *blkg;
1144 struct hlist_node *n;
1145 uint64_t cgroup_total = 0;
1146
1147 rcu_read_lock();
1148 hlist_for_each_entry_rcu(blkg, n, &blkcg->blkg_list, blkcg_node) {
1149 if (blkg->dev) {
1150 if (!cftype_blkg_same_policy(cft, blkg))
1151 continue;
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001152 if (pcpu)
1153 cgroup_total += blkio_get_stat_cpu(blkg, cb,
1154 blkg->dev, type);
1155 else {
1156 spin_lock_irq(&blkg->stats_lock);
1157 cgroup_total += blkio_get_stat(blkg, cb,
1158 blkg->dev, type);
1159 spin_unlock_irq(&blkg->stats_lock);
1160 }
Vivek Goyal062a6442010-09-15 17:06:33 -04001161 }
1162 }
1163 if (show_total)
1164 cb->fill(cb, "Total", cgroup_total);
1165 rcu_read_unlock();
1166 return 0;
1167}
1168
1169/* All map kind of cgroup file get serviced by this function */
1170static int blkiocg_file_read_map(struct cgroup *cgrp, struct cftype *cft,
1171 struct cgroup_map_cb *cb)
1172{
1173 struct blkio_cgroup *blkcg;
1174 enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
1175 int name = BLKIOFILE_ATTR(cft->private);
1176
1177 blkcg = cgroup_to_blkio_cgroup(cgrp);
1178
1179 switch(plid) {
1180 case BLKIO_POLICY_PROP:
1181 switch(name) {
1182 case BLKIO_PROP_time:
1183 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001184 BLKIO_STAT_TIME, 0, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001185 case BLKIO_PROP_sectors:
1186 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001187 BLKIO_STAT_CPU_SECTORS, 0, 1);
Vivek Goyal062a6442010-09-15 17:06:33 -04001188 case BLKIO_PROP_io_service_bytes:
1189 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001190 BLKIO_STAT_CPU_SERVICE_BYTES, 1, 1);
Vivek Goyal062a6442010-09-15 17:06:33 -04001191 case BLKIO_PROP_io_serviced:
1192 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001193 BLKIO_STAT_CPU_SERVICED, 1, 1);
Vivek Goyal062a6442010-09-15 17:06:33 -04001194 case BLKIO_PROP_io_service_time:
1195 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001196 BLKIO_STAT_SERVICE_TIME, 1, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001197 case BLKIO_PROP_io_wait_time:
1198 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001199 BLKIO_STAT_WAIT_TIME, 1, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001200 case BLKIO_PROP_io_merged:
1201 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001202 BLKIO_STAT_MERGED, 1, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001203 case BLKIO_PROP_io_queued:
1204 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001205 BLKIO_STAT_QUEUED, 1, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001206#ifdef CONFIG_DEBUG_BLK_CGROUP
Justin TerAvest9026e522011-03-22 21:26:54 +01001207 case BLKIO_PROP_unaccounted_time:
1208 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001209 BLKIO_STAT_UNACCOUNTED_TIME, 0, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001210 case BLKIO_PROP_dequeue:
1211 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001212 BLKIO_STAT_DEQUEUE, 0, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001213 case BLKIO_PROP_avg_queue_size:
1214 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001215 BLKIO_STAT_AVG_QUEUE_SIZE, 0, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001216 case BLKIO_PROP_group_wait_time:
1217 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001218 BLKIO_STAT_GROUP_WAIT_TIME, 0, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001219 case BLKIO_PROP_idle_time:
1220 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001221 BLKIO_STAT_IDLE_TIME, 0, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001222 case BLKIO_PROP_empty_time:
1223 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001224 BLKIO_STAT_EMPTY_TIME, 0, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001225#endif
1226 default:
1227 BUG();
1228 }
1229 break;
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001230 case BLKIO_POLICY_THROTL:
1231 switch(name){
1232 case BLKIO_THROTL_io_service_bytes:
1233 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001234 BLKIO_STAT_CPU_SERVICE_BYTES, 1, 1);
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001235 case BLKIO_THROTL_io_serviced:
1236 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001237 BLKIO_STAT_CPU_SERVICED, 1, 1);
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001238 default:
1239 BUG();
1240 }
1241 break;
Vivek Goyal062a6442010-09-15 17:06:33 -04001242 default:
1243 BUG();
1244 }
1245
1246 return 0;
1247}
1248
1249static int blkio_weight_write(struct blkio_cgroup *blkcg, u64 val)
1250{
1251 struct blkio_group *blkg;
1252 struct hlist_node *n;
1253 struct blkio_policy_node *pn;
1254
1255 if (val < BLKIO_WEIGHT_MIN || val > BLKIO_WEIGHT_MAX)
1256 return -EINVAL;
1257
1258 spin_lock(&blkio_list_lock);
1259 spin_lock_irq(&blkcg->lock);
1260 blkcg->weight = (unsigned int)val;
1261
1262 hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) {
1263 pn = blkio_policy_search_node(blkcg, blkg->dev,
1264 BLKIO_POLICY_PROP, BLKIO_PROP_weight_device);
1265 if (pn)
1266 continue;
1267
1268 blkio_update_group_weight(blkg, blkcg->weight);
1269 }
1270 spin_unlock_irq(&blkcg->lock);
1271 spin_unlock(&blkio_list_lock);
1272 return 0;
1273}
1274
1275static u64 blkiocg_file_read_u64 (struct cgroup *cgrp, struct cftype *cft) {
1276 struct blkio_cgroup *blkcg;
1277 enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
1278 int name = BLKIOFILE_ATTR(cft->private);
1279
1280 blkcg = cgroup_to_blkio_cgroup(cgrp);
1281
1282 switch(plid) {
1283 case BLKIO_POLICY_PROP:
1284 switch(name) {
1285 case BLKIO_PROP_weight:
1286 return (u64)blkcg->weight;
1287 }
1288 break;
1289 default:
1290 BUG();
1291 }
1292 return 0;
1293}
1294
1295static int
1296blkiocg_file_write_u64(struct cgroup *cgrp, struct cftype *cft, u64 val)
1297{
1298 struct blkio_cgroup *blkcg;
1299 enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
1300 int name = BLKIOFILE_ATTR(cft->private);
1301
1302 blkcg = cgroup_to_blkio_cgroup(cgrp);
1303
1304 switch(plid) {
1305 case BLKIO_POLICY_PROP:
1306 switch(name) {
1307 case BLKIO_PROP_weight:
1308 return blkio_weight_write(blkcg, val);
1309 }
1310 break;
1311 default:
1312 BUG();
1313 }
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001314
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001315 return 0;
1316}
1317
Vivek Goyal31e4c282009-12-03 12:59:42 -05001318struct cftype blkio_files[] = {
1319 {
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001320 .name = "weight_device",
Vivek Goyal062a6442010-09-15 17:06:33 -04001321 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1322 BLKIO_PROP_weight_device),
1323 .read_seq_string = blkiocg_file_read,
1324 .write_string = blkiocg_file_write,
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001325 .max_write_len = 256,
1326 },
1327 {
Vivek Goyal31e4c282009-12-03 12:59:42 -05001328 .name = "weight",
Vivek Goyal062a6442010-09-15 17:06:33 -04001329 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1330 BLKIO_PROP_weight),
1331 .read_u64 = blkiocg_file_read_u64,
1332 .write_u64 = blkiocg_file_write_u64,
Vivek Goyal31e4c282009-12-03 12:59:42 -05001333 },
Vivek Goyal22084192009-12-03 12:59:49 -05001334 {
1335 .name = "time",
Vivek Goyal13f98252010-10-01 14:49:41 +02001336 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1337 BLKIO_PROP_time),
1338 .read_map = blkiocg_file_read_map,
Vivek Goyal22084192009-12-03 12:59:49 -05001339 },
1340 {
1341 .name = "sectors",
Vivek Goyal13f98252010-10-01 14:49:41 +02001342 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1343 BLKIO_PROP_sectors),
1344 .read_map = blkiocg_file_read_map,
Divyesh Shah303a3ac2010-04-01 15:01:24 -07001345 },
1346 {
1347 .name = "io_service_bytes",
Vivek Goyal13f98252010-10-01 14:49:41 +02001348 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1349 BLKIO_PROP_io_service_bytes),
1350 .read_map = blkiocg_file_read_map,
Divyesh Shah303a3ac2010-04-01 15:01:24 -07001351 },
1352 {
1353 .name = "io_serviced",
Vivek Goyal13f98252010-10-01 14:49:41 +02001354 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1355 BLKIO_PROP_io_serviced),
1356 .read_map = blkiocg_file_read_map,
Divyesh Shah303a3ac2010-04-01 15:01:24 -07001357 },
1358 {
1359 .name = "io_service_time",
Vivek Goyal13f98252010-10-01 14:49:41 +02001360 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1361 BLKIO_PROP_io_service_time),
1362 .read_map = blkiocg_file_read_map,
Divyesh Shah303a3ac2010-04-01 15:01:24 -07001363 },
1364 {
1365 .name = "io_wait_time",
Vivek Goyal13f98252010-10-01 14:49:41 +02001366 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1367 BLKIO_PROP_io_wait_time),
1368 .read_map = blkiocg_file_read_map,
Divyesh Shah84c124d2010-04-09 08:31:19 +02001369 },
1370 {
Divyesh Shah812d4022010-04-08 21:14:23 -07001371 .name = "io_merged",
Vivek Goyal13f98252010-10-01 14:49:41 +02001372 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1373 BLKIO_PROP_io_merged),
1374 .read_map = blkiocg_file_read_map,
Divyesh Shah812d4022010-04-08 21:14:23 -07001375 },
1376 {
Divyesh Shahcdc11842010-04-08 21:15:10 -07001377 .name = "io_queued",
Vivek Goyal13f98252010-10-01 14:49:41 +02001378 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1379 BLKIO_PROP_io_queued),
1380 .read_map = blkiocg_file_read_map,
Divyesh Shahcdc11842010-04-08 21:15:10 -07001381 },
1382 {
Divyesh Shah84c124d2010-04-09 08:31:19 +02001383 .name = "reset_stats",
1384 .write_u64 = blkiocg_reset_stats,
Vivek Goyal22084192009-12-03 12:59:49 -05001385 },
Vivek Goyal13f98252010-10-01 14:49:41 +02001386#ifdef CONFIG_BLK_DEV_THROTTLING
1387 {
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001388 .name = "throttle.read_bps_device",
1389 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1390 BLKIO_THROTL_read_bps_device),
1391 .read_seq_string = blkiocg_file_read,
1392 .write_string = blkiocg_file_write,
1393 .max_write_len = 256,
1394 },
1395
1396 {
1397 .name = "throttle.write_bps_device",
1398 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1399 BLKIO_THROTL_write_bps_device),
1400 .read_seq_string = blkiocg_file_read,
1401 .write_string = blkiocg_file_write,
1402 .max_write_len = 256,
1403 },
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001404
1405 {
1406 .name = "throttle.read_iops_device",
1407 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1408 BLKIO_THROTL_read_iops_device),
1409 .read_seq_string = blkiocg_file_read,
1410 .write_string = blkiocg_file_write,
1411 .max_write_len = 256,
1412 },
1413
1414 {
1415 .name = "throttle.write_iops_device",
1416 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1417 BLKIO_THROTL_write_iops_device),
1418 .read_seq_string = blkiocg_file_read,
1419 .write_string = blkiocg_file_write,
1420 .max_write_len = 256,
1421 },
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001422 {
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001423 .name = "throttle.io_service_bytes",
1424 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1425 BLKIO_THROTL_io_service_bytes),
1426 .read_map = blkiocg_file_read_map,
1427 },
1428 {
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001429 .name = "throttle.io_serviced",
1430 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1431 BLKIO_THROTL_io_serviced),
1432 .read_map = blkiocg_file_read_map,
1433 },
Vivek Goyal13f98252010-10-01 14:49:41 +02001434#endif /* CONFIG_BLK_DEV_THROTTLING */
1435
Vivek Goyal22084192009-12-03 12:59:49 -05001436#ifdef CONFIG_DEBUG_BLK_CGROUP
Divyesh Shahcdc11842010-04-08 21:15:10 -07001437 {
1438 .name = "avg_queue_size",
Vivek Goyal062a6442010-09-15 17:06:33 -04001439 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1440 BLKIO_PROP_avg_queue_size),
1441 .read_map = blkiocg_file_read_map,
Divyesh Shahcdc11842010-04-08 21:15:10 -07001442 },
1443 {
Divyesh Shah812df482010-04-08 21:15:35 -07001444 .name = "group_wait_time",
Vivek Goyal062a6442010-09-15 17:06:33 -04001445 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1446 BLKIO_PROP_group_wait_time),
1447 .read_map = blkiocg_file_read_map,
Divyesh Shah812df482010-04-08 21:15:35 -07001448 },
1449 {
1450 .name = "idle_time",
Vivek Goyal062a6442010-09-15 17:06:33 -04001451 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1452 BLKIO_PROP_idle_time),
1453 .read_map = blkiocg_file_read_map,
Divyesh Shah812df482010-04-08 21:15:35 -07001454 },
1455 {
1456 .name = "empty_time",
Vivek Goyal062a6442010-09-15 17:06:33 -04001457 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1458 BLKIO_PROP_empty_time),
1459 .read_map = blkiocg_file_read_map,
Divyesh Shah812df482010-04-08 21:15:35 -07001460 },
1461 {
Vivek Goyal22084192009-12-03 12:59:49 -05001462 .name = "dequeue",
Vivek Goyal062a6442010-09-15 17:06:33 -04001463 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1464 BLKIO_PROP_dequeue),
1465 .read_map = blkiocg_file_read_map,
Divyesh Shahcdc11842010-04-08 21:15:10 -07001466 },
Justin TerAvest9026e522011-03-22 21:26:54 +01001467 {
1468 .name = "unaccounted_time",
1469 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1470 BLKIO_PROP_unaccounted_time),
1471 .read_map = blkiocg_file_read_map,
1472 },
Vivek Goyal22084192009-12-03 12:59:49 -05001473#endif
Vivek Goyal31e4c282009-12-03 12:59:42 -05001474};
1475
1476static int blkiocg_populate(struct cgroup_subsys *subsys, struct cgroup *cgroup)
1477{
1478 return cgroup_add_files(cgroup, subsys, blkio_files,
1479 ARRAY_SIZE(blkio_files));
1480}
1481
1482static void blkiocg_destroy(struct cgroup_subsys *subsys, struct cgroup *cgroup)
1483{
1484 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgroup);
Vivek Goyalb1c35762009-12-03 12:59:47 -05001485 unsigned long flags;
1486 struct blkio_group *blkg;
1487 void *key;
Vivek Goyal3e252062009-12-04 10:36:42 -05001488 struct blkio_policy_type *blkiop;
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001489 struct blkio_policy_node *pn, *pntmp;
Vivek Goyal31e4c282009-12-03 12:59:42 -05001490
Vivek Goyalb1c35762009-12-03 12:59:47 -05001491 rcu_read_lock();
Jens Axboe0f3942a2010-05-03 14:28:55 +02001492 do {
1493 spin_lock_irqsave(&blkcg->lock, flags);
Vivek Goyalb1c35762009-12-03 12:59:47 -05001494
Jens Axboe0f3942a2010-05-03 14:28:55 +02001495 if (hlist_empty(&blkcg->blkg_list)) {
1496 spin_unlock_irqrestore(&blkcg->lock, flags);
1497 break;
1498 }
1499
1500 blkg = hlist_entry(blkcg->blkg_list.first, struct blkio_group,
1501 blkcg_node);
1502 key = rcu_dereference(blkg->key);
1503 __blkiocg_del_blkio_group(blkg);
1504
Vivek Goyalb1c35762009-12-03 12:59:47 -05001505 spin_unlock_irqrestore(&blkcg->lock, flags);
Vivek Goyalb1c35762009-12-03 12:59:47 -05001506
Jens Axboe0f3942a2010-05-03 14:28:55 +02001507 /*
1508 * This blkio_group is being unlinked as associated cgroup is
1509 * going away. Let all the IO controlling policies know about
Vivek Goyal61014e92010-10-01 14:49:44 +02001510 * this event.
Jens Axboe0f3942a2010-05-03 14:28:55 +02001511 */
1512 spin_lock(&blkio_list_lock);
Vivek Goyal61014e92010-10-01 14:49:44 +02001513 list_for_each_entry(blkiop, &blkio_list, list) {
1514 if (blkiop->plid != blkg->plid)
1515 continue;
Jens Axboe0f3942a2010-05-03 14:28:55 +02001516 blkiop->ops.blkio_unlink_group_fn(key, blkg);
Vivek Goyal61014e92010-10-01 14:49:44 +02001517 }
Jens Axboe0f3942a2010-05-03 14:28:55 +02001518 spin_unlock(&blkio_list_lock);
1519 } while (1);
Vivek Goyalb1c35762009-12-03 12:59:47 -05001520
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001521 list_for_each_entry_safe(pn, pntmp, &blkcg->policy_list, node) {
1522 blkio_policy_delete_node(pn);
1523 kfree(pn);
1524 }
Jens Axboe0f3942a2010-05-03 14:28:55 +02001525
Vivek Goyal31e4c282009-12-03 12:59:42 -05001526 free_css_id(&blkio_subsys, &blkcg->css);
Vivek Goyalb1c35762009-12-03 12:59:47 -05001527 rcu_read_unlock();
Ben Blum67523c42010-03-10 15:22:11 -08001528 if (blkcg != &blkio_root_cgroup)
1529 kfree(blkcg);
Vivek Goyal31e4c282009-12-03 12:59:42 -05001530}
1531
1532static struct cgroup_subsys_state *
1533blkiocg_create(struct cgroup_subsys *subsys, struct cgroup *cgroup)
1534{
Li Zefan03415092010-05-07 08:57:00 +02001535 struct blkio_cgroup *blkcg;
1536 struct cgroup *parent = cgroup->parent;
Vivek Goyal31e4c282009-12-03 12:59:42 -05001537
Li Zefan03415092010-05-07 08:57:00 +02001538 if (!parent) {
Vivek Goyal31e4c282009-12-03 12:59:42 -05001539 blkcg = &blkio_root_cgroup;
1540 goto done;
1541 }
1542
Vivek Goyal31e4c282009-12-03 12:59:42 -05001543 blkcg = kzalloc(sizeof(*blkcg), GFP_KERNEL);
1544 if (!blkcg)
1545 return ERR_PTR(-ENOMEM);
1546
1547 blkcg->weight = BLKIO_WEIGHT_DEFAULT;
1548done:
1549 spin_lock_init(&blkcg->lock);
1550 INIT_HLIST_HEAD(&blkcg->blkg_list);
1551
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001552 INIT_LIST_HEAD(&blkcg->policy_list);
Vivek Goyal31e4c282009-12-03 12:59:42 -05001553 return &blkcg->css;
1554}
1555
1556/*
1557 * We cannot support shared io contexts, as we have no mean to support
1558 * two tasks with the same ioc in two different groups without major rework
1559 * of the main cic data structures. For now we allow a task to change
1560 * its cgroup only if it's the only owner of its ioc.
1561 */
1562static int blkiocg_can_attach(struct cgroup_subsys *subsys,
1563 struct cgroup *cgroup, struct task_struct *tsk,
1564 bool threadgroup)
1565{
1566 struct io_context *ioc;
1567 int ret = 0;
1568
1569 /* task_lock() is needed to avoid races with exit_io_context() */
1570 task_lock(tsk);
1571 ioc = tsk->io_context;
1572 if (ioc && atomic_read(&ioc->nr_tasks) > 1)
1573 ret = -EINVAL;
1574 task_unlock(tsk);
1575
1576 return ret;
1577}
1578
1579static void blkiocg_attach(struct cgroup_subsys *subsys, struct cgroup *cgroup,
1580 struct cgroup *prev, struct task_struct *tsk,
1581 bool threadgroup)
1582{
1583 struct io_context *ioc;
1584
1585 task_lock(tsk);
1586 ioc = tsk->io_context;
1587 if (ioc)
1588 ioc->cgroup_changed = 1;
1589 task_unlock(tsk);
1590}
1591
Vivek Goyal3e252062009-12-04 10:36:42 -05001592void blkio_policy_register(struct blkio_policy_type *blkiop)
1593{
1594 spin_lock(&blkio_list_lock);
1595 list_add_tail(&blkiop->list, &blkio_list);
1596 spin_unlock(&blkio_list_lock);
1597}
1598EXPORT_SYMBOL_GPL(blkio_policy_register);
1599
1600void blkio_policy_unregister(struct blkio_policy_type *blkiop)
1601{
1602 spin_lock(&blkio_list_lock);
1603 list_del_init(&blkiop->list);
1604 spin_unlock(&blkio_list_lock);
1605}
1606EXPORT_SYMBOL_GPL(blkio_policy_unregister);
Ben Blum67523c42010-03-10 15:22:11 -08001607
1608static int __init init_cgroup_blkio(void)
1609{
1610 return cgroup_load_subsys(&blkio_subsys);
1611}
1612
1613static void __exit exit_cgroup_blkio(void)
1614{
1615 cgroup_unload_subsys(&blkio_subsys);
1616}
1617
1618module_init(init_cgroup_blkio);
1619module_exit(exit_cgroup_blkio);
1620MODULE_LICENSE("GPL");