blob: 44cb9086ed424238c286afa9c7c7ffd237e250f0 [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 Heof95a04a2012-04-16 13:57:26 -070062/*
63 * A blkcg_gq (blkg) is association between a block cgroup (blkcg) and a
64 * request_queue (q). This is used by blkcg policies which need to track
65 * information per blkcg - q pair.
66 *
67 * There can be multiple active blkcg policies and each has its private
68 * data on each blkg, the size of which is determined by
69 * blkcg_policy->pd_size. blkcg core allocates and frees such areas
70 * together with blkg and invokes pd_init/exit_fn() methods.
71 *
72 * Such private data must embed struct blkg_policy_data (pd) at the
73 * beginning and pd_size can't be smaller than pd.
74 */
Tejun Heo03814112012-03-05 13:15:14 -080075struct blkg_policy_data {
76 /* the blkg this per-policy data belongs to */
Tejun Heo3c798392012-04-16 13:57:25 -070077 struct blkcg_gq *blkg;
Tejun Heo03814112012-03-05 13:15:14 -080078
Tejun Heoa2b16932012-04-13 13:11:33 -070079 /* used during policy activation */
Tejun Heo36558c82012-04-16 13:57:24 -070080 struct list_head alloc_node;
Tejun Heo03814112012-03-05 13:15:14 -080081};
82
Tejun Heo3c798392012-04-16 13:57:25 -070083/* association between a blk cgroup and a request queue */
84struct blkcg_gq {
Tejun Heoc875f4d2012-03-05 13:15:22 -080085 /* Pointer to the associated request_queue */
Tejun Heo36558c82012-04-16 13:57:24 -070086 struct request_queue *q;
87 struct list_head q_node;
88 struct hlist_node blkcg_node;
Tejun Heo3c798392012-04-16 13:57:25 -070089 struct blkcg *blkcg;
Tejun Heo1adaf3d2012-03-05 13:15:15 -080090 /* reference count */
Tejun Heo36558c82012-04-16 13:57:24 -070091 int refcnt;
Vivek Goyal22084192009-12-03 12:59:49 -050092
Tejun Heo36558c82012-04-16 13:57:24 -070093 struct blkg_policy_data *pd[BLKCG_MAX_POLS];
Tejun Heo1adaf3d2012-03-05 13:15:15 -080094
Tejun Heo36558c82012-04-16 13:57:24 -070095 struct rcu_head rcu_head;
Vivek Goyal31e4c282009-12-03 12:59:42 -050096};
97
Tejun Heo3c798392012-04-16 13:57:25 -070098typedef void (blkcg_pol_init_pd_fn)(struct blkcg_gq *blkg);
99typedef void (blkcg_pol_exit_pd_fn)(struct blkcg_gq *blkg);
100typedef void (blkcg_pol_reset_pd_stats_fn)(struct blkcg_gq *blkg);
Vivek Goyal3e252062009-12-04 10:36:42 -0500101
Tejun Heo3c798392012-04-16 13:57:25 -0700102struct blkcg_policy {
Tejun Heo36558c82012-04-16 13:57:24 -0700103 int plid;
104 /* policy specific private data size */
Tejun Heof95a04a2012-04-16 13:57:26 -0700105 size_t pd_size;
Tejun Heo36558c82012-04-16 13:57:24 -0700106 /* cgroup files for the policy */
107 struct cftype *cftypes;
Tejun Heof9fcc2d2012-04-16 13:57:27 -0700108
109 /* operations */
110 blkcg_pol_init_pd_fn *pd_init_fn;
111 blkcg_pol_exit_pd_fn *pd_exit_fn;
112 blkcg_pol_reset_pd_stats_fn *pd_reset_stats_fn;
Vivek Goyal3e252062009-12-04 10:36:42 -0500113};
114
Tejun Heo3c798392012-04-16 13:57:25 -0700115extern struct blkcg blkcg_root;
Tejun Heo36558c82012-04-16 13:57:24 -0700116
Tejun Heo3c798392012-04-16 13:57:25 -0700117struct blkcg *cgroup_to_blkcg(struct cgroup *cgroup);
118struct blkcg *bio_blkcg(struct bio *bio);
119struct blkcg_gq *blkg_lookup(struct blkcg *blkcg, struct request_queue *q);
120struct blkcg_gq *blkg_lookup_create(struct blkcg *blkcg,
121 struct request_queue *q);
Tejun Heo36558c82012-04-16 13:57:24 -0700122int blkcg_init_queue(struct request_queue *q);
123void blkcg_drain_queue(struct request_queue *q);
124void blkcg_exit_queue(struct request_queue *q);
Tejun Heo5efd6112012-03-05 13:15:12 -0800125
Vivek Goyal3e252062009-12-04 10:36:42 -0500126/* Blkio controller policy registration */
Tejun Heo3c798392012-04-16 13:57:25 -0700127int blkcg_policy_register(struct blkcg_policy *pol);
128void blkcg_policy_unregister(struct blkcg_policy *pol);
Tejun Heo36558c82012-04-16 13:57:24 -0700129int blkcg_activate_policy(struct request_queue *q,
Tejun Heo3c798392012-04-16 13:57:25 -0700130 const struct blkcg_policy *pol);
Tejun Heo36558c82012-04-16 13:57:24 -0700131void blkcg_deactivate_policy(struct request_queue *q,
Tejun Heo3c798392012-04-16 13:57:25 -0700132 const struct blkcg_policy *pol);
Vivek Goyal3e252062009-12-04 10:36:42 -0500133
Tejun Heo3c798392012-04-16 13:57:25 -0700134void blkcg_print_blkgs(struct seq_file *sf, struct blkcg *blkcg,
Tejun Heof95a04a2012-04-16 13:57:26 -0700135 u64 (*prfill)(struct seq_file *,
136 struct blkg_policy_data *, int),
Tejun Heo3c798392012-04-16 13:57:25 -0700137 const struct blkcg_policy *pol, int data,
Tejun Heoec399342012-04-13 13:11:27 -0700138 bool show_total);
Tejun Heof95a04a2012-04-16 13:57:26 -0700139u64 __blkg_prfill_u64(struct seq_file *sf, struct blkg_policy_data *pd, u64 v);
140u64 __blkg_prfill_rwstat(struct seq_file *sf, struct blkg_policy_data *pd,
Tejun Heo829fdb52012-04-01 14:38:43 -0700141 const struct blkg_rwstat *rwstat);
Tejun Heof95a04a2012-04-16 13:57:26 -0700142u64 blkg_prfill_stat(struct seq_file *sf, struct blkg_policy_data *pd, int off);
143u64 blkg_prfill_rwstat(struct seq_file *sf, struct blkg_policy_data *pd,
144 int off);
Tejun Heo829fdb52012-04-01 14:38:43 -0700145
146struct blkg_conf_ctx {
Tejun Heo36558c82012-04-16 13:57:24 -0700147 struct gendisk *disk;
Tejun Heo3c798392012-04-16 13:57:25 -0700148 struct blkcg_gq *blkg;
Tejun Heo36558c82012-04-16 13:57:24 -0700149 u64 v;
Tejun Heo829fdb52012-04-01 14:38:43 -0700150};
151
Tejun Heo3c798392012-04-16 13:57:25 -0700152int blkg_conf_prep(struct blkcg *blkcg, const struct blkcg_policy *pol,
153 const char *input, struct blkg_conf_ctx *ctx);
Tejun Heo829fdb52012-04-01 14:38:43 -0700154void blkg_conf_finish(struct blkg_conf_ctx *ctx);
155
156
Tejun Heo03814112012-03-05 13:15:14 -0800157/**
158 * blkg_to_pdata - get policy private data
159 * @blkg: blkg of interest
160 * @pol: policy of interest
161 *
162 * Return pointer to private data associated with the @blkg-@pol pair.
163 */
Tejun Heof95a04a2012-04-16 13:57:26 -0700164static inline struct blkg_policy_data *blkg_to_pd(struct blkcg_gq *blkg,
165 struct blkcg_policy *pol)
Tejun Heo03814112012-03-05 13:15:14 -0800166{
Tejun Heof95a04a2012-04-16 13:57:26 -0700167 return blkg ? blkg->pd[pol->plid] : NULL;
Tejun Heo03814112012-03-05 13:15:14 -0800168}
169
170/**
171 * pdata_to_blkg - get blkg associated with policy private data
Tejun Heof95a04a2012-04-16 13:57:26 -0700172 * @pd: policy private data of interest
Tejun Heo03814112012-03-05 13:15:14 -0800173 *
Tejun Heof95a04a2012-04-16 13:57:26 -0700174 * @pd is policy private data. Determine the blkg it's associated with.
Tejun Heo03814112012-03-05 13:15:14 -0800175 */
Tejun Heof95a04a2012-04-16 13:57:26 -0700176static inline struct blkcg_gq *pd_to_blkg(struct blkg_policy_data *pd)
Tejun Heo03814112012-03-05 13:15:14 -0800177{
Tejun Heof95a04a2012-04-16 13:57:26 -0700178 return pd ? pd->blkg : NULL;
Tejun Heo03814112012-03-05 13:15:14 -0800179}
180
Tejun Heo54e7ed12012-04-16 13:57:23 -0700181/**
182 * blkg_path - format cgroup path of blkg
183 * @blkg: blkg of interest
184 * @buf: target buffer
185 * @buflen: target buffer length
186 *
187 * Format the path of the cgroup of @blkg into @buf.
188 */
Tejun Heo3c798392012-04-16 13:57:25 -0700189static inline int blkg_path(struct blkcg_gq *blkg, char *buf, int buflen)
Vivek Goyalafc24d42010-04-26 19:27:56 +0200190{
Tejun Heo54e7ed12012-04-16 13:57:23 -0700191 int ret;
192
193 rcu_read_lock();
194 ret = cgroup_path(blkg->blkcg->css.cgroup, buf, buflen);
195 rcu_read_unlock();
196 if (ret)
197 strncpy(buf, "<unavailable>", buflen);
198 return ret;
Vivek Goyalafc24d42010-04-26 19:27:56 +0200199}
200
Tejun Heo1adaf3d2012-03-05 13:15:15 -0800201/**
202 * blkg_get - get a blkg reference
203 * @blkg: blkg to get
204 *
205 * The caller should be holding queue_lock and an existing reference.
206 */
Tejun Heo3c798392012-04-16 13:57:25 -0700207static inline void blkg_get(struct blkcg_gq *blkg)
Tejun Heo1adaf3d2012-03-05 13:15:15 -0800208{
209 lockdep_assert_held(blkg->q->queue_lock);
210 WARN_ON_ONCE(!blkg->refcnt);
211 blkg->refcnt++;
212}
213
Tejun Heo3c798392012-04-16 13:57:25 -0700214void __blkg_release(struct blkcg_gq *blkg);
Tejun Heo1adaf3d2012-03-05 13:15:15 -0800215
216/**
217 * blkg_put - put a blkg reference
218 * @blkg: blkg to put
219 *
220 * The caller should be holding queue_lock.
221 */
Tejun Heo3c798392012-04-16 13:57:25 -0700222static inline void blkg_put(struct blkcg_gq *blkg)
Tejun Heo1adaf3d2012-03-05 13:15:15 -0800223{
224 lockdep_assert_held(blkg->q->queue_lock);
225 WARN_ON_ONCE(blkg->refcnt <= 0);
226 if (!--blkg->refcnt)
227 __blkg_release(blkg);
228}
229
Tejun Heoedcb0722012-04-01 14:38:42 -0700230/**
231 * blkg_stat_add - add a value to a blkg_stat
232 * @stat: target blkg_stat
233 * @val: value to add
234 *
235 * Add @val to @stat. The caller is responsible for synchronizing calls to
236 * this function.
237 */
238static inline void blkg_stat_add(struct blkg_stat *stat, uint64_t val)
239{
240 u64_stats_update_begin(&stat->syncp);
241 stat->cnt += val;
242 u64_stats_update_end(&stat->syncp);
243}
244
245/**
246 * blkg_stat_read - read the current value of a blkg_stat
247 * @stat: blkg_stat to read
248 *
249 * Read the current value of @stat. This function can be called without
250 * synchroniztion and takes care of u64 atomicity.
251 */
252static inline uint64_t blkg_stat_read(struct blkg_stat *stat)
253{
254 unsigned int start;
255 uint64_t v;
256
257 do {
258 start = u64_stats_fetch_begin(&stat->syncp);
259 v = stat->cnt;
260 } while (u64_stats_fetch_retry(&stat->syncp, start));
261
262 return v;
263}
264
265/**
266 * blkg_stat_reset - reset a blkg_stat
267 * @stat: blkg_stat to reset
268 */
269static inline void blkg_stat_reset(struct blkg_stat *stat)
270{
271 stat->cnt = 0;
272}
273
274/**
275 * blkg_rwstat_add - add a value to a blkg_rwstat
276 * @rwstat: target blkg_rwstat
277 * @rw: mask of REQ_{WRITE|SYNC}
278 * @val: value to add
279 *
280 * Add @val to @rwstat. The counters are chosen according to @rw. The
281 * caller is responsible for synchronizing calls to this function.
282 */
283static inline void blkg_rwstat_add(struct blkg_rwstat *rwstat,
284 int rw, uint64_t val)
285{
286 u64_stats_update_begin(&rwstat->syncp);
287
288 if (rw & REQ_WRITE)
289 rwstat->cnt[BLKG_RWSTAT_WRITE] += val;
290 else
291 rwstat->cnt[BLKG_RWSTAT_READ] += val;
292 if (rw & REQ_SYNC)
293 rwstat->cnt[BLKG_RWSTAT_SYNC] += val;
294 else
295 rwstat->cnt[BLKG_RWSTAT_ASYNC] += val;
296
297 u64_stats_update_end(&rwstat->syncp);
298}
299
300/**
301 * blkg_rwstat_read - read the current values of a blkg_rwstat
302 * @rwstat: blkg_rwstat to read
303 *
304 * Read the current snapshot of @rwstat and return it as the return value.
305 * This function can be called without synchronization and takes care of
306 * u64 atomicity.
307 */
Tejun Heoc94bed892012-04-16 13:57:22 -0700308static inline struct blkg_rwstat blkg_rwstat_read(struct blkg_rwstat *rwstat)
Tejun Heoedcb0722012-04-01 14:38:42 -0700309{
310 unsigned int start;
311 struct blkg_rwstat tmp;
312
313 do {
314 start = u64_stats_fetch_begin(&rwstat->syncp);
315 tmp = *rwstat;
316 } while (u64_stats_fetch_retry(&rwstat->syncp, start));
317
318 return tmp;
319}
320
321/**
322 * blkg_rwstat_sum - read the total count of a blkg_rwstat
323 * @rwstat: blkg_rwstat to read
324 *
325 * Return the total count of @rwstat regardless of the IO direction. This
326 * function can be called without synchronization and takes care of u64
327 * atomicity.
328 */
329static inline uint64_t blkg_rwstat_sum(struct blkg_rwstat *rwstat)
330{
331 struct blkg_rwstat tmp = blkg_rwstat_read(rwstat);
332
333 return tmp.cnt[BLKG_RWSTAT_READ] + tmp.cnt[BLKG_RWSTAT_WRITE];
334}
335
336/**
337 * blkg_rwstat_reset - reset a blkg_rwstat
338 * @rwstat: blkg_rwstat to reset
339 */
340static inline void blkg_rwstat_reset(struct blkg_rwstat *rwstat)
341{
342 memset(rwstat->cnt, 0, sizeof(rwstat->cnt));
343}
344
Tejun Heo36558c82012-04-16 13:57:24 -0700345#else /* CONFIG_BLK_CGROUP */
346
347struct cgroup;
Jens Axboe2f5ea472009-12-03 21:06:43 +0100348
Tejun Heof95a04a2012-04-16 13:57:26 -0700349struct blkg_policy_data {
350};
351
Tejun Heo3c798392012-04-16 13:57:25 -0700352struct blkcg_gq {
Jens Axboe2f5ea472009-12-03 21:06:43 +0100353};
354
Tejun Heo3c798392012-04-16 13:57:25 -0700355struct blkcg_policy {
Vivek Goyal3e252062009-12-04 10:36:42 -0500356};
357
Tejun Heo3c798392012-04-16 13:57:25 -0700358static inline struct blkcg *cgroup_to_blkcg(struct cgroup *cgroup) { return NULL; }
359static inline struct blkcg *bio_blkcg(struct bio *bio) { return NULL; }
360static inline struct blkcg_gq *blkg_lookup(struct blkcg *blkcg, void *key) { return NULL; }
Tejun Heo5efd6112012-03-05 13:15:12 -0800361static inline int blkcg_init_queue(struct request_queue *q) { return 0; }
362static inline void blkcg_drain_queue(struct request_queue *q) { }
363static inline void blkcg_exit_queue(struct request_queue *q) { }
Tejun Heo3c798392012-04-16 13:57:25 -0700364static inline int blkcg_policy_register(struct blkcg_policy *pol) { return 0; }
365static inline void blkcg_policy_unregister(struct blkcg_policy *pol) { }
Tejun Heoa2b16932012-04-13 13:11:33 -0700366static inline int blkcg_activate_policy(struct request_queue *q,
Tejun Heo3c798392012-04-16 13:57:25 -0700367 const struct blkcg_policy *pol) { return 0; }
Tejun Heoa2b16932012-04-13 13:11:33 -0700368static inline void blkcg_deactivate_policy(struct request_queue *q,
Tejun Heo3c798392012-04-16 13:57:25 -0700369 const struct blkcg_policy *pol) { }
Vivek Goyal3e252062009-12-04 10:36:42 -0500370
Tejun Heof95a04a2012-04-16 13:57:26 -0700371static inline struct blkg_policy_data *blkg_to_pd(struct blkcg_gq *blkg,
372 struct blkcg_policy *pol) { return NULL; }
373static inline struct blkcg_gq *pd_to_blkg(struct blkg_policy_data *pd) { return NULL; }
Tejun Heo3c798392012-04-16 13:57:25 -0700374static inline char *blkg_path(struct blkcg_gq *blkg) { return NULL; }
375static inline void blkg_get(struct blkcg_gq *blkg) { }
376static inline void blkg_put(struct blkcg_gq *blkg) { }
Vivek Goyalafc24d42010-04-26 19:27:56 +0200377
Tejun Heo36558c82012-04-16 13:57:24 -0700378#endif /* CONFIG_BLK_CGROUP */
379#endif /* _BLK_CGROUP_H */