blob: 951e7f3a8c899204ab090eb55683519e3dde6d6c [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>
Tejun Heo9a9e8a22012-03-19 15:10:56 -070022#include <linux/atomic.h>
Tejun Heo72e06c22012-03-05 13:15:00 -080023#include "blk-cgroup.h"
Tejun Heo5efd6112012-03-05 13:15:12 -080024#include "blk.h"
Vivek Goyal3e252062009-12-04 10:36:42 -050025
Divyesh Shah84c124d2010-04-09 08:31:19 +020026#define MAX_KEY_LEN 100
27
Vivek Goyal3e252062009-12-04 10:36:42 -050028static DEFINE_SPINLOCK(blkio_list_lock);
29static LIST_HEAD(blkio_list);
Vivek Goyalb1c35762009-12-03 12:59:47 -050030
Tejun Heo923adde2012-03-05 13:15:13 -080031static DEFINE_MUTEX(all_q_mutex);
32static LIST_HEAD(all_q_list);
33
Vivek Goyal1cd9e032012-03-08 10:53:56 -080034/* List of groups pending per cpu stats allocation */
35static DEFINE_SPINLOCK(alloc_list_lock);
36static LIST_HEAD(alloc_list);
37
38static void blkio_stat_alloc_fn(struct work_struct *);
39static DECLARE_DELAYED_WORK(blkio_stat_alloc_work, blkio_stat_alloc_fn);
40
Vivek Goyal31e4c282009-12-03 12:59:42 -050041struct blkio_cgroup blkio_root_cgroup = { .weight = 2*BLKIO_WEIGHT_DEFAULT };
Vivek Goyal9d6a9862009-12-04 10:36:41 -050042EXPORT_SYMBOL_GPL(blkio_root_cgroup);
43
Tejun Heo035d10b2012-03-05 13:15:04 -080044static struct blkio_policy_type *blkio_policy[BLKIO_NR_POLICIES];
45
Vivek Goyal062a6442010-09-15 17:06:33 -040046/* for encoding cft->private value on file */
47#define BLKIOFILE_PRIVATE(x, val) (((x) << 16) | (val))
48/* What policy owns the file, proportional or throttle */
49#define BLKIOFILE_POLICY(val) (((val) >> 16) & 0xffff)
50#define BLKIOFILE_ATTR(val) ((val) & 0xffff)
51
Vivek Goyal31e4c282009-12-03 12:59:42 -050052struct blkio_cgroup *cgroup_to_blkio_cgroup(struct cgroup *cgroup)
53{
54 return container_of(cgroup_subsys_state(cgroup, blkio_subsys_id),
55 struct blkio_cgroup, css);
56}
Vivek Goyal9d6a9862009-12-04 10:36:41 -050057EXPORT_SYMBOL_GPL(cgroup_to_blkio_cgroup);
Vivek Goyal31e4c282009-12-03 12:59:42 -050058
Tejun Heo4f85cb92012-03-05 13:15:28 -080059static struct blkio_cgroup *task_blkio_cgroup(struct task_struct *tsk)
Vivek Goyal70087dc2011-05-16 15:24:08 +020060{
61 return container_of(task_subsys_state(tsk, blkio_subsys_id),
62 struct blkio_cgroup, css);
63}
Tejun Heo4f85cb92012-03-05 13:15:28 -080064
65struct blkio_cgroup *bio_blkio_cgroup(struct bio *bio)
66{
67 if (bio && bio->bi_css)
68 return container_of(bio->bi_css, struct blkio_cgroup, css);
69 return task_blkio_cgroup(current);
70}
71EXPORT_SYMBOL_GPL(bio_blkio_cgroup);
Vivek Goyal70087dc2011-05-16 15:24:08 +020072
Tejun Heoc1768262012-03-05 13:15:17 -080073static inline void blkio_update_group_weight(struct blkio_group *blkg,
74 int plid, unsigned int weight)
Vivek Goyal062a6442010-09-15 17:06:33 -040075{
76 struct blkio_policy_type *blkiop;
77
78 list_for_each_entry(blkiop, &blkio_list, list) {
79 /* If this policy does not own the blkg, do not send updates */
Tejun Heoc1768262012-03-05 13:15:17 -080080 if (blkiop->plid != plid)
Vivek Goyal062a6442010-09-15 17:06:33 -040081 continue;
82 if (blkiop->ops.blkio_update_group_weight_fn)
Tejun Heoca32aef2012-03-05 13:15:03 -080083 blkiop->ops.blkio_update_group_weight_fn(blkg->q,
Vivek Goyalfe071432010-10-01 14:49:49 +020084 blkg, weight);
Vivek Goyal062a6442010-09-15 17:06:33 -040085 }
86}
87
Tejun Heoc1768262012-03-05 13:15:17 -080088static inline void blkio_update_group_bps(struct blkio_group *blkg, int plid,
89 u64 bps, int fileid)
Vivek Goyal4c9eefa2010-09-15 17:06:34 -040090{
91 struct blkio_policy_type *blkiop;
92
93 list_for_each_entry(blkiop, &blkio_list, list) {
94
95 /* If this policy does not own the blkg, do not send updates */
Tejun Heoc1768262012-03-05 13:15:17 -080096 if (blkiop->plid != plid)
Vivek Goyal4c9eefa2010-09-15 17:06:34 -040097 continue;
98
99 if (fileid == BLKIO_THROTL_read_bps_device
100 && blkiop->ops.blkio_update_group_read_bps_fn)
Tejun Heoca32aef2012-03-05 13:15:03 -0800101 blkiop->ops.blkio_update_group_read_bps_fn(blkg->q,
Vivek Goyalfe071432010-10-01 14:49:49 +0200102 blkg, bps);
Vivek Goyal4c9eefa2010-09-15 17:06:34 -0400103
104 if (fileid == BLKIO_THROTL_write_bps_device
105 && blkiop->ops.blkio_update_group_write_bps_fn)
Tejun Heoca32aef2012-03-05 13:15:03 -0800106 blkiop->ops.blkio_update_group_write_bps_fn(blkg->q,
Vivek Goyalfe071432010-10-01 14:49:49 +0200107 blkg, bps);
Vivek Goyal4c9eefa2010-09-15 17:06:34 -0400108 }
109}
110
Vivek Goyal7702e8f2010-09-15 17:06:36 -0400111static inline void blkio_update_group_iops(struct blkio_group *blkg,
Tejun Heoc1768262012-03-05 13:15:17 -0800112 int plid, unsigned int iops,
113 int fileid)
Vivek Goyal7702e8f2010-09-15 17:06:36 -0400114{
115 struct blkio_policy_type *blkiop;
116
117 list_for_each_entry(blkiop, &blkio_list, list) {
118
119 /* If this policy does not own the blkg, do not send updates */
Tejun Heoc1768262012-03-05 13:15:17 -0800120 if (blkiop->plid != plid)
Vivek Goyal7702e8f2010-09-15 17:06:36 -0400121 continue;
122
123 if (fileid == BLKIO_THROTL_read_iops_device
124 && blkiop->ops.blkio_update_group_read_iops_fn)
Tejun Heoca32aef2012-03-05 13:15:03 -0800125 blkiop->ops.blkio_update_group_read_iops_fn(blkg->q,
Vivek Goyalfe071432010-10-01 14:49:49 +0200126 blkg, iops);
Vivek Goyal7702e8f2010-09-15 17:06:36 -0400127
128 if (fileid == BLKIO_THROTL_write_iops_device
129 && blkiop->ops.blkio_update_group_write_iops_fn)
Tejun Heoca32aef2012-03-05 13:15:03 -0800130 blkiop->ops.blkio_update_group_write_iops_fn(blkg->q,
Vivek Goyalfe071432010-10-01 14:49:49 +0200131 blkg,iops);
Vivek Goyal7702e8f2010-09-15 17:06:36 -0400132 }
133}
134
Divyesh Shahcdc11842010-04-08 21:15:10 -0700135#ifdef CONFIG_DEBUG_BLK_CGROUP
Tejun Heoedf1b872012-03-08 10:54:00 -0800136/* This should be called with the queue_lock held. */
Divyesh Shah812df482010-04-08 21:15:35 -0700137static void blkio_set_start_group_wait_time(struct blkio_group *blkg,
Tejun Heoc1768262012-03-05 13:15:17 -0800138 struct blkio_policy_type *pol,
139 struct blkio_group *curr_blkg)
Divyesh Shah812df482010-04-08 21:15:35 -0700140{
Tejun Heoc1768262012-03-05 13:15:17 -0800141 struct blkg_policy_data *pd = blkg->pd[pol->plid];
Tejun Heo549d3aa2012-03-05 13:15:16 -0800142
143 if (blkio_blkg_waiting(&pd->stats))
Divyesh Shah812df482010-04-08 21:15:35 -0700144 return;
145 if (blkg == curr_blkg)
146 return;
Tejun Heo549d3aa2012-03-05 13:15:16 -0800147 pd->stats.start_group_wait_time = sched_clock();
148 blkio_mark_blkg_waiting(&pd->stats);
Divyesh Shah812df482010-04-08 21:15:35 -0700149}
150
Tejun Heoedf1b872012-03-08 10:54:00 -0800151/* This should be called with the queue_lock held. */
Divyesh Shah812df482010-04-08 21:15:35 -0700152static void blkio_update_group_wait_time(struct blkio_group_stats *stats)
153{
154 unsigned long long now;
155
156 if (!blkio_blkg_waiting(stats))
157 return;
158
159 now = sched_clock();
160 if (time_after64(now, stats->start_group_wait_time))
Tejun Heoedcb0722012-04-01 14:38:42 -0700161 blkg_stat_add(&stats->group_wait_time,
162 now - stats->start_group_wait_time);
Divyesh Shah812df482010-04-08 21:15:35 -0700163 blkio_clear_blkg_waiting(stats);
164}
165
Tejun Heoedf1b872012-03-08 10:54:00 -0800166/* This should be called with the queue_lock held. */
Divyesh Shah812df482010-04-08 21:15:35 -0700167static void blkio_end_empty_time(struct blkio_group_stats *stats)
168{
169 unsigned long long now;
170
171 if (!blkio_blkg_empty(stats))
172 return;
173
174 now = sched_clock();
175 if (time_after64(now, stats->start_empty_time))
Tejun Heoedcb0722012-04-01 14:38:42 -0700176 blkg_stat_add(&stats->empty_time,
177 now - stats->start_empty_time);
Divyesh Shah812df482010-04-08 21:15:35 -0700178 blkio_clear_blkg_empty(stats);
179}
180
Tejun Heoc1768262012-03-05 13:15:17 -0800181void blkiocg_update_set_idle_time_stats(struct blkio_group *blkg,
182 struct blkio_policy_type *pol)
Divyesh Shah812df482010-04-08 21:15:35 -0700183{
Tejun Heoedf1b872012-03-08 10:54:00 -0800184 struct blkio_group_stats *stats = &blkg->pd[pol->plid]->stats;
Divyesh Shah812df482010-04-08 21:15:35 -0700185
Tejun Heoedf1b872012-03-08 10:54:00 -0800186 lockdep_assert_held(blkg->q->queue_lock);
187 BUG_ON(blkio_blkg_idling(stats));
188
189 stats->start_idle_time = sched_clock();
190 blkio_mark_blkg_idling(stats);
Divyesh Shah812df482010-04-08 21:15:35 -0700191}
192EXPORT_SYMBOL_GPL(blkiocg_update_set_idle_time_stats);
193
Tejun Heoc1768262012-03-05 13:15:17 -0800194void blkiocg_update_idle_time_stats(struct blkio_group *blkg,
195 struct blkio_policy_type *pol)
Divyesh Shah812df482010-04-08 21:15:35 -0700196{
Tejun Heoedf1b872012-03-08 10:54:00 -0800197 struct blkio_group_stats *stats = &blkg->pd[pol->plid]->stats;
Divyesh Shah812df482010-04-08 21:15:35 -0700198
Tejun Heoedf1b872012-03-08 10:54:00 -0800199 lockdep_assert_held(blkg->q->queue_lock);
200
Divyesh Shah812df482010-04-08 21:15:35 -0700201 if (blkio_blkg_idling(stats)) {
Tejun Heoedf1b872012-03-08 10:54:00 -0800202 unsigned long long now = sched_clock();
203
Tejun Heoedcb0722012-04-01 14:38:42 -0700204 if (time_after64(now, stats->start_idle_time))
205 blkg_stat_add(&stats->idle_time,
206 now - stats->start_idle_time);
Divyesh Shah812df482010-04-08 21:15:35 -0700207 blkio_clear_blkg_idling(stats);
208 }
Divyesh Shah812df482010-04-08 21:15:35 -0700209}
210EXPORT_SYMBOL_GPL(blkiocg_update_idle_time_stats);
211
Tejun Heoc1768262012-03-05 13:15:17 -0800212void blkiocg_update_avg_queue_size_stats(struct blkio_group *blkg,
213 struct blkio_policy_type *pol)
Divyesh Shahcdc11842010-04-08 21:15:10 -0700214{
Tejun Heoedf1b872012-03-08 10:54:00 -0800215 struct blkio_group_stats *stats = &blkg->pd[pol->plid]->stats;
Divyesh Shahcdc11842010-04-08 21:15:10 -0700216
Tejun Heoedf1b872012-03-08 10:54:00 -0800217 lockdep_assert_held(blkg->q->queue_lock);
218
Tejun Heoedcb0722012-04-01 14:38:42 -0700219 blkg_stat_add(&stats->avg_queue_size_sum,
220 blkg_rwstat_sum(&stats->queued));
221 blkg_stat_add(&stats->avg_queue_size_samples, 1);
Divyesh Shah812df482010-04-08 21:15:35 -0700222 blkio_update_group_wait_time(stats);
Divyesh Shahcdc11842010-04-08 21:15:10 -0700223}
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200224EXPORT_SYMBOL_GPL(blkiocg_update_avg_queue_size_stats);
225
Tejun Heoc1768262012-03-05 13:15:17 -0800226void blkiocg_set_start_empty_time(struct blkio_group *blkg,
227 struct blkio_policy_type *pol)
Divyesh Shah28baf442010-04-14 11:22:38 +0200228{
Tejun Heoedf1b872012-03-08 10:54:00 -0800229 struct blkio_group_stats *stats = &blkg->pd[pol->plid]->stats;
Divyesh Shah28baf442010-04-14 11:22:38 +0200230
Tejun Heoedf1b872012-03-08 10:54:00 -0800231 lockdep_assert_held(blkg->q->queue_lock);
Divyesh Shah28baf442010-04-14 11:22:38 +0200232
Tejun Heoedcb0722012-04-01 14:38:42 -0700233 if (blkg_rwstat_sum(&stats->queued))
Divyesh Shah28baf442010-04-14 11:22:38 +0200234 return;
Divyesh Shah28baf442010-04-14 11:22:38 +0200235
236 /*
Vivek Goyale5ff0822010-04-26 19:25:11 +0200237 * group is already marked empty. This can happen if cfqq got new
238 * request in parent group and moved to this group while being added
239 * to service tree. Just ignore the event and move on.
Divyesh Shah28baf442010-04-14 11:22:38 +0200240 */
Tejun Heoedf1b872012-03-08 10:54:00 -0800241 if (blkio_blkg_empty(stats))
Vivek Goyale5ff0822010-04-26 19:25:11 +0200242 return;
Vivek Goyale5ff0822010-04-26 19:25:11 +0200243
Divyesh Shah28baf442010-04-14 11:22:38 +0200244 stats->start_empty_time = sched_clock();
245 blkio_mark_blkg_empty(stats);
Divyesh Shah28baf442010-04-14 11:22:38 +0200246}
247EXPORT_SYMBOL_GPL(blkiocg_set_start_empty_time);
248
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200249void blkiocg_update_dequeue_stats(struct blkio_group *blkg,
Tejun Heoc1768262012-03-05 13:15:17 -0800250 struct blkio_policy_type *pol,
251 unsigned long dequeue)
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200252{
Tejun Heoc1768262012-03-05 13:15:17 -0800253 struct blkg_policy_data *pd = blkg->pd[pol->plid];
Tejun Heo549d3aa2012-03-05 13:15:16 -0800254
Tejun Heoedf1b872012-03-08 10:54:00 -0800255 lockdep_assert_held(blkg->q->queue_lock);
256
Tejun Heoedcb0722012-04-01 14:38:42 -0700257 blkg_stat_add(&pd->stats.dequeue, dequeue);
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200258}
259EXPORT_SYMBOL_GPL(blkiocg_update_dequeue_stats);
Divyesh Shah812df482010-04-08 21:15:35 -0700260#else
261static inline void blkio_set_start_group_wait_time(struct blkio_group *blkg,
Tejun Heoc1768262012-03-05 13:15:17 -0800262 struct blkio_policy_type *pol,
263 struct blkio_group *curr_blkg) { }
264static inline void blkio_end_empty_time(struct blkio_group_stats *stats) { }
Divyesh Shahcdc11842010-04-08 21:15:10 -0700265#endif
266
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200267void blkiocg_update_io_add_stats(struct blkio_group *blkg,
Tejun Heoc1768262012-03-05 13:15:17 -0800268 struct blkio_policy_type *pol,
269 struct blkio_group *curr_blkg, bool direction,
270 bool sync)
Divyesh Shahcdc11842010-04-08 21:15:10 -0700271{
Tejun Heoedf1b872012-03-08 10:54:00 -0800272 struct blkio_group_stats *stats = &blkg->pd[pol->plid]->stats;
Tejun Heoedcb0722012-04-01 14:38:42 -0700273 int rw = (direction ? REQ_WRITE : 0) | (sync ? REQ_SYNC : 0);
Divyesh Shahcdc11842010-04-08 21:15:10 -0700274
Tejun Heoedf1b872012-03-08 10:54:00 -0800275 lockdep_assert_held(blkg->q->queue_lock);
276
Tejun Heoedcb0722012-04-01 14:38:42 -0700277 blkg_rwstat_add(&stats->queued, rw, 1);
Tejun Heoedf1b872012-03-08 10:54:00 -0800278 blkio_end_empty_time(stats);
Tejun Heoc1768262012-03-05 13:15:17 -0800279 blkio_set_start_group_wait_time(blkg, pol, curr_blkg);
Divyesh Shahcdc11842010-04-08 21:15:10 -0700280}
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200281EXPORT_SYMBOL_GPL(blkiocg_update_io_add_stats);
Divyesh Shahcdc11842010-04-08 21:15:10 -0700282
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200283void blkiocg_update_io_remove_stats(struct blkio_group *blkg,
Tejun Heoc1768262012-03-05 13:15:17 -0800284 struct blkio_policy_type *pol,
285 bool direction, bool sync)
Divyesh Shahcdc11842010-04-08 21:15:10 -0700286{
Tejun Heoedf1b872012-03-08 10:54:00 -0800287 struct blkio_group_stats *stats = &blkg->pd[pol->plid]->stats;
Tejun Heoedcb0722012-04-01 14:38:42 -0700288 int rw = (direction ? REQ_WRITE : 0) | (sync ? REQ_SYNC : 0);
Divyesh Shahcdc11842010-04-08 21:15:10 -0700289
Tejun Heoedf1b872012-03-08 10:54:00 -0800290 lockdep_assert_held(blkg->q->queue_lock);
291
Tejun Heoedcb0722012-04-01 14:38:42 -0700292 blkg_rwstat_add(&stats->queued, rw, -1);
Divyesh Shahcdc11842010-04-08 21:15:10 -0700293}
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200294EXPORT_SYMBOL_GPL(blkiocg_update_io_remove_stats);
Divyesh Shahcdc11842010-04-08 21:15:10 -0700295
Tejun Heoc1768262012-03-05 13:15:17 -0800296void blkiocg_update_timeslice_used(struct blkio_group *blkg,
297 struct blkio_policy_type *pol,
298 unsigned long time,
299 unsigned long unaccounted_time)
Vivek Goyal22084192009-12-03 12:59:49 -0500300{
Tejun Heoedf1b872012-03-08 10:54:00 -0800301 struct blkio_group_stats *stats = &blkg->pd[pol->plid]->stats;
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700302
Tejun Heoedf1b872012-03-08 10:54:00 -0800303 lockdep_assert_held(blkg->q->queue_lock);
304
Tejun Heoedcb0722012-04-01 14:38:42 -0700305 blkg_stat_add(&stats->time, time);
Vivek Goyala23e6862011-05-19 15:38:20 -0400306#ifdef CONFIG_DEBUG_BLK_CGROUP
Tejun Heoedcb0722012-04-01 14:38:42 -0700307 blkg_stat_add(&stats->unaccounted_time, unaccounted_time);
Vivek Goyala23e6862011-05-19 15:38:20 -0400308#endif
Vivek Goyal22084192009-12-03 12:59:49 -0500309}
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700310EXPORT_SYMBOL_GPL(blkiocg_update_timeslice_used);
Vivek Goyal22084192009-12-03 12:59:49 -0500311
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400312/*
313 * should be called under rcu read lock or queue lock to make sure blkg pointer
314 * is valid.
315 */
Divyesh Shah84c124d2010-04-09 08:31:19 +0200316void blkiocg_update_dispatch_stats(struct blkio_group *blkg,
Tejun Heoc1768262012-03-05 13:15:17 -0800317 struct blkio_policy_type *pol,
318 uint64_t bytes, bool direction, bool sync)
Divyesh Shah91952912010-04-01 15:01:41 -0700319{
Tejun Heoedcb0722012-04-01 14:38:42 -0700320 int rw = (direction ? REQ_WRITE : 0) | (sync ? REQ_SYNC : 0);
Tejun Heoc1768262012-03-05 13:15:17 -0800321 struct blkg_policy_data *pd = blkg->pd[pol->plid];
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400322 struct blkio_group_stats_cpu *stats_cpu;
Vivek Goyal575969a2011-05-19 15:38:29 -0400323 unsigned long flags;
324
Vivek Goyal1cd9e032012-03-08 10:53:56 -0800325 /* If per cpu stats are not allocated yet, don't do any accounting. */
326 if (pd->stats_cpu == NULL)
327 return;
328
Vivek Goyal575969a2011-05-19 15:38:29 -0400329 /*
330 * Disabling interrupts to provide mutual exclusion between two
331 * writes on same cpu. It probably is not needed for 64bit. Not
332 * optimizing that case yet.
333 */
334 local_irq_save(flags);
Divyesh Shah91952912010-04-01 15:01:41 -0700335
Tejun Heo549d3aa2012-03-05 13:15:16 -0800336 stats_cpu = this_cpu_ptr(pd->stats_cpu);
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400337
Tejun Heoedcb0722012-04-01 14:38:42 -0700338 blkg_stat_add(&stats_cpu->sectors, bytes >> 9);
339 blkg_rwstat_add(&stats_cpu->serviced, rw, 1);
340 blkg_rwstat_add(&stats_cpu->service_bytes, rw, bytes);
341
Vivek Goyal575969a2011-05-19 15:38:29 -0400342 local_irq_restore(flags);
Divyesh Shah91952912010-04-01 15:01:41 -0700343}
Divyesh Shah84c124d2010-04-09 08:31:19 +0200344EXPORT_SYMBOL_GPL(blkiocg_update_dispatch_stats);
Divyesh Shah91952912010-04-01 15:01:41 -0700345
Divyesh Shah84c124d2010-04-09 08:31:19 +0200346void blkiocg_update_completion_stats(struct blkio_group *blkg,
Tejun Heoc1768262012-03-05 13:15:17 -0800347 struct blkio_policy_type *pol,
348 uint64_t start_time,
349 uint64_t io_start_time, bool direction,
350 bool sync)
Divyesh Shah91952912010-04-01 15:01:41 -0700351{
Tejun Heoedf1b872012-03-08 10:54:00 -0800352 struct blkio_group_stats *stats = &blkg->pd[pol->plid]->stats;
Divyesh Shah91952912010-04-01 15:01:41 -0700353 unsigned long long now = sched_clock();
Tejun Heoedcb0722012-04-01 14:38:42 -0700354 int rw = (direction ? REQ_WRITE : 0) | (sync ? REQ_SYNC : 0);
Divyesh Shah91952912010-04-01 15:01:41 -0700355
Tejun Heoedf1b872012-03-08 10:54:00 -0800356 lockdep_assert_held(blkg->q->queue_lock);
357
Divyesh Shah84c124d2010-04-09 08:31:19 +0200358 if (time_after64(now, io_start_time))
Tejun Heoedcb0722012-04-01 14:38:42 -0700359 blkg_rwstat_add(&stats->service_time, rw, now - io_start_time);
Divyesh Shah84c124d2010-04-09 08:31:19 +0200360 if (time_after64(io_start_time, start_time))
Tejun Heoedcb0722012-04-01 14:38:42 -0700361 blkg_rwstat_add(&stats->wait_time, rw,
362 io_start_time - start_time);
Divyesh Shah91952912010-04-01 15:01:41 -0700363}
Divyesh Shah84c124d2010-04-09 08:31:19 +0200364EXPORT_SYMBOL_GPL(blkiocg_update_completion_stats);
Divyesh Shah91952912010-04-01 15:01:41 -0700365
Vivek Goyal317389a2011-05-23 10:02:19 +0200366/* Merged stats are per cpu. */
Tejun Heoc1768262012-03-05 13:15:17 -0800367void blkiocg_update_io_merged_stats(struct blkio_group *blkg,
368 struct blkio_policy_type *pol,
369 bool direction, bool sync)
Divyesh Shah812d4022010-04-08 21:14:23 -0700370{
Tejun Heoedf1b872012-03-08 10:54:00 -0800371 struct blkio_group_stats *stats = &blkg->pd[pol->plid]->stats;
Tejun Heoedcb0722012-04-01 14:38:42 -0700372 int rw = (direction ? REQ_WRITE : 0) | (sync ? REQ_SYNC : 0);
Divyesh Shah812d4022010-04-08 21:14:23 -0700373
Tejun Heoedf1b872012-03-08 10:54:00 -0800374 lockdep_assert_held(blkg->q->queue_lock);
375
Tejun Heoedcb0722012-04-01 14:38:42 -0700376 blkg_rwstat_add(&stats->merged, rw, 1);
Divyesh Shah812d4022010-04-08 21:14:23 -0700377}
378EXPORT_SYMBOL_GPL(blkiocg_update_io_merged_stats);
379
Vivek Goyal1cd9e032012-03-08 10:53:56 -0800380/*
381 * Worker for allocating per cpu stat for blk groups. This is scheduled on
382 * the system_nrt_wq once there are some groups on the alloc_list waiting
383 * for allocation.
384 */
385static void blkio_stat_alloc_fn(struct work_struct *work)
386{
387 static void *pcpu_stats[BLKIO_NR_POLICIES];
388 struct delayed_work *dwork = to_delayed_work(work);
389 struct blkio_group *blkg;
390 int i;
391 bool empty = false;
392
393alloc_stats:
394 for (i = 0; i < BLKIO_NR_POLICIES; i++) {
395 if (pcpu_stats[i] != NULL)
396 continue;
397
398 pcpu_stats[i] = alloc_percpu(struct blkio_group_stats_cpu);
399
400 /* Allocation failed. Try again after some time. */
401 if (pcpu_stats[i] == NULL) {
402 queue_delayed_work(system_nrt_wq, dwork,
403 msecs_to_jiffies(10));
404 return;
405 }
406 }
407
408 spin_lock_irq(&blkio_list_lock);
409 spin_lock(&alloc_list_lock);
410
411 /* cgroup got deleted or queue exited. */
412 if (!list_empty(&alloc_list)) {
413 blkg = list_first_entry(&alloc_list, struct blkio_group,
414 alloc_node);
415 for (i = 0; i < BLKIO_NR_POLICIES; i++) {
416 struct blkg_policy_data *pd = blkg->pd[i];
417
418 if (blkio_policy[i] && pd && !pd->stats_cpu)
419 swap(pd->stats_cpu, pcpu_stats[i]);
420 }
421
422 list_del_init(&blkg->alloc_node);
423 }
424
425 empty = list_empty(&alloc_list);
426
427 spin_unlock(&alloc_list_lock);
428 spin_unlock_irq(&blkio_list_lock);
429
430 if (!empty)
431 goto alloc_stats;
432}
433
Tejun Heo03814112012-03-05 13:15:14 -0800434/**
435 * blkg_free - free a blkg
436 * @blkg: blkg to free
437 *
438 * Free @blkg which may be partially allocated.
439 */
440static void blkg_free(struct blkio_group *blkg)
441{
Tejun Heoe8989fa2012-03-05 13:15:20 -0800442 int i;
Tejun Heo549d3aa2012-03-05 13:15:16 -0800443
444 if (!blkg)
445 return;
446
Tejun Heoe8989fa2012-03-05 13:15:20 -0800447 for (i = 0; i < BLKIO_NR_POLICIES; i++) {
448 struct blkg_policy_data *pd = blkg->pd[i];
449
450 if (pd) {
451 free_percpu(pd->stats_cpu);
452 kfree(pd);
453 }
Tejun Heo03814112012-03-05 13:15:14 -0800454 }
Tejun Heoe8989fa2012-03-05 13:15:20 -0800455
Tejun Heo549d3aa2012-03-05 13:15:16 -0800456 kfree(blkg);
Tejun Heo03814112012-03-05 13:15:14 -0800457}
458
459/**
460 * blkg_alloc - allocate a blkg
461 * @blkcg: block cgroup the new blkg is associated with
462 * @q: request_queue the new blkg is associated with
Tejun Heo03814112012-03-05 13:15:14 -0800463 *
Tejun Heoe8989fa2012-03-05 13:15:20 -0800464 * Allocate a new blkg assocating @blkcg and @q.
Tejun Heo03814112012-03-05 13:15:14 -0800465 */
466static struct blkio_group *blkg_alloc(struct blkio_cgroup *blkcg,
Tejun Heoe8989fa2012-03-05 13:15:20 -0800467 struct request_queue *q)
Tejun Heo03814112012-03-05 13:15:14 -0800468{
469 struct blkio_group *blkg;
Tejun Heoe8989fa2012-03-05 13:15:20 -0800470 int i;
Tejun Heo03814112012-03-05 13:15:14 -0800471
472 /* alloc and init base part */
473 blkg = kzalloc_node(sizeof(*blkg), GFP_ATOMIC, q->node);
474 if (!blkg)
475 return NULL;
476
Tejun Heoc875f4d2012-03-05 13:15:22 -0800477 blkg->q = q;
Tejun Heoe8989fa2012-03-05 13:15:20 -0800478 INIT_LIST_HEAD(&blkg->q_node);
Vivek Goyal1cd9e032012-03-08 10:53:56 -0800479 INIT_LIST_HEAD(&blkg->alloc_node);
Tejun Heo03814112012-03-05 13:15:14 -0800480 blkg->blkcg = blkcg;
Tejun Heo1adaf3d2012-03-05 13:15:15 -0800481 blkg->refcnt = 1;
Tejun Heo03814112012-03-05 13:15:14 -0800482 cgroup_path(blkcg->css.cgroup, blkg->path, sizeof(blkg->path));
483
Tejun Heoe8989fa2012-03-05 13:15:20 -0800484 for (i = 0; i < BLKIO_NR_POLICIES; i++) {
485 struct blkio_policy_type *pol = blkio_policy[i];
486 struct blkg_policy_data *pd;
Tejun Heo03814112012-03-05 13:15:14 -0800487
Tejun Heoe8989fa2012-03-05 13:15:20 -0800488 if (!pol)
489 continue;
Tejun Heo549d3aa2012-03-05 13:15:16 -0800490
Tejun Heoe8989fa2012-03-05 13:15:20 -0800491 /* alloc per-policy data and attach it to blkg */
492 pd = kzalloc_node(sizeof(*pd) + pol->pdata_size, GFP_ATOMIC,
493 q->node);
494 if (!pd) {
495 blkg_free(blkg);
496 return NULL;
497 }
Tejun Heo549d3aa2012-03-05 13:15:16 -0800498
Tejun Heoe8989fa2012-03-05 13:15:20 -0800499 blkg->pd[i] = pd;
500 pd->blkg = blkg;
Tejun Heo03814112012-03-05 13:15:14 -0800501 }
502
Tejun Heo549d3aa2012-03-05 13:15:16 -0800503 /* invoke per-policy init */
Tejun Heoe8989fa2012-03-05 13:15:20 -0800504 for (i = 0; i < BLKIO_NR_POLICIES; i++) {
505 struct blkio_policy_type *pol = blkio_policy[i];
506
507 if (pol)
508 pol->ops.blkio_init_group_fn(blkg);
509 }
510
Tejun Heo03814112012-03-05 13:15:14 -0800511 return blkg;
512}
513
Tejun Heocd1604f2012-03-05 13:15:06 -0800514struct blkio_group *blkg_lookup_create(struct blkio_cgroup *blkcg,
515 struct request_queue *q,
Tejun Heocd1604f2012-03-05 13:15:06 -0800516 bool for_root)
517 __releases(q->queue_lock) __acquires(q->queue_lock)
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400518{
Vivek Goyal1cd9e032012-03-08 10:53:56 -0800519 struct blkio_group *blkg;
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400520
Tejun Heocd1604f2012-03-05 13:15:06 -0800521 WARN_ON_ONCE(!rcu_read_lock_held());
522 lockdep_assert_held(q->queue_lock);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500523
Tejun Heocd1604f2012-03-05 13:15:06 -0800524 /*
525 * This could be the first entry point of blkcg implementation and
526 * we shouldn't allow anything to go through for a bypassing queue.
527 * The following can be removed if blkg lookup is guaranteed to
528 * fail on a bypassing queue.
529 */
530 if (unlikely(blk_queue_bypass(q)) && !for_root)
531 return ERR_PTR(blk_queue_dead(q) ? -EINVAL : -EBUSY);
532
Tejun Heoe8989fa2012-03-05 13:15:20 -0800533 blkg = blkg_lookup(blkcg, q);
Tejun Heocd1604f2012-03-05 13:15:06 -0800534 if (blkg)
535 return blkg;
536
Tejun Heo7ee9c562012-03-05 13:15:11 -0800537 /* blkg holds a reference to blkcg */
Tejun Heocd1604f2012-03-05 13:15:06 -0800538 if (!css_tryget(&blkcg->css))
539 return ERR_PTR(-EINVAL);
540
541 /*
542 * Allocate and initialize.
Tejun Heocd1604f2012-03-05 13:15:06 -0800543 */
Vivek Goyal1cd9e032012-03-08 10:53:56 -0800544 blkg = blkg_alloc(blkcg, q);
Tejun Heocd1604f2012-03-05 13:15:06 -0800545
546 /* did alloc fail? */
Vivek Goyal1cd9e032012-03-08 10:53:56 -0800547 if (unlikely(!blkg)) {
Tejun Heocd1604f2012-03-05 13:15:06 -0800548 blkg = ERR_PTR(-ENOMEM);
549 goto out;
550 }
551
552 /* insert */
553 spin_lock(&blkcg->lock);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500554 hlist_add_head_rcu(&blkg->blkcg_node, &blkcg->blkg_list);
Tejun Heoe8989fa2012-03-05 13:15:20 -0800555 list_add(&blkg->q_node, &q->blkg_list);
Tejun Heocd1604f2012-03-05 13:15:06 -0800556 spin_unlock(&blkcg->lock);
Vivek Goyal1cd9e032012-03-08 10:53:56 -0800557
558 spin_lock(&alloc_list_lock);
559 list_add(&blkg->alloc_node, &alloc_list);
560 /* Queue per cpu stat allocation from worker thread. */
561 queue_delayed_work(system_nrt_wq, &blkio_stat_alloc_work, 0);
562 spin_unlock(&alloc_list_lock);
Tejun Heocd1604f2012-03-05 13:15:06 -0800563out:
Tejun Heocd1604f2012-03-05 13:15:06 -0800564 return blkg;
Vivek Goyal31e4c282009-12-03 12:59:42 -0500565}
Tejun Heocd1604f2012-03-05 13:15:06 -0800566EXPORT_SYMBOL_GPL(blkg_lookup_create);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500567
Vivek Goyal31e4c282009-12-03 12:59:42 -0500568/* called under rcu_read_lock(). */
Tejun Heocd1604f2012-03-05 13:15:06 -0800569struct blkio_group *blkg_lookup(struct blkio_cgroup *blkcg,
Tejun Heoe8989fa2012-03-05 13:15:20 -0800570 struct request_queue *q)
Vivek Goyal31e4c282009-12-03 12:59:42 -0500571{
572 struct blkio_group *blkg;
573 struct hlist_node *n;
Vivek Goyal31e4c282009-12-03 12:59:42 -0500574
Tejun Heoca32aef2012-03-05 13:15:03 -0800575 hlist_for_each_entry_rcu(blkg, n, &blkcg->blkg_list, blkcg_node)
Tejun Heoe8989fa2012-03-05 13:15:20 -0800576 if (blkg->q == q)
Vivek Goyal31e4c282009-12-03 12:59:42 -0500577 return blkg;
Vivek Goyal31e4c282009-12-03 12:59:42 -0500578 return NULL;
579}
Tejun Heocd1604f2012-03-05 13:15:06 -0800580EXPORT_SYMBOL_GPL(blkg_lookup);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500581
Tejun Heoe8989fa2012-03-05 13:15:20 -0800582static void blkg_destroy(struct blkio_group *blkg)
Tejun Heo72e06c22012-03-05 13:15:00 -0800583{
Tejun Heo03aa2642012-03-05 13:15:19 -0800584 struct request_queue *q = blkg->q;
Tejun Heo9f13ef62012-03-05 13:15:21 -0800585 struct blkio_cgroup *blkcg = blkg->blkcg;
Tejun Heo03aa2642012-03-05 13:15:19 -0800586
587 lockdep_assert_held(q->queue_lock);
Tejun Heo9f13ef62012-03-05 13:15:21 -0800588 lockdep_assert_held(&blkcg->lock);
Tejun Heo03aa2642012-03-05 13:15:19 -0800589
590 /* Something wrong if we are trying to remove same group twice */
Tejun Heoe8989fa2012-03-05 13:15:20 -0800591 WARN_ON_ONCE(list_empty(&blkg->q_node));
Tejun Heo9f13ef62012-03-05 13:15:21 -0800592 WARN_ON_ONCE(hlist_unhashed(&blkg->blkcg_node));
Tejun Heoe8989fa2012-03-05 13:15:20 -0800593 list_del_init(&blkg->q_node);
Tejun Heo9f13ef62012-03-05 13:15:21 -0800594 hlist_del_init_rcu(&blkg->blkcg_node);
Tejun Heo03aa2642012-03-05 13:15:19 -0800595
Vivek Goyal1cd9e032012-03-08 10:53:56 -0800596 spin_lock(&alloc_list_lock);
597 list_del_init(&blkg->alloc_node);
598 spin_unlock(&alloc_list_lock);
599
Tejun Heo03aa2642012-03-05 13:15:19 -0800600 /*
601 * Put the reference taken at the time of creation so that when all
602 * queues are gone, group can be destroyed.
603 */
604 blkg_put(blkg);
605}
606
Tejun Heoe8989fa2012-03-05 13:15:20 -0800607/*
608 * XXX: This updates blkg policy data in-place for root blkg, which is
609 * necessary across elevator switch and policy registration as root blkgs
610 * aren't shot down. This broken and racy implementation is temporary.
611 * Eventually, blkg shoot down will be replaced by proper in-place update.
612 */
613void update_root_blkg_pd(struct request_queue *q, enum blkio_policy_id plid)
614{
615 struct blkio_policy_type *pol = blkio_policy[plid];
616 struct blkio_group *blkg = blkg_lookup(&blkio_root_cgroup, q);
617 struct blkg_policy_data *pd;
618
619 if (!blkg)
620 return;
621
622 kfree(blkg->pd[plid]);
623 blkg->pd[plid] = NULL;
624
625 if (!pol)
626 return;
627
628 pd = kzalloc(sizeof(*pd) + pol->pdata_size, GFP_KERNEL);
629 WARN_ON_ONCE(!pd);
630
631 pd->stats_cpu = alloc_percpu(struct blkio_group_stats_cpu);
632 WARN_ON_ONCE(!pd->stats_cpu);
633
634 blkg->pd[plid] = pd;
635 pd->blkg = blkg;
636 pol->ops.blkio_init_group_fn(blkg);
637}
638EXPORT_SYMBOL_GPL(update_root_blkg_pd);
639
Tejun Heo9f13ef62012-03-05 13:15:21 -0800640/**
641 * blkg_destroy_all - destroy all blkgs associated with a request_queue
642 * @q: request_queue of interest
643 * @destroy_root: whether to destroy root blkg or not
644 *
645 * Destroy blkgs associated with @q. If @destroy_root is %true, all are
646 * destroyed; otherwise, root blkg is left alone.
647 */
Tejun Heoe8989fa2012-03-05 13:15:20 -0800648void blkg_destroy_all(struct request_queue *q, bool destroy_root)
Tejun Heo03aa2642012-03-05 13:15:19 -0800649{
650 struct blkio_group *blkg, *n;
Tejun Heo72e06c22012-03-05 13:15:00 -0800651
Tejun Heo9f13ef62012-03-05 13:15:21 -0800652 spin_lock_irq(q->queue_lock);
Tejun Heo72e06c22012-03-05 13:15:00 -0800653
Tejun Heo9f13ef62012-03-05 13:15:21 -0800654 list_for_each_entry_safe(blkg, n, &q->blkg_list, q_node) {
655 struct blkio_cgroup *blkcg = blkg->blkcg;
Tejun Heo72e06c22012-03-05 13:15:00 -0800656
Tejun Heo9f13ef62012-03-05 13:15:21 -0800657 /* skip root? */
658 if (!destroy_root && blkg->blkcg == &blkio_root_cgroup)
659 continue;
Tejun Heo03aa2642012-03-05 13:15:19 -0800660
Tejun Heo9f13ef62012-03-05 13:15:21 -0800661 spin_lock(&blkcg->lock);
662 blkg_destroy(blkg);
663 spin_unlock(&blkcg->lock);
Tejun Heo72e06c22012-03-05 13:15:00 -0800664 }
Tejun Heo9f13ef62012-03-05 13:15:21 -0800665
666 spin_unlock_irq(q->queue_lock);
Tejun Heo72e06c22012-03-05 13:15:00 -0800667}
Tejun Heo03aa2642012-03-05 13:15:19 -0800668EXPORT_SYMBOL_GPL(blkg_destroy_all);
Tejun Heo72e06c22012-03-05 13:15:00 -0800669
Tejun Heo1adaf3d2012-03-05 13:15:15 -0800670static void blkg_rcu_free(struct rcu_head *rcu_head)
671{
672 blkg_free(container_of(rcu_head, struct blkio_group, rcu_head));
673}
674
675void __blkg_release(struct blkio_group *blkg)
676{
677 /* release the extra blkcg reference this blkg has been holding */
678 css_put(&blkg->blkcg->css);
679
680 /*
681 * A group is freed in rcu manner. But having an rcu lock does not
682 * mean that one can access all the fields of blkg and assume these
683 * are valid. For example, don't try to follow throtl_data and
684 * request queue links.
685 *
686 * Having a reference to blkg under an rcu allows acess to only
687 * values local to groups like group stats and group rate limits
688 */
689 call_rcu(&blkg->rcu_head, blkg_rcu_free);
690}
691EXPORT_SYMBOL_GPL(__blkg_release);
692
Tejun Heoc1768262012-03-05 13:15:17 -0800693static void blkio_reset_stats_cpu(struct blkio_group *blkg, int plid)
Vivek Goyalf0bdc8c2011-05-19 15:38:30 -0400694{
Tejun Heoc1768262012-03-05 13:15:17 -0800695 struct blkg_policy_data *pd = blkg->pd[plid];
Tejun Heo997a0262012-03-08 10:53:58 -0800696 int cpu;
Vivek Goyal1cd9e032012-03-08 10:53:56 -0800697
698 if (pd->stats_cpu == NULL)
699 return;
Tejun Heo997a0262012-03-08 10:53:58 -0800700
701 for_each_possible_cpu(cpu) {
702 struct blkio_group_stats_cpu *sc =
703 per_cpu_ptr(pd->stats_cpu, cpu);
704
Tejun Heoedcb0722012-04-01 14:38:42 -0700705 blkg_rwstat_reset(&sc->service_bytes);
706 blkg_rwstat_reset(&sc->serviced);
707 blkg_stat_reset(&sc->sectors);
Vivek Goyalf0bdc8c2011-05-19 15:38:30 -0400708 }
709}
710
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700711static int
Divyesh Shah84c124d2010-04-09 08:31:19 +0200712blkiocg_reset_stats(struct cgroup *cgroup, struct cftype *cftype, u64 val)
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700713{
Tejun Heo997a0262012-03-08 10:53:58 -0800714 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgroup);
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700715 struct blkio_group *blkg;
716 struct hlist_node *n;
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700717
Tejun Heoe8989fa2012-03-05 13:15:20 -0800718 spin_lock(&blkio_list_lock);
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700719 spin_lock_irq(&blkcg->lock);
Tejun Heo997a0262012-03-08 10:53:58 -0800720
721 /*
722 * Note that stat reset is racy - it doesn't synchronize against
723 * stat updates. This is a debug feature which shouldn't exist
724 * anyway. If you get hit by a race, retry.
725 */
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700726 hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) {
Tejun Heoe8989fa2012-03-05 13:15:20 -0800727 struct blkio_policy_type *pol;
Tejun Heo549d3aa2012-03-05 13:15:16 -0800728
Tejun Heoe8989fa2012-03-05 13:15:20 -0800729 list_for_each_entry(pol, &blkio_list, list) {
730 struct blkg_policy_data *pd = blkg->pd[pol->plid];
Tejun Heo997a0262012-03-08 10:53:58 -0800731 struct blkio_group_stats *stats = &pd->stats;
Vivek Goyalf0bdc8c2011-05-19 15:38:30 -0400732
Tejun Heo997a0262012-03-08 10:53:58 -0800733 /* queued stats shouldn't be cleared */
Tejun Heoedcb0722012-04-01 14:38:42 -0700734 blkg_rwstat_reset(&stats->merged);
735 blkg_rwstat_reset(&stats->service_time);
736 blkg_rwstat_reset(&stats->wait_time);
737 blkg_stat_reset(&stats->time);
Tejun Heoe8989fa2012-03-05 13:15:20 -0800738#ifdef CONFIG_DEBUG_BLK_CGROUP
Tejun Heoedcb0722012-04-01 14:38:42 -0700739 blkg_stat_reset(&stats->unaccounted_time);
740 blkg_stat_reset(&stats->avg_queue_size_sum);
741 blkg_stat_reset(&stats->avg_queue_size_samples);
742 blkg_stat_reset(&stats->dequeue);
743 blkg_stat_reset(&stats->group_wait_time);
744 blkg_stat_reset(&stats->idle_time);
745 blkg_stat_reset(&stats->empty_time);
Tejun Heoe8989fa2012-03-05 13:15:20 -0800746#endif
Tejun Heoe8989fa2012-03-05 13:15:20 -0800747 blkio_reset_stats_cpu(blkg, pol->plid);
748 }
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700749 }
Vivek Goyalf0bdc8c2011-05-19 15:38:30 -0400750
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700751 spin_unlock_irq(&blkcg->lock);
Tejun Heoe8989fa2012-03-05 13:15:20 -0800752 spin_unlock(&blkio_list_lock);
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700753 return 0;
754}
755
Tejun Heod3d32e62012-04-01 14:38:42 -0700756static const char *blkg_dev_name(struct blkio_group *blkg)
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700757{
Tejun Heod3d32e62012-04-01 14:38:42 -0700758 /* some drivers (floppy) instantiate a queue w/o disk registered */
759 if (blkg->q->backing_dev_info.dev)
760 return dev_name(blkg->q->backing_dev_info.dev);
761 return NULL;
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700762}
763
Tejun Heod3d32e62012-04-01 14:38:42 -0700764/**
765 * blkcg_print_blkgs - helper for printing per-blkg data
766 * @sf: seq_file to print to
767 * @blkcg: blkcg of interest
768 * @prfill: fill function to print out a blkg
769 * @pol: policy in question
770 * @data: data to be passed to @prfill
771 * @show_total: to print out sum of prfill return values or not
772 *
773 * This function invokes @prfill on each blkg of @blkcg if pd for the
774 * policy specified by @pol exists. @prfill is invoked with @sf, the
775 * policy data and @data. If @show_total is %true, the sum of the return
776 * values from @prfill is printed with "Total" label at the end.
777 *
778 * This is to be used to construct print functions for
779 * cftype->read_seq_string method.
780 */
781static void blkcg_print_blkgs(struct seq_file *sf, struct blkio_cgroup *blkcg,
782 u64 (*prfill)(struct seq_file *,
783 struct blkg_policy_data *, int),
784 int pol, int data, bool show_total)
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400785{
Tejun Heod3d32e62012-04-01 14:38:42 -0700786 struct blkio_group *blkg;
787 struct hlist_node *n;
788 u64 total = 0;
789
790 spin_lock_irq(&blkcg->lock);
791 hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node)
792 if (blkg->pd[pol])
793 total += prfill(sf, blkg->pd[pol], data);
794 spin_unlock_irq(&blkcg->lock);
795
796 if (show_total)
797 seq_printf(sf, "Total %llu\n", (unsigned long long)total);
798}
799
800/**
801 * __blkg_prfill_u64 - prfill helper for a single u64 value
802 * @sf: seq_file to print to
803 * @pd: policy data of interest
804 * @v: value to print
805 *
806 * Print @v to @sf for the device assocaited with @pd.
807 */
808static u64 __blkg_prfill_u64(struct seq_file *sf, struct blkg_policy_data *pd,
809 u64 v)
810{
811 const char *dname = blkg_dev_name(pd->blkg);
812
813 if (!dname)
814 return 0;
815
816 seq_printf(sf, "%s %llu\n", dname, (unsigned long long)v);
817 return v;
818}
819
820/**
821 * __blkg_prfill_rwstat - prfill helper for a blkg_rwstat
822 * @sf: seq_file to print to
823 * @pd: policy data of interest
824 * @rwstat: rwstat to print
825 *
826 * Print @rwstat to @sf for the device assocaited with @pd.
827 */
828static u64 __blkg_prfill_rwstat(struct seq_file *sf,
829 struct blkg_policy_data *pd,
830 const struct blkg_rwstat *rwstat)
831{
832 static const char *rwstr[] = {
833 [BLKG_RWSTAT_READ] = "Read",
834 [BLKG_RWSTAT_WRITE] = "Write",
835 [BLKG_RWSTAT_SYNC] = "Sync",
836 [BLKG_RWSTAT_ASYNC] = "Async",
837 };
838 const char *dname = blkg_dev_name(pd->blkg);
839 u64 v;
840 int i;
841
842 if (!dname)
843 return 0;
844
845 for (i = 0; i < BLKG_RWSTAT_NR; i++)
846 seq_printf(sf, "%s %s %llu\n", dname, rwstr[i],
847 (unsigned long long)rwstat->cnt[i]);
848
849 v = rwstat->cnt[BLKG_RWSTAT_READ] + rwstat->cnt[BLKG_RWSTAT_WRITE];
850 seq_printf(sf, "%s Total %llu\n", dname, (unsigned long long)v);
851 return v;
852}
853
854static u64 blkg_prfill_stat(struct seq_file *sf, struct blkg_policy_data *pd,
855 int off)
856{
857 return __blkg_prfill_u64(sf, pd,
858 blkg_stat_read((void *)&pd->stats + off));
859}
860
861static u64 blkg_prfill_rwstat(struct seq_file *sf, struct blkg_policy_data *pd,
862 int off)
863{
864 struct blkg_rwstat rwstat = blkg_rwstat_read((void *)&pd->stats + off);
865
866 return __blkg_prfill_rwstat(sf, pd, &rwstat);
867}
868
869/* print blkg_stat specified by BLKCG_STAT_PRIV() */
870static int blkcg_print_stat(struct cgroup *cgrp, struct cftype *cft,
871 struct seq_file *sf)
872{
873 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgrp);
874
875 blkcg_print_blkgs(sf, blkcg, blkg_prfill_stat,
876 BLKCG_STAT_POL(cft->private),
877 BLKCG_STAT_OFF(cft->private), false);
878 return 0;
879}
880
881/* print blkg_rwstat specified by BLKCG_STAT_PRIV() */
882static int blkcg_print_rwstat(struct cgroup *cgrp, struct cftype *cft,
883 struct seq_file *sf)
884{
885 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgrp);
886
887 blkcg_print_blkgs(sf, blkcg, blkg_prfill_rwstat,
888 BLKCG_STAT_POL(cft->private),
889 BLKCG_STAT_OFF(cft->private), true);
890 return 0;
891}
892
893static u64 blkg_prfill_cpu_stat(struct seq_file *sf,
894 struct blkg_policy_data *pd, int off)
895{
896 u64 v = 0;
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400897 int cpu;
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400898
Tejun Heod3d32e62012-04-01 14:38:42 -0700899 for_each_possible_cpu(cpu) {
900 struct blkio_group_stats_cpu *sc =
901 per_cpu_ptr(pd->stats_cpu, cpu);
902
903 v += blkg_stat_read((void *)sc + off);
904 }
905
906 return __blkg_prfill_u64(sf, pd, v);
907}
908
909static u64 blkg_prfill_cpu_rwstat(struct seq_file *sf,
910 struct blkg_policy_data *pd, int off)
911{
912 struct blkg_rwstat rwstat = { }, tmp;
913 int i, cpu;
Vivek Goyal1cd9e032012-03-08 10:53:56 -0800914
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400915 for_each_possible_cpu(cpu) {
Tejun Heod3d32e62012-04-01 14:38:42 -0700916 struct blkio_group_stats_cpu *sc =
Tejun Heoedcb0722012-04-01 14:38:42 -0700917 per_cpu_ptr(pd->stats_cpu, cpu);
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400918
Tejun Heod3d32e62012-04-01 14:38:42 -0700919 tmp = blkg_rwstat_read((void *)sc + off);
920 for (i = 0; i < BLKG_RWSTAT_NR; i++)
921 rwstat.cnt[i] += tmp.cnt[i];
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400922 }
923
Tejun Heod3d32e62012-04-01 14:38:42 -0700924 return __blkg_prfill_rwstat(sf, pd, &rwstat);
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400925}
926
Tejun Heod3d32e62012-04-01 14:38:42 -0700927/* print per-cpu blkg_stat specified by BLKCG_STAT_PRIV() */
928static int blkcg_print_cpu_stat(struct cgroup *cgrp, struct cftype *cft,
929 struct seq_file *sf)
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400930{
Tejun Heod3d32e62012-04-01 14:38:42 -0700931 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgrp);
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400932
Tejun Heod3d32e62012-04-01 14:38:42 -0700933 blkcg_print_blkgs(sf, blkcg, blkg_prfill_cpu_stat,
934 BLKCG_STAT_POL(cft->private),
935 BLKCG_STAT_OFF(cft->private), false);
936 return 0;
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400937}
938
Tejun Heod3d32e62012-04-01 14:38:42 -0700939/* print per-cpu blkg_rwstat specified by BLKCG_STAT_PRIV() */
940static int blkcg_print_cpu_rwstat(struct cgroup *cgrp, struct cftype *cft,
941 struct seq_file *sf)
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700942{
Tejun Heod3d32e62012-04-01 14:38:42 -0700943 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgrp);
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700944
Tejun Heod3d32e62012-04-01 14:38:42 -0700945 blkcg_print_blkgs(sf, blkcg, blkg_prfill_cpu_rwstat,
946 BLKCG_STAT_POL(cft->private),
947 BLKCG_STAT_OFF(cft->private), true);
948 return 0;
949}
950
Justin TerAvest9026e522011-03-22 21:26:54 +0100951#ifdef CONFIG_DEBUG_BLK_CGROUP
Tejun Heod3d32e62012-04-01 14:38:42 -0700952static u64 blkg_prfill_avg_queue_size(struct seq_file *sf,
953 struct blkg_policy_data *pd, int off)
954{
955 u64 samples = blkg_stat_read(&pd->stats.avg_queue_size_samples);
956 u64 v = 0;
Divyesh Shah84c124d2010-04-09 08:31:19 +0200957
Tejun Heod3d32e62012-04-01 14:38:42 -0700958 if (samples) {
959 v = blkg_stat_read(&pd->stats.avg_queue_size_sum);
960 do_div(v, samples);
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700961 }
Tejun Heod3d32e62012-04-01 14:38:42 -0700962 __blkg_prfill_u64(sf, pd, v);
963 return 0;
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700964}
965
Tejun Heod3d32e62012-04-01 14:38:42 -0700966/* print avg_queue_size */
967static int blkcg_print_avg_queue_size(struct cgroup *cgrp, struct cftype *cft,
968 struct seq_file *sf)
969{
970 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgrp);
971
972 blkcg_print_blkgs(sf, blkcg, blkg_prfill_avg_queue_size,
973 BLKIO_POLICY_PROP, 0, false);
974 return 0;
975}
976#endif /* CONFIG_DEBUG_BLK_CGROUP */
977
Tejun Heo4bfd4822012-03-05 13:15:08 -0800978static int blkio_policy_parse_and_set(char *buf, enum blkio_policy_id plid,
979 int fileid, struct blkio_cgroup *blkcg)
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800980{
Tejun Heoece84242011-10-19 14:31:15 +0200981 struct gendisk *disk = NULL;
Tejun Heoe56da7e2012-03-05 13:15:07 -0800982 struct blkio_group *blkg = NULL;
Tejun Heo549d3aa2012-03-05 13:15:16 -0800983 struct blkg_policy_data *pd;
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800984 char *s[4], *p, *major_s = NULL, *minor_s = NULL;
Wanlong Gaod11bb442011-09-21 10:22:10 +0200985 unsigned long major, minor;
Tejun Heoece84242011-10-19 14:31:15 +0200986 int i = 0, ret = -EINVAL;
987 int part;
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800988 dev_t dev;
Wanlong Gaod11bb442011-09-21 10:22:10 +0200989 u64 temp;
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800990
991 memset(s, 0, sizeof(s));
992
993 while ((p = strsep(&buf, " ")) != NULL) {
994 if (!*p)
995 continue;
996
997 s[i++] = p;
998
999 /* Prevent from inputing too many things */
1000 if (i == 3)
1001 break;
1002 }
1003
1004 if (i != 2)
Tejun Heoece84242011-10-19 14:31:15 +02001005 goto out;
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001006
1007 p = strsep(&s[0], ":");
1008 if (p != NULL)
1009 major_s = p;
1010 else
Tejun Heoece84242011-10-19 14:31:15 +02001011 goto out;
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001012
1013 minor_s = s[0];
1014 if (!minor_s)
Tejun Heoece84242011-10-19 14:31:15 +02001015 goto out;
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001016
Tejun Heoece84242011-10-19 14:31:15 +02001017 if (strict_strtoul(major_s, 10, &major))
1018 goto out;
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001019
Tejun Heoece84242011-10-19 14:31:15 +02001020 if (strict_strtoul(minor_s, 10, &minor))
1021 goto out;
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001022
1023 dev = MKDEV(major, minor);
1024
Tejun Heoece84242011-10-19 14:31:15 +02001025 if (strict_strtoull(s[1], 10, &temp))
1026 goto out;
Wanlong Gaod11bb442011-09-21 10:22:10 +02001027
Tejun Heoe56da7e2012-03-05 13:15:07 -08001028 disk = get_gendisk(dev, &part);
Tejun Heo4bfd4822012-03-05 13:15:08 -08001029 if (!disk || part)
Tejun Heoe56da7e2012-03-05 13:15:07 -08001030 goto out;
Tejun Heoe56da7e2012-03-05 13:15:07 -08001031
1032 rcu_read_lock();
1033
Tejun Heo4bfd4822012-03-05 13:15:08 -08001034 spin_lock_irq(disk->queue->queue_lock);
Tejun Heoaaec55a2012-04-01 14:38:42 -07001035 blkg = blkg_lookup_create(blkcg, disk->queue, false);
Tejun Heo4bfd4822012-03-05 13:15:08 -08001036 spin_unlock_irq(disk->queue->queue_lock);
Tejun Heoe56da7e2012-03-05 13:15:07 -08001037
Tejun Heo4bfd4822012-03-05 13:15:08 -08001038 if (IS_ERR(blkg)) {
1039 ret = PTR_ERR(blkg);
1040 goto out_unlock;
Wanlong Gaod11bb442011-09-21 10:22:10 +02001041 }
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001042
Tejun Heo549d3aa2012-03-05 13:15:16 -08001043 pd = blkg->pd[plid];
1044
Vivek Goyal062a6442010-09-15 17:06:33 -04001045 switch (plid) {
1046 case BLKIO_POLICY_PROP:
Wanlong Gaod11bb442011-09-21 10:22:10 +02001047 if ((temp < BLKIO_WEIGHT_MIN && temp > 0) ||
1048 temp > BLKIO_WEIGHT_MAX)
Tejun Heoe56da7e2012-03-05 13:15:07 -08001049 goto out_unlock;
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001050
Tejun Heo549d3aa2012-03-05 13:15:16 -08001051 pd->conf.weight = temp;
Tejun Heoc1768262012-03-05 13:15:17 -08001052 blkio_update_group_weight(blkg, plid, temp ?: blkcg->weight);
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001053 break;
1054 case BLKIO_POLICY_THROTL:
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001055 switch(fileid) {
1056 case BLKIO_THROTL_read_bps_device:
Tejun Heo549d3aa2012-03-05 13:15:16 -08001057 pd->conf.bps[READ] = temp;
Tejun Heoc1768262012-03-05 13:15:17 -08001058 blkio_update_group_bps(blkg, plid, temp ?: -1, fileid);
Tejun Heoe56da7e2012-03-05 13:15:07 -08001059 break;
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001060 case BLKIO_THROTL_write_bps_device:
Tejun Heo549d3aa2012-03-05 13:15:16 -08001061 pd->conf.bps[WRITE] = temp;
Tejun Heoc1768262012-03-05 13:15:17 -08001062 blkio_update_group_bps(blkg, plid, temp ?: -1, fileid);
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001063 break;
1064 case BLKIO_THROTL_read_iops_device:
Tejun Heoe56da7e2012-03-05 13:15:07 -08001065 if (temp > THROTL_IOPS_MAX)
1066 goto out_unlock;
Tejun Heo549d3aa2012-03-05 13:15:16 -08001067 pd->conf.iops[READ] = temp;
Tejun Heoc1768262012-03-05 13:15:17 -08001068 blkio_update_group_iops(blkg, plid, temp ?: -1, fileid);
Tejun Heoe56da7e2012-03-05 13:15:07 -08001069 break;
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001070 case BLKIO_THROTL_write_iops_device:
Wanlong Gaod11bb442011-09-21 10:22:10 +02001071 if (temp > THROTL_IOPS_MAX)
Tejun Heoe56da7e2012-03-05 13:15:07 -08001072 goto out_unlock;
Tejun Heo549d3aa2012-03-05 13:15:16 -08001073 pd->conf.iops[WRITE] = temp;
Tejun Heoc1768262012-03-05 13:15:17 -08001074 blkio_update_group_iops(blkg, plid, temp ?: -1, fileid);
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001075 break;
1076 }
Vivek Goyal062a6442010-09-15 17:06:33 -04001077 break;
1078 default:
1079 BUG();
1080 }
Tejun Heoece84242011-10-19 14:31:15 +02001081 ret = 0;
Tejun Heoe56da7e2012-03-05 13:15:07 -08001082out_unlock:
1083 rcu_read_unlock();
Tejun Heoece84242011-10-19 14:31:15 +02001084out:
1085 put_disk(disk);
Tejun Heoe56da7e2012-03-05 13:15:07 -08001086
1087 /*
1088 * If queue was bypassing, we should retry. Do so after a short
1089 * msleep(). It isn't strictly necessary but queue can be
1090 * bypassing for some time and it's always nice to avoid busy
1091 * looping.
1092 */
1093 if (ret == -EBUSY) {
1094 msleep(10);
1095 return restart_syscall();
1096 }
Tejun Heoece84242011-10-19 14:31:15 +02001097 return ret;
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001098}
1099
Vivek Goyal062a6442010-09-15 17:06:33 -04001100static int blkiocg_file_write(struct cgroup *cgrp, struct cftype *cft,
1101 const char *buffer)
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001102{
1103 int ret = 0;
1104 char *buf;
Tejun Heoe56da7e2012-03-05 13:15:07 -08001105 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgrp);
Vivek Goyal062a6442010-09-15 17:06:33 -04001106 enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
1107 int fileid = BLKIOFILE_ATTR(cft->private);
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001108
1109 buf = kstrdup(buffer, GFP_KERNEL);
1110 if (!buf)
1111 return -ENOMEM;
1112
Tejun Heo4bfd4822012-03-05 13:15:08 -08001113 ret = blkio_policy_parse_and_set(buf, plid, fileid, blkcg);
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001114 kfree(buf);
1115 return ret;
1116}
1117
Tejun Heo4bfd4822012-03-05 13:15:08 -08001118static void blkio_print_group_conf(struct cftype *cft, struct blkio_group *blkg,
1119 struct seq_file *m)
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001120{
Tejun Heoc1768262012-03-05 13:15:17 -08001121 int plid = BLKIOFILE_POLICY(cft->private);
Tejun Heo4bfd4822012-03-05 13:15:08 -08001122 int fileid = BLKIOFILE_ATTR(cft->private);
Tejun Heoc1768262012-03-05 13:15:17 -08001123 struct blkg_policy_data *pd = blkg->pd[plid];
1124 const char *dname = blkg_dev_name(blkg);
Tejun Heo4bfd4822012-03-05 13:15:08 -08001125 int rw = WRITE;
1126
Vivek Goyal92616b52012-03-05 13:15:10 -08001127 if (!dname)
1128 return;
1129
Tejun Heoc1768262012-03-05 13:15:17 -08001130 switch (plid) {
Vivek Goyal062a6442010-09-15 17:06:33 -04001131 case BLKIO_POLICY_PROP:
Tejun Heo549d3aa2012-03-05 13:15:16 -08001132 if (pd->conf.weight)
Tejun Heo7a4dd282012-03-05 13:15:09 -08001133 seq_printf(m, "%s\t%u\n",
Tejun Heo549d3aa2012-03-05 13:15:16 -08001134 dname, pd->conf.weight);
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001135 break;
1136 case BLKIO_POLICY_THROTL:
Tejun Heo4bfd4822012-03-05 13:15:08 -08001137 switch (fileid) {
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001138 case BLKIO_THROTL_read_bps_device:
Tejun Heo4bfd4822012-03-05 13:15:08 -08001139 rw = READ;
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001140 case BLKIO_THROTL_write_bps_device:
Tejun Heo549d3aa2012-03-05 13:15:16 -08001141 if (pd->conf.bps[rw])
Tejun Heo7a4dd282012-03-05 13:15:09 -08001142 seq_printf(m, "%s\t%llu\n",
Tejun Heo549d3aa2012-03-05 13:15:16 -08001143 dname, pd->conf.bps[rw]);
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001144 break;
1145 case BLKIO_THROTL_read_iops_device:
Tejun Heo4bfd4822012-03-05 13:15:08 -08001146 rw = READ;
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001147 case BLKIO_THROTL_write_iops_device:
Tejun Heo549d3aa2012-03-05 13:15:16 -08001148 if (pd->conf.iops[rw])
Tejun Heo7a4dd282012-03-05 13:15:09 -08001149 seq_printf(m, "%s\t%u\n",
Tejun Heo549d3aa2012-03-05 13:15:16 -08001150 dname, pd->conf.iops[rw]);
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001151 break;
1152 }
Vivek Goyal062a6442010-09-15 17:06:33 -04001153 break;
1154 default:
1155 BUG();
1156 }
1157}
1158
1159/* cgroup files which read their data from policy nodes end up here */
Tejun Heo4bfd4822012-03-05 13:15:08 -08001160static void blkio_read_conf(struct cftype *cft, struct blkio_cgroup *blkcg,
1161 struct seq_file *m)
Vivek Goyal062a6442010-09-15 17:06:33 -04001162{
Tejun Heo4bfd4822012-03-05 13:15:08 -08001163 struct blkio_group *blkg;
1164 struct hlist_node *n;
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001165
Tejun Heo4bfd4822012-03-05 13:15:08 -08001166 spin_lock_irq(&blkcg->lock);
1167 hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node)
Tejun Heoe8989fa2012-03-05 13:15:20 -08001168 blkio_print_group_conf(cft, blkg, m);
Tejun Heo4bfd4822012-03-05 13:15:08 -08001169 spin_unlock_irq(&blkcg->lock);
Vivek Goyal062a6442010-09-15 17:06:33 -04001170}
1171
1172static int blkiocg_file_read(struct cgroup *cgrp, struct cftype *cft,
1173 struct seq_file *m)
1174{
1175 struct blkio_cgroup *blkcg;
1176 enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
1177 int name = BLKIOFILE_ATTR(cft->private);
1178
1179 blkcg = cgroup_to_blkio_cgroup(cgrp);
1180
1181 switch(plid) {
1182 case BLKIO_POLICY_PROP:
1183 switch(name) {
1184 case BLKIO_PROP_weight_device:
Tejun Heo4bfd4822012-03-05 13:15:08 -08001185 blkio_read_conf(cft, blkcg, m);
Vivek Goyal062a6442010-09-15 17:06:33 -04001186 return 0;
1187 default:
1188 BUG();
1189 }
1190 break;
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001191 case BLKIO_POLICY_THROTL:
1192 switch(name){
1193 case BLKIO_THROTL_read_bps_device:
1194 case BLKIO_THROTL_write_bps_device:
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001195 case BLKIO_THROTL_read_iops_device:
1196 case BLKIO_THROTL_write_iops_device:
Tejun Heo4bfd4822012-03-05 13:15:08 -08001197 blkio_read_conf(cft, blkcg, m);
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001198 return 0;
1199 default:
1200 BUG();
1201 }
1202 break;
Vivek Goyal062a6442010-09-15 17:06:33 -04001203 default:
1204 BUG();
1205 }
1206
1207 return 0;
1208}
1209
Tejun Heo4bfd4822012-03-05 13:15:08 -08001210static int blkio_weight_write(struct blkio_cgroup *blkcg, int plid, u64 val)
Vivek Goyal062a6442010-09-15 17:06:33 -04001211{
1212 struct blkio_group *blkg;
1213 struct hlist_node *n;
Vivek Goyal062a6442010-09-15 17:06:33 -04001214
1215 if (val < BLKIO_WEIGHT_MIN || val > BLKIO_WEIGHT_MAX)
1216 return -EINVAL;
1217
1218 spin_lock(&blkio_list_lock);
1219 spin_lock_irq(&blkcg->lock);
1220 blkcg->weight = (unsigned int)val;
1221
Tejun Heo549d3aa2012-03-05 13:15:16 -08001222 hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) {
Tejun Heoe8989fa2012-03-05 13:15:20 -08001223 struct blkg_policy_data *pd = blkg->pd[plid];
Tejun Heo549d3aa2012-03-05 13:15:16 -08001224
Tejun Heoe8989fa2012-03-05 13:15:20 -08001225 if (!pd->conf.weight)
Tejun Heoc1768262012-03-05 13:15:17 -08001226 blkio_update_group_weight(blkg, plid, blkcg->weight);
Tejun Heo549d3aa2012-03-05 13:15:16 -08001227 }
Vivek Goyal062a6442010-09-15 17:06:33 -04001228
Vivek Goyal062a6442010-09-15 17:06:33 -04001229 spin_unlock_irq(&blkcg->lock);
1230 spin_unlock(&blkio_list_lock);
1231 return 0;
1232}
1233
1234static u64 blkiocg_file_read_u64 (struct cgroup *cgrp, struct cftype *cft) {
1235 struct blkio_cgroup *blkcg;
1236 enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
1237 int name = BLKIOFILE_ATTR(cft->private);
1238
1239 blkcg = cgroup_to_blkio_cgroup(cgrp);
1240
1241 switch(plid) {
1242 case BLKIO_POLICY_PROP:
1243 switch(name) {
1244 case BLKIO_PROP_weight:
1245 return (u64)blkcg->weight;
1246 }
1247 break;
1248 default:
1249 BUG();
1250 }
1251 return 0;
1252}
1253
1254static int
1255blkiocg_file_write_u64(struct cgroup *cgrp, struct cftype *cft, u64 val)
1256{
1257 struct blkio_cgroup *blkcg;
1258 enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
1259 int name = BLKIOFILE_ATTR(cft->private);
1260
1261 blkcg = cgroup_to_blkio_cgroup(cgrp);
1262
1263 switch(plid) {
1264 case BLKIO_POLICY_PROP:
1265 switch(name) {
1266 case BLKIO_PROP_weight:
Tejun Heo4bfd4822012-03-05 13:15:08 -08001267 return blkio_weight_write(blkcg, plid, val);
Vivek Goyal062a6442010-09-15 17:06:33 -04001268 }
1269 break;
1270 default:
1271 BUG();
1272 }
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001273
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001274 return 0;
1275}
1276
Vivek Goyal31e4c282009-12-03 12:59:42 -05001277struct cftype blkio_files[] = {
1278 {
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001279 .name = "weight_device",
Vivek Goyal062a6442010-09-15 17:06:33 -04001280 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1281 BLKIO_PROP_weight_device),
1282 .read_seq_string = blkiocg_file_read,
1283 .write_string = blkiocg_file_write,
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001284 .max_write_len = 256,
1285 },
1286 {
Vivek Goyal31e4c282009-12-03 12:59:42 -05001287 .name = "weight",
Vivek Goyal062a6442010-09-15 17:06:33 -04001288 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1289 BLKIO_PROP_weight),
1290 .read_u64 = blkiocg_file_read_u64,
1291 .write_u64 = blkiocg_file_write_u64,
Vivek Goyal31e4c282009-12-03 12:59:42 -05001292 },
Vivek Goyal22084192009-12-03 12:59:49 -05001293 {
1294 .name = "time",
Tejun Heod3d32e62012-04-01 14:38:42 -07001295 .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP,
1296 offsetof(struct blkio_group_stats, time)),
1297 .read_seq_string = blkcg_print_stat,
Vivek Goyal22084192009-12-03 12:59:49 -05001298 },
1299 {
1300 .name = "sectors",
Tejun Heod3d32e62012-04-01 14:38:42 -07001301 .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP,
1302 offsetof(struct blkio_group_stats_cpu, sectors)),
1303 .read_seq_string = blkcg_print_cpu_stat,
Divyesh Shah303a3ac2010-04-01 15:01:24 -07001304 },
1305 {
1306 .name = "io_service_bytes",
Tejun Heod3d32e62012-04-01 14:38:42 -07001307 .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP,
1308 offsetof(struct blkio_group_stats_cpu, service_bytes)),
1309 .read_seq_string = blkcg_print_cpu_rwstat,
Divyesh Shah303a3ac2010-04-01 15:01:24 -07001310 },
1311 {
1312 .name = "io_serviced",
Tejun Heod3d32e62012-04-01 14:38:42 -07001313 .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP,
1314 offsetof(struct blkio_group_stats_cpu, serviced)),
1315 .read_seq_string = blkcg_print_cpu_rwstat,
Divyesh Shah303a3ac2010-04-01 15:01:24 -07001316 },
1317 {
1318 .name = "io_service_time",
Tejun Heod3d32e62012-04-01 14:38:42 -07001319 .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP,
1320 offsetof(struct blkio_group_stats, service_time)),
1321 .read_seq_string = blkcg_print_rwstat,
Divyesh Shah303a3ac2010-04-01 15:01:24 -07001322 },
1323 {
1324 .name = "io_wait_time",
Tejun Heod3d32e62012-04-01 14:38:42 -07001325 .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP,
1326 offsetof(struct blkio_group_stats, wait_time)),
1327 .read_seq_string = blkcg_print_rwstat,
Divyesh Shah84c124d2010-04-09 08:31:19 +02001328 },
1329 {
Divyesh Shah812d4022010-04-08 21:14:23 -07001330 .name = "io_merged",
Tejun Heod3d32e62012-04-01 14:38:42 -07001331 .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP,
1332 offsetof(struct blkio_group_stats, merged)),
1333 .read_seq_string = blkcg_print_rwstat,
Divyesh Shah812d4022010-04-08 21:14:23 -07001334 },
1335 {
Divyesh Shahcdc11842010-04-08 21:15:10 -07001336 .name = "io_queued",
Tejun Heod3d32e62012-04-01 14:38:42 -07001337 .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP,
1338 offsetof(struct blkio_group_stats, queued)),
1339 .read_seq_string = blkcg_print_rwstat,
Divyesh Shahcdc11842010-04-08 21:15:10 -07001340 },
1341 {
Divyesh Shah84c124d2010-04-09 08:31:19 +02001342 .name = "reset_stats",
1343 .write_u64 = blkiocg_reset_stats,
Vivek Goyal22084192009-12-03 12:59:49 -05001344 },
Vivek Goyal13f98252010-10-01 14:49:41 +02001345#ifdef CONFIG_BLK_DEV_THROTTLING
1346 {
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001347 .name = "throttle.read_bps_device",
1348 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1349 BLKIO_THROTL_read_bps_device),
1350 .read_seq_string = blkiocg_file_read,
1351 .write_string = blkiocg_file_write,
1352 .max_write_len = 256,
1353 },
1354
1355 {
1356 .name = "throttle.write_bps_device",
1357 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1358 BLKIO_THROTL_write_bps_device),
1359 .read_seq_string = blkiocg_file_read,
1360 .write_string = blkiocg_file_write,
1361 .max_write_len = 256,
1362 },
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001363
1364 {
1365 .name = "throttle.read_iops_device",
1366 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1367 BLKIO_THROTL_read_iops_device),
1368 .read_seq_string = blkiocg_file_read,
1369 .write_string = blkiocg_file_write,
1370 .max_write_len = 256,
1371 },
1372
1373 {
1374 .name = "throttle.write_iops_device",
1375 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1376 BLKIO_THROTL_write_iops_device),
1377 .read_seq_string = blkiocg_file_read,
1378 .write_string = blkiocg_file_write,
1379 .max_write_len = 256,
1380 },
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001381 {
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001382 .name = "throttle.io_service_bytes",
Tejun Heod3d32e62012-04-01 14:38:42 -07001383 .private = BLKCG_STAT_PRIV(BLKIO_POLICY_THROTL,
1384 offsetof(struct blkio_group_stats_cpu, service_bytes)),
1385 .read_seq_string = blkcg_print_cpu_rwstat,
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001386 },
1387 {
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001388 .name = "throttle.io_serviced",
Tejun Heod3d32e62012-04-01 14:38:42 -07001389 .private = BLKCG_STAT_PRIV(BLKIO_POLICY_THROTL,
1390 offsetof(struct blkio_group_stats_cpu, serviced)),
1391 .read_seq_string = blkcg_print_cpu_rwstat,
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001392 },
Vivek Goyal13f98252010-10-01 14:49:41 +02001393#endif /* CONFIG_BLK_DEV_THROTTLING */
1394
Vivek Goyal22084192009-12-03 12:59:49 -05001395#ifdef CONFIG_DEBUG_BLK_CGROUP
Divyesh Shahcdc11842010-04-08 21:15:10 -07001396 {
1397 .name = "avg_queue_size",
Tejun Heod3d32e62012-04-01 14:38:42 -07001398 .read_seq_string = blkcg_print_avg_queue_size,
Divyesh Shahcdc11842010-04-08 21:15:10 -07001399 },
1400 {
Divyesh Shah812df482010-04-08 21:15:35 -07001401 .name = "group_wait_time",
Tejun Heod3d32e62012-04-01 14:38:42 -07001402 .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP,
1403 offsetof(struct blkio_group_stats, group_wait_time)),
1404 .read_seq_string = blkcg_print_stat,
Divyesh Shah812df482010-04-08 21:15:35 -07001405 },
1406 {
1407 .name = "idle_time",
Tejun Heod3d32e62012-04-01 14:38:42 -07001408 .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP,
1409 offsetof(struct blkio_group_stats, idle_time)),
1410 .read_seq_string = blkcg_print_stat,
Divyesh Shah812df482010-04-08 21:15:35 -07001411 },
1412 {
1413 .name = "empty_time",
Tejun Heod3d32e62012-04-01 14:38:42 -07001414 .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP,
1415 offsetof(struct blkio_group_stats, empty_time)),
1416 .read_seq_string = blkcg_print_stat,
Divyesh Shah812df482010-04-08 21:15:35 -07001417 },
1418 {
Vivek Goyal22084192009-12-03 12:59:49 -05001419 .name = "dequeue",
Tejun Heod3d32e62012-04-01 14:38:42 -07001420 .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP,
1421 offsetof(struct blkio_group_stats, dequeue)),
1422 .read_seq_string = blkcg_print_stat,
Divyesh Shahcdc11842010-04-08 21:15:10 -07001423 },
Justin TerAvest9026e522011-03-22 21:26:54 +01001424 {
1425 .name = "unaccounted_time",
Tejun Heod3d32e62012-04-01 14:38:42 -07001426 .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP,
1427 offsetof(struct blkio_group_stats, unaccounted_time)),
1428 .read_seq_string = blkcg_print_stat,
Justin TerAvest9026e522011-03-22 21:26:54 +01001429 },
Vivek Goyal22084192009-12-03 12:59:49 -05001430#endif
Tejun Heo4baf6e32012-04-01 12:09:55 -07001431 { } /* terminate */
Vivek Goyal31e4c282009-12-03 12:59:42 -05001432};
1433
Tejun Heo9f13ef62012-03-05 13:15:21 -08001434/**
1435 * blkiocg_pre_destroy - cgroup pre_destroy callback
Tejun Heo9f13ef62012-03-05 13:15:21 -08001436 * @cgroup: cgroup of interest
1437 *
1438 * This function is called when @cgroup is about to go away and responsible
1439 * for shooting down all blkgs associated with @cgroup. blkgs should be
1440 * removed while holding both q and blkcg locks. As blkcg lock is nested
1441 * inside q lock, this function performs reverse double lock dancing.
1442 *
1443 * This is the blkcg counterpart of ioc_release_fn().
1444 */
Tejun Heo959d8512012-04-01 12:30:01 -07001445static int blkiocg_pre_destroy(struct cgroup *cgroup)
Vivek Goyal31e4c282009-12-03 12:59:42 -05001446{
1447 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgroup);
1448
Tejun Heo9f13ef62012-03-05 13:15:21 -08001449 spin_lock_irq(&blkcg->lock);
Tejun Heo7ee9c562012-03-05 13:15:11 -08001450
Tejun Heo9f13ef62012-03-05 13:15:21 -08001451 while (!hlist_empty(&blkcg->blkg_list)) {
1452 struct blkio_group *blkg = hlist_entry(blkcg->blkg_list.first,
1453 struct blkio_group, blkcg_node);
Tejun Heoc875f4d2012-03-05 13:15:22 -08001454 struct request_queue *q = blkg->q;
Vivek Goyalb1c35762009-12-03 12:59:47 -05001455
Tejun Heo9f13ef62012-03-05 13:15:21 -08001456 if (spin_trylock(q->queue_lock)) {
1457 blkg_destroy(blkg);
1458 spin_unlock(q->queue_lock);
1459 } else {
1460 spin_unlock_irq(&blkcg->lock);
Tejun Heo9f13ef62012-03-05 13:15:21 -08001461 cpu_relax();
Dan Carpentera5567932012-03-29 20:57:08 +02001462 spin_lock_irq(&blkcg->lock);
Jens Axboe0f3942a2010-05-03 14:28:55 +02001463 }
Tejun Heo9f13ef62012-03-05 13:15:21 -08001464 }
Jens Axboe0f3942a2010-05-03 14:28:55 +02001465
Tejun Heo9f13ef62012-03-05 13:15:21 -08001466 spin_unlock_irq(&blkcg->lock);
Tejun Heo7ee9c562012-03-05 13:15:11 -08001467 return 0;
1468}
1469
Li Zefan761b3ef2012-01-31 13:47:36 +08001470static void blkiocg_destroy(struct cgroup *cgroup)
Tejun Heo7ee9c562012-03-05 13:15:11 -08001471{
1472 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgroup);
1473
Ben Blum67523c42010-03-10 15:22:11 -08001474 if (blkcg != &blkio_root_cgroup)
1475 kfree(blkcg);
Vivek Goyal31e4c282009-12-03 12:59:42 -05001476}
1477
Li Zefan761b3ef2012-01-31 13:47:36 +08001478static struct cgroup_subsys_state *blkiocg_create(struct cgroup *cgroup)
Vivek Goyal31e4c282009-12-03 12:59:42 -05001479{
Tejun Heo9a9e8a22012-03-19 15:10:56 -07001480 static atomic64_t id_seq = ATOMIC64_INIT(0);
Li Zefan03415092010-05-07 08:57:00 +02001481 struct blkio_cgroup *blkcg;
1482 struct cgroup *parent = cgroup->parent;
Vivek Goyal31e4c282009-12-03 12:59:42 -05001483
Li Zefan03415092010-05-07 08:57:00 +02001484 if (!parent) {
Vivek Goyal31e4c282009-12-03 12:59:42 -05001485 blkcg = &blkio_root_cgroup;
1486 goto done;
1487 }
1488
Vivek Goyal31e4c282009-12-03 12:59:42 -05001489 blkcg = kzalloc(sizeof(*blkcg), GFP_KERNEL);
1490 if (!blkcg)
1491 return ERR_PTR(-ENOMEM);
1492
1493 blkcg->weight = BLKIO_WEIGHT_DEFAULT;
Tejun Heo9a9e8a22012-03-19 15:10:56 -07001494 blkcg->id = atomic64_inc_return(&id_seq); /* root is 0, start from 1 */
Vivek Goyal31e4c282009-12-03 12:59:42 -05001495done:
1496 spin_lock_init(&blkcg->lock);
1497 INIT_HLIST_HEAD(&blkcg->blkg_list);
1498
1499 return &blkcg->css;
1500}
1501
Tejun Heo5efd6112012-03-05 13:15:12 -08001502/**
1503 * blkcg_init_queue - initialize blkcg part of request queue
1504 * @q: request_queue to initialize
1505 *
1506 * Called from blk_alloc_queue_node(). Responsible for initializing blkcg
1507 * part of new request_queue @q.
1508 *
1509 * RETURNS:
1510 * 0 on success, -errno on failure.
1511 */
1512int blkcg_init_queue(struct request_queue *q)
1513{
Tejun Heo923adde2012-03-05 13:15:13 -08001514 int ret;
1515
Tejun Heo5efd6112012-03-05 13:15:12 -08001516 might_sleep();
1517
Tejun Heo923adde2012-03-05 13:15:13 -08001518 ret = blk_throtl_init(q);
1519 if (ret)
1520 return ret;
1521
1522 mutex_lock(&all_q_mutex);
1523 INIT_LIST_HEAD(&q->all_q_node);
1524 list_add_tail(&q->all_q_node, &all_q_list);
1525 mutex_unlock(&all_q_mutex);
1526
1527 return 0;
Tejun Heo5efd6112012-03-05 13:15:12 -08001528}
1529
1530/**
1531 * blkcg_drain_queue - drain blkcg part of request_queue
1532 * @q: request_queue to drain
1533 *
1534 * Called from blk_drain_queue(). Responsible for draining blkcg part.
1535 */
1536void blkcg_drain_queue(struct request_queue *q)
1537{
1538 lockdep_assert_held(q->queue_lock);
1539
1540 blk_throtl_drain(q);
1541}
1542
1543/**
1544 * blkcg_exit_queue - exit and release blkcg part of request_queue
1545 * @q: request_queue being released
1546 *
1547 * Called from blk_release_queue(). Responsible for exiting blkcg part.
1548 */
1549void blkcg_exit_queue(struct request_queue *q)
1550{
Tejun Heo923adde2012-03-05 13:15:13 -08001551 mutex_lock(&all_q_mutex);
1552 list_del_init(&q->all_q_node);
1553 mutex_unlock(&all_q_mutex);
1554
Tejun Heoe8989fa2012-03-05 13:15:20 -08001555 blkg_destroy_all(q, true);
1556
Tejun Heo5efd6112012-03-05 13:15:12 -08001557 blk_throtl_exit(q);
1558}
1559
Vivek Goyal31e4c282009-12-03 12:59:42 -05001560/*
1561 * We cannot support shared io contexts, as we have no mean to support
1562 * two tasks with the same ioc in two different groups without major rework
1563 * of the main cic data structures. For now we allow a task to change
1564 * its cgroup only if it's the only owner of its ioc.
1565 */
Li Zefan761b3ef2012-01-31 13:47:36 +08001566static int blkiocg_can_attach(struct cgroup *cgrp, struct cgroup_taskset *tset)
Vivek Goyal31e4c282009-12-03 12:59:42 -05001567{
Tejun Heobb9d97b2011-12-12 18:12:21 -08001568 struct task_struct *task;
Vivek Goyal31e4c282009-12-03 12:59:42 -05001569 struct io_context *ioc;
1570 int ret = 0;
1571
1572 /* task_lock() is needed to avoid races with exit_io_context() */
Tejun Heobb9d97b2011-12-12 18:12:21 -08001573 cgroup_taskset_for_each(task, cgrp, tset) {
1574 task_lock(task);
1575 ioc = task->io_context;
1576 if (ioc && atomic_read(&ioc->nr_tasks) > 1)
1577 ret = -EINVAL;
1578 task_unlock(task);
1579 if (ret)
1580 break;
1581 }
Vivek Goyal31e4c282009-12-03 12:59:42 -05001582 return ret;
1583}
1584
Tejun Heo923adde2012-03-05 13:15:13 -08001585static void blkcg_bypass_start(void)
1586 __acquires(&all_q_mutex)
1587{
1588 struct request_queue *q;
1589
1590 mutex_lock(&all_q_mutex);
1591
1592 list_for_each_entry(q, &all_q_list, all_q_node) {
1593 blk_queue_bypass_start(q);
Tejun Heoe8989fa2012-03-05 13:15:20 -08001594 blkg_destroy_all(q, false);
Tejun Heo923adde2012-03-05 13:15:13 -08001595 }
1596}
1597
1598static void blkcg_bypass_end(void)
1599 __releases(&all_q_mutex)
1600{
1601 struct request_queue *q;
1602
1603 list_for_each_entry(q, &all_q_list, all_q_node)
1604 blk_queue_bypass_end(q);
1605
1606 mutex_unlock(&all_q_mutex);
1607}
1608
Tejun Heo676f7c82012-04-01 12:09:55 -07001609struct cgroup_subsys blkio_subsys = {
1610 .name = "blkio",
1611 .create = blkiocg_create,
1612 .can_attach = blkiocg_can_attach,
Tejun Heo959d8512012-04-01 12:30:01 -07001613 .pre_destroy = blkiocg_pre_destroy,
Tejun Heo676f7c82012-04-01 12:09:55 -07001614 .destroy = blkiocg_destroy,
Tejun Heo676f7c82012-04-01 12:09:55 -07001615 .subsys_id = blkio_subsys_id,
Tejun Heo4baf6e32012-04-01 12:09:55 -07001616 .base_cftypes = blkio_files,
Tejun Heo676f7c82012-04-01 12:09:55 -07001617 .module = THIS_MODULE,
1618};
1619EXPORT_SYMBOL_GPL(blkio_subsys);
1620
Vivek Goyal3e252062009-12-04 10:36:42 -05001621void blkio_policy_register(struct blkio_policy_type *blkiop)
1622{
Tejun Heoe8989fa2012-03-05 13:15:20 -08001623 struct request_queue *q;
1624
Tejun Heo923adde2012-03-05 13:15:13 -08001625 blkcg_bypass_start();
Vivek Goyal3e252062009-12-04 10:36:42 -05001626 spin_lock(&blkio_list_lock);
Tejun Heo035d10b2012-03-05 13:15:04 -08001627
1628 BUG_ON(blkio_policy[blkiop->plid]);
1629 blkio_policy[blkiop->plid] = blkiop;
Vivek Goyal3e252062009-12-04 10:36:42 -05001630 list_add_tail(&blkiop->list, &blkio_list);
Tejun Heo035d10b2012-03-05 13:15:04 -08001631
Vivek Goyal3e252062009-12-04 10:36:42 -05001632 spin_unlock(&blkio_list_lock);
Tejun Heoe8989fa2012-03-05 13:15:20 -08001633 list_for_each_entry(q, &all_q_list, all_q_node)
1634 update_root_blkg_pd(q, blkiop->plid);
Tejun Heo923adde2012-03-05 13:15:13 -08001635 blkcg_bypass_end();
Vivek Goyal3e252062009-12-04 10:36:42 -05001636}
1637EXPORT_SYMBOL_GPL(blkio_policy_register);
1638
1639void blkio_policy_unregister(struct blkio_policy_type *blkiop)
1640{
Tejun Heoe8989fa2012-03-05 13:15:20 -08001641 struct request_queue *q;
1642
Tejun Heo923adde2012-03-05 13:15:13 -08001643 blkcg_bypass_start();
Vivek Goyal3e252062009-12-04 10:36:42 -05001644 spin_lock(&blkio_list_lock);
Tejun Heo035d10b2012-03-05 13:15:04 -08001645
1646 BUG_ON(blkio_policy[blkiop->plid] != blkiop);
1647 blkio_policy[blkiop->plid] = NULL;
Vivek Goyal3e252062009-12-04 10:36:42 -05001648 list_del_init(&blkiop->list);
Tejun Heo035d10b2012-03-05 13:15:04 -08001649
Vivek Goyal3e252062009-12-04 10:36:42 -05001650 spin_unlock(&blkio_list_lock);
Tejun Heoe8989fa2012-03-05 13:15:20 -08001651 list_for_each_entry(q, &all_q_list, all_q_node)
1652 update_root_blkg_pd(q, blkiop->plid);
Tejun Heo923adde2012-03-05 13:15:13 -08001653 blkcg_bypass_end();
Vivek Goyal3e252062009-12-04 10:36:42 -05001654}
1655EXPORT_SYMBOL_GPL(blkio_policy_unregister);