blob: a9723a8dc9836ab29a0b26d0cb5f8674d7147d73 [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 Heoc4682ae2012-04-01 14:38:43 -07001118/* for propio conf */
1119static u64 blkg_prfill_weight_device(struct seq_file *sf,
1120 struct blkg_policy_data *pd, int off)
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001121{
Tejun Heoc4682ae2012-04-01 14:38:43 -07001122 if (!pd->conf.weight)
1123 return 0;
1124 return __blkg_prfill_u64(sf, pd, pd->conf.weight);
Vivek Goyal062a6442010-09-15 17:06:33 -04001125}
1126
Tejun Heoc4682ae2012-04-01 14:38:43 -07001127static int blkcg_print_weight_device(struct cgroup *cgrp, struct cftype *cft,
1128 struct seq_file *sf)
Vivek Goyal062a6442010-09-15 17:06:33 -04001129{
Tejun Heoc4682ae2012-04-01 14:38:43 -07001130 blkcg_print_blkgs(sf, cgroup_to_blkio_cgroup(cgrp),
1131 blkg_prfill_weight_device, BLKIO_POLICY_PROP, 0,
1132 false);
1133 return 0;
Vivek Goyal062a6442010-09-15 17:06:33 -04001134}
1135
Tejun Heoc4682ae2012-04-01 14:38:43 -07001136static int blkcg_print_weight(struct cgroup *cgrp, struct cftype *cft,
1137 struct seq_file *sf)
Vivek Goyal062a6442010-09-15 17:06:33 -04001138{
Tejun Heoc4682ae2012-04-01 14:38:43 -07001139 seq_printf(sf, "%u\n", cgroup_to_blkio_cgroup(cgrp)->weight);
Vivek Goyal062a6442010-09-15 17:06:33 -04001140 return 0;
1141}
1142
Tejun Heo627f29f2012-04-01 14:38:43 -07001143static int blkcg_set_weight(struct cgroup *cgrp, struct cftype *cft, u64 val)
Vivek Goyal062a6442010-09-15 17:06:33 -04001144{
Tejun Heo627f29f2012-04-01 14:38:43 -07001145 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgrp);
Vivek Goyal062a6442010-09-15 17:06:33 -04001146 struct blkio_group *blkg;
1147 struct hlist_node *n;
Vivek Goyal062a6442010-09-15 17:06:33 -04001148
1149 if (val < BLKIO_WEIGHT_MIN || val > BLKIO_WEIGHT_MAX)
1150 return -EINVAL;
1151
1152 spin_lock(&blkio_list_lock);
1153 spin_lock_irq(&blkcg->lock);
1154 blkcg->weight = (unsigned int)val;
1155
Tejun Heo549d3aa2012-03-05 13:15:16 -08001156 hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) {
Tejun Heo627f29f2012-04-01 14:38:43 -07001157 struct blkg_policy_data *pd = blkg->pd[BLKIO_POLICY_PROP];
Tejun Heo549d3aa2012-03-05 13:15:16 -08001158
Tejun Heo627f29f2012-04-01 14:38:43 -07001159 if (pd && !pd->conf.weight)
1160 blkio_update_group_weight(blkg, BLKIO_POLICY_PROP,
1161 blkcg->weight);
Tejun Heo549d3aa2012-03-05 13:15:16 -08001162 }
Vivek Goyal062a6442010-09-15 17:06:33 -04001163
Vivek Goyal062a6442010-09-15 17:06:33 -04001164 spin_unlock_irq(&blkcg->lock);
1165 spin_unlock(&blkio_list_lock);
1166 return 0;
1167}
1168
Tejun Heoc4682ae2012-04-01 14:38:43 -07001169/* for blk-throttle conf */
1170#ifdef CONFIG_BLK_DEV_THROTTLING
1171static u64 blkg_prfill_conf_u64(struct seq_file *sf,
1172 struct blkg_policy_data *pd, int off)
1173{
1174 u64 v = *(u64 *)((void *)&pd->conf + off);
Vivek Goyal062a6442010-09-15 17:06:33 -04001175
Tejun Heoc4682ae2012-04-01 14:38:43 -07001176 if (!v)
1177 return 0;
1178 return __blkg_prfill_u64(sf, pd, v);
1179}
Vivek Goyal062a6442010-09-15 17:06:33 -04001180
Tejun Heoc4682ae2012-04-01 14:38:43 -07001181static int blkcg_print_conf_u64(struct cgroup *cgrp, struct cftype *cft,
1182 struct seq_file *sf)
1183{
1184 int off;
1185
1186 switch (BLKIOFILE_ATTR(cft->private)) {
1187 case BLKIO_THROTL_read_bps_device:
1188 off = offsetof(struct blkio_group_conf, bps[READ]);
1189 break;
1190 case BLKIO_THROTL_write_bps_device:
1191 off = offsetof(struct blkio_group_conf, bps[WRITE]);
1192 break;
1193 case BLKIO_THROTL_read_iops_device:
1194 off = offsetof(struct blkio_group_conf, iops[READ]);
1195 break;
1196 case BLKIO_THROTL_write_iops_device:
1197 off = offsetof(struct blkio_group_conf, iops[WRITE]);
Vivek Goyal062a6442010-09-15 17:06:33 -04001198 break;
1199 default:
Tejun Heoc4682ae2012-04-01 14:38:43 -07001200 return -EINVAL;
Vivek Goyal062a6442010-09-15 17:06:33 -04001201 }
Tejun Heoc4682ae2012-04-01 14:38:43 -07001202
1203 blkcg_print_blkgs(sf, cgroup_to_blkio_cgroup(cgrp),
1204 blkg_prfill_conf_u64, BLKIO_POLICY_THROTL,
1205 off, false);
Vivek Goyal062a6442010-09-15 17:06:33 -04001206 return 0;
1207}
Tejun Heoc4682ae2012-04-01 14:38:43 -07001208#endif
Vivek Goyal062a6442010-09-15 17:06:33 -04001209
Vivek Goyal31e4c282009-12-03 12:59:42 -05001210struct cftype blkio_files[] = {
1211 {
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001212 .name = "weight_device",
Vivek Goyal062a6442010-09-15 17:06:33 -04001213 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1214 BLKIO_PROP_weight_device),
Tejun Heoc4682ae2012-04-01 14:38:43 -07001215 .read_seq_string = blkcg_print_weight_device,
Vivek Goyal062a6442010-09-15 17:06:33 -04001216 .write_string = blkiocg_file_write,
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001217 .max_write_len = 256,
1218 },
1219 {
Vivek Goyal31e4c282009-12-03 12:59:42 -05001220 .name = "weight",
Tejun Heoc4682ae2012-04-01 14:38:43 -07001221 .read_seq_string = blkcg_print_weight,
Tejun Heo627f29f2012-04-01 14:38:43 -07001222 .write_u64 = blkcg_set_weight,
Vivek Goyal31e4c282009-12-03 12:59:42 -05001223 },
Vivek Goyal22084192009-12-03 12:59:49 -05001224 {
1225 .name = "time",
Tejun Heod3d32e62012-04-01 14:38:42 -07001226 .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP,
1227 offsetof(struct blkio_group_stats, time)),
1228 .read_seq_string = blkcg_print_stat,
Vivek Goyal22084192009-12-03 12:59:49 -05001229 },
1230 {
1231 .name = "sectors",
Tejun Heod3d32e62012-04-01 14:38:42 -07001232 .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP,
1233 offsetof(struct blkio_group_stats_cpu, sectors)),
1234 .read_seq_string = blkcg_print_cpu_stat,
Divyesh Shah303a3ac2010-04-01 15:01:24 -07001235 },
1236 {
1237 .name = "io_service_bytes",
Tejun Heod3d32e62012-04-01 14:38:42 -07001238 .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP,
1239 offsetof(struct blkio_group_stats_cpu, service_bytes)),
1240 .read_seq_string = blkcg_print_cpu_rwstat,
Divyesh Shah303a3ac2010-04-01 15:01:24 -07001241 },
1242 {
1243 .name = "io_serviced",
Tejun Heod3d32e62012-04-01 14:38:42 -07001244 .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP,
1245 offsetof(struct blkio_group_stats_cpu, serviced)),
1246 .read_seq_string = blkcg_print_cpu_rwstat,
Divyesh Shah303a3ac2010-04-01 15:01:24 -07001247 },
1248 {
1249 .name = "io_service_time",
Tejun Heod3d32e62012-04-01 14:38:42 -07001250 .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP,
1251 offsetof(struct blkio_group_stats, service_time)),
1252 .read_seq_string = blkcg_print_rwstat,
Divyesh Shah303a3ac2010-04-01 15:01:24 -07001253 },
1254 {
1255 .name = "io_wait_time",
Tejun Heod3d32e62012-04-01 14:38:42 -07001256 .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP,
1257 offsetof(struct blkio_group_stats, wait_time)),
1258 .read_seq_string = blkcg_print_rwstat,
Divyesh Shah84c124d2010-04-09 08:31:19 +02001259 },
1260 {
Divyesh Shah812d4022010-04-08 21:14:23 -07001261 .name = "io_merged",
Tejun Heod3d32e62012-04-01 14:38:42 -07001262 .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP,
1263 offsetof(struct blkio_group_stats, merged)),
1264 .read_seq_string = blkcg_print_rwstat,
Divyesh Shah812d4022010-04-08 21:14:23 -07001265 },
1266 {
Divyesh Shahcdc11842010-04-08 21:15:10 -07001267 .name = "io_queued",
Tejun Heod3d32e62012-04-01 14:38:42 -07001268 .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP,
1269 offsetof(struct blkio_group_stats, queued)),
1270 .read_seq_string = blkcg_print_rwstat,
Divyesh Shahcdc11842010-04-08 21:15:10 -07001271 },
1272 {
Divyesh Shah84c124d2010-04-09 08:31:19 +02001273 .name = "reset_stats",
1274 .write_u64 = blkiocg_reset_stats,
Vivek Goyal22084192009-12-03 12:59:49 -05001275 },
Vivek Goyal13f98252010-10-01 14:49:41 +02001276#ifdef CONFIG_BLK_DEV_THROTTLING
1277 {
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001278 .name = "throttle.read_bps_device",
1279 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1280 BLKIO_THROTL_read_bps_device),
Tejun Heoc4682ae2012-04-01 14:38:43 -07001281 .read_seq_string = blkcg_print_conf_u64,
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001282 .write_string = blkiocg_file_write,
1283 .max_write_len = 256,
1284 },
1285
1286 {
1287 .name = "throttle.write_bps_device",
1288 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1289 BLKIO_THROTL_write_bps_device),
Tejun Heoc4682ae2012-04-01 14:38:43 -07001290 .read_seq_string = blkcg_print_conf_u64,
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001291 .write_string = blkiocg_file_write,
1292 .max_write_len = 256,
1293 },
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001294
1295 {
1296 .name = "throttle.read_iops_device",
1297 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1298 BLKIO_THROTL_read_iops_device),
Tejun Heoc4682ae2012-04-01 14:38:43 -07001299 .read_seq_string = blkcg_print_conf_u64,
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001300 .write_string = blkiocg_file_write,
1301 .max_write_len = 256,
1302 },
1303
1304 {
1305 .name = "throttle.write_iops_device",
1306 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1307 BLKIO_THROTL_write_iops_device),
Tejun Heoc4682ae2012-04-01 14:38:43 -07001308 .read_seq_string = blkcg_print_conf_u64,
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001309 .write_string = blkiocg_file_write,
1310 .max_write_len = 256,
1311 },
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001312 {
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001313 .name = "throttle.io_service_bytes",
Tejun Heod3d32e62012-04-01 14:38:42 -07001314 .private = BLKCG_STAT_PRIV(BLKIO_POLICY_THROTL,
1315 offsetof(struct blkio_group_stats_cpu, service_bytes)),
1316 .read_seq_string = blkcg_print_cpu_rwstat,
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001317 },
1318 {
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001319 .name = "throttle.io_serviced",
Tejun Heod3d32e62012-04-01 14:38:42 -07001320 .private = BLKCG_STAT_PRIV(BLKIO_POLICY_THROTL,
1321 offsetof(struct blkio_group_stats_cpu, serviced)),
1322 .read_seq_string = blkcg_print_cpu_rwstat,
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001323 },
Vivek Goyal13f98252010-10-01 14:49:41 +02001324#endif /* CONFIG_BLK_DEV_THROTTLING */
1325
Vivek Goyal22084192009-12-03 12:59:49 -05001326#ifdef CONFIG_DEBUG_BLK_CGROUP
Divyesh Shahcdc11842010-04-08 21:15:10 -07001327 {
1328 .name = "avg_queue_size",
Tejun Heod3d32e62012-04-01 14:38:42 -07001329 .read_seq_string = blkcg_print_avg_queue_size,
Divyesh Shahcdc11842010-04-08 21:15:10 -07001330 },
1331 {
Divyesh Shah812df482010-04-08 21:15:35 -07001332 .name = "group_wait_time",
Tejun Heod3d32e62012-04-01 14:38:42 -07001333 .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP,
1334 offsetof(struct blkio_group_stats, group_wait_time)),
1335 .read_seq_string = blkcg_print_stat,
Divyesh Shah812df482010-04-08 21:15:35 -07001336 },
1337 {
1338 .name = "idle_time",
Tejun Heod3d32e62012-04-01 14:38:42 -07001339 .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP,
1340 offsetof(struct blkio_group_stats, idle_time)),
1341 .read_seq_string = blkcg_print_stat,
Divyesh Shah812df482010-04-08 21:15:35 -07001342 },
1343 {
1344 .name = "empty_time",
Tejun Heod3d32e62012-04-01 14:38:42 -07001345 .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP,
1346 offsetof(struct blkio_group_stats, empty_time)),
1347 .read_seq_string = blkcg_print_stat,
Divyesh Shah812df482010-04-08 21:15:35 -07001348 },
1349 {
Vivek Goyal22084192009-12-03 12:59:49 -05001350 .name = "dequeue",
Tejun Heod3d32e62012-04-01 14:38:42 -07001351 .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP,
1352 offsetof(struct blkio_group_stats, dequeue)),
1353 .read_seq_string = blkcg_print_stat,
Divyesh Shahcdc11842010-04-08 21:15:10 -07001354 },
Justin TerAvest9026e522011-03-22 21:26:54 +01001355 {
1356 .name = "unaccounted_time",
Tejun Heod3d32e62012-04-01 14:38:42 -07001357 .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP,
1358 offsetof(struct blkio_group_stats, unaccounted_time)),
1359 .read_seq_string = blkcg_print_stat,
Justin TerAvest9026e522011-03-22 21:26:54 +01001360 },
Vivek Goyal22084192009-12-03 12:59:49 -05001361#endif
Tejun Heo4baf6e32012-04-01 12:09:55 -07001362 { } /* terminate */
Vivek Goyal31e4c282009-12-03 12:59:42 -05001363};
1364
Tejun Heo9f13ef62012-03-05 13:15:21 -08001365/**
1366 * blkiocg_pre_destroy - cgroup pre_destroy callback
Tejun Heo9f13ef62012-03-05 13:15:21 -08001367 * @cgroup: cgroup of interest
1368 *
1369 * This function is called when @cgroup is about to go away and responsible
1370 * for shooting down all blkgs associated with @cgroup. blkgs should be
1371 * removed while holding both q and blkcg locks. As blkcg lock is nested
1372 * inside q lock, this function performs reverse double lock dancing.
1373 *
1374 * This is the blkcg counterpart of ioc_release_fn().
1375 */
Tejun Heo959d8512012-04-01 12:30:01 -07001376static int blkiocg_pre_destroy(struct cgroup *cgroup)
Vivek Goyal31e4c282009-12-03 12:59:42 -05001377{
1378 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgroup);
1379
Tejun Heo9f13ef62012-03-05 13:15:21 -08001380 spin_lock_irq(&blkcg->lock);
Tejun Heo7ee9c562012-03-05 13:15:11 -08001381
Tejun Heo9f13ef62012-03-05 13:15:21 -08001382 while (!hlist_empty(&blkcg->blkg_list)) {
1383 struct blkio_group *blkg = hlist_entry(blkcg->blkg_list.first,
1384 struct blkio_group, blkcg_node);
Tejun Heoc875f4d2012-03-05 13:15:22 -08001385 struct request_queue *q = blkg->q;
Vivek Goyalb1c35762009-12-03 12:59:47 -05001386
Tejun Heo9f13ef62012-03-05 13:15:21 -08001387 if (spin_trylock(q->queue_lock)) {
1388 blkg_destroy(blkg);
1389 spin_unlock(q->queue_lock);
1390 } else {
1391 spin_unlock_irq(&blkcg->lock);
Tejun Heo9f13ef62012-03-05 13:15:21 -08001392 cpu_relax();
Dan Carpentera5567932012-03-29 20:57:08 +02001393 spin_lock_irq(&blkcg->lock);
Jens Axboe0f3942a2010-05-03 14:28:55 +02001394 }
Tejun Heo9f13ef62012-03-05 13:15:21 -08001395 }
Jens Axboe0f3942a2010-05-03 14:28:55 +02001396
Tejun Heo9f13ef62012-03-05 13:15:21 -08001397 spin_unlock_irq(&blkcg->lock);
Tejun Heo7ee9c562012-03-05 13:15:11 -08001398 return 0;
1399}
1400
Li Zefan761b3ef2012-01-31 13:47:36 +08001401static void blkiocg_destroy(struct cgroup *cgroup)
Tejun Heo7ee9c562012-03-05 13:15:11 -08001402{
1403 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgroup);
1404
Ben Blum67523c42010-03-10 15:22:11 -08001405 if (blkcg != &blkio_root_cgroup)
1406 kfree(blkcg);
Vivek Goyal31e4c282009-12-03 12:59:42 -05001407}
1408
Li Zefan761b3ef2012-01-31 13:47:36 +08001409static struct cgroup_subsys_state *blkiocg_create(struct cgroup *cgroup)
Vivek Goyal31e4c282009-12-03 12:59:42 -05001410{
Tejun Heo9a9e8a22012-03-19 15:10:56 -07001411 static atomic64_t id_seq = ATOMIC64_INIT(0);
Li Zefan03415092010-05-07 08:57:00 +02001412 struct blkio_cgroup *blkcg;
1413 struct cgroup *parent = cgroup->parent;
Vivek Goyal31e4c282009-12-03 12:59:42 -05001414
Li Zefan03415092010-05-07 08:57:00 +02001415 if (!parent) {
Vivek Goyal31e4c282009-12-03 12:59:42 -05001416 blkcg = &blkio_root_cgroup;
1417 goto done;
1418 }
1419
Vivek Goyal31e4c282009-12-03 12:59:42 -05001420 blkcg = kzalloc(sizeof(*blkcg), GFP_KERNEL);
1421 if (!blkcg)
1422 return ERR_PTR(-ENOMEM);
1423
1424 blkcg->weight = BLKIO_WEIGHT_DEFAULT;
Tejun Heo9a9e8a22012-03-19 15:10:56 -07001425 blkcg->id = atomic64_inc_return(&id_seq); /* root is 0, start from 1 */
Vivek Goyal31e4c282009-12-03 12:59:42 -05001426done:
1427 spin_lock_init(&blkcg->lock);
1428 INIT_HLIST_HEAD(&blkcg->blkg_list);
1429
1430 return &blkcg->css;
1431}
1432
Tejun Heo5efd6112012-03-05 13:15:12 -08001433/**
1434 * blkcg_init_queue - initialize blkcg part of request queue
1435 * @q: request_queue to initialize
1436 *
1437 * Called from blk_alloc_queue_node(). Responsible for initializing blkcg
1438 * part of new request_queue @q.
1439 *
1440 * RETURNS:
1441 * 0 on success, -errno on failure.
1442 */
1443int blkcg_init_queue(struct request_queue *q)
1444{
Tejun Heo923adde2012-03-05 13:15:13 -08001445 int ret;
1446
Tejun Heo5efd6112012-03-05 13:15:12 -08001447 might_sleep();
1448
Tejun Heo923adde2012-03-05 13:15:13 -08001449 ret = blk_throtl_init(q);
1450 if (ret)
1451 return ret;
1452
1453 mutex_lock(&all_q_mutex);
1454 INIT_LIST_HEAD(&q->all_q_node);
1455 list_add_tail(&q->all_q_node, &all_q_list);
1456 mutex_unlock(&all_q_mutex);
1457
1458 return 0;
Tejun Heo5efd6112012-03-05 13:15:12 -08001459}
1460
1461/**
1462 * blkcg_drain_queue - drain blkcg part of request_queue
1463 * @q: request_queue to drain
1464 *
1465 * Called from blk_drain_queue(). Responsible for draining blkcg part.
1466 */
1467void blkcg_drain_queue(struct request_queue *q)
1468{
1469 lockdep_assert_held(q->queue_lock);
1470
1471 blk_throtl_drain(q);
1472}
1473
1474/**
1475 * blkcg_exit_queue - exit and release blkcg part of request_queue
1476 * @q: request_queue being released
1477 *
1478 * Called from blk_release_queue(). Responsible for exiting blkcg part.
1479 */
1480void blkcg_exit_queue(struct request_queue *q)
1481{
Tejun Heo923adde2012-03-05 13:15:13 -08001482 mutex_lock(&all_q_mutex);
1483 list_del_init(&q->all_q_node);
1484 mutex_unlock(&all_q_mutex);
1485
Tejun Heoe8989fa2012-03-05 13:15:20 -08001486 blkg_destroy_all(q, true);
1487
Tejun Heo5efd6112012-03-05 13:15:12 -08001488 blk_throtl_exit(q);
1489}
1490
Vivek Goyal31e4c282009-12-03 12:59:42 -05001491/*
1492 * We cannot support shared io contexts, as we have no mean to support
1493 * two tasks with the same ioc in two different groups without major rework
1494 * of the main cic data structures. For now we allow a task to change
1495 * its cgroup only if it's the only owner of its ioc.
1496 */
Li Zefan761b3ef2012-01-31 13:47:36 +08001497static int blkiocg_can_attach(struct cgroup *cgrp, struct cgroup_taskset *tset)
Vivek Goyal31e4c282009-12-03 12:59:42 -05001498{
Tejun Heobb9d97b2011-12-12 18:12:21 -08001499 struct task_struct *task;
Vivek Goyal31e4c282009-12-03 12:59:42 -05001500 struct io_context *ioc;
1501 int ret = 0;
1502
1503 /* task_lock() is needed to avoid races with exit_io_context() */
Tejun Heobb9d97b2011-12-12 18:12:21 -08001504 cgroup_taskset_for_each(task, cgrp, tset) {
1505 task_lock(task);
1506 ioc = task->io_context;
1507 if (ioc && atomic_read(&ioc->nr_tasks) > 1)
1508 ret = -EINVAL;
1509 task_unlock(task);
1510 if (ret)
1511 break;
1512 }
Vivek Goyal31e4c282009-12-03 12:59:42 -05001513 return ret;
1514}
1515
Tejun Heo923adde2012-03-05 13:15:13 -08001516static void blkcg_bypass_start(void)
1517 __acquires(&all_q_mutex)
1518{
1519 struct request_queue *q;
1520
1521 mutex_lock(&all_q_mutex);
1522
1523 list_for_each_entry(q, &all_q_list, all_q_node) {
1524 blk_queue_bypass_start(q);
Tejun Heoe8989fa2012-03-05 13:15:20 -08001525 blkg_destroy_all(q, false);
Tejun Heo923adde2012-03-05 13:15:13 -08001526 }
1527}
1528
1529static void blkcg_bypass_end(void)
1530 __releases(&all_q_mutex)
1531{
1532 struct request_queue *q;
1533
1534 list_for_each_entry(q, &all_q_list, all_q_node)
1535 blk_queue_bypass_end(q);
1536
1537 mutex_unlock(&all_q_mutex);
1538}
1539
Tejun Heo676f7c82012-04-01 12:09:55 -07001540struct cgroup_subsys blkio_subsys = {
1541 .name = "blkio",
1542 .create = blkiocg_create,
1543 .can_attach = blkiocg_can_attach,
Tejun Heo959d8512012-04-01 12:30:01 -07001544 .pre_destroy = blkiocg_pre_destroy,
Tejun Heo676f7c82012-04-01 12:09:55 -07001545 .destroy = blkiocg_destroy,
Tejun Heo676f7c82012-04-01 12:09:55 -07001546 .subsys_id = blkio_subsys_id,
Tejun Heo4baf6e32012-04-01 12:09:55 -07001547 .base_cftypes = blkio_files,
Tejun Heo676f7c82012-04-01 12:09:55 -07001548 .module = THIS_MODULE,
1549};
1550EXPORT_SYMBOL_GPL(blkio_subsys);
1551
Vivek Goyal3e252062009-12-04 10:36:42 -05001552void blkio_policy_register(struct blkio_policy_type *blkiop)
1553{
Tejun Heoe8989fa2012-03-05 13:15:20 -08001554 struct request_queue *q;
1555
Tejun Heo923adde2012-03-05 13:15:13 -08001556 blkcg_bypass_start();
Vivek Goyal3e252062009-12-04 10:36:42 -05001557 spin_lock(&blkio_list_lock);
Tejun Heo035d10b2012-03-05 13:15:04 -08001558
1559 BUG_ON(blkio_policy[blkiop->plid]);
1560 blkio_policy[blkiop->plid] = blkiop;
Vivek Goyal3e252062009-12-04 10:36:42 -05001561 list_add_tail(&blkiop->list, &blkio_list);
Tejun Heo035d10b2012-03-05 13:15:04 -08001562
Vivek Goyal3e252062009-12-04 10:36:42 -05001563 spin_unlock(&blkio_list_lock);
Tejun Heoe8989fa2012-03-05 13:15:20 -08001564 list_for_each_entry(q, &all_q_list, all_q_node)
1565 update_root_blkg_pd(q, blkiop->plid);
Tejun Heo923adde2012-03-05 13:15:13 -08001566 blkcg_bypass_end();
Vivek Goyal3e252062009-12-04 10:36:42 -05001567}
1568EXPORT_SYMBOL_GPL(blkio_policy_register);
1569
1570void blkio_policy_unregister(struct blkio_policy_type *blkiop)
1571{
Tejun Heoe8989fa2012-03-05 13:15:20 -08001572 struct request_queue *q;
1573
Tejun Heo923adde2012-03-05 13:15:13 -08001574 blkcg_bypass_start();
Vivek Goyal3e252062009-12-04 10:36:42 -05001575 spin_lock(&blkio_list_lock);
Tejun Heo035d10b2012-03-05 13:15:04 -08001576
1577 BUG_ON(blkio_policy[blkiop->plid] != blkiop);
1578 blkio_policy[blkiop->plid] = NULL;
Vivek Goyal3e252062009-12-04 10:36:42 -05001579 list_del_init(&blkiop->list);
Tejun Heo035d10b2012-03-05 13:15:04 -08001580
Vivek Goyal3e252062009-12-04 10:36:42 -05001581 spin_unlock(&blkio_list_lock);
Tejun Heoe8989fa2012-03-05 13:15:20 -08001582 list_for_each_entry(q, &all_q_list, all_q_node)
1583 update_root_blkg_pd(q, blkiop->plid);
Tejun Heo923adde2012-03-05 13:15:13 -08001584 blkcg_bypass_end();
Vivek Goyal3e252062009-12-04 10:36:42 -05001585}
1586EXPORT_SYMBOL_GPL(blkio_policy_unregister);