blob: 386db2974a08e71e66e471e6f21c6de8fa0fe4b7 [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 Heod3d32e62012-04-01 14:38:42 -070037/* cft->private [un]packing for stat printing */
38#define BLKCG_STAT_PRIV(pol, off) (((unsigned)(pol) << 16) | (off))
39#define BLKCG_STAT_POL(prv) ((unsigned)(prv) >> 16)
40#define BLKCG_STAT_OFF(prv) ((unsigned)(prv) & 0xffff)
Tejun Heo2aa4a152012-04-01 14:38:42 -070041
Tejun Heoedcb0722012-04-01 14:38:42 -070042enum blkg_rwstat_type {
43 BLKG_RWSTAT_READ,
44 BLKG_RWSTAT_WRITE,
45 BLKG_RWSTAT_SYNC,
46 BLKG_RWSTAT_ASYNC,
47
48 BLKG_RWSTAT_NR,
49 BLKG_RWSTAT_TOTAL = BLKG_RWSTAT_NR,
Divyesh Shah303a3ac2010-04-01 15:01:24 -070050};
51
Vivek Goyal31e4c282009-12-03 12:59:42 -050052struct blkio_cgroup {
53 struct cgroup_subsys_state css;
Vivek Goyal31e4c282009-12-03 12:59:42 -050054 spinlock_t lock;
55 struct hlist_head blkg_list;
Tejun Heo9a9e8a22012-03-19 15:10:56 -070056
57 /* for policies to test whether associated blkcg has changed */
58 uint64_t id;
Tejun Heo3381cb82012-04-01 14:38:44 -070059
60 /* TODO: per-policy storage in blkio_cgroup */
61 unsigned int cfq_weight; /* belongs to cfq */
Vivek Goyal31e4c282009-12-03 12:59:42 -050062};
63
Tejun Heoedcb0722012-04-01 14:38:42 -070064struct blkg_stat {
65 struct u64_stats_sync syncp;
66 uint64_t cnt;
67};
68
69struct blkg_rwstat {
70 struct u64_stats_sync syncp;
71 uint64_t cnt[BLKG_RWSTAT_NR];
72};
73
Tejun Heoe56da7e2012-03-05 13:15:07 -080074struct blkio_group_conf {
Tejun Heoc4682ae2012-04-01 14:38:43 -070075 u64 iops[2];
Tejun Heoe56da7e2012-03-05 13:15:07 -080076 u64 bps[2];
77};
78
Tejun Heo03814112012-03-05 13:15:14 -080079/* per-blkg per-policy data */
80struct blkg_policy_data {
81 /* the blkg this per-policy data belongs to */
82 struct blkio_group *blkg;
83
Tejun Heo549d3aa2012-03-05 13:15:16 -080084 /* Configuration */
85 struct blkio_group_conf conf;
86
Tejun Heo03814112012-03-05 13:15:14 -080087 /* pol->pdata_size bytes of private data used by policy impl */
88 char pdata[] __aligned(__alignof__(unsigned long long));
89};
90
Vivek Goyal31e4c282009-12-03 12:59:42 -050091struct blkio_group {
Tejun Heoc875f4d2012-03-05 13:15:22 -080092 /* Pointer to the associated request_queue */
93 struct request_queue *q;
Tejun Heoe8989fa2012-03-05 13:15:20 -080094 struct list_head q_node;
Vivek Goyal31e4c282009-12-03 12:59:42 -050095 struct hlist_node blkcg_node;
Tejun Heo7ee9c562012-03-05 13:15:11 -080096 struct blkio_cgroup *blkcg;
Vivek Goyal2868ef72009-12-03 12:59:48 -050097 /* Store cgroup path */
98 char path[128];
Tejun Heo1adaf3d2012-03-05 13:15:15 -080099 /* reference count */
100 int refcnt;
Vivek Goyal22084192009-12-03 12:59:49 -0500101
Tejun Heo549d3aa2012-03-05 13:15:16 -0800102 struct blkg_policy_data *pd[BLKIO_NR_POLICIES];
Tejun Heo1adaf3d2012-03-05 13:15:15 -0800103
104 struct rcu_head rcu_head;
Vivek Goyal31e4c282009-12-03 12:59:42 -0500105};
106
Tejun Heo03814112012-03-05 13:15:14 -0800107typedef void (blkio_init_group_fn)(struct blkio_group *blkg);
Tejun Heo9ade5ea2012-04-01 14:38:44 -0700108typedef void (blkio_exit_group_fn)(struct blkio_group *blkg);
109typedef void (blkio_reset_group_stats_fn)(struct blkio_group *blkg);
Vivek Goyal3e252062009-12-04 10:36:42 -0500110
111struct blkio_policy_ops {
Tejun Heo03814112012-03-05 13:15:14 -0800112 blkio_init_group_fn *blkio_init_group_fn;
Tejun Heo9ade5ea2012-04-01 14:38:44 -0700113 blkio_exit_group_fn *blkio_exit_group_fn;
114 blkio_reset_group_stats_fn *blkio_reset_group_stats_fn;
Vivek Goyal3e252062009-12-04 10:36:42 -0500115};
116
117struct blkio_policy_type {
118 struct list_head list;
119 struct blkio_policy_ops ops;
Vivek Goyal062a6442010-09-15 17:06:33 -0400120 enum blkio_policy_id plid;
Tejun Heo03814112012-03-05 13:15:14 -0800121 size_t pdata_size; /* policy specific private data size */
Tejun Heo44ea53d2012-04-01 14:38:43 -0700122 struct cftype *cftypes; /* cgroup files for the policy */
Vivek Goyal3e252062009-12-04 10:36:42 -0500123};
124
Tejun Heo5efd6112012-03-05 13:15:12 -0800125extern int blkcg_init_queue(struct request_queue *q);
126extern void blkcg_drain_queue(struct request_queue *q);
127extern void blkcg_exit_queue(struct request_queue *q);
128
Vivek Goyal3e252062009-12-04 10:36:42 -0500129/* Blkio controller policy registration */
130extern void blkio_policy_register(struct blkio_policy_type *);
131extern void blkio_policy_unregister(struct blkio_policy_type *);
Tejun Heoe8989fa2012-03-05 13:15:20 -0800132extern void blkg_destroy_all(struct request_queue *q, bool destroy_root);
133extern void update_root_blkg_pd(struct request_queue *q,
134 enum blkio_policy_id plid);
Vivek Goyal3e252062009-12-04 10:36:42 -0500135
Tejun Heo829fdb52012-04-01 14:38:43 -0700136void blkcg_print_blkgs(struct seq_file *sf, struct blkio_cgroup *blkcg,
137 u64 (*prfill)(struct seq_file *, struct blkg_policy_data *, int),
138 int pol, int data, bool show_total);
139u64 __blkg_prfill_u64(struct seq_file *sf, struct blkg_policy_data *pd, u64 v);
140u64 __blkg_prfill_rwstat(struct seq_file *sf, struct blkg_policy_data *pd,
141 const struct blkg_rwstat *rwstat);
142int blkcg_print_stat(struct cgroup *cgrp, struct cftype *cft,
143 struct seq_file *sf);
144int blkcg_print_rwstat(struct cgroup *cgrp, struct cftype *cft,
145 struct seq_file *sf);
Tejun Heo829fdb52012-04-01 14:38:43 -0700146
147struct blkg_conf_ctx {
148 struct gendisk *disk;
149 struct blkio_group *blkg;
150 u64 v;
151};
152
153int blkg_conf_prep(struct blkio_cgroup *blkcg, const char *input,
154 struct blkg_conf_ctx *ctx);
155void blkg_conf_finish(struct blkg_conf_ctx *ctx);
156
157
Tejun Heo03814112012-03-05 13:15:14 -0800158/**
159 * blkg_to_pdata - get policy private data
160 * @blkg: blkg of interest
161 * @pol: policy of interest
162 *
163 * Return pointer to private data associated with the @blkg-@pol pair.
164 */
165static inline void *blkg_to_pdata(struct blkio_group *blkg,
166 struct blkio_policy_type *pol)
167{
Tejun Heo549d3aa2012-03-05 13:15:16 -0800168 return blkg ? blkg->pd[pol->plid]->pdata : NULL;
Tejun Heo03814112012-03-05 13:15:14 -0800169}
170
171/**
172 * pdata_to_blkg - get blkg associated with policy private data
173 * @pdata: policy private data of interest
Tejun Heo03814112012-03-05 13:15:14 -0800174 *
Tejun Heoaaec55a2012-04-01 14:38:42 -0700175 * @pdata is policy private data. Determine the blkg it's associated with.
Tejun Heo03814112012-03-05 13:15:14 -0800176 */
Tejun Heoaaec55a2012-04-01 14:38:42 -0700177static inline struct blkio_group *pdata_to_blkg(void *pdata)
Tejun Heo03814112012-03-05 13:15:14 -0800178{
179 if (pdata) {
180 struct blkg_policy_data *pd =
181 container_of(pdata, struct blkg_policy_data, pdata);
182 return pd->blkg;
183 }
184 return NULL;
185}
186
Vivek Goyalafc24d42010-04-26 19:27:56 +0200187static inline char *blkg_path(struct blkio_group *blkg)
188{
189 return blkg->path;
190}
191
Tejun Heo1adaf3d2012-03-05 13:15:15 -0800192/**
193 * blkg_get - get a blkg reference
194 * @blkg: blkg to get
195 *
196 * The caller should be holding queue_lock and an existing reference.
197 */
198static inline void blkg_get(struct blkio_group *blkg)
199{
200 lockdep_assert_held(blkg->q->queue_lock);
201 WARN_ON_ONCE(!blkg->refcnt);
202 blkg->refcnt++;
203}
204
205void __blkg_release(struct blkio_group *blkg);
206
207/**
208 * blkg_put - put a blkg reference
209 * @blkg: blkg to put
210 *
211 * The caller should be holding queue_lock.
212 */
213static inline void blkg_put(struct blkio_group *blkg)
214{
215 lockdep_assert_held(blkg->q->queue_lock);
216 WARN_ON_ONCE(blkg->refcnt <= 0);
217 if (!--blkg->refcnt)
218 __blkg_release(blkg);
219}
220
Tejun Heoedcb0722012-04-01 14:38:42 -0700221/**
222 * blkg_stat_add - add a value to a blkg_stat
223 * @stat: target blkg_stat
224 * @val: value to add
225 *
226 * Add @val to @stat. The caller is responsible for synchronizing calls to
227 * this function.
228 */
229static inline void blkg_stat_add(struct blkg_stat *stat, uint64_t val)
230{
231 u64_stats_update_begin(&stat->syncp);
232 stat->cnt += val;
233 u64_stats_update_end(&stat->syncp);
234}
235
236/**
237 * blkg_stat_read - read the current value of a blkg_stat
238 * @stat: blkg_stat to read
239 *
240 * Read the current value of @stat. This function can be called without
241 * synchroniztion and takes care of u64 atomicity.
242 */
243static inline uint64_t blkg_stat_read(struct blkg_stat *stat)
244{
245 unsigned int start;
246 uint64_t v;
247
248 do {
249 start = u64_stats_fetch_begin(&stat->syncp);
250 v = stat->cnt;
251 } while (u64_stats_fetch_retry(&stat->syncp, start));
252
253 return v;
254}
255
256/**
257 * blkg_stat_reset - reset a blkg_stat
258 * @stat: blkg_stat to reset
259 */
260static inline void blkg_stat_reset(struct blkg_stat *stat)
261{
262 stat->cnt = 0;
263}
264
265/**
266 * blkg_rwstat_add - add a value to a blkg_rwstat
267 * @rwstat: target blkg_rwstat
268 * @rw: mask of REQ_{WRITE|SYNC}
269 * @val: value to add
270 *
271 * Add @val to @rwstat. The counters are chosen according to @rw. The
272 * caller is responsible for synchronizing calls to this function.
273 */
274static inline void blkg_rwstat_add(struct blkg_rwstat *rwstat,
275 int rw, uint64_t val)
276{
277 u64_stats_update_begin(&rwstat->syncp);
278
279 if (rw & REQ_WRITE)
280 rwstat->cnt[BLKG_RWSTAT_WRITE] += val;
281 else
282 rwstat->cnt[BLKG_RWSTAT_READ] += val;
283 if (rw & REQ_SYNC)
284 rwstat->cnt[BLKG_RWSTAT_SYNC] += val;
285 else
286 rwstat->cnt[BLKG_RWSTAT_ASYNC] += val;
287
288 u64_stats_update_end(&rwstat->syncp);
289}
290
291/**
292 * blkg_rwstat_read - read the current values of a blkg_rwstat
293 * @rwstat: blkg_rwstat to read
294 *
295 * Read the current snapshot of @rwstat and return it as the return value.
296 * This function can be called without synchronization and takes care of
297 * u64 atomicity.
298 */
299static struct blkg_rwstat blkg_rwstat_read(struct blkg_rwstat *rwstat)
300{
301 unsigned int start;
302 struct blkg_rwstat tmp;
303
304 do {
305 start = u64_stats_fetch_begin(&rwstat->syncp);
306 tmp = *rwstat;
307 } while (u64_stats_fetch_retry(&rwstat->syncp, start));
308
309 return tmp;
310}
311
312/**
313 * blkg_rwstat_sum - read the total count of a blkg_rwstat
314 * @rwstat: blkg_rwstat to read
315 *
316 * Return the total count of @rwstat regardless of the IO direction. This
317 * function can be called without synchronization and takes care of u64
318 * atomicity.
319 */
320static inline uint64_t blkg_rwstat_sum(struct blkg_rwstat *rwstat)
321{
322 struct blkg_rwstat tmp = blkg_rwstat_read(rwstat);
323
324 return tmp.cnt[BLKG_RWSTAT_READ] + tmp.cnt[BLKG_RWSTAT_WRITE];
325}
326
327/**
328 * blkg_rwstat_reset - reset a blkg_rwstat
329 * @rwstat: blkg_rwstat to reset
330 */
331static inline void blkg_rwstat_reset(struct blkg_rwstat *rwstat)
332{
333 memset(rwstat->cnt, 0, sizeof(rwstat->cnt));
334}
335
Jens Axboe2f5ea472009-12-03 21:06:43 +0100336#else
337
338struct blkio_group {
339};
340
Vivek Goyal3e252062009-12-04 10:36:42 -0500341struct blkio_policy_type {
342};
343
Tejun Heo5efd6112012-03-05 13:15:12 -0800344static inline int blkcg_init_queue(struct request_queue *q) { return 0; }
345static inline void blkcg_drain_queue(struct request_queue *q) { }
346static inline void blkcg_exit_queue(struct request_queue *q) { }
Vivek Goyal3e252062009-12-04 10:36:42 -0500347static inline void blkio_policy_register(struct blkio_policy_type *blkiop) { }
348static inline void blkio_policy_unregister(struct blkio_policy_type *blkiop) { }
Tejun Heo03aa2642012-03-05 13:15:19 -0800349static inline void blkg_destroy_all(struct request_queue *q,
Tejun Heo03aa2642012-03-05 13:15:19 -0800350 bool destory_root) { }
Tejun Heoe8989fa2012-03-05 13:15:20 -0800351static inline void update_root_blkg_pd(struct request_queue *q,
352 enum blkio_policy_id plid) { }
Vivek Goyal3e252062009-12-04 10:36:42 -0500353
Tejun Heo03814112012-03-05 13:15:14 -0800354static inline void *blkg_to_pdata(struct blkio_group *blkg,
355 struct blkio_policy_type *pol) { return NULL; }
356static inline struct blkio_group *pdata_to_blkg(void *pdata,
357 struct blkio_policy_type *pol) { return NULL; }
Vivek Goyalafc24d42010-04-26 19:27:56 +0200358static inline char *blkg_path(struct blkio_group *blkg) { return NULL; }
Tejun Heo1adaf3d2012-03-05 13:15:15 -0800359static inline void blkg_get(struct blkio_group *blkg) { }
360static inline void blkg_put(struct blkio_group *blkg) { }
Vivek Goyalafc24d42010-04-26 19:27:56 +0200361
Jens Axboe2f5ea472009-12-03 21:06:43 +0100362#endif
363
Tejun Heo32e380a2012-03-05 13:14:54 -0800364#ifdef CONFIG_BLK_CGROUP
Vivek Goyal31e4c282009-12-03 12:59:42 -0500365extern struct blkio_cgroup blkio_root_cgroup;
366extern struct blkio_cgroup *cgroup_to_blkio_cgroup(struct cgroup *cgroup);
Tejun Heo4f85cb92012-03-05 13:15:28 -0800367extern struct blkio_cgroup *bio_blkio_cgroup(struct bio *bio);
Tejun Heocd1604f2012-03-05 13:15:06 -0800368extern struct blkio_group *blkg_lookup(struct blkio_cgroup *blkcg,
Tejun Heoe8989fa2012-03-05 13:15:20 -0800369 struct request_queue *q);
Tejun Heocd1604f2012-03-05 13:15:06 -0800370struct blkio_group *blkg_lookup_create(struct blkio_cgroup *blkcg,
371 struct request_queue *q,
Tejun Heocd1604f2012-03-05 13:15:06 -0800372 bool for_root);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500373#else
Jens Axboe2f5ea472009-12-03 21:06:43 +0100374struct cgroup;
Vivek Goyal31e4c282009-12-03 12:59:42 -0500375static inline struct blkio_cgroup *
376cgroup_to_blkio_cgroup(struct cgroup *cgroup) { return NULL; }
Vivek Goyal70087dc2011-05-16 15:24:08 +0200377static inline struct blkio_cgroup *
Tejun Heo4f85cb92012-03-05 13:15:28 -0800378bio_blkio_cgroup(struct bio *bio) { return NULL; }
Vivek Goyal31e4c282009-12-03 12:59:42 -0500379
Tejun Heocd1604f2012-03-05 13:15:06 -0800380static inline struct blkio_group *blkg_lookup(struct blkio_cgroup *blkcg,
381 void *key) { return NULL; }
Vivek Goyal31e4c282009-12-03 12:59:42 -0500382#endif
383#endif /* _BLK_CGROUP_H */