blob: 1e1ee2af7b5fbf97b8942a0ea3bb2fcdfedef717 [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 Goyal31e4c282009-12-03 12:59:42 -050046struct blkio_cgroup *cgroup_to_blkio_cgroup(struct cgroup *cgroup)
47{
48 return container_of(cgroup_subsys_state(cgroup, blkio_subsys_id),
49 struct blkio_cgroup, css);
50}
Vivek Goyal9d6a9862009-12-04 10:36:41 -050051EXPORT_SYMBOL_GPL(cgroup_to_blkio_cgroup);
Vivek Goyal31e4c282009-12-03 12:59:42 -050052
Tejun Heo4f85cb92012-03-05 13:15:28 -080053static struct blkio_cgroup *task_blkio_cgroup(struct task_struct *tsk)
Vivek Goyal70087dc2011-05-16 15:24:08 +020054{
55 return container_of(task_subsys_state(tsk, blkio_subsys_id),
56 struct blkio_cgroup, css);
57}
Tejun Heo4f85cb92012-03-05 13:15:28 -080058
59struct blkio_cgroup *bio_blkio_cgroup(struct bio *bio)
60{
61 if (bio && bio->bi_css)
62 return container_of(bio->bi_css, struct blkio_cgroup, css);
63 return task_blkio_cgroup(current);
64}
65EXPORT_SYMBOL_GPL(bio_blkio_cgroup);
Vivek Goyal70087dc2011-05-16 15:24:08 +020066
Tejun Heoc1768262012-03-05 13:15:17 -080067static inline void blkio_update_group_weight(struct blkio_group *blkg,
68 int plid, unsigned int weight)
Vivek Goyal062a6442010-09-15 17:06:33 -040069{
70 struct blkio_policy_type *blkiop;
71
72 list_for_each_entry(blkiop, &blkio_list, list) {
73 /* If this policy does not own the blkg, do not send updates */
Tejun Heoc1768262012-03-05 13:15:17 -080074 if (blkiop->plid != plid)
Vivek Goyal062a6442010-09-15 17:06:33 -040075 continue;
76 if (blkiop->ops.blkio_update_group_weight_fn)
Tejun Heoca32aef2012-03-05 13:15:03 -080077 blkiop->ops.blkio_update_group_weight_fn(blkg->q,
Vivek Goyalfe071432010-10-01 14:49:49 +020078 blkg, weight);
Vivek Goyal062a6442010-09-15 17:06:33 -040079 }
80}
81
Tejun Heoc1768262012-03-05 13:15:17 -080082static inline void blkio_update_group_bps(struct blkio_group *blkg, int plid,
Tejun Heo3a8b31d2012-04-01 14:38:43 -070083 u64 bps, int rw)
Vivek Goyal4c9eefa2010-09-15 17:06:34 -040084{
85 struct blkio_policy_type *blkiop;
86
87 list_for_each_entry(blkiop, &blkio_list, list) {
88
89 /* If this policy does not own the blkg, do not send updates */
Tejun Heoc1768262012-03-05 13:15:17 -080090 if (blkiop->plid != plid)
Vivek Goyal4c9eefa2010-09-15 17:06:34 -040091 continue;
92
Tejun Heo3a8b31d2012-04-01 14:38:43 -070093 if (rw == READ && blkiop->ops.blkio_update_group_read_bps_fn)
Tejun Heoca32aef2012-03-05 13:15:03 -080094 blkiop->ops.blkio_update_group_read_bps_fn(blkg->q,
Vivek Goyalfe071432010-10-01 14:49:49 +020095 blkg, bps);
Vivek Goyal4c9eefa2010-09-15 17:06:34 -040096
Tejun Heo3a8b31d2012-04-01 14:38:43 -070097 if (rw == WRITE && blkiop->ops.blkio_update_group_write_bps_fn)
Tejun Heoca32aef2012-03-05 13:15:03 -080098 blkiop->ops.blkio_update_group_write_bps_fn(blkg->q,
Vivek Goyalfe071432010-10-01 14:49:49 +020099 blkg, bps);
Vivek Goyal4c9eefa2010-09-15 17:06:34 -0400100 }
101}
102
Tejun Heo3a8b31d2012-04-01 14:38:43 -0700103static inline void blkio_update_group_iops(struct blkio_group *blkg, int plid,
104 u64 iops, int rw)
Vivek Goyal7702e8f2010-09-15 17:06:36 -0400105{
106 struct blkio_policy_type *blkiop;
107
108 list_for_each_entry(blkiop, &blkio_list, list) {
109
110 /* If this policy does not own the blkg, do not send updates */
Tejun Heoc1768262012-03-05 13:15:17 -0800111 if (blkiop->plid != plid)
Vivek Goyal7702e8f2010-09-15 17:06:36 -0400112 continue;
113
Tejun Heo3a8b31d2012-04-01 14:38:43 -0700114 if (rw == READ && blkiop->ops.blkio_update_group_read_iops_fn)
Tejun Heoca32aef2012-03-05 13:15:03 -0800115 blkiop->ops.blkio_update_group_read_iops_fn(blkg->q,
Vivek Goyalfe071432010-10-01 14:49:49 +0200116 blkg, iops);
Vivek Goyal7702e8f2010-09-15 17:06:36 -0400117
Tejun Heo3a8b31d2012-04-01 14:38:43 -0700118 if (rw == WRITE && blkiop->ops.blkio_update_group_write_iops_fn)
Tejun Heoca32aef2012-03-05 13:15:03 -0800119 blkiop->ops.blkio_update_group_write_iops_fn(blkg->q,
Vivek Goyalfe071432010-10-01 14:49:49 +0200120 blkg,iops);
Vivek Goyal7702e8f2010-09-15 17:06:36 -0400121 }
122}
123
Divyesh Shahcdc11842010-04-08 21:15:10 -0700124#ifdef CONFIG_DEBUG_BLK_CGROUP
Tejun Heoedf1b872012-03-08 10:54:00 -0800125/* This should be called with the queue_lock held. */
Divyesh Shah812df482010-04-08 21:15:35 -0700126static void blkio_set_start_group_wait_time(struct blkio_group *blkg,
Tejun Heoc1768262012-03-05 13:15:17 -0800127 struct blkio_policy_type *pol,
128 struct blkio_group *curr_blkg)
Divyesh Shah812df482010-04-08 21:15:35 -0700129{
Tejun Heoc1768262012-03-05 13:15:17 -0800130 struct blkg_policy_data *pd = blkg->pd[pol->plid];
Tejun Heo549d3aa2012-03-05 13:15:16 -0800131
132 if (blkio_blkg_waiting(&pd->stats))
Divyesh Shah812df482010-04-08 21:15:35 -0700133 return;
134 if (blkg == curr_blkg)
135 return;
Tejun Heo549d3aa2012-03-05 13:15:16 -0800136 pd->stats.start_group_wait_time = sched_clock();
137 blkio_mark_blkg_waiting(&pd->stats);
Divyesh Shah812df482010-04-08 21:15:35 -0700138}
139
Tejun Heoedf1b872012-03-08 10:54:00 -0800140/* This should be called with the queue_lock held. */
Divyesh Shah812df482010-04-08 21:15:35 -0700141static void blkio_update_group_wait_time(struct blkio_group_stats *stats)
142{
143 unsigned long long now;
144
145 if (!blkio_blkg_waiting(stats))
146 return;
147
148 now = sched_clock();
149 if (time_after64(now, stats->start_group_wait_time))
Tejun Heoedcb0722012-04-01 14:38:42 -0700150 blkg_stat_add(&stats->group_wait_time,
151 now - stats->start_group_wait_time);
Divyesh Shah812df482010-04-08 21:15:35 -0700152 blkio_clear_blkg_waiting(stats);
153}
154
Tejun Heoedf1b872012-03-08 10:54:00 -0800155/* This should be called with the queue_lock held. */
Divyesh Shah812df482010-04-08 21:15:35 -0700156static void blkio_end_empty_time(struct blkio_group_stats *stats)
157{
158 unsigned long long now;
159
160 if (!blkio_blkg_empty(stats))
161 return;
162
163 now = sched_clock();
164 if (time_after64(now, stats->start_empty_time))
Tejun Heoedcb0722012-04-01 14:38:42 -0700165 blkg_stat_add(&stats->empty_time,
166 now - stats->start_empty_time);
Divyesh Shah812df482010-04-08 21:15:35 -0700167 blkio_clear_blkg_empty(stats);
168}
169
Tejun Heoc1768262012-03-05 13:15:17 -0800170void blkiocg_update_set_idle_time_stats(struct blkio_group *blkg,
171 struct blkio_policy_type *pol)
Divyesh Shah812df482010-04-08 21:15:35 -0700172{
Tejun Heoedf1b872012-03-08 10:54:00 -0800173 struct blkio_group_stats *stats = &blkg->pd[pol->plid]->stats;
Divyesh Shah812df482010-04-08 21:15:35 -0700174
Tejun Heoedf1b872012-03-08 10:54:00 -0800175 lockdep_assert_held(blkg->q->queue_lock);
176 BUG_ON(blkio_blkg_idling(stats));
177
178 stats->start_idle_time = sched_clock();
179 blkio_mark_blkg_idling(stats);
Divyesh Shah812df482010-04-08 21:15:35 -0700180}
181EXPORT_SYMBOL_GPL(blkiocg_update_set_idle_time_stats);
182
Tejun Heoc1768262012-03-05 13:15:17 -0800183void blkiocg_update_idle_time_stats(struct blkio_group *blkg,
184 struct blkio_policy_type *pol)
Divyesh Shah812df482010-04-08 21:15:35 -0700185{
Tejun Heoedf1b872012-03-08 10:54:00 -0800186 struct blkio_group_stats *stats = &blkg->pd[pol->plid]->stats;
Divyesh Shah812df482010-04-08 21:15:35 -0700187
Tejun Heoedf1b872012-03-08 10:54:00 -0800188 lockdep_assert_held(blkg->q->queue_lock);
189
Divyesh Shah812df482010-04-08 21:15:35 -0700190 if (blkio_blkg_idling(stats)) {
Tejun Heoedf1b872012-03-08 10:54:00 -0800191 unsigned long long now = sched_clock();
192
Tejun Heoedcb0722012-04-01 14:38:42 -0700193 if (time_after64(now, stats->start_idle_time))
194 blkg_stat_add(&stats->idle_time,
195 now - stats->start_idle_time);
Divyesh Shah812df482010-04-08 21:15:35 -0700196 blkio_clear_blkg_idling(stats);
197 }
Divyesh Shah812df482010-04-08 21:15:35 -0700198}
199EXPORT_SYMBOL_GPL(blkiocg_update_idle_time_stats);
200
Tejun Heoc1768262012-03-05 13:15:17 -0800201void blkiocg_update_avg_queue_size_stats(struct blkio_group *blkg,
202 struct blkio_policy_type *pol)
Divyesh Shahcdc11842010-04-08 21:15:10 -0700203{
Tejun Heoedf1b872012-03-08 10:54:00 -0800204 struct blkio_group_stats *stats = &blkg->pd[pol->plid]->stats;
Divyesh Shahcdc11842010-04-08 21:15:10 -0700205
Tejun Heoedf1b872012-03-08 10:54:00 -0800206 lockdep_assert_held(blkg->q->queue_lock);
207
Tejun Heoedcb0722012-04-01 14:38:42 -0700208 blkg_stat_add(&stats->avg_queue_size_sum,
209 blkg_rwstat_sum(&stats->queued));
210 blkg_stat_add(&stats->avg_queue_size_samples, 1);
Divyesh Shah812df482010-04-08 21:15:35 -0700211 blkio_update_group_wait_time(stats);
Divyesh Shahcdc11842010-04-08 21:15:10 -0700212}
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200213EXPORT_SYMBOL_GPL(blkiocg_update_avg_queue_size_stats);
214
Tejun Heoc1768262012-03-05 13:15:17 -0800215void blkiocg_set_start_empty_time(struct blkio_group *blkg,
216 struct blkio_policy_type *pol)
Divyesh Shah28baf442010-04-14 11:22:38 +0200217{
Tejun Heoedf1b872012-03-08 10:54:00 -0800218 struct blkio_group_stats *stats = &blkg->pd[pol->plid]->stats;
Divyesh Shah28baf442010-04-14 11:22:38 +0200219
Tejun Heoedf1b872012-03-08 10:54:00 -0800220 lockdep_assert_held(blkg->q->queue_lock);
Divyesh Shah28baf442010-04-14 11:22:38 +0200221
Tejun Heoedcb0722012-04-01 14:38:42 -0700222 if (blkg_rwstat_sum(&stats->queued))
Divyesh Shah28baf442010-04-14 11:22:38 +0200223 return;
Divyesh Shah28baf442010-04-14 11:22:38 +0200224
225 /*
Vivek Goyale5ff0822010-04-26 19:25:11 +0200226 * group is already marked empty. This can happen if cfqq got new
227 * request in parent group and moved to this group while being added
228 * to service tree. Just ignore the event and move on.
Divyesh Shah28baf442010-04-14 11:22:38 +0200229 */
Tejun Heoedf1b872012-03-08 10:54:00 -0800230 if (blkio_blkg_empty(stats))
Vivek Goyale5ff0822010-04-26 19:25:11 +0200231 return;
Vivek Goyale5ff0822010-04-26 19:25:11 +0200232
Divyesh Shah28baf442010-04-14 11:22:38 +0200233 stats->start_empty_time = sched_clock();
234 blkio_mark_blkg_empty(stats);
Divyesh Shah28baf442010-04-14 11:22:38 +0200235}
236EXPORT_SYMBOL_GPL(blkiocg_set_start_empty_time);
237
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200238void blkiocg_update_dequeue_stats(struct blkio_group *blkg,
Tejun Heoc1768262012-03-05 13:15:17 -0800239 struct blkio_policy_type *pol,
240 unsigned long dequeue)
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200241{
Tejun Heoc1768262012-03-05 13:15:17 -0800242 struct blkg_policy_data *pd = blkg->pd[pol->plid];
Tejun Heo549d3aa2012-03-05 13:15:16 -0800243
Tejun Heoedf1b872012-03-08 10:54:00 -0800244 lockdep_assert_held(blkg->q->queue_lock);
245
Tejun Heoedcb0722012-04-01 14:38:42 -0700246 blkg_stat_add(&pd->stats.dequeue, dequeue);
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200247}
248EXPORT_SYMBOL_GPL(blkiocg_update_dequeue_stats);
Divyesh Shah812df482010-04-08 21:15:35 -0700249#else
250static inline void blkio_set_start_group_wait_time(struct blkio_group *blkg,
Tejun Heoc1768262012-03-05 13:15:17 -0800251 struct blkio_policy_type *pol,
252 struct blkio_group *curr_blkg) { }
253static inline void blkio_end_empty_time(struct blkio_group_stats *stats) { }
Divyesh Shahcdc11842010-04-08 21:15:10 -0700254#endif
255
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200256void blkiocg_update_io_add_stats(struct blkio_group *blkg,
Tejun Heoc1768262012-03-05 13:15:17 -0800257 struct blkio_policy_type *pol,
258 struct blkio_group *curr_blkg, bool direction,
259 bool sync)
Divyesh Shahcdc11842010-04-08 21:15:10 -0700260{
Tejun Heoedf1b872012-03-08 10:54:00 -0800261 struct blkio_group_stats *stats = &blkg->pd[pol->plid]->stats;
Tejun Heoedcb0722012-04-01 14:38:42 -0700262 int rw = (direction ? REQ_WRITE : 0) | (sync ? REQ_SYNC : 0);
Divyesh Shahcdc11842010-04-08 21:15:10 -0700263
Tejun Heoedf1b872012-03-08 10:54:00 -0800264 lockdep_assert_held(blkg->q->queue_lock);
265
Tejun Heoedcb0722012-04-01 14:38:42 -0700266 blkg_rwstat_add(&stats->queued, rw, 1);
Tejun Heoedf1b872012-03-08 10:54:00 -0800267 blkio_end_empty_time(stats);
Tejun Heoc1768262012-03-05 13:15:17 -0800268 blkio_set_start_group_wait_time(blkg, pol, curr_blkg);
Divyesh Shahcdc11842010-04-08 21:15:10 -0700269}
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200270EXPORT_SYMBOL_GPL(blkiocg_update_io_add_stats);
Divyesh Shahcdc11842010-04-08 21:15:10 -0700271
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200272void blkiocg_update_io_remove_stats(struct blkio_group *blkg,
Tejun Heoc1768262012-03-05 13:15:17 -0800273 struct blkio_policy_type *pol,
274 bool direction, bool sync)
Divyesh Shahcdc11842010-04-08 21:15:10 -0700275{
Tejun Heoedf1b872012-03-08 10:54:00 -0800276 struct blkio_group_stats *stats = &blkg->pd[pol->plid]->stats;
Tejun Heoedcb0722012-04-01 14:38:42 -0700277 int rw = (direction ? REQ_WRITE : 0) | (sync ? REQ_SYNC : 0);
Divyesh Shahcdc11842010-04-08 21:15:10 -0700278
Tejun Heoedf1b872012-03-08 10:54:00 -0800279 lockdep_assert_held(blkg->q->queue_lock);
280
Tejun Heoedcb0722012-04-01 14:38:42 -0700281 blkg_rwstat_add(&stats->queued, rw, -1);
Divyesh Shahcdc11842010-04-08 21:15:10 -0700282}
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200283EXPORT_SYMBOL_GPL(blkiocg_update_io_remove_stats);
Divyesh Shahcdc11842010-04-08 21:15:10 -0700284
Tejun Heoc1768262012-03-05 13:15:17 -0800285void blkiocg_update_timeslice_used(struct blkio_group *blkg,
286 struct blkio_policy_type *pol,
287 unsigned long time,
288 unsigned long unaccounted_time)
Vivek Goyal22084192009-12-03 12:59:49 -0500289{
Tejun Heoedf1b872012-03-08 10:54:00 -0800290 struct blkio_group_stats *stats = &blkg->pd[pol->plid]->stats;
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700291
Tejun Heoedf1b872012-03-08 10:54:00 -0800292 lockdep_assert_held(blkg->q->queue_lock);
293
Tejun Heoedcb0722012-04-01 14:38:42 -0700294 blkg_stat_add(&stats->time, time);
Vivek Goyala23e6862011-05-19 15:38:20 -0400295#ifdef CONFIG_DEBUG_BLK_CGROUP
Tejun Heoedcb0722012-04-01 14:38:42 -0700296 blkg_stat_add(&stats->unaccounted_time, unaccounted_time);
Vivek Goyala23e6862011-05-19 15:38:20 -0400297#endif
Vivek Goyal22084192009-12-03 12:59:49 -0500298}
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700299EXPORT_SYMBOL_GPL(blkiocg_update_timeslice_used);
Vivek Goyal22084192009-12-03 12:59:49 -0500300
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400301/*
302 * should be called under rcu read lock or queue lock to make sure blkg pointer
303 * is valid.
304 */
Divyesh Shah84c124d2010-04-09 08:31:19 +0200305void blkiocg_update_dispatch_stats(struct blkio_group *blkg,
Tejun Heoc1768262012-03-05 13:15:17 -0800306 struct blkio_policy_type *pol,
307 uint64_t bytes, bool direction, bool sync)
Divyesh Shah91952912010-04-01 15:01:41 -0700308{
Tejun Heoedcb0722012-04-01 14:38:42 -0700309 int rw = (direction ? REQ_WRITE : 0) | (sync ? REQ_SYNC : 0);
Tejun Heoc1768262012-03-05 13:15:17 -0800310 struct blkg_policy_data *pd = blkg->pd[pol->plid];
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400311 struct blkio_group_stats_cpu *stats_cpu;
Vivek Goyal575969a2011-05-19 15:38:29 -0400312 unsigned long flags;
313
Vivek Goyal1cd9e032012-03-08 10:53:56 -0800314 /* If per cpu stats are not allocated yet, don't do any accounting. */
315 if (pd->stats_cpu == NULL)
316 return;
317
Vivek Goyal575969a2011-05-19 15:38:29 -0400318 /*
319 * Disabling interrupts to provide mutual exclusion between two
320 * writes on same cpu. It probably is not needed for 64bit. Not
321 * optimizing that case yet.
322 */
323 local_irq_save(flags);
Divyesh Shah91952912010-04-01 15:01:41 -0700324
Tejun Heo549d3aa2012-03-05 13:15:16 -0800325 stats_cpu = this_cpu_ptr(pd->stats_cpu);
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400326
Tejun Heoedcb0722012-04-01 14:38:42 -0700327 blkg_stat_add(&stats_cpu->sectors, bytes >> 9);
328 blkg_rwstat_add(&stats_cpu->serviced, rw, 1);
329 blkg_rwstat_add(&stats_cpu->service_bytes, rw, bytes);
330
Vivek Goyal575969a2011-05-19 15:38:29 -0400331 local_irq_restore(flags);
Divyesh Shah91952912010-04-01 15:01:41 -0700332}
Divyesh Shah84c124d2010-04-09 08:31:19 +0200333EXPORT_SYMBOL_GPL(blkiocg_update_dispatch_stats);
Divyesh Shah91952912010-04-01 15:01:41 -0700334
Divyesh Shah84c124d2010-04-09 08:31:19 +0200335void blkiocg_update_completion_stats(struct blkio_group *blkg,
Tejun Heoc1768262012-03-05 13:15:17 -0800336 struct blkio_policy_type *pol,
337 uint64_t start_time,
338 uint64_t io_start_time, bool direction,
339 bool sync)
Divyesh Shah91952912010-04-01 15:01:41 -0700340{
Tejun Heoedf1b872012-03-08 10:54:00 -0800341 struct blkio_group_stats *stats = &blkg->pd[pol->plid]->stats;
Divyesh Shah91952912010-04-01 15:01:41 -0700342 unsigned long long now = sched_clock();
Tejun Heoedcb0722012-04-01 14:38:42 -0700343 int rw = (direction ? REQ_WRITE : 0) | (sync ? REQ_SYNC : 0);
Divyesh Shah91952912010-04-01 15:01:41 -0700344
Tejun Heoedf1b872012-03-08 10:54:00 -0800345 lockdep_assert_held(blkg->q->queue_lock);
346
Divyesh Shah84c124d2010-04-09 08:31:19 +0200347 if (time_after64(now, io_start_time))
Tejun Heoedcb0722012-04-01 14:38:42 -0700348 blkg_rwstat_add(&stats->service_time, rw, now - io_start_time);
Divyesh Shah84c124d2010-04-09 08:31:19 +0200349 if (time_after64(io_start_time, start_time))
Tejun Heoedcb0722012-04-01 14:38:42 -0700350 blkg_rwstat_add(&stats->wait_time, rw,
351 io_start_time - start_time);
Divyesh Shah91952912010-04-01 15:01:41 -0700352}
Divyesh Shah84c124d2010-04-09 08:31:19 +0200353EXPORT_SYMBOL_GPL(blkiocg_update_completion_stats);
Divyesh Shah91952912010-04-01 15:01:41 -0700354
Vivek Goyal317389a2011-05-23 10:02:19 +0200355/* Merged stats are per cpu. */
Tejun Heoc1768262012-03-05 13:15:17 -0800356void blkiocg_update_io_merged_stats(struct blkio_group *blkg,
357 struct blkio_policy_type *pol,
358 bool direction, bool sync)
Divyesh Shah812d4022010-04-08 21:14:23 -0700359{
Tejun Heoedf1b872012-03-08 10:54:00 -0800360 struct blkio_group_stats *stats = &blkg->pd[pol->plid]->stats;
Tejun Heoedcb0722012-04-01 14:38:42 -0700361 int rw = (direction ? REQ_WRITE : 0) | (sync ? REQ_SYNC : 0);
Divyesh Shah812d4022010-04-08 21:14:23 -0700362
Tejun Heoedf1b872012-03-08 10:54:00 -0800363 lockdep_assert_held(blkg->q->queue_lock);
364
Tejun Heoedcb0722012-04-01 14:38:42 -0700365 blkg_rwstat_add(&stats->merged, rw, 1);
Divyesh Shah812d4022010-04-08 21:14:23 -0700366}
367EXPORT_SYMBOL_GPL(blkiocg_update_io_merged_stats);
368
Vivek Goyal1cd9e032012-03-08 10:53:56 -0800369/*
370 * Worker for allocating per cpu stat for blk groups. This is scheduled on
371 * the system_nrt_wq once there are some groups on the alloc_list waiting
372 * for allocation.
373 */
374static void blkio_stat_alloc_fn(struct work_struct *work)
375{
376 static void *pcpu_stats[BLKIO_NR_POLICIES];
377 struct delayed_work *dwork = to_delayed_work(work);
378 struct blkio_group *blkg;
379 int i;
380 bool empty = false;
381
382alloc_stats:
383 for (i = 0; i < BLKIO_NR_POLICIES; i++) {
384 if (pcpu_stats[i] != NULL)
385 continue;
386
387 pcpu_stats[i] = alloc_percpu(struct blkio_group_stats_cpu);
388
389 /* Allocation failed. Try again after some time. */
390 if (pcpu_stats[i] == NULL) {
391 queue_delayed_work(system_nrt_wq, dwork,
392 msecs_to_jiffies(10));
393 return;
394 }
395 }
396
397 spin_lock_irq(&blkio_list_lock);
398 spin_lock(&alloc_list_lock);
399
400 /* cgroup got deleted or queue exited. */
401 if (!list_empty(&alloc_list)) {
402 blkg = list_first_entry(&alloc_list, struct blkio_group,
403 alloc_node);
404 for (i = 0; i < BLKIO_NR_POLICIES; i++) {
405 struct blkg_policy_data *pd = blkg->pd[i];
406
407 if (blkio_policy[i] && pd && !pd->stats_cpu)
408 swap(pd->stats_cpu, pcpu_stats[i]);
409 }
410
411 list_del_init(&blkg->alloc_node);
412 }
413
414 empty = list_empty(&alloc_list);
415
416 spin_unlock(&alloc_list_lock);
417 spin_unlock_irq(&blkio_list_lock);
418
419 if (!empty)
420 goto alloc_stats;
421}
422
Tejun Heo03814112012-03-05 13:15:14 -0800423/**
424 * blkg_free - free a blkg
425 * @blkg: blkg to free
426 *
427 * Free @blkg which may be partially allocated.
428 */
429static void blkg_free(struct blkio_group *blkg)
430{
Tejun Heoe8989fa2012-03-05 13:15:20 -0800431 int i;
Tejun Heo549d3aa2012-03-05 13:15:16 -0800432
433 if (!blkg)
434 return;
435
Tejun Heoe8989fa2012-03-05 13:15:20 -0800436 for (i = 0; i < BLKIO_NR_POLICIES; i++) {
437 struct blkg_policy_data *pd = blkg->pd[i];
438
439 if (pd) {
440 free_percpu(pd->stats_cpu);
441 kfree(pd);
442 }
Tejun Heo03814112012-03-05 13:15:14 -0800443 }
Tejun Heoe8989fa2012-03-05 13:15:20 -0800444
Tejun Heo549d3aa2012-03-05 13:15:16 -0800445 kfree(blkg);
Tejun Heo03814112012-03-05 13:15:14 -0800446}
447
448/**
449 * blkg_alloc - allocate a blkg
450 * @blkcg: block cgroup the new blkg is associated with
451 * @q: request_queue the new blkg is associated with
Tejun Heo03814112012-03-05 13:15:14 -0800452 *
Tejun Heoe8989fa2012-03-05 13:15:20 -0800453 * Allocate a new blkg assocating @blkcg and @q.
Tejun Heo03814112012-03-05 13:15:14 -0800454 */
455static struct blkio_group *blkg_alloc(struct blkio_cgroup *blkcg,
Tejun Heoe8989fa2012-03-05 13:15:20 -0800456 struct request_queue *q)
Tejun Heo03814112012-03-05 13:15:14 -0800457{
458 struct blkio_group *blkg;
Tejun Heoe8989fa2012-03-05 13:15:20 -0800459 int i;
Tejun Heo03814112012-03-05 13:15:14 -0800460
461 /* alloc and init base part */
462 blkg = kzalloc_node(sizeof(*blkg), GFP_ATOMIC, q->node);
463 if (!blkg)
464 return NULL;
465
Tejun Heoc875f4d2012-03-05 13:15:22 -0800466 blkg->q = q;
Tejun Heoe8989fa2012-03-05 13:15:20 -0800467 INIT_LIST_HEAD(&blkg->q_node);
Vivek Goyal1cd9e032012-03-08 10:53:56 -0800468 INIT_LIST_HEAD(&blkg->alloc_node);
Tejun Heo03814112012-03-05 13:15:14 -0800469 blkg->blkcg = blkcg;
Tejun Heo1adaf3d2012-03-05 13:15:15 -0800470 blkg->refcnt = 1;
Tejun Heo03814112012-03-05 13:15:14 -0800471 cgroup_path(blkcg->css.cgroup, blkg->path, sizeof(blkg->path));
472
Tejun Heoe8989fa2012-03-05 13:15:20 -0800473 for (i = 0; i < BLKIO_NR_POLICIES; i++) {
474 struct blkio_policy_type *pol = blkio_policy[i];
475 struct blkg_policy_data *pd;
Tejun Heo03814112012-03-05 13:15:14 -0800476
Tejun Heoe8989fa2012-03-05 13:15:20 -0800477 if (!pol)
478 continue;
Tejun Heo549d3aa2012-03-05 13:15:16 -0800479
Tejun Heoe8989fa2012-03-05 13:15:20 -0800480 /* alloc per-policy data and attach it to blkg */
481 pd = kzalloc_node(sizeof(*pd) + pol->pdata_size, GFP_ATOMIC,
482 q->node);
483 if (!pd) {
484 blkg_free(blkg);
485 return NULL;
486 }
Tejun Heo549d3aa2012-03-05 13:15:16 -0800487
Tejun Heoe8989fa2012-03-05 13:15:20 -0800488 blkg->pd[i] = pd;
489 pd->blkg = blkg;
Tejun Heo03814112012-03-05 13:15:14 -0800490 }
491
Tejun Heo549d3aa2012-03-05 13:15:16 -0800492 /* invoke per-policy init */
Tejun Heoe8989fa2012-03-05 13:15:20 -0800493 for (i = 0; i < BLKIO_NR_POLICIES; i++) {
494 struct blkio_policy_type *pol = blkio_policy[i];
495
496 if (pol)
497 pol->ops.blkio_init_group_fn(blkg);
498 }
499
Tejun Heo03814112012-03-05 13:15:14 -0800500 return blkg;
501}
502
Tejun Heocd1604f2012-03-05 13:15:06 -0800503struct blkio_group *blkg_lookup_create(struct blkio_cgroup *blkcg,
504 struct request_queue *q,
Tejun Heocd1604f2012-03-05 13:15:06 -0800505 bool for_root)
506 __releases(q->queue_lock) __acquires(q->queue_lock)
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400507{
Vivek Goyal1cd9e032012-03-08 10:53:56 -0800508 struct blkio_group *blkg;
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400509
Tejun Heocd1604f2012-03-05 13:15:06 -0800510 WARN_ON_ONCE(!rcu_read_lock_held());
511 lockdep_assert_held(q->queue_lock);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500512
Tejun Heocd1604f2012-03-05 13:15:06 -0800513 /*
514 * This could be the first entry point of blkcg implementation and
515 * we shouldn't allow anything to go through for a bypassing queue.
516 * The following can be removed if blkg lookup is guaranteed to
517 * fail on a bypassing queue.
518 */
519 if (unlikely(blk_queue_bypass(q)) && !for_root)
520 return ERR_PTR(blk_queue_dead(q) ? -EINVAL : -EBUSY);
521
Tejun Heoe8989fa2012-03-05 13:15:20 -0800522 blkg = blkg_lookup(blkcg, q);
Tejun Heocd1604f2012-03-05 13:15:06 -0800523 if (blkg)
524 return blkg;
525
Tejun Heo7ee9c562012-03-05 13:15:11 -0800526 /* blkg holds a reference to blkcg */
Tejun Heocd1604f2012-03-05 13:15:06 -0800527 if (!css_tryget(&blkcg->css))
528 return ERR_PTR(-EINVAL);
529
530 /*
531 * Allocate and initialize.
Tejun Heocd1604f2012-03-05 13:15:06 -0800532 */
Vivek Goyal1cd9e032012-03-08 10:53:56 -0800533 blkg = blkg_alloc(blkcg, q);
Tejun Heocd1604f2012-03-05 13:15:06 -0800534
535 /* did alloc fail? */
Vivek Goyal1cd9e032012-03-08 10:53:56 -0800536 if (unlikely(!blkg)) {
Tejun Heocd1604f2012-03-05 13:15:06 -0800537 blkg = ERR_PTR(-ENOMEM);
538 goto out;
539 }
540
541 /* insert */
542 spin_lock(&blkcg->lock);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500543 hlist_add_head_rcu(&blkg->blkcg_node, &blkcg->blkg_list);
Tejun Heoe8989fa2012-03-05 13:15:20 -0800544 list_add(&blkg->q_node, &q->blkg_list);
Tejun Heocd1604f2012-03-05 13:15:06 -0800545 spin_unlock(&blkcg->lock);
Vivek Goyal1cd9e032012-03-08 10:53:56 -0800546
547 spin_lock(&alloc_list_lock);
548 list_add(&blkg->alloc_node, &alloc_list);
549 /* Queue per cpu stat allocation from worker thread. */
550 queue_delayed_work(system_nrt_wq, &blkio_stat_alloc_work, 0);
551 spin_unlock(&alloc_list_lock);
Tejun Heocd1604f2012-03-05 13:15:06 -0800552out:
Tejun Heocd1604f2012-03-05 13:15:06 -0800553 return blkg;
Vivek Goyal31e4c282009-12-03 12:59:42 -0500554}
Tejun Heocd1604f2012-03-05 13:15:06 -0800555EXPORT_SYMBOL_GPL(blkg_lookup_create);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500556
Vivek Goyal31e4c282009-12-03 12:59:42 -0500557/* called under rcu_read_lock(). */
Tejun Heocd1604f2012-03-05 13:15:06 -0800558struct blkio_group *blkg_lookup(struct blkio_cgroup *blkcg,
Tejun Heoe8989fa2012-03-05 13:15:20 -0800559 struct request_queue *q)
Vivek Goyal31e4c282009-12-03 12:59:42 -0500560{
561 struct blkio_group *blkg;
562 struct hlist_node *n;
Vivek Goyal31e4c282009-12-03 12:59:42 -0500563
Tejun Heoca32aef2012-03-05 13:15:03 -0800564 hlist_for_each_entry_rcu(blkg, n, &blkcg->blkg_list, blkcg_node)
Tejun Heoe8989fa2012-03-05 13:15:20 -0800565 if (blkg->q == q)
Vivek Goyal31e4c282009-12-03 12:59:42 -0500566 return blkg;
Vivek Goyal31e4c282009-12-03 12:59:42 -0500567 return NULL;
568}
Tejun Heocd1604f2012-03-05 13:15:06 -0800569EXPORT_SYMBOL_GPL(blkg_lookup);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500570
Tejun Heoe8989fa2012-03-05 13:15:20 -0800571static void blkg_destroy(struct blkio_group *blkg)
Tejun Heo72e06c22012-03-05 13:15:00 -0800572{
Tejun Heo03aa2642012-03-05 13:15:19 -0800573 struct request_queue *q = blkg->q;
Tejun Heo9f13ef62012-03-05 13:15:21 -0800574 struct blkio_cgroup *blkcg = blkg->blkcg;
Tejun Heo03aa2642012-03-05 13:15:19 -0800575
576 lockdep_assert_held(q->queue_lock);
Tejun Heo9f13ef62012-03-05 13:15:21 -0800577 lockdep_assert_held(&blkcg->lock);
Tejun Heo03aa2642012-03-05 13:15:19 -0800578
579 /* Something wrong if we are trying to remove same group twice */
Tejun Heoe8989fa2012-03-05 13:15:20 -0800580 WARN_ON_ONCE(list_empty(&blkg->q_node));
Tejun Heo9f13ef62012-03-05 13:15:21 -0800581 WARN_ON_ONCE(hlist_unhashed(&blkg->blkcg_node));
Tejun Heoe8989fa2012-03-05 13:15:20 -0800582 list_del_init(&blkg->q_node);
Tejun Heo9f13ef62012-03-05 13:15:21 -0800583 hlist_del_init_rcu(&blkg->blkcg_node);
Tejun Heo03aa2642012-03-05 13:15:19 -0800584
Vivek Goyal1cd9e032012-03-08 10:53:56 -0800585 spin_lock(&alloc_list_lock);
586 list_del_init(&blkg->alloc_node);
587 spin_unlock(&alloc_list_lock);
588
Tejun Heo03aa2642012-03-05 13:15:19 -0800589 /*
590 * Put the reference taken at the time of creation so that when all
591 * queues are gone, group can be destroyed.
592 */
593 blkg_put(blkg);
594}
595
Tejun Heoe8989fa2012-03-05 13:15:20 -0800596/*
597 * XXX: This updates blkg policy data in-place for root blkg, which is
598 * necessary across elevator switch and policy registration as root blkgs
599 * aren't shot down. This broken and racy implementation is temporary.
600 * Eventually, blkg shoot down will be replaced by proper in-place update.
601 */
602void update_root_blkg_pd(struct request_queue *q, enum blkio_policy_id plid)
603{
604 struct blkio_policy_type *pol = blkio_policy[plid];
605 struct blkio_group *blkg = blkg_lookup(&blkio_root_cgroup, q);
606 struct blkg_policy_data *pd;
607
608 if (!blkg)
609 return;
610
611 kfree(blkg->pd[plid]);
612 blkg->pd[plid] = NULL;
613
614 if (!pol)
615 return;
616
617 pd = kzalloc(sizeof(*pd) + pol->pdata_size, GFP_KERNEL);
618 WARN_ON_ONCE(!pd);
619
620 pd->stats_cpu = alloc_percpu(struct blkio_group_stats_cpu);
621 WARN_ON_ONCE(!pd->stats_cpu);
622
623 blkg->pd[plid] = pd;
624 pd->blkg = blkg;
625 pol->ops.blkio_init_group_fn(blkg);
626}
627EXPORT_SYMBOL_GPL(update_root_blkg_pd);
628
Tejun Heo9f13ef62012-03-05 13:15:21 -0800629/**
630 * blkg_destroy_all - destroy all blkgs associated with a request_queue
631 * @q: request_queue of interest
632 * @destroy_root: whether to destroy root blkg or not
633 *
634 * Destroy blkgs associated with @q. If @destroy_root is %true, all are
635 * destroyed; otherwise, root blkg is left alone.
636 */
Tejun Heoe8989fa2012-03-05 13:15:20 -0800637void blkg_destroy_all(struct request_queue *q, bool destroy_root)
Tejun Heo03aa2642012-03-05 13:15:19 -0800638{
639 struct blkio_group *blkg, *n;
Tejun Heo72e06c22012-03-05 13:15:00 -0800640
Tejun Heo9f13ef62012-03-05 13:15:21 -0800641 spin_lock_irq(q->queue_lock);
Tejun Heo72e06c22012-03-05 13:15:00 -0800642
Tejun Heo9f13ef62012-03-05 13:15:21 -0800643 list_for_each_entry_safe(blkg, n, &q->blkg_list, q_node) {
644 struct blkio_cgroup *blkcg = blkg->blkcg;
Tejun Heo72e06c22012-03-05 13:15:00 -0800645
Tejun Heo9f13ef62012-03-05 13:15:21 -0800646 /* skip root? */
647 if (!destroy_root && blkg->blkcg == &blkio_root_cgroup)
648 continue;
Tejun Heo03aa2642012-03-05 13:15:19 -0800649
Tejun Heo9f13ef62012-03-05 13:15:21 -0800650 spin_lock(&blkcg->lock);
651 blkg_destroy(blkg);
652 spin_unlock(&blkcg->lock);
Tejun Heo72e06c22012-03-05 13:15:00 -0800653 }
Tejun Heo9f13ef62012-03-05 13:15:21 -0800654
655 spin_unlock_irq(q->queue_lock);
Tejun Heo72e06c22012-03-05 13:15:00 -0800656}
Tejun Heo03aa2642012-03-05 13:15:19 -0800657EXPORT_SYMBOL_GPL(blkg_destroy_all);
Tejun Heo72e06c22012-03-05 13:15:00 -0800658
Tejun Heo1adaf3d2012-03-05 13:15:15 -0800659static void blkg_rcu_free(struct rcu_head *rcu_head)
660{
661 blkg_free(container_of(rcu_head, struct blkio_group, rcu_head));
662}
663
664void __blkg_release(struct blkio_group *blkg)
665{
666 /* release the extra blkcg reference this blkg has been holding */
667 css_put(&blkg->blkcg->css);
668
669 /*
670 * A group is freed in rcu manner. But having an rcu lock does not
671 * mean that one can access all the fields of blkg and assume these
672 * are valid. For example, don't try to follow throtl_data and
673 * request queue links.
674 *
675 * Having a reference to blkg under an rcu allows acess to only
676 * values local to groups like group stats and group rate limits
677 */
678 call_rcu(&blkg->rcu_head, blkg_rcu_free);
679}
680EXPORT_SYMBOL_GPL(__blkg_release);
681
Tejun Heoc1768262012-03-05 13:15:17 -0800682static void blkio_reset_stats_cpu(struct blkio_group *blkg, int plid)
Vivek Goyalf0bdc8c2011-05-19 15:38:30 -0400683{
Tejun Heoc1768262012-03-05 13:15:17 -0800684 struct blkg_policy_data *pd = blkg->pd[plid];
Tejun Heo997a0262012-03-08 10:53:58 -0800685 int cpu;
Vivek Goyal1cd9e032012-03-08 10:53:56 -0800686
687 if (pd->stats_cpu == NULL)
688 return;
Tejun Heo997a0262012-03-08 10:53:58 -0800689
690 for_each_possible_cpu(cpu) {
691 struct blkio_group_stats_cpu *sc =
692 per_cpu_ptr(pd->stats_cpu, cpu);
693
Tejun Heoedcb0722012-04-01 14:38:42 -0700694 blkg_rwstat_reset(&sc->service_bytes);
695 blkg_rwstat_reset(&sc->serviced);
696 blkg_stat_reset(&sc->sectors);
Vivek Goyalf0bdc8c2011-05-19 15:38:30 -0400697 }
698}
699
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700700static int
Divyesh Shah84c124d2010-04-09 08:31:19 +0200701blkiocg_reset_stats(struct cgroup *cgroup, struct cftype *cftype, u64 val)
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700702{
Tejun Heo997a0262012-03-08 10:53:58 -0800703 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgroup);
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700704 struct blkio_group *blkg;
705 struct hlist_node *n;
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700706
Tejun Heoe8989fa2012-03-05 13:15:20 -0800707 spin_lock(&blkio_list_lock);
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700708 spin_lock_irq(&blkcg->lock);
Tejun Heo997a0262012-03-08 10:53:58 -0800709
710 /*
711 * Note that stat reset is racy - it doesn't synchronize against
712 * stat updates. This is a debug feature which shouldn't exist
713 * anyway. If you get hit by a race, retry.
714 */
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700715 hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) {
Tejun Heoe8989fa2012-03-05 13:15:20 -0800716 struct blkio_policy_type *pol;
Tejun Heo549d3aa2012-03-05 13:15:16 -0800717
Tejun Heoe8989fa2012-03-05 13:15:20 -0800718 list_for_each_entry(pol, &blkio_list, list) {
719 struct blkg_policy_data *pd = blkg->pd[pol->plid];
Tejun Heo997a0262012-03-08 10:53:58 -0800720 struct blkio_group_stats *stats = &pd->stats;
Vivek Goyalf0bdc8c2011-05-19 15:38:30 -0400721
Tejun Heo997a0262012-03-08 10:53:58 -0800722 /* queued stats shouldn't be cleared */
Tejun Heoedcb0722012-04-01 14:38:42 -0700723 blkg_rwstat_reset(&stats->merged);
724 blkg_rwstat_reset(&stats->service_time);
725 blkg_rwstat_reset(&stats->wait_time);
726 blkg_stat_reset(&stats->time);
Tejun Heoe8989fa2012-03-05 13:15:20 -0800727#ifdef CONFIG_DEBUG_BLK_CGROUP
Tejun Heoedcb0722012-04-01 14:38:42 -0700728 blkg_stat_reset(&stats->unaccounted_time);
729 blkg_stat_reset(&stats->avg_queue_size_sum);
730 blkg_stat_reset(&stats->avg_queue_size_samples);
731 blkg_stat_reset(&stats->dequeue);
732 blkg_stat_reset(&stats->group_wait_time);
733 blkg_stat_reset(&stats->idle_time);
734 blkg_stat_reset(&stats->empty_time);
Tejun Heoe8989fa2012-03-05 13:15:20 -0800735#endif
Tejun Heoe8989fa2012-03-05 13:15:20 -0800736 blkio_reset_stats_cpu(blkg, pol->plid);
737 }
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700738 }
Vivek Goyalf0bdc8c2011-05-19 15:38:30 -0400739
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700740 spin_unlock_irq(&blkcg->lock);
Tejun Heoe8989fa2012-03-05 13:15:20 -0800741 spin_unlock(&blkio_list_lock);
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700742 return 0;
743}
744
Tejun Heod3d32e62012-04-01 14:38:42 -0700745static const char *blkg_dev_name(struct blkio_group *blkg)
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700746{
Tejun Heod3d32e62012-04-01 14:38:42 -0700747 /* some drivers (floppy) instantiate a queue w/o disk registered */
748 if (blkg->q->backing_dev_info.dev)
749 return dev_name(blkg->q->backing_dev_info.dev);
750 return NULL;
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700751}
752
Tejun Heod3d32e62012-04-01 14:38:42 -0700753/**
754 * blkcg_print_blkgs - helper for printing per-blkg data
755 * @sf: seq_file to print to
756 * @blkcg: blkcg of interest
757 * @prfill: fill function to print out a blkg
758 * @pol: policy in question
759 * @data: data to be passed to @prfill
760 * @show_total: to print out sum of prfill return values or not
761 *
762 * This function invokes @prfill on each blkg of @blkcg if pd for the
763 * policy specified by @pol exists. @prfill is invoked with @sf, the
764 * policy data and @data. If @show_total is %true, the sum of the return
765 * values from @prfill is printed with "Total" label at the end.
766 *
767 * This is to be used to construct print functions for
768 * cftype->read_seq_string method.
769 */
770static void blkcg_print_blkgs(struct seq_file *sf, struct blkio_cgroup *blkcg,
771 u64 (*prfill)(struct seq_file *,
772 struct blkg_policy_data *, int),
773 int pol, int data, bool show_total)
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400774{
Tejun Heod3d32e62012-04-01 14:38:42 -0700775 struct blkio_group *blkg;
776 struct hlist_node *n;
777 u64 total = 0;
778
779 spin_lock_irq(&blkcg->lock);
780 hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node)
781 if (blkg->pd[pol])
782 total += prfill(sf, blkg->pd[pol], data);
783 spin_unlock_irq(&blkcg->lock);
784
785 if (show_total)
786 seq_printf(sf, "Total %llu\n", (unsigned long long)total);
787}
788
789/**
790 * __blkg_prfill_u64 - prfill helper for a single u64 value
791 * @sf: seq_file to print to
792 * @pd: policy data of interest
793 * @v: value to print
794 *
795 * Print @v to @sf for the device assocaited with @pd.
796 */
797static u64 __blkg_prfill_u64(struct seq_file *sf, struct blkg_policy_data *pd,
798 u64 v)
799{
800 const char *dname = blkg_dev_name(pd->blkg);
801
802 if (!dname)
803 return 0;
804
805 seq_printf(sf, "%s %llu\n", dname, (unsigned long long)v);
806 return v;
807}
808
809/**
810 * __blkg_prfill_rwstat - prfill helper for a blkg_rwstat
811 * @sf: seq_file to print to
812 * @pd: policy data of interest
813 * @rwstat: rwstat to print
814 *
815 * Print @rwstat to @sf for the device assocaited with @pd.
816 */
817static u64 __blkg_prfill_rwstat(struct seq_file *sf,
818 struct blkg_policy_data *pd,
819 const struct blkg_rwstat *rwstat)
820{
821 static const char *rwstr[] = {
822 [BLKG_RWSTAT_READ] = "Read",
823 [BLKG_RWSTAT_WRITE] = "Write",
824 [BLKG_RWSTAT_SYNC] = "Sync",
825 [BLKG_RWSTAT_ASYNC] = "Async",
826 };
827 const char *dname = blkg_dev_name(pd->blkg);
828 u64 v;
829 int i;
830
831 if (!dname)
832 return 0;
833
834 for (i = 0; i < BLKG_RWSTAT_NR; i++)
835 seq_printf(sf, "%s %s %llu\n", dname, rwstr[i],
836 (unsigned long long)rwstat->cnt[i]);
837
838 v = rwstat->cnt[BLKG_RWSTAT_READ] + rwstat->cnt[BLKG_RWSTAT_WRITE];
839 seq_printf(sf, "%s Total %llu\n", dname, (unsigned long long)v);
840 return v;
841}
842
843static u64 blkg_prfill_stat(struct seq_file *sf, struct blkg_policy_data *pd,
844 int off)
845{
846 return __blkg_prfill_u64(sf, pd,
847 blkg_stat_read((void *)&pd->stats + off));
848}
849
850static u64 blkg_prfill_rwstat(struct seq_file *sf, struct blkg_policy_data *pd,
851 int off)
852{
853 struct blkg_rwstat rwstat = blkg_rwstat_read((void *)&pd->stats + off);
854
855 return __blkg_prfill_rwstat(sf, pd, &rwstat);
856}
857
858/* print blkg_stat specified by BLKCG_STAT_PRIV() */
859static int blkcg_print_stat(struct cgroup *cgrp, struct cftype *cft,
860 struct seq_file *sf)
861{
862 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgrp);
863
864 blkcg_print_blkgs(sf, blkcg, blkg_prfill_stat,
865 BLKCG_STAT_POL(cft->private),
866 BLKCG_STAT_OFF(cft->private), false);
867 return 0;
868}
869
870/* print blkg_rwstat specified by BLKCG_STAT_PRIV() */
871static int blkcg_print_rwstat(struct cgroup *cgrp, struct cftype *cft,
872 struct seq_file *sf)
873{
874 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgrp);
875
876 blkcg_print_blkgs(sf, blkcg, blkg_prfill_rwstat,
877 BLKCG_STAT_POL(cft->private),
878 BLKCG_STAT_OFF(cft->private), true);
879 return 0;
880}
881
882static u64 blkg_prfill_cpu_stat(struct seq_file *sf,
883 struct blkg_policy_data *pd, int off)
884{
885 u64 v = 0;
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400886 int cpu;
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400887
Tejun Heod3d32e62012-04-01 14:38:42 -0700888 for_each_possible_cpu(cpu) {
889 struct blkio_group_stats_cpu *sc =
890 per_cpu_ptr(pd->stats_cpu, cpu);
891
892 v += blkg_stat_read((void *)sc + off);
893 }
894
895 return __blkg_prfill_u64(sf, pd, v);
896}
897
898static u64 blkg_prfill_cpu_rwstat(struct seq_file *sf,
899 struct blkg_policy_data *pd, int off)
900{
901 struct blkg_rwstat rwstat = { }, tmp;
902 int i, cpu;
Vivek Goyal1cd9e032012-03-08 10:53:56 -0800903
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400904 for_each_possible_cpu(cpu) {
Tejun Heod3d32e62012-04-01 14:38:42 -0700905 struct blkio_group_stats_cpu *sc =
Tejun Heoedcb0722012-04-01 14:38:42 -0700906 per_cpu_ptr(pd->stats_cpu, cpu);
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400907
Tejun Heod3d32e62012-04-01 14:38:42 -0700908 tmp = blkg_rwstat_read((void *)sc + off);
909 for (i = 0; i < BLKG_RWSTAT_NR; i++)
910 rwstat.cnt[i] += tmp.cnt[i];
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400911 }
912
Tejun Heod3d32e62012-04-01 14:38:42 -0700913 return __blkg_prfill_rwstat(sf, pd, &rwstat);
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400914}
915
Tejun Heod3d32e62012-04-01 14:38:42 -0700916/* print per-cpu blkg_stat specified by BLKCG_STAT_PRIV() */
917static int blkcg_print_cpu_stat(struct cgroup *cgrp, struct cftype *cft,
918 struct seq_file *sf)
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400919{
Tejun Heod3d32e62012-04-01 14:38:42 -0700920 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgrp);
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400921
Tejun Heod3d32e62012-04-01 14:38:42 -0700922 blkcg_print_blkgs(sf, blkcg, blkg_prfill_cpu_stat,
923 BLKCG_STAT_POL(cft->private),
924 BLKCG_STAT_OFF(cft->private), false);
925 return 0;
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400926}
927
Tejun Heod3d32e62012-04-01 14:38:42 -0700928/* print per-cpu blkg_rwstat specified by BLKCG_STAT_PRIV() */
929static int blkcg_print_cpu_rwstat(struct cgroup *cgrp, struct cftype *cft,
930 struct seq_file *sf)
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700931{
Tejun Heod3d32e62012-04-01 14:38:42 -0700932 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgrp);
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700933
Tejun Heod3d32e62012-04-01 14:38:42 -0700934 blkcg_print_blkgs(sf, blkcg, blkg_prfill_cpu_rwstat,
935 BLKCG_STAT_POL(cft->private),
936 BLKCG_STAT_OFF(cft->private), true);
937 return 0;
938}
939
Justin TerAvest9026e522011-03-22 21:26:54 +0100940#ifdef CONFIG_DEBUG_BLK_CGROUP
Tejun Heod3d32e62012-04-01 14:38:42 -0700941static u64 blkg_prfill_avg_queue_size(struct seq_file *sf,
942 struct blkg_policy_data *pd, int off)
943{
944 u64 samples = blkg_stat_read(&pd->stats.avg_queue_size_samples);
945 u64 v = 0;
Divyesh Shah84c124d2010-04-09 08:31:19 +0200946
Tejun Heod3d32e62012-04-01 14:38:42 -0700947 if (samples) {
948 v = blkg_stat_read(&pd->stats.avg_queue_size_sum);
949 do_div(v, samples);
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700950 }
Tejun Heod3d32e62012-04-01 14:38:42 -0700951 __blkg_prfill_u64(sf, pd, v);
952 return 0;
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700953}
954
Tejun Heod3d32e62012-04-01 14:38:42 -0700955/* print avg_queue_size */
956static int blkcg_print_avg_queue_size(struct cgroup *cgrp, struct cftype *cft,
957 struct seq_file *sf)
958{
959 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgrp);
960
961 blkcg_print_blkgs(sf, blkcg, blkg_prfill_avg_queue_size,
962 BLKIO_POLICY_PROP, 0, false);
963 return 0;
964}
965#endif /* CONFIG_DEBUG_BLK_CGROUP */
966
Tejun Heo3a8b31d2012-04-01 14:38:43 -0700967struct blkg_conf_ctx {
968 struct gendisk *disk;
969 struct blkio_group *blkg;
970 u64 v;
971};
972
973/**
974 * blkg_conf_prep - parse and prepare for per-blkg config update
975 * @blkcg: target block cgroup
976 * @input: input string
977 * @ctx: blkg_conf_ctx to be filled
978 *
979 * Parse per-blkg config update from @input and initialize @ctx with the
980 * result. @ctx->blkg points to the blkg to be updated and @ctx->v the new
981 * value. This function returns with RCU read locked and must be paired
982 * with blkg_conf_finish().
983 */
984static int blkg_conf_prep(struct blkio_cgroup *blkcg, const char *input,
985 struct blkg_conf_ctx *ctx)
986 __acquires(rcu)
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800987{
Tejun Heo3a8b31d2012-04-01 14:38:43 -0700988 struct gendisk *disk;
989 struct blkio_group *blkg;
990 char *buf, *s[4], *p, *major_s, *minor_s;
Wanlong Gaod11bb442011-09-21 10:22:10 +0200991 unsigned long major, minor;
Tejun Heoece84242011-10-19 14:31:15 +0200992 int i = 0, ret = -EINVAL;
993 int part;
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800994 dev_t dev;
Wanlong Gaod11bb442011-09-21 10:22:10 +0200995 u64 temp;
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800996
Tejun Heo3a8b31d2012-04-01 14:38:43 -0700997 buf = kstrdup(input, GFP_KERNEL);
998 if (!buf)
999 return -ENOMEM;
1000
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001001 memset(s, 0, sizeof(s));
1002
1003 while ((p = strsep(&buf, " ")) != NULL) {
1004 if (!*p)
1005 continue;
1006
1007 s[i++] = p;
1008
1009 /* Prevent from inputing too many things */
1010 if (i == 3)
1011 break;
1012 }
1013
1014 if (i != 2)
Tejun Heoece84242011-10-19 14:31:15 +02001015 goto out;
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001016
1017 p = strsep(&s[0], ":");
1018 if (p != NULL)
1019 major_s = p;
1020 else
Tejun Heoece84242011-10-19 14:31:15 +02001021 goto out;
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001022
1023 minor_s = s[0];
1024 if (!minor_s)
Tejun Heoece84242011-10-19 14:31:15 +02001025 goto out;
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001026
Tejun Heoece84242011-10-19 14:31:15 +02001027 if (strict_strtoul(major_s, 10, &major))
1028 goto out;
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001029
Tejun Heoece84242011-10-19 14:31:15 +02001030 if (strict_strtoul(minor_s, 10, &minor))
1031 goto out;
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001032
1033 dev = MKDEV(major, minor);
1034
Tejun Heoece84242011-10-19 14:31:15 +02001035 if (strict_strtoull(s[1], 10, &temp))
1036 goto out;
Wanlong Gaod11bb442011-09-21 10:22:10 +02001037
Tejun Heoe56da7e2012-03-05 13:15:07 -08001038 disk = get_gendisk(dev, &part);
Tejun Heo4bfd4822012-03-05 13:15:08 -08001039 if (!disk || part)
Tejun Heoe56da7e2012-03-05 13:15:07 -08001040 goto out;
Tejun Heoe56da7e2012-03-05 13:15:07 -08001041
1042 rcu_read_lock();
1043
Tejun Heo4bfd4822012-03-05 13:15:08 -08001044 spin_lock_irq(disk->queue->queue_lock);
Tejun Heoaaec55a2012-04-01 14:38:42 -07001045 blkg = blkg_lookup_create(blkcg, disk->queue, false);
Tejun Heo4bfd4822012-03-05 13:15:08 -08001046 spin_unlock_irq(disk->queue->queue_lock);
Tejun Heoe56da7e2012-03-05 13:15:07 -08001047
Tejun Heo4bfd4822012-03-05 13:15:08 -08001048 if (IS_ERR(blkg)) {
1049 ret = PTR_ERR(blkg);
Tejun Heo3a8b31d2012-04-01 14:38:43 -07001050 rcu_read_unlock();
1051 put_disk(disk);
1052 /*
1053 * If queue was bypassing, we should retry. Do so after a
1054 * short msleep(). It isn't strictly necessary but queue
1055 * can be bypassing for some time and it's always nice to
1056 * avoid busy looping.
1057 */
1058 if (ret == -EBUSY) {
1059 msleep(10);
1060 ret = restart_syscall();
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001061 }
Tejun Heo3a8b31d2012-04-01 14:38:43 -07001062 goto out;
Vivek Goyal062a6442010-09-15 17:06:33 -04001063 }
Tejun Heoe56da7e2012-03-05 13:15:07 -08001064
Tejun Heo3a8b31d2012-04-01 14:38:43 -07001065 ctx->disk = disk;
1066 ctx->blkg = blkg;
1067 ctx->v = temp;
1068 ret = 0;
1069out:
1070 kfree(buf);
Tejun Heoece84242011-10-19 14:31:15 +02001071 return ret;
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001072}
1073
Tejun Heo3a8b31d2012-04-01 14:38:43 -07001074/**
1075 * blkg_conf_finish - finish up per-blkg config update
1076 * @ctx: blkg_conf_ctx intiailized by blkg_conf_prep()
1077 *
1078 * Finish up after per-blkg config update. This function must be paired
1079 * with blkg_conf_prep().
1080 */
1081static void blkg_conf_finish(struct blkg_conf_ctx *ctx)
1082 __releases(rcu)
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001083{
Tejun Heo3a8b31d2012-04-01 14:38:43 -07001084 rcu_read_unlock();
1085 put_disk(ctx->disk);
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001086}
1087
Tejun Heoc4682ae2012-04-01 14:38:43 -07001088/* for propio conf */
1089static u64 blkg_prfill_weight_device(struct seq_file *sf,
1090 struct blkg_policy_data *pd, int off)
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001091{
Tejun Heoc4682ae2012-04-01 14:38:43 -07001092 if (!pd->conf.weight)
1093 return 0;
1094 return __blkg_prfill_u64(sf, pd, pd->conf.weight);
Vivek Goyal062a6442010-09-15 17:06:33 -04001095}
1096
Tejun Heoc4682ae2012-04-01 14:38:43 -07001097static int blkcg_print_weight_device(struct cgroup *cgrp, struct cftype *cft,
1098 struct seq_file *sf)
Vivek Goyal062a6442010-09-15 17:06:33 -04001099{
Tejun Heoc4682ae2012-04-01 14:38:43 -07001100 blkcg_print_blkgs(sf, cgroup_to_blkio_cgroup(cgrp),
1101 blkg_prfill_weight_device, BLKIO_POLICY_PROP, 0,
1102 false);
1103 return 0;
Vivek Goyal062a6442010-09-15 17:06:33 -04001104}
1105
Tejun Heoc4682ae2012-04-01 14:38:43 -07001106static int blkcg_print_weight(struct cgroup *cgrp, struct cftype *cft,
1107 struct seq_file *sf)
Vivek Goyal062a6442010-09-15 17:06:33 -04001108{
Tejun Heoc4682ae2012-04-01 14:38:43 -07001109 seq_printf(sf, "%u\n", cgroup_to_blkio_cgroup(cgrp)->weight);
Vivek Goyal062a6442010-09-15 17:06:33 -04001110 return 0;
1111}
1112
Tejun Heo3a8b31d2012-04-01 14:38:43 -07001113static int blkcg_set_weight_device(struct cgroup *cgrp, struct cftype *cft,
1114 const char *buf)
1115{
1116 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgrp);
1117 struct blkg_policy_data *pd;
1118 struct blkg_conf_ctx ctx;
1119 int ret;
1120
1121 ret = blkg_conf_prep(blkcg, buf, &ctx);
1122 if (ret)
1123 return ret;
1124
1125 ret = -EINVAL;
1126 pd = ctx.blkg->pd[BLKIO_POLICY_PROP];
1127 if (pd && (!ctx.v || (ctx.v >= BLKIO_WEIGHT_MIN &&
1128 ctx.v <= BLKIO_WEIGHT_MAX))) {
1129 pd->conf.weight = ctx.v;
1130 blkio_update_group_weight(ctx.blkg, BLKIO_POLICY_PROP,
1131 ctx.v ?: blkcg->weight);
1132 ret = 0;
1133 }
1134
1135 blkg_conf_finish(&ctx);
1136 return ret;
1137}
1138
Tejun Heo627f29f2012-04-01 14:38:43 -07001139static int blkcg_set_weight(struct cgroup *cgrp, struct cftype *cft, u64 val)
Vivek Goyal062a6442010-09-15 17:06:33 -04001140{
Tejun Heo627f29f2012-04-01 14:38:43 -07001141 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgrp);
Vivek Goyal062a6442010-09-15 17:06:33 -04001142 struct blkio_group *blkg;
1143 struct hlist_node *n;
Vivek Goyal062a6442010-09-15 17:06:33 -04001144
1145 if (val < BLKIO_WEIGHT_MIN || val > BLKIO_WEIGHT_MAX)
1146 return -EINVAL;
1147
1148 spin_lock(&blkio_list_lock);
1149 spin_lock_irq(&blkcg->lock);
1150 blkcg->weight = (unsigned int)val;
1151
Tejun Heo549d3aa2012-03-05 13:15:16 -08001152 hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) {
Tejun Heo627f29f2012-04-01 14:38:43 -07001153 struct blkg_policy_data *pd = blkg->pd[BLKIO_POLICY_PROP];
Tejun Heo549d3aa2012-03-05 13:15:16 -08001154
Tejun Heo627f29f2012-04-01 14:38:43 -07001155 if (pd && !pd->conf.weight)
1156 blkio_update_group_weight(blkg, BLKIO_POLICY_PROP,
1157 blkcg->weight);
Tejun Heo549d3aa2012-03-05 13:15:16 -08001158 }
Vivek Goyal062a6442010-09-15 17:06:33 -04001159
Vivek Goyal062a6442010-09-15 17:06:33 -04001160 spin_unlock_irq(&blkcg->lock);
1161 spin_unlock(&blkio_list_lock);
1162 return 0;
1163}
1164
Tejun Heoc4682ae2012-04-01 14:38:43 -07001165/* for blk-throttle conf */
1166#ifdef CONFIG_BLK_DEV_THROTTLING
1167static u64 blkg_prfill_conf_u64(struct seq_file *sf,
1168 struct blkg_policy_data *pd, int off)
1169{
1170 u64 v = *(u64 *)((void *)&pd->conf + off);
Vivek Goyal062a6442010-09-15 17:06:33 -04001171
Tejun Heoc4682ae2012-04-01 14:38:43 -07001172 if (!v)
1173 return 0;
1174 return __blkg_prfill_u64(sf, pd, v);
1175}
Vivek Goyal062a6442010-09-15 17:06:33 -04001176
Tejun Heoc4682ae2012-04-01 14:38:43 -07001177static int blkcg_print_conf_u64(struct cgroup *cgrp, struct cftype *cft,
1178 struct seq_file *sf)
1179{
Tejun Heoc4682ae2012-04-01 14:38:43 -07001180 blkcg_print_blkgs(sf, cgroup_to_blkio_cgroup(cgrp),
1181 blkg_prfill_conf_u64, BLKIO_POLICY_THROTL,
Tejun Heo3a8b31d2012-04-01 14:38:43 -07001182 cft->private, false);
Vivek Goyal062a6442010-09-15 17:06:33 -04001183 return 0;
1184}
Tejun Heo3a8b31d2012-04-01 14:38:43 -07001185
1186static int blkcg_set_conf_u64(struct cgroup *cgrp, struct cftype *cft,
1187 const char *buf, int rw,
1188 void (*update)(struct blkio_group *, int, u64, int))
1189{
1190 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgrp);
1191 struct blkg_policy_data *pd;
1192 struct blkg_conf_ctx ctx;
1193 int ret;
1194
1195 ret = blkg_conf_prep(blkcg, buf, &ctx);
1196 if (ret)
1197 return ret;
1198
1199 ret = -EINVAL;
1200 pd = ctx.blkg->pd[BLKIO_POLICY_THROTL];
1201 if (pd) {
1202 *(u64 *)((void *)&pd->conf + cft->private) = ctx.v;
1203 update(ctx.blkg, BLKIO_POLICY_THROTL, ctx.v ?: -1, rw);
1204 ret = 0;
1205 }
1206
1207 blkg_conf_finish(&ctx);
1208 return ret;
1209}
1210
1211static int blkcg_set_conf_bps_r(struct cgroup *cgrp, struct cftype *cft,
1212 const char *buf)
1213{
1214 return blkcg_set_conf_u64(cgrp, cft, buf, READ, blkio_update_group_bps);
1215}
1216
1217static int blkcg_set_conf_bps_w(struct cgroup *cgrp, struct cftype *cft,
1218 const char *buf)
1219{
1220 return blkcg_set_conf_u64(cgrp, cft, buf, WRITE, blkio_update_group_bps);
1221}
1222
1223static int blkcg_set_conf_iops_r(struct cgroup *cgrp, struct cftype *cft,
1224 const char *buf)
1225{
1226 return blkcg_set_conf_u64(cgrp, cft, buf, READ, blkio_update_group_iops);
1227}
1228
1229static int blkcg_set_conf_iops_w(struct cgroup *cgrp, struct cftype *cft,
1230 const char *buf)
1231{
1232 return blkcg_set_conf_u64(cgrp, cft, buf, WRITE, blkio_update_group_iops);
1233}
Tejun Heoc4682ae2012-04-01 14:38:43 -07001234#endif
Vivek Goyal062a6442010-09-15 17:06:33 -04001235
Vivek Goyal31e4c282009-12-03 12:59:42 -05001236struct cftype blkio_files[] = {
1237 {
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001238 .name = "weight_device",
Tejun Heoc4682ae2012-04-01 14:38:43 -07001239 .read_seq_string = blkcg_print_weight_device,
Tejun Heo3a8b31d2012-04-01 14:38:43 -07001240 .write_string = blkcg_set_weight_device,
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001241 .max_write_len = 256,
1242 },
1243 {
Vivek Goyal31e4c282009-12-03 12:59:42 -05001244 .name = "weight",
Tejun Heoc4682ae2012-04-01 14:38:43 -07001245 .read_seq_string = blkcg_print_weight,
Tejun Heo627f29f2012-04-01 14:38:43 -07001246 .write_u64 = blkcg_set_weight,
Vivek Goyal31e4c282009-12-03 12:59:42 -05001247 },
Vivek Goyal22084192009-12-03 12:59:49 -05001248 {
1249 .name = "time",
Tejun Heod3d32e62012-04-01 14:38:42 -07001250 .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP,
1251 offsetof(struct blkio_group_stats, time)),
1252 .read_seq_string = blkcg_print_stat,
Vivek Goyal22084192009-12-03 12:59:49 -05001253 },
1254 {
1255 .name = "sectors",
Tejun Heod3d32e62012-04-01 14:38:42 -07001256 .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP,
1257 offsetof(struct blkio_group_stats_cpu, sectors)),
1258 .read_seq_string = blkcg_print_cpu_stat,
Divyesh Shah303a3ac2010-04-01 15:01:24 -07001259 },
1260 {
1261 .name = "io_service_bytes",
Tejun Heod3d32e62012-04-01 14:38:42 -07001262 .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP,
1263 offsetof(struct blkio_group_stats_cpu, service_bytes)),
1264 .read_seq_string = blkcg_print_cpu_rwstat,
Divyesh Shah303a3ac2010-04-01 15:01:24 -07001265 },
1266 {
1267 .name = "io_serviced",
Tejun Heod3d32e62012-04-01 14:38:42 -07001268 .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP,
1269 offsetof(struct blkio_group_stats_cpu, serviced)),
1270 .read_seq_string = blkcg_print_cpu_rwstat,
Divyesh Shah303a3ac2010-04-01 15:01:24 -07001271 },
1272 {
1273 .name = "io_service_time",
Tejun Heod3d32e62012-04-01 14:38:42 -07001274 .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP,
1275 offsetof(struct blkio_group_stats, service_time)),
1276 .read_seq_string = blkcg_print_rwstat,
Divyesh Shah303a3ac2010-04-01 15:01:24 -07001277 },
1278 {
1279 .name = "io_wait_time",
Tejun Heod3d32e62012-04-01 14:38:42 -07001280 .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP,
1281 offsetof(struct blkio_group_stats, wait_time)),
1282 .read_seq_string = blkcg_print_rwstat,
Divyesh Shah84c124d2010-04-09 08:31:19 +02001283 },
1284 {
Divyesh Shah812d4022010-04-08 21:14:23 -07001285 .name = "io_merged",
Tejun Heod3d32e62012-04-01 14:38:42 -07001286 .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP,
1287 offsetof(struct blkio_group_stats, merged)),
1288 .read_seq_string = blkcg_print_rwstat,
Divyesh Shah812d4022010-04-08 21:14:23 -07001289 },
1290 {
Divyesh Shahcdc11842010-04-08 21:15:10 -07001291 .name = "io_queued",
Tejun Heod3d32e62012-04-01 14:38:42 -07001292 .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP,
1293 offsetof(struct blkio_group_stats, queued)),
1294 .read_seq_string = blkcg_print_rwstat,
Divyesh Shahcdc11842010-04-08 21:15:10 -07001295 },
1296 {
Divyesh Shah84c124d2010-04-09 08:31:19 +02001297 .name = "reset_stats",
1298 .write_u64 = blkiocg_reset_stats,
Vivek Goyal22084192009-12-03 12:59:49 -05001299 },
Vivek Goyal13f98252010-10-01 14:49:41 +02001300#ifdef CONFIG_BLK_DEV_THROTTLING
1301 {
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001302 .name = "throttle.read_bps_device",
Tejun Heo3a8b31d2012-04-01 14:38:43 -07001303 .private = offsetof(struct blkio_group_conf, bps[READ]),
Tejun Heoc4682ae2012-04-01 14:38:43 -07001304 .read_seq_string = blkcg_print_conf_u64,
Tejun Heo3a8b31d2012-04-01 14:38:43 -07001305 .write_string = blkcg_set_conf_bps_r,
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001306 .max_write_len = 256,
1307 },
1308
1309 {
1310 .name = "throttle.write_bps_device",
Tejun Heo3a8b31d2012-04-01 14:38:43 -07001311 .private = offsetof(struct blkio_group_conf, bps[WRITE]),
Tejun Heoc4682ae2012-04-01 14:38:43 -07001312 .read_seq_string = blkcg_print_conf_u64,
Tejun Heo3a8b31d2012-04-01 14:38:43 -07001313 .write_string = blkcg_set_conf_bps_w,
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001314 .max_write_len = 256,
1315 },
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001316
1317 {
1318 .name = "throttle.read_iops_device",
Tejun Heo3a8b31d2012-04-01 14:38:43 -07001319 .private = offsetof(struct blkio_group_conf, iops[READ]),
Tejun Heoc4682ae2012-04-01 14:38:43 -07001320 .read_seq_string = blkcg_print_conf_u64,
Tejun Heo3a8b31d2012-04-01 14:38:43 -07001321 .write_string = blkcg_set_conf_iops_r,
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001322 .max_write_len = 256,
1323 },
1324
1325 {
1326 .name = "throttle.write_iops_device",
Tejun Heo3a8b31d2012-04-01 14:38:43 -07001327 .private = offsetof(struct blkio_group_conf, iops[WRITE]),
Tejun Heoc4682ae2012-04-01 14:38:43 -07001328 .read_seq_string = blkcg_print_conf_u64,
Tejun Heo3a8b31d2012-04-01 14:38:43 -07001329 .write_string = blkcg_set_conf_iops_w,
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001330 .max_write_len = 256,
1331 },
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001332 {
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001333 .name = "throttle.io_service_bytes",
Tejun Heod3d32e62012-04-01 14:38:42 -07001334 .private = BLKCG_STAT_PRIV(BLKIO_POLICY_THROTL,
1335 offsetof(struct blkio_group_stats_cpu, service_bytes)),
1336 .read_seq_string = blkcg_print_cpu_rwstat,
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001337 },
1338 {
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001339 .name = "throttle.io_serviced",
Tejun Heod3d32e62012-04-01 14:38:42 -07001340 .private = BLKCG_STAT_PRIV(BLKIO_POLICY_THROTL,
1341 offsetof(struct blkio_group_stats_cpu, serviced)),
1342 .read_seq_string = blkcg_print_cpu_rwstat,
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001343 },
Vivek Goyal13f98252010-10-01 14:49:41 +02001344#endif /* CONFIG_BLK_DEV_THROTTLING */
1345
Vivek Goyal22084192009-12-03 12:59:49 -05001346#ifdef CONFIG_DEBUG_BLK_CGROUP
Divyesh Shahcdc11842010-04-08 21:15:10 -07001347 {
1348 .name = "avg_queue_size",
Tejun Heod3d32e62012-04-01 14:38:42 -07001349 .read_seq_string = blkcg_print_avg_queue_size,
Divyesh Shahcdc11842010-04-08 21:15:10 -07001350 },
1351 {
Divyesh Shah812df482010-04-08 21:15:35 -07001352 .name = "group_wait_time",
Tejun Heod3d32e62012-04-01 14:38:42 -07001353 .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP,
1354 offsetof(struct blkio_group_stats, group_wait_time)),
1355 .read_seq_string = blkcg_print_stat,
Divyesh Shah812df482010-04-08 21:15:35 -07001356 },
1357 {
1358 .name = "idle_time",
Tejun Heod3d32e62012-04-01 14:38:42 -07001359 .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP,
1360 offsetof(struct blkio_group_stats, idle_time)),
1361 .read_seq_string = blkcg_print_stat,
Divyesh Shah812df482010-04-08 21:15:35 -07001362 },
1363 {
1364 .name = "empty_time",
Tejun Heod3d32e62012-04-01 14:38:42 -07001365 .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP,
1366 offsetof(struct blkio_group_stats, empty_time)),
1367 .read_seq_string = blkcg_print_stat,
Divyesh Shah812df482010-04-08 21:15:35 -07001368 },
1369 {
Vivek Goyal22084192009-12-03 12:59:49 -05001370 .name = "dequeue",
Tejun Heod3d32e62012-04-01 14:38:42 -07001371 .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP,
1372 offsetof(struct blkio_group_stats, dequeue)),
1373 .read_seq_string = blkcg_print_stat,
Divyesh Shahcdc11842010-04-08 21:15:10 -07001374 },
Justin TerAvest9026e522011-03-22 21:26:54 +01001375 {
1376 .name = "unaccounted_time",
Tejun Heod3d32e62012-04-01 14:38:42 -07001377 .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP,
1378 offsetof(struct blkio_group_stats, unaccounted_time)),
1379 .read_seq_string = blkcg_print_stat,
Justin TerAvest9026e522011-03-22 21:26:54 +01001380 },
Vivek Goyal22084192009-12-03 12:59:49 -05001381#endif
Tejun Heo4baf6e32012-04-01 12:09:55 -07001382 { } /* terminate */
Vivek Goyal31e4c282009-12-03 12:59:42 -05001383};
1384
Tejun Heo9f13ef62012-03-05 13:15:21 -08001385/**
1386 * blkiocg_pre_destroy - cgroup pre_destroy callback
Tejun Heo9f13ef62012-03-05 13:15:21 -08001387 * @cgroup: cgroup of interest
1388 *
1389 * This function is called when @cgroup is about to go away and responsible
1390 * for shooting down all blkgs associated with @cgroup. blkgs should be
1391 * removed while holding both q and blkcg locks. As blkcg lock is nested
1392 * inside q lock, this function performs reverse double lock dancing.
1393 *
1394 * This is the blkcg counterpart of ioc_release_fn().
1395 */
Tejun Heo959d8512012-04-01 12:30:01 -07001396static int blkiocg_pre_destroy(struct cgroup *cgroup)
Vivek Goyal31e4c282009-12-03 12:59:42 -05001397{
1398 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgroup);
1399
Tejun Heo9f13ef62012-03-05 13:15:21 -08001400 spin_lock_irq(&blkcg->lock);
Tejun Heo7ee9c562012-03-05 13:15:11 -08001401
Tejun Heo9f13ef62012-03-05 13:15:21 -08001402 while (!hlist_empty(&blkcg->blkg_list)) {
1403 struct blkio_group *blkg = hlist_entry(blkcg->blkg_list.first,
1404 struct blkio_group, blkcg_node);
Tejun Heoc875f4d2012-03-05 13:15:22 -08001405 struct request_queue *q = blkg->q;
Vivek Goyalb1c35762009-12-03 12:59:47 -05001406
Tejun Heo9f13ef62012-03-05 13:15:21 -08001407 if (spin_trylock(q->queue_lock)) {
1408 blkg_destroy(blkg);
1409 spin_unlock(q->queue_lock);
1410 } else {
1411 spin_unlock_irq(&blkcg->lock);
Tejun Heo9f13ef62012-03-05 13:15:21 -08001412 cpu_relax();
Dan Carpentera5567932012-03-29 20:57:08 +02001413 spin_lock_irq(&blkcg->lock);
Jens Axboe0f3942a2010-05-03 14:28:55 +02001414 }
Tejun Heo9f13ef62012-03-05 13:15:21 -08001415 }
Jens Axboe0f3942a2010-05-03 14:28:55 +02001416
Tejun Heo9f13ef62012-03-05 13:15:21 -08001417 spin_unlock_irq(&blkcg->lock);
Tejun Heo7ee9c562012-03-05 13:15:11 -08001418 return 0;
1419}
1420
Li Zefan761b3ef2012-01-31 13:47:36 +08001421static void blkiocg_destroy(struct cgroup *cgroup)
Tejun Heo7ee9c562012-03-05 13:15:11 -08001422{
1423 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgroup);
1424
Ben Blum67523c42010-03-10 15:22:11 -08001425 if (blkcg != &blkio_root_cgroup)
1426 kfree(blkcg);
Vivek Goyal31e4c282009-12-03 12:59:42 -05001427}
1428
Li Zefan761b3ef2012-01-31 13:47:36 +08001429static struct cgroup_subsys_state *blkiocg_create(struct cgroup *cgroup)
Vivek Goyal31e4c282009-12-03 12:59:42 -05001430{
Tejun Heo9a9e8a22012-03-19 15:10:56 -07001431 static atomic64_t id_seq = ATOMIC64_INIT(0);
Li Zefan03415092010-05-07 08:57:00 +02001432 struct blkio_cgroup *blkcg;
1433 struct cgroup *parent = cgroup->parent;
Vivek Goyal31e4c282009-12-03 12:59:42 -05001434
Li Zefan03415092010-05-07 08:57:00 +02001435 if (!parent) {
Vivek Goyal31e4c282009-12-03 12:59:42 -05001436 blkcg = &blkio_root_cgroup;
1437 goto done;
1438 }
1439
Vivek Goyal31e4c282009-12-03 12:59:42 -05001440 blkcg = kzalloc(sizeof(*blkcg), GFP_KERNEL);
1441 if (!blkcg)
1442 return ERR_PTR(-ENOMEM);
1443
1444 blkcg->weight = BLKIO_WEIGHT_DEFAULT;
Tejun Heo9a9e8a22012-03-19 15:10:56 -07001445 blkcg->id = atomic64_inc_return(&id_seq); /* root is 0, start from 1 */
Vivek Goyal31e4c282009-12-03 12:59:42 -05001446done:
1447 spin_lock_init(&blkcg->lock);
1448 INIT_HLIST_HEAD(&blkcg->blkg_list);
1449
1450 return &blkcg->css;
1451}
1452
Tejun Heo5efd6112012-03-05 13:15:12 -08001453/**
1454 * blkcg_init_queue - initialize blkcg part of request queue
1455 * @q: request_queue to initialize
1456 *
1457 * Called from blk_alloc_queue_node(). Responsible for initializing blkcg
1458 * part of new request_queue @q.
1459 *
1460 * RETURNS:
1461 * 0 on success, -errno on failure.
1462 */
1463int blkcg_init_queue(struct request_queue *q)
1464{
Tejun Heo923adde2012-03-05 13:15:13 -08001465 int ret;
1466
Tejun Heo5efd6112012-03-05 13:15:12 -08001467 might_sleep();
1468
Tejun Heo923adde2012-03-05 13:15:13 -08001469 ret = blk_throtl_init(q);
1470 if (ret)
1471 return ret;
1472
1473 mutex_lock(&all_q_mutex);
1474 INIT_LIST_HEAD(&q->all_q_node);
1475 list_add_tail(&q->all_q_node, &all_q_list);
1476 mutex_unlock(&all_q_mutex);
1477
1478 return 0;
Tejun Heo5efd6112012-03-05 13:15:12 -08001479}
1480
1481/**
1482 * blkcg_drain_queue - drain blkcg part of request_queue
1483 * @q: request_queue to drain
1484 *
1485 * Called from blk_drain_queue(). Responsible for draining blkcg part.
1486 */
1487void blkcg_drain_queue(struct request_queue *q)
1488{
1489 lockdep_assert_held(q->queue_lock);
1490
1491 blk_throtl_drain(q);
1492}
1493
1494/**
1495 * blkcg_exit_queue - exit and release blkcg part of request_queue
1496 * @q: request_queue being released
1497 *
1498 * Called from blk_release_queue(). Responsible for exiting blkcg part.
1499 */
1500void blkcg_exit_queue(struct request_queue *q)
1501{
Tejun Heo923adde2012-03-05 13:15:13 -08001502 mutex_lock(&all_q_mutex);
1503 list_del_init(&q->all_q_node);
1504 mutex_unlock(&all_q_mutex);
1505
Tejun Heoe8989fa2012-03-05 13:15:20 -08001506 blkg_destroy_all(q, true);
1507
Tejun Heo5efd6112012-03-05 13:15:12 -08001508 blk_throtl_exit(q);
1509}
1510
Vivek Goyal31e4c282009-12-03 12:59:42 -05001511/*
1512 * We cannot support shared io contexts, as we have no mean to support
1513 * two tasks with the same ioc in two different groups without major rework
1514 * of the main cic data structures. For now we allow a task to change
1515 * its cgroup only if it's the only owner of its ioc.
1516 */
Li Zefan761b3ef2012-01-31 13:47:36 +08001517static int blkiocg_can_attach(struct cgroup *cgrp, struct cgroup_taskset *tset)
Vivek Goyal31e4c282009-12-03 12:59:42 -05001518{
Tejun Heobb9d97b2011-12-12 18:12:21 -08001519 struct task_struct *task;
Vivek Goyal31e4c282009-12-03 12:59:42 -05001520 struct io_context *ioc;
1521 int ret = 0;
1522
1523 /* task_lock() is needed to avoid races with exit_io_context() */
Tejun Heobb9d97b2011-12-12 18:12:21 -08001524 cgroup_taskset_for_each(task, cgrp, tset) {
1525 task_lock(task);
1526 ioc = task->io_context;
1527 if (ioc && atomic_read(&ioc->nr_tasks) > 1)
1528 ret = -EINVAL;
1529 task_unlock(task);
1530 if (ret)
1531 break;
1532 }
Vivek Goyal31e4c282009-12-03 12:59:42 -05001533 return ret;
1534}
1535
Tejun Heo923adde2012-03-05 13:15:13 -08001536static void blkcg_bypass_start(void)
1537 __acquires(&all_q_mutex)
1538{
1539 struct request_queue *q;
1540
1541 mutex_lock(&all_q_mutex);
1542
1543 list_for_each_entry(q, &all_q_list, all_q_node) {
1544 blk_queue_bypass_start(q);
Tejun Heoe8989fa2012-03-05 13:15:20 -08001545 blkg_destroy_all(q, false);
Tejun Heo923adde2012-03-05 13:15:13 -08001546 }
1547}
1548
1549static void blkcg_bypass_end(void)
1550 __releases(&all_q_mutex)
1551{
1552 struct request_queue *q;
1553
1554 list_for_each_entry(q, &all_q_list, all_q_node)
1555 blk_queue_bypass_end(q);
1556
1557 mutex_unlock(&all_q_mutex);
1558}
1559
Tejun Heo676f7c82012-04-01 12:09:55 -07001560struct cgroup_subsys blkio_subsys = {
1561 .name = "blkio",
1562 .create = blkiocg_create,
1563 .can_attach = blkiocg_can_attach,
Tejun Heo959d8512012-04-01 12:30:01 -07001564 .pre_destroy = blkiocg_pre_destroy,
Tejun Heo676f7c82012-04-01 12:09:55 -07001565 .destroy = blkiocg_destroy,
Tejun Heo676f7c82012-04-01 12:09:55 -07001566 .subsys_id = blkio_subsys_id,
Tejun Heo4baf6e32012-04-01 12:09:55 -07001567 .base_cftypes = blkio_files,
Tejun Heo676f7c82012-04-01 12:09:55 -07001568 .module = THIS_MODULE,
1569};
1570EXPORT_SYMBOL_GPL(blkio_subsys);
1571
Vivek Goyal3e252062009-12-04 10:36:42 -05001572void blkio_policy_register(struct blkio_policy_type *blkiop)
1573{
Tejun Heoe8989fa2012-03-05 13:15:20 -08001574 struct request_queue *q;
1575
Tejun Heo923adde2012-03-05 13:15:13 -08001576 blkcg_bypass_start();
Vivek Goyal3e252062009-12-04 10:36:42 -05001577 spin_lock(&blkio_list_lock);
Tejun Heo035d10b2012-03-05 13:15:04 -08001578
1579 BUG_ON(blkio_policy[blkiop->plid]);
1580 blkio_policy[blkiop->plid] = blkiop;
Vivek Goyal3e252062009-12-04 10:36:42 -05001581 list_add_tail(&blkiop->list, &blkio_list);
Tejun Heo035d10b2012-03-05 13:15:04 -08001582
Vivek Goyal3e252062009-12-04 10:36:42 -05001583 spin_unlock(&blkio_list_lock);
Tejun Heoe8989fa2012-03-05 13:15:20 -08001584 list_for_each_entry(q, &all_q_list, all_q_node)
1585 update_root_blkg_pd(q, blkiop->plid);
Tejun Heo923adde2012-03-05 13:15:13 -08001586 blkcg_bypass_end();
Vivek Goyal3e252062009-12-04 10:36:42 -05001587}
1588EXPORT_SYMBOL_GPL(blkio_policy_register);
1589
1590void blkio_policy_unregister(struct blkio_policy_type *blkiop)
1591{
Tejun Heoe8989fa2012-03-05 13:15:20 -08001592 struct request_queue *q;
1593
Tejun Heo923adde2012-03-05 13:15:13 -08001594 blkcg_bypass_start();
Vivek Goyal3e252062009-12-04 10:36:42 -05001595 spin_lock(&blkio_list_lock);
Tejun Heo035d10b2012-03-05 13:15:04 -08001596
1597 BUG_ON(blkio_policy[blkiop->plid] != blkiop);
1598 blkio_policy[blkiop->plid] = NULL;
Vivek Goyal3e252062009-12-04 10:36:42 -05001599 list_del_init(&blkiop->list);
Tejun Heo035d10b2012-03-05 13:15:04 -08001600
Vivek Goyal3e252062009-12-04 10:36:42 -05001601 spin_unlock(&blkio_list_lock);
Tejun Heoe8989fa2012-03-05 13:15:20 -08001602 list_for_each_entry(q, &all_q_list, all_q_node)
1603 update_root_blkg_pd(q, blkiop->plid);
Tejun Heo923adde2012-03-05 13:15:13 -08001604 blkcg_bypass_end();
Vivek Goyal3e252062009-12-04 10:36:42 -05001605}
1606EXPORT_SYMBOL_GPL(blkio_policy_unregister);