blob: b347aa08d1660b561703ece090024cd3f10759a9 [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 {
Tejun Heo36558c82012-04-16 13:57:24 -070041 struct cgroup_subsys_state css;
42 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 */
Tejun Heo36558c82012-04-16 13:57:24 -070046 uint64_t id;
Tejun Heo3381cb82012-04-01 14:38:44 -070047
48 /* TODO: per-policy storage in blkio_cgroup */
Tejun Heo36558c82012-04-16 13:57:24 -070049 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 */
Tejun Heo36558c82012-04-16 13:57:24 -070065 struct blkio_group *blkg;
Tejun Heo03814112012-03-05 13:15:14 -080066
Tejun Heoa2b16932012-04-13 13:11:33 -070067 /* used during policy activation */
Tejun Heo36558c82012-04-16 13:57:24 -070068 struct list_head alloc_node;
Tejun Heoa2b16932012-04-13 13:11:33 -070069
Tejun Heo03814112012-03-05 13:15:14 -080070 /* pol->pdata_size bytes of private data used by policy impl */
Tejun Heo36558c82012-04-16 13:57:24 -070071 char pdata[] __aligned(__alignof__(unsigned long long));
Tejun Heo03814112012-03-05 13:15:14 -080072};
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 */
Tejun Heo36558c82012-04-16 13:57:24 -070076 struct request_queue *q;
77 struct list_head q_node;
78 struct hlist_node blkcg_node;
79 struct blkio_cgroup *blkcg;
Tejun Heo1adaf3d2012-03-05 13:15:15 -080080 /* reference count */
Tejun Heo36558c82012-04-16 13:57:24 -070081 int refcnt;
Vivek Goyal22084192009-12-03 12:59:49 -050082
Tejun Heo36558c82012-04-16 13:57:24 -070083 struct blkg_policy_data *pd[BLKCG_MAX_POLS];
Tejun Heo1adaf3d2012-03-05 13:15:15 -080084
Tejun Heo36558c82012-04-16 13:57:24 -070085 struct rcu_head rcu_head;
Vivek Goyal31e4c282009-12-03 12:59:42 -050086};
87
Tejun Heo03814112012-03-05 13:15:14 -080088typedef void (blkio_init_group_fn)(struct blkio_group *blkg);
Tejun Heo9ade5ea2012-04-01 14:38:44 -070089typedef void (blkio_exit_group_fn)(struct blkio_group *blkg);
90typedef void (blkio_reset_group_stats_fn)(struct blkio_group *blkg);
Vivek Goyal3e252062009-12-04 10:36:42 -050091
92struct blkio_policy_ops {
Tejun Heo36558c82012-04-16 13:57:24 -070093 blkio_init_group_fn *blkio_init_group_fn;
94 blkio_exit_group_fn *blkio_exit_group_fn;
95 blkio_reset_group_stats_fn *blkio_reset_group_stats_fn;
Vivek Goyal3e252062009-12-04 10:36:42 -050096};
97
98struct blkio_policy_type {
Tejun Heo36558c82012-04-16 13:57:24 -070099 struct blkio_policy_ops ops;
100 int plid;
101 /* policy specific private data size */
102 size_t pdata_size;
103 /* cgroup files for the policy */
104 struct cftype *cftypes;
Vivek Goyal3e252062009-12-04 10:36:42 -0500105};
106
Tejun Heo36558c82012-04-16 13:57:24 -0700107extern struct blkio_cgroup blkio_root_cgroup;
108
109struct blkio_cgroup *cgroup_to_blkio_cgroup(struct cgroup *cgroup);
110struct blkio_cgroup *bio_blkio_cgroup(struct bio *bio);
111struct blkio_group *blkg_lookup(struct blkio_cgroup *blkcg,
112 struct request_queue *q);
113struct blkio_group *blkg_lookup_create(struct blkio_cgroup *blkcg,
114 struct request_queue *q);
115int blkcg_init_queue(struct request_queue *q);
116void blkcg_drain_queue(struct request_queue *q);
117void blkcg_exit_queue(struct request_queue *q);
Tejun Heo5efd6112012-03-05 13:15:12 -0800118
Vivek Goyal3e252062009-12-04 10:36:42 -0500119/* Blkio controller policy registration */
Tejun Heo36558c82012-04-16 13:57:24 -0700120int blkio_policy_register(struct blkio_policy_type *);
121void blkio_policy_unregister(struct blkio_policy_type *);
122int blkcg_activate_policy(struct request_queue *q,
123 const struct blkio_policy_type *pol);
124void blkcg_deactivate_policy(struct request_queue *q,
125 const struct blkio_policy_type *pol);
Vivek Goyal3e252062009-12-04 10:36:42 -0500126
Tejun Heo829fdb52012-04-01 14:38:43 -0700127void blkcg_print_blkgs(struct seq_file *sf, struct blkio_cgroup *blkcg,
Tejun Heod366e7e2012-04-01 14:38:44 -0700128 u64 (*prfill)(struct seq_file *, void *, int),
Tejun Heoec399342012-04-13 13:11:27 -0700129 const struct blkio_policy_type *pol, int data,
130 bool show_total);
Tejun Heod366e7e2012-04-01 14:38:44 -0700131u64 __blkg_prfill_u64(struct seq_file *sf, void *pdata, u64 v);
132u64 __blkg_prfill_rwstat(struct seq_file *sf, void *pdata,
Tejun Heo829fdb52012-04-01 14:38:43 -0700133 const struct blkg_rwstat *rwstat);
Tejun Heo5bc4afb12012-04-01 14:38:45 -0700134u64 blkg_prfill_stat(struct seq_file *sf, void *pdata, int off);
135u64 blkg_prfill_rwstat(struct seq_file *sf, void *pdata, int off);
Tejun Heo829fdb52012-04-01 14:38:43 -0700136
137struct blkg_conf_ctx {
Tejun Heo36558c82012-04-16 13:57:24 -0700138 struct gendisk *disk;
139 struct blkio_group *blkg;
140 u64 v;
Tejun Heo829fdb52012-04-01 14:38:43 -0700141};
142
Tejun Heoda8b0662012-04-13 13:11:29 -0700143int blkg_conf_prep(struct blkio_cgroup *blkcg,
144 const struct blkio_policy_type *pol, const char *input,
Tejun Heo829fdb52012-04-01 14:38:43 -0700145 struct blkg_conf_ctx *ctx);
146void blkg_conf_finish(struct blkg_conf_ctx *ctx);
147
148
Tejun Heo03814112012-03-05 13:15:14 -0800149/**
150 * blkg_to_pdata - get policy private data
151 * @blkg: blkg of interest
152 * @pol: policy of interest
153 *
154 * Return pointer to private data associated with the @blkg-@pol pair.
155 */
156static inline void *blkg_to_pdata(struct blkio_group *blkg,
157 struct blkio_policy_type *pol)
158{
Tejun Heo549d3aa2012-03-05 13:15:16 -0800159 return blkg ? blkg->pd[pol->plid]->pdata : NULL;
Tejun Heo03814112012-03-05 13:15:14 -0800160}
161
162/**
163 * pdata_to_blkg - get blkg associated with policy private data
164 * @pdata: policy private data of interest
Tejun Heo03814112012-03-05 13:15:14 -0800165 *
Tejun Heoaaec55a2012-04-01 14:38:42 -0700166 * @pdata is policy private data. Determine the blkg it's associated with.
Tejun Heo03814112012-03-05 13:15:14 -0800167 */
Tejun Heoaaec55a2012-04-01 14:38:42 -0700168static inline struct blkio_group *pdata_to_blkg(void *pdata)
Tejun Heo03814112012-03-05 13:15:14 -0800169{
170 if (pdata) {
171 struct blkg_policy_data *pd =
172 container_of(pdata, struct blkg_policy_data, pdata);
173 return pd->blkg;
174 }
175 return NULL;
176}
177
Tejun Heo54e7ed12012-04-16 13:57:23 -0700178/**
179 * blkg_path - format cgroup path of blkg
180 * @blkg: blkg of interest
181 * @buf: target buffer
182 * @buflen: target buffer length
183 *
184 * Format the path of the cgroup of @blkg into @buf.
185 */
186static inline int blkg_path(struct blkio_group *blkg, char *buf, int buflen)
Vivek Goyalafc24d42010-04-26 19:27:56 +0200187{
Tejun Heo54e7ed12012-04-16 13:57:23 -0700188 int ret;
189
190 rcu_read_lock();
191 ret = cgroup_path(blkg->blkcg->css.cgroup, buf, buflen);
192 rcu_read_unlock();
193 if (ret)
194 strncpy(buf, "<unavailable>", buflen);
195 return ret;
Vivek Goyalafc24d42010-04-26 19:27:56 +0200196}
197
Tejun Heo1adaf3d2012-03-05 13:15:15 -0800198/**
199 * blkg_get - get a blkg reference
200 * @blkg: blkg to get
201 *
202 * The caller should be holding queue_lock and an existing reference.
203 */
204static inline void blkg_get(struct blkio_group *blkg)
205{
206 lockdep_assert_held(blkg->q->queue_lock);
207 WARN_ON_ONCE(!blkg->refcnt);
208 blkg->refcnt++;
209}
210
211void __blkg_release(struct blkio_group *blkg);
212
213/**
214 * blkg_put - put a blkg reference
215 * @blkg: blkg to put
216 *
217 * The caller should be holding queue_lock.
218 */
219static inline void blkg_put(struct blkio_group *blkg)
220{
221 lockdep_assert_held(blkg->q->queue_lock);
222 WARN_ON_ONCE(blkg->refcnt <= 0);
223 if (!--blkg->refcnt)
224 __blkg_release(blkg);
225}
226
Tejun Heoedcb0722012-04-01 14:38:42 -0700227/**
228 * blkg_stat_add - add a value to a blkg_stat
229 * @stat: target blkg_stat
230 * @val: value to add
231 *
232 * Add @val to @stat. The caller is responsible for synchronizing calls to
233 * this function.
234 */
235static inline void blkg_stat_add(struct blkg_stat *stat, uint64_t val)
236{
237 u64_stats_update_begin(&stat->syncp);
238 stat->cnt += val;
239 u64_stats_update_end(&stat->syncp);
240}
241
242/**
243 * blkg_stat_read - read the current value of a blkg_stat
244 * @stat: blkg_stat to read
245 *
246 * Read the current value of @stat. This function can be called without
247 * synchroniztion and takes care of u64 atomicity.
248 */
249static inline uint64_t blkg_stat_read(struct blkg_stat *stat)
250{
251 unsigned int start;
252 uint64_t v;
253
254 do {
255 start = u64_stats_fetch_begin(&stat->syncp);
256 v = stat->cnt;
257 } while (u64_stats_fetch_retry(&stat->syncp, start));
258
259 return v;
260}
261
262/**
263 * blkg_stat_reset - reset a blkg_stat
264 * @stat: blkg_stat to reset
265 */
266static inline void blkg_stat_reset(struct blkg_stat *stat)
267{
268 stat->cnt = 0;
269}
270
271/**
272 * blkg_rwstat_add - add a value to a blkg_rwstat
273 * @rwstat: target blkg_rwstat
274 * @rw: mask of REQ_{WRITE|SYNC}
275 * @val: value to add
276 *
277 * Add @val to @rwstat. The counters are chosen according to @rw. The
278 * caller is responsible for synchronizing calls to this function.
279 */
280static inline void blkg_rwstat_add(struct blkg_rwstat *rwstat,
281 int rw, uint64_t val)
282{
283 u64_stats_update_begin(&rwstat->syncp);
284
285 if (rw & REQ_WRITE)
286 rwstat->cnt[BLKG_RWSTAT_WRITE] += val;
287 else
288 rwstat->cnt[BLKG_RWSTAT_READ] += val;
289 if (rw & REQ_SYNC)
290 rwstat->cnt[BLKG_RWSTAT_SYNC] += val;
291 else
292 rwstat->cnt[BLKG_RWSTAT_ASYNC] += val;
293
294 u64_stats_update_end(&rwstat->syncp);
295}
296
297/**
298 * blkg_rwstat_read - read the current values of a blkg_rwstat
299 * @rwstat: blkg_rwstat to read
300 *
301 * Read the current snapshot of @rwstat and return it as the return value.
302 * This function can be called without synchronization and takes care of
303 * u64 atomicity.
304 */
Tejun Heoc94bed892012-04-16 13:57:22 -0700305static inline struct blkg_rwstat blkg_rwstat_read(struct blkg_rwstat *rwstat)
Tejun Heoedcb0722012-04-01 14:38:42 -0700306{
307 unsigned int start;
308 struct blkg_rwstat tmp;
309
310 do {
311 start = u64_stats_fetch_begin(&rwstat->syncp);
312 tmp = *rwstat;
313 } while (u64_stats_fetch_retry(&rwstat->syncp, start));
314
315 return tmp;
316}
317
318/**
319 * blkg_rwstat_sum - read the total count of a blkg_rwstat
320 * @rwstat: blkg_rwstat to read
321 *
322 * Return the total count of @rwstat regardless of the IO direction. This
323 * function can be called without synchronization and takes care of u64
324 * atomicity.
325 */
326static inline uint64_t blkg_rwstat_sum(struct blkg_rwstat *rwstat)
327{
328 struct blkg_rwstat tmp = blkg_rwstat_read(rwstat);
329
330 return tmp.cnt[BLKG_RWSTAT_READ] + tmp.cnt[BLKG_RWSTAT_WRITE];
331}
332
333/**
334 * blkg_rwstat_reset - reset a blkg_rwstat
335 * @rwstat: blkg_rwstat to reset
336 */
337static inline void blkg_rwstat_reset(struct blkg_rwstat *rwstat)
338{
339 memset(rwstat->cnt, 0, sizeof(rwstat->cnt));
340}
341
Tejun Heo36558c82012-04-16 13:57:24 -0700342#else /* CONFIG_BLK_CGROUP */
343
344struct cgroup;
Jens Axboe2f5ea472009-12-03 21:06:43 +0100345
346struct blkio_group {
347};
348
Vivek Goyal3e252062009-12-04 10:36:42 -0500349struct blkio_policy_type {
350};
351
Tejun Heo36558c82012-04-16 13:57:24 -0700352static inline struct blkio_cgroup *cgroup_to_blkio_cgroup(struct cgroup *cgroup) { return NULL; }
353static inline struct blkio_cgroup *bio_blkio_cgroup(struct bio *bio) { return NULL; }
354static inline struct blkio_group *blkg_lookup(struct blkio_cgroup *blkcg, void *key) { return NULL; }
Tejun Heo5efd6112012-03-05 13:15:12 -0800355static inline int blkcg_init_queue(struct request_queue *q) { return 0; }
356static inline void blkcg_drain_queue(struct request_queue *q) { }
357static inline void blkcg_exit_queue(struct request_queue *q) { }
Tejun Heo8bd435b2012-04-13 13:11:28 -0700358static inline int blkio_policy_register(struct blkio_policy_type *blkiop) { return 0; }
Vivek Goyal3e252062009-12-04 10:36:42 -0500359static inline void blkio_policy_unregister(struct blkio_policy_type *blkiop) { }
Tejun Heoa2b16932012-04-13 13:11:33 -0700360static inline int blkcg_activate_policy(struct request_queue *q,
361 const struct blkio_policy_type *pol) { return 0; }
362static inline void blkcg_deactivate_policy(struct request_queue *q,
363 const struct blkio_policy_type *pol) { }
Vivek Goyal3e252062009-12-04 10:36:42 -0500364
Tejun Heo03814112012-03-05 13:15:14 -0800365static inline void *blkg_to_pdata(struct blkio_group *blkg,
366 struct blkio_policy_type *pol) { return NULL; }
367static inline struct blkio_group *pdata_to_blkg(void *pdata,
368 struct blkio_policy_type *pol) { return NULL; }
Vivek Goyalafc24d42010-04-26 19:27:56 +0200369static inline char *blkg_path(struct blkio_group *blkg) { return NULL; }
Tejun Heo1adaf3d2012-03-05 13:15:15 -0800370static inline void blkg_get(struct blkio_group *blkg) { }
371static inline void blkg_put(struct blkio_group *blkg) { }
Vivek Goyalafc24d42010-04-26 19:27:56 +0200372
Tejun Heo36558c82012-04-16 13:57:24 -0700373#endif /* CONFIG_BLK_CGROUP */
374#endif /* _BLK_CGROUP_H */