blob: b07a501839e7be140a32ce599947a45611facede [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;
Tejun Heo726fa692012-04-01 14:38:43 -0700990 unsigned int major, minor;
991 unsigned long long v;
992 int part, ret;
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800993
Tejun Heo726fa692012-04-01 14:38:43 -0700994 if (sscanf(input, "%u:%u %llu", &major, &minor, &v) != 3)
995 return -EINVAL;
Tejun Heo3a8b31d2012-04-01 14:38:43 -0700996
Tejun Heo726fa692012-04-01 14:38:43 -0700997 disk = get_gendisk(MKDEV(major, minor), &part);
Tejun Heo4bfd4822012-03-05 13:15:08 -0800998 if (!disk || part)
Tejun Heo726fa692012-04-01 14:38:43 -0700999 return -EINVAL;
Tejun Heoe56da7e2012-03-05 13:15:07 -08001000
1001 rcu_read_lock();
1002
Tejun Heo4bfd4822012-03-05 13:15:08 -08001003 spin_lock_irq(disk->queue->queue_lock);
Tejun Heoaaec55a2012-04-01 14:38:42 -07001004 blkg = blkg_lookup_create(blkcg, disk->queue, false);
Tejun Heo4bfd4822012-03-05 13:15:08 -08001005 spin_unlock_irq(disk->queue->queue_lock);
Tejun Heoe56da7e2012-03-05 13:15:07 -08001006
Tejun Heo4bfd4822012-03-05 13:15:08 -08001007 if (IS_ERR(blkg)) {
1008 ret = PTR_ERR(blkg);
Tejun Heo3a8b31d2012-04-01 14:38:43 -07001009 rcu_read_unlock();
1010 put_disk(disk);
1011 /*
1012 * If queue was bypassing, we should retry. Do so after a
1013 * short msleep(). It isn't strictly necessary but queue
1014 * can be bypassing for some time and it's always nice to
1015 * avoid busy looping.
1016 */
1017 if (ret == -EBUSY) {
1018 msleep(10);
1019 ret = restart_syscall();
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001020 }
Tejun Heo726fa692012-04-01 14:38:43 -07001021 return ret;
Vivek Goyal062a6442010-09-15 17:06:33 -04001022 }
Tejun Heoe56da7e2012-03-05 13:15:07 -08001023
Tejun Heo3a8b31d2012-04-01 14:38:43 -07001024 ctx->disk = disk;
1025 ctx->blkg = blkg;
Tejun Heo726fa692012-04-01 14:38:43 -07001026 ctx->v = v;
1027 return 0;
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001028}
1029
Tejun Heo3a8b31d2012-04-01 14:38:43 -07001030/**
1031 * blkg_conf_finish - finish up per-blkg config update
1032 * @ctx: blkg_conf_ctx intiailized by blkg_conf_prep()
1033 *
1034 * Finish up after per-blkg config update. This function must be paired
1035 * with blkg_conf_prep().
1036 */
1037static void blkg_conf_finish(struct blkg_conf_ctx *ctx)
1038 __releases(rcu)
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001039{
Tejun Heo3a8b31d2012-04-01 14:38:43 -07001040 rcu_read_unlock();
1041 put_disk(ctx->disk);
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001042}
1043
Tejun Heoc4682ae2012-04-01 14:38:43 -07001044/* for propio conf */
1045static u64 blkg_prfill_weight_device(struct seq_file *sf,
1046 struct blkg_policy_data *pd, int off)
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001047{
Tejun Heoc4682ae2012-04-01 14:38:43 -07001048 if (!pd->conf.weight)
1049 return 0;
1050 return __blkg_prfill_u64(sf, pd, pd->conf.weight);
Vivek Goyal062a6442010-09-15 17:06:33 -04001051}
1052
Tejun Heoc4682ae2012-04-01 14:38:43 -07001053static int blkcg_print_weight_device(struct cgroup *cgrp, struct cftype *cft,
1054 struct seq_file *sf)
Vivek Goyal062a6442010-09-15 17:06:33 -04001055{
Tejun Heoc4682ae2012-04-01 14:38:43 -07001056 blkcg_print_blkgs(sf, cgroup_to_blkio_cgroup(cgrp),
1057 blkg_prfill_weight_device, BLKIO_POLICY_PROP, 0,
1058 false);
1059 return 0;
Vivek Goyal062a6442010-09-15 17:06:33 -04001060}
1061
Tejun Heoc4682ae2012-04-01 14:38:43 -07001062static int blkcg_print_weight(struct cgroup *cgrp, struct cftype *cft,
1063 struct seq_file *sf)
Vivek Goyal062a6442010-09-15 17:06:33 -04001064{
Tejun Heoc4682ae2012-04-01 14:38:43 -07001065 seq_printf(sf, "%u\n", cgroup_to_blkio_cgroup(cgrp)->weight);
Vivek Goyal062a6442010-09-15 17:06:33 -04001066 return 0;
1067}
1068
Tejun Heo3a8b31d2012-04-01 14:38:43 -07001069static int blkcg_set_weight_device(struct cgroup *cgrp, struct cftype *cft,
1070 const char *buf)
1071{
1072 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgrp);
1073 struct blkg_policy_data *pd;
1074 struct blkg_conf_ctx ctx;
1075 int ret;
1076
1077 ret = blkg_conf_prep(blkcg, buf, &ctx);
1078 if (ret)
1079 return ret;
1080
1081 ret = -EINVAL;
1082 pd = ctx.blkg->pd[BLKIO_POLICY_PROP];
1083 if (pd && (!ctx.v || (ctx.v >= BLKIO_WEIGHT_MIN &&
1084 ctx.v <= BLKIO_WEIGHT_MAX))) {
1085 pd->conf.weight = ctx.v;
1086 blkio_update_group_weight(ctx.blkg, BLKIO_POLICY_PROP,
1087 ctx.v ?: blkcg->weight);
1088 ret = 0;
1089 }
1090
1091 blkg_conf_finish(&ctx);
1092 return ret;
1093}
1094
Tejun Heo627f29f2012-04-01 14:38:43 -07001095static int blkcg_set_weight(struct cgroup *cgrp, struct cftype *cft, u64 val)
Vivek Goyal062a6442010-09-15 17:06:33 -04001096{
Tejun Heo627f29f2012-04-01 14:38:43 -07001097 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgrp);
Vivek Goyal062a6442010-09-15 17:06:33 -04001098 struct blkio_group *blkg;
1099 struct hlist_node *n;
Vivek Goyal062a6442010-09-15 17:06:33 -04001100
1101 if (val < BLKIO_WEIGHT_MIN || val > BLKIO_WEIGHT_MAX)
1102 return -EINVAL;
1103
1104 spin_lock(&blkio_list_lock);
1105 spin_lock_irq(&blkcg->lock);
1106 blkcg->weight = (unsigned int)val;
1107
Tejun Heo549d3aa2012-03-05 13:15:16 -08001108 hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) {
Tejun Heo627f29f2012-04-01 14:38:43 -07001109 struct blkg_policy_data *pd = blkg->pd[BLKIO_POLICY_PROP];
Tejun Heo549d3aa2012-03-05 13:15:16 -08001110
Tejun Heo627f29f2012-04-01 14:38:43 -07001111 if (pd && !pd->conf.weight)
1112 blkio_update_group_weight(blkg, BLKIO_POLICY_PROP,
1113 blkcg->weight);
Tejun Heo549d3aa2012-03-05 13:15:16 -08001114 }
Vivek Goyal062a6442010-09-15 17:06:33 -04001115
Vivek Goyal062a6442010-09-15 17:06:33 -04001116 spin_unlock_irq(&blkcg->lock);
1117 spin_unlock(&blkio_list_lock);
1118 return 0;
1119}
1120
Tejun Heoc4682ae2012-04-01 14:38:43 -07001121/* for blk-throttle conf */
1122#ifdef CONFIG_BLK_DEV_THROTTLING
1123static u64 blkg_prfill_conf_u64(struct seq_file *sf,
1124 struct blkg_policy_data *pd, int off)
1125{
1126 u64 v = *(u64 *)((void *)&pd->conf + off);
Vivek Goyal062a6442010-09-15 17:06:33 -04001127
Tejun Heoc4682ae2012-04-01 14:38:43 -07001128 if (!v)
1129 return 0;
1130 return __blkg_prfill_u64(sf, pd, v);
1131}
Vivek Goyal062a6442010-09-15 17:06:33 -04001132
Tejun Heoc4682ae2012-04-01 14:38:43 -07001133static int blkcg_print_conf_u64(struct cgroup *cgrp, struct cftype *cft,
1134 struct seq_file *sf)
1135{
Tejun Heoc4682ae2012-04-01 14:38:43 -07001136 blkcg_print_blkgs(sf, cgroup_to_blkio_cgroup(cgrp),
1137 blkg_prfill_conf_u64, BLKIO_POLICY_THROTL,
Tejun Heo3a8b31d2012-04-01 14:38:43 -07001138 cft->private, false);
Vivek Goyal062a6442010-09-15 17:06:33 -04001139 return 0;
1140}
Tejun Heo3a8b31d2012-04-01 14:38:43 -07001141
1142static int blkcg_set_conf_u64(struct cgroup *cgrp, struct cftype *cft,
1143 const char *buf, int rw,
1144 void (*update)(struct blkio_group *, int, u64, int))
1145{
1146 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgrp);
1147 struct blkg_policy_data *pd;
1148 struct blkg_conf_ctx ctx;
1149 int ret;
1150
1151 ret = blkg_conf_prep(blkcg, buf, &ctx);
1152 if (ret)
1153 return ret;
1154
1155 ret = -EINVAL;
1156 pd = ctx.blkg->pd[BLKIO_POLICY_THROTL];
1157 if (pd) {
1158 *(u64 *)((void *)&pd->conf + cft->private) = ctx.v;
1159 update(ctx.blkg, BLKIO_POLICY_THROTL, ctx.v ?: -1, rw);
1160 ret = 0;
1161 }
1162
1163 blkg_conf_finish(&ctx);
1164 return ret;
1165}
1166
1167static int blkcg_set_conf_bps_r(struct cgroup *cgrp, struct cftype *cft,
1168 const char *buf)
1169{
1170 return blkcg_set_conf_u64(cgrp, cft, buf, READ, blkio_update_group_bps);
1171}
1172
1173static int blkcg_set_conf_bps_w(struct cgroup *cgrp, struct cftype *cft,
1174 const char *buf)
1175{
1176 return blkcg_set_conf_u64(cgrp, cft, buf, WRITE, blkio_update_group_bps);
1177}
1178
1179static int blkcg_set_conf_iops_r(struct cgroup *cgrp, struct cftype *cft,
1180 const char *buf)
1181{
1182 return blkcg_set_conf_u64(cgrp, cft, buf, READ, blkio_update_group_iops);
1183}
1184
1185static int blkcg_set_conf_iops_w(struct cgroup *cgrp, struct cftype *cft,
1186 const char *buf)
1187{
1188 return blkcg_set_conf_u64(cgrp, cft, buf, WRITE, blkio_update_group_iops);
1189}
Tejun Heoc4682ae2012-04-01 14:38:43 -07001190#endif
Vivek Goyal062a6442010-09-15 17:06:33 -04001191
Vivek Goyal31e4c282009-12-03 12:59:42 -05001192struct cftype blkio_files[] = {
1193 {
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001194 .name = "weight_device",
Tejun Heoc4682ae2012-04-01 14:38:43 -07001195 .read_seq_string = blkcg_print_weight_device,
Tejun Heo3a8b31d2012-04-01 14:38:43 -07001196 .write_string = blkcg_set_weight_device,
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001197 .max_write_len = 256,
1198 },
1199 {
Vivek Goyal31e4c282009-12-03 12:59:42 -05001200 .name = "weight",
Tejun Heoc4682ae2012-04-01 14:38:43 -07001201 .read_seq_string = blkcg_print_weight,
Tejun Heo627f29f2012-04-01 14:38:43 -07001202 .write_u64 = blkcg_set_weight,
Vivek Goyal31e4c282009-12-03 12:59:42 -05001203 },
Vivek Goyal22084192009-12-03 12:59:49 -05001204 {
1205 .name = "time",
Tejun Heod3d32e62012-04-01 14:38:42 -07001206 .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP,
1207 offsetof(struct blkio_group_stats, time)),
1208 .read_seq_string = blkcg_print_stat,
Vivek Goyal22084192009-12-03 12:59:49 -05001209 },
1210 {
1211 .name = "sectors",
Tejun Heod3d32e62012-04-01 14:38:42 -07001212 .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP,
1213 offsetof(struct blkio_group_stats_cpu, sectors)),
1214 .read_seq_string = blkcg_print_cpu_stat,
Divyesh Shah303a3ac2010-04-01 15:01:24 -07001215 },
1216 {
1217 .name = "io_service_bytes",
Tejun Heod3d32e62012-04-01 14:38:42 -07001218 .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP,
1219 offsetof(struct blkio_group_stats_cpu, service_bytes)),
1220 .read_seq_string = blkcg_print_cpu_rwstat,
Divyesh Shah303a3ac2010-04-01 15:01:24 -07001221 },
1222 {
1223 .name = "io_serviced",
Tejun Heod3d32e62012-04-01 14:38:42 -07001224 .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP,
1225 offsetof(struct blkio_group_stats_cpu, serviced)),
1226 .read_seq_string = blkcg_print_cpu_rwstat,
Divyesh Shah303a3ac2010-04-01 15:01:24 -07001227 },
1228 {
1229 .name = "io_service_time",
Tejun Heod3d32e62012-04-01 14:38:42 -07001230 .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP,
1231 offsetof(struct blkio_group_stats, service_time)),
1232 .read_seq_string = blkcg_print_rwstat,
Divyesh Shah303a3ac2010-04-01 15:01:24 -07001233 },
1234 {
1235 .name = "io_wait_time",
Tejun Heod3d32e62012-04-01 14:38:42 -07001236 .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP,
1237 offsetof(struct blkio_group_stats, wait_time)),
1238 .read_seq_string = blkcg_print_rwstat,
Divyesh Shah84c124d2010-04-09 08:31:19 +02001239 },
1240 {
Divyesh Shah812d4022010-04-08 21:14:23 -07001241 .name = "io_merged",
Tejun Heod3d32e62012-04-01 14:38:42 -07001242 .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP,
1243 offsetof(struct blkio_group_stats, merged)),
1244 .read_seq_string = blkcg_print_rwstat,
Divyesh Shah812d4022010-04-08 21:14:23 -07001245 },
1246 {
Divyesh Shahcdc11842010-04-08 21:15:10 -07001247 .name = "io_queued",
Tejun Heod3d32e62012-04-01 14:38:42 -07001248 .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP,
1249 offsetof(struct blkio_group_stats, queued)),
1250 .read_seq_string = blkcg_print_rwstat,
Divyesh Shahcdc11842010-04-08 21:15:10 -07001251 },
1252 {
Divyesh Shah84c124d2010-04-09 08:31:19 +02001253 .name = "reset_stats",
1254 .write_u64 = blkiocg_reset_stats,
Vivek Goyal22084192009-12-03 12:59:49 -05001255 },
Vivek Goyal13f98252010-10-01 14:49:41 +02001256#ifdef CONFIG_BLK_DEV_THROTTLING
1257 {
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001258 .name = "throttle.read_bps_device",
Tejun Heo3a8b31d2012-04-01 14:38:43 -07001259 .private = offsetof(struct blkio_group_conf, bps[READ]),
Tejun Heoc4682ae2012-04-01 14:38:43 -07001260 .read_seq_string = blkcg_print_conf_u64,
Tejun Heo3a8b31d2012-04-01 14:38:43 -07001261 .write_string = blkcg_set_conf_bps_r,
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001262 .max_write_len = 256,
1263 },
1264
1265 {
1266 .name = "throttle.write_bps_device",
Tejun Heo3a8b31d2012-04-01 14:38:43 -07001267 .private = offsetof(struct blkio_group_conf, bps[WRITE]),
Tejun Heoc4682ae2012-04-01 14:38:43 -07001268 .read_seq_string = blkcg_print_conf_u64,
Tejun Heo3a8b31d2012-04-01 14:38:43 -07001269 .write_string = blkcg_set_conf_bps_w,
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001270 .max_write_len = 256,
1271 },
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001272
1273 {
1274 .name = "throttle.read_iops_device",
Tejun Heo3a8b31d2012-04-01 14:38:43 -07001275 .private = offsetof(struct blkio_group_conf, iops[READ]),
Tejun Heoc4682ae2012-04-01 14:38:43 -07001276 .read_seq_string = blkcg_print_conf_u64,
Tejun Heo3a8b31d2012-04-01 14:38:43 -07001277 .write_string = blkcg_set_conf_iops_r,
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001278 .max_write_len = 256,
1279 },
1280
1281 {
1282 .name = "throttle.write_iops_device",
Tejun Heo3a8b31d2012-04-01 14:38:43 -07001283 .private = offsetof(struct blkio_group_conf, iops[WRITE]),
Tejun Heoc4682ae2012-04-01 14:38:43 -07001284 .read_seq_string = blkcg_print_conf_u64,
Tejun Heo3a8b31d2012-04-01 14:38:43 -07001285 .write_string = blkcg_set_conf_iops_w,
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001286 .max_write_len = 256,
1287 },
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001288 {
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001289 .name = "throttle.io_service_bytes",
Tejun Heod3d32e62012-04-01 14:38:42 -07001290 .private = BLKCG_STAT_PRIV(BLKIO_POLICY_THROTL,
1291 offsetof(struct blkio_group_stats_cpu, service_bytes)),
1292 .read_seq_string = blkcg_print_cpu_rwstat,
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001293 },
1294 {
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001295 .name = "throttle.io_serviced",
Tejun Heod3d32e62012-04-01 14:38:42 -07001296 .private = BLKCG_STAT_PRIV(BLKIO_POLICY_THROTL,
1297 offsetof(struct blkio_group_stats_cpu, serviced)),
1298 .read_seq_string = blkcg_print_cpu_rwstat,
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001299 },
Vivek Goyal13f98252010-10-01 14:49:41 +02001300#endif /* CONFIG_BLK_DEV_THROTTLING */
1301
Vivek Goyal22084192009-12-03 12:59:49 -05001302#ifdef CONFIG_DEBUG_BLK_CGROUP
Divyesh Shahcdc11842010-04-08 21:15:10 -07001303 {
1304 .name = "avg_queue_size",
Tejun Heod3d32e62012-04-01 14:38:42 -07001305 .read_seq_string = blkcg_print_avg_queue_size,
Divyesh Shahcdc11842010-04-08 21:15:10 -07001306 },
1307 {
Divyesh Shah812df482010-04-08 21:15:35 -07001308 .name = "group_wait_time",
Tejun Heod3d32e62012-04-01 14:38:42 -07001309 .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP,
1310 offsetof(struct blkio_group_stats, group_wait_time)),
1311 .read_seq_string = blkcg_print_stat,
Divyesh Shah812df482010-04-08 21:15:35 -07001312 },
1313 {
1314 .name = "idle_time",
Tejun Heod3d32e62012-04-01 14:38:42 -07001315 .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP,
1316 offsetof(struct blkio_group_stats, idle_time)),
1317 .read_seq_string = blkcg_print_stat,
Divyesh Shah812df482010-04-08 21:15:35 -07001318 },
1319 {
1320 .name = "empty_time",
Tejun Heod3d32e62012-04-01 14:38:42 -07001321 .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP,
1322 offsetof(struct blkio_group_stats, empty_time)),
1323 .read_seq_string = blkcg_print_stat,
Divyesh Shah812df482010-04-08 21:15:35 -07001324 },
1325 {
Vivek Goyal22084192009-12-03 12:59:49 -05001326 .name = "dequeue",
Tejun Heod3d32e62012-04-01 14:38:42 -07001327 .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP,
1328 offsetof(struct blkio_group_stats, dequeue)),
1329 .read_seq_string = blkcg_print_stat,
Divyesh Shahcdc11842010-04-08 21:15:10 -07001330 },
Justin TerAvest9026e522011-03-22 21:26:54 +01001331 {
1332 .name = "unaccounted_time",
Tejun Heod3d32e62012-04-01 14:38:42 -07001333 .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP,
1334 offsetof(struct blkio_group_stats, unaccounted_time)),
1335 .read_seq_string = blkcg_print_stat,
Justin TerAvest9026e522011-03-22 21:26:54 +01001336 },
Vivek Goyal22084192009-12-03 12:59:49 -05001337#endif
Tejun Heo4baf6e32012-04-01 12:09:55 -07001338 { } /* terminate */
Vivek Goyal31e4c282009-12-03 12:59:42 -05001339};
1340
Tejun Heo9f13ef62012-03-05 13:15:21 -08001341/**
1342 * blkiocg_pre_destroy - cgroup pre_destroy callback
Tejun Heo9f13ef62012-03-05 13:15:21 -08001343 * @cgroup: cgroup of interest
1344 *
1345 * This function is called when @cgroup is about to go away and responsible
1346 * for shooting down all blkgs associated with @cgroup. blkgs should be
1347 * removed while holding both q and blkcg locks. As blkcg lock is nested
1348 * inside q lock, this function performs reverse double lock dancing.
1349 *
1350 * This is the blkcg counterpart of ioc_release_fn().
1351 */
Tejun Heo959d8512012-04-01 12:30:01 -07001352static int blkiocg_pre_destroy(struct cgroup *cgroup)
Vivek Goyal31e4c282009-12-03 12:59:42 -05001353{
1354 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgroup);
1355
Tejun Heo9f13ef62012-03-05 13:15:21 -08001356 spin_lock_irq(&blkcg->lock);
Tejun Heo7ee9c562012-03-05 13:15:11 -08001357
Tejun Heo9f13ef62012-03-05 13:15:21 -08001358 while (!hlist_empty(&blkcg->blkg_list)) {
1359 struct blkio_group *blkg = hlist_entry(blkcg->blkg_list.first,
1360 struct blkio_group, blkcg_node);
Tejun Heoc875f4d2012-03-05 13:15:22 -08001361 struct request_queue *q = blkg->q;
Vivek Goyalb1c35762009-12-03 12:59:47 -05001362
Tejun Heo9f13ef62012-03-05 13:15:21 -08001363 if (spin_trylock(q->queue_lock)) {
1364 blkg_destroy(blkg);
1365 spin_unlock(q->queue_lock);
1366 } else {
1367 spin_unlock_irq(&blkcg->lock);
Tejun Heo9f13ef62012-03-05 13:15:21 -08001368 cpu_relax();
Dan Carpentera5567932012-03-29 20:57:08 +02001369 spin_lock_irq(&blkcg->lock);
Jens Axboe0f3942a2010-05-03 14:28:55 +02001370 }
Tejun Heo9f13ef62012-03-05 13:15:21 -08001371 }
Jens Axboe0f3942a2010-05-03 14:28:55 +02001372
Tejun Heo9f13ef62012-03-05 13:15:21 -08001373 spin_unlock_irq(&blkcg->lock);
Tejun Heo7ee9c562012-03-05 13:15:11 -08001374 return 0;
1375}
1376
Li Zefan761b3ef2012-01-31 13:47:36 +08001377static void blkiocg_destroy(struct cgroup *cgroup)
Tejun Heo7ee9c562012-03-05 13:15:11 -08001378{
1379 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgroup);
1380
Ben Blum67523c42010-03-10 15:22:11 -08001381 if (blkcg != &blkio_root_cgroup)
1382 kfree(blkcg);
Vivek Goyal31e4c282009-12-03 12:59:42 -05001383}
1384
Li Zefan761b3ef2012-01-31 13:47:36 +08001385static struct cgroup_subsys_state *blkiocg_create(struct cgroup *cgroup)
Vivek Goyal31e4c282009-12-03 12:59:42 -05001386{
Tejun Heo9a9e8a22012-03-19 15:10:56 -07001387 static atomic64_t id_seq = ATOMIC64_INIT(0);
Li Zefan03415092010-05-07 08:57:00 +02001388 struct blkio_cgroup *blkcg;
1389 struct cgroup *parent = cgroup->parent;
Vivek Goyal31e4c282009-12-03 12:59:42 -05001390
Li Zefan03415092010-05-07 08:57:00 +02001391 if (!parent) {
Vivek Goyal31e4c282009-12-03 12:59:42 -05001392 blkcg = &blkio_root_cgroup;
1393 goto done;
1394 }
1395
Vivek Goyal31e4c282009-12-03 12:59:42 -05001396 blkcg = kzalloc(sizeof(*blkcg), GFP_KERNEL);
1397 if (!blkcg)
1398 return ERR_PTR(-ENOMEM);
1399
1400 blkcg->weight = BLKIO_WEIGHT_DEFAULT;
Tejun Heo9a9e8a22012-03-19 15:10:56 -07001401 blkcg->id = atomic64_inc_return(&id_seq); /* root is 0, start from 1 */
Vivek Goyal31e4c282009-12-03 12:59:42 -05001402done:
1403 spin_lock_init(&blkcg->lock);
1404 INIT_HLIST_HEAD(&blkcg->blkg_list);
1405
1406 return &blkcg->css;
1407}
1408
Tejun Heo5efd6112012-03-05 13:15:12 -08001409/**
1410 * blkcg_init_queue - initialize blkcg part of request queue
1411 * @q: request_queue to initialize
1412 *
1413 * Called from blk_alloc_queue_node(). Responsible for initializing blkcg
1414 * part of new request_queue @q.
1415 *
1416 * RETURNS:
1417 * 0 on success, -errno on failure.
1418 */
1419int blkcg_init_queue(struct request_queue *q)
1420{
Tejun Heo923adde2012-03-05 13:15:13 -08001421 int ret;
1422
Tejun Heo5efd6112012-03-05 13:15:12 -08001423 might_sleep();
1424
Tejun Heo923adde2012-03-05 13:15:13 -08001425 ret = blk_throtl_init(q);
1426 if (ret)
1427 return ret;
1428
1429 mutex_lock(&all_q_mutex);
1430 INIT_LIST_HEAD(&q->all_q_node);
1431 list_add_tail(&q->all_q_node, &all_q_list);
1432 mutex_unlock(&all_q_mutex);
1433
1434 return 0;
Tejun Heo5efd6112012-03-05 13:15:12 -08001435}
1436
1437/**
1438 * blkcg_drain_queue - drain blkcg part of request_queue
1439 * @q: request_queue to drain
1440 *
1441 * Called from blk_drain_queue(). Responsible for draining blkcg part.
1442 */
1443void blkcg_drain_queue(struct request_queue *q)
1444{
1445 lockdep_assert_held(q->queue_lock);
1446
1447 blk_throtl_drain(q);
1448}
1449
1450/**
1451 * blkcg_exit_queue - exit and release blkcg part of request_queue
1452 * @q: request_queue being released
1453 *
1454 * Called from blk_release_queue(). Responsible for exiting blkcg part.
1455 */
1456void blkcg_exit_queue(struct request_queue *q)
1457{
Tejun Heo923adde2012-03-05 13:15:13 -08001458 mutex_lock(&all_q_mutex);
1459 list_del_init(&q->all_q_node);
1460 mutex_unlock(&all_q_mutex);
1461
Tejun Heoe8989fa2012-03-05 13:15:20 -08001462 blkg_destroy_all(q, true);
1463
Tejun Heo5efd6112012-03-05 13:15:12 -08001464 blk_throtl_exit(q);
1465}
1466
Vivek Goyal31e4c282009-12-03 12:59:42 -05001467/*
1468 * We cannot support shared io contexts, as we have no mean to support
1469 * two tasks with the same ioc in two different groups without major rework
1470 * of the main cic data structures. For now we allow a task to change
1471 * its cgroup only if it's the only owner of its ioc.
1472 */
Li Zefan761b3ef2012-01-31 13:47:36 +08001473static int blkiocg_can_attach(struct cgroup *cgrp, struct cgroup_taskset *tset)
Vivek Goyal31e4c282009-12-03 12:59:42 -05001474{
Tejun Heobb9d97b2011-12-12 18:12:21 -08001475 struct task_struct *task;
Vivek Goyal31e4c282009-12-03 12:59:42 -05001476 struct io_context *ioc;
1477 int ret = 0;
1478
1479 /* task_lock() is needed to avoid races with exit_io_context() */
Tejun Heobb9d97b2011-12-12 18:12:21 -08001480 cgroup_taskset_for_each(task, cgrp, tset) {
1481 task_lock(task);
1482 ioc = task->io_context;
1483 if (ioc && atomic_read(&ioc->nr_tasks) > 1)
1484 ret = -EINVAL;
1485 task_unlock(task);
1486 if (ret)
1487 break;
1488 }
Vivek Goyal31e4c282009-12-03 12:59:42 -05001489 return ret;
1490}
1491
Tejun Heo923adde2012-03-05 13:15:13 -08001492static void blkcg_bypass_start(void)
1493 __acquires(&all_q_mutex)
1494{
1495 struct request_queue *q;
1496
1497 mutex_lock(&all_q_mutex);
1498
1499 list_for_each_entry(q, &all_q_list, all_q_node) {
1500 blk_queue_bypass_start(q);
Tejun Heoe8989fa2012-03-05 13:15:20 -08001501 blkg_destroy_all(q, false);
Tejun Heo923adde2012-03-05 13:15:13 -08001502 }
1503}
1504
1505static void blkcg_bypass_end(void)
1506 __releases(&all_q_mutex)
1507{
1508 struct request_queue *q;
1509
1510 list_for_each_entry(q, &all_q_list, all_q_node)
1511 blk_queue_bypass_end(q);
1512
1513 mutex_unlock(&all_q_mutex);
1514}
1515
Tejun Heo676f7c82012-04-01 12:09:55 -07001516struct cgroup_subsys blkio_subsys = {
1517 .name = "blkio",
1518 .create = blkiocg_create,
1519 .can_attach = blkiocg_can_attach,
Tejun Heo959d8512012-04-01 12:30:01 -07001520 .pre_destroy = blkiocg_pre_destroy,
Tejun Heo676f7c82012-04-01 12:09:55 -07001521 .destroy = blkiocg_destroy,
Tejun Heo676f7c82012-04-01 12:09:55 -07001522 .subsys_id = blkio_subsys_id,
Tejun Heo4baf6e32012-04-01 12:09:55 -07001523 .base_cftypes = blkio_files,
Tejun Heo676f7c82012-04-01 12:09:55 -07001524 .module = THIS_MODULE,
1525};
1526EXPORT_SYMBOL_GPL(blkio_subsys);
1527
Vivek Goyal3e252062009-12-04 10:36:42 -05001528void blkio_policy_register(struct blkio_policy_type *blkiop)
1529{
Tejun Heoe8989fa2012-03-05 13:15:20 -08001530 struct request_queue *q;
1531
Tejun Heo923adde2012-03-05 13:15:13 -08001532 blkcg_bypass_start();
Vivek Goyal3e252062009-12-04 10:36:42 -05001533 spin_lock(&blkio_list_lock);
Tejun Heo035d10b2012-03-05 13:15:04 -08001534
1535 BUG_ON(blkio_policy[blkiop->plid]);
1536 blkio_policy[blkiop->plid] = blkiop;
Vivek Goyal3e252062009-12-04 10:36:42 -05001537 list_add_tail(&blkiop->list, &blkio_list);
Tejun Heo035d10b2012-03-05 13:15:04 -08001538
Vivek Goyal3e252062009-12-04 10:36:42 -05001539 spin_unlock(&blkio_list_lock);
Tejun Heoe8989fa2012-03-05 13:15:20 -08001540 list_for_each_entry(q, &all_q_list, all_q_node)
1541 update_root_blkg_pd(q, blkiop->plid);
Tejun Heo923adde2012-03-05 13:15:13 -08001542 blkcg_bypass_end();
Vivek Goyal3e252062009-12-04 10:36:42 -05001543}
1544EXPORT_SYMBOL_GPL(blkio_policy_register);
1545
1546void blkio_policy_unregister(struct blkio_policy_type *blkiop)
1547{
Tejun Heoe8989fa2012-03-05 13:15:20 -08001548 struct request_queue *q;
1549
Tejun Heo923adde2012-03-05 13:15:13 -08001550 blkcg_bypass_start();
Vivek Goyal3e252062009-12-04 10:36:42 -05001551 spin_lock(&blkio_list_lock);
Tejun Heo035d10b2012-03-05 13:15:04 -08001552
1553 BUG_ON(blkio_policy[blkiop->plid] != blkiop);
1554 blkio_policy[blkiop->plid] = NULL;
Vivek Goyal3e252062009-12-04 10:36:42 -05001555 list_del_init(&blkiop->list);
Tejun Heo035d10b2012-03-05 13:15:04 -08001556
Vivek Goyal3e252062009-12-04 10:36:42 -05001557 spin_unlock(&blkio_list_lock);
Tejun Heoe8989fa2012-03-05 13:15:20 -08001558 list_for_each_entry(q, &all_q_list, all_q_node)
1559 update_root_blkg_pd(q, blkiop->plid);
Tejun Heo923adde2012-03-05 13:15:13 -08001560 blkcg_bypass_end();
Vivek Goyal3e252062009-12-04 10:36:42 -05001561}
1562EXPORT_SYMBOL_GPL(blkio_policy_unregister);