blob: a443b84d2c16b576c7cd0e1360058091a2c42245 [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
Tejun Heo3c798392012-04-16 13:57:25 -070040struct blkcg {
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
Tejun Heo3c798392012-04-16 13:57:25 -070048 /* TODO: per-policy storage in blkcg */
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 Heo3c798392012-04-16 13:57:25 -070065 struct blkcg_gq *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
Tejun Heo3c798392012-04-16 13:57:25 -070074/* association between a blk cgroup and a request queue */
75struct blkcg_gq {
Tejun Heoc875f4d2012-03-05 13:15:22 -080076 /* Pointer to the associated request_queue */
Tejun Heo36558c82012-04-16 13:57:24 -070077 struct request_queue *q;
78 struct list_head q_node;
79 struct hlist_node blkcg_node;
Tejun Heo3c798392012-04-16 13:57:25 -070080 struct blkcg *blkcg;
Tejun Heo1adaf3d2012-03-05 13:15:15 -080081 /* reference count */
Tejun Heo36558c82012-04-16 13:57:24 -070082 int refcnt;
Vivek Goyal22084192009-12-03 12:59:49 -050083
Tejun Heo36558c82012-04-16 13:57:24 -070084 struct blkg_policy_data *pd[BLKCG_MAX_POLS];
Tejun Heo1adaf3d2012-03-05 13:15:15 -080085
Tejun Heo36558c82012-04-16 13:57:24 -070086 struct rcu_head rcu_head;
Vivek Goyal31e4c282009-12-03 12:59:42 -050087};
88
Tejun Heo3c798392012-04-16 13:57:25 -070089typedef void (blkcg_pol_init_pd_fn)(struct blkcg_gq *blkg);
90typedef void (blkcg_pol_exit_pd_fn)(struct blkcg_gq *blkg);
91typedef void (blkcg_pol_reset_pd_stats_fn)(struct blkcg_gq *blkg);
Vivek Goyal3e252062009-12-04 10:36:42 -050092
Tejun Heo3c798392012-04-16 13:57:25 -070093struct blkcg_policy_ops {
94 blkcg_pol_init_pd_fn *pd_init_fn;
95 blkcg_pol_exit_pd_fn *pd_exit_fn;
96 blkcg_pol_reset_pd_stats_fn *pd_reset_stats_fn;
Vivek Goyal3e252062009-12-04 10:36:42 -050097};
98
Tejun Heo3c798392012-04-16 13:57:25 -070099struct blkcg_policy {
100 struct blkcg_policy_ops ops;
Tejun Heo36558c82012-04-16 13:57:24 -0700101 int plid;
102 /* policy specific private data size */
103 size_t pdata_size;
104 /* cgroup files for the policy */
105 struct cftype *cftypes;
Vivek Goyal3e252062009-12-04 10:36:42 -0500106};
107
Tejun Heo3c798392012-04-16 13:57:25 -0700108extern struct blkcg blkcg_root;
Tejun Heo36558c82012-04-16 13:57:24 -0700109
Tejun Heo3c798392012-04-16 13:57:25 -0700110struct blkcg *cgroup_to_blkcg(struct cgroup *cgroup);
111struct blkcg *bio_blkcg(struct bio *bio);
112struct blkcg_gq *blkg_lookup(struct blkcg *blkcg, struct request_queue *q);
113struct blkcg_gq *blkg_lookup_create(struct blkcg *blkcg,
114 struct request_queue *q);
Tejun Heo36558c82012-04-16 13:57:24 -0700115int 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 Heo3c798392012-04-16 13:57:25 -0700120int blkcg_policy_register(struct blkcg_policy *pol);
121void blkcg_policy_unregister(struct blkcg_policy *pol);
Tejun Heo36558c82012-04-16 13:57:24 -0700122int blkcg_activate_policy(struct request_queue *q,
Tejun Heo3c798392012-04-16 13:57:25 -0700123 const struct blkcg_policy *pol);
Tejun Heo36558c82012-04-16 13:57:24 -0700124void blkcg_deactivate_policy(struct request_queue *q,
Tejun Heo3c798392012-04-16 13:57:25 -0700125 const struct blkcg_policy *pol);
Vivek Goyal3e252062009-12-04 10:36:42 -0500126
Tejun Heo3c798392012-04-16 13:57:25 -0700127void blkcg_print_blkgs(struct seq_file *sf, struct blkcg *blkcg,
Tejun Heod366e7e2012-04-01 14:38:44 -0700128 u64 (*prfill)(struct seq_file *, void *, int),
Tejun Heo3c798392012-04-16 13:57:25 -0700129 const struct blkcg_policy *pol, int data,
Tejun Heoec399342012-04-13 13:11:27 -0700130 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;
Tejun Heo3c798392012-04-16 13:57:25 -0700139 struct blkcg_gq *blkg;
Tejun Heo36558c82012-04-16 13:57:24 -0700140 u64 v;
Tejun Heo829fdb52012-04-01 14:38:43 -0700141};
142
Tejun Heo3c798392012-04-16 13:57:25 -0700143int blkg_conf_prep(struct blkcg *blkcg, const struct blkcg_policy *pol,
144 const char *input, struct blkg_conf_ctx *ctx);
Tejun Heo829fdb52012-04-01 14:38:43 -0700145void blkg_conf_finish(struct blkg_conf_ctx *ctx);
146
147
Tejun Heo03814112012-03-05 13:15:14 -0800148/**
149 * blkg_to_pdata - get policy private data
150 * @blkg: blkg of interest
151 * @pol: policy of interest
152 *
153 * Return pointer to private data associated with the @blkg-@pol pair.
154 */
Tejun Heo3c798392012-04-16 13:57:25 -0700155static inline void *blkg_to_pdata(struct blkcg_gq *blkg,
156 struct blkcg_policy *pol)
Tejun Heo03814112012-03-05 13:15:14 -0800157{
Tejun Heo549d3aa2012-03-05 13:15:16 -0800158 return blkg ? blkg->pd[pol->plid]->pdata : NULL;
Tejun Heo03814112012-03-05 13:15:14 -0800159}
160
161/**
162 * pdata_to_blkg - get blkg associated with policy private data
163 * @pdata: policy private data of interest
Tejun Heo03814112012-03-05 13:15:14 -0800164 *
Tejun Heoaaec55a2012-04-01 14:38:42 -0700165 * @pdata is policy private data. Determine the blkg it's associated with.
Tejun Heo03814112012-03-05 13:15:14 -0800166 */
Tejun Heo3c798392012-04-16 13:57:25 -0700167static inline struct blkcg_gq *pdata_to_blkg(void *pdata)
Tejun Heo03814112012-03-05 13:15:14 -0800168{
169 if (pdata) {
170 struct blkg_policy_data *pd =
171 container_of(pdata, struct blkg_policy_data, pdata);
172 return pd->blkg;
173 }
174 return NULL;
175}
176
Tejun Heo54e7ed12012-04-16 13:57:23 -0700177/**
178 * blkg_path - format cgroup path of blkg
179 * @blkg: blkg of interest
180 * @buf: target buffer
181 * @buflen: target buffer length
182 *
183 * Format the path of the cgroup of @blkg into @buf.
184 */
Tejun Heo3c798392012-04-16 13:57:25 -0700185static inline int blkg_path(struct blkcg_gq *blkg, char *buf, int buflen)
Vivek Goyalafc24d42010-04-26 19:27:56 +0200186{
Tejun Heo54e7ed12012-04-16 13:57:23 -0700187 int ret;
188
189 rcu_read_lock();
190 ret = cgroup_path(blkg->blkcg->css.cgroup, buf, buflen);
191 rcu_read_unlock();
192 if (ret)
193 strncpy(buf, "<unavailable>", buflen);
194 return ret;
Vivek Goyalafc24d42010-04-26 19:27:56 +0200195}
196
Tejun Heo1adaf3d2012-03-05 13:15:15 -0800197/**
198 * blkg_get - get a blkg reference
199 * @blkg: blkg to get
200 *
201 * The caller should be holding queue_lock and an existing reference.
202 */
Tejun Heo3c798392012-04-16 13:57:25 -0700203static inline void blkg_get(struct blkcg_gq *blkg)
Tejun Heo1adaf3d2012-03-05 13:15:15 -0800204{
205 lockdep_assert_held(blkg->q->queue_lock);
206 WARN_ON_ONCE(!blkg->refcnt);
207 blkg->refcnt++;
208}
209
Tejun Heo3c798392012-04-16 13:57:25 -0700210void __blkg_release(struct blkcg_gq *blkg);
Tejun Heo1adaf3d2012-03-05 13:15:15 -0800211
212/**
213 * blkg_put - put a blkg reference
214 * @blkg: blkg to put
215 *
216 * The caller should be holding queue_lock.
217 */
Tejun Heo3c798392012-04-16 13:57:25 -0700218static inline void blkg_put(struct blkcg_gq *blkg)
Tejun Heo1adaf3d2012-03-05 13:15:15 -0800219{
220 lockdep_assert_held(blkg->q->queue_lock);
221 WARN_ON_ONCE(blkg->refcnt <= 0);
222 if (!--blkg->refcnt)
223 __blkg_release(blkg);
224}
225
Tejun Heoedcb0722012-04-01 14:38:42 -0700226/**
227 * blkg_stat_add - add a value to a blkg_stat
228 * @stat: target blkg_stat
229 * @val: value to add
230 *
231 * Add @val to @stat. The caller is responsible for synchronizing calls to
232 * this function.
233 */
234static inline void blkg_stat_add(struct blkg_stat *stat, uint64_t val)
235{
236 u64_stats_update_begin(&stat->syncp);
237 stat->cnt += val;
238 u64_stats_update_end(&stat->syncp);
239}
240
241/**
242 * blkg_stat_read - read the current value of a blkg_stat
243 * @stat: blkg_stat to read
244 *
245 * Read the current value of @stat. This function can be called without
246 * synchroniztion and takes care of u64 atomicity.
247 */
248static inline uint64_t blkg_stat_read(struct blkg_stat *stat)
249{
250 unsigned int start;
251 uint64_t v;
252
253 do {
254 start = u64_stats_fetch_begin(&stat->syncp);
255 v = stat->cnt;
256 } while (u64_stats_fetch_retry(&stat->syncp, start));
257
258 return v;
259}
260
261/**
262 * blkg_stat_reset - reset a blkg_stat
263 * @stat: blkg_stat to reset
264 */
265static inline void blkg_stat_reset(struct blkg_stat *stat)
266{
267 stat->cnt = 0;
268}
269
270/**
271 * blkg_rwstat_add - add a value to a blkg_rwstat
272 * @rwstat: target blkg_rwstat
273 * @rw: mask of REQ_{WRITE|SYNC}
274 * @val: value to add
275 *
276 * Add @val to @rwstat. The counters are chosen according to @rw. The
277 * caller is responsible for synchronizing calls to this function.
278 */
279static inline void blkg_rwstat_add(struct blkg_rwstat *rwstat,
280 int rw, uint64_t val)
281{
282 u64_stats_update_begin(&rwstat->syncp);
283
284 if (rw & REQ_WRITE)
285 rwstat->cnt[BLKG_RWSTAT_WRITE] += val;
286 else
287 rwstat->cnt[BLKG_RWSTAT_READ] += val;
288 if (rw & REQ_SYNC)
289 rwstat->cnt[BLKG_RWSTAT_SYNC] += val;
290 else
291 rwstat->cnt[BLKG_RWSTAT_ASYNC] += val;
292
293 u64_stats_update_end(&rwstat->syncp);
294}
295
296/**
297 * blkg_rwstat_read - read the current values of a blkg_rwstat
298 * @rwstat: blkg_rwstat to read
299 *
300 * Read the current snapshot of @rwstat and return it as the return value.
301 * This function can be called without synchronization and takes care of
302 * u64 atomicity.
303 */
Tejun Heoc94bed892012-04-16 13:57:22 -0700304static inline struct blkg_rwstat blkg_rwstat_read(struct blkg_rwstat *rwstat)
Tejun Heoedcb0722012-04-01 14:38:42 -0700305{
306 unsigned int start;
307 struct blkg_rwstat tmp;
308
309 do {
310 start = u64_stats_fetch_begin(&rwstat->syncp);
311 tmp = *rwstat;
312 } while (u64_stats_fetch_retry(&rwstat->syncp, start));
313
314 return tmp;
315}
316
317/**
318 * blkg_rwstat_sum - read the total count of a blkg_rwstat
319 * @rwstat: blkg_rwstat to read
320 *
321 * Return the total count of @rwstat regardless of the IO direction. This
322 * function can be called without synchronization and takes care of u64
323 * atomicity.
324 */
325static inline uint64_t blkg_rwstat_sum(struct blkg_rwstat *rwstat)
326{
327 struct blkg_rwstat tmp = blkg_rwstat_read(rwstat);
328
329 return tmp.cnt[BLKG_RWSTAT_READ] + tmp.cnt[BLKG_RWSTAT_WRITE];
330}
331
332/**
333 * blkg_rwstat_reset - reset a blkg_rwstat
334 * @rwstat: blkg_rwstat to reset
335 */
336static inline void blkg_rwstat_reset(struct blkg_rwstat *rwstat)
337{
338 memset(rwstat->cnt, 0, sizeof(rwstat->cnt));
339}
340
Tejun Heo36558c82012-04-16 13:57:24 -0700341#else /* CONFIG_BLK_CGROUP */
342
343struct cgroup;
Jens Axboe2f5ea472009-12-03 21:06:43 +0100344
Tejun Heo3c798392012-04-16 13:57:25 -0700345struct blkcg_gq {
Jens Axboe2f5ea472009-12-03 21:06:43 +0100346};
347
Tejun Heo3c798392012-04-16 13:57:25 -0700348struct blkcg_policy {
Vivek Goyal3e252062009-12-04 10:36:42 -0500349};
350
Tejun Heo3c798392012-04-16 13:57:25 -0700351static inline struct blkcg *cgroup_to_blkcg(struct cgroup *cgroup) { return NULL; }
352static inline struct blkcg *bio_blkcg(struct bio *bio) { return NULL; }
353static inline struct blkcg_gq *blkg_lookup(struct blkcg *blkcg, void *key) { return NULL; }
Tejun Heo5efd6112012-03-05 13:15:12 -0800354static inline int blkcg_init_queue(struct request_queue *q) { return 0; }
355static inline void blkcg_drain_queue(struct request_queue *q) { }
356static inline void blkcg_exit_queue(struct request_queue *q) { }
Tejun Heo3c798392012-04-16 13:57:25 -0700357static inline int blkcg_policy_register(struct blkcg_policy *pol) { return 0; }
358static inline void blkcg_policy_unregister(struct blkcg_policy *pol) { }
Tejun Heoa2b16932012-04-13 13:11:33 -0700359static inline int blkcg_activate_policy(struct request_queue *q,
Tejun Heo3c798392012-04-16 13:57:25 -0700360 const struct blkcg_policy *pol) { return 0; }
Tejun Heoa2b16932012-04-13 13:11:33 -0700361static inline void blkcg_deactivate_policy(struct request_queue *q,
Tejun Heo3c798392012-04-16 13:57:25 -0700362 const struct blkcg_policy *pol) { }
Vivek Goyal3e252062009-12-04 10:36:42 -0500363
Tejun Heo3c798392012-04-16 13:57:25 -0700364static inline void *blkg_to_pdata(struct blkcg_gq *blkg,
365 struct blkcg_policy *pol) { return NULL; }
366static inline struct blkcg_gq *pdata_to_blkg(void *pdata,
367 struct blkcg_policy *pol) { return NULL; }
368static inline char *blkg_path(struct blkcg_gq *blkg) { return NULL; }
369static inline void blkg_get(struct blkcg_gq *blkg) { }
370static inline void blkg_put(struct blkcg_gq *blkg) { }
Vivek Goyalafc24d42010-04-26 19:27:56 +0200371
Tejun Heo36558c82012-04-16 13:57:24 -0700372#endif /* CONFIG_BLK_CGROUP */
373#endif /* _BLK_CGROUP_H */