blob: c524267e4f7f73e7dfc06427a2793c6b61d38b9e [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;
Tejun Heo1adaf3d2012-03-05 13:15:15 -080080 /* reference count */
81 int refcnt;
Vivek Goyal22084192009-12-03 12:59:49 -050082
Tejun Heo8bd435b2012-04-13 13:11:28 -070083 struct blkg_policy_data *pd[BLKCG_MAX_POLS];
Tejun Heo1adaf3d2012-03-05 13:15:15 -080084
85 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 Heo03814112012-03-05 13:15:14 -080093 blkio_init_group_fn *blkio_init_group_fn;
Tejun Heo9ade5ea2012-04-01 14:38:44 -070094 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 {
Vivek Goyal3e252062009-12-04 10:36:42 -050099 struct blkio_policy_ops ops;
Tejun Heo8bd435b2012-04-13 13:11:28 -0700100 int plid;
Tejun Heo03814112012-03-05 13:15:14 -0800101 size_t pdata_size; /* policy specific private data size */
Tejun Heo44ea53d2012-04-01 14:38:43 -0700102 struct cftype *cftypes; /* cgroup files for the policy */
Vivek Goyal3e252062009-12-04 10:36:42 -0500103};
104
Tejun Heo5efd6112012-03-05 13:15:12 -0800105extern int blkcg_init_queue(struct request_queue *q);
106extern void blkcg_drain_queue(struct request_queue *q);
107extern void blkcg_exit_queue(struct request_queue *q);
108
Vivek Goyal3e252062009-12-04 10:36:42 -0500109/* Blkio controller policy registration */
Tejun Heo8bd435b2012-04-13 13:11:28 -0700110extern int blkio_policy_register(struct blkio_policy_type *);
Vivek Goyal3e252062009-12-04 10:36:42 -0500111extern void blkio_policy_unregister(struct blkio_policy_type *);
Tejun Heoa2b16932012-04-13 13:11:33 -0700112extern int blkcg_activate_policy(struct request_queue *q,
113 const struct blkio_policy_type *pol);
114extern void blkcg_deactivate_policy(struct request_queue *q,
115 const struct blkio_policy_type *pol);
Vivek Goyal3e252062009-12-04 10:36:42 -0500116
Tejun Heo829fdb52012-04-01 14:38:43 -0700117void blkcg_print_blkgs(struct seq_file *sf, struct blkio_cgroup *blkcg,
Tejun Heod366e7e2012-04-01 14:38:44 -0700118 u64 (*prfill)(struct seq_file *, void *, int),
Tejun Heoec399342012-04-13 13:11:27 -0700119 const struct blkio_policy_type *pol, int data,
120 bool show_total);
Tejun Heod366e7e2012-04-01 14:38:44 -0700121u64 __blkg_prfill_u64(struct seq_file *sf, void *pdata, u64 v);
122u64 __blkg_prfill_rwstat(struct seq_file *sf, void *pdata,
Tejun Heo829fdb52012-04-01 14:38:43 -0700123 const struct blkg_rwstat *rwstat);
Tejun Heo5bc4afb12012-04-01 14:38:45 -0700124u64 blkg_prfill_stat(struct seq_file *sf, void *pdata, int off);
125u64 blkg_prfill_rwstat(struct seq_file *sf, void *pdata, int off);
Tejun Heo829fdb52012-04-01 14:38:43 -0700126
127struct blkg_conf_ctx {
128 struct gendisk *disk;
129 struct blkio_group *blkg;
130 u64 v;
131};
132
Tejun Heoda8b0662012-04-13 13:11:29 -0700133int blkg_conf_prep(struct blkio_cgroup *blkcg,
134 const struct blkio_policy_type *pol, const char *input,
Tejun Heo829fdb52012-04-01 14:38:43 -0700135 struct blkg_conf_ctx *ctx);
136void blkg_conf_finish(struct blkg_conf_ctx *ctx);
137
138
Tejun Heo03814112012-03-05 13:15:14 -0800139/**
140 * blkg_to_pdata - get policy private data
141 * @blkg: blkg of interest
142 * @pol: policy of interest
143 *
144 * Return pointer to private data associated with the @blkg-@pol pair.
145 */
146static inline void *blkg_to_pdata(struct blkio_group *blkg,
147 struct blkio_policy_type *pol)
148{
Tejun Heo549d3aa2012-03-05 13:15:16 -0800149 return blkg ? blkg->pd[pol->plid]->pdata : NULL;
Tejun Heo03814112012-03-05 13:15:14 -0800150}
151
152/**
153 * pdata_to_blkg - get blkg associated with policy private data
154 * @pdata: policy private data of interest
Tejun Heo03814112012-03-05 13:15:14 -0800155 *
Tejun Heoaaec55a2012-04-01 14:38:42 -0700156 * @pdata is policy private data. Determine the blkg it's associated with.
Tejun Heo03814112012-03-05 13:15:14 -0800157 */
Tejun Heoaaec55a2012-04-01 14:38:42 -0700158static inline struct blkio_group *pdata_to_blkg(void *pdata)
Tejun Heo03814112012-03-05 13:15:14 -0800159{
160 if (pdata) {
161 struct blkg_policy_data *pd =
162 container_of(pdata, struct blkg_policy_data, pdata);
163 return pd->blkg;
164 }
165 return NULL;
166}
167
Tejun Heo54e7ed12012-04-16 13:57:23 -0700168/**
169 * blkg_path - format cgroup path of blkg
170 * @blkg: blkg of interest
171 * @buf: target buffer
172 * @buflen: target buffer length
173 *
174 * Format the path of the cgroup of @blkg into @buf.
175 */
176static inline int blkg_path(struct blkio_group *blkg, char *buf, int buflen)
Vivek Goyalafc24d42010-04-26 19:27:56 +0200177{
Tejun Heo54e7ed12012-04-16 13:57:23 -0700178 int ret;
179
180 rcu_read_lock();
181 ret = cgroup_path(blkg->blkcg->css.cgroup, buf, buflen);
182 rcu_read_unlock();
183 if (ret)
184 strncpy(buf, "<unavailable>", buflen);
185 return ret;
Vivek Goyalafc24d42010-04-26 19:27:56 +0200186}
187
Tejun Heo1adaf3d2012-03-05 13:15:15 -0800188/**
189 * blkg_get - get a blkg reference
190 * @blkg: blkg to get
191 *
192 * The caller should be holding queue_lock and an existing reference.
193 */
194static inline void blkg_get(struct blkio_group *blkg)
195{
196 lockdep_assert_held(blkg->q->queue_lock);
197 WARN_ON_ONCE(!blkg->refcnt);
198 blkg->refcnt++;
199}
200
201void __blkg_release(struct blkio_group *blkg);
202
203/**
204 * blkg_put - put a blkg reference
205 * @blkg: blkg to put
206 *
207 * The caller should be holding queue_lock.
208 */
209static inline void blkg_put(struct blkio_group *blkg)
210{
211 lockdep_assert_held(blkg->q->queue_lock);
212 WARN_ON_ONCE(blkg->refcnt <= 0);
213 if (!--blkg->refcnt)
214 __blkg_release(blkg);
215}
216
Tejun Heoedcb0722012-04-01 14:38:42 -0700217/**
218 * blkg_stat_add - add a value to a blkg_stat
219 * @stat: target blkg_stat
220 * @val: value to add
221 *
222 * Add @val to @stat. The caller is responsible for synchronizing calls to
223 * this function.
224 */
225static inline void blkg_stat_add(struct blkg_stat *stat, uint64_t val)
226{
227 u64_stats_update_begin(&stat->syncp);
228 stat->cnt += val;
229 u64_stats_update_end(&stat->syncp);
230}
231
232/**
233 * blkg_stat_read - read the current value of a blkg_stat
234 * @stat: blkg_stat to read
235 *
236 * Read the current value of @stat. This function can be called without
237 * synchroniztion and takes care of u64 atomicity.
238 */
239static inline uint64_t blkg_stat_read(struct blkg_stat *stat)
240{
241 unsigned int start;
242 uint64_t v;
243
244 do {
245 start = u64_stats_fetch_begin(&stat->syncp);
246 v = stat->cnt;
247 } while (u64_stats_fetch_retry(&stat->syncp, start));
248
249 return v;
250}
251
252/**
253 * blkg_stat_reset - reset a blkg_stat
254 * @stat: blkg_stat to reset
255 */
256static inline void blkg_stat_reset(struct blkg_stat *stat)
257{
258 stat->cnt = 0;
259}
260
261/**
262 * blkg_rwstat_add - add a value to a blkg_rwstat
263 * @rwstat: target blkg_rwstat
264 * @rw: mask of REQ_{WRITE|SYNC}
265 * @val: value to add
266 *
267 * Add @val to @rwstat. The counters are chosen according to @rw. The
268 * caller is responsible for synchronizing calls to this function.
269 */
270static inline void blkg_rwstat_add(struct blkg_rwstat *rwstat,
271 int rw, uint64_t val)
272{
273 u64_stats_update_begin(&rwstat->syncp);
274
275 if (rw & REQ_WRITE)
276 rwstat->cnt[BLKG_RWSTAT_WRITE] += val;
277 else
278 rwstat->cnt[BLKG_RWSTAT_READ] += val;
279 if (rw & REQ_SYNC)
280 rwstat->cnt[BLKG_RWSTAT_SYNC] += val;
281 else
282 rwstat->cnt[BLKG_RWSTAT_ASYNC] += val;
283
284 u64_stats_update_end(&rwstat->syncp);
285}
286
287/**
288 * blkg_rwstat_read - read the current values of a blkg_rwstat
289 * @rwstat: blkg_rwstat to read
290 *
291 * Read the current snapshot of @rwstat and return it as the return value.
292 * This function can be called without synchronization and takes care of
293 * u64 atomicity.
294 */
Tejun Heoc94bed892012-04-16 13:57:22 -0700295static inline struct blkg_rwstat blkg_rwstat_read(struct blkg_rwstat *rwstat)
Tejun Heoedcb0722012-04-01 14:38:42 -0700296{
297 unsigned int start;
298 struct blkg_rwstat tmp;
299
300 do {
301 start = u64_stats_fetch_begin(&rwstat->syncp);
302 tmp = *rwstat;
303 } while (u64_stats_fetch_retry(&rwstat->syncp, start));
304
305 return tmp;
306}
307
308/**
309 * blkg_rwstat_sum - read the total count of a blkg_rwstat
310 * @rwstat: blkg_rwstat to read
311 *
312 * Return the total count of @rwstat regardless of the IO direction. This
313 * function can be called without synchronization and takes care of u64
314 * atomicity.
315 */
316static inline uint64_t blkg_rwstat_sum(struct blkg_rwstat *rwstat)
317{
318 struct blkg_rwstat tmp = blkg_rwstat_read(rwstat);
319
320 return tmp.cnt[BLKG_RWSTAT_READ] + tmp.cnt[BLKG_RWSTAT_WRITE];
321}
322
323/**
324 * blkg_rwstat_reset - reset a blkg_rwstat
325 * @rwstat: blkg_rwstat to reset
326 */
327static inline void blkg_rwstat_reset(struct blkg_rwstat *rwstat)
328{
329 memset(rwstat->cnt, 0, sizeof(rwstat->cnt));
330}
331
Jens Axboe2f5ea472009-12-03 21:06:43 +0100332#else
333
334struct blkio_group {
335};
336
Vivek Goyal3e252062009-12-04 10:36:42 -0500337struct blkio_policy_type {
338};
339
Tejun Heo5efd6112012-03-05 13:15:12 -0800340static inline int blkcg_init_queue(struct request_queue *q) { return 0; }
341static inline void blkcg_drain_queue(struct request_queue *q) { }
342static inline void blkcg_exit_queue(struct request_queue *q) { }
Tejun Heo8bd435b2012-04-13 13:11:28 -0700343static inline int blkio_policy_register(struct blkio_policy_type *blkiop) { return 0; }
Vivek Goyal3e252062009-12-04 10:36:42 -0500344static inline void blkio_policy_unregister(struct blkio_policy_type *blkiop) { }
Tejun Heoa2b16932012-04-13 13:11:33 -0700345static inline int blkcg_activate_policy(struct request_queue *q,
346 const struct blkio_policy_type *pol) { return 0; }
347static inline void blkcg_deactivate_policy(struct request_queue *q,
348 const struct blkio_policy_type *pol) { }
Vivek Goyal3e252062009-12-04 10:36:42 -0500349
Tejun Heo03814112012-03-05 13:15:14 -0800350static inline void *blkg_to_pdata(struct blkio_group *blkg,
351 struct blkio_policy_type *pol) { return NULL; }
352static inline struct blkio_group *pdata_to_blkg(void *pdata,
353 struct blkio_policy_type *pol) { return NULL; }
Vivek Goyalafc24d42010-04-26 19:27:56 +0200354static inline char *blkg_path(struct blkio_group *blkg) { return NULL; }
Tejun Heo1adaf3d2012-03-05 13:15:15 -0800355static inline void blkg_get(struct blkio_group *blkg) { }
356static inline void blkg_put(struct blkio_group *blkg) { }
Vivek Goyalafc24d42010-04-26 19:27:56 +0200357
Jens Axboe2f5ea472009-12-03 21:06:43 +0100358#endif
359
Tejun Heo32e380a2012-03-05 13:14:54 -0800360#ifdef CONFIG_BLK_CGROUP
Vivek Goyal31e4c282009-12-03 12:59:42 -0500361extern struct blkio_cgroup blkio_root_cgroup;
362extern struct blkio_cgroup *cgroup_to_blkio_cgroup(struct cgroup *cgroup);
Tejun Heo4f85cb92012-03-05 13:15:28 -0800363extern struct blkio_cgroup *bio_blkio_cgroup(struct bio *bio);
Tejun Heocd1604f2012-03-05 13:15:06 -0800364extern struct blkio_group *blkg_lookup(struct blkio_cgroup *blkcg,
Tejun Heoe8989fa2012-03-05 13:15:20 -0800365 struct request_queue *q);
Tejun Heocd1604f2012-03-05 13:15:06 -0800366struct blkio_group *blkg_lookup_create(struct blkio_cgroup *blkcg,
Tejun Heo3c96cb32012-04-13 13:11:34 -0700367 struct request_queue *q);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500368#else
Jens Axboe2f5ea472009-12-03 21:06:43 +0100369struct cgroup;
Vivek Goyal31e4c282009-12-03 12:59:42 -0500370static inline struct blkio_cgroup *
371cgroup_to_blkio_cgroup(struct cgroup *cgroup) { return NULL; }
Vivek Goyal70087dc2011-05-16 15:24:08 +0200372static inline struct blkio_cgroup *
Tejun Heo4f85cb92012-03-05 13:15:28 -0800373bio_blkio_cgroup(struct bio *bio) { return NULL; }
Vivek Goyal31e4c282009-12-03 12:59:42 -0500374
Tejun Heocd1604f2012-03-05 13:15:06 -0800375static inline struct blkio_group *blkg_lookup(struct blkio_cgroup *blkcg,
376 void *key) { return NULL; }
Vivek Goyal31e4c282009-12-03 12:59:42 -0500377#endif
378#endif /* _BLK_CGROUP_H */