blob: 3622518e1c23d998e97685480951b74a5af1778c [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;
Vivek Goyal575969a2011-05-19 15:38:29 -0400403 unsigned long flags;
404
405 /*
406 * Disabling interrupts to provide mutual exclusion between two
407 * writes on same cpu. It probably is not needed for 64bit. Not
408 * optimizing that case yet.
409 */
410 local_irq_save(flags);
Divyesh Shah91952912010-04-01 15:01:41 -0700411
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400412 stats_cpu = this_cpu_ptr(blkg->stats_cpu);
413
Vivek Goyal575969a2011-05-19 15:38:29 -0400414 u64_stats_update_begin(&stats_cpu->syncp);
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400415 stats_cpu->sectors += bytes >> 9;
416 blkio_add_stat(stats_cpu->stat_arr_cpu[BLKIO_STAT_CPU_SERVICED],
417 1, direction, sync);
418 blkio_add_stat(stats_cpu->stat_arr_cpu[BLKIO_STAT_CPU_SERVICE_BYTES],
419 bytes, direction, sync);
Vivek Goyal575969a2011-05-19 15:38:29 -0400420 u64_stats_update_end(&stats_cpu->syncp);
421 local_irq_restore(flags);
Divyesh Shah91952912010-04-01 15:01:41 -0700422}
Divyesh Shah84c124d2010-04-09 08:31:19 +0200423EXPORT_SYMBOL_GPL(blkiocg_update_dispatch_stats);
Divyesh Shah91952912010-04-01 15:01:41 -0700424
Divyesh Shah84c124d2010-04-09 08:31:19 +0200425void blkiocg_update_completion_stats(struct blkio_group *blkg,
426 uint64_t start_time, uint64_t io_start_time, bool direction, bool sync)
Divyesh Shah91952912010-04-01 15:01:41 -0700427{
428 struct blkio_group_stats *stats;
429 unsigned long flags;
430 unsigned long long now = sched_clock();
431
432 spin_lock_irqsave(&blkg->stats_lock, flags);
433 stats = &blkg->stats;
Divyesh Shah84c124d2010-04-09 08:31:19 +0200434 if (time_after64(now, io_start_time))
435 blkio_add_stat(stats->stat_arr[BLKIO_STAT_SERVICE_TIME],
436 now - io_start_time, direction, sync);
437 if (time_after64(io_start_time, start_time))
438 blkio_add_stat(stats->stat_arr[BLKIO_STAT_WAIT_TIME],
439 io_start_time - start_time, direction, sync);
Divyesh Shah91952912010-04-01 15:01:41 -0700440 spin_unlock_irqrestore(&blkg->stats_lock, flags);
441}
Divyesh Shah84c124d2010-04-09 08:31:19 +0200442EXPORT_SYMBOL_GPL(blkiocg_update_completion_stats);
Divyesh Shah91952912010-04-01 15:01:41 -0700443
Divyesh Shah812d4022010-04-08 21:14:23 -0700444void blkiocg_update_io_merged_stats(struct blkio_group *blkg, bool direction,
445 bool sync)
446{
447 unsigned long flags;
448
449 spin_lock_irqsave(&blkg->stats_lock, flags);
450 blkio_add_stat(blkg->stats.stat_arr[BLKIO_STAT_MERGED], 1, direction,
451 sync);
452 spin_unlock_irqrestore(&blkg->stats_lock, flags);
453}
454EXPORT_SYMBOL_GPL(blkiocg_update_io_merged_stats);
455
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400456/*
457 * This function allocates the per cpu stats for blkio_group. Should be called
458 * from sleepable context as alloc_per_cpu() requires that.
459 */
460int blkio_alloc_blkg_stats(struct blkio_group *blkg)
461{
462 /* Allocate memory for per cpu stats */
463 blkg->stats_cpu = alloc_percpu(struct blkio_group_stats_cpu);
464 if (!blkg->stats_cpu)
465 return -ENOMEM;
466 return 0;
467}
468EXPORT_SYMBOL_GPL(blkio_alloc_blkg_stats);
469
Vivek Goyal31e4c282009-12-03 12:59:42 -0500470void blkiocg_add_blkio_group(struct blkio_cgroup *blkcg,
Vivek Goyal062a6442010-09-15 17:06:33 -0400471 struct blkio_group *blkg, void *key, dev_t dev,
472 enum blkio_policy_id plid)
Vivek Goyal31e4c282009-12-03 12:59:42 -0500473{
474 unsigned long flags;
475
476 spin_lock_irqsave(&blkcg->lock, flags);
Divyesh Shah8d2a91f2010-04-16 08:10:51 +0200477 spin_lock_init(&blkg->stats_lock);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500478 rcu_assign_pointer(blkg->key, key);
Vivek Goyalb1c35762009-12-03 12:59:47 -0500479 blkg->blkcg_id = css_id(&blkcg->css);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500480 hlist_add_head_rcu(&blkg->blkcg_node, &blkcg->blkg_list);
Vivek Goyal062a6442010-09-15 17:06:33 -0400481 blkg->plid = plid;
Vivek Goyal31e4c282009-12-03 12:59:42 -0500482 spin_unlock_irqrestore(&blkcg->lock, flags);
Vivek Goyal2868ef72009-12-03 12:59:48 -0500483 /* Need to take css reference ? */
484 cgroup_path(blkcg->css.cgroup, blkg->path, sizeof(blkg->path));
Vivek Goyal22084192009-12-03 12:59:49 -0500485 blkg->dev = dev;
Vivek Goyal31e4c282009-12-03 12:59:42 -0500486}
Vivek Goyal9d6a9862009-12-04 10:36:41 -0500487EXPORT_SYMBOL_GPL(blkiocg_add_blkio_group);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500488
Vivek Goyalb1c35762009-12-03 12:59:47 -0500489static void __blkiocg_del_blkio_group(struct blkio_group *blkg)
490{
491 hlist_del_init_rcu(&blkg->blkcg_node);
492 blkg->blkcg_id = 0;
493}
494
495/*
496 * returns 0 if blkio_group was still on cgroup list. Otherwise returns 1
497 * indicating that blk_group was unhashed by the time we got to it.
498 */
Vivek Goyal31e4c282009-12-03 12:59:42 -0500499int blkiocg_del_blkio_group(struct blkio_group *blkg)
500{
Vivek Goyalb1c35762009-12-03 12:59:47 -0500501 struct blkio_cgroup *blkcg;
502 unsigned long flags;
503 struct cgroup_subsys_state *css;
504 int ret = 1;
505
506 rcu_read_lock();
507 css = css_lookup(&blkio_subsys, blkg->blkcg_id);
Jens Axboe0f3942a2010-05-03 14:28:55 +0200508 if (css) {
509 blkcg = container_of(css, struct blkio_cgroup, css);
510 spin_lock_irqsave(&blkcg->lock, flags);
511 if (!hlist_unhashed(&blkg->blkcg_node)) {
512 __blkiocg_del_blkio_group(blkg);
513 ret = 0;
514 }
515 spin_unlock_irqrestore(&blkcg->lock, flags);
Vivek Goyalb1c35762009-12-03 12:59:47 -0500516 }
Jens Axboe0f3942a2010-05-03 14:28:55 +0200517
Vivek Goyalb1c35762009-12-03 12:59:47 -0500518 rcu_read_unlock();
519 return ret;
Vivek Goyal31e4c282009-12-03 12:59:42 -0500520}
Vivek Goyal9d6a9862009-12-04 10:36:41 -0500521EXPORT_SYMBOL_GPL(blkiocg_del_blkio_group);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500522
523/* called under rcu_read_lock(). */
524struct blkio_group *blkiocg_lookup_group(struct blkio_cgroup *blkcg, void *key)
525{
526 struct blkio_group *blkg;
527 struct hlist_node *n;
528 void *__key;
529
530 hlist_for_each_entry_rcu(blkg, n, &blkcg->blkg_list, blkcg_node) {
531 __key = blkg->key;
532 if (__key == key)
533 return blkg;
534 }
535
536 return NULL;
537}
Vivek Goyal9d6a9862009-12-04 10:36:41 -0500538EXPORT_SYMBOL_GPL(blkiocg_lookup_group);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500539
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700540static int
Divyesh Shah84c124d2010-04-09 08:31:19 +0200541blkiocg_reset_stats(struct cgroup *cgroup, struct cftype *cftype, u64 val)
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700542{
543 struct blkio_cgroup *blkcg;
544 struct blkio_group *blkg;
Divyesh Shah812df482010-04-08 21:15:35 -0700545 struct blkio_group_stats *stats;
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700546 struct hlist_node *n;
Divyesh Shahcdc11842010-04-08 21:15:10 -0700547 uint64_t queued[BLKIO_STAT_TOTAL];
548 int i;
Divyesh Shah812df482010-04-08 21:15:35 -0700549#ifdef CONFIG_DEBUG_BLK_CGROUP
550 bool idling, waiting, empty;
551 unsigned long long now = sched_clock();
552#endif
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700553
554 blkcg = cgroup_to_blkio_cgroup(cgroup);
555 spin_lock_irq(&blkcg->lock);
556 hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) {
557 spin_lock(&blkg->stats_lock);
Divyesh Shah812df482010-04-08 21:15:35 -0700558 stats = &blkg->stats;
559#ifdef CONFIG_DEBUG_BLK_CGROUP
560 idling = blkio_blkg_idling(stats);
561 waiting = blkio_blkg_waiting(stats);
562 empty = blkio_blkg_empty(stats);
563#endif
Divyesh Shahcdc11842010-04-08 21:15:10 -0700564 for (i = 0; i < BLKIO_STAT_TOTAL; i++)
Divyesh Shah812df482010-04-08 21:15:35 -0700565 queued[i] = stats->stat_arr[BLKIO_STAT_QUEUED][i];
566 memset(stats, 0, sizeof(struct blkio_group_stats));
Divyesh Shahcdc11842010-04-08 21:15:10 -0700567 for (i = 0; i < BLKIO_STAT_TOTAL; i++)
Divyesh Shah812df482010-04-08 21:15:35 -0700568 stats->stat_arr[BLKIO_STAT_QUEUED][i] = queued[i];
569#ifdef CONFIG_DEBUG_BLK_CGROUP
570 if (idling) {
571 blkio_mark_blkg_idling(stats);
572 stats->start_idle_time = now;
573 }
574 if (waiting) {
575 blkio_mark_blkg_waiting(stats);
576 stats->start_group_wait_time = now;
577 }
578 if (empty) {
579 blkio_mark_blkg_empty(stats);
580 stats->start_empty_time = now;
581 }
582#endif
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700583 spin_unlock(&blkg->stats_lock);
584 }
585 spin_unlock_irq(&blkcg->lock);
586 return 0;
587}
588
Divyesh Shah84c124d2010-04-09 08:31:19 +0200589static void blkio_get_key_name(enum stat_sub_type type, dev_t dev, char *str,
590 int chars_left, bool diskname_only)
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700591{
Divyesh Shah84c124d2010-04-09 08:31:19 +0200592 snprintf(str, chars_left, "%d:%d", MAJOR(dev), MINOR(dev));
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700593 chars_left -= strlen(str);
594 if (chars_left <= 0) {
595 printk(KERN_WARNING
596 "Possibly incorrect cgroup stat display format");
597 return;
598 }
Divyesh Shah84c124d2010-04-09 08:31:19 +0200599 if (diskname_only)
600 return;
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700601 switch (type) {
Divyesh Shah84c124d2010-04-09 08:31:19 +0200602 case BLKIO_STAT_READ:
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700603 strlcat(str, " Read", chars_left);
604 break;
Divyesh Shah84c124d2010-04-09 08:31:19 +0200605 case BLKIO_STAT_WRITE:
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700606 strlcat(str, " Write", chars_left);
607 break;
Divyesh Shah84c124d2010-04-09 08:31:19 +0200608 case BLKIO_STAT_SYNC:
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700609 strlcat(str, " Sync", chars_left);
610 break;
Divyesh Shah84c124d2010-04-09 08:31:19 +0200611 case BLKIO_STAT_ASYNC:
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700612 strlcat(str, " Async", chars_left);
613 break;
Divyesh Shah84c124d2010-04-09 08:31:19 +0200614 case BLKIO_STAT_TOTAL:
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700615 strlcat(str, " Total", chars_left);
616 break;
617 default:
618 strlcat(str, " Invalid", chars_left);
619 }
620}
621
Divyesh Shah84c124d2010-04-09 08:31:19 +0200622static uint64_t blkio_fill_stat(char *str, int chars_left, uint64_t val,
623 struct cgroup_map_cb *cb, dev_t dev)
624{
625 blkio_get_key_name(0, dev, str, chars_left, true);
626 cb->fill(cb, str, val);
627 return val;
628}
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700629
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400630
631static uint64_t blkio_read_stat_cpu(struct blkio_group *blkg,
632 enum stat_type_cpu type, enum stat_sub_type sub_type)
633{
634 int cpu;
635 struct blkio_group_stats_cpu *stats_cpu;
Vivek Goyal575969a2011-05-19 15:38:29 -0400636 u64 val = 0, tval;
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400637
638 for_each_possible_cpu(cpu) {
Vivek Goyal575969a2011-05-19 15:38:29 -0400639 unsigned int start;
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400640 stats_cpu = per_cpu_ptr(blkg->stats_cpu, cpu);
641
Vivek Goyal575969a2011-05-19 15:38:29 -0400642 do {
643 start = u64_stats_fetch_begin(&stats_cpu->syncp);
644 if (type == BLKIO_STAT_CPU_SECTORS)
645 tval = stats_cpu->sectors;
646 else
647 tval = stats_cpu->stat_arr_cpu[type][sub_type];
648 } while(u64_stats_fetch_retry(&stats_cpu->syncp, start));
649
650 val += tval;
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400651 }
652
653 return val;
654}
655
656static uint64_t blkio_get_stat_cpu(struct blkio_group *blkg,
657 struct cgroup_map_cb *cb, dev_t dev, enum stat_type_cpu type)
658{
659 uint64_t disk_total, val;
660 char key_str[MAX_KEY_LEN];
661 enum stat_sub_type sub_type;
662
663 if (type == BLKIO_STAT_CPU_SECTORS) {
664 val = blkio_read_stat_cpu(blkg, type, 0);
665 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1, val, cb, dev);
666 }
667
668 for (sub_type = BLKIO_STAT_READ; sub_type < BLKIO_STAT_TOTAL;
669 sub_type++) {
670 blkio_get_key_name(sub_type, dev, key_str, MAX_KEY_LEN, false);
671 val = blkio_read_stat_cpu(blkg, type, sub_type);
672 cb->fill(cb, key_str, val);
673 }
674
675 disk_total = blkio_read_stat_cpu(blkg, type, BLKIO_STAT_READ) +
676 blkio_read_stat_cpu(blkg, type, BLKIO_STAT_WRITE);
677
678 blkio_get_key_name(BLKIO_STAT_TOTAL, dev, key_str, MAX_KEY_LEN, false);
679 cb->fill(cb, key_str, disk_total);
680 return disk_total;
681}
682
Divyesh Shah84c124d2010-04-09 08:31:19 +0200683/* This should be called with blkg->stats_lock held */
684static uint64_t blkio_get_stat(struct blkio_group *blkg,
685 struct cgroup_map_cb *cb, dev_t dev, enum stat_type type)
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700686{
687 uint64_t disk_total;
688 char key_str[MAX_KEY_LEN];
Divyesh Shah84c124d2010-04-09 08:31:19 +0200689 enum stat_sub_type sub_type;
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700690
Divyesh Shah84c124d2010-04-09 08:31:19 +0200691 if (type == BLKIO_STAT_TIME)
692 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
693 blkg->stats.time, cb, dev);
Justin TerAvest9026e522011-03-22 21:26:54 +0100694#ifdef CONFIG_DEBUG_BLK_CGROUP
Justin TerAvest167400d2011-03-12 16:54:00 +0100695 if (type == BLKIO_STAT_UNACCOUNTED_TIME)
696 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
697 blkg->stats.unaccounted_time, cb, dev);
Divyesh Shahcdc11842010-04-08 21:15:10 -0700698 if (type == BLKIO_STAT_AVG_QUEUE_SIZE) {
699 uint64_t sum = blkg->stats.avg_queue_size_sum;
700 uint64_t samples = blkg->stats.avg_queue_size_samples;
701 if (samples)
702 do_div(sum, samples);
703 else
704 sum = 0;
705 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1, sum, cb, dev);
706 }
Divyesh Shah812df482010-04-08 21:15:35 -0700707 if (type == BLKIO_STAT_GROUP_WAIT_TIME)
708 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
709 blkg->stats.group_wait_time, cb, dev);
710 if (type == BLKIO_STAT_IDLE_TIME)
711 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
712 blkg->stats.idle_time, cb, dev);
713 if (type == BLKIO_STAT_EMPTY_TIME)
714 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
715 blkg->stats.empty_time, cb, dev);
Divyesh Shah84c124d2010-04-09 08:31:19 +0200716 if (type == BLKIO_STAT_DEQUEUE)
717 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
718 blkg->stats.dequeue, cb, dev);
719#endif
720
721 for (sub_type = BLKIO_STAT_READ; sub_type < BLKIO_STAT_TOTAL;
722 sub_type++) {
723 blkio_get_key_name(sub_type, dev, key_str, MAX_KEY_LEN, false);
724 cb->fill(cb, key_str, blkg->stats.stat_arr[type][sub_type]);
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700725 }
Divyesh Shah84c124d2010-04-09 08:31:19 +0200726 disk_total = blkg->stats.stat_arr[type][BLKIO_STAT_READ] +
727 blkg->stats.stat_arr[type][BLKIO_STAT_WRITE];
728 blkio_get_key_name(BLKIO_STAT_TOTAL, dev, key_str, MAX_KEY_LEN, false);
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700729 cb->fill(cb, key_str, disk_total);
730 return disk_total;
731}
732
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800733static int blkio_check_dev_num(dev_t dev)
734{
735 int part = 0;
736 struct gendisk *disk;
737
738 disk = get_gendisk(dev, &part);
739 if (!disk || part)
740 return -ENODEV;
741
742 return 0;
743}
744
745static int blkio_policy_parse_and_set(char *buf,
Vivek Goyal062a6442010-09-15 17:06:33 -0400746 struct blkio_policy_node *newpn, enum blkio_policy_id plid, int fileid)
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800747{
748 char *s[4], *p, *major_s = NULL, *minor_s = NULL;
749 int ret;
750 unsigned long major, minor, temp;
751 int i = 0;
752 dev_t dev;
Vivek Goyal9355aed2010-10-01 21:16:41 +0200753 u64 bps, iops;
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800754
755 memset(s, 0, sizeof(s));
756
757 while ((p = strsep(&buf, " ")) != NULL) {
758 if (!*p)
759 continue;
760
761 s[i++] = p;
762
763 /* Prevent from inputing too many things */
764 if (i == 3)
765 break;
766 }
767
768 if (i != 2)
769 return -EINVAL;
770
771 p = strsep(&s[0], ":");
772 if (p != NULL)
773 major_s = p;
774 else
775 return -EINVAL;
776
777 minor_s = s[0];
778 if (!minor_s)
779 return -EINVAL;
780
781 ret = strict_strtoul(major_s, 10, &major);
782 if (ret)
783 return -EINVAL;
784
785 ret = strict_strtoul(minor_s, 10, &minor);
786 if (ret)
787 return -EINVAL;
788
789 dev = MKDEV(major, minor);
790
791 ret = blkio_check_dev_num(dev);
792 if (ret)
793 return ret;
794
795 newpn->dev = dev;
796
797 if (s[1] == NULL)
798 return -EINVAL;
799
Vivek Goyal062a6442010-09-15 17:06:33 -0400800 switch (plid) {
801 case BLKIO_POLICY_PROP:
802 ret = strict_strtoul(s[1], 10, &temp);
803 if (ret || (temp < BLKIO_WEIGHT_MIN && temp > 0) ||
804 temp > BLKIO_WEIGHT_MAX)
805 return -EINVAL;
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800806
Vivek Goyal062a6442010-09-15 17:06:33 -0400807 newpn->plid = plid;
808 newpn->fileid = fileid;
Vivek Goyal4c9eefa2010-09-15 17:06:34 -0400809 newpn->val.weight = temp;
810 break;
811 case BLKIO_POLICY_THROTL:
Vivek Goyal7702e8f2010-09-15 17:06:36 -0400812 switch(fileid) {
813 case BLKIO_THROTL_read_bps_device:
814 case BLKIO_THROTL_write_bps_device:
815 ret = strict_strtoull(s[1], 10, &bps);
816 if (ret)
817 return -EINVAL;
Vivek Goyal4c9eefa2010-09-15 17:06:34 -0400818
Vivek Goyal7702e8f2010-09-15 17:06:36 -0400819 newpn->plid = plid;
820 newpn->fileid = fileid;
821 newpn->val.bps = bps;
822 break;
823 case BLKIO_THROTL_read_iops_device:
824 case BLKIO_THROTL_write_iops_device:
Vivek Goyal9355aed2010-10-01 21:16:41 +0200825 ret = strict_strtoull(s[1], 10, &iops);
Vivek Goyal7702e8f2010-09-15 17:06:36 -0400826 if (ret)
827 return -EINVAL;
828
Vivek Goyal9355aed2010-10-01 21:16:41 +0200829 if (iops > THROTL_IOPS_MAX)
830 return -EINVAL;
831
Vivek Goyal7702e8f2010-09-15 17:06:36 -0400832 newpn->plid = plid;
833 newpn->fileid = fileid;
Vivek Goyal9355aed2010-10-01 21:16:41 +0200834 newpn->val.iops = (unsigned int)iops;
Vivek Goyal7702e8f2010-09-15 17:06:36 -0400835 break;
836 }
Vivek Goyal062a6442010-09-15 17:06:33 -0400837 break;
838 default:
839 BUG();
840 }
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800841
842 return 0;
843}
844
845unsigned int blkcg_get_weight(struct blkio_cgroup *blkcg,
846 dev_t dev)
847{
848 struct blkio_policy_node *pn;
849
Vivek Goyal062a6442010-09-15 17:06:33 -0400850 pn = blkio_policy_search_node(blkcg, dev, BLKIO_POLICY_PROP,
851 BLKIO_PROP_weight_device);
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800852 if (pn)
Vivek Goyal4c9eefa2010-09-15 17:06:34 -0400853 return pn->val.weight;
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800854 else
855 return blkcg->weight;
856}
857EXPORT_SYMBOL_GPL(blkcg_get_weight);
858
Vivek Goyal4c9eefa2010-09-15 17:06:34 -0400859uint64_t blkcg_get_read_bps(struct blkio_cgroup *blkcg, dev_t dev)
860{
861 struct blkio_policy_node *pn;
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800862
Vivek Goyal4c9eefa2010-09-15 17:06:34 -0400863 pn = blkio_policy_search_node(blkcg, dev, BLKIO_POLICY_THROTL,
864 BLKIO_THROTL_read_bps_device);
865 if (pn)
866 return pn->val.bps;
867 else
868 return -1;
869}
870
871uint64_t blkcg_get_write_bps(struct blkio_cgroup *blkcg, dev_t dev)
872{
873 struct blkio_policy_node *pn;
874 pn = blkio_policy_search_node(blkcg, dev, BLKIO_POLICY_THROTL,
875 BLKIO_THROTL_write_bps_device);
876 if (pn)
877 return pn->val.bps;
878 else
879 return -1;
880}
881
Vivek Goyal7702e8f2010-09-15 17:06:36 -0400882unsigned int blkcg_get_read_iops(struct blkio_cgroup *blkcg, dev_t dev)
883{
884 struct blkio_policy_node *pn;
885
886 pn = blkio_policy_search_node(blkcg, dev, BLKIO_POLICY_THROTL,
887 BLKIO_THROTL_read_iops_device);
888 if (pn)
889 return pn->val.iops;
890 else
891 return -1;
892}
893
894unsigned int blkcg_get_write_iops(struct blkio_cgroup *blkcg, dev_t dev)
895{
896 struct blkio_policy_node *pn;
897 pn = blkio_policy_search_node(blkcg, dev, BLKIO_POLICY_THROTL,
898 BLKIO_THROTL_write_iops_device);
899 if (pn)
900 return pn->val.iops;
901 else
902 return -1;
903}
904
Vivek Goyal062a6442010-09-15 17:06:33 -0400905/* Checks whether user asked for deleting a policy rule */
906static bool blkio_delete_rule_command(struct blkio_policy_node *pn)
907{
908 switch(pn->plid) {
909 case BLKIO_POLICY_PROP:
Vivek Goyal4c9eefa2010-09-15 17:06:34 -0400910 if (pn->val.weight == 0)
911 return 1;
912 break;
913 case BLKIO_POLICY_THROTL:
Vivek Goyal7702e8f2010-09-15 17:06:36 -0400914 switch(pn->fileid) {
915 case BLKIO_THROTL_read_bps_device:
916 case BLKIO_THROTL_write_bps_device:
917 if (pn->val.bps == 0)
918 return 1;
919 break;
920 case BLKIO_THROTL_read_iops_device:
921 case BLKIO_THROTL_write_iops_device:
922 if (pn->val.iops == 0)
923 return 1;
924 }
Vivek Goyal062a6442010-09-15 17:06:33 -0400925 break;
926 default:
927 BUG();
928 }
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800929
Vivek Goyal062a6442010-09-15 17:06:33 -0400930 return 0;
931}
932
933static void blkio_update_policy_rule(struct blkio_policy_node *oldpn,
934 struct blkio_policy_node *newpn)
935{
936 switch(oldpn->plid) {
937 case BLKIO_POLICY_PROP:
Vivek Goyal4c9eefa2010-09-15 17:06:34 -0400938 oldpn->val.weight = newpn->val.weight;
939 break;
940 case BLKIO_POLICY_THROTL:
Vivek Goyal7702e8f2010-09-15 17:06:36 -0400941 switch(newpn->fileid) {
942 case BLKIO_THROTL_read_bps_device:
943 case BLKIO_THROTL_write_bps_device:
944 oldpn->val.bps = newpn->val.bps;
945 break;
946 case BLKIO_THROTL_read_iops_device:
947 case BLKIO_THROTL_write_iops_device:
948 oldpn->val.iops = newpn->val.iops;
949 }
Vivek Goyal062a6442010-09-15 17:06:33 -0400950 break;
951 default:
952 BUG();
953 }
954}
955
956/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300957 * Some rules/values in blkg have changed. Propagate those to respective
Vivek Goyal062a6442010-09-15 17:06:33 -0400958 * policies.
959 */
960static void blkio_update_blkg_policy(struct blkio_cgroup *blkcg,
961 struct blkio_group *blkg, struct blkio_policy_node *pn)
962{
Vivek Goyal7702e8f2010-09-15 17:06:36 -0400963 unsigned int weight, iops;
Vivek Goyal4c9eefa2010-09-15 17:06:34 -0400964 u64 bps;
Vivek Goyal062a6442010-09-15 17:06:33 -0400965
966 switch(pn->plid) {
967 case BLKIO_POLICY_PROP:
Vivek Goyal4c9eefa2010-09-15 17:06:34 -0400968 weight = pn->val.weight ? pn->val.weight :
Vivek Goyal062a6442010-09-15 17:06:33 -0400969 blkcg->weight;
970 blkio_update_group_weight(blkg, weight);
971 break;
Vivek Goyal4c9eefa2010-09-15 17:06:34 -0400972 case BLKIO_POLICY_THROTL:
973 switch(pn->fileid) {
974 case BLKIO_THROTL_read_bps_device:
975 case BLKIO_THROTL_write_bps_device:
976 bps = pn->val.bps ? pn->val.bps : (-1);
977 blkio_update_group_bps(blkg, bps, pn->fileid);
978 break;
Vivek Goyal7702e8f2010-09-15 17:06:36 -0400979 case BLKIO_THROTL_read_iops_device:
980 case BLKIO_THROTL_write_iops_device:
981 iops = pn->val.iops ? pn->val.iops : (-1);
982 blkio_update_group_iops(blkg, iops, pn->fileid);
983 break;
Vivek Goyal4c9eefa2010-09-15 17:06:34 -0400984 }
985 break;
Vivek Goyal062a6442010-09-15 17:06:33 -0400986 default:
987 BUG();
988 }
989}
990
991/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300992 * A policy node rule has been updated. Propagate this update to all the
Vivek Goyal062a6442010-09-15 17:06:33 -0400993 * block groups which might be affected by this update.
994 */
995static void blkio_update_policy_node_blkg(struct blkio_cgroup *blkcg,
996 struct blkio_policy_node *pn)
997{
998 struct blkio_group *blkg;
999 struct hlist_node *n;
1000
1001 spin_lock(&blkio_list_lock);
1002 spin_lock_irq(&blkcg->lock);
1003
1004 hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) {
1005 if (pn->dev != blkg->dev || pn->plid != blkg->plid)
1006 continue;
1007 blkio_update_blkg_policy(blkcg, blkg, pn);
1008 }
1009
1010 spin_unlock_irq(&blkcg->lock);
1011 spin_unlock(&blkio_list_lock);
1012}
1013
1014static int blkiocg_file_write(struct cgroup *cgrp, struct cftype *cft,
1015 const char *buffer)
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001016{
1017 int ret = 0;
1018 char *buf;
1019 struct blkio_policy_node *newpn, *pn;
1020 struct blkio_cgroup *blkcg;
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001021 int keep_newpn = 0;
Vivek Goyal062a6442010-09-15 17:06:33 -04001022 enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
1023 int fileid = BLKIOFILE_ATTR(cft->private);
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001024
1025 buf = kstrdup(buffer, GFP_KERNEL);
1026 if (!buf)
1027 return -ENOMEM;
1028
1029 newpn = kzalloc(sizeof(*newpn), GFP_KERNEL);
1030 if (!newpn) {
1031 ret = -ENOMEM;
1032 goto free_buf;
1033 }
1034
Vivek Goyal062a6442010-09-15 17:06:33 -04001035 ret = blkio_policy_parse_and_set(buf, newpn, plid, fileid);
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001036 if (ret)
1037 goto free_newpn;
1038
1039 blkcg = cgroup_to_blkio_cgroup(cgrp);
1040
1041 spin_lock_irq(&blkcg->lock);
1042
Vivek Goyal062a6442010-09-15 17:06:33 -04001043 pn = blkio_policy_search_node(blkcg, newpn->dev, plid, fileid);
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001044 if (!pn) {
Vivek Goyal062a6442010-09-15 17:06:33 -04001045 if (!blkio_delete_rule_command(newpn)) {
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001046 blkio_policy_insert_node(blkcg, newpn);
1047 keep_newpn = 1;
1048 }
1049 spin_unlock_irq(&blkcg->lock);
1050 goto update_io_group;
1051 }
1052
Vivek Goyal062a6442010-09-15 17:06:33 -04001053 if (blkio_delete_rule_command(newpn)) {
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001054 blkio_policy_delete_node(pn);
1055 spin_unlock_irq(&blkcg->lock);
1056 goto update_io_group;
1057 }
1058 spin_unlock_irq(&blkcg->lock);
1059
Vivek Goyal062a6442010-09-15 17:06:33 -04001060 blkio_update_policy_rule(pn, newpn);
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001061
1062update_io_group:
Vivek Goyal062a6442010-09-15 17:06:33 -04001063 blkio_update_policy_node_blkg(blkcg, newpn);
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001064
1065free_newpn:
1066 if (!keep_newpn)
1067 kfree(newpn);
1068free_buf:
1069 kfree(buf);
1070 return ret;
1071}
1072
Vivek Goyal062a6442010-09-15 17:06:33 -04001073static void
1074blkio_print_policy_node(struct seq_file *m, struct blkio_policy_node *pn)
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001075{
Vivek Goyal062a6442010-09-15 17:06:33 -04001076 switch(pn->plid) {
1077 case BLKIO_POLICY_PROP:
1078 if (pn->fileid == BLKIO_PROP_weight_device)
1079 seq_printf(m, "%u:%u\t%u\n", MAJOR(pn->dev),
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001080 MINOR(pn->dev), pn->val.weight);
1081 break;
1082 case BLKIO_POLICY_THROTL:
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001083 switch(pn->fileid) {
1084 case BLKIO_THROTL_read_bps_device:
1085 case BLKIO_THROTL_write_bps_device:
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001086 seq_printf(m, "%u:%u\t%llu\n", MAJOR(pn->dev),
1087 MINOR(pn->dev), pn->val.bps);
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001088 break;
1089 case BLKIO_THROTL_read_iops_device:
1090 case BLKIO_THROTL_write_iops_device:
1091 seq_printf(m, "%u:%u\t%u\n", MAJOR(pn->dev),
1092 MINOR(pn->dev), pn->val.iops);
1093 break;
1094 }
Vivek Goyal062a6442010-09-15 17:06:33 -04001095 break;
1096 default:
1097 BUG();
1098 }
1099}
1100
1101/* cgroup files which read their data from policy nodes end up here */
1102static void blkio_read_policy_node_files(struct cftype *cft,
1103 struct blkio_cgroup *blkcg, struct seq_file *m)
1104{
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001105 struct blkio_policy_node *pn;
1106
Jens Axboe0f3942a2010-05-03 14:28:55 +02001107 if (!list_empty(&blkcg->policy_list)) {
1108 spin_lock_irq(&blkcg->lock);
1109 list_for_each_entry(pn, &blkcg->policy_list, node) {
Vivek Goyal062a6442010-09-15 17:06:33 -04001110 if (!pn_matches_cftype(cft, pn))
1111 continue;
1112 blkio_print_policy_node(m, pn);
Jens Axboe0f3942a2010-05-03 14:28:55 +02001113 }
1114 spin_unlock_irq(&blkcg->lock);
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001115 }
Vivek Goyal062a6442010-09-15 17:06:33 -04001116}
1117
1118static int blkiocg_file_read(struct cgroup *cgrp, struct cftype *cft,
1119 struct seq_file *m)
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_device:
1131 blkio_read_policy_node_files(cft, blkcg, m);
1132 return 0;
1133 default:
1134 BUG();
1135 }
1136 break;
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001137 case BLKIO_POLICY_THROTL:
1138 switch(name){
1139 case BLKIO_THROTL_read_bps_device:
1140 case BLKIO_THROTL_write_bps_device:
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001141 case BLKIO_THROTL_read_iops_device:
1142 case BLKIO_THROTL_write_iops_device:
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001143 blkio_read_policy_node_files(cft, blkcg, m);
1144 return 0;
1145 default:
1146 BUG();
1147 }
1148 break;
Vivek Goyal062a6442010-09-15 17:06:33 -04001149 default:
1150 BUG();
1151 }
1152
1153 return 0;
1154}
1155
1156static int blkio_read_blkg_stats(struct blkio_cgroup *blkcg,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001157 struct cftype *cft, struct cgroup_map_cb *cb,
1158 enum stat_type type, bool show_total, bool pcpu)
Vivek Goyal062a6442010-09-15 17:06:33 -04001159{
1160 struct blkio_group *blkg;
1161 struct hlist_node *n;
1162 uint64_t cgroup_total = 0;
1163
1164 rcu_read_lock();
1165 hlist_for_each_entry_rcu(blkg, n, &blkcg->blkg_list, blkcg_node) {
1166 if (blkg->dev) {
1167 if (!cftype_blkg_same_policy(cft, blkg))
1168 continue;
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001169 if (pcpu)
1170 cgroup_total += blkio_get_stat_cpu(blkg, cb,
1171 blkg->dev, type);
1172 else {
1173 spin_lock_irq(&blkg->stats_lock);
1174 cgroup_total += blkio_get_stat(blkg, cb,
1175 blkg->dev, type);
1176 spin_unlock_irq(&blkg->stats_lock);
1177 }
Vivek Goyal062a6442010-09-15 17:06:33 -04001178 }
1179 }
1180 if (show_total)
1181 cb->fill(cb, "Total", cgroup_total);
1182 rcu_read_unlock();
1183 return 0;
1184}
1185
1186/* All map kind of cgroup file get serviced by this function */
1187static int blkiocg_file_read_map(struct cgroup *cgrp, struct cftype *cft,
1188 struct cgroup_map_cb *cb)
1189{
1190 struct blkio_cgroup *blkcg;
1191 enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
1192 int name = BLKIOFILE_ATTR(cft->private);
1193
1194 blkcg = cgroup_to_blkio_cgroup(cgrp);
1195
1196 switch(plid) {
1197 case BLKIO_POLICY_PROP:
1198 switch(name) {
1199 case BLKIO_PROP_time:
1200 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001201 BLKIO_STAT_TIME, 0, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001202 case BLKIO_PROP_sectors:
1203 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001204 BLKIO_STAT_CPU_SECTORS, 0, 1);
Vivek Goyal062a6442010-09-15 17:06:33 -04001205 case BLKIO_PROP_io_service_bytes:
1206 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001207 BLKIO_STAT_CPU_SERVICE_BYTES, 1, 1);
Vivek Goyal062a6442010-09-15 17:06:33 -04001208 case BLKIO_PROP_io_serviced:
1209 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001210 BLKIO_STAT_CPU_SERVICED, 1, 1);
Vivek Goyal062a6442010-09-15 17:06:33 -04001211 case BLKIO_PROP_io_service_time:
1212 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001213 BLKIO_STAT_SERVICE_TIME, 1, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001214 case BLKIO_PROP_io_wait_time:
1215 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001216 BLKIO_STAT_WAIT_TIME, 1, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001217 case BLKIO_PROP_io_merged:
1218 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001219 BLKIO_STAT_MERGED, 1, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001220 case BLKIO_PROP_io_queued:
1221 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001222 BLKIO_STAT_QUEUED, 1, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001223#ifdef CONFIG_DEBUG_BLK_CGROUP
Justin TerAvest9026e522011-03-22 21:26:54 +01001224 case BLKIO_PROP_unaccounted_time:
1225 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001226 BLKIO_STAT_UNACCOUNTED_TIME, 0, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001227 case BLKIO_PROP_dequeue:
1228 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001229 BLKIO_STAT_DEQUEUE, 0, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001230 case BLKIO_PROP_avg_queue_size:
1231 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001232 BLKIO_STAT_AVG_QUEUE_SIZE, 0, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001233 case BLKIO_PROP_group_wait_time:
1234 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001235 BLKIO_STAT_GROUP_WAIT_TIME, 0, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001236 case BLKIO_PROP_idle_time:
1237 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001238 BLKIO_STAT_IDLE_TIME, 0, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001239 case BLKIO_PROP_empty_time:
1240 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001241 BLKIO_STAT_EMPTY_TIME, 0, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001242#endif
1243 default:
1244 BUG();
1245 }
1246 break;
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001247 case BLKIO_POLICY_THROTL:
1248 switch(name){
1249 case BLKIO_THROTL_io_service_bytes:
1250 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001251 BLKIO_STAT_CPU_SERVICE_BYTES, 1, 1);
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001252 case BLKIO_THROTL_io_serviced:
1253 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001254 BLKIO_STAT_CPU_SERVICED, 1, 1);
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001255 default:
1256 BUG();
1257 }
1258 break;
Vivek Goyal062a6442010-09-15 17:06:33 -04001259 default:
1260 BUG();
1261 }
1262
1263 return 0;
1264}
1265
1266static int blkio_weight_write(struct blkio_cgroup *blkcg, u64 val)
1267{
1268 struct blkio_group *blkg;
1269 struct hlist_node *n;
1270 struct blkio_policy_node *pn;
1271
1272 if (val < BLKIO_WEIGHT_MIN || val > BLKIO_WEIGHT_MAX)
1273 return -EINVAL;
1274
1275 spin_lock(&blkio_list_lock);
1276 spin_lock_irq(&blkcg->lock);
1277 blkcg->weight = (unsigned int)val;
1278
1279 hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) {
1280 pn = blkio_policy_search_node(blkcg, blkg->dev,
1281 BLKIO_POLICY_PROP, BLKIO_PROP_weight_device);
1282 if (pn)
1283 continue;
1284
1285 blkio_update_group_weight(blkg, blkcg->weight);
1286 }
1287 spin_unlock_irq(&blkcg->lock);
1288 spin_unlock(&blkio_list_lock);
1289 return 0;
1290}
1291
1292static u64 blkiocg_file_read_u64 (struct cgroup *cgrp, struct cftype *cft) {
1293 struct blkio_cgroup *blkcg;
1294 enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
1295 int name = BLKIOFILE_ATTR(cft->private);
1296
1297 blkcg = cgroup_to_blkio_cgroup(cgrp);
1298
1299 switch(plid) {
1300 case BLKIO_POLICY_PROP:
1301 switch(name) {
1302 case BLKIO_PROP_weight:
1303 return (u64)blkcg->weight;
1304 }
1305 break;
1306 default:
1307 BUG();
1308 }
1309 return 0;
1310}
1311
1312static int
1313blkiocg_file_write_u64(struct cgroup *cgrp, struct cftype *cft, u64 val)
1314{
1315 struct blkio_cgroup *blkcg;
1316 enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
1317 int name = BLKIOFILE_ATTR(cft->private);
1318
1319 blkcg = cgroup_to_blkio_cgroup(cgrp);
1320
1321 switch(plid) {
1322 case BLKIO_POLICY_PROP:
1323 switch(name) {
1324 case BLKIO_PROP_weight:
1325 return blkio_weight_write(blkcg, val);
1326 }
1327 break;
1328 default:
1329 BUG();
1330 }
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001331
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001332 return 0;
1333}
1334
Vivek Goyal31e4c282009-12-03 12:59:42 -05001335struct cftype blkio_files[] = {
1336 {
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001337 .name = "weight_device",
Vivek Goyal062a6442010-09-15 17:06:33 -04001338 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1339 BLKIO_PROP_weight_device),
1340 .read_seq_string = blkiocg_file_read,
1341 .write_string = blkiocg_file_write,
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001342 .max_write_len = 256,
1343 },
1344 {
Vivek Goyal31e4c282009-12-03 12:59:42 -05001345 .name = "weight",
Vivek Goyal062a6442010-09-15 17:06:33 -04001346 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1347 BLKIO_PROP_weight),
1348 .read_u64 = blkiocg_file_read_u64,
1349 .write_u64 = blkiocg_file_write_u64,
Vivek Goyal31e4c282009-12-03 12:59:42 -05001350 },
Vivek Goyal22084192009-12-03 12:59:49 -05001351 {
1352 .name = "time",
Vivek Goyal13f98252010-10-01 14:49:41 +02001353 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1354 BLKIO_PROP_time),
1355 .read_map = blkiocg_file_read_map,
Vivek Goyal22084192009-12-03 12:59:49 -05001356 },
1357 {
1358 .name = "sectors",
Vivek Goyal13f98252010-10-01 14:49:41 +02001359 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1360 BLKIO_PROP_sectors),
1361 .read_map = blkiocg_file_read_map,
Divyesh Shah303a3ac2010-04-01 15:01:24 -07001362 },
1363 {
1364 .name = "io_service_bytes",
Vivek Goyal13f98252010-10-01 14:49:41 +02001365 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1366 BLKIO_PROP_io_service_bytes),
1367 .read_map = blkiocg_file_read_map,
Divyesh Shah303a3ac2010-04-01 15:01:24 -07001368 },
1369 {
1370 .name = "io_serviced",
Vivek Goyal13f98252010-10-01 14:49:41 +02001371 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1372 BLKIO_PROP_io_serviced),
1373 .read_map = blkiocg_file_read_map,
Divyesh Shah303a3ac2010-04-01 15:01:24 -07001374 },
1375 {
1376 .name = "io_service_time",
Vivek Goyal13f98252010-10-01 14:49:41 +02001377 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1378 BLKIO_PROP_io_service_time),
1379 .read_map = blkiocg_file_read_map,
Divyesh Shah303a3ac2010-04-01 15:01:24 -07001380 },
1381 {
1382 .name = "io_wait_time",
Vivek Goyal13f98252010-10-01 14:49:41 +02001383 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1384 BLKIO_PROP_io_wait_time),
1385 .read_map = blkiocg_file_read_map,
Divyesh Shah84c124d2010-04-09 08:31:19 +02001386 },
1387 {
Divyesh Shah812d4022010-04-08 21:14:23 -07001388 .name = "io_merged",
Vivek Goyal13f98252010-10-01 14:49:41 +02001389 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1390 BLKIO_PROP_io_merged),
1391 .read_map = blkiocg_file_read_map,
Divyesh Shah812d4022010-04-08 21:14:23 -07001392 },
1393 {
Divyesh Shahcdc11842010-04-08 21:15:10 -07001394 .name = "io_queued",
Vivek Goyal13f98252010-10-01 14:49:41 +02001395 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1396 BLKIO_PROP_io_queued),
1397 .read_map = blkiocg_file_read_map,
Divyesh Shahcdc11842010-04-08 21:15:10 -07001398 },
1399 {
Divyesh Shah84c124d2010-04-09 08:31:19 +02001400 .name = "reset_stats",
1401 .write_u64 = blkiocg_reset_stats,
Vivek Goyal22084192009-12-03 12:59:49 -05001402 },
Vivek Goyal13f98252010-10-01 14:49:41 +02001403#ifdef CONFIG_BLK_DEV_THROTTLING
1404 {
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001405 .name = "throttle.read_bps_device",
1406 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1407 BLKIO_THROTL_read_bps_device),
1408 .read_seq_string = blkiocg_file_read,
1409 .write_string = blkiocg_file_write,
1410 .max_write_len = 256,
1411 },
1412
1413 {
1414 .name = "throttle.write_bps_device",
1415 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1416 BLKIO_THROTL_write_bps_device),
1417 .read_seq_string = blkiocg_file_read,
1418 .write_string = blkiocg_file_write,
1419 .max_write_len = 256,
1420 },
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001421
1422 {
1423 .name = "throttle.read_iops_device",
1424 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1425 BLKIO_THROTL_read_iops_device),
1426 .read_seq_string = blkiocg_file_read,
1427 .write_string = blkiocg_file_write,
1428 .max_write_len = 256,
1429 },
1430
1431 {
1432 .name = "throttle.write_iops_device",
1433 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1434 BLKIO_THROTL_write_iops_device),
1435 .read_seq_string = blkiocg_file_read,
1436 .write_string = blkiocg_file_write,
1437 .max_write_len = 256,
1438 },
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001439 {
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001440 .name = "throttle.io_service_bytes",
1441 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1442 BLKIO_THROTL_io_service_bytes),
1443 .read_map = blkiocg_file_read_map,
1444 },
1445 {
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001446 .name = "throttle.io_serviced",
1447 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1448 BLKIO_THROTL_io_serviced),
1449 .read_map = blkiocg_file_read_map,
1450 },
Vivek Goyal13f98252010-10-01 14:49:41 +02001451#endif /* CONFIG_BLK_DEV_THROTTLING */
1452
Vivek Goyal22084192009-12-03 12:59:49 -05001453#ifdef CONFIG_DEBUG_BLK_CGROUP
Divyesh Shahcdc11842010-04-08 21:15:10 -07001454 {
1455 .name = "avg_queue_size",
Vivek Goyal062a6442010-09-15 17:06:33 -04001456 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1457 BLKIO_PROP_avg_queue_size),
1458 .read_map = blkiocg_file_read_map,
Divyesh Shahcdc11842010-04-08 21:15:10 -07001459 },
1460 {
Divyesh Shah812df482010-04-08 21:15:35 -07001461 .name = "group_wait_time",
Vivek Goyal062a6442010-09-15 17:06:33 -04001462 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1463 BLKIO_PROP_group_wait_time),
1464 .read_map = blkiocg_file_read_map,
Divyesh Shah812df482010-04-08 21:15:35 -07001465 },
1466 {
1467 .name = "idle_time",
Vivek Goyal062a6442010-09-15 17:06:33 -04001468 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1469 BLKIO_PROP_idle_time),
1470 .read_map = blkiocg_file_read_map,
Divyesh Shah812df482010-04-08 21:15:35 -07001471 },
1472 {
1473 .name = "empty_time",
Vivek Goyal062a6442010-09-15 17:06:33 -04001474 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1475 BLKIO_PROP_empty_time),
1476 .read_map = blkiocg_file_read_map,
Divyesh Shah812df482010-04-08 21:15:35 -07001477 },
1478 {
Vivek Goyal22084192009-12-03 12:59:49 -05001479 .name = "dequeue",
Vivek Goyal062a6442010-09-15 17:06:33 -04001480 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1481 BLKIO_PROP_dequeue),
1482 .read_map = blkiocg_file_read_map,
Divyesh Shahcdc11842010-04-08 21:15:10 -07001483 },
Justin TerAvest9026e522011-03-22 21:26:54 +01001484 {
1485 .name = "unaccounted_time",
1486 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1487 BLKIO_PROP_unaccounted_time),
1488 .read_map = blkiocg_file_read_map,
1489 },
Vivek Goyal22084192009-12-03 12:59:49 -05001490#endif
Vivek Goyal31e4c282009-12-03 12:59:42 -05001491};
1492
1493static int blkiocg_populate(struct cgroup_subsys *subsys, struct cgroup *cgroup)
1494{
1495 return cgroup_add_files(cgroup, subsys, blkio_files,
1496 ARRAY_SIZE(blkio_files));
1497}
1498
1499static void blkiocg_destroy(struct cgroup_subsys *subsys, struct cgroup *cgroup)
1500{
1501 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgroup);
Vivek Goyalb1c35762009-12-03 12:59:47 -05001502 unsigned long flags;
1503 struct blkio_group *blkg;
1504 void *key;
Vivek Goyal3e252062009-12-04 10:36:42 -05001505 struct blkio_policy_type *blkiop;
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001506 struct blkio_policy_node *pn, *pntmp;
Vivek Goyal31e4c282009-12-03 12:59:42 -05001507
Vivek Goyalb1c35762009-12-03 12:59:47 -05001508 rcu_read_lock();
Jens Axboe0f3942a2010-05-03 14:28:55 +02001509 do {
1510 spin_lock_irqsave(&blkcg->lock, flags);
Vivek Goyalb1c35762009-12-03 12:59:47 -05001511
Jens Axboe0f3942a2010-05-03 14:28:55 +02001512 if (hlist_empty(&blkcg->blkg_list)) {
1513 spin_unlock_irqrestore(&blkcg->lock, flags);
1514 break;
1515 }
1516
1517 blkg = hlist_entry(blkcg->blkg_list.first, struct blkio_group,
1518 blkcg_node);
1519 key = rcu_dereference(blkg->key);
1520 __blkiocg_del_blkio_group(blkg);
1521
Vivek Goyalb1c35762009-12-03 12:59:47 -05001522 spin_unlock_irqrestore(&blkcg->lock, flags);
Vivek Goyalb1c35762009-12-03 12:59:47 -05001523
Jens Axboe0f3942a2010-05-03 14:28:55 +02001524 /*
1525 * This blkio_group is being unlinked as associated cgroup is
1526 * going away. Let all the IO controlling policies know about
Vivek Goyal61014e92010-10-01 14:49:44 +02001527 * this event.
Jens Axboe0f3942a2010-05-03 14:28:55 +02001528 */
1529 spin_lock(&blkio_list_lock);
Vivek Goyal61014e92010-10-01 14:49:44 +02001530 list_for_each_entry(blkiop, &blkio_list, list) {
1531 if (blkiop->plid != blkg->plid)
1532 continue;
Jens Axboe0f3942a2010-05-03 14:28:55 +02001533 blkiop->ops.blkio_unlink_group_fn(key, blkg);
Vivek Goyal61014e92010-10-01 14:49:44 +02001534 }
Jens Axboe0f3942a2010-05-03 14:28:55 +02001535 spin_unlock(&blkio_list_lock);
1536 } while (1);
Vivek Goyalb1c35762009-12-03 12:59:47 -05001537
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001538 list_for_each_entry_safe(pn, pntmp, &blkcg->policy_list, node) {
1539 blkio_policy_delete_node(pn);
1540 kfree(pn);
1541 }
Jens Axboe0f3942a2010-05-03 14:28:55 +02001542
Vivek Goyal31e4c282009-12-03 12:59:42 -05001543 free_css_id(&blkio_subsys, &blkcg->css);
Vivek Goyalb1c35762009-12-03 12:59:47 -05001544 rcu_read_unlock();
Ben Blum67523c42010-03-10 15:22:11 -08001545 if (blkcg != &blkio_root_cgroup)
1546 kfree(blkcg);
Vivek Goyal31e4c282009-12-03 12:59:42 -05001547}
1548
1549static struct cgroup_subsys_state *
1550blkiocg_create(struct cgroup_subsys *subsys, struct cgroup *cgroup)
1551{
Li Zefan03415092010-05-07 08:57:00 +02001552 struct blkio_cgroup *blkcg;
1553 struct cgroup *parent = cgroup->parent;
Vivek Goyal31e4c282009-12-03 12:59:42 -05001554
Li Zefan03415092010-05-07 08:57:00 +02001555 if (!parent) {
Vivek Goyal31e4c282009-12-03 12:59:42 -05001556 blkcg = &blkio_root_cgroup;
1557 goto done;
1558 }
1559
Vivek Goyal31e4c282009-12-03 12:59:42 -05001560 blkcg = kzalloc(sizeof(*blkcg), GFP_KERNEL);
1561 if (!blkcg)
1562 return ERR_PTR(-ENOMEM);
1563
1564 blkcg->weight = BLKIO_WEIGHT_DEFAULT;
1565done:
1566 spin_lock_init(&blkcg->lock);
1567 INIT_HLIST_HEAD(&blkcg->blkg_list);
1568
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001569 INIT_LIST_HEAD(&blkcg->policy_list);
Vivek Goyal31e4c282009-12-03 12:59:42 -05001570 return &blkcg->css;
1571}
1572
1573/*
1574 * We cannot support shared io contexts, as we have no mean to support
1575 * two tasks with the same ioc in two different groups without major rework
1576 * of the main cic data structures. For now we allow a task to change
1577 * its cgroup only if it's the only owner of its ioc.
1578 */
1579static int blkiocg_can_attach(struct cgroup_subsys *subsys,
1580 struct cgroup *cgroup, struct task_struct *tsk,
1581 bool threadgroup)
1582{
1583 struct io_context *ioc;
1584 int ret = 0;
1585
1586 /* task_lock() is needed to avoid races with exit_io_context() */
1587 task_lock(tsk);
1588 ioc = tsk->io_context;
1589 if (ioc && atomic_read(&ioc->nr_tasks) > 1)
1590 ret = -EINVAL;
1591 task_unlock(tsk);
1592
1593 return ret;
1594}
1595
1596static void blkiocg_attach(struct cgroup_subsys *subsys, struct cgroup *cgroup,
1597 struct cgroup *prev, struct task_struct *tsk,
1598 bool threadgroup)
1599{
1600 struct io_context *ioc;
1601
1602 task_lock(tsk);
1603 ioc = tsk->io_context;
1604 if (ioc)
1605 ioc->cgroup_changed = 1;
1606 task_unlock(tsk);
1607}
1608
Vivek Goyal3e252062009-12-04 10:36:42 -05001609void blkio_policy_register(struct blkio_policy_type *blkiop)
1610{
1611 spin_lock(&blkio_list_lock);
1612 list_add_tail(&blkiop->list, &blkio_list);
1613 spin_unlock(&blkio_list_lock);
1614}
1615EXPORT_SYMBOL_GPL(blkio_policy_register);
1616
1617void blkio_policy_unregister(struct blkio_policy_type *blkiop)
1618{
1619 spin_lock(&blkio_list_lock);
1620 list_del_init(&blkiop->list);
1621 spin_unlock(&blkio_list_lock);
1622}
1623EXPORT_SYMBOL_GPL(blkio_policy_unregister);
Ben Blum67523c42010-03-10 15:22:11 -08001624
1625static int __init init_cgroup_blkio(void)
1626{
1627 return cgroup_load_subsys(&blkio_subsys);
1628}
1629
1630static void __exit exit_cgroup_blkio(void)
1631{
1632 cgroup_unload_subsys(&blkio_subsys);
1633}
1634
1635module_init(init_cgroup_blkio);
1636module_exit(exit_cgroup_blkio);
1637MODULE_LICENSE("GPL");