blob: 8ac457ce7783847522c1340c008d3c7789f3a1b2 [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>
Tejun Heoa6371202012-04-19 16:29:24 -070019#include <linux/radix-tree.h>
Vivek Goyal31e4c282009-12-03 12:59:42 -050020
Vivek Goyal9355aed2010-10-01 21:16:41 +020021/* Max limits for throttle policy */
22#define THROTL_IOPS_MAX UINT_MAX
23
Tejun Heo3381cb82012-04-01 14:38:44 -070024/* CFQ specific, out here for blkcg->cfq_weight */
25#define CFQ_WEIGHT_MIN 10
26#define CFQ_WEIGHT_MAX 1000
27#define CFQ_WEIGHT_DEFAULT 500
28
Tejun Heof48ec1d2012-04-13 13:11:25 -070029#ifdef CONFIG_BLK_CGROUP
30
Tejun Heoedcb0722012-04-01 14:38:42 -070031enum blkg_rwstat_type {
32 BLKG_RWSTAT_READ,
33 BLKG_RWSTAT_WRITE,
34 BLKG_RWSTAT_SYNC,
35 BLKG_RWSTAT_ASYNC,
36
37 BLKG_RWSTAT_NR,
38 BLKG_RWSTAT_TOTAL = BLKG_RWSTAT_NR,
Divyesh Shah303a3ac2010-04-01 15:01:24 -070039};
40
Tejun Heoa6371202012-04-19 16:29:24 -070041struct blkcg_gq;
42
Tejun Heo3c798392012-04-16 13:57:25 -070043struct blkcg {
Tejun Heo36558c82012-04-16 13:57:24 -070044 struct cgroup_subsys_state css;
45 spinlock_t lock;
Tejun Heoa6371202012-04-19 16:29:24 -070046
47 struct radix_tree_root blkg_tree;
48 struct blkcg_gq *blkg_hint;
Tejun Heo36558c82012-04-16 13:57:24 -070049 struct hlist_head blkg_list;
Tejun Heo9a9e8a22012-03-19 15:10:56 -070050
51 /* for policies to test whether associated blkcg has changed */
Tejun Heo36558c82012-04-16 13:57:24 -070052 uint64_t id;
Tejun Heo3381cb82012-04-01 14:38:44 -070053
Tejun Heo3c798392012-04-16 13:57:25 -070054 /* TODO: per-policy storage in blkcg */
Tejun Heo36558c82012-04-16 13:57:24 -070055 unsigned int cfq_weight; /* belongs to cfq */
Vivek Goyal31e4c282009-12-03 12:59:42 -050056};
57
Tejun Heoedcb0722012-04-01 14:38:42 -070058struct blkg_stat {
59 struct u64_stats_sync syncp;
60 uint64_t cnt;
61};
62
63struct blkg_rwstat {
64 struct u64_stats_sync syncp;
65 uint64_t cnt[BLKG_RWSTAT_NR];
66};
67
Tejun Heof95a04a2012-04-16 13:57:26 -070068/*
69 * A blkcg_gq (blkg) is association between a block cgroup (blkcg) and a
70 * request_queue (q). This is used by blkcg policies which need to track
71 * information per blkcg - q pair.
72 *
73 * There can be multiple active blkcg policies and each has its private
74 * data on each blkg, the size of which is determined by
75 * blkcg_policy->pd_size. blkcg core allocates and frees such areas
76 * together with blkg and invokes pd_init/exit_fn() methods.
77 *
78 * Such private data must embed struct blkg_policy_data (pd) at the
79 * beginning and pd_size can't be smaller than pd.
80 */
Tejun Heo03814112012-03-05 13:15:14 -080081struct blkg_policy_data {
82 /* the blkg this per-policy data belongs to */
Tejun Heo3c798392012-04-16 13:57:25 -070083 struct blkcg_gq *blkg;
Tejun Heo03814112012-03-05 13:15:14 -080084
Tejun Heoa2b16932012-04-13 13:11:33 -070085 /* used during policy activation */
Tejun Heo36558c82012-04-16 13:57:24 -070086 struct list_head alloc_node;
Tejun Heo03814112012-03-05 13:15:14 -080087};
88
Tejun Heo3c798392012-04-16 13:57:25 -070089/* association between a blk cgroup and a request queue */
90struct blkcg_gq {
Tejun Heoc875f4d2012-03-05 13:15:22 -080091 /* Pointer to the associated request_queue */
Tejun Heo36558c82012-04-16 13:57:24 -070092 struct request_queue *q;
93 struct list_head q_node;
94 struct hlist_node blkcg_node;
Tejun Heo3c798392012-04-16 13:57:25 -070095 struct blkcg *blkcg;
Tejun Heo1adaf3d2012-03-05 13:15:15 -080096 /* reference count */
Tejun Heo36558c82012-04-16 13:57:24 -070097 int refcnt;
Vivek Goyal22084192009-12-03 12:59:49 -050098
Tejun Heo36558c82012-04-16 13:57:24 -070099 struct blkg_policy_data *pd[BLKCG_MAX_POLS];
Tejun Heo1adaf3d2012-03-05 13:15:15 -0800100
Tejun Heo36558c82012-04-16 13:57:24 -0700101 struct rcu_head rcu_head;
Vivek Goyal31e4c282009-12-03 12:59:42 -0500102};
103
Tejun Heo3c798392012-04-16 13:57:25 -0700104typedef void (blkcg_pol_init_pd_fn)(struct blkcg_gq *blkg);
105typedef void (blkcg_pol_exit_pd_fn)(struct blkcg_gq *blkg);
106typedef void (blkcg_pol_reset_pd_stats_fn)(struct blkcg_gq *blkg);
Vivek Goyal3e252062009-12-04 10:36:42 -0500107
Tejun Heo3c798392012-04-16 13:57:25 -0700108struct blkcg_policy {
Tejun Heo36558c82012-04-16 13:57:24 -0700109 int plid;
110 /* policy specific private data size */
Tejun Heof95a04a2012-04-16 13:57:26 -0700111 size_t pd_size;
Tejun Heo36558c82012-04-16 13:57:24 -0700112 /* cgroup files for the policy */
113 struct cftype *cftypes;
Tejun Heof9fcc2d2012-04-16 13:57:27 -0700114
115 /* operations */
116 blkcg_pol_init_pd_fn *pd_init_fn;
117 blkcg_pol_exit_pd_fn *pd_exit_fn;
118 blkcg_pol_reset_pd_stats_fn *pd_reset_stats_fn;
Vivek Goyal3e252062009-12-04 10:36:42 -0500119};
120
Tejun Heo3c798392012-04-16 13:57:25 -0700121extern struct blkcg blkcg_root;
Tejun Heo36558c82012-04-16 13:57:24 -0700122
Tejun Heo3c798392012-04-16 13:57:25 -0700123struct blkcg *cgroup_to_blkcg(struct cgroup *cgroup);
124struct blkcg *bio_blkcg(struct bio *bio);
125struct blkcg_gq *blkg_lookup(struct blkcg *blkcg, struct request_queue *q);
126struct blkcg_gq *blkg_lookup_create(struct blkcg *blkcg,
127 struct request_queue *q);
Tejun Heo36558c82012-04-16 13:57:24 -0700128int blkcg_init_queue(struct request_queue *q);
129void blkcg_drain_queue(struct request_queue *q);
130void blkcg_exit_queue(struct request_queue *q);
Tejun Heo5efd6112012-03-05 13:15:12 -0800131
Vivek Goyal3e252062009-12-04 10:36:42 -0500132/* Blkio controller policy registration */
Tejun Heo3c798392012-04-16 13:57:25 -0700133int blkcg_policy_register(struct blkcg_policy *pol);
134void blkcg_policy_unregister(struct blkcg_policy *pol);
Tejun Heo36558c82012-04-16 13:57:24 -0700135int blkcg_activate_policy(struct request_queue *q,
Tejun Heo3c798392012-04-16 13:57:25 -0700136 const struct blkcg_policy *pol);
Tejun Heo36558c82012-04-16 13:57:24 -0700137void blkcg_deactivate_policy(struct request_queue *q,
Tejun Heo3c798392012-04-16 13:57:25 -0700138 const struct blkcg_policy *pol);
Vivek Goyal3e252062009-12-04 10:36:42 -0500139
Tejun Heo3c798392012-04-16 13:57:25 -0700140void blkcg_print_blkgs(struct seq_file *sf, struct blkcg *blkcg,
Tejun Heof95a04a2012-04-16 13:57:26 -0700141 u64 (*prfill)(struct seq_file *,
142 struct blkg_policy_data *, int),
Tejun Heo3c798392012-04-16 13:57:25 -0700143 const struct blkcg_policy *pol, int data,
Tejun Heoec399342012-04-13 13:11:27 -0700144 bool show_total);
Tejun Heof95a04a2012-04-16 13:57:26 -0700145u64 __blkg_prfill_u64(struct seq_file *sf, struct blkg_policy_data *pd, u64 v);
146u64 __blkg_prfill_rwstat(struct seq_file *sf, struct blkg_policy_data *pd,
Tejun Heo829fdb52012-04-01 14:38:43 -0700147 const struct blkg_rwstat *rwstat);
Tejun Heof95a04a2012-04-16 13:57:26 -0700148u64 blkg_prfill_stat(struct seq_file *sf, struct blkg_policy_data *pd, int off);
149u64 blkg_prfill_rwstat(struct seq_file *sf, struct blkg_policy_data *pd,
150 int off);
Tejun Heo829fdb52012-04-01 14:38:43 -0700151
152struct blkg_conf_ctx {
Tejun Heo36558c82012-04-16 13:57:24 -0700153 struct gendisk *disk;
Tejun Heo3c798392012-04-16 13:57:25 -0700154 struct blkcg_gq *blkg;
Tejun Heo36558c82012-04-16 13:57:24 -0700155 u64 v;
Tejun Heo829fdb52012-04-01 14:38:43 -0700156};
157
Tejun Heo3c798392012-04-16 13:57:25 -0700158int blkg_conf_prep(struct blkcg *blkcg, const struct blkcg_policy *pol,
159 const char *input, struct blkg_conf_ctx *ctx);
Tejun Heo829fdb52012-04-01 14:38:43 -0700160void blkg_conf_finish(struct blkg_conf_ctx *ctx);
161
162
Tejun Heo03814112012-03-05 13:15:14 -0800163/**
164 * blkg_to_pdata - get policy private data
165 * @blkg: blkg of interest
166 * @pol: policy of interest
167 *
168 * Return pointer to private data associated with the @blkg-@pol pair.
169 */
Tejun Heof95a04a2012-04-16 13:57:26 -0700170static inline struct blkg_policy_data *blkg_to_pd(struct blkcg_gq *blkg,
171 struct blkcg_policy *pol)
Tejun Heo03814112012-03-05 13:15:14 -0800172{
Tejun Heof95a04a2012-04-16 13:57:26 -0700173 return blkg ? blkg->pd[pol->plid] : NULL;
Tejun Heo03814112012-03-05 13:15:14 -0800174}
175
176/**
177 * pdata_to_blkg - get blkg associated with policy private data
Tejun Heof95a04a2012-04-16 13:57:26 -0700178 * @pd: policy private data of interest
Tejun Heo03814112012-03-05 13:15:14 -0800179 *
Tejun Heof95a04a2012-04-16 13:57:26 -0700180 * @pd is policy private data. Determine the blkg it's associated with.
Tejun Heo03814112012-03-05 13:15:14 -0800181 */
Tejun Heof95a04a2012-04-16 13:57:26 -0700182static inline struct blkcg_gq *pd_to_blkg(struct blkg_policy_data *pd)
Tejun Heo03814112012-03-05 13:15:14 -0800183{
Tejun Heof95a04a2012-04-16 13:57:26 -0700184 return pd ? pd->blkg : NULL;
Tejun Heo03814112012-03-05 13:15:14 -0800185}
186
Tejun Heo54e7ed12012-04-16 13:57:23 -0700187/**
188 * blkg_path - format cgroup path of blkg
189 * @blkg: blkg of interest
190 * @buf: target buffer
191 * @buflen: target buffer length
192 *
193 * Format the path of the cgroup of @blkg into @buf.
194 */
Tejun Heo3c798392012-04-16 13:57:25 -0700195static inline int blkg_path(struct blkcg_gq *blkg, char *buf, int buflen)
Vivek Goyalafc24d42010-04-26 19:27:56 +0200196{
Tejun Heo54e7ed12012-04-16 13:57:23 -0700197 int ret;
198
199 rcu_read_lock();
200 ret = cgroup_path(blkg->blkcg->css.cgroup, buf, buflen);
201 rcu_read_unlock();
202 if (ret)
203 strncpy(buf, "<unavailable>", buflen);
204 return ret;
Vivek Goyalafc24d42010-04-26 19:27:56 +0200205}
206
Tejun Heo1adaf3d2012-03-05 13:15:15 -0800207/**
208 * blkg_get - get a blkg reference
209 * @blkg: blkg to get
210 *
211 * The caller should be holding queue_lock and an existing reference.
212 */
Tejun Heo3c798392012-04-16 13:57:25 -0700213static inline void blkg_get(struct blkcg_gq *blkg)
Tejun Heo1adaf3d2012-03-05 13:15:15 -0800214{
215 lockdep_assert_held(blkg->q->queue_lock);
216 WARN_ON_ONCE(!blkg->refcnt);
217 blkg->refcnt++;
218}
219
Tejun Heo3c798392012-04-16 13:57:25 -0700220void __blkg_release(struct blkcg_gq *blkg);
Tejun Heo1adaf3d2012-03-05 13:15:15 -0800221
222/**
223 * blkg_put - put a blkg reference
224 * @blkg: blkg to put
225 *
226 * The caller should be holding queue_lock.
227 */
Tejun Heo3c798392012-04-16 13:57:25 -0700228static inline void blkg_put(struct blkcg_gq *blkg)
Tejun Heo1adaf3d2012-03-05 13:15:15 -0800229{
230 lockdep_assert_held(blkg->q->queue_lock);
231 WARN_ON_ONCE(blkg->refcnt <= 0);
232 if (!--blkg->refcnt)
233 __blkg_release(blkg);
234}
235
Tejun Heoedcb0722012-04-01 14:38:42 -0700236/**
237 * blkg_stat_add - add a value to a blkg_stat
238 * @stat: target blkg_stat
239 * @val: value to add
240 *
241 * Add @val to @stat. The caller is responsible for synchronizing calls to
242 * this function.
243 */
244static inline void blkg_stat_add(struct blkg_stat *stat, uint64_t val)
245{
246 u64_stats_update_begin(&stat->syncp);
247 stat->cnt += val;
248 u64_stats_update_end(&stat->syncp);
249}
250
251/**
252 * blkg_stat_read - read the current value of a blkg_stat
253 * @stat: blkg_stat to read
254 *
255 * Read the current value of @stat. This function can be called without
256 * synchroniztion and takes care of u64 atomicity.
257 */
258static inline uint64_t blkg_stat_read(struct blkg_stat *stat)
259{
260 unsigned int start;
261 uint64_t v;
262
263 do {
264 start = u64_stats_fetch_begin(&stat->syncp);
265 v = stat->cnt;
266 } while (u64_stats_fetch_retry(&stat->syncp, start));
267
268 return v;
269}
270
271/**
272 * blkg_stat_reset - reset a blkg_stat
273 * @stat: blkg_stat to reset
274 */
275static inline void blkg_stat_reset(struct blkg_stat *stat)
276{
277 stat->cnt = 0;
278}
279
280/**
281 * blkg_rwstat_add - add a value to a blkg_rwstat
282 * @rwstat: target blkg_rwstat
283 * @rw: mask of REQ_{WRITE|SYNC}
284 * @val: value to add
285 *
286 * Add @val to @rwstat. The counters are chosen according to @rw. The
287 * caller is responsible for synchronizing calls to this function.
288 */
289static inline void blkg_rwstat_add(struct blkg_rwstat *rwstat,
290 int rw, uint64_t val)
291{
292 u64_stats_update_begin(&rwstat->syncp);
293
294 if (rw & REQ_WRITE)
295 rwstat->cnt[BLKG_RWSTAT_WRITE] += val;
296 else
297 rwstat->cnt[BLKG_RWSTAT_READ] += val;
298 if (rw & REQ_SYNC)
299 rwstat->cnt[BLKG_RWSTAT_SYNC] += val;
300 else
301 rwstat->cnt[BLKG_RWSTAT_ASYNC] += val;
302
303 u64_stats_update_end(&rwstat->syncp);
304}
305
306/**
307 * blkg_rwstat_read - read the current values of a blkg_rwstat
308 * @rwstat: blkg_rwstat to read
309 *
310 * Read the current snapshot of @rwstat and return it as the return value.
311 * This function can be called without synchronization and takes care of
312 * u64 atomicity.
313 */
Tejun Heoc94bed892012-04-16 13:57:22 -0700314static inline struct blkg_rwstat blkg_rwstat_read(struct blkg_rwstat *rwstat)
Tejun Heoedcb0722012-04-01 14:38:42 -0700315{
316 unsigned int start;
317 struct blkg_rwstat tmp;
318
319 do {
320 start = u64_stats_fetch_begin(&rwstat->syncp);
321 tmp = *rwstat;
322 } while (u64_stats_fetch_retry(&rwstat->syncp, start));
323
324 return tmp;
325}
326
327/**
328 * blkg_rwstat_sum - read the total count of a blkg_rwstat
329 * @rwstat: blkg_rwstat to read
330 *
331 * Return the total count of @rwstat regardless of the IO direction. This
332 * function can be called without synchronization and takes care of u64
333 * atomicity.
334 */
335static inline uint64_t blkg_rwstat_sum(struct blkg_rwstat *rwstat)
336{
337 struct blkg_rwstat tmp = blkg_rwstat_read(rwstat);
338
339 return tmp.cnt[BLKG_RWSTAT_READ] + tmp.cnt[BLKG_RWSTAT_WRITE];
340}
341
342/**
343 * blkg_rwstat_reset - reset a blkg_rwstat
344 * @rwstat: blkg_rwstat to reset
345 */
346static inline void blkg_rwstat_reset(struct blkg_rwstat *rwstat)
347{
348 memset(rwstat->cnt, 0, sizeof(rwstat->cnt));
349}
350
Tejun Heo36558c82012-04-16 13:57:24 -0700351#else /* CONFIG_BLK_CGROUP */
352
353struct cgroup;
Jens Axboe2f5ea472009-12-03 21:06:43 +0100354
Tejun Heof95a04a2012-04-16 13:57:26 -0700355struct blkg_policy_data {
356};
357
Tejun Heo3c798392012-04-16 13:57:25 -0700358struct blkcg_gq {
Jens Axboe2f5ea472009-12-03 21:06:43 +0100359};
360
Tejun Heo3c798392012-04-16 13:57:25 -0700361struct blkcg_policy {
Vivek Goyal3e252062009-12-04 10:36:42 -0500362};
363
Tejun Heo3c798392012-04-16 13:57:25 -0700364static inline struct blkcg *cgroup_to_blkcg(struct cgroup *cgroup) { return NULL; }
365static inline struct blkcg *bio_blkcg(struct bio *bio) { return NULL; }
366static inline struct blkcg_gq *blkg_lookup(struct blkcg *blkcg, void *key) { return NULL; }
Tejun Heo5efd6112012-03-05 13:15:12 -0800367static inline int blkcg_init_queue(struct request_queue *q) { return 0; }
368static inline void blkcg_drain_queue(struct request_queue *q) { }
369static inline void blkcg_exit_queue(struct request_queue *q) { }
Tejun Heo3c798392012-04-16 13:57:25 -0700370static inline int blkcg_policy_register(struct blkcg_policy *pol) { return 0; }
371static inline void blkcg_policy_unregister(struct blkcg_policy *pol) { }
Tejun Heoa2b16932012-04-13 13:11:33 -0700372static inline int blkcg_activate_policy(struct request_queue *q,
Tejun Heo3c798392012-04-16 13:57:25 -0700373 const struct blkcg_policy *pol) { return 0; }
Tejun Heoa2b16932012-04-13 13:11:33 -0700374static inline void blkcg_deactivate_policy(struct request_queue *q,
Tejun Heo3c798392012-04-16 13:57:25 -0700375 const struct blkcg_policy *pol) { }
Vivek Goyal3e252062009-12-04 10:36:42 -0500376
Tejun Heof95a04a2012-04-16 13:57:26 -0700377static inline struct blkg_policy_data *blkg_to_pd(struct blkcg_gq *blkg,
378 struct blkcg_policy *pol) { return NULL; }
379static inline struct blkcg_gq *pd_to_blkg(struct blkg_policy_data *pd) { return NULL; }
Tejun Heo3c798392012-04-16 13:57:25 -0700380static inline char *blkg_path(struct blkcg_gq *blkg) { return NULL; }
381static inline void blkg_get(struct blkcg_gq *blkg) { }
382static inline void blkg_put(struct blkcg_gq *blkg) { }
Vivek Goyalafc24d42010-04-26 19:27:56 +0200383
Tejun Heo36558c82012-04-16 13:57:24 -0700384#endif /* CONFIG_BLK_CGROUP */
385#endif /* _BLK_CGROUP_H */