blob: fe8ce148017a48f68963dbdb4f1beb869220e129 [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>
Gui Jianfeng34d0f172010-04-13 16:05:49 +080020#include <linux/genhd.h>
Tejun Heo72e06c22012-03-05 13:15:00 -080021#include <linux/delay.h>
22#include "blk-cgroup.h"
Vivek Goyal3e252062009-12-04 10:36:42 -050023
Divyesh Shah84c124d2010-04-09 08:31:19 +020024#define MAX_KEY_LEN 100
25
Vivek Goyal3e252062009-12-04 10:36:42 -050026static DEFINE_SPINLOCK(blkio_list_lock);
27static LIST_HEAD(blkio_list);
Vivek Goyalb1c35762009-12-03 12:59:47 -050028
Vivek Goyal31e4c282009-12-03 12:59:42 -050029struct blkio_cgroup blkio_root_cgroup = { .weight = 2*BLKIO_WEIGHT_DEFAULT };
Vivek Goyal9d6a9862009-12-04 10:36:41 -050030EXPORT_SYMBOL_GPL(blkio_root_cgroup);
31
Tejun Heo035d10b2012-03-05 13:15:04 -080032static struct blkio_policy_type *blkio_policy[BLKIO_NR_POLICIES];
33
Ben Blum67523c42010-03-10 15:22:11 -080034static struct cgroup_subsys_state *blkiocg_create(struct cgroup_subsys *,
35 struct cgroup *);
Tejun Heobb9d97b2011-12-12 18:12:21 -080036static int blkiocg_can_attach(struct cgroup_subsys *, struct cgroup *,
37 struct cgroup_taskset *);
38static void blkiocg_attach(struct cgroup_subsys *, struct cgroup *,
39 struct cgroup_taskset *);
Ben Blum67523c42010-03-10 15:22:11 -080040static void blkiocg_destroy(struct cgroup_subsys *, struct cgroup *);
41static int blkiocg_populate(struct cgroup_subsys *, struct cgroup *);
42
Vivek Goyal062a6442010-09-15 17:06:33 -040043/* for encoding cft->private value on file */
44#define BLKIOFILE_PRIVATE(x, val) (((x) << 16) | (val))
45/* What policy owns the file, proportional or throttle */
46#define BLKIOFILE_POLICY(val) (((val) >> 16) & 0xffff)
47#define BLKIOFILE_ATTR(val) ((val) & 0xffff)
48
Ben Blum67523c42010-03-10 15:22:11 -080049struct cgroup_subsys blkio_subsys = {
50 .name = "blkio",
51 .create = blkiocg_create,
Tejun Heobb9d97b2011-12-12 18:12:21 -080052 .can_attach = blkiocg_can_attach,
53 .attach = blkiocg_attach,
Ben Blum67523c42010-03-10 15:22:11 -080054 .destroy = blkiocg_destroy,
55 .populate = blkiocg_populate,
Ben Blum67523c42010-03-10 15:22:11 -080056 .subsys_id = blkio_subsys_id,
Ben Blum67523c42010-03-10 15:22:11 -080057 .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)
Tejun Heoca32aef2012-03-05 13:15:03 -0800134 blkiop->ops.blkio_update_group_weight_fn(blkg->q,
Vivek Goyalfe071432010-10-01 14:49:49 +0200135 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)
Tejun Heoca32aef2012-03-05 13:15:03 -0800152 blkiop->ops.blkio_update_group_read_bps_fn(blkg->q,
Vivek Goyalfe071432010-10-01 14:49:49 +0200153 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)
Tejun Heoca32aef2012-03-05 13:15:03 -0800157 blkiop->ops.blkio_update_group_write_bps_fn(blkg->q,
Vivek Goyalfe071432010-10-01 14:49:49 +0200158 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)
Tejun Heoca32aef2012-03-05 13:15:03 -0800175 blkiop->ops.blkio_update_group_read_iops_fn(blkg->q,
Vivek Goyalfe071432010-10-01 14:49:49 +0200176 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)
Tejun Heoca32aef2012-03-05 13:15:03 -0800180 blkiop->ops.blkio_update_group_write_iops_fn(blkg->q,
Vivek Goyalfe071432010-10-01 14:49:49 +0200181 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
Vivek Goyal317389a2011-05-23 10:02:19 +0200444/* Merged stats are per cpu. */
Divyesh Shah812d4022010-04-08 21:14:23 -0700445void blkiocg_update_io_merged_stats(struct blkio_group *blkg, bool direction,
446 bool sync)
447{
Vivek Goyal317389a2011-05-23 10:02:19 +0200448 struct blkio_group_stats_cpu *stats_cpu;
Divyesh Shah812d4022010-04-08 21:14:23 -0700449 unsigned long flags;
450
Vivek Goyal317389a2011-05-23 10:02:19 +0200451 /*
452 * Disabling interrupts to provide mutual exclusion between two
453 * writes on same cpu. It probably is not needed for 64bit. Not
454 * optimizing that case yet.
455 */
456 local_irq_save(flags);
457
458 stats_cpu = this_cpu_ptr(blkg->stats_cpu);
459
460 u64_stats_update_begin(&stats_cpu->syncp);
461 blkio_add_stat(stats_cpu->stat_arr_cpu[BLKIO_STAT_CPU_MERGED], 1,
462 direction, sync);
463 u64_stats_update_end(&stats_cpu->syncp);
464 local_irq_restore(flags);
Divyesh Shah812d4022010-04-08 21:14:23 -0700465}
466EXPORT_SYMBOL_GPL(blkiocg_update_io_merged_stats);
467
Tejun Heocd1604f2012-03-05 13:15:06 -0800468struct blkio_group *blkg_lookup_create(struct blkio_cgroup *blkcg,
469 struct request_queue *q,
470 enum blkio_policy_id plid,
471 bool for_root)
472 __releases(q->queue_lock) __acquires(q->queue_lock)
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400473{
Tejun Heocd1604f2012-03-05 13:15:06 -0800474 struct blkio_policy_type *pol = blkio_policy[plid];
475 struct blkio_group *blkg, *new_blkg;
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400476
Tejun Heocd1604f2012-03-05 13:15:06 -0800477 WARN_ON_ONCE(!rcu_read_lock_held());
478 lockdep_assert_held(q->queue_lock);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500479
Tejun Heocd1604f2012-03-05 13:15:06 -0800480 /*
481 * This could be the first entry point of blkcg implementation and
482 * we shouldn't allow anything to go through for a bypassing queue.
483 * The following can be removed if blkg lookup is guaranteed to
484 * fail on a bypassing queue.
485 */
486 if (unlikely(blk_queue_bypass(q)) && !for_root)
487 return ERR_PTR(blk_queue_dead(q) ? -EINVAL : -EBUSY);
488
489 blkg = blkg_lookup(blkcg, q, plid);
490 if (blkg)
491 return blkg;
492
493 if (!css_tryget(&blkcg->css))
494 return ERR_PTR(-EINVAL);
495
496 /*
497 * Allocate and initialize.
498 *
499 * FIXME: The following is broken. Percpu memory allocation
500 * requires %GFP_KERNEL context and can't be performed from IO
501 * path. Allocation here should inherently be atomic and the
502 * following lock dancing can be removed once the broken percpu
503 * allocation is fixed.
504 */
505 spin_unlock_irq(q->queue_lock);
506 rcu_read_unlock();
507
508 new_blkg = pol->ops.blkio_alloc_group_fn(q, blkcg);
509 if (new_blkg) {
510 new_blkg->stats_cpu = alloc_percpu(struct blkio_group_stats_cpu);
511
512 spin_lock_init(&new_blkg->stats_lock);
513 rcu_assign_pointer(new_blkg->q, q);
514 new_blkg->blkcg_id = css_id(&blkcg->css);
515 new_blkg->plid = plid;
516 cgroup_path(blkcg->css.cgroup, new_blkg->path,
517 sizeof(new_blkg->path));
518 }
519
520 rcu_read_lock();
521 spin_lock_irq(q->queue_lock);
522 css_put(&blkcg->css);
523
524 /* did bypass get turned on inbetween? */
525 if (unlikely(blk_queue_bypass(q)) && !for_root) {
526 blkg = ERR_PTR(blk_queue_dead(q) ? -EINVAL : -EBUSY);
527 goto out;
528 }
529
530 /* did someone beat us to it? */
531 blkg = blkg_lookup(blkcg, q, plid);
532 if (unlikely(blkg))
533 goto out;
534
535 /* did alloc fail? */
536 if (unlikely(!new_blkg || !new_blkg->stats_cpu)) {
537 blkg = ERR_PTR(-ENOMEM);
538 goto out;
539 }
540
541 /* insert */
542 spin_lock(&blkcg->lock);
543 swap(blkg, new_blkg);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500544 hlist_add_head_rcu(&blkg->blkcg_node, &blkcg->blkg_list);
Tejun Heocd1604f2012-03-05 13:15:06 -0800545 pol->ops.blkio_link_group_fn(q, blkg);
546 spin_unlock(&blkcg->lock);
547out:
548 if (new_blkg) {
549 free_percpu(new_blkg->stats_cpu);
550 kfree(new_blkg);
551 }
552 return blkg;
Vivek Goyal31e4c282009-12-03 12:59:42 -0500553}
Tejun Heocd1604f2012-03-05 13:15:06 -0800554EXPORT_SYMBOL_GPL(blkg_lookup_create);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500555
Vivek Goyalb1c35762009-12-03 12:59:47 -0500556static void __blkiocg_del_blkio_group(struct blkio_group *blkg)
557{
558 hlist_del_init_rcu(&blkg->blkcg_node);
559 blkg->blkcg_id = 0;
560}
561
562/*
563 * returns 0 if blkio_group was still on cgroup list. Otherwise returns 1
564 * indicating that blk_group was unhashed by the time we got to it.
565 */
Vivek Goyal31e4c282009-12-03 12:59:42 -0500566int blkiocg_del_blkio_group(struct blkio_group *blkg)
567{
Vivek Goyalb1c35762009-12-03 12:59:47 -0500568 struct blkio_cgroup *blkcg;
569 unsigned long flags;
570 struct cgroup_subsys_state *css;
571 int ret = 1;
572
573 rcu_read_lock();
574 css = css_lookup(&blkio_subsys, blkg->blkcg_id);
Jens Axboe0f3942a2010-05-03 14:28:55 +0200575 if (css) {
576 blkcg = container_of(css, struct blkio_cgroup, css);
577 spin_lock_irqsave(&blkcg->lock, flags);
578 if (!hlist_unhashed(&blkg->blkcg_node)) {
579 __blkiocg_del_blkio_group(blkg);
580 ret = 0;
581 }
582 spin_unlock_irqrestore(&blkcg->lock, flags);
Vivek Goyalb1c35762009-12-03 12:59:47 -0500583 }
Jens Axboe0f3942a2010-05-03 14:28:55 +0200584
Vivek Goyalb1c35762009-12-03 12:59:47 -0500585 rcu_read_unlock();
586 return ret;
Vivek Goyal31e4c282009-12-03 12:59:42 -0500587}
Vivek Goyal9d6a9862009-12-04 10:36:41 -0500588EXPORT_SYMBOL_GPL(blkiocg_del_blkio_group);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500589
590/* called under rcu_read_lock(). */
Tejun Heocd1604f2012-03-05 13:15:06 -0800591struct blkio_group *blkg_lookup(struct blkio_cgroup *blkcg,
592 struct request_queue *q,
593 enum blkio_policy_id plid)
Vivek Goyal31e4c282009-12-03 12:59:42 -0500594{
595 struct blkio_group *blkg;
596 struct hlist_node *n;
Vivek Goyal31e4c282009-12-03 12:59:42 -0500597
Tejun Heoca32aef2012-03-05 13:15:03 -0800598 hlist_for_each_entry_rcu(blkg, n, &blkcg->blkg_list, blkcg_node)
599 if (blkg->q == q && blkg->plid == plid)
Vivek Goyal31e4c282009-12-03 12:59:42 -0500600 return blkg;
Vivek Goyal31e4c282009-12-03 12:59:42 -0500601 return NULL;
602}
Tejun Heocd1604f2012-03-05 13:15:06 -0800603EXPORT_SYMBOL_GPL(blkg_lookup);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500604
Tejun Heo72e06c22012-03-05 13:15:00 -0800605void blkg_destroy_all(struct request_queue *q)
606{
607 struct blkio_policy_type *pol;
608
609 while (true) {
610 bool done = true;
611
612 spin_lock(&blkio_list_lock);
613 spin_lock_irq(q->queue_lock);
614
615 /*
616 * clear_queue_fn() might return with non-empty group list
617 * if it raced cgroup removal and lost. cgroup removal is
618 * guaranteed to make forward progress and retrying after a
619 * while is enough. This ugliness is scheduled to be
620 * removed after locking update.
621 */
622 list_for_each_entry(pol, &blkio_list, list)
623 if (!pol->ops.blkio_clear_queue_fn(q))
624 done = false;
625
626 spin_unlock_irq(q->queue_lock);
627 spin_unlock(&blkio_list_lock);
628
629 if (done)
630 break;
631
632 msleep(10); /* just some random duration I like */
633 }
634}
635
Vivek Goyalf0bdc8c2011-05-19 15:38:30 -0400636static void blkio_reset_stats_cpu(struct blkio_group *blkg)
637{
638 struct blkio_group_stats_cpu *stats_cpu;
639 int i, j, k;
640 /*
641 * Note: On 64 bit arch this should not be an issue. This has the
642 * possibility of returning some inconsistent value on 32bit arch
643 * as 64bit update on 32bit is non atomic. Taking care of this
644 * corner case makes code very complicated, like sending IPIs to
645 * cpus, taking care of stats of offline cpus etc.
646 *
647 * reset stats is anyway more of a debug feature and this sounds a
648 * corner case. So I am not complicating the code yet until and
649 * unless this becomes a real issue.
650 */
651 for_each_possible_cpu(i) {
652 stats_cpu = per_cpu_ptr(blkg->stats_cpu, i);
653 stats_cpu->sectors = 0;
654 for(j = 0; j < BLKIO_STAT_CPU_NR; j++)
655 for (k = 0; k < BLKIO_STAT_TOTAL; k++)
656 stats_cpu->stat_arr_cpu[j][k] = 0;
657 }
658}
659
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700660static int
Divyesh Shah84c124d2010-04-09 08:31:19 +0200661blkiocg_reset_stats(struct cgroup *cgroup, struct cftype *cftype, u64 val)
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700662{
663 struct blkio_cgroup *blkcg;
664 struct blkio_group *blkg;
Divyesh Shah812df482010-04-08 21:15:35 -0700665 struct blkio_group_stats *stats;
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700666 struct hlist_node *n;
Divyesh Shahcdc11842010-04-08 21:15:10 -0700667 uint64_t queued[BLKIO_STAT_TOTAL];
668 int i;
Divyesh Shah812df482010-04-08 21:15:35 -0700669#ifdef CONFIG_DEBUG_BLK_CGROUP
670 bool idling, waiting, empty;
671 unsigned long long now = sched_clock();
672#endif
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700673
674 blkcg = cgroup_to_blkio_cgroup(cgroup);
675 spin_lock_irq(&blkcg->lock);
676 hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) {
677 spin_lock(&blkg->stats_lock);
Divyesh Shah812df482010-04-08 21:15:35 -0700678 stats = &blkg->stats;
679#ifdef CONFIG_DEBUG_BLK_CGROUP
680 idling = blkio_blkg_idling(stats);
681 waiting = blkio_blkg_waiting(stats);
682 empty = blkio_blkg_empty(stats);
683#endif
Divyesh Shahcdc11842010-04-08 21:15:10 -0700684 for (i = 0; i < BLKIO_STAT_TOTAL; i++)
Divyesh Shah812df482010-04-08 21:15:35 -0700685 queued[i] = stats->stat_arr[BLKIO_STAT_QUEUED][i];
686 memset(stats, 0, sizeof(struct blkio_group_stats));
Divyesh Shahcdc11842010-04-08 21:15:10 -0700687 for (i = 0; i < BLKIO_STAT_TOTAL; i++)
Divyesh Shah812df482010-04-08 21:15:35 -0700688 stats->stat_arr[BLKIO_STAT_QUEUED][i] = queued[i];
689#ifdef CONFIG_DEBUG_BLK_CGROUP
690 if (idling) {
691 blkio_mark_blkg_idling(stats);
692 stats->start_idle_time = now;
693 }
694 if (waiting) {
695 blkio_mark_blkg_waiting(stats);
696 stats->start_group_wait_time = now;
697 }
698 if (empty) {
699 blkio_mark_blkg_empty(stats);
700 stats->start_empty_time = now;
701 }
702#endif
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700703 spin_unlock(&blkg->stats_lock);
Vivek Goyalf0bdc8c2011-05-19 15:38:30 -0400704
705 /* Reset Per cpu stats which don't take blkg->stats_lock */
706 blkio_reset_stats_cpu(blkg);
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700707 }
Vivek Goyalf0bdc8c2011-05-19 15:38:30 -0400708
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700709 spin_unlock_irq(&blkcg->lock);
710 return 0;
711}
712
Divyesh Shah84c124d2010-04-09 08:31:19 +0200713static void blkio_get_key_name(enum stat_sub_type type, dev_t dev, char *str,
714 int chars_left, bool diskname_only)
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700715{
Divyesh Shah84c124d2010-04-09 08:31:19 +0200716 snprintf(str, chars_left, "%d:%d", MAJOR(dev), MINOR(dev));
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700717 chars_left -= strlen(str);
718 if (chars_left <= 0) {
719 printk(KERN_WARNING
720 "Possibly incorrect cgroup stat display format");
721 return;
722 }
Divyesh Shah84c124d2010-04-09 08:31:19 +0200723 if (diskname_only)
724 return;
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700725 switch (type) {
Divyesh Shah84c124d2010-04-09 08:31:19 +0200726 case BLKIO_STAT_READ:
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700727 strlcat(str, " Read", chars_left);
728 break;
Divyesh Shah84c124d2010-04-09 08:31:19 +0200729 case BLKIO_STAT_WRITE:
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700730 strlcat(str, " Write", chars_left);
731 break;
Divyesh Shah84c124d2010-04-09 08:31:19 +0200732 case BLKIO_STAT_SYNC:
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700733 strlcat(str, " Sync", chars_left);
734 break;
Divyesh Shah84c124d2010-04-09 08:31:19 +0200735 case BLKIO_STAT_ASYNC:
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700736 strlcat(str, " Async", chars_left);
737 break;
Divyesh Shah84c124d2010-04-09 08:31:19 +0200738 case BLKIO_STAT_TOTAL:
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700739 strlcat(str, " Total", chars_left);
740 break;
741 default:
742 strlcat(str, " Invalid", chars_left);
743 }
744}
745
Divyesh Shah84c124d2010-04-09 08:31:19 +0200746static uint64_t blkio_fill_stat(char *str, int chars_left, uint64_t val,
747 struct cgroup_map_cb *cb, dev_t dev)
748{
749 blkio_get_key_name(0, dev, str, chars_left, true);
750 cb->fill(cb, str, val);
751 return val;
752}
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700753
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400754
755static uint64_t blkio_read_stat_cpu(struct blkio_group *blkg,
756 enum stat_type_cpu type, enum stat_sub_type sub_type)
757{
758 int cpu;
759 struct blkio_group_stats_cpu *stats_cpu;
Vivek Goyal575969a2011-05-19 15:38:29 -0400760 u64 val = 0, tval;
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400761
762 for_each_possible_cpu(cpu) {
Vivek Goyal575969a2011-05-19 15:38:29 -0400763 unsigned int start;
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400764 stats_cpu = per_cpu_ptr(blkg->stats_cpu, cpu);
765
Vivek Goyal575969a2011-05-19 15:38:29 -0400766 do {
767 start = u64_stats_fetch_begin(&stats_cpu->syncp);
768 if (type == BLKIO_STAT_CPU_SECTORS)
769 tval = stats_cpu->sectors;
770 else
771 tval = stats_cpu->stat_arr_cpu[type][sub_type];
772 } while(u64_stats_fetch_retry(&stats_cpu->syncp, start));
773
774 val += tval;
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400775 }
776
777 return val;
778}
779
780static uint64_t blkio_get_stat_cpu(struct blkio_group *blkg,
781 struct cgroup_map_cb *cb, dev_t dev, enum stat_type_cpu type)
782{
783 uint64_t disk_total, val;
784 char key_str[MAX_KEY_LEN];
785 enum stat_sub_type sub_type;
786
787 if (type == BLKIO_STAT_CPU_SECTORS) {
788 val = blkio_read_stat_cpu(blkg, type, 0);
789 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1, val, cb, dev);
790 }
791
792 for (sub_type = BLKIO_STAT_READ; sub_type < BLKIO_STAT_TOTAL;
793 sub_type++) {
794 blkio_get_key_name(sub_type, dev, key_str, MAX_KEY_LEN, false);
795 val = blkio_read_stat_cpu(blkg, type, sub_type);
796 cb->fill(cb, key_str, val);
797 }
798
799 disk_total = blkio_read_stat_cpu(blkg, type, BLKIO_STAT_READ) +
800 blkio_read_stat_cpu(blkg, type, BLKIO_STAT_WRITE);
801
802 blkio_get_key_name(BLKIO_STAT_TOTAL, dev, key_str, MAX_KEY_LEN, false);
803 cb->fill(cb, key_str, disk_total);
804 return disk_total;
805}
806
Divyesh Shah84c124d2010-04-09 08:31:19 +0200807/* This should be called with blkg->stats_lock held */
808static uint64_t blkio_get_stat(struct blkio_group *blkg,
809 struct cgroup_map_cb *cb, dev_t dev, enum stat_type type)
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700810{
811 uint64_t disk_total;
812 char key_str[MAX_KEY_LEN];
Divyesh Shah84c124d2010-04-09 08:31:19 +0200813 enum stat_sub_type sub_type;
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700814
Divyesh Shah84c124d2010-04-09 08:31:19 +0200815 if (type == BLKIO_STAT_TIME)
816 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
817 blkg->stats.time, cb, dev);
Justin TerAvest9026e522011-03-22 21:26:54 +0100818#ifdef CONFIG_DEBUG_BLK_CGROUP
Justin TerAvest167400d2011-03-12 16:54:00 +0100819 if (type == BLKIO_STAT_UNACCOUNTED_TIME)
820 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
821 blkg->stats.unaccounted_time, cb, dev);
Divyesh Shahcdc11842010-04-08 21:15:10 -0700822 if (type == BLKIO_STAT_AVG_QUEUE_SIZE) {
823 uint64_t sum = blkg->stats.avg_queue_size_sum;
824 uint64_t samples = blkg->stats.avg_queue_size_samples;
825 if (samples)
826 do_div(sum, samples);
827 else
828 sum = 0;
829 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1, sum, cb, dev);
830 }
Divyesh Shah812df482010-04-08 21:15:35 -0700831 if (type == BLKIO_STAT_GROUP_WAIT_TIME)
832 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
833 blkg->stats.group_wait_time, cb, dev);
834 if (type == BLKIO_STAT_IDLE_TIME)
835 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
836 blkg->stats.idle_time, cb, dev);
837 if (type == BLKIO_STAT_EMPTY_TIME)
838 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
839 blkg->stats.empty_time, cb, dev);
Divyesh Shah84c124d2010-04-09 08:31:19 +0200840 if (type == BLKIO_STAT_DEQUEUE)
841 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
842 blkg->stats.dequeue, cb, dev);
843#endif
844
845 for (sub_type = BLKIO_STAT_READ; sub_type < BLKIO_STAT_TOTAL;
846 sub_type++) {
847 blkio_get_key_name(sub_type, dev, key_str, MAX_KEY_LEN, false);
848 cb->fill(cb, key_str, blkg->stats.stat_arr[type][sub_type]);
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700849 }
Divyesh Shah84c124d2010-04-09 08:31:19 +0200850 disk_total = blkg->stats.stat_arr[type][BLKIO_STAT_READ] +
851 blkg->stats.stat_arr[type][BLKIO_STAT_WRITE];
852 blkio_get_key_name(BLKIO_STAT_TOTAL, dev, key_str, MAX_KEY_LEN, false);
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700853 cb->fill(cb, key_str, disk_total);
854 return disk_total;
855}
856
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800857static int blkio_policy_parse_and_set(char *buf,
Tejun Heoe56da7e2012-03-05 13:15:07 -0800858 struct blkio_policy_node *newpn,
859 enum blkio_policy_id plid, int fileid,
860 struct blkio_cgroup *blkcg)
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800861{
Tejun Heoece84242011-10-19 14:31:15 +0200862 struct gendisk *disk = NULL;
Tejun Heoe56da7e2012-03-05 13:15:07 -0800863 struct blkio_group *blkg = NULL;
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800864 char *s[4], *p, *major_s = NULL, *minor_s = NULL;
Wanlong Gaod11bb442011-09-21 10:22:10 +0200865 unsigned long major, minor;
Tejun Heoece84242011-10-19 14:31:15 +0200866 int i = 0, ret = -EINVAL;
867 int part;
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800868 dev_t dev;
Wanlong Gaod11bb442011-09-21 10:22:10 +0200869 u64 temp;
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800870
871 memset(s, 0, sizeof(s));
872
873 while ((p = strsep(&buf, " ")) != NULL) {
874 if (!*p)
875 continue;
876
877 s[i++] = p;
878
879 /* Prevent from inputing too many things */
880 if (i == 3)
881 break;
882 }
883
884 if (i != 2)
Tejun Heoece84242011-10-19 14:31:15 +0200885 goto out;
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800886
887 p = strsep(&s[0], ":");
888 if (p != NULL)
889 major_s = p;
890 else
Tejun Heoece84242011-10-19 14:31:15 +0200891 goto out;
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800892
893 minor_s = s[0];
894 if (!minor_s)
Tejun Heoece84242011-10-19 14:31:15 +0200895 goto out;
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800896
Tejun Heoece84242011-10-19 14:31:15 +0200897 if (strict_strtoul(major_s, 10, &major))
898 goto out;
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800899
Tejun Heoece84242011-10-19 14:31:15 +0200900 if (strict_strtoul(minor_s, 10, &minor))
901 goto out;
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800902
903 dev = MKDEV(major, minor);
904
Tejun Heoece84242011-10-19 14:31:15 +0200905 if (strict_strtoull(s[1], 10, &temp))
906 goto out;
Wanlong Gaod11bb442011-09-21 10:22:10 +0200907
908 /* For rule removal, do not check for device presence. */
Tejun Heoe56da7e2012-03-05 13:15:07 -0800909 disk = get_gendisk(dev, &part);
910
911 if ((!disk || part) && temp) {
912 ret = -ENODEV;
913 goto out;
914 }
915
916 rcu_read_lock();
917
918 if (disk && !part) {
919 spin_lock_irq(disk->queue->queue_lock);
920 blkg = blkg_lookup_create(blkcg, disk->queue, plid, false);
921 spin_unlock_irq(disk->queue->queue_lock);
922
923 if (IS_ERR(blkg)) {
924 ret = PTR_ERR(blkg);
925 if (ret == -EBUSY)
926 goto out_unlock;
927 blkg = NULL;
Tejun Heoece84242011-10-19 14:31:15 +0200928 }
Wanlong Gaod11bb442011-09-21 10:22:10 +0200929 }
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800930
931 newpn->dev = dev;
932
Vivek Goyal062a6442010-09-15 17:06:33 -0400933 switch (plid) {
934 case BLKIO_POLICY_PROP:
Wanlong Gaod11bb442011-09-21 10:22:10 +0200935 if ((temp < BLKIO_WEIGHT_MIN && temp > 0) ||
936 temp > BLKIO_WEIGHT_MAX)
Tejun Heoe56da7e2012-03-05 13:15:07 -0800937 goto out_unlock;
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800938
Vivek Goyal062a6442010-09-15 17:06:33 -0400939 newpn->plid = plid;
940 newpn->fileid = fileid;
Vivek Goyal4c9eefa2010-09-15 17:06:34 -0400941 newpn->val.weight = temp;
Tejun Heoe56da7e2012-03-05 13:15:07 -0800942 if (blkg)
943 blkg->conf.weight = temp;
Vivek Goyal4c9eefa2010-09-15 17:06:34 -0400944 break;
945 case BLKIO_POLICY_THROTL:
Vivek Goyal7702e8f2010-09-15 17:06:36 -0400946 switch(fileid) {
947 case BLKIO_THROTL_read_bps_device:
Tejun Heoe56da7e2012-03-05 13:15:07 -0800948 if (blkg)
949 blkg->conf.bps[READ] = temp;
950 newpn->plid = plid;
951 newpn->fileid = fileid;
952 newpn->val.bps = temp;
953 break;
Vivek Goyal7702e8f2010-09-15 17:06:36 -0400954 case BLKIO_THROTL_write_bps_device:
Tejun Heoe56da7e2012-03-05 13:15:07 -0800955 if (blkg)
956 blkg->conf.bps[WRITE] = temp;
Vivek Goyal7702e8f2010-09-15 17:06:36 -0400957 newpn->plid = plid;
958 newpn->fileid = fileid;
Wanlong Gaod11bb442011-09-21 10:22:10 +0200959 newpn->val.bps = temp;
Vivek Goyal7702e8f2010-09-15 17:06:36 -0400960 break;
961 case BLKIO_THROTL_read_iops_device:
Tejun Heoe56da7e2012-03-05 13:15:07 -0800962 if (temp > THROTL_IOPS_MAX)
963 goto out_unlock;
964
965 if (blkg)
966 blkg->conf.iops[READ] = temp;
967 newpn->plid = plid;
968 newpn->fileid = fileid;
969 newpn->val.iops = (unsigned int)temp;
970 break;
Vivek Goyal7702e8f2010-09-15 17:06:36 -0400971 case BLKIO_THROTL_write_iops_device:
Wanlong Gaod11bb442011-09-21 10:22:10 +0200972 if (temp > THROTL_IOPS_MAX)
Tejun Heoe56da7e2012-03-05 13:15:07 -0800973 goto out_unlock;
Vivek Goyal9355aed2010-10-01 21:16:41 +0200974
Tejun Heoe56da7e2012-03-05 13:15:07 -0800975 if (blkg)
976 blkg->conf.iops[WRITE] = temp;
Vivek Goyal7702e8f2010-09-15 17:06:36 -0400977 newpn->plid = plid;
978 newpn->fileid = fileid;
Wanlong Gaod11bb442011-09-21 10:22:10 +0200979 newpn->val.iops = (unsigned int)temp;
Vivek Goyal7702e8f2010-09-15 17:06:36 -0400980 break;
981 }
Vivek Goyal062a6442010-09-15 17:06:33 -0400982 break;
983 default:
984 BUG();
985 }
Tejun Heoece84242011-10-19 14:31:15 +0200986 ret = 0;
Tejun Heoe56da7e2012-03-05 13:15:07 -0800987out_unlock:
988 rcu_read_unlock();
Tejun Heoece84242011-10-19 14:31:15 +0200989out:
990 put_disk(disk);
Tejun Heoe56da7e2012-03-05 13:15:07 -0800991
992 /*
993 * If queue was bypassing, we should retry. Do so after a short
994 * msleep(). It isn't strictly necessary but queue can be
995 * bypassing for some time and it's always nice to avoid busy
996 * looping.
997 */
998 if (ret == -EBUSY) {
999 msleep(10);
1000 return restart_syscall();
1001 }
Tejun Heoece84242011-10-19 14:31:15 +02001002 return ret;
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001003}
1004
1005unsigned int blkcg_get_weight(struct blkio_cgroup *blkcg,
1006 dev_t dev)
1007{
1008 struct blkio_policy_node *pn;
Vivek Goyala38eb632011-10-25 15:48:12 +02001009 unsigned long flags;
1010 unsigned int weight;
1011
1012 spin_lock_irqsave(&blkcg->lock, flags);
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001013
Vivek Goyal062a6442010-09-15 17:06:33 -04001014 pn = blkio_policy_search_node(blkcg, dev, BLKIO_POLICY_PROP,
1015 BLKIO_PROP_weight_device);
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001016 if (pn)
Vivek Goyala38eb632011-10-25 15:48:12 +02001017 weight = pn->val.weight;
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001018 else
Vivek Goyala38eb632011-10-25 15:48:12 +02001019 weight = blkcg->weight;
1020
1021 spin_unlock_irqrestore(&blkcg->lock, flags);
1022
1023 return weight;
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001024}
1025EXPORT_SYMBOL_GPL(blkcg_get_weight);
1026
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001027uint64_t blkcg_get_read_bps(struct blkio_cgroup *blkcg, dev_t dev)
1028{
1029 struct blkio_policy_node *pn;
Vivek Goyala38eb632011-10-25 15:48:12 +02001030 unsigned long flags;
1031 uint64_t bps = -1;
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001032
Vivek Goyala38eb632011-10-25 15:48:12 +02001033 spin_lock_irqsave(&blkcg->lock, flags);
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001034 pn = blkio_policy_search_node(blkcg, dev, BLKIO_POLICY_THROTL,
1035 BLKIO_THROTL_read_bps_device);
1036 if (pn)
Vivek Goyala38eb632011-10-25 15:48:12 +02001037 bps = pn->val.bps;
1038 spin_unlock_irqrestore(&blkcg->lock, flags);
1039
1040 return bps;
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001041}
1042
1043uint64_t blkcg_get_write_bps(struct blkio_cgroup *blkcg, dev_t dev)
1044{
1045 struct blkio_policy_node *pn;
Vivek Goyala38eb632011-10-25 15:48:12 +02001046 unsigned long flags;
1047 uint64_t bps = -1;
1048
1049 spin_lock_irqsave(&blkcg->lock, flags);
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001050 pn = blkio_policy_search_node(blkcg, dev, BLKIO_POLICY_THROTL,
1051 BLKIO_THROTL_write_bps_device);
1052 if (pn)
Vivek Goyala38eb632011-10-25 15:48:12 +02001053 bps = pn->val.bps;
1054 spin_unlock_irqrestore(&blkcg->lock, flags);
1055
1056 return bps;
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001057}
1058
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001059unsigned int blkcg_get_read_iops(struct blkio_cgroup *blkcg, dev_t dev)
1060{
1061 struct blkio_policy_node *pn;
Vivek Goyala38eb632011-10-25 15:48:12 +02001062 unsigned long flags;
1063 unsigned int iops = -1;
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001064
Vivek Goyala38eb632011-10-25 15:48:12 +02001065 spin_lock_irqsave(&blkcg->lock, flags);
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001066 pn = blkio_policy_search_node(blkcg, dev, BLKIO_POLICY_THROTL,
1067 BLKIO_THROTL_read_iops_device);
1068 if (pn)
Vivek Goyala38eb632011-10-25 15:48:12 +02001069 iops = pn->val.iops;
1070 spin_unlock_irqrestore(&blkcg->lock, flags);
1071
1072 return iops;
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001073}
1074
1075unsigned int blkcg_get_write_iops(struct blkio_cgroup *blkcg, dev_t dev)
1076{
1077 struct blkio_policy_node *pn;
Vivek Goyala38eb632011-10-25 15:48:12 +02001078 unsigned long flags;
1079 unsigned int iops = -1;
1080
1081 spin_lock_irqsave(&blkcg->lock, flags);
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001082 pn = blkio_policy_search_node(blkcg, dev, BLKIO_POLICY_THROTL,
1083 BLKIO_THROTL_write_iops_device);
1084 if (pn)
Vivek Goyala38eb632011-10-25 15:48:12 +02001085 iops = pn->val.iops;
1086 spin_unlock_irqrestore(&blkcg->lock, flags);
1087
1088 return iops;
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001089}
1090
Vivek Goyal062a6442010-09-15 17:06:33 -04001091/* Checks whether user asked for deleting a policy rule */
1092static bool blkio_delete_rule_command(struct blkio_policy_node *pn)
1093{
1094 switch(pn->plid) {
1095 case BLKIO_POLICY_PROP:
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001096 if (pn->val.weight == 0)
1097 return 1;
1098 break;
1099 case BLKIO_POLICY_THROTL:
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001100 switch(pn->fileid) {
1101 case BLKIO_THROTL_read_bps_device:
1102 case BLKIO_THROTL_write_bps_device:
1103 if (pn->val.bps == 0)
1104 return 1;
1105 break;
1106 case BLKIO_THROTL_read_iops_device:
1107 case BLKIO_THROTL_write_iops_device:
1108 if (pn->val.iops == 0)
1109 return 1;
1110 }
Vivek Goyal062a6442010-09-15 17:06:33 -04001111 break;
1112 default:
1113 BUG();
1114 }
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001115
Vivek Goyal062a6442010-09-15 17:06:33 -04001116 return 0;
1117}
1118
1119static void blkio_update_policy_rule(struct blkio_policy_node *oldpn,
1120 struct blkio_policy_node *newpn)
1121{
1122 switch(oldpn->plid) {
1123 case BLKIO_POLICY_PROP:
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001124 oldpn->val.weight = newpn->val.weight;
1125 break;
1126 case BLKIO_POLICY_THROTL:
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001127 switch(newpn->fileid) {
1128 case BLKIO_THROTL_read_bps_device:
1129 case BLKIO_THROTL_write_bps_device:
1130 oldpn->val.bps = newpn->val.bps;
1131 break;
1132 case BLKIO_THROTL_read_iops_device:
1133 case BLKIO_THROTL_write_iops_device:
1134 oldpn->val.iops = newpn->val.iops;
1135 }
Vivek Goyal062a6442010-09-15 17:06:33 -04001136 break;
1137 default:
1138 BUG();
1139 }
1140}
1141
1142/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001143 * Some rules/values in blkg have changed. Propagate those to respective
Vivek Goyal062a6442010-09-15 17:06:33 -04001144 * policies.
1145 */
1146static void blkio_update_blkg_policy(struct blkio_cgroup *blkcg,
1147 struct blkio_group *blkg, struct blkio_policy_node *pn)
1148{
Tejun Heoe56da7e2012-03-05 13:15:07 -08001149 struct blkio_group_conf *conf = &blkg->conf;
Vivek Goyal062a6442010-09-15 17:06:33 -04001150
1151 switch(pn->plid) {
1152 case BLKIO_POLICY_PROP:
Tejun Heoe56da7e2012-03-05 13:15:07 -08001153 blkio_update_group_weight(blkg, conf->weight ?: blkcg->weight);
Vivek Goyal062a6442010-09-15 17:06:33 -04001154 break;
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001155 case BLKIO_POLICY_THROTL:
1156 switch(pn->fileid) {
1157 case BLKIO_THROTL_read_bps_device:
Tejun Heoe56da7e2012-03-05 13:15:07 -08001158 blkio_update_group_bps(blkg, conf->bps[READ] ?: -1,
1159 pn->fileid);
1160 break;
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001161 case BLKIO_THROTL_write_bps_device:
Tejun Heoe56da7e2012-03-05 13:15:07 -08001162 blkio_update_group_bps(blkg, conf->bps[WRITE] ?: -1,
1163 pn->fileid);
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001164 break;
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001165 case BLKIO_THROTL_read_iops_device:
Tejun Heoe56da7e2012-03-05 13:15:07 -08001166 blkio_update_group_iops(blkg, conf->iops[READ] ?: -1,
1167 pn->fileid);
1168 break;
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001169 case BLKIO_THROTL_write_iops_device:
Tejun Heoe56da7e2012-03-05 13:15:07 -08001170 blkio_update_group_iops(blkg, conf->iops[WRITE] ?: -1,
1171 pn->fileid);
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001172 break;
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001173 }
1174 break;
Vivek Goyal062a6442010-09-15 17:06:33 -04001175 default:
1176 BUG();
1177 }
1178}
1179
1180/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001181 * A policy node rule has been updated. Propagate this update to all the
Vivek Goyal062a6442010-09-15 17:06:33 -04001182 * block groups which might be affected by this update.
1183 */
1184static void blkio_update_policy_node_blkg(struct blkio_cgroup *blkcg,
1185 struct blkio_policy_node *pn)
1186{
1187 struct blkio_group *blkg;
1188 struct hlist_node *n;
1189
1190 spin_lock(&blkio_list_lock);
1191 spin_lock_irq(&blkcg->lock);
1192
1193 hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) {
1194 if (pn->dev != blkg->dev || pn->plid != blkg->plid)
1195 continue;
1196 blkio_update_blkg_policy(blkcg, blkg, pn);
1197 }
1198
1199 spin_unlock_irq(&blkcg->lock);
1200 spin_unlock(&blkio_list_lock);
1201}
1202
1203static int blkiocg_file_write(struct cgroup *cgrp, struct cftype *cft,
1204 const char *buffer)
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001205{
1206 int ret = 0;
1207 char *buf;
1208 struct blkio_policy_node *newpn, *pn;
Tejun Heoe56da7e2012-03-05 13:15:07 -08001209 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgrp);
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001210 int keep_newpn = 0;
Vivek Goyal062a6442010-09-15 17:06:33 -04001211 enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
1212 int fileid = BLKIOFILE_ATTR(cft->private);
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001213
1214 buf = kstrdup(buffer, GFP_KERNEL);
1215 if (!buf)
1216 return -ENOMEM;
1217
1218 newpn = kzalloc(sizeof(*newpn), GFP_KERNEL);
1219 if (!newpn) {
1220 ret = -ENOMEM;
1221 goto free_buf;
1222 }
1223
Tejun Heoe56da7e2012-03-05 13:15:07 -08001224 ret = blkio_policy_parse_and_set(buf, newpn, plid, fileid, blkcg);
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001225 if (ret)
1226 goto free_newpn;
1227
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001228 spin_lock_irq(&blkcg->lock);
1229
Vivek Goyal062a6442010-09-15 17:06:33 -04001230 pn = blkio_policy_search_node(blkcg, newpn->dev, plid, fileid);
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001231 if (!pn) {
Vivek Goyal062a6442010-09-15 17:06:33 -04001232 if (!blkio_delete_rule_command(newpn)) {
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001233 blkio_policy_insert_node(blkcg, newpn);
1234 keep_newpn = 1;
1235 }
1236 spin_unlock_irq(&blkcg->lock);
1237 goto update_io_group;
1238 }
1239
Vivek Goyal062a6442010-09-15 17:06:33 -04001240 if (blkio_delete_rule_command(newpn)) {
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001241 blkio_policy_delete_node(pn);
Vivek Goyale060f002011-10-25 15:48:12 +02001242 kfree(pn);
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001243 spin_unlock_irq(&blkcg->lock);
1244 goto update_io_group;
1245 }
1246 spin_unlock_irq(&blkcg->lock);
1247
Vivek Goyal062a6442010-09-15 17:06:33 -04001248 blkio_update_policy_rule(pn, newpn);
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001249
1250update_io_group:
Vivek Goyal062a6442010-09-15 17:06:33 -04001251 blkio_update_policy_node_blkg(blkcg, newpn);
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001252
1253free_newpn:
1254 if (!keep_newpn)
1255 kfree(newpn);
1256free_buf:
1257 kfree(buf);
1258 return ret;
1259}
1260
Vivek Goyal062a6442010-09-15 17:06:33 -04001261static void
1262blkio_print_policy_node(struct seq_file *m, struct blkio_policy_node *pn)
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001263{
Vivek Goyal062a6442010-09-15 17:06:33 -04001264 switch(pn->plid) {
1265 case BLKIO_POLICY_PROP:
1266 if (pn->fileid == BLKIO_PROP_weight_device)
1267 seq_printf(m, "%u:%u\t%u\n", MAJOR(pn->dev),
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001268 MINOR(pn->dev), pn->val.weight);
1269 break;
1270 case BLKIO_POLICY_THROTL:
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001271 switch(pn->fileid) {
1272 case BLKIO_THROTL_read_bps_device:
1273 case BLKIO_THROTL_write_bps_device:
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001274 seq_printf(m, "%u:%u\t%llu\n", MAJOR(pn->dev),
1275 MINOR(pn->dev), pn->val.bps);
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001276 break;
1277 case BLKIO_THROTL_read_iops_device:
1278 case BLKIO_THROTL_write_iops_device:
1279 seq_printf(m, "%u:%u\t%u\n", MAJOR(pn->dev),
1280 MINOR(pn->dev), pn->val.iops);
1281 break;
1282 }
Vivek Goyal062a6442010-09-15 17:06:33 -04001283 break;
1284 default:
1285 BUG();
1286 }
1287}
1288
1289/* cgroup files which read their data from policy nodes end up here */
1290static void blkio_read_policy_node_files(struct cftype *cft,
1291 struct blkio_cgroup *blkcg, struct seq_file *m)
1292{
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001293 struct blkio_policy_node *pn;
1294
Jens Axboe0f3942a2010-05-03 14:28:55 +02001295 if (!list_empty(&blkcg->policy_list)) {
1296 spin_lock_irq(&blkcg->lock);
1297 list_for_each_entry(pn, &blkcg->policy_list, node) {
Vivek Goyal062a6442010-09-15 17:06:33 -04001298 if (!pn_matches_cftype(cft, pn))
1299 continue;
1300 blkio_print_policy_node(m, pn);
Jens Axboe0f3942a2010-05-03 14:28:55 +02001301 }
1302 spin_unlock_irq(&blkcg->lock);
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001303 }
Vivek Goyal062a6442010-09-15 17:06:33 -04001304}
1305
1306static int blkiocg_file_read(struct cgroup *cgrp, struct cftype *cft,
1307 struct seq_file *m)
1308{
1309 struct blkio_cgroup *blkcg;
1310 enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
1311 int name = BLKIOFILE_ATTR(cft->private);
1312
1313 blkcg = cgroup_to_blkio_cgroup(cgrp);
1314
1315 switch(plid) {
1316 case BLKIO_POLICY_PROP:
1317 switch(name) {
1318 case BLKIO_PROP_weight_device:
1319 blkio_read_policy_node_files(cft, blkcg, m);
1320 return 0;
1321 default:
1322 BUG();
1323 }
1324 break;
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001325 case BLKIO_POLICY_THROTL:
1326 switch(name){
1327 case BLKIO_THROTL_read_bps_device:
1328 case BLKIO_THROTL_write_bps_device:
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001329 case BLKIO_THROTL_read_iops_device:
1330 case BLKIO_THROTL_write_iops_device:
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001331 blkio_read_policy_node_files(cft, blkcg, m);
1332 return 0;
1333 default:
1334 BUG();
1335 }
1336 break;
Vivek Goyal062a6442010-09-15 17:06:33 -04001337 default:
1338 BUG();
1339 }
1340
1341 return 0;
1342}
1343
1344static int blkio_read_blkg_stats(struct blkio_cgroup *blkcg,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001345 struct cftype *cft, struct cgroup_map_cb *cb,
1346 enum stat_type type, bool show_total, bool pcpu)
Vivek Goyal062a6442010-09-15 17:06:33 -04001347{
1348 struct blkio_group *blkg;
1349 struct hlist_node *n;
1350 uint64_t cgroup_total = 0;
1351
1352 rcu_read_lock();
1353 hlist_for_each_entry_rcu(blkg, n, &blkcg->blkg_list, blkcg_node) {
1354 if (blkg->dev) {
1355 if (!cftype_blkg_same_policy(cft, blkg))
1356 continue;
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001357 if (pcpu)
1358 cgroup_total += blkio_get_stat_cpu(blkg, cb,
1359 blkg->dev, type);
1360 else {
1361 spin_lock_irq(&blkg->stats_lock);
1362 cgroup_total += blkio_get_stat(blkg, cb,
1363 blkg->dev, type);
1364 spin_unlock_irq(&blkg->stats_lock);
1365 }
Vivek Goyal062a6442010-09-15 17:06:33 -04001366 }
1367 }
1368 if (show_total)
1369 cb->fill(cb, "Total", cgroup_total);
1370 rcu_read_unlock();
1371 return 0;
1372}
1373
1374/* All map kind of cgroup file get serviced by this function */
1375static int blkiocg_file_read_map(struct cgroup *cgrp, struct cftype *cft,
1376 struct cgroup_map_cb *cb)
1377{
1378 struct blkio_cgroup *blkcg;
1379 enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
1380 int name = BLKIOFILE_ATTR(cft->private);
1381
1382 blkcg = cgroup_to_blkio_cgroup(cgrp);
1383
1384 switch(plid) {
1385 case BLKIO_POLICY_PROP:
1386 switch(name) {
1387 case BLKIO_PROP_time:
1388 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001389 BLKIO_STAT_TIME, 0, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001390 case BLKIO_PROP_sectors:
1391 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001392 BLKIO_STAT_CPU_SECTORS, 0, 1);
Vivek Goyal062a6442010-09-15 17:06:33 -04001393 case BLKIO_PROP_io_service_bytes:
1394 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001395 BLKIO_STAT_CPU_SERVICE_BYTES, 1, 1);
Vivek Goyal062a6442010-09-15 17:06:33 -04001396 case BLKIO_PROP_io_serviced:
1397 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001398 BLKIO_STAT_CPU_SERVICED, 1, 1);
Vivek Goyal062a6442010-09-15 17:06:33 -04001399 case BLKIO_PROP_io_service_time:
1400 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001401 BLKIO_STAT_SERVICE_TIME, 1, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001402 case BLKIO_PROP_io_wait_time:
1403 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001404 BLKIO_STAT_WAIT_TIME, 1, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001405 case BLKIO_PROP_io_merged:
1406 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal317389a2011-05-23 10:02:19 +02001407 BLKIO_STAT_CPU_MERGED, 1, 1);
Vivek Goyal062a6442010-09-15 17:06:33 -04001408 case BLKIO_PROP_io_queued:
1409 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001410 BLKIO_STAT_QUEUED, 1, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001411#ifdef CONFIG_DEBUG_BLK_CGROUP
Justin TerAvest9026e522011-03-22 21:26:54 +01001412 case BLKIO_PROP_unaccounted_time:
1413 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001414 BLKIO_STAT_UNACCOUNTED_TIME, 0, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001415 case BLKIO_PROP_dequeue:
1416 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001417 BLKIO_STAT_DEQUEUE, 0, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001418 case BLKIO_PROP_avg_queue_size:
1419 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001420 BLKIO_STAT_AVG_QUEUE_SIZE, 0, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001421 case BLKIO_PROP_group_wait_time:
1422 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001423 BLKIO_STAT_GROUP_WAIT_TIME, 0, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001424 case BLKIO_PROP_idle_time:
1425 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001426 BLKIO_STAT_IDLE_TIME, 0, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001427 case BLKIO_PROP_empty_time:
1428 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001429 BLKIO_STAT_EMPTY_TIME, 0, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001430#endif
1431 default:
1432 BUG();
1433 }
1434 break;
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001435 case BLKIO_POLICY_THROTL:
1436 switch(name){
1437 case BLKIO_THROTL_io_service_bytes:
1438 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001439 BLKIO_STAT_CPU_SERVICE_BYTES, 1, 1);
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001440 case BLKIO_THROTL_io_serviced:
1441 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001442 BLKIO_STAT_CPU_SERVICED, 1, 1);
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001443 default:
1444 BUG();
1445 }
1446 break;
Vivek Goyal062a6442010-09-15 17:06:33 -04001447 default:
1448 BUG();
1449 }
1450
1451 return 0;
1452}
1453
1454static int blkio_weight_write(struct blkio_cgroup *blkcg, u64 val)
1455{
1456 struct blkio_group *blkg;
1457 struct hlist_node *n;
1458 struct blkio_policy_node *pn;
1459
1460 if (val < BLKIO_WEIGHT_MIN || val > BLKIO_WEIGHT_MAX)
1461 return -EINVAL;
1462
1463 spin_lock(&blkio_list_lock);
1464 spin_lock_irq(&blkcg->lock);
1465 blkcg->weight = (unsigned int)val;
1466
1467 hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) {
1468 pn = blkio_policy_search_node(blkcg, blkg->dev,
1469 BLKIO_POLICY_PROP, BLKIO_PROP_weight_device);
1470 if (pn)
1471 continue;
1472
1473 blkio_update_group_weight(blkg, blkcg->weight);
1474 }
1475 spin_unlock_irq(&blkcg->lock);
1476 spin_unlock(&blkio_list_lock);
1477 return 0;
1478}
1479
1480static u64 blkiocg_file_read_u64 (struct cgroup *cgrp, struct cftype *cft) {
1481 struct blkio_cgroup *blkcg;
1482 enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
1483 int name = BLKIOFILE_ATTR(cft->private);
1484
1485 blkcg = cgroup_to_blkio_cgroup(cgrp);
1486
1487 switch(plid) {
1488 case BLKIO_POLICY_PROP:
1489 switch(name) {
1490 case BLKIO_PROP_weight:
1491 return (u64)blkcg->weight;
1492 }
1493 break;
1494 default:
1495 BUG();
1496 }
1497 return 0;
1498}
1499
1500static int
1501blkiocg_file_write_u64(struct cgroup *cgrp, struct cftype *cft, u64 val)
1502{
1503 struct blkio_cgroup *blkcg;
1504 enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
1505 int name = BLKIOFILE_ATTR(cft->private);
1506
1507 blkcg = cgroup_to_blkio_cgroup(cgrp);
1508
1509 switch(plid) {
1510 case BLKIO_POLICY_PROP:
1511 switch(name) {
1512 case BLKIO_PROP_weight:
1513 return blkio_weight_write(blkcg, val);
1514 }
1515 break;
1516 default:
1517 BUG();
1518 }
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001519
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001520 return 0;
1521}
1522
Vivek Goyal31e4c282009-12-03 12:59:42 -05001523struct cftype blkio_files[] = {
1524 {
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001525 .name = "weight_device",
Vivek Goyal062a6442010-09-15 17:06:33 -04001526 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1527 BLKIO_PROP_weight_device),
1528 .read_seq_string = blkiocg_file_read,
1529 .write_string = blkiocg_file_write,
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001530 .max_write_len = 256,
1531 },
1532 {
Vivek Goyal31e4c282009-12-03 12:59:42 -05001533 .name = "weight",
Vivek Goyal062a6442010-09-15 17:06:33 -04001534 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1535 BLKIO_PROP_weight),
1536 .read_u64 = blkiocg_file_read_u64,
1537 .write_u64 = blkiocg_file_write_u64,
Vivek Goyal31e4c282009-12-03 12:59:42 -05001538 },
Vivek Goyal22084192009-12-03 12:59:49 -05001539 {
1540 .name = "time",
Vivek Goyal13f98252010-10-01 14:49:41 +02001541 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1542 BLKIO_PROP_time),
1543 .read_map = blkiocg_file_read_map,
Vivek Goyal22084192009-12-03 12:59:49 -05001544 },
1545 {
1546 .name = "sectors",
Vivek Goyal13f98252010-10-01 14:49:41 +02001547 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1548 BLKIO_PROP_sectors),
1549 .read_map = blkiocg_file_read_map,
Divyesh Shah303a3ac2010-04-01 15:01:24 -07001550 },
1551 {
1552 .name = "io_service_bytes",
Vivek Goyal13f98252010-10-01 14:49:41 +02001553 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1554 BLKIO_PROP_io_service_bytes),
1555 .read_map = blkiocg_file_read_map,
Divyesh Shah303a3ac2010-04-01 15:01:24 -07001556 },
1557 {
1558 .name = "io_serviced",
Vivek Goyal13f98252010-10-01 14:49:41 +02001559 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1560 BLKIO_PROP_io_serviced),
1561 .read_map = blkiocg_file_read_map,
Divyesh Shah303a3ac2010-04-01 15:01:24 -07001562 },
1563 {
1564 .name = "io_service_time",
Vivek Goyal13f98252010-10-01 14:49:41 +02001565 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1566 BLKIO_PROP_io_service_time),
1567 .read_map = blkiocg_file_read_map,
Divyesh Shah303a3ac2010-04-01 15:01:24 -07001568 },
1569 {
1570 .name = "io_wait_time",
Vivek Goyal13f98252010-10-01 14:49:41 +02001571 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1572 BLKIO_PROP_io_wait_time),
1573 .read_map = blkiocg_file_read_map,
Divyesh Shah84c124d2010-04-09 08:31:19 +02001574 },
1575 {
Divyesh Shah812d4022010-04-08 21:14:23 -07001576 .name = "io_merged",
Vivek Goyal13f98252010-10-01 14:49:41 +02001577 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1578 BLKIO_PROP_io_merged),
1579 .read_map = blkiocg_file_read_map,
Divyesh Shah812d4022010-04-08 21:14:23 -07001580 },
1581 {
Divyesh Shahcdc11842010-04-08 21:15:10 -07001582 .name = "io_queued",
Vivek Goyal13f98252010-10-01 14:49:41 +02001583 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1584 BLKIO_PROP_io_queued),
1585 .read_map = blkiocg_file_read_map,
Divyesh Shahcdc11842010-04-08 21:15:10 -07001586 },
1587 {
Divyesh Shah84c124d2010-04-09 08:31:19 +02001588 .name = "reset_stats",
1589 .write_u64 = blkiocg_reset_stats,
Vivek Goyal22084192009-12-03 12:59:49 -05001590 },
Vivek Goyal13f98252010-10-01 14:49:41 +02001591#ifdef CONFIG_BLK_DEV_THROTTLING
1592 {
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001593 .name = "throttle.read_bps_device",
1594 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1595 BLKIO_THROTL_read_bps_device),
1596 .read_seq_string = blkiocg_file_read,
1597 .write_string = blkiocg_file_write,
1598 .max_write_len = 256,
1599 },
1600
1601 {
1602 .name = "throttle.write_bps_device",
1603 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1604 BLKIO_THROTL_write_bps_device),
1605 .read_seq_string = blkiocg_file_read,
1606 .write_string = blkiocg_file_write,
1607 .max_write_len = 256,
1608 },
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001609
1610 {
1611 .name = "throttle.read_iops_device",
1612 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1613 BLKIO_THROTL_read_iops_device),
1614 .read_seq_string = blkiocg_file_read,
1615 .write_string = blkiocg_file_write,
1616 .max_write_len = 256,
1617 },
1618
1619 {
1620 .name = "throttle.write_iops_device",
1621 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1622 BLKIO_THROTL_write_iops_device),
1623 .read_seq_string = blkiocg_file_read,
1624 .write_string = blkiocg_file_write,
1625 .max_write_len = 256,
1626 },
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001627 {
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001628 .name = "throttle.io_service_bytes",
1629 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1630 BLKIO_THROTL_io_service_bytes),
1631 .read_map = blkiocg_file_read_map,
1632 },
1633 {
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001634 .name = "throttle.io_serviced",
1635 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1636 BLKIO_THROTL_io_serviced),
1637 .read_map = blkiocg_file_read_map,
1638 },
Vivek Goyal13f98252010-10-01 14:49:41 +02001639#endif /* CONFIG_BLK_DEV_THROTTLING */
1640
Vivek Goyal22084192009-12-03 12:59:49 -05001641#ifdef CONFIG_DEBUG_BLK_CGROUP
Divyesh Shahcdc11842010-04-08 21:15:10 -07001642 {
1643 .name = "avg_queue_size",
Vivek Goyal062a6442010-09-15 17:06:33 -04001644 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1645 BLKIO_PROP_avg_queue_size),
1646 .read_map = blkiocg_file_read_map,
Divyesh Shahcdc11842010-04-08 21:15:10 -07001647 },
1648 {
Divyesh Shah812df482010-04-08 21:15:35 -07001649 .name = "group_wait_time",
Vivek Goyal062a6442010-09-15 17:06:33 -04001650 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1651 BLKIO_PROP_group_wait_time),
1652 .read_map = blkiocg_file_read_map,
Divyesh Shah812df482010-04-08 21:15:35 -07001653 },
1654 {
1655 .name = "idle_time",
Vivek Goyal062a6442010-09-15 17:06:33 -04001656 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1657 BLKIO_PROP_idle_time),
1658 .read_map = blkiocg_file_read_map,
Divyesh Shah812df482010-04-08 21:15:35 -07001659 },
1660 {
1661 .name = "empty_time",
Vivek Goyal062a6442010-09-15 17:06:33 -04001662 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1663 BLKIO_PROP_empty_time),
1664 .read_map = blkiocg_file_read_map,
Divyesh Shah812df482010-04-08 21:15:35 -07001665 },
1666 {
Vivek Goyal22084192009-12-03 12:59:49 -05001667 .name = "dequeue",
Vivek Goyal062a6442010-09-15 17:06:33 -04001668 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1669 BLKIO_PROP_dequeue),
1670 .read_map = blkiocg_file_read_map,
Divyesh Shahcdc11842010-04-08 21:15:10 -07001671 },
Justin TerAvest9026e522011-03-22 21:26:54 +01001672 {
1673 .name = "unaccounted_time",
1674 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1675 BLKIO_PROP_unaccounted_time),
1676 .read_map = blkiocg_file_read_map,
1677 },
Vivek Goyal22084192009-12-03 12:59:49 -05001678#endif
Vivek Goyal31e4c282009-12-03 12:59:42 -05001679};
1680
1681static int blkiocg_populate(struct cgroup_subsys *subsys, struct cgroup *cgroup)
1682{
1683 return cgroup_add_files(cgroup, subsys, blkio_files,
1684 ARRAY_SIZE(blkio_files));
1685}
1686
1687static void blkiocg_destroy(struct cgroup_subsys *subsys, struct cgroup *cgroup)
1688{
1689 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgroup);
Vivek Goyalb1c35762009-12-03 12:59:47 -05001690 unsigned long flags;
1691 struct blkio_group *blkg;
Tejun Heoca32aef2012-03-05 13:15:03 -08001692 struct request_queue *q;
Vivek Goyal3e252062009-12-04 10:36:42 -05001693 struct blkio_policy_type *blkiop;
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001694 struct blkio_policy_node *pn, *pntmp;
Vivek Goyal31e4c282009-12-03 12:59:42 -05001695
Vivek Goyalb1c35762009-12-03 12:59:47 -05001696 rcu_read_lock();
Jens Axboe0f3942a2010-05-03 14:28:55 +02001697 do {
1698 spin_lock_irqsave(&blkcg->lock, flags);
Vivek Goyalb1c35762009-12-03 12:59:47 -05001699
Jens Axboe0f3942a2010-05-03 14:28:55 +02001700 if (hlist_empty(&blkcg->blkg_list)) {
1701 spin_unlock_irqrestore(&blkcg->lock, flags);
1702 break;
1703 }
1704
1705 blkg = hlist_entry(blkcg->blkg_list.first, struct blkio_group,
1706 blkcg_node);
Tejun Heoca32aef2012-03-05 13:15:03 -08001707 q = rcu_dereference(blkg->q);
Jens Axboe0f3942a2010-05-03 14:28:55 +02001708 __blkiocg_del_blkio_group(blkg);
1709
Vivek Goyalb1c35762009-12-03 12:59:47 -05001710 spin_unlock_irqrestore(&blkcg->lock, flags);
Vivek Goyalb1c35762009-12-03 12:59:47 -05001711
Jens Axboe0f3942a2010-05-03 14:28:55 +02001712 /*
1713 * This blkio_group is being unlinked as associated cgroup is
1714 * going away. Let all the IO controlling policies know about
Vivek Goyal61014e92010-10-01 14:49:44 +02001715 * this event.
Jens Axboe0f3942a2010-05-03 14:28:55 +02001716 */
1717 spin_lock(&blkio_list_lock);
Vivek Goyal61014e92010-10-01 14:49:44 +02001718 list_for_each_entry(blkiop, &blkio_list, list) {
1719 if (blkiop->plid != blkg->plid)
1720 continue;
Tejun Heoca32aef2012-03-05 13:15:03 -08001721 blkiop->ops.blkio_unlink_group_fn(q, blkg);
Vivek Goyal61014e92010-10-01 14:49:44 +02001722 }
Jens Axboe0f3942a2010-05-03 14:28:55 +02001723 spin_unlock(&blkio_list_lock);
1724 } while (1);
Vivek Goyalb1c35762009-12-03 12:59:47 -05001725
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001726 list_for_each_entry_safe(pn, pntmp, &blkcg->policy_list, node) {
1727 blkio_policy_delete_node(pn);
1728 kfree(pn);
1729 }
Jens Axboe0f3942a2010-05-03 14:28:55 +02001730
Vivek Goyal31e4c282009-12-03 12:59:42 -05001731 free_css_id(&blkio_subsys, &blkcg->css);
Vivek Goyalb1c35762009-12-03 12:59:47 -05001732 rcu_read_unlock();
Ben Blum67523c42010-03-10 15:22:11 -08001733 if (blkcg != &blkio_root_cgroup)
1734 kfree(blkcg);
Vivek Goyal31e4c282009-12-03 12:59:42 -05001735}
1736
1737static struct cgroup_subsys_state *
1738blkiocg_create(struct cgroup_subsys *subsys, struct cgroup *cgroup)
1739{
Li Zefan03415092010-05-07 08:57:00 +02001740 struct blkio_cgroup *blkcg;
1741 struct cgroup *parent = cgroup->parent;
Vivek Goyal31e4c282009-12-03 12:59:42 -05001742
Li Zefan03415092010-05-07 08:57:00 +02001743 if (!parent) {
Vivek Goyal31e4c282009-12-03 12:59:42 -05001744 blkcg = &blkio_root_cgroup;
1745 goto done;
1746 }
1747
Vivek Goyal31e4c282009-12-03 12:59:42 -05001748 blkcg = kzalloc(sizeof(*blkcg), GFP_KERNEL);
1749 if (!blkcg)
1750 return ERR_PTR(-ENOMEM);
1751
1752 blkcg->weight = BLKIO_WEIGHT_DEFAULT;
1753done:
1754 spin_lock_init(&blkcg->lock);
1755 INIT_HLIST_HEAD(&blkcg->blkg_list);
1756
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001757 INIT_LIST_HEAD(&blkcg->policy_list);
Vivek Goyal31e4c282009-12-03 12:59:42 -05001758 return &blkcg->css;
1759}
1760
1761/*
1762 * We cannot support shared io contexts, as we have no mean to support
1763 * two tasks with the same ioc in two different groups without major rework
1764 * of the main cic data structures. For now we allow a task to change
1765 * its cgroup only if it's the only owner of its ioc.
1766 */
Tejun Heobb9d97b2011-12-12 18:12:21 -08001767static int blkiocg_can_attach(struct cgroup_subsys *ss, struct cgroup *cgrp,
1768 struct cgroup_taskset *tset)
Vivek Goyal31e4c282009-12-03 12:59:42 -05001769{
Tejun Heobb9d97b2011-12-12 18:12:21 -08001770 struct task_struct *task;
Vivek Goyal31e4c282009-12-03 12:59:42 -05001771 struct io_context *ioc;
1772 int ret = 0;
1773
1774 /* task_lock() is needed to avoid races with exit_io_context() */
Tejun Heobb9d97b2011-12-12 18:12:21 -08001775 cgroup_taskset_for_each(task, cgrp, tset) {
1776 task_lock(task);
1777 ioc = task->io_context;
1778 if (ioc && atomic_read(&ioc->nr_tasks) > 1)
1779 ret = -EINVAL;
1780 task_unlock(task);
1781 if (ret)
1782 break;
1783 }
Vivek Goyal31e4c282009-12-03 12:59:42 -05001784 return ret;
1785}
1786
Tejun Heobb9d97b2011-12-12 18:12:21 -08001787static void blkiocg_attach(struct cgroup_subsys *ss, struct cgroup *cgrp,
1788 struct cgroup_taskset *tset)
Vivek Goyal31e4c282009-12-03 12:59:42 -05001789{
Tejun Heobb9d97b2011-12-12 18:12:21 -08001790 struct task_struct *task;
Vivek Goyal31e4c282009-12-03 12:59:42 -05001791 struct io_context *ioc;
1792
Tejun Heobb9d97b2011-12-12 18:12:21 -08001793 cgroup_taskset_for_each(task, cgrp, tset) {
Linus Torvaldsb3c9dd12012-01-15 12:24:45 -08001794 /* we don't lose anything even if ioc allocation fails */
1795 ioc = get_task_io_context(task, GFP_ATOMIC, NUMA_NO_NODE);
1796 if (ioc) {
1797 ioc_cgroup_changed(ioc);
Tejun Heo11a31222012-02-07 07:51:30 +01001798 put_io_context(ioc);
Linus Torvaldsb3c9dd12012-01-15 12:24:45 -08001799 }
Tejun Heobb9d97b2011-12-12 18:12:21 -08001800 }
Vivek Goyal31e4c282009-12-03 12:59:42 -05001801}
1802
Vivek Goyal3e252062009-12-04 10:36:42 -05001803void blkio_policy_register(struct blkio_policy_type *blkiop)
1804{
1805 spin_lock(&blkio_list_lock);
Tejun Heo035d10b2012-03-05 13:15:04 -08001806
1807 BUG_ON(blkio_policy[blkiop->plid]);
1808 blkio_policy[blkiop->plid] = blkiop;
Vivek Goyal3e252062009-12-04 10:36:42 -05001809 list_add_tail(&blkiop->list, &blkio_list);
Tejun Heo035d10b2012-03-05 13:15:04 -08001810
Vivek Goyal3e252062009-12-04 10:36:42 -05001811 spin_unlock(&blkio_list_lock);
1812}
1813EXPORT_SYMBOL_GPL(blkio_policy_register);
1814
1815void blkio_policy_unregister(struct blkio_policy_type *blkiop)
1816{
1817 spin_lock(&blkio_list_lock);
Tejun Heo035d10b2012-03-05 13:15:04 -08001818
1819 BUG_ON(blkio_policy[blkiop->plid] != blkiop);
1820 blkio_policy[blkiop->plid] = NULL;
Vivek Goyal3e252062009-12-04 10:36:42 -05001821 list_del_init(&blkiop->list);
Tejun Heo035d10b2012-03-05 13:15:04 -08001822
Vivek Goyal3e252062009-12-04 10:36:42 -05001823 spin_unlock(&blkio_list_lock);
1824}
1825EXPORT_SYMBOL_GPL(blkio_policy_unregister);