blob: 61553670735da49062ba256fe3e4cd138063ca17 [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 *);
Ben Blumf780bdb2011-05-26 16:25:19 -070033static int blkiocg_can_attach_task(struct cgroup *, struct task_struct *);
34static void blkiocg_attach_task(struct cgroup *, struct task_struct *);
Ben Blum67523c42010-03-10 15:22:11 -080035static void blkiocg_destroy(struct cgroup_subsys *, struct cgroup *);
36static int blkiocg_populate(struct cgroup_subsys *, struct cgroup *);
37
Vivek Goyal062a6442010-09-15 17:06:33 -040038/* for encoding cft->private value on file */
39#define BLKIOFILE_PRIVATE(x, val) (((x) << 16) | (val))
40/* What policy owns the file, proportional or throttle */
41#define BLKIOFILE_POLICY(val) (((val) >> 16) & 0xffff)
42#define BLKIOFILE_ATTR(val) ((val) & 0xffff)
43
Ben Blum67523c42010-03-10 15:22:11 -080044struct cgroup_subsys blkio_subsys = {
45 .name = "blkio",
46 .create = blkiocg_create,
Ben Blumf780bdb2011-05-26 16:25:19 -070047 .can_attach_task = blkiocg_can_attach_task,
48 .attach_task = blkiocg_attach_task,
Ben Blum67523c42010-03-10 15:22:11 -080049 .destroy = blkiocg_destroy,
50 .populate = blkiocg_populate,
51#ifdef CONFIG_BLK_CGROUP
52 /* note: blkio_subsys_id is otherwise defined in blk-cgroup.h */
53 .subsys_id = blkio_subsys_id,
54#endif
55 .use_id = 1,
56 .module = THIS_MODULE,
57};
58EXPORT_SYMBOL_GPL(blkio_subsys);
59
Gui Jianfeng34d0f172010-04-13 16:05:49 +080060static inline void blkio_policy_insert_node(struct blkio_cgroup *blkcg,
61 struct blkio_policy_node *pn)
62{
63 list_add(&pn->node, &blkcg->policy_list);
64}
65
Vivek Goyal062a6442010-09-15 17:06:33 -040066static inline bool cftype_blkg_same_policy(struct cftype *cft,
67 struct blkio_group *blkg)
68{
69 enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
70
71 if (blkg->plid == plid)
72 return 1;
73
74 return 0;
75}
76
77/* Determines if policy node matches cgroup file being accessed */
78static inline bool pn_matches_cftype(struct cftype *cft,
79 struct blkio_policy_node *pn)
80{
81 enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
82 int fileid = BLKIOFILE_ATTR(cft->private);
83
84 return (plid == pn->plid && fileid == pn->fileid);
85}
86
Gui Jianfeng34d0f172010-04-13 16:05:49 +080087/* Must be called with blkcg->lock held */
88static inline void blkio_policy_delete_node(struct blkio_policy_node *pn)
89{
90 list_del(&pn->node);
91}
92
93/* Must be called with blkcg->lock held */
94static struct blkio_policy_node *
Vivek Goyal062a6442010-09-15 17:06:33 -040095blkio_policy_search_node(const struct blkio_cgroup *blkcg, dev_t dev,
96 enum blkio_policy_id plid, int fileid)
Gui Jianfeng34d0f172010-04-13 16:05:49 +080097{
98 struct blkio_policy_node *pn;
99
100 list_for_each_entry(pn, &blkcg->policy_list, node) {
Vivek Goyal062a6442010-09-15 17:06:33 -0400101 if (pn->dev == dev && pn->plid == plid && pn->fileid == fileid)
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800102 return pn;
103 }
104
105 return NULL;
106}
107
Vivek Goyal31e4c282009-12-03 12:59:42 -0500108struct blkio_cgroup *cgroup_to_blkio_cgroup(struct cgroup *cgroup)
109{
110 return container_of(cgroup_subsys_state(cgroup, blkio_subsys_id),
111 struct blkio_cgroup, css);
112}
Vivek Goyal9d6a9862009-12-04 10:36:41 -0500113EXPORT_SYMBOL_GPL(cgroup_to_blkio_cgroup);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500114
Vivek Goyal70087dc2011-05-16 15:24:08 +0200115struct blkio_cgroup *task_blkio_cgroup(struct task_struct *tsk)
116{
117 return container_of(task_subsys_state(tsk, blkio_subsys_id),
118 struct blkio_cgroup, css);
119}
120EXPORT_SYMBOL_GPL(task_blkio_cgroup);
121
Vivek Goyal062a6442010-09-15 17:06:33 -0400122static inline void
123blkio_update_group_weight(struct blkio_group *blkg, unsigned int weight)
124{
125 struct blkio_policy_type *blkiop;
126
127 list_for_each_entry(blkiop, &blkio_list, list) {
128 /* If this policy does not own the blkg, do not send updates */
129 if (blkiop->plid != blkg->plid)
130 continue;
131 if (blkiop->ops.blkio_update_group_weight_fn)
Vivek Goyalfe071432010-10-01 14:49:49 +0200132 blkiop->ops.blkio_update_group_weight_fn(blkg->key,
133 blkg, weight);
Vivek Goyal062a6442010-09-15 17:06:33 -0400134 }
135}
136
Vivek Goyal4c9eefa2010-09-15 17:06:34 -0400137static inline void blkio_update_group_bps(struct blkio_group *blkg, u64 bps,
138 int fileid)
139{
140 struct blkio_policy_type *blkiop;
141
142 list_for_each_entry(blkiop, &blkio_list, list) {
143
144 /* If this policy does not own the blkg, do not send updates */
145 if (blkiop->plid != blkg->plid)
146 continue;
147
148 if (fileid == BLKIO_THROTL_read_bps_device
149 && blkiop->ops.blkio_update_group_read_bps_fn)
Vivek Goyalfe071432010-10-01 14:49:49 +0200150 blkiop->ops.blkio_update_group_read_bps_fn(blkg->key,
151 blkg, bps);
Vivek Goyal4c9eefa2010-09-15 17:06:34 -0400152
153 if (fileid == BLKIO_THROTL_write_bps_device
154 && blkiop->ops.blkio_update_group_write_bps_fn)
Vivek Goyalfe071432010-10-01 14:49:49 +0200155 blkiop->ops.blkio_update_group_write_bps_fn(blkg->key,
156 blkg, bps);
Vivek Goyal4c9eefa2010-09-15 17:06:34 -0400157 }
158}
159
Vivek Goyal7702e8f2010-09-15 17:06:36 -0400160static inline void blkio_update_group_iops(struct blkio_group *blkg,
161 unsigned int iops, int fileid)
162{
163 struct blkio_policy_type *blkiop;
164
165 list_for_each_entry(blkiop, &blkio_list, list) {
166
167 /* If this policy does not own the blkg, do not send updates */
168 if (blkiop->plid != blkg->plid)
169 continue;
170
171 if (fileid == BLKIO_THROTL_read_iops_device
172 && blkiop->ops.blkio_update_group_read_iops_fn)
Vivek Goyalfe071432010-10-01 14:49:49 +0200173 blkiop->ops.blkio_update_group_read_iops_fn(blkg->key,
174 blkg, iops);
Vivek Goyal7702e8f2010-09-15 17:06:36 -0400175
176 if (fileid == BLKIO_THROTL_write_iops_device
177 && blkiop->ops.blkio_update_group_write_iops_fn)
Vivek Goyalfe071432010-10-01 14:49:49 +0200178 blkiop->ops.blkio_update_group_write_iops_fn(blkg->key,
179 blkg,iops);
Vivek Goyal7702e8f2010-09-15 17:06:36 -0400180 }
181}
182
Divyesh Shah91952912010-04-01 15:01:41 -0700183/*
184 * Add to the appropriate stat variable depending on the request type.
185 * This should be called with the blkg->stats_lock held.
186 */
Divyesh Shah84c124d2010-04-09 08:31:19 +0200187static void blkio_add_stat(uint64_t *stat, uint64_t add, bool direction,
188 bool sync)
Divyesh Shah91952912010-04-01 15:01:41 -0700189{
Divyesh Shah84c124d2010-04-09 08:31:19 +0200190 if (direction)
191 stat[BLKIO_STAT_WRITE] += add;
Divyesh Shah91952912010-04-01 15:01:41 -0700192 else
Divyesh Shah84c124d2010-04-09 08:31:19 +0200193 stat[BLKIO_STAT_READ] += add;
194 if (sync)
195 stat[BLKIO_STAT_SYNC] += add;
Divyesh Shah91952912010-04-01 15:01:41 -0700196 else
Divyesh Shah84c124d2010-04-09 08:31:19 +0200197 stat[BLKIO_STAT_ASYNC] += add;
Divyesh Shah91952912010-04-01 15:01:41 -0700198}
199
Divyesh Shahcdc11842010-04-08 21:15:10 -0700200/*
201 * Decrements the appropriate stat variable if non-zero depending on the
202 * request type. Panics on value being zero.
203 * This should be called with the blkg->stats_lock held.
204 */
205static void blkio_check_and_dec_stat(uint64_t *stat, bool direction, bool sync)
206{
207 if (direction) {
208 BUG_ON(stat[BLKIO_STAT_WRITE] == 0);
209 stat[BLKIO_STAT_WRITE]--;
210 } else {
211 BUG_ON(stat[BLKIO_STAT_READ] == 0);
212 stat[BLKIO_STAT_READ]--;
213 }
214 if (sync) {
215 BUG_ON(stat[BLKIO_STAT_SYNC] == 0);
216 stat[BLKIO_STAT_SYNC]--;
217 } else {
218 BUG_ON(stat[BLKIO_STAT_ASYNC] == 0);
219 stat[BLKIO_STAT_ASYNC]--;
220 }
221}
222
223#ifdef CONFIG_DEBUG_BLK_CGROUP
Divyesh Shah812df482010-04-08 21:15:35 -0700224/* This should be called with the blkg->stats_lock held. */
225static void blkio_set_start_group_wait_time(struct blkio_group *blkg,
226 struct blkio_group *curr_blkg)
227{
228 if (blkio_blkg_waiting(&blkg->stats))
229 return;
230 if (blkg == curr_blkg)
231 return;
232 blkg->stats.start_group_wait_time = sched_clock();
233 blkio_mark_blkg_waiting(&blkg->stats);
234}
235
236/* This should be called with the blkg->stats_lock held. */
237static void blkio_update_group_wait_time(struct blkio_group_stats *stats)
238{
239 unsigned long long now;
240
241 if (!blkio_blkg_waiting(stats))
242 return;
243
244 now = sched_clock();
245 if (time_after64(now, stats->start_group_wait_time))
246 stats->group_wait_time += now - stats->start_group_wait_time;
247 blkio_clear_blkg_waiting(stats);
248}
249
250/* This should be called with the blkg->stats_lock held. */
251static void blkio_end_empty_time(struct blkio_group_stats *stats)
252{
253 unsigned long long now;
254
255 if (!blkio_blkg_empty(stats))
256 return;
257
258 now = sched_clock();
259 if (time_after64(now, stats->start_empty_time))
260 stats->empty_time += now - stats->start_empty_time;
261 blkio_clear_blkg_empty(stats);
262}
263
264void blkiocg_update_set_idle_time_stats(struct blkio_group *blkg)
265{
266 unsigned long flags;
267
268 spin_lock_irqsave(&blkg->stats_lock, flags);
269 BUG_ON(blkio_blkg_idling(&blkg->stats));
270 blkg->stats.start_idle_time = sched_clock();
271 blkio_mark_blkg_idling(&blkg->stats);
272 spin_unlock_irqrestore(&blkg->stats_lock, flags);
273}
274EXPORT_SYMBOL_GPL(blkiocg_update_set_idle_time_stats);
275
276void blkiocg_update_idle_time_stats(struct blkio_group *blkg)
277{
278 unsigned long flags;
279 unsigned long long now;
280 struct blkio_group_stats *stats;
281
282 spin_lock_irqsave(&blkg->stats_lock, flags);
283 stats = &blkg->stats;
284 if (blkio_blkg_idling(stats)) {
285 now = sched_clock();
286 if (time_after64(now, stats->start_idle_time))
287 stats->idle_time += now - stats->start_idle_time;
288 blkio_clear_blkg_idling(stats);
289 }
290 spin_unlock_irqrestore(&blkg->stats_lock, flags);
291}
292EXPORT_SYMBOL_GPL(blkiocg_update_idle_time_stats);
293
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200294void blkiocg_update_avg_queue_size_stats(struct blkio_group *blkg)
Divyesh Shahcdc11842010-04-08 21:15:10 -0700295{
296 unsigned long flags;
297 struct blkio_group_stats *stats;
298
299 spin_lock_irqsave(&blkg->stats_lock, flags);
300 stats = &blkg->stats;
301 stats->avg_queue_size_sum +=
302 stats->stat_arr[BLKIO_STAT_QUEUED][BLKIO_STAT_READ] +
303 stats->stat_arr[BLKIO_STAT_QUEUED][BLKIO_STAT_WRITE];
304 stats->avg_queue_size_samples++;
Divyesh Shah812df482010-04-08 21:15:35 -0700305 blkio_update_group_wait_time(stats);
Divyesh Shahcdc11842010-04-08 21:15:10 -0700306 spin_unlock_irqrestore(&blkg->stats_lock, flags);
307}
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200308EXPORT_SYMBOL_GPL(blkiocg_update_avg_queue_size_stats);
309
Vivek Goyale5ff0822010-04-26 19:25:11 +0200310void blkiocg_set_start_empty_time(struct blkio_group *blkg)
Divyesh Shah28baf442010-04-14 11:22:38 +0200311{
312 unsigned long flags;
313 struct blkio_group_stats *stats;
314
315 spin_lock_irqsave(&blkg->stats_lock, flags);
316 stats = &blkg->stats;
317
318 if (stats->stat_arr[BLKIO_STAT_QUEUED][BLKIO_STAT_READ] ||
319 stats->stat_arr[BLKIO_STAT_QUEUED][BLKIO_STAT_WRITE]) {
320 spin_unlock_irqrestore(&blkg->stats_lock, flags);
321 return;
322 }
323
324 /*
Vivek Goyale5ff0822010-04-26 19:25:11 +0200325 * group is already marked empty. This can happen if cfqq got new
326 * request in parent group and moved to this group while being added
327 * to service tree. Just ignore the event and move on.
Divyesh Shah28baf442010-04-14 11:22:38 +0200328 */
Vivek Goyale5ff0822010-04-26 19:25:11 +0200329 if(blkio_blkg_empty(stats)) {
330 spin_unlock_irqrestore(&blkg->stats_lock, flags);
331 return;
332 }
333
Divyesh Shah28baf442010-04-14 11:22:38 +0200334 stats->start_empty_time = sched_clock();
335 blkio_mark_blkg_empty(stats);
336 spin_unlock_irqrestore(&blkg->stats_lock, flags);
337}
338EXPORT_SYMBOL_GPL(blkiocg_set_start_empty_time);
339
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200340void blkiocg_update_dequeue_stats(struct blkio_group *blkg,
341 unsigned long dequeue)
342{
343 blkg->stats.dequeue += dequeue;
344}
345EXPORT_SYMBOL_GPL(blkiocg_update_dequeue_stats);
Divyesh Shah812df482010-04-08 21:15:35 -0700346#else
347static inline void blkio_set_start_group_wait_time(struct blkio_group *blkg,
348 struct blkio_group *curr_blkg) {}
349static inline void blkio_end_empty_time(struct blkio_group_stats *stats) {}
Divyesh Shahcdc11842010-04-08 21:15:10 -0700350#endif
351
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200352void blkiocg_update_io_add_stats(struct blkio_group *blkg,
Divyesh Shahcdc11842010-04-08 21:15:10 -0700353 struct blkio_group *curr_blkg, bool direction,
354 bool sync)
355{
356 unsigned long flags;
357
358 spin_lock_irqsave(&blkg->stats_lock, flags);
359 blkio_add_stat(blkg->stats.stat_arr[BLKIO_STAT_QUEUED], 1, direction,
360 sync);
Divyesh Shah812df482010-04-08 21:15:35 -0700361 blkio_end_empty_time(&blkg->stats);
362 blkio_set_start_group_wait_time(blkg, curr_blkg);
Divyesh Shahcdc11842010-04-08 21:15:10 -0700363 spin_unlock_irqrestore(&blkg->stats_lock, flags);
364}
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200365EXPORT_SYMBOL_GPL(blkiocg_update_io_add_stats);
Divyesh Shahcdc11842010-04-08 21:15:10 -0700366
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200367void blkiocg_update_io_remove_stats(struct blkio_group *blkg,
Divyesh Shahcdc11842010-04-08 21:15:10 -0700368 bool direction, bool sync)
369{
370 unsigned long flags;
371
372 spin_lock_irqsave(&blkg->stats_lock, flags);
373 blkio_check_and_dec_stat(blkg->stats.stat_arr[BLKIO_STAT_QUEUED],
374 direction, sync);
375 spin_unlock_irqrestore(&blkg->stats_lock, flags);
376}
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200377EXPORT_SYMBOL_GPL(blkiocg_update_io_remove_stats);
Divyesh Shahcdc11842010-04-08 21:15:10 -0700378
Justin TerAvest167400d2011-03-12 16:54:00 +0100379void blkiocg_update_timeslice_used(struct blkio_group *blkg, unsigned long time,
380 unsigned long unaccounted_time)
Vivek Goyal22084192009-12-03 12:59:49 -0500381{
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700382 unsigned long flags;
383
384 spin_lock_irqsave(&blkg->stats_lock, flags);
385 blkg->stats.time += time;
Vivek Goyala23e6862011-05-19 15:38:20 -0400386#ifdef CONFIG_DEBUG_BLK_CGROUP
Justin TerAvest167400d2011-03-12 16:54:00 +0100387 blkg->stats.unaccounted_time += unaccounted_time;
Vivek Goyala23e6862011-05-19 15:38:20 -0400388#endif
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700389 spin_unlock_irqrestore(&blkg->stats_lock, flags);
Vivek Goyal22084192009-12-03 12:59:49 -0500390}
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700391EXPORT_SYMBOL_GPL(blkiocg_update_timeslice_used);
Vivek Goyal22084192009-12-03 12:59:49 -0500392
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400393/*
394 * should be called under rcu read lock or queue lock to make sure blkg pointer
395 * is valid.
396 */
Divyesh Shah84c124d2010-04-09 08:31:19 +0200397void blkiocg_update_dispatch_stats(struct blkio_group *blkg,
398 uint64_t bytes, bool direction, bool sync)
Divyesh Shah91952912010-04-01 15:01:41 -0700399{
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400400 struct blkio_group_stats_cpu *stats_cpu;
Vivek Goyal575969a2011-05-19 15:38:29 -0400401 unsigned long flags;
402
403 /*
404 * Disabling interrupts to provide mutual exclusion between two
405 * writes on same cpu. It probably is not needed for 64bit. Not
406 * optimizing that case yet.
407 */
408 local_irq_save(flags);
Divyesh Shah91952912010-04-01 15:01:41 -0700409
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400410 stats_cpu = this_cpu_ptr(blkg->stats_cpu);
411
Vivek Goyal575969a2011-05-19 15:38:29 -0400412 u64_stats_update_begin(&stats_cpu->syncp);
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400413 stats_cpu->sectors += bytes >> 9;
414 blkio_add_stat(stats_cpu->stat_arr_cpu[BLKIO_STAT_CPU_SERVICED],
415 1, direction, sync);
416 blkio_add_stat(stats_cpu->stat_arr_cpu[BLKIO_STAT_CPU_SERVICE_BYTES],
417 bytes, direction, sync);
Vivek Goyal575969a2011-05-19 15:38:29 -0400418 u64_stats_update_end(&stats_cpu->syncp);
419 local_irq_restore(flags);
Divyesh Shah91952912010-04-01 15:01:41 -0700420}
Divyesh Shah84c124d2010-04-09 08:31:19 +0200421EXPORT_SYMBOL_GPL(blkiocg_update_dispatch_stats);
Divyesh Shah91952912010-04-01 15:01:41 -0700422
Divyesh Shah84c124d2010-04-09 08:31:19 +0200423void blkiocg_update_completion_stats(struct blkio_group *blkg,
424 uint64_t start_time, uint64_t io_start_time, bool direction, bool sync)
Divyesh Shah91952912010-04-01 15:01:41 -0700425{
426 struct blkio_group_stats *stats;
427 unsigned long flags;
428 unsigned long long now = sched_clock();
429
430 spin_lock_irqsave(&blkg->stats_lock, flags);
431 stats = &blkg->stats;
Divyesh Shah84c124d2010-04-09 08:31:19 +0200432 if (time_after64(now, io_start_time))
433 blkio_add_stat(stats->stat_arr[BLKIO_STAT_SERVICE_TIME],
434 now - io_start_time, direction, sync);
435 if (time_after64(io_start_time, start_time))
436 blkio_add_stat(stats->stat_arr[BLKIO_STAT_WAIT_TIME],
437 io_start_time - start_time, direction, sync);
Divyesh Shah91952912010-04-01 15:01:41 -0700438 spin_unlock_irqrestore(&blkg->stats_lock, flags);
439}
Divyesh Shah84c124d2010-04-09 08:31:19 +0200440EXPORT_SYMBOL_GPL(blkiocg_update_completion_stats);
Divyesh Shah91952912010-04-01 15:01:41 -0700441
Vivek Goyal317389a2011-05-23 10:02:19 +0200442/* Merged stats are per cpu. */
Divyesh Shah812d4022010-04-08 21:14:23 -0700443void blkiocg_update_io_merged_stats(struct blkio_group *blkg, bool direction,
444 bool sync)
445{
Vivek Goyal317389a2011-05-23 10:02:19 +0200446 struct blkio_group_stats_cpu *stats_cpu;
Divyesh Shah812d4022010-04-08 21:14:23 -0700447 unsigned long flags;
448
Vivek Goyal317389a2011-05-23 10:02:19 +0200449 /*
450 * Disabling interrupts to provide mutual exclusion between two
451 * writes on same cpu. It probably is not needed for 64bit. Not
452 * optimizing that case yet.
453 */
454 local_irq_save(flags);
455
456 stats_cpu = this_cpu_ptr(blkg->stats_cpu);
457
458 u64_stats_update_begin(&stats_cpu->syncp);
459 blkio_add_stat(stats_cpu->stat_arr_cpu[BLKIO_STAT_CPU_MERGED], 1,
460 direction, sync);
461 u64_stats_update_end(&stats_cpu->syncp);
462 local_irq_restore(flags);
Divyesh Shah812d4022010-04-08 21:14:23 -0700463}
464EXPORT_SYMBOL_GPL(blkiocg_update_io_merged_stats);
465
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400466/*
467 * This function allocates the per cpu stats for blkio_group. Should be called
468 * from sleepable context as alloc_per_cpu() requires that.
469 */
470int blkio_alloc_blkg_stats(struct blkio_group *blkg)
471{
472 /* Allocate memory for per cpu stats */
473 blkg->stats_cpu = alloc_percpu(struct blkio_group_stats_cpu);
474 if (!blkg->stats_cpu)
475 return -ENOMEM;
476 return 0;
477}
478EXPORT_SYMBOL_GPL(blkio_alloc_blkg_stats);
479
Vivek Goyal31e4c282009-12-03 12:59:42 -0500480void blkiocg_add_blkio_group(struct blkio_cgroup *blkcg,
Vivek Goyal062a6442010-09-15 17:06:33 -0400481 struct blkio_group *blkg, void *key, dev_t dev,
482 enum blkio_policy_id plid)
Vivek Goyal31e4c282009-12-03 12:59:42 -0500483{
484 unsigned long flags;
485
486 spin_lock_irqsave(&blkcg->lock, flags);
Divyesh Shah8d2a91f2010-04-16 08:10:51 +0200487 spin_lock_init(&blkg->stats_lock);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500488 rcu_assign_pointer(blkg->key, key);
Vivek Goyalb1c35762009-12-03 12:59:47 -0500489 blkg->blkcg_id = css_id(&blkcg->css);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500490 hlist_add_head_rcu(&blkg->blkcg_node, &blkcg->blkg_list);
Vivek Goyal062a6442010-09-15 17:06:33 -0400491 blkg->plid = plid;
Vivek Goyal31e4c282009-12-03 12:59:42 -0500492 spin_unlock_irqrestore(&blkcg->lock, flags);
Vivek Goyal2868ef72009-12-03 12:59:48 -0500493 /* Need to take css reference ? */
494 cgroup_path(blkcg->css.cgroup, blkg->path, sizeof(blkg->path));
Vivek Goyal22084192009-12-03 12:59:49 -0500495 blkg->dev = dev;
Vivek Goyal31e4c282009-12-03 12:59:42 -0500496}
Vivek Goyal9d6a9862009-12-04 10:36:41 -0500497EXPORT_SYMBOL_GPL(blkiocg_add_blkio_group);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500498
Vivek Goyalb1c35762009-12-03 12:59:47 -0500499static void __blkiocg_del_blkio_group(struct blkio_group *blkg)
500{
501 hlist_del_init_rcu(&blkg->blkcg_node);
502 blkg->blkcg_id = 0;
503}
504
505/*
506 * returns 0 if blkio_group was still on cgroup list. Otherwise returns 1
507 * indicating that blk_group was unhashed by the time we got to it.
508 */
Vivek Goyal31e4c282009-12-03 12:59:42 -0500509int blkiocg_del_blkio_group(struct blkio_group *blkg)
510{
Vivek Goyalb1c35762009-12-03 12:59:47 -0500511 struct blkio_cgroup *blkcg;
512 unsigned long flags;
513 struct cgroup_subsys_state *css;
514 int ret = 1;
515
516 rcu_read_lock();
517 css = css_lookup(&blkio_subsys, blkg->blkcg_id);
Jens Axboe0f3942a2010-05-03 14:28:55 +0200518 if (css) {
519 blkcg = container_of(css, struct blkio_cgroup, css);
520 spin_lock_irqsave(&blkcg->lock, flags);
521 if (!hlist_unhashed(&blkg->blkcg_node)) {
522 __blkiocg_del_blkio_group(blkg);
523 ret = 0;
524 }
525 spin_unlock_irqrestore(&blkcg->lock, flags);
Vivek Goyalb1c35762009-12-03 12:59:47 -0500526 }
Jens Axboe0f3942a2010-05-03 14:28:55 +0200527
Vivek Goyalb1c35762009-12-03 12:59:47 -0500528 rcu_read_unlock();
529 return ret;
Vivek Goyal31e4c282009-12-03 12:59:42 -0500530}
Vivek Goyal9d6a9862009-12-04 10:36:41 -0500531EXPORT_SYMBOL_GPL(blkiocg_del_blkio_group);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500532
533/* called under rcu_read_lock(). */
534struct blkio_group *blkiocg_lookup_group(struct blkio_cgroup *blkcg, void *key)
535{
536 struct blkio_group *blkg;
537 struct hlist_node *n;
538 void *__key;
539
540 hlist_for_each_entry_rcu(blkg, n, &blkcg->blkg_list, blkcg_node) {
541 __key = blkg->key;
542 if (__key == key)
543 return blkg;
544 }
545
546 return NULL;
547}
Vivek Goyal9d6a9862009-12-04 10:36:41 -0500548EXPORT_SYMBOL_GPL(blkiocg_lookup_group);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500549
Vivek Goyalf0bdc8c2011-05-19 15:38:30 -0400550static void blkio_reset_stats_cpu(struct blkio_group *blkg)
551{
552 struct blkio_group_stats_cpu *stats_cpu;
553 int i, j, k;
554 /*
555 * Note: On 64 bit arch this should not be an issue. This has the
556 * possibility of returning some inconsistent value on 32bit arch
557 * as 64bit update on 32bit is non atomic. Taking care of this
558 * corner case makes code very complicated, like sending IPIs to
559 * cpus, taking care of stats of offline cpus etc.
560 *
561 * reset stats is anyway more of a debug feature and this sounds a
562 * corner case. So I am not complicating the code yet until and
563 * unless this becomes a real issue.
564 */
565 for_each_possible_cpu(i) {
566 stats_cpu = per_cpu_ptr(blkg->stats_cpu, i);
567 stats_cpu->sectors = 0;
568 for(j = 0; j < BLKIO_STAT_CPU_NR; j++)
569 for (k = 0; k < BLKIO_STAT_TOTAL; k++)
570 stats_cpu->stat_arr_cpu[j][k] = 0;
571 }
572}
573
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700574static int
Divyesh Shah84c124d2010-04-09 08:31:19 +0200575blkiocg_reset_stats(struct cgroup *cgroup, struct cftype *cftype, u64 val)
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700576{
577 struct blkio_cgroup *blkcg;
578 struct blkio_group *blkg;
Divyesh Shah812df482010-04-08 21:15:35 -0700579 struct blkio_group_stats *stats;
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700580 struct hlist_node *n;
Divyesh Shahcdc11842010-04-08 21:15:10 -0700581 uint64_t queued[BLKIO_STAT_TOTAL];
582 int i;
Divyesh Shah812df482010-04-08 21:15:35 -0700583#ifdef CONFIG_DEBUG_BLK_CGROUP
584 bool idling, waiting, empty;
585 unsigned long long now = sched_clock();
586#endif
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700587
588 blkcg = cgroup_to_blkio_cgroup(cgroup);
589 spin_lock_irq(&blkcg->lock);
590 hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) {
591 spin_lock(&blkg->stats_lock);
Divyesh Shah812df482010-04-08 21:15:35 -0700592 stats = &blkg->stats;
593#ifdef CONFIG_DEBUG_BLK_CGROUP
594 idling = blkio_blkg_idling(stats);
595 waiting = blkio_blkg_waiting(stats);
596 empty = blkio_blkg_empty(stats);
597#endif
Divyesh Shahcdc11842010-04-08 21:15:10 -0700598 for (i = 0; i < BLKIO_STAT_TOTAL; i++)
Divyesh Shah812df482010-04-08 21:15:35 -0700599 queued[i] = stats->stat_arr[BLKIO_STAT_QUEUED][i];
600 memset(stats, 0, sizeof(struct blkio_group_stats));
Divyesh Shahcdc11842010-04-08 21:15:10 -0700601 for (i = 0; i < BLKIO_STAT_TOTAL; i++)
Divyesh Shah812df482010-04-08 21:15:35 -0700602 stats->stat_arr[BLKIO_STAT_QUEUED][i] = queued[i];
603#ifdef CONFIG_DEBUG_BLK_CGROUP
604 if (idling) {
605 blkio_mark_blkg_idling(stats);
606 stats->start_idle_time = now;
607 }
608 if (waiting) {
609 blkio_mark_blkg_waiting(stats);
610 stats->start_group_wait_time = now;
611 }
612 if (empty) {
613 blkio_mark_blkg_empty(stats);
614 stats->start_empty_time = now;
615 }
616#endif
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700617 spin_unlock(&blkg->stats_lock);
Vivek Goyalf0bdc8c2011-05-19 15:38:30 -0400618
619 /* Reset Per cpu stats which don't take blkg->stats_lock */
620 blkio_reset_stats_cpu(blkg);
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700621 }
Vivek Goyalf0bdc8c2011-05-19 15:38:30 -0400622
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700623 spin_unlock_irq(&blkcg->lock);
624 return 0;
625}
626
Divyesh Shah84c124d2010-04-09 08:31:19 +0200627static void blkio_get_key_name(enum stat_sub_type type, dev_t dev, char *str,
628 int chars_left, bool diskname_only)
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700629{
Divyesh Shah84c124d2010-04-09 08:31:19 +0200630 snprintf(str, chars_left, "%d:%d", MAJOR(dev), MINOR(dev));
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700631 chars_left -= strlen(str);
632 if (chars_left <= 0) {
633 printk(KERN_WARNING
634 "Possibly incorrect cgroup stat display format");
635 return;
636 }
Divyesh Shah84c124d2010-04-09 08:31:19 +0200637 if (diskname_only)
638 return;
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700639 switch (type) {
Divyesh Shah84c124d2010-04-09 08:31:19 +0200640 case BLKIO_STAT_READ:
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700641 strlcat(str, " Read", chars_left);
642 break;
Divyesh Shah84c124d2010-04-09 08:31:19 +0200643 case BLKIO_STAT_WRITE:
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700644 strlcat(str, " Write", chars_left);
645 break;
Divyesh Shah84c124d2010-04-09 08:31:19 +0200646 case BLKIO_STAT_SYNC:
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700647 strlcat(str, " Sync", chars_left);
648 break;
Divyesh Shah84c124d2010-04-09 08:31:19 +0200649 case BLKIO_STAT_ASYNC:
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700650 strlcat(str, " Async", chars_left);
651 break;
Divyesh Shah84c124d2010-04-09 08:31:19 +0200652 case BLKIO_STAT_TOTAL:
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700653 strlcat(str, " Total", chars_left);
654 break;
655 default:
656 strlcat(str, " Invalid", chars_left);
657 }
658}
659
Divyesh Shah84c124d2010-04-09 08:31:19 +0200660static uint64_t blkio_fill_stat(char *str, int chars_left, uint64_t val,
661 struct cgroup_map_cb *cb, dev_t dev)
662{
663 blkio_get_key_name(0, dev, str, chars_left, true);
664 cb->fill(cb, str, val);
665 return val;
666}
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700667
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400668
669static uint64_t blkio_read_stat_cpu(struct blkio_group *blkg,
670 enum stat_type_cpu type, enum stat_sub_type sub_type)
671{
672 int cpu;
673 struct blkio_group_stats_cpu *stats_cpu;
Vivek Goyal575969a2011-05-19 15:38:29 -0400674 u64 val = 0, tval;
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400675
676 for_each_possible_cpu(cpu) {
Vivek Goyal575969a2011-05-19 15:38:29 -0400677 unsigned int start;
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400678 stats_cpu = per_cpu_ptr(blkg->stats_cpu, cpu);
679
Vivek Goyal575969a2011-05-19 15:38:29 -0400680 do {
681 start = u64_stats_fetch_begin(&stats_cpu->syncp);
682 if (type == BLKIO_STAT_CPU_SECTORS)
683 tval = stats_cpu->sectors;
684 else
685 tval = stats_cpu->stat_arr_cpu[type][sub_type];
686 } while(u64_stats_fetch_retry(&stats_cpu->syncp, start));
687
688 val += tval;
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400689 }
690
691 return val;
692}
693
694static uint64_t blkio_get_stat_cpu(struct blkio_group *blkg,
695 struct cgroup_map_cb *cb, dev_t dev, enum stat_type_cpu type)
696{
697 uint64_t disk_total, val;
698 char key_str[MAX_KEY_LEN];
699 enum stat_sub_type sub_type;
700
701 if (type == BLKIO_STAT_CPU_SECTORS) {
702 val = blkio_read_stat_cpu(blkg, type, 0);
703 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1, val, cb, dev);
704 }
705
706 for (sub_type = BLKIO_STAT_READ; sub_type < BLKIO_STAT_TOTAL;
707 sub_type++) {
708 blkio_get_key_name(sub_type, dev, key_str, MAX_KEY_LEN, false);
709 val = blkio_read_stat_cpu(blkg, type, sub_type);
710 cb->fill(cb, key_str, val);
711 }
712
713 disk_total = blkio_read_stat_cpu(blkg, type, BLKIO_STAT_READ) +
714 blkio_read_stat_cpu(blkg, type, BLKIO_STAT_WRITE);
715
716 blkio_get_key_name(BLKIO_STAT_TOTAL, dev, key_str, MAX_KEY_LEN, false);
717 cb->fill(cb, key_str, disk_total);
718 return disk_total;
719}
720
Divyesh Shah84c124d2010-04-09 08:31:19 +0200721/* This should be called with blkg->stats_lock held */
722static uint64_t blkio_get_stat(struct blkio_group *blkg,
723 struct cgroup_map_cb *cb, dev_t dev, enum stat_type type)
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700724{
725 uint64_t disk_total;
726 char key_str[MAX_KEY_LEN];
Divyesh Shah84c124d2010-04-09 08:31:19 +0200727 enum stat_sub_type sub_type;
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700728
Divyesh Shah84c124d2010-04-09 08:31:19 +0200729 if (type == BLKIO_STAT_TIME)
730 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
731 blkg->stats.time, cb, dev);
Justin TerAvest9026e522011-03-22 21:26:54 +0100732#ifdef CONFIG_DEBUG_BLK_CGROUP
Justin TerAvest167400d2011-03-12 16:54:00 +0100733 if (type == BLKIO_STAT_UNACCOUNTED_TIME)
734 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
735 blkg->stats.unaccounted_time, cb, dev);
Divyesh Shahcdc11842010-04-08 21:15:10 -0700736 if (type == BLKIO_STAT_AVG_QUEUE_SIZE) {
737 uint64_t sum = blkg->stats.avg_queue_size_sum;
738 uint64_t samples = blkg->stats.avg_queue_size_samples;
739 if (samples)
740 do_div(sum, samples);
741 else
742 sum = 0;
743 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1, sum, cb, dev);
744 }
Divyesh Shah812df482010-04-08 21:15:35 -0700745 if (type == BLKIO_STAT_GROUP_WAIT_TIME)
746 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
747 blkg->stats.group_wait_time, cb, dev);
748 if (type == BLKIO_STAT_IDLE_TIME)
749 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
750 blkg->stats.idle_time, cb, dev);
751 if (type == BLKIO_STAT_EMPTY_TIME)
752 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
753 blkg->stats.empty_time, cb, dev);
Divyesh Shah84c124d2010-04-09 08:31:19 +0200754 if (type == BLKIO_STAT_DEQUEUE)
755 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
756 blkg->stats.dequeue, cb, dev);
757#endif
758
759 for (sub_type = BLKIO_STAT_READ; sub_type < BLKIO_STAT_TOTAL;
760 sub_type++) {
761 blkio_get_key_name(sub_type, dev, key_str, MAX_KEY_LEN, false);
762 cb->fill(cb, key_str, blkg->stats.stat_arr[type][sub_type]);
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700763 }
Divyesh Shah84c124d2010-04-09 08:31:19 +0200764 disk_total = blkg->stats.stat_arr[type][BLKIO_STAT_READ] +
765 blkg->stats.stat_arr[type][BLKIO_STAT_WRITE];
766 blkio_get_key_name(BLKIO_STAT_TOTAL, dev, key_str, MAX_KEY_LEN, false);
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700767 cb->fill(cb, key_str, disk_total);
768 return disk_total;
769}
770
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800771static int blkio_policy_parse_and_set(char *buf,
Vivek Goyal062a6442010-09-15 17:06:33 -0400772 struct blkio_policy_node *newpn, enum blkio_policy_id plid, int fileid)
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800773{
Tejun Heoece84242011-10-19 14:31:15 +0200774 struct gendisk *disk = NULL;
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800775 char *s[4], *p, *major_s = NULL, *minor_s = NULL;
Wanlong Gaod11bb442011-09-21 10:22:10 +0200776 unsigned long major, minor;
Tejun Heoece84242011-10-19 14:31:15 +0200777 int i = 0, ret = -EINVAL;
778 int part;
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800779 dev_t dev;
Wanlong Gaod11bb442011-09-21 10:22:10 +0200780 u64 temp;
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800781
782 memset(s, 0, sizeof(s));
783
784 while ((p = strsep(&buf, " ")) != NULL) {
785 if (!*p)
786 continue;
787
788 s[i++] = p;
789
790 /* Prevent from inputing too many things */
791 if (i == 3)
792 break;
793 }
794
795 if (i != 2)
Tejun Heoece84242011-10-19 14:31:15 +0200796 goto out;
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800797
798 p = strsep(&s[0], ":");
799 if (p != NULL)
800 major_s = p;
801 else
Tejun Heoece84242011-10-19 14:31:15 +0200802 goto out;
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800803
804 minor_s = s[0];
805 if (!minor_s)
Tejun Heoece84242011-10-19 14:31:15 +0200806 goto out;
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800807
Tejun Heoece84242011-10-19 14:31:15 +0200808 if (strict_strtoul(major_s, 10, &major))
809 goto out;
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800810
Tejun Heoece84242011-10-19 14:31:15 +0200811 if (strict_strtoul(minor_s, 10, &minor))
812 goto out;
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800813
814 dev = MKDEV(major, minor);
815
Tejun Heoece84242011-10-19 14:31:15 +0200816 if (strict_strtoull(s[1], 10, &temp))
817 goto out;
Wanlong Gaod11bb442011-09-21 10:22:10 +0200818
819 /* For rule removal, do not check for device presence. */
820 if (temp) {
Tejun Heoece84242011-10-19 14:31:15 +0200821 disk = get_gendisk(dev, &part);
822 if (!disk || part) {
823 ret = -ENODEV;
824 goto out;
825 }
Wanlong Gaod11bb442011-09-21 10:22:10 +0200826 }
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800827
828 newpn->dev = dev;
829
Vivek Goyal062a6442010-09-15 17:06:33 -0400830 switch (plid) {
831 case BLKIO_POLICY_PROP:
Wanlong Gaod11bb442011-09-21 10:22:10 +0200832 if ((temp < BLKIO_WEIGHT_MIN && temp > 0) ||
833 temp > BLKIO_WEIGHT_MAX)
Tejun Heoece84242011-10-19 14:31:15 +0200834 goto out;
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800835
Vivek Goyal062a6442010-09-15 17:06:33 -0400836 newpn->plid = plid;
837 newpn->fileid = fileid;
Vivek Goyal4c9eefa2010-09-15 17:06:34 -0400838 newpn->val.weight = temp;
839 break;
840 case BLKIO_POLICY_THROTL:
Vivek Goyal7702e8f2010-09-15 17:06:36 -0400841 switch(fileid) {
842 case BLKIO_THROTL_read_bps_device:
843 case BLKIO_THROTL_write_bps_device:
Vivek Goyal7702e8f2010-09-15 17:06:36 -0400844 newpn->plid = plid;
845 newpn->fileid = fileid;
Wanlong Gaod11bb442011-09-21 10:22:10 +0200846 newpn->val.bps = temp;
Vivek Goyal7702e8f2010-09-15 17:06:36 -0400847 break;
848 case BLKIO_THROTL_read_iops_device:
849 case BLKIO_THROTL_write_iops_device:
Wanlong Gaod11bb442011-09-21 10:22:10 +0200850 if (temp > THROTL_IOPS_MAX)
Tejun Heoece84242011-10-19 14:31:15 +0200851 goto out;
Vivek Goyal9355aed2010-10-01 21:16:41 +0200852
Vivek Goyal7702e8f2010-09-15 17:06:36 -0400853 newpn->plid = plid;
854 newpn->fileid = fileid;
Wanlong Gaod11bb442011-09-21 10:22:10 +0200855 newpn->val.iops = (unsigned int)temp;
Vivek Goyal7702e8f2010-09-15 17:06:36 -0400856 break;
857 }
Vivek Goyal062a6442010-09-15 17:06:33 -0400858 break;
859 default:
860 BUG();
861 }
Tejun Heoece84242011-10-19 14:31:15 +0200862 ret = 0;
863out:
864 put_disk(disk);
865 return ret;
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800866}
867
868unsigned int blkcg_get_weight(struct blkio_cgroup *blkcg,
869 dev_t dev)
870{
871 struct blkio_policy_node *pn;
872
Vivek Goyal062a6442010-09-15 17:06:33 -0400873 pn = blkio_policy_search_node(blkcg, dev, BLKIO_POLICY_PROP,
874 BLKIO_PROP_weight_device);
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800875 if (pn)
Vivek Goyal4c9eefa2010-09-15 17:06:34 -0400876 return pn->val.weight;
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800877 else
878 return blkcg->weight;
879}
880EXPORT_SYMBOL_GPL(blkcg_get_weight);
881
Vivek Goyal4c9eefa2010-09-15 17:06:34 -0400882uint64_t blkcg_get_read_bps(struct blkio_cgroup *blkcg, dev_t dev)
883{
884 struct blkio_policy_node *pn;
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800885
Vivek Goyal4c9eefa2010-09-15 17:06:34 -0400886 pn = blkio_policy_search_node(blkcg, dev, BLKIO_POLICY_THROTL,
887 BLKIO_THROTL_read_bps_device);
888 if (pn)
889 return pn->val.bps;
890 else
891 return -1;
892}
893
894uint64_t blkcg_get_write_bps(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_bps_device);
899 if (pn)
900 return pn->val.bps;
901 else
902 return -1;
903}
904
Vivek Goyal7702e8f2010-09-15 17:06:36 -0400905unsigned int blkcg_get_read_iops(struct blkio_cgroup *blkcg, dev_t dev)
906{
907 struct blkio_policy_node *pn;
908
909 pn = blkio_policy_search_node(blkcg, dev, BLKIO_POLICY_THROTL,
910 BLKIO_THROTL_read_iops_device);
911 if (pn)
912 return pn->val.iops;
913 else
914 return -1;
915}
916
917unsigned int blkcg_get_write_iops(struct blkio_cgroup *blkcg, dev_t dev)
918{
919 struct blkio_policy_node *pn;
920 pn = blkio_policy_search_node(blkcg, dev, BLKIO_POLICY_THROTL,
921 BLKIO_THROTL_write_iops_device);
922 if (pn)
923 return pn->val.iops;
924 else
925 return -1;
926}
927
Vivek Goyal062a6442010-09-15 17:06:33 -0400928/* Checks whether user asked for deleting a policy rule */
929static bool blkio_delete_rule_command(struct blkio_policy_node *pn)
930{
931 switch(pn->plid) {
932 case BLKIO_POLICY_PROP:
Vivek Goyal4c9eefa2010-09-15 17:06:34 -0400933 if (pn->val.weight == 0)
934 return 1;
935 break;
936 case BLKIO_POLICY_THROTL:
Vivek Goyal7702e8f2010-09-15 17:06:36 -0400937 switch(pn->fileid) {
938 case BLKIO_THROTL_read_bps_device:
939 case BLKIO_THROTL_write_bps_device:
940 if (pn->val.bps == 0)
941 return 1;
942 break;
943 case BLKIO_THROTL_read_iops_device:
944 case BLKIO_THROTL_write_iops_device:
945 if (pn->val.iops == 0)
946 return 1;
947 }
Vivek Goyal062a6442010-09-15 17:06:33 -0400948 break;
949 default:
950 BUG();
951 }
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800952
Vivek Goyal062a6442010-09-15 17:06:33 -0400953 return 0;
954}
955
956static void blkio_update_policy_rule(struct blkio_policy_node *oldpn,
957 struct blkio_policy_node *newpn)
958{
959 switch(oldpn->plid) {
960 case BLKIO_POLICY_PROP:
Vivek Goyal4c9eefa2010-09-15 17:06:34 -0400961 oldpn->val.weight = newpn->val.weight;
962 break;
963 case BLKIO_POLICY_THROTL:
Vivek Goyal7702e8f2010-09-15 17:06:36 -0400964 switch(newpn->fileid) {
965 case BLKIO_THROTL_read_bps_device:
966 case BLKIO_THROTL_write_bps_device:
967 oldpn->val.bps = newpn->val.bps;
968 break;
969 case BLKIO_THROTL_read_iops_device:
970 case BLKIO_THROTL_write_iops_device:
971 oldpn->val.iops = newpn->val.iops;
972 }
Vivek Goyal062a6442010-09-15 17:06:33 -0400973 break;
974 default:
975 BUG();
976 }
977}
978
979/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300980 * Some rules/values in blkg have changed. Propagate those to respective
Vivek Goyal062a6442010-09-15 17:06:33 -0400981 * policies.
982 */
983static void blkio_update_blkg_policy(struct blkio_cgroup *blkcg,
984 struct blkio_group *blkg, struct blkio_policy_node *pn)
985{
Vivek Goyal7702e8f2010-09-15 17:06:36 -0400986 unsigned int weight, iops;
Vivek Goyal4c9eefa2010-09-15 17:06:34 -0400987 u64 bps;
Vivek Goyal062a6442010-09-15 17:06:33 -0400988
989 switch(pn->plid) {
990 case BLKIO_POLICY_PROP:
Vivek Goyal4c9eefa2010-09-15 17:06:34 -0400991 weight = pn->val.weight ? pn->val.weight :
Vivek Goyal062a6442010-09-15 17:06:33 -0400992 blkcg->weight;
993 blkio_update_group_weight(blkg, weight);
994 break;
Vivek Goyal4c9eefa2010-09-15 17:06:34 -0400995 case BLKIO_POLICY_THROTL:
996 switch(pn->fileid) {
997 case BLKIO_THROTL_read_bps_device:
998 case BLKIO_THROTL_write_bps_device:
999 bps = pn->val.bps ? pn->val.bps : (-1);
1000 blkio_update_group_bps(blkg, bps, pn->fileid);
1001 break;
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001002 case BLKIO_THROTL_read_iops_device:
1003 case BLKIO_THROTL_write_iops_device:
1004 iops = pn->val.iops ? pn->val.iops : (-1);
1005 blkio_update_group_iops(blkg, iops, pn->fileid);
1006 break;
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001007 }
1008 break;
Vivek Goyal062a6442010-09-15 17:06:33 -04001009 default:
1010 BUG();
1011 }
1012}
1013
1014/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001015 * A policy node rule has been updated. Propagate this update to all the
Vivek Goyal062a6442010-09-15 17:06:33 -04001016 * block groups which might be affected by this update.
1017 */
1018static void blkio_update_policy_node_blkg(struct blkio_cgroup *blkcg,
1019 struct blkio_policy_node *pn)
1020{
1021 struct blkio_group *blkg;
1022 struct hlist_node *n;
1023
1024 spin_lock(&blkio_list_lock);
1025 spin_lock_irq(&blkcg->lock);
1026
1027 hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) {
1028 if (pn->dev != blkg->dev || pn->plid != blkg->plid)
1029 continue;
1030 blkio_update_blkg_policy(blkcg, blkg, pn);
1031 }
1032
1033 spin_unlock_irq(&blkcg->lock);
1034 spin_unlock(&blkio_list_lock);
1035}
1036
1037static int blkiocg_file_write(struct cgroup *cgrp, struct cftype *cft,
1038 const char *buffer)
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001039{
1040 int ret = 0;
1041 char *buf;
1042 struct blkio_policy_node *newpn, *pn;
1043 struct blkio_cgroup *blkcg;
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001044 int keep_newpn = 0;
Vivek Goyal062a6442010-09-15 17:06:33 -04001045 enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
1046 int fileid = BLKIOFILE_ATTR(cft->private);
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001047
1048 buf = kstrdup(buffer, GFP_KERNEL);
1049 if (!buf)
1050 return -ENOMEM;
1051
1052 newpn = kzalloc(sizeof(*newpn), GFP_KERNEL);
1053 if (!newpn) {
1054 ret = -ENOMEM;
1055 goto free_buf;
1056 }
1057
Vivek Goyal062a6442010-09-15 17:06:33 -04001058 ret = blkio_policy_parse_and_set(buf, newpn, plid, fileid);
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001059 if (ret)
1060 goto free_newpn;
1061
1062 blkcg = cgroup_to_blkio_cgroup(cgrp);
1063
1064 spin_lock_irq(&blkcg->lock);
1065
Vivek Goyal062a6442010-09-15 17:06:33 -04001066 pn = blkio_policy_search_node(blkcg, newpn->dev, plid, fileid);
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001067 if (!pn) {
Vivek Goyal062a6442010-09-15 17:06:33 -04001068 if (!blkio_delete_rule_command(newpn)) {
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001069 blkio_policy_insert_node(blkcg, newpn);
1070 keep_newpn = 1;
1071 }
1072 spin_unlock_irq(&blkcg->lock);
1073 goto update_io_group;
1074 }
1075
Vivek Goyal062a6442010-09-15 17:06:33 -04001076 if (blkio_delete_rule_command(newpn)) {
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001077 blkio_policy_delete_node(pn);
Vivek Goyale060f002011-10-25 15:48:12 +02001078 kfree(pn);
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001079 spin_unlock_irq(&blkcg->lock);
1080 goto update_io_group;
1081 }
1082 spin_unlock_irq(&blkcg->lock);
1083
Vivek Goyal062a6442010-09-15 17:06:33 -04001084 blkio_update_policy_rule(pn, newpn);
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001085
1086update_io_group:
Vivek Goyal062a6442010-09-15 17:06:33 -04001087 blkio_update_policy_node_blkg(blkcg, newpn);
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001088
1089free_newpn:
1090 if (!keep_newpn)
1091 kfree(newpn);
1092free_buf:
1093 kfree(buf);
1094 return ret;
1095}
1096
Vivek Goyal062a6442010-09-15 17:06:33 -04001097static void
1098blkio_print_policy_node(struct seq_file *m, struct blkio_policy_node *pn)
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001099{
Vivek Goyal062a6442010-09-15 17:06:33 -04001100 switch(pn->plid) {
1101 case BLKIO_POLICY_PROP:
1102 if (pn->fileid == BLKIO_PROP_weight_device)
1103 seq_printf(m, "%u:%u\t%u\n", MAJOR(pn->dev),
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001104 MINOR(pn->dev), pn->val.weight);
1105 break;
1106 case BLKIO_POLICY_THROTL:
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001107 switch(pn->fileid) {
1108 case BLKIO_THROTL_read_bps_device:
1109 case BLKIO_THROTL_write_bps_device:
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001110 seq_printf(m, "%u:%u\t%llu\n", MAJOR(pn->dev),
1111 MINOR(pn->dev), pn->val.bps);
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001112 break;
1113 case BLKIO_THROTL_read_iops_device:
1114 case BLKIO_THROTL_write_iops_device:
1115 seq_printf(m, "%u:%u\t%u\n", MAJOR(pn->dev),
1116 MINOR(pn->dev), pn->val.iops);
1117 break;
1118 }
Vivek Goyal062a6442010-09-15 17:06:33 -04001119 break;
1120 default:
1121 BUG();
1122 }
1123}
1124
1125/* cgroup files which read their data from policy nodes end up here */
1126static void blkio_read_policy_node_files(struct cftype *cft,
1127 struct blkio_cgroup *blkcg, struct seq_file *m)
1128{
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001129 struct blkio_policy_node *pn;
1130
Jens Axboe0f3942a2010-05-03 14:28:55 +02001131 if (!list_empty(&blkcg->policy_list)) {
1132 spin_lock_irq(&blkcg->lock);
1133 list_for_each_entry(pn, &blkcg->policy_list, node) {
Vivek Goyal062a6442010-09-15 17:06:33 -04001134 if (!pn_matches_cftype(cft, pn))
1135 continue;
1136 blkio_print_policy_node(m, pn);
Jens Axboe0f3942a2010-05-03 14:28:55 +02001137 }
1138 spin_unlock_irq(&blkcg->lock);
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001139 }
Vivek Goyal062a6442010-09-15 17:06:33 -04001140}
1141
1142static int blkiocg_file_read(struct cgroup *cgrp, struct cftype *cft,
1143 struct seq_file *m)
1144{
1145 struct blkio_cgroup *blkcg;
1146 enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
1147 int name = BLKIOFILE_ATTR(cft->private);
1148
1149 blkcg = cgroup_to_blkio_cgroup(cgrp);
1150
1151 switch(plid) {
1152 case BLKIO_POLICY_PROP:
1153 switch(name) {
1154 case BLKIO_PROP_weight_device:
1155 blkio_read_policy_node_files(cft, blkcg, m);
1156 return 0;
1157 default:
1158 BUG();
1159 }
1160 break;
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001161 case BLKIO_POLICY_THROTL:
1162 switch(name){
1163 case BLKIO_THROTL_read_bps_device:
1164 case BLKIO_THROTL_write_bps_device:
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001165 case BLKIO_THROTL_read_iops_device:
1166 case BLKIO_THROTL_write_iops_device:
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001167 blkio_read_policy_node_files(cft, blkcg, m);
1168 return 0;
1169 default:
1170 BUG();
1171 }
1172 break;
Vivek Goyal062a6442010-09-15 17:06:33 -04001173 default:
1174 BUG();
1175 }
1176
1177 return 0;
1178}
1179
1180static int blkio_read_blkg_stats(struct blkio_cgroup *blkcg,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001181 struct cftype *cft, struct cgroup_map_cb *cb,
1182 enum stat_type type, bool show_total, bool pcpu)
Vivek Goyal062a6442010-09-15 17:06:33 -04001183{
1184 struct blkio_group *blkg;
1185 struct hlist_node *n;
1186 uint64_t cgroup_total = 0;
1187
1188 rcu_read_lock();
1189 hlist_for_each_entry_rcu(blkg, n, &blkcg->blkg_list, blkcg_node) {
1190 if (blkg->dev) {
1191 if (!cftype_blkg_same_policy(cft, blkg))
1192 continue;
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001193 if (pcpu)
1194 cgroup_total += blkio_get_stat_cpu(blkg, cb,
1195 blkg->dev, type);
1196 else {
1197 spin_lock_irq(&blkg->stats_lock);
1198 cgroup_total += blkio_get_stat(blkg, cb,
1199 blkg->dev, type);
1200 spin_unlock_irq(&blkg->stats_lock);
1201 }
Vivek Goyal062a6442010-09-15 17:06:33 -04001202 }
1203 }
1204 if (show_total)
1205 cb->fill(cb, "Total", cgroup_total);
1206 rcu_read_unlock();
1207 return 0;
1208}
1209
1210/* All map kind of cgroup file get serviced by this function */
1211static int blkiocg_file_read_map(struct cgroup *cgrp, struct cftype *cft,
1212 struct cgroup_map_cb *cb)
1213{
1214 struct blkio_cgroup *blkcg;
1215 enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
1216 int name = BLKIOFILE_ATTR(cft->private);
1217
1218 blkcg = cgroup_to_blkio_cgroup(cgrp);
1219
1220 switch(plid) {
1221 case BLKIO_POLICY_PROP:
1222 switch(name) {
1223 case BLKIO_PROP_time:
1224 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001225 BLKIO_STAT_TIME, 0, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001226 case BLKIO_PROP_sectors:
1227 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001228 BLKIO_STAT_CPU_SECTORS, 0, 1);
Vivek Goyal062a6442010-09-15 17:06:33 -04001229 case BLKIO_PROP_io_service_bytes:
1230 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001231 BLKIO_STAT_CPU_SERVICE_BYTES, 1, 1);
Vivek Goyal062a6442010-09-15 17:06:33 -04001232 case BLKIO_PROP_io_serviced:
1233 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001234 BLKIO_STAT_CPU_SERVICED, 1, 1);
Vivek Goyal062a6442010-09-15 17:06:33 -04001235 case BLKIO_PROP_io_service_time:
1236 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001237 BLKIO_STAT_SERVICE_TIME, 1, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001238 case BLKIO_PROP_io_wait_time:
1239 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001240 BLKIO_STAT_WAIT_TIME, 1, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001241 case BLKIO_PROP_io_merged:
1242 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal317389a2011-05-23 10:02:19 +02001243 BLKIO_STAT_CPU_MERGED, 1, 1);
Vivek Goyal062a6442010-09-15 17:06:33 -04001244 case BLKIO_PROP_io_queued:
1245 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001246 BLKIO_STAT_QUEUED, 1, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001247#ifdef CONFIG_DEBUG_BLK_CGROUP
Justin TerAvest9026e522011-03-22 21:26:54 +01001248 case BLKIO_PROP_unaccounted_time:
1249 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001250 BLKIO_STAT_UNACCOUNTED_TIME, 0, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001251 case BLKIO_PROP_dequeue:
1252 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001253 BLKIO_STAT_DEQUEUE, 0, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001254 case BLKIO_PROP_avg_queue_size:
1255 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001256 BLKIO_STAT_AVG_QUEUE_SIZE, 0, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001257 case BLKIO_PROP_group_wait_time:
1258 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001259 BLKIO_STAT_GROUP_WAIT_TIME, 0, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001260 case BLKIO_PROP_idle_time:
1261 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001262 BLKIO_STAT_IDLE_TIME, 0, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001263 case BLKIO_PROP_empty_time:
1264 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001265 BLKIO_STAT_EMPTY_TIME, 0, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001266#endif
1267 default:
1268 BUG();
1269 }
1270 break;
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001271 case BLKIO_POLICY_THROTL:
1272 switch(name){
1273 case BLKIO_THROTL_io_service_bytes:
1274 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001275 BLKIO_STAT_CPU_SERVICE_BYTES, 1, 1);
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001276 case BLKIO_THROTL_io_serviced:
1277 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001278 BLKIO_STAT_CPU_SERVICED, 1, 1);
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001279 default:
1280 BUG();
1281 }
1282 break;
Vivek Goyal062a6442010-09-15 17:06:33 -04001283 default:
1284 BUG();
1285 }
1286
1287 return 0;
1288}
1289
1290static int blkio_weight_write(struct blkio_cgroup *blkcg, u64 val)
1291{
1292 struct blkio_group *blkg;
1293 struct hlist_node *n;
1294 struct blkio_policy_node *pn;
1295
1296 if (val < BLKIO_WEIGHT_MIN || val > BLKIO_WEIGHT_MAX)
1297 return -EINVAL;
1298
1299 spin_lock(&blkio_list_lock);
1300 spin_lock_irq(&blkcg->lock);
1301 blkcg->weight = (unsigned int)val;
1302
1303 hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) {
1304 pn = blkio_policy_search_node(blkcg, blkg->dev,
1305 BLKIO_POLICY_PROP, BLKIO_PROP_weight_device);
1306 if (pn)
1307 continue;
1308
1309 blkio_update_group_weight(blkg, blkcg->weight);
1310 }
1311 spin_unlock_irq(&blkcg->lock);
1312 spin_unlock(&blkio_list_lock);
1313 return 0;
1314}
1315
1316static u64 blkiocg_file_read_u64 (struct cgroup *cgrp, struct cftype *cft) {
1317 struct blkio_cgroup *blkcg;
1318 enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
1319 int name = BLKIOFILE_ATTR(cft->private);
1320
1321 blkcg = cgroup_to_blkio_cgroup(cgrp);
1322
1323 switch(plid) {
1324 case BLKIO_POLICY_PROP:
1325 switch(name) {
1326 case BLKIO_PROP_weight:
1327 return (u64)blkcg->weight;
1328 }
1329 break;
1330 default:
1331 BUG();
1332 }
1333 return 0;
1334}
1335
1336static int
1337blkiocg_file_write_u64(struct cgroup *cgrp, struct cftype *cft, u64 val)
1338{
1339 struct blkio_cgroup *blkcg;
1340 enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
1341 int name = BLKIOFILE_ATTR(cft->private);
1342
1343 blkcg = cgroup_to_blkio_cgroup(cgrp);
1344
1345 switch(plid) {
1346 case BLKIO_POLICY_PROP:
1347 switch(name) {
1348 case BLKIO_PROP_weight:
1349 return blkio_weight_write(blkcg, val);
1350 }
1351 break;
1352 default:
1353 BUG();
1354 }
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001355
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001356 return 0;
1357}
1358
Vivek Goyal31e4c282009-12-03 12:59:42 -05001359struct cftype blkio_files[] = {
1360 {
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001361 .name = "weight_device",
Vivek Goyal062a6442010-09-15 17:06:33 -04001362 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1363 BLKIO_PROP_weight_device),
1364 .read_seq_string = blkiocg_file_read,
1365 .write_string = blkiocg_file_write,
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001366 .max_write_len = 256,
1367 },
1368 {
Vivek Goyal31e4c282009-12-03 12:59:42 -05001369 .name = "weight",
Vivek Goyal062a6442010-09-15 17:06:33 -04001370 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1371 BLKIO_PROP_weight),
1372 .read_u64 = blkiocg_file_read_u64,
1373 .write_u64 = blkiocg_file_write_u64,
Vivek Goyal31e4c282009-12-03 12:59:42 -05001374 },
Vivek Goyal22084192009-12-03 12:59:49 -05001375 {
1376 .name = "time",
Vivek Goyal13f98252010-10-01 14:49:41 +02001377 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1378 BLKIO_PROP_time),
1379 .read_map = blkiocg_file_read_map,
Vivek Goyal22084192009-12-03 12:59:49 -05001380 },
1381 {
1382 .name = "sectors",
Vivek Goyal13f98252010-10-01 14:49:41 +02001383 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1384 BLKIO_PROP_sectors),
1385 .read_map = blkiocg_file_read_map,
Divyesh Shah303a3ac2010-04-01 15:01:24 -07001386 },
1387 {
1388 .name = "io_service_bytes",
Vivek Goyal13f98252010-10-01 14:49:41 +02001389 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1390 BLKIO_PROP_io_service_bytes),
1391 .read_map = blkiocg_file_read_map,
Divyesh Shah303a3ac2010-04-01 15:01:24 -07001392 },
1393 {
1394 .name = "io_serviced",
Vivek Goyal13f98252010-10-01 14:49:41 +02001395 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1396 BLKIO_PROP_io_serviced),
1397 .read_map = blkiocg_file_read_map,
Divyesh Shah303a3ac2010-04-01 15:01:24 -07001398 },
1399 {
1400 .name = "io_service_time",
Vivek Goyal13f98252010-10-01 14:49:41 +02001401 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1402 BLKIO_PROP_io_service_time),
1403 .read_map = blkiocg_file_read_map,
Divyesh Shah303a3ac2010-04-01 15:01:24 -07001404 },
1405 {
1406 .name = "io_wait_time",
Vivek Goyal13f98252010-10-01 14:49:41 +02001407 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1408 BLKIO_PROP_io_wait_time),
1409 .read_map = blkiocg_file_read_map,
Divyesh Shah84c124d2010-04-09 08:31:19 +02001410 },
1411 {
Divyesh Shah812d4022010-04-08 21:14:23 -07001412 .name = "io_merged",
Vivek Goyal13f98252010-10-01 14:49:41 +02001413 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1414 BLKIO_PROP_io_merged),
1415 .read_map = blkiocg_file_read_map,
Divyesh Shah812d4022010-04-08 21:14:23 -07001416 },
1417 {
Divyesh Shahcdc11842010-04-08 21:15:10 -07001418 .name = "io_queued",
Vivek Goyal13f98252010-10-01 14:49:41 +02001419 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1420 BLKIO_PROP_io_queued),
1421 .read_map = blkiocg_file_read_map,
Divyesh Shahcdc11842010-04-08 21:15:10 -07001422 },
1423 {
Divyesh Shah84c124d2010-04-09 08:31:19 +02001424 .name = "reset_stats",
1425 .write_u64 = blkiocg_reset_stats,
Vivek Goyal22084192009-12-03 12:59:49 -05001426 },
Vivek Goyal13f98252010-10-01 14:49:41 +02001427#ifdef CONFIG_BLK_DEV_THROTTLING
1428 {
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001429 .name = "throttle.read_bps_device",
1430 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1431 BLKIO_THROTL_read_bps_device),
1432 .read_seq_string = blkiocg_file_read,
1433 .write_string = blkiocg_file_write,
1434 .max_write_len = 256,
1435 },
1436
1437 {
1438 .name = "throttle.write_bps_device",
1439 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1440 BLKIO_THROTL_write_bps_device),
1441 .read_seq_string = blkiocg_file_read,
1442 .write_string = blkiocg_file_write,
1443 .max_write_len = 256,
1444 },
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001445
1446 {
1447 .name = "throttle.read_iops_device",
1448 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1449 BLKIO_THROTL_read_iops_device),
1450 .read_seq_string = blkiocg_file_read,
1451 .write_string = blkiocg_file_write,
1452 .max_write_len = 256,
1453 },
1454
1455 {
1456 .name = "throttle.write_iops_device",
1457 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1458 BLKIO_THROTL_write_iops_device),
1459 .read_seq_string = blkiocg_file_read,
1460 .write_string = blkiocg_file_write,
1461 .max_write_len = 256,
1462 },
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001463 {
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001464 .name = "throttle.io_service_bytes",
1465 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1466 BLKIO_THROTL_io_service_bytes),
1467 .read_map = blkiocg_file_read_map,
1468 },
1469 {
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001470 .name = "throttle.io_serviced",
1471 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1472 BLKIO_THROTL_io_serviced),
1473 .read_map = blkiocg_file_read_map,
1474 },
Vivek Goyal13f98252010-10-01 14:49:41 +02001475#endif /* CONFIG_BLK_DEV_THROTTLING */
1476
Vivek Goyal22084192009-12-03 12:59:49 -05001477#ifdef CONFIG_DEBUG_BLK_CGROUP
Divyesh Shahcdc11842010-04-08 21:15:10 -07001478 {
1479 .name = "avg_queue_size",
Vivek Goyal062a6442010-09-15 17:06:33 -04001480 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1481 BLKIO_PROP_avg_queue_size),
1482 .read_map = blkiocg_file_read_map,
Divyesh Shahcdc11842010-04-08 21:15:10 -07001483 },
1484 {
Divyesh Shah812df482010-04-08 21:15:35 -07001485 .name = "group_wait_time",
Vivek Goyal062a6442010-09-15 17:06:33 -04001486 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1487 BLKIO_PROP_group_wait_time),
1488 .read_map = blkiocg_file_read_map,
Divyesh Shah812df482010-04-08 21:15:35 -07001489 },
1490 {
1491 .name = "idle_time",
Vivek Goyal062a6442010-09-15 17:06:33 -04001492 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1493 BLKIO_PROP_idle_time),
1494 .read_map = blkiocg_file_read_map,
Divyesh Shah812df482010-04-08 21:15:35 -07001495 },
1496 {
1497 .name = "empty_time",
Vivek Goyal062a6442010-09-15 17:06:33 -04001498 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1499 BLKIO_PROP_empty_time),
1500 .read_map = blkiocg_file_read_map,
Divyesh Shah812df482010-04-08 21:15:35 -07001501 },
1502 {
Vivek Goyal22084192009-12-03 12:59:49 -05001503 .name = "dequeue",
Vivek Goyal062a6442010-09-15 17:06:33 -04001504 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1505 BLKIO_PROP_dequeue),
1506 .read_map = blkiocg_file_read_map,
Divyesh Shahcdc11842010-04-08 21:15:10 -07001507 },
Justin TerAvest9026e522011-03-22 21:26:54 +01001508 {
1509 .name = "unaccounted_time",
1510 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1511 BLKIO_PROP_unaccounted_time),
1512 .read_map = blkiocg_file_read_map,
1513 },
Vivek Goyal22084192009-12-03 12:59:49 -05001514#endif
Vivek Goyal31e4c282009-12-03 12:59:42 -05001515};
1516
1517static int blkiocg_populate(struct cgroup_subsys *subsys, struct cgroup *cgroup)
1518{
1519 return cgroup_add_files(cgroup, subsys, blkio_files,
1520 ARRAY_SIZE(blkio_files));
1521}
1522
1523static void blkiocg_destroy(struct cgroup_subsys *subsys, struct cgroup *cgroup)
1524{
1525 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgroup);
Vivek Goyalb1c35762009-12-03 12:59:47 -05001526 unsigned long flags;
1527 struct blkio_group *blkg;
1528 void *key;
Vivek Goyal3e252062009-12-04 10:36:42 -05001529 struct blkio_policy_type *blkiop;
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001530 struct blkio_policy_node *pn, *pntmp;
Vivek Goyal31e4c282009-12-03 12:59:42 -05001531
Vivek Goyalb1c35762009-12-03 12:59:47 -05001532 rcu_read_lock();
Jens Axboe0f3942a2010-05-03 14:28:55 +02001533 do {
1534 spin_lock_irqsave(&blkcg->lock, flags);
Vivek Goyalb1c35762009-12-03 12:59:47 -05001535
Jens Axboe0f3942a2010-05-03 14:28:55 +02001536 if (hlist_empty(&blkcg->blkg_list)) {
1537 spin_unlock_irqrestore(&blkcg->lock, flags);
1538 break;
1539 }
1540
1541 blkg = hlist_entry(blkcg->blkg_list.first, struct blkio_group,
1542 blkcg_node);
1543 key = rcu_dereference(blkg->key);
1544 __blkiocg_del_blkio_group(blkg);
1545
Vivek Goyalb1c35762009-12-03 12:59:47 -05001546 spin_unlock_irqrestore(&blkcg->lock, flags);
Vivek Goyalb1c35762009-12-03 12:59:47 -05001547
Jens Axboe0f3942a2010-05-03 14:28:55 +02001548 /*
1549 * This blkio_group is being unlinked as associated cgroup is
1550 * going away. Let all the IO controlling policies know about
Vivek Goyal61014e92010-10-01 14:49:44 +02001551 * this event.
Jens Axboe0f3942a2010-05-03 14:28:55 +02001552 */
1553 spin_lock(&blkio_list_lock);
Vivek Goyal61014e92010-10-01 14:49:44 +02001554 list_for_each_entry(blkiop, &blkio_list, list) {
1555 if (blkiop->plid != blkg->plid)
1556 continue;
Jens Axboe0f3942a2010-05-03 14:28:55 +02001557 blkiop->ops.blkio_unlink_group_fn(key, blkg);
Vivek Goyal61014e92010-10-01 14:49:44 +02001558 }
Jens Axboe0f3942a2010-05-03 14:28:55 +02001559 spin_unlock(&blkio_list_lock);
1560 } while (1);
Vivek Goyalb1c35762009-12-03 12:59:47 -05001561
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001562 list_for_each_entry_safe(pn, pntmp, &blkcg->policy_list, node) {
1563 blkio_policy_delete_node(pn);
1564 kfree(pn);
1565 }
Jens Axboe0f3942a2010-05-03 14:28:55 +02001566
Vivek Goyal31e4c282009-12-03 12:59:42 -05001567 free_css_id(&blkio_subsys, &blkcg->css);
Vivek Goyalb1c35762009-12-03 12:59:47 -05001568 rcu_read_unlock();
Ben Blum67523c42010-03-10 15:22:11 -08001569 if (blkcg != &blkio_root_cgroup)
1570 kfree(blkcg);
Vivek Goyal31e4c282009-12-03 12:59:42 -05001571}
1572
1573static struct cgroup_subsys_state *
1574blkiocg_create(struct cgroup_subsys *subsys, struct cgroup *cgroup)
1575{
Li Zefan03415092010-05-07 08:57:00 +02001576 struct blkio_cgroup *blkcg;
1577 struct cgroup *parent = cgroup->parent;
Vivek Goyal31e4c282009-12-03 12:59:42 -05001578
Li Zefan03415092010-05-07 08:57:00 +02001579 if (!parent) {
Vivek Goyal31e4c282009-12-03 12:59:42 -05001580 blkcg = &blkio_root_cgroup;
1581 goto done;
1582 }
1583
Vivek Goyal31e4c282009-12-03 12:59:42 -05001584 blkcg = kzalloc(sizeof(*blkcg), GFP_KERNEL);
1585 if (!blkcg)
1586 return ERR_PTR(-ENOMEM);
1587
1588 blkcg->weight = BLKIO_WEIGHT_DEFAULT;
1589done:
1590 spin_lock_init(&blkcg->lock);
1591 INIT_HLIST_HEAD(&blkcg->blkg_list);
1592
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001593 INIT_LIST_HEAD(&blkcg->policy_list);
Vivek Goyal31e4c282009-12-03 12:59:42 -05001594 return &blkcg->css;
1595}
1596
1597/*
1598 * We cannot support shared io contexts, as we have no mean to support
1599 * two tasks with the same ioc in two different groups without major rework
1600 * of the main cic data structures. For now we allow a task to change
1601 * its cgroup only if it's the only owner of its ioc.
1602 */
Ben Blumf780bdb2011-05-26 16:25:19 -07001603static int blkiocg_can_attach_task(struct cgroup *cgrp, struct task_struct *tsk)
Vivek Goyal31e4c282009-12-03 12:59:42 -05001604{
1605 struct io_context *ioc;
1606 int ret = 0;
1607
1608 /* task_lock() is needed to avoid races with exit_io_context() */
1609 task_lock(tsk);
1610 ioc = tsk->io_context;
1611 if (ioc && atomic_read(&ioc->nr_tasks) > 1)
1612 ret = -EINVAL;
1613 task_unlock(tsk);
1614
1615 return ret;
1616}
1617
Ben Blumf780bdb2011-05-26 16:25:19 -07001618static void blkiocg_attach_task(struct cgroup *cgrp, struct task_struct *tsk)
Vivek Goyal31e4c282009-12-03 12:59:42 -05001619{
1620 struct io_context *ioc;
1621
1622 task_lock(tsk);
1623 ioc = tsk->io_context;
1624 if (ioc)
1625 ioc->cgroup_changed = 1;
1626 task_unlock(tsk);
1627}
1628
Vivek Goyal3e252062009-12-04 10:36:42 -05001629void blkio_policy_register(struct blkio_policy_type *blkiop)
1630{
1631 spin_lock(&blkio_list_lock);
1632 list_add_tail(&blkiop->list, &blkio_list);
1633 spin_unlock(&blkio_list_lock);
1634}
1635EXPORT_SYMBOL_GPL(blkio_policy_register);
1636
1637void blkio_policy_unregister(struct blkio_policy_type *blkiop)
1638{
1639 spin_lock(&blkio_list_lock);
1640 list_del_init(&blkiop->list);
1641 spin_unlock(&blkio_list_lock);
1642}
1643EXPORT_SYMBOL_GPL(blkio_policy_unregister);
Ben Blum67523c42010-03-10 15:22:11 -08001644
1645static int __init init_cgroup_blkio(void)
1646{
1647 return cgroup_load_subsys(&blkio_subsys);
1648}
1649
1650static void __exit exit_cgroup_blkio(void)
1651{
1652 cgroup_unload_subsys(&blkio_subsys);
1653}
1654
1655module_init(init_cgroup_blkio);
1656module_exit(exit_cgroup_blkio);
1657MODULE_LICENSE("GPL");