blob: be80d6eb6531d4880f37ff1611c5a2eab6a7affd [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 Goyal9355aed2010-10-01 21:16:41 +020020/* Max limits for throttle policy */
21#define THROTL_IOPS_MAX UINT_MAX
22
Tejun Heo3381cb82012-04-01 14:38:44 -070023/* CFQ specific, out here for blkcg->cfq_weight */
24#define CFQ_WEIGHT_MIN 10
25#define CFQ_WEIGHT_MAX 1000
26#define CFQ_WEIGHT_DEFAULT 500
27
Tejun Heof48ec1d2012-04-13 13:11:25 -070028#ifdef CONFIG_BLK_CGROUP
29
Tejun Heoedcb0722012-04-01 14:38:42 -070030enum blkg_rwstat_type {
31 BLKG_RWSTAT_READ,
32 BLKG_RWSTAT_WRITE,
33 BLKG_RWSTAT_SYNC,
34 BLKG_RWSTAT_ASYNC,
35
36 BLKG_RWSTAT_NR,
37 BLKG_RWSTAT_TOTAL = BLKG_RWSTAT_NR,
Divyesh Shah303a3ac2010-04-01 15:01:24 -070038};
39
Vivek Goyal31e4c282009-12-03 12:59:42 -050040struct blkio_cgroup {
41 struct cgroup_subsys_state css;
Vivek Goyal31e4c282009-12-03 12:59:42 -050042 spinlock_t lock;
43 struct hlist_head blkg_list;
Tejun Heo9a9e8a22012-03-19 15:10:56 -070044
45 /* for policies to test whether associated blkcg has changed */
46 uint64_t id;
Tejun Heo3381cb82012-04-01 14:38:44 -070047
48 /* TODO: per-policy storage in blkio_cgroup */
49 unsigned int cfq_weight; /* belongs to cfq */
Vivek Goyal31e4c282009-12-03 12:59:42 -050050};
51
Tejun Heoedcb0722012-04-01 14:38:42 -070052struct blkg_stat {
53 struct u64_stats_sync syncp;
54 uint64_t cnt;
55};
56
57struct blkg_rwstat {
58 struct u64_stats_sync syncp;
59 uint64_t cnt[BLKG_RWSTAT_NR];
60};
61
Tejun Heo03814112012-03-05 13:15:14 -080062/* per-blkg per-policy data */
63struct blkg_policy_data {
64 /* the blkg this per-policy data belongs to */
65 struct blkio_group *blkg;
66
67 /* pol->pdata_size bytes of private data used by policy impl */
68 char pdata[] __aligned(__alignof__(unsigned long long));
69};
70
Vivek Goyal31e4c282009-12-03 12:59:42 -050071struct blkio_group {
Tejun Heoc875f4d2012-03-05 13:15:22 -080072 /* Pointer to the associated request_queue */
73 struct request_queue *q;
Tejun Heoe8989fa2012-03-05 13:15:20 -080074 struct list_head q_node;
Vivek Goyal31e4c282009-12-03 12:59:42 -050075 struct hlist_node blkcg_node;
Tejun Heo7ee9c562012-03-05 13:15:11 -080076 struct blkio_cgroup *blkcg;
Vivek Goyal2868ef72009-12-03 12:59:48 -050077 /* Store cgroup path */
78 char path[128];
Tejun Heo1adaf3d2012-03-05 13:15:15 -080079 /* reference count */
80 int refcnt;
Vivek Goyal22084192009-12-03 12:59:49 -050081
Tejun Heo8bd435b2012-04-13 13:11:28 -070082 struct blkg_policy_data *pd[BLKCG_MAX_POLS];
Tejun Heo1adaf3d2012-03-05 13:15:15 -080083
84 struct rcu_head rcu_head;
Vivek Goyal31e4c282009-12-03 12:59:42 -050085};
86
Tejun Heo03814112012-03-05 13:15:14 -080087typedef void (blkio_init_group_fn)(struct blkio_group *blkg);
Tejun Heo9ade5ea2012-04-01 14:38:44 -070088typedef void (blkio_exit_group_fn)(struct blkio_group *blkg);
89typedef void (blkio_reset_group_stats_fn)(struct blkio_group *blkg);
Vivek Goyal3e252062009-12-04 10:36:42 -050090
91struct blkio_policy_ops {
Tejun Heo03814112012-03-05 13:15:14 -080092 blkio_init_group_fn *blkio_init_group_fn;
Tejun Heo9ade5ea2012-04-01 14:38:44 -070093 blkio_exit_group_fn *blkio_exit_group_fn;
94 blkio_reset_group_stats_fn *blkio_reset_group_stats_fn;
Vivek Goyal3e252062009-12-04 10:36:42 -050095};
96
97struct blkio_policy_type {
Vivek Goyal3e252062009-12-04 10:36:42 -050098 struct blkio_policy_ops ops;
Tejun Heo8bd435b2012-04-13 13:11:28 -070099 int plid;
Tejun Heo03814112012-03-05 13:15:14 -0800100 size_t pdata_size; /* policy specific private data size */
Tejun Heo44ea53d2012-04-01 14:38:43 -0700101 struct cftype *cftypes; /* cgroup files for the policy */
Vivek Goyal3e252062009-12-04 10:36:42 -0500102};
103
Tejun Heo5efd6112012-03-05 13:15:12 -0800104extern int blkcg_init_queue(struct request_queue *q);
105extern void blkcg_drain_queue(struct request_queue *q);
106extern void blkcg_exit_queue(struct request_queue *q);
107
Vivek Goyal3e252062009-12-04 10:36:42 -0500108/* Blkio controller policy registration */
Tejun Heo8bd435b2012-04-13 13:11:28 -0700109extern int blkio_policy_register(struct blkio_policy_type *);
Vivek Goyal3e252062009-12-04 10:36:42 -0500110extern void blkio_policy_unregister(struct blkio_policy_type *);
Tejun Heoe8989fa2012-03-05 13:15:20 -0800111extern void blkg_destroy_all(struct request_queue *q, bool destroy_root);
112extern void update_root_blkg_pd(struct request_queue *q,
Tejun Heoec399342012-04-13 13:11:27 -0700113 const struct blkio_policy_type *pol);
Vivek Goyal3e252062009-12-04 10:36:42 -0500114
Tejun Heo829fdb52012-04-01 14:38:43 -0700115void blkcg_print_blkgs(struct seq_file *sf, struct blkio_cgroup *blkcg,
Tejun Heod366e7e2012-04-01 14:38:44 -0700116 u64 (*prfill)(struct seq_file *, void *, int),
Tejun Heoec399342012-04-13 13:11:27 -0700117 const struct blkio_policy_type *pol, int data,
118 bool show_total);
Tejun Heod366e7e2012-04-01 14:38:44 -0700119u64 __blkg_prfill_u64(struct seq_file *sf, void *pdata, u64 v);
120u64 __blkg_prfill_rwstat(struct seq_file *sf, void *pdata,
Tejun Heo829fdb52012-04-01 14:38:43 -0700121 const struct blkg_rwstat *rwstat);
Tejun Heo5bc4afb12012-04-01 14:38:45 -0700122u64 blkg_prfill_stat(struct seq_file *sf, void *pdata, int off);
123u64 blkg_prfill_rwstat(struct seq_file *sf, void *pdata, int off);
Tejun Heo829fdb52012-04-01 14:38:43 -0700124
125struct blkg_conf_ctx {
126 struct gendisk *disk;
127 struct blkio_group *blkg;
128 u64 v;
129};
130
131int blkg_conf_prep(struct blkio_cgroup *blkcg, const char *input,
132 struct blkg_conf_ctx *ctx);
133void blkg_conf_finish(struct blkg_conf_ctx *ctx);
134
135
Tejun Heo03814112012-03-05 13:15:14 -0800136/**
137 * blkg_to_pdata - get policy private data
138 * @blkg: blkg of interest
139 * @pol: policy of interest
140 *
141 * Return pointer to private data associated with the @blkg-@pol pair.
142 */
143static inline void *blkg_to_pdata(struct blkio_group *blkg,
144 struct blkio_policy_type *pol)
145{
Tejun Heo549d3aa2012-03-05 13:15:16 -0800146 return blkg ? blkg->pd[pol->plid]->pdata : NULL;
Tejun Heo03814112012-03-05 13:15:14 -0800147}
148
149/**
150 * pdata_to_blkg - get blkg associated with policy private data
151 * @pdata: policy private data of interest
Tejun Heo03814112012-03-05 13:15:14 -0800152 *
Tejun Heoaaec55a2012-04-01 14:38:42 -0700153 * @pdata is policy private data. Determine the blkg it's associated with.
Tejun Heo03814112012-03-05 13:15:14 -0800154 */
Tejun Heoaaec55a2012-04-01 14:38:42 -0700155static inline struct blkio_group *pdata_to_blkg(void *pdata)
Tejun Heo03814112012-03-05 13:15:14 -0800156{
157 if (pdata) {
158 struct blkg_policy_data *pd =
159 container_of(pdata, struct blkg_policy_data, pdata);
160 return pd->blkg;
161 }
162 return NULL;
163}
164
Vivek Goyalafc24d42010-04-26 19:27:56 +0200165static inline char *blkg_path(struct blkio_group *blkg)
166{
167 return blkg->path;
168}
169
Tejun Heo1adaf3d2012-03-05 13:15:15 -0800170/**
171 * blkg_get - get a blkg reference
172 * @blkg: blkg to get
173 *
174 * The caller should be holding queue_lock and an existing reference.
175 */
176static inline void blkg_get(struct blkio_group *blkg)
177{
178 lockdep_assert_held(blkg->q->queue_lock);
179 WARN_ON_ONCE(!blkg->refcnt);
180 blkg->refcnt++;
181}
182
183void __blkg_release(struct blkio_group *blkg);
184
185/**
186 * blkg_put - put a blkg reference
187 * @blkg: blkg to put
188 *
189 * The caller should be holding queue_lock.
190 */
191static inline void blkg_put(struct blkio_group *blkg)
192{
193 lockdep_assert_held(blkg->q->queue_lock);
194 WARN_ON_ONCE(blkg->refcnt <= 0);
195 if (!--blkg->refcnt)
196 __blkg_release(blkg);
197}
198
Tejun Heoedcb0722012-04-01 14:38:42 -0700199/**
200 * blkg_stat_add - add a value to a blkg_stat
201 * @stat: target blkg_stat
202 * @val: value to add
203 *
204 * Add @val to @stat. The caller is responsible for synchronizing calls to
205 * this function.
206 */
207static inline void blkg_stat_add(struct blkg_stat *stat, uint64_t val)
208{
209 u64_stats_update_begin(&stat->syncp);
210 stat->cnt += val;
211 u64_stats_update_end(&stat->syncp);
212}
213
214/**
215 * blkg_stat_read - read the current value of a blkg_stat
216 * @stat: blkg_stat to read
217 *
218 * Read the current value of @stat. This function can be called without
219 * synchroniztion and takes care of u64 atomicity.
220 */
221static inline uint64_t blkg_stat_read(struct blkg_stat *stat)
222{
223 unsigned int start;
224 uint64_t v;
225
226 do {
227 start = u64_stats_fetch_begin(&stat->syncp);
228 v = stat->cnt;
229 } while (u64_stats_fetch_retry(&stat->syncp, start));
230
231 return v;
232}
233
234/**
235 * blkg_stat_reset - reset a blkg_stat
236 * @stat: blkg_stat to reset
237 */
238static inline void blkg_stat_reset(struct blkg_stat *stat)
239{
240 stat->cnt = 0;
241}
242
243/**
244 * blkg_rwstat_add - add a value to a blkg_rwstat
245 * @rwstat: target blkg_rwstat
246 * @rw: mask of REQ_{WRITE|SYNC}
247 * @val: value to add
248 *
249 * Add @val to @rwstat. The counters are chosen according to @rw. The
250 * caller is responsible for synchronizing calls to this function.
251 */
252static inline void blkg_rwstat_add(struct blkg_rwstat *rwstat,
253 int rw, uint64_t val)
254{
255 u64_stats_update_begin(&rwstat->syncp);
256
257 if (rw & REQ_WRITE)
258 rwstat->cnt[BLKG_RWSTAT_WRITE] += val;
259 else
260 rwstat->cnt[BLKG_RWSTAT_READ] += val;
261 if (rw & REQ_SYNC)
262 rwstat->cnt[BLKG_RWSTAT_SYNC] += val;
263 else
264 rwstat->cnt[BLKG_RWSTAT_ASYNC] += val;
265
266 u64_stats_update_end(&rwstat->syncp);
267}
268
269/**
270 * blkg_rwstat_read - read the current values of a blkg_rwstat
271 * @rwstat: blkg_rwstat to read
272 *
273 * Read the current snapshot of @rwstat and return it as the return value.
274 * This function can be called without synchronization and takes care of
275 * u64 atomicity.
276 */
277static struct blkg_rwstat blkg_rwstat_read(struct blkg_rwstat *rwstat)
278{
279 unsigned int start;
280 struct blkg_rwstat tmp;
281
282 do {
283 start = u64_stats_fetch_begin(&rwstat->syncp);
284 tmp = *rwstat;
285 } while (u64_stats_fetch_retry(&rwstat->syncp, start));
286
287 return tmp;
288}
289
290/**
291 * blkg_rwstat_sum - read the total count of a blkg_rwstat
292 * @rwstat: blkg_rwstat to read
293 *
294 * Return the total count of @rwstat regardless of the IO direction. This
295 * function can be called without synchronization and takes care of u64
296 * atomicity.
297 */
298static inline uint64_t blkg_rwstat_sum(struct blkg_rwstat *rwstat)
299{
300 struct blkg_rwstat tmp = blkg_rwstat_read(rwstat);
301
302 return tmp.cnt[BLKG_RWSTAT_READ] + tmp.cnt[BLKG_RWSTAT_WRITE];
303}
304
305/**
306 * blkg_rwstat_reset - reset a blkg_rwstat
307 * @rwstat: blkg_rwstat to reset
308 */
309static inline void blkg_rwstat_reset(struct blkg_rwstat *rwstat)
310{
311 memset(rwstat->cnt, 0, sizeof(rwstat->cnt));
312}
313
Jens Axboe2f5ea472009-12-03 21:06:43 +0100314#else
315
316struct blkio_group {
317};
318
Vivek Goyal3e252062009-12-04 10:36:42 -0500319struct blkio_policy_type {
320};
321
Tejun Heo5efd6112012-03-05 13:15:12 -0800322static inline int blkcg_init_queue(struct request_queue *q) { return 0; }
323static inline void blkcg_drain_queue(struct request_queue *q) { }
324static inline void blkcg_exit_queue(struct request_queue *q) { }
Tejun Heo8bd435b2012-04-13 13:11:28 -0700325static inline int blkio_policy_register(struct blkio_policy_type *blkiop) { return 0; }
Vivek Goyal3e252062009-12-04 10:36:42 -0500326static inline void blkio_policy_unregister(struct blkio_policy_type *blkiop) { }
Tejun Heo03aa264a2012-03-05 13:15:19 -0800327static inline void blkg_destroy_all(struct request_queue *q,
Tejun Heo03aa264a2012-03-05 13:15:19 -0800328 bool destory_root) { }
Tejun Heoe8989fa2012-03-05 13:15:20 -0800329static inline void update_root_blkg_pd(struct request_queue *q,
Tejun Heoec399342012-04-13 13:11:27 -0700330 const struct blkio_policy_type *pol) { }
Vivek Goyal3e252062009-12-04 10:36:42 -0500331
Tejun Heo03814112012-03-05 13:15:14 -0800332static inline void *blkg_to_pdata(struct blkio_group *blkg,
333 struct blkio_policy_type *pol) { return NULL; }
334static inline struct blkio_group *pdata_to_blkg(void *pdata,
335 struct blkio_policy_type *pol) { return NULL; }
Vivek Goyalafc24d42010-04-26 19:27:56 +0200336static inline char *blkg_path(struct blkio_group *blkg) { return NULL; }
Tejun Heo1adaf3d2012-03-05 13:15:15 -0800337static inline void blkg_get(struct blkio_group *blkg) { }
338static inline void blkg_put(struct blkio_group *blkg) { }
Vivek Goyalafc24d42010-04-26 19:27:56 +0200339
Jens Axboe2f5ea472009-12-03 21:06:43 +0100340#endif
341
Tejun Heo32e380a2012-03-05 13:14:54 -0800342#ifdef CONFIG_BLK_CGROUP
Vivek Goyal31e4c282009-12-03 12:59:42 -0500343extern struct blkio_cgroup blkio_root_cgroup;
344extern struct blkio_cgroup *cgroup_to_blkio_cgroup(struct cgroup *cgroup);
Tejun Heo4f85cb92012-03-05 13:15:28 -0800345extern struct blkio_cgroup *bio_blkio_cgroup(struct bio *bio);
Tejun Heocd1604f2012-03-05 13:15:06 -0800346extern struct blkio_group *blkg_lookup(struct blkio_cgroup *blkcg,
Tejun Heoe8989fa2012-03-05 13:15:20 -0800347 struct request_queue *q);
Tejun Heocd1604f2012-03-05 13:15:06 -0800348struct blkio_group *blkg_lookup_create(struct blkio_cgroup *blkcg,
349 struct request_queue *q,
Tejun Heocd1604f2012-03-05 13:15:06 -0800350 bool for_root);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500351#else
Jens Axboe2f5ea472009-12-03 21:06:43 +0100352struct cgroup;
Vivek Goyal31e4c282009-12-03 12:59:42 -0500353static inline struct blkio_cgroup *
354cgroup_to_blkio_cgroup(struct cgroup *cgroup) { return NULL; }
Vivek Goyal70087dc2011-05-16 15:24:08 +0200355static inline struct blkio_cgroup *
Tejun Heo4f85cb92012-03-05 13:15:28 -0800356bio_blkio_cgroup(struct bio *bio) { return NULL; }
Vivek Goyal31e4c282009-12-03 12:59:42 -0500357
Tejun Heocd1604f2012-03-05 13:15:06 -0800358static inline struct blkio_group *blkg_lookup(struct blkio_cgroup *blkcg,
359 void *key) { return NULL; }
Vivek Goyal31e4c282009-12-03 12:59:42 -0500360#endif
361#endif /* _BLK_CGROUP_H */