blob: ca0ff7c0ffb6852ea39acde2d4e2ccd0da4e8416 [file] [log] [blame]
Vivek Goyal31e4c282009-12-03 12:59:42 -05001#ifndef _BLK_CGROUP_H
2#define _BLK_CGROUP_H
3/*
4 * Common Block IO controller cgroup interface
5 *
6 * Based on ideas and code from CFQ, CFS and BFQ:
7 * Copyright (C) 2003 Jens Axboe <axboe@kernel.dk>
8 *
9 * Copyright (C) 2008 Fabio Checconi <fabio@gandalf.sssup.it>
10 * Paolo Valente <paolo.valente@unimore.it>
11 *
12 * Copyright (C) 2009 Vivek Goyal <vgoyal@redhat.com>
13 * Nauman Rafique <nauman@google.com>
14 */
15
16#include <linux/cgroup.h>
Vivek Goyal575969a2011-05-19 15:38:29 -040017#include <linux/u64_stats_sync.h>
Tejun Heo829fdb52012-04-01 14:38:43 -070018#include <linux/seq_file.h>
Vivek Goyal31e4c282009-12-03 12:59:42 -050019
Vivek Goyal062a6442010-09-15 17:06:33 -040020enum blkio_policy_id {
21 BLKIO_POLICY_PROP = 0, /* Proportional Bandwidth division */
Vivek Goyal4c9eefa2010-09-15 17:06:34 -040022 BLKIO_POLICY_THROTL, /* Throttling */
Tejun Heo035d10b2012-03-05 13:15:04 -080023
24 BLKIO_NR_POLICIES,
Vivek Goyal062a6442010-09-15 17:06:33 -040025};
26
Vivek Goyal9355aed2010-10-01 21:16:41 +020027/* Max limits for throttle policy */
28#define THROTL_IOPS_MAX UINT_MAX
29
Tejun Heo32e380a2012-03-05 13:14:54 -080030#ifdef CONFIG_BLK_CGROUP
Jens Axboe2f5ea472009-12-03 21:06:43 +010031
Tejun Heo3381cb82012-04-01 14:38:44 -070032/* CFQ specific, out here for blkcg->cfq_weight */
33#define CFQ_WEIGHT_MIN 10
34#define CFQ_WEIGHT_MAX 1000
35#define CFQ_WEIGHT_DEFAULT 500
36
Tejun Heoedcb0722012-04-01 14:38:42 -070037enum blkg_rwstat_type {
38 BLKG_RWSTAT_READ,
39 BLKG_RWSTAT_WRITE,
40 BLKG_RWSTAT_SYNC,
41 BLKG_RWSTAT_ASYNC,
42
43 BLKG_RWSTAT_NR,
44 BLKG_RWSTAT_TOTAL = BLKG_RWSTAT_NR,
Divyesh Shah303a3ac2010-04-01 15:01:24 -070045};
46
Vivek Goyal31e4c282009-12-03 12:59:42 -050047struct blkio_cgroup {
48 struct cgroup_subsys_state css;
Vivek Goyal31e4c282009-12-03 12:59:42 -050049 spinlock_t lock;
50 struct hlist_head blkg_list;
Tejun Heo9a9e8a22012-03-19 15:10:56 -070051
52 /* for policies to test whether associated blkcg has changed */
53 uint64_t id;
Tejun Heo3381cb82012-04-01 14:38:44 -070054
55 /* TODO: per-policy storage in blkio_cgroup */
56 unsigned int cfq_weight; /* belongs to cfq */
Vivek Goyal31e4c282009-12-03 12:59:42 -050057};
58
Tejun Heoedcb0722012-04-01 14:38:42 -070059struct blkg_stat {
60 struct u64_stats_sync syncp;
61 uint64_t cnt;
62};
63
64struct blkg_rwstat {
65 struct u64_stats_sync syncp;
66 uint64_t cnt[BLKG_RWSTAT_NR];
67};
68
Tejun Heo03814112012-03-05 13:15:14 -080069/* per-blkg per-policy data */
70struct blkg_policy_data {
71 /* the blkg this per-policy data belongs to */
72 struct blkio_group *blkg;
73
74 /* pol->pdata_size bytes of private data used by policy impl */
75 char pdata[] __aligned(__alignof__(unsigned long long));
76};
77
Vivek Goyal31e4c282009-12-03 12:59:42 -050078struct blkio_group {
Tejun Heoc875f4d2012-03-05 13:15:22 -080079 /* Pointer to the associated request_queue */
80 struct request_queue *q;
Tejun Heoe8989fa2012-03-05 13:15:20 -080081 struct list_head q_node;
Vivek Goyal31e4c282009-12-03 12:59:42 -050082 struct hlist_node blkcg_node;
Tejun Heo7ee9c562012-03-05 13:15:11 -080083 struct blkio_cgroup *blkcg;
Vivek Goyal2868ef72009-12-03 12:59:48 -050084 /* Store cgroup path */
85 char path[128];
Tejun Heo1adaf3d2012-03-05 13:15:15 -080086 /* reference count */
87 int refcnt;
Vivek Goyal22084192009-12-03 12:59:49 -050088
Tejun Heo549d3aa2012-03-05 13:15:16 -080089 struct blkg_policy_data *pd[BLKIO_NR_POLICIES];
Tejun Heo1adaf3d2012-03-05 13:15:15 -080090
91 struct rcu_head rcu_head;
Vivek Goyal31e4c282009-12-03 12:59:42 -050092};
93
Tejun Heo03814112012-03-05 13:15:14 -080094typedef void (blkio_init_group_fn)(struct blkio_group *blkg);
Tejun Heo9ade5ea2012-04-01 14:38:44 -070095typedef void (blkio_exit_group_fn)(struct blkio_group *blkg);
96typedef void (blkio_reset_group_stats_fn)(struct blkio_group *blkg);
Vivek Goyal3e252062009-12-04 10:36:42 -050097
98struct blkio_policy_ops {
Tejun Heo03814112012-03-05 13:15:14 -080099 blkio_init_group_fn *blkio_init_group_fn;
Tejun Heo9ade5ea2012-04-01 14:38:44 -0700100 blkio_exit_group_fn *blkio_exit_group_fn;
101 blkio_reset_group_stats_fn *blkio_reset_group_stats_fn;
Vivek Goyal3e252062009-12-04 10:36:42 -0500102};
103
104struct blkio_policy_type {
105 struct list_head list;
106 struct blkio_policy_ops ops;
Vivek Goyal062a6442010-09-15 17:06:33 -0400107 enum blkio_policy_id plid;
Tejun Heo03814112012-03-05 13:15:14 -0800108 size_t pdata_size; /* policy specific private data size */
Tejun Heo44ea53d2012-04-01 14:38:43 -0700109 struct cftype *cftypes; /* cgroup files for the policy */
Vivek Goyal3e252062009-12-04 10:36:42 -0500110};
111
Tejun Heo5efd6112012-03-05 13:15:12 -0800112extern int blkcg_init_queue(struct request_queue *q);
113extern void blkcg_drain_queue(struct request_queue *q);
114extern void blkcg_exit_queue(struct request_queue *q);
115
Vivek Goyal3e252062009-12-04 10:36:42 -0500116/* Blkio controller policy registration */
117extern void blkio_policy_register(struct blkio_policy_type *);
118extern void blkio_policy_unregister(struct blkio_policy_type *);
Tejun Heoe8989fa2012-03-05 13:15:20 -0800119extern void blkg_destroy_all(struct request_queue *q, bool destroy_root);
120extern void update_root_blkg_pd(struct request_queue *q,
121 enum blkio_policy_id plid);
Vivek Goyal3e252062009-12-04 10:36:42 -0500122
Tejun Heo829fdb52012-04-01 14:38:43 -0700123void blkcg_print_blkgs(struct seq_file *sf, struct blkio_cgroup *blkcg,
Tejun Heod366e7e2012-04-01 14:38:44 -0700124 u64 (*prfill)(struct seq_file *, void *, int),
Tejun Heo829fdb52012-04-01 14:38:43 -0700125 int pol, int data, bool show_total);
Tejun Heod366e7e2012-04-01 14:38:44 -0700126u64 __blkg_prfill_u64(struct seq_file *sf, void *pdata, u64 v);
127u64 __blkg_prfill_rwstat(struct seq_file *sf, void *pdata,
Tejun Heo829fdb52012-04-01 14:38:43 -0700128 const struct blkg_rwstat *rwstat);
Tejun Heo5bc4afb12012-04-01 14:38:45 -0700129u64 blkg_prfill_stat(struct seq_file *sf, void *pdata, int off);
130u64 blkg_prfill_rwstat(struct seq_file *sf, void *pdata, int off);
Tejun Heo829fdb52012-04-01 14:38:43 -0700131
132struct blkg_conf_ctx {
133 struct gendisk *disk;
134 struct blkio_group *blkg;
135 u64 v;
136};
137
138int blkg_conf_prep(struct blkio_cgroup *blkcg, const char *input,
139 struct blkg_conf_ctx *ctx);
140void blkg_conf_finish(struct blkg_conf_ctx *ctx);
141
142
Tejun Heo03814112012-03-05 13:15:14 -0800143/**
144 * blkg_to_pdata - get policy private data
145 * @blkg: blkg of interest
146 * @pol: policy of interest
147 *
148 * Return pointer to private data associated with the @blkg-@pol pair.
149 */
150static inline void *blkg_to_pdata(struct blkio_group *blkg,
151 struct blkio_policy_type *pol)
152{
Tejun Heo549d3aa2012-03-05 13:15:16 -0800153 return blkg ? blkg->pd[pol->plid]->pdata : NULL;
Tejun Heo03814112012-03-05 13:15:14 -0800154}
155
156/**
157 * pdata_to_blkg - get blkg associated with policy private data
158 * @pdata: policy private data of interest
Tejun Heo03814112012-03-05 13:15:14 -0800159 *
Tejun Heoaaec55a2012-04-01 14:38:42 -0700160 * @pdata is policy private data. Determine the blkg it's associated with.
Tejun Heo03814112012-03-05 13:15:14 -0800161 */
Tejun Heoaaec55a2012-04-01 14:38:42 -0700162static inline struct blkio_group *pdata_to_blkg(void *pdata)
Tejun Heo03814112012-03-05 13:15:14 -0800163{
164 if (pdata) {
165 struct blkg_policy_data *pd =
166 container_of(pdata, struct blkg_policy_data, pdata);
167 return pd->blkg;
168 }
169 return NULL;
170}
171
Vivek Goyalafc24d42010-04-26 19:27:56 +0200172static inline char *blkg_path(struct blkio_group *blkg)
173{
174 return blkg->path;
175}
176
Tejun Heo1adaf3d2012-03-05 13:15:15 -0800177/**
178 * blkg_get - get a blkg reference
179 * @blkg: blkg to get
180 *
181 * The caller should be holding queue_lock and an existing reference.
182 */
183static inline void blkg_get(struct blkio_group *blkg)
184{
185 lockdep_assert_held(blkg->q->queue_lock);
186 WARN_ON_ONCE(!blkg->refcnt);
187 blkg->refcnt++;
188}
189
190void __blkg_release(struct blkio_group *blkg);
191
192/**
193 * blkg_put - put a blkg reference
194 * @blkg: blkg to put
195 *
196 * The caller should be holding queue_lock.
197 */
198static inline void blkg_put(struct blkio_group *blkg)
199{
200 lockdep_assert_held(blkg->q->queue_lock);
201 WARN_ON_ONCE(blkg->refcnt <= 0);
202 if (!--blkg->refcnt)
203 __blkg_release(blkg);
204}
205
Tejun Heoedcb0722012-04-01 14:38:42 -0700206/**
207 * blkg_stat_add - add a value to a blkg_stat
208 * @stat: target blkg_stat
209 * @val: value to add
210 *
211 * Add @val to @stat. The caller is responsible for synchronizing calls to
212 * this function.
213 */
214static inline void blkg_stat_add(struct blkg_stat *stat, uint64_t val)
215{
216 u64_stats_update_begin(&stat->syncp);
217 stat->cnt += val;
218 u64_stats_update_end(&stat->syncp);
219}
220
221/**
222 * blkg_stat_read - read the current value of a blkg_stat
223 * @stat: blkg_stat to read
224 *
225 * Read the current value of @stat. This function can be called without
226 * synchroniztion and takes care of u64 atomicity.
227 */
228static inline uint64_t blkg_stat_read(struct blkg_stat *stat)
229{
230 unsigned int start;
231 uint64_t v;
232
233 do {
234 start = u64_stats_fetch_begin(&stat->syncp);
235 v = stat->cnt;
236 } while (u64_stats_fetch_retry(&stat->syncp, start));
237
238 return v;
239}
240
241/**
242 * blkg_stat_reset - reset a blkg_stat
243 * @stat: blkg_stat to reset
244 */
245static inline void blkg_stat_reset(struct blkg_stat *stat)
246{
247 stat->cnt = 0;
248}
249
250/**
251 * blkg_rwstat_add - add a value to a blkg_rwstat
252 * @rwstat: target blkg_rwstat
253 * @rw: mask of REQ_{WRITE|SYNC}
254 * @val: value to add
255 *
256 * Add @val to @rwstat. The counters are chosen according to @rw. The
257 * caller is responsible for synchronizing calls to this function.
258 */
259static inline void blkg_rwstat_add(struct blkg_rwstat *rwstat,
260 int rw, uint64_t val)
261{
262 u64_stats_update_begin(&rwstat->syncp);
263
264 if (rw & REQ_WRITE)
265 rwstat->cnt[BLKG_RWSTAT_WRITE] += val;
266 else
267 rwstat->cnt[BLKG_RWSTAT_READ] += val;
268 if (rw & REQ_SYNC)
269 rwstat->cnt[BLKG_RWSTAT_SYNC] += val;
270 else
271 rwstat->cnt[BLKG_RWSTAT_ASYNC] += val;
272
273 u64_stats_update_end(&rwstat->syncp);
274}
275
276/**
277 * blkg_rwstat_read - read the current values of a blkg_rwstat
278 * @rwstat: blkg_rwstat to read
279 *
280 * Read the current snapshot of @rwstat and return it as the return value.
281 * This function can be called without synchronization and takes care of
282 * u64 atomicity.
283 */
284static struct blkg_rwstat blkg_rwstat_read(struct blkg_rwstat *rwstat)
285{
286 unsigned int start;
287 struct blkg_rwstat tmp;
288
289 do {
290 start = u64_stats_fetch_begin(&rwstat->syncp);
291 tmp = *rwstat;
292 } while (u64_stats_fetch_retry(&rwstat->syncp, start));
293
294 return tmp;
295}
296
297/**
298 * blkg_rwstat_sum - read the total count of a blkg_rwstat
299 * @rwstat: blkg_rwstat to read
300 *
301 * Return the total count of @rwstat regardless of the IO direction. This
302 * function can be called without synchronization and takes care of u64
303 * atomicity.
304 */
305static inline uint64_t blkg_rwstat_sum(struct blkg_rwstat *rwstat)
306{
307 struct blkg_rwstat tmp = blkg_rwstat_read(rwstat);
308
309 return tmp.cnt[BLKG_RWSTAT_READ] + tmp.cnt[BLKG_RWSTAT_WRITE];
310}
311
312/**
313 * blkg_rwstat_reset - reset a blkg_rwstat
314 * @rwstat: blkg_rwstat to reset
315 */
316static inline void blkg_rwstat_reset(struct blkg_rwstat *rwstat)
317{
318 memset(rwstat->cnt, 0, sizeof(rwstat->cnt));
319}
320
Jens Axboe2f5ea472009-12-03 21:06:43 +0100321#else
322
323struct blkio_group {
324};
325
Vivek Goyal3e252062009-12-04 10:36:42 -0500326struct blkio_policy_type {
327};
328
Tejun Heo5efd6112012-03-05 13:15:12 -0800329static inline int blkcg_init_queue(struct request_queue *q) { return 0; }
330static inline void blkcg_drain_queue(struct request_queue *q) { }
331static inline void blkcg_exit_queue(struct request_queue *q) { }
Vivek Goyal3e252062009-12-04 10:36:42 -0500332static inline void blkio_policy_register(struct blkio_policy_type *blkiop) { }
333static inline void blkio_policy_unregister(struct blkio_policy_type *blkiop) { }
Tejun Heo03aa2642012-03-05 13:15:19 -0800334static inline void blkg_destroy_all(struct request_queue *q,
Tejun Heo03aa2642012-03-05 13:15:19 -0800335 bool destory_root) { }
Tejun Heoe8989fa2012-03-05 13:15:20 -0800336static inline void update_root_blkg_pd(struct request_queue *q,
337 enum blkio_policy_id plid) { }
Vivek Goyal3e252062009-12-04 10:36:42 -0500338
Tejun Heo03814112012-03-05 13:15:14 -0800339static inline void *blkg_to_pdata(struct blkio_group *blkg,
340 struct blkio_policy_type *pol) { return NULL; }
341static inline struct blkio_group *pdata_to_blkg(void *pdata,
342 struct blkio_policy_type *pol) { return NULL; }
Vivek Goyalafc24d42010-04-26 19:27:56 +0200343static inline char *blkg_path(struct blkio_group *blkg) { return NULL; }
Tejun Heo1adaf3d2012-03-05 13:15:15 -0800344static inline void blkg_get(struct blkio_group *blkg) { }
345static inline void blkg_put(struct blkio_group *blkg) { }
Vivek Goyalafc24d42010-04-26 19:27:56 +0200346
Jens Axboe2f5ea472009-12-03 21:06:43 +0100347#endif
348
Tejun Heo32e380a2012-03-05 13:14:54 -0800349#ifdef CONFIG_BLK_CGROUP
Vivek Goyal31e4c282009-12-03 12:59:42 -0500350extern struct blkio_cgroup blkio_root_cgroup;
351extern struct blkio_cgroup *cgroup_to_blkio_cgroup(struct cgroup *cgroup);
Tejun Heo4f85cb92012-03-05 13:15:28 -0800352extern struct blkio_cgroup *bio_blkio_cgroup(struct bio *bio);
Tejun Heocd1604f2012-03-05 13:15:06 -0800353extern struct blkio_group *blkg_lookup(struct blkio_cgroup *blkcg,
Tejun Heoe8989fa2012-03-05 13:15:20 -0800354 struct request_queue *q);
Tejun Heocd1604f2012-03-05 13:15:06 -0800355struct blkio_group *blkg_lookup_create(struct blkio_cgroup *blkcg,
356 struct request_queue *q,
Tejun Heocd1604f2012-03-05 13:15:06 -0800357 bool for_root);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500358#else
Jens Axboe2f5ea472009-12-03 21:06:43 +0100359struct cgroup;
Vivek Goyal31e4c282009-12-03 12:59:42 -0500360static inline struct blkio_cgroup *
361cgroup_to_blkio_cgroup(struct cgroup *cgroup) { return NULL; }
Vivek Goyal70087dc2011-05-16 15:24:08 +0200362static inline struct blkio_cgroup *
Tejun Heo4f85cb92012-03-05 13:15:28 -0800363bio_blkio_cgroup(struct bio *bio) { return NULL; }
Vivek Goyal31e4c282009-12-03 12:59:42 -0500364
Tejun Heocd1604f2012-03-05 13:15:06 -0800365static inline struct blkio_group *blkg_lookup(struct blkio_cgroup *blkcg,
366 void *key) { return NULL; }
Vivek Goyal31e4c282009-12-03 12:59:42 -0500367#endif
368#endif /* _BLK_CGROUP_H */