blob: 66253a7c8ff4cb90683b2092edb8239cc7eb5124 [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
Tejun Heoa2b16932012-04-13 13:11:33 -070067 /* used during policy activation */
68 struct list_head alloc_node;
69
Tejun Heo03814112012-03-05 13:15:14 -080070 /* pol->pdata_size bytes of private data used by policy impl */
71 char pdata[] __aligned(__alignof__(unsigned long long));
72};
73
Vivek Goyal31e4c282009-12-03 12:59:42 -050074struct blkio_group {
Tejun Heoc875f4d2012-03-05 13:15:22 -080075 /* Pointer to the associated request_queue */
76 struct request_queue *q;
Tejun Heoe8989fa2012-03-05 13:15:20 -080077 struct list_head q_node;
Vivek Goyal31e4c282009-12-03 12:59:42 -050078 struct hlist_node blkcg_node;
Tejun Heo7ee9c562012-03-05 13:15:11 -080079 struct blkio_cgroup *blkcg;
Vivek Goyal2868ef72009-12-03 12:59:48 -050080 /* Store cgroup path */
81 char path[128];
Tejun Heo1adaf3d2012-03-05 13:15:15 -080082 /* reference count */
83 int refcnt;
Vivek Goyal22084192009-12-03 12:59:49 -050084
Tejun Heo8bd435b2012-04-13 13:11:28 -070085 struct blkg_policy_data *pd[BLKCG_MAX_POLS];
Tejun Heo1adaf3d2012-03-05 13:15:15 -080086
87 struct rcu_head rcu_head;
Vivek Goyal31e4c282009-12-03 12:59:42 -050088};
89
Tejun Heo03814112012-03-05 13:15:14 -080090typedef void (blkio_init_group_fn)(struct blkio_group *blkg);
Tejun Heo9ade5ea2012-04-01 14:38:44 -070091typedef void (blkio_exit_group_fn)(struct blkio_group *blkg);
92typedef void (blkio_reset_group_stats_fn)(struct blkio_group *blkg);
Vivek Goyal3e252062009-12-04 10:36:42 -050093
94struct blkio_policy_ops {
Tejun Heo03814112012-03-05 13:15:14 -080095 blkio_init_group_fn *blkio_init_group_fn;
Tejun Heo9ade5ea2012-04-01 14:38:44 -070096 blkio_exit_group_fn *blkio_exit_group_fn;
97 blkio_reset_group_stats_fn *blkio_reset_group_stats_fn;
Vivek Goyal3e252062009-12-04 10:36:42 -050098};
99
100struct blkio_policy_type {
Vivek Goyal3e252062009-12-04 10:36:42 -0500101 struct blkio_policy_ops ops;
Tejun Heo8bd435b2012-04-13 13:11:28 -0700102 int plid;
Tejun Heo03814112012-03-05 13:15:14 -0800103 size_t pdata_size; /* policy specific private data size */
Tejun Heo44ea53d2012-04-01 14:38:43 -0700104 struct cftype *cftypes; /* cgroup files for the policy */
Vivek Goyal3e252062009-12-04 10:36:42 -0500105};
106
Tejun Heo5efd6112012-03-05 13:15:12 -0800107extern int blkcg_init_queue(struct request_queue *q);
108extern void blkcg_drain_queue(struct request_queue *q);
109extern void blkcg_exit_queue(struct request_queue *q);
110
Vivek Goyal3e252062009-12-04 10:36:42 -0500111/* Blkio controller policy registration */
Tejun Heo8bd435b2012-04-13 13:11:28 -0700112extern int blkio_policy_register(struct blkio_policy_type *);
Vivek Goyal3e252062009-12-04 10:36:42 -0500113extern void blkio_policy_unregister(struct blkio_policy_type *);
Tejun Heoa2b16932012-04-13 13:11:33 -0700114extern int blkcg_activate_policy(struct request_queue *q,
115 const struct blkio_policy_type *pol);
116extern void blkcg_deactivate_policy(struct request_queue *q,
117 const struct blkio_policy_type *pol);
Tejun Heoe8989fa2012-03-05 13:15:20 -0800118extern void blkg_destroy_all(struct request_queue *q, bool destroy_root);
Vivek Goyal3e252062009-12-04 10:36:42 -0500119
Tejun Heo829fdb52012-04-01 14:38:43 -0700120void blkcg_print_blkgs(struct seq_file *sf, struct blkio_cgroup *blkcg,
Tejun Heod366e7e2012-04-01 14:38:44 -0700121 u64 (*prfill)(struct seq_file *, void *, int),
Tejun Heoec399342012-04-13 13:11:27 -0700122 const struct blkio_policy_type *pol, int data,
123 bool show_total);
Tejun Heod366e7e2012-04-01 14:38:44 -0700124u64 __blkg_prfill_u64(struct seq_file *sf, void *pdata, u64 v);
125u64 __blkg_prfill_rwstat(struct seq_file *sf, void *pdata,
Tejun Heo829fdb52012-04-01 14:38:43 -0700126 const struct blkg_rwstat *rwstat);
Tejun Heo5bc4afb12012-04-01 14:38:45 -0700127u64 blkg_prfill_stat(struct seq_file *sf, void *pdata, int off);
128u64 blkg_prfill_rwstat(struct seq_file *sf, void *pdata, int off);
Tejun Heo829fdb52012-04-01 14:38:43 -0700129
130struct blkg_conf_ctx {
131 struct gendisk *disk;
132 struct blkio_group *blkg;
133 u64 v;
134};
135
Tejun Heoda8b0662012-04-13 13:11:29 -0700136int blkg_conf_prep(struct blkio_cgroup *blkcg,
137 const struct blkio_policy_type *pol, const char *input,
Tejun Heo829fdb52012-04-01 14:38:43 -0700138 struct blkg_conf_ctx *ctx);
139void blkg_conf_finish(struct blkg_conf_ctx *ctx);
140
141
Tejun Heo03814112012-03-05 13:15:14 -0800142/**
143 * blkg_to_pdata - get policy private data
144 * @blkg: blkg of interest
145 * @pol: policy of interest
146 *
147 * Return pointer to private data associated with the @blkg-@pol pair.
148 */
149static inline void *blkg_to_pdata(struct blkio_group *blkg,
150 struct blkio_policy_type *pol)
151{
Tejun Heo549d3aa2012-03-05 13:15:16 -0800152 return blkg ? blkg->pd[pol->plid]->pdata : NULL;
Tejun Heo03814112012-03-05 13:15:14 -0800153}
154
155/**
156 * pdata_to_blkg - get blkg associated with policy private data
157 * @pdata: policy private data of interest
Tejun Heo03814112012-03-05 13:15:14 -0800158 *
Tejun Heoaaec55a2012-04-01 14:38:42 -0700159 * @pdata is policy private data. Determine the blkg it's associated with.
Tejun Heo03814112012-03-05 13:15:14 -0800160 */
Tejun Heoaaec55a2012-04-01 14:38:42 -0700161static inline struct blkio_group *pdata_to_blkg(void *pdata)
Tejun Heo03814112012-03-05 13:15:14 -0800162{
163 if (pdata) {
164 struct blkg_policy_data *pd =
165 container_of(pdata, struct blkg_policy_data, pdata);
166 return pd->blkg;
167 }
168 return NULL;
169}
170
Vivek Goyalafc24d42010-04-26 19:27:56 +0200171static inline char *blkg_path(struct blkio_group *blkg)
172{
173 return blkg->path;
174}
175
Tejun Heo1adaf3d2012-03-05 13:15:15 -0800176/**
177 * blkg_get - get a blkg reference
178 * @blkg: blkg to get
179 *
180 * The caller should be holding queue_lock and an existing reference.
181 */
182static inline void blkg_get(struct blkio_group *blkg)
183{
184 lockdep_assert_held(blkg->q->queue_lock);
185 WARN_ON_ONCE(!blkg->refcnt);
186 blkg->refcnt++;
187}
188
189void __blkg_release(struct blkio_group *blkg);
190
191/**
192 * blkg_put - put a blkg reference
193 * @blkg: blkg to put
194 *
195 * The caller should be holding queue_lock.
196 */
197static inline void blkg_put(struct blkio_group *blkg)
198{
199 lockdep_assert_held(blkg->q->queue_lock);
200 WARN_ON_ONCE(blkg->refcnt <= 0);
201 if (!--blkg->refcnt)
202 __blkg_release(blkg);
203}
204
Tejun Heoedcb0722012-04-01 14:38:42 -0700205/**
206 * blkg_stat_add - add a value to a blkg_stat
207 * @stat: target blkg_stat
208 * @val: value to add
209 *
210 * Add @val to @stat. The caller is responsible for synchronizing calls to
211 * this function.
212 */
213static inline void blkg_stat_add(struct blkg_stat *stat, uint64_t val)
214{
215 u64_stats_update_begin(&stat->syncp);
216 stat->cnt += val;
217 u64_stats_update_end(&stat->syncp);
218}
219
220/**
221 * blkg_stat_read - read the current value of a blkg_stat
222 * @stat: blkg_stat to read
223 *
224 * Read the current value of @stat. This function can be called without
225 * synchroniztion and takes care of u64 atomicity.
226 */
227static inline uint64_t blkg_stat_read(struct blkg_stat *stat)
228{
229 unsigned int start;
230 uint64_t v;
231
232 do {
233 start = u64_stats_fetch_begin(&stat->syncp);
234 v = stat->cnt;
235 } while (u64_stats_fetch_retry(&stat->syncp, start));
236
237 return v;
238}
239
240/**
241 * blkg_stat_reset - reset a blkg_stat
242 * @stat: blkg_stat to reset
243 */
244static inline void blkg_stat_reset(struct blkg_stat *stat)
245{
246 stat->cnt = 0;
247}
248
249/**
250 * blkg_rwstat_add - add a value to a blkg_rwstat
251 * @rwstat: target blkg_rwstat
252 * @rw: mask of REQ_{WRITE|SYNC}
253 * @val: value to add
254 *
255 * Add @val to @rwstat. The counters are chosen according to @rw. The
256 * caller is responsible for synchronizing calls to this function.
257 */
258static inline void blkg_rwstat_add(struct blkg_rwstat *rwstat,
259 int rw, uint64_t val)
260{
261 u64_stats_update_begin(&rwstat->syncp);
262
263 if (rw & REQ_WRITE)
264 rwstat->cnt[BLKG_RWSTAT_WRITE] += val;
265 else
266 rwstat->cnt[BLKG_RWSTAT_READ] += val;
267 if (rw & REQ_SYNC)
268 rwstat->cnt[BLKG_RWSTAT_SYNC] += val;
269 else
270 rwstat->cnt[BLKG_RWSTAT_ASYNC] += val;
271
272 u64_stats_update_end(&rwstat->syncp);
273}
274
275/**
276 * blkg_rwstat_read - read the current values of a blkg_rwstat
277 * @rwstat: blkg_rwstat to read
278 *
279 * Read the current snapshot of @rwstat and return it as the return value.
280 * This function can be called without synchronization and takes care of
281 * u64 atomicity.
282 */
283static struct blkg_rwstat blkg_rwstat_read(struct blkg_rwstat *rwstat)
284{
285 unsigned int start;
286 struct blkg_rwstat tmp;
287
288 do {
289 start = u64_stats_fetch_begin(&rwstat->syncp);
290 tmp = *rwstat;
291 } while (u64_stats_fetch_retry(&rwstat->syncp, start));
292
293 return tmp;
294}
295
296/**
297 * blkg_rwstat_sum - read the total count of a blkg_rwstat
298 * @rwstat: blkg_rwstat to read
299 *
300 * Return the total count of @rwstat regardless of the IO direction. This
301 * function can be called without synchronization and takes care of u64
302 * atomicity.
303 */
304static inline uint64_t blkg_rwstat_sum(struct blkg_rwstat *rwstat)
305{
306 struct blkg_rwstat tmp = blkg_rwstat_read(rwstat);
307
308 return tmp.cnt[BLKG_RWSTAT_READ] + tmp.cnt[BLKG_RWSTAT_WRITE];
309}
310
311/**
312 * blkg_rwstat_reset - reset a blkg_rwstat
313 * @rwstat: blkg_rwstat to reset
314 */
315static inline void blkg_rwstat_reset(struct blkg_rwstat *rwstat)
316{
317 memset(rwstat->cnt, 0, sizeof(rwstat->cnt));
318}
319
Jens Axboe2f5ea472009-12-03 21:06:43 +0100320#else
321
322struct blkio_group {
323};
324
Vivek Goyal3e252062009-12-04 10:36:42 -0500325struct blkio_policy_type {
326};
327
Tejun Heo5efd6112012-03-05 13:15:12 -0800328static inline int blkcg_init_queue(struct request_queue *q) { return 0; }
329static inline void blkcg_drain_queue(struct request_queue *q) { }
330static inline void blkcg_exit_queue(struct request_queue *q) { }
Tejun Heo8bd435b2012-04-13 13:11:28 -0700331static inline int blkio_policy_register(struct blkio_policy_type *blkiop) { return 0; }
Vivek Goyal3e252062009-12-04 10:36:42 -0500332static inline void blkio_policy_unregister(struct blkio_policy_type *blkiop) { }
Tejun Heoa2b16932012-04-13 13:11:33 -0700333static inline int blkcg_activate_policy(struct request_queue *q,
334 const struct blkio_policy_type *pol) { return 0; }
335static inline void blkcg_deactivate_policy(struct request_queue *q,
336 const struct blkio_policy_type *pol) { }
Tejun Heo03aa264a2012-03-05 13:15:19 -0800337static inline void blkg_destroy_all(struct request_queue *q,
Tejun Heo03aa264a2012-03-05 13:15:19 -0800338 bool destory_root) { }
Vivek Goyal3e252062009-12-04 10:36:42 -0500339
Tejun Heo03814112012-03-05 13:15:14 -0800340static inline void *blkg_to_pdata(struct blkio_group *blkg,
341 struct blkio_policy_type *pol) { return NULL; }
342static inline struct blkio_group *pdata_to_blkg(void *pdata,
343 struct blkio_policy_type *pol) { return NULL; }
Vivek Goyalafc24d42010-04-26 19:27:56 +0200344static inline char *blkg_path(struct blkio_group *blkg) { return NULL; }
Tejun Heo1adaf3d2012-03-05 13:15:15 -0800345static inline void blkg_get(struct blkio_group *blkg) { }
346static inline void blkg_put(struct blkio_group *blkg) { }
Vivek Goyalafc24d42010-04-26 19:27:56 +0200347
Jens Axboe2f5ea472009-12-03 21:06:43 +0100348#endif
349
Tejun Heo32e380a2012-03-05 13:14:54 -0800350#ifdef CONFIG_BLK_CGROUP
Vivek Goyal31e4c282009-12-03 12:59:42 -0500351extern struct blkio_cgroup blkio_root_cgroup;
352extern struct blkio_cgroup *cgroup_to_blkio_cgroup(struct cgroup *cgroup);
Tejun Heo4f85cb92012-03-05 13:15:28 -0800353extern struct blkio_cgroup *bio_blkio_cgroup(struct bio *bio);
Tejun Heocd1604f2012-03-05 13:15:06 -0800354extern struct blkio_group *blkg_lookup(struct blkio_cgroup *blkcg,
Tejun Heoe8989fa2012-03-05 13:15:20 -0800355 struct request_queue *q);
Tejun Heocd1604f2012-03-05 13:15:06 -0800356struct blkio_group *blkg_lookup_create(struct blkio_cgroup *blkcg,
357 struct request_queue *q,
Tejun Heocd1604f2012-03-05 13:15:06 -0800358 bool for_root);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500359#else
Jens Axboe2f5ea472009-12-03 21:06:43 +0100360struct cgroup;
Vivek Goyal31e4c282009-12-03 12:59:42 -0500361static inline struct blkio_cgroup *
362cgroup_to_blkio_cgroup(struct cgroup *cgroup) { return NULL; }
Vivek Goyal70087dc2011-05-16 15:24:08 +0200363static inline struct blkio_cgroup *
Tejun Heo4f85cb92012-03-05 13:15:28 -0800364bio_blkio_cgroup(struct bio *bio) { return NULL; }
Vivek Goyal31e4c282009-12-03 12:59:42 -0500365
Tejun Heocd1604f2012-03-05 13:15:06 -0800366static inline struct blkio_group *blkg_lookup(struct blkio_cgroup *blkcg,
367 void *key) { return NULL; }
Vivek Goyal31e4c282009-12-03 12:59:42 -0500368#endif
369#endif /* _BLK_CGROUP_H */