]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - block/blk-cgroup.h
blkcg: blkg_rwstat_read() was missing inline
[mirror_ubuntu-zesty-kernel.git] / block / blk-cgroup.h
CommitLineData
31e4c28d
VG
1#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>
575969a0 17#include <linux/u64_stats_sync.h>
829fdb50 18#include <linux/seq_file.h>
31e4c28d 19
9355aede
VG
20/* Max limits for throttle policy */
21#define THROTL_IOPS_MAX UINT_MAX
22
3381cb8d
TH
23/* 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
f48ec1d7
TH
28#ifdef CONFIG_BLK_CGROUP
29
edcb0722
TH
30enum 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,
303a3acb
DS
38};
39
31e4c28d
VG
40struct blkio_cgroup {
41 struct cgroup_subsys_state css;
31e4c28d
VG
42 spinlock_t lock;
43 struct hlist_head blkg_list;
9a9e8a26
TH
44
45 /* for policies to test whether associated blkcg has changed */
46 uint64_t id;
3381cb8d
TH
47
48 /* TODO: per-policy storage in blkio_cgroup */
49 unsigned int cfq_weight; /* belongs to cfq */
31e4c28d
VG
50};
51
edcb0722
TH
52struct 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
0381411e
TH
62/* per-blkg per-policy data */
63struct blkg_policy_data {
64 /* the blkg this per-policy data belongs to */
65 struct blkio_group *blkg;
66
a2b1693b
TH
67 /* used during policy activation */
68 struct list_head alloc_node;
69
0381411e
TH
70 /* pol->pdata_size bytes of private data used by policy impl */
71 char pdata[] __aligned(__alignof__(unsigned long long));
72};
73
31e4c28d 74struct blkio_group {
c875f4d0
TH
75 /* Pointer to the associated request_queue */
76 struct request_queue *q;
e8989fae 77 struct list_head q_node;
31e4c28d 78 struct hlist_node blkcg_node;
7ee9c562 79 struct blkio_cgroup *blkcg;
2868ef7b
VG
80 /* Store cgroup path */
81 char path[128];
1adaf3dd
TH
82 /* reference count */
83 int refcnt;
22084190 84
8bd435b3 85 struct blkg_policy_data *pd[BLKCG_MAX_POLS];
1adaf3dd
TH
86
87 struct rcu_head rcu_head;
31e4c28d
VG
88};
89
0381411e 90typedef void (blkio_init_group_fn)(struct blkio_group *blkg);
9ade5ea4
TH
91typedef void (blkio_exit_group_fn)(struct blkio_group *blkg);
92typedef void (blkio_reset_group_stats_fn)(struct blkio_group *blkg);
3e252066
VG
93
94struct blkio_policy_ops {
0381411e 95 blkio_init_group_fn *blkio_init_group_fn;
9ade5ea4
TH
96 blkio_exit_group_fn *blkio_exit_group_fn;
97 blkio_reset_group_stats_fn *blkio_reset_group_stats_fn;
3e252066
VG
98};
99
100struct blkio_policy_type {
3e252066 101 struct blkio_policy_ops ops;
8bd435b3 102 int plid;
0381411e 103 size_t pdata_size; /* policy specific private data size */
44ea53de 104 struct cftype *cftypes; /* cgroup files for the policy */
3e252066
VG
105};
106
5efd6113
TH
107extern int blkcg_init_queue(struct request_queue *q);
108extern void blkcg_drain_queue(struct request_queue *q);
109extern void blkcg_exit_queue(struct request_queue *q);
110
3e252066 111/* Blkio controller policy registration */
8bd435b3 112extern int blkio_policy_register(struct blkio_policy_type *);
3e252066 113extern void blkio_policy_unregister(struct blkio_policy_type *);
a2b1693b
TH
114extern int blkcg_activate_policy(struct request_queue *q,
115 const struct blkio_policy_type *pol);
116extern void blkcg_deactivate_policy(struct request_queue *q,
117 const struct blkio_policy_type *pol);
3e252066 118
829fdb50 119void blkcg_print_blkgs(struct seq_file *sf, struct blkio_cgroup *blkcg,
d366e7ec 120 u64 (*prfill)(struct seq_file *, void *, int),
ec399347
TH
121 const struct blkio_policy_type *pol, int data,
122 bool show_total);
d366e7ec
TH
123u64 __blkg_prfill_u64(struct seq_file *sf, void *pdata, u64 v);
124u64 __blkg_prfill_rwstat(struct seq_file *sf, void *pdata,
829fdb50 125 const struct blkg_rwstat *rwstat);
5bc4afb1
TH
126u64 blkg_prfill_stat(struct seq_file *sf, void *pdata, int off);
127u64 blkg_prfill_rwstat(struct seq_file *sf, void *pdata, int off);
829fdb50
TH
128
129struct blkg_conf_ctx {
130 struct gendisk *disk;
131 struct blkio_group *blkg;
132 u64 v;
133};
134
da8b0662
TH
135int blkg_conf_prep(struct blkio_cgroup *blkcg,
136 const struct blkio_policy_type *pol, const char *input,
829fdb50
TH
137 struct blkg_conf_ctx *ctx);
138void blkg_conf_finish(struct blkg_conf_ctx *ctx);
139
140
0381411e
TH
141/**
142 * blkg_to_pdata - get policy private data
143 * @blkg: blkg of interest
144 * @pol: policy of interest
145 *
146 * Return pointer to private data associated with the @blkg-@pol pair.
147 */
148static inline void *blkg_to_pdata(struct blkio_group *blkg,
149 struct blkio_policy_type *pol)
150{
549d3aa8 151 return blkg ? blkg->pd[pol->plid]->pdata : NULL;
0381411e
TH
152}
153
154/**
155 * pdata_to_blkg - get blkg associated with policy private data
156 * @pdata: policy private data of interest
0381411e 157 *
aaec55a0 158 * @pdata is policy private data. Determine the blkg it's associated with.
0381411e 159 */
aaec55a0 160static inline struct blkio_group *pdata_to_blkg(void *pdata)
0381411e
TH
161{
162 if (pdata) {
163 struct blkg_policy_data *pd =
164 container_of(pdata, struct blkg_policy_data, pdata);
165 return pd->blkg;
166 }
167 return NULL;
168}
169
afc24d49
VG
170static inline char *blkg_path(struct blkio_group *blkg)
171{
172 return blkg->path;
173}
174
1adaf3dd
TH
175/**
176 * blkg_get - get a blkg reference
177 * @blkg: blkg to get
178 *
179 * The caller should be holding queue_lock and an existing reference.
180 */
181static inline void blkg_get(struct blkio_group *blkg)
182{
183 lockdep_assert_held(blkg->q->queue_lock);
184 WARN_ON_ONCE(!blkg->refcnt);
185 blkg->refcnt++;
186}
187
188void __blkg_release(struct blkio_group *blkg);
189
190/**
191 * blkg_put - put a blkg reference
192 * @blkg: blkg to put
193 *
194 * The caller should be holding queue_lock.
195 */
196static inline void blkg_put(struct blkio_group *blkg)
197{
198 lockdep_assert_held(blkg->q->queue_lock);
199 WARN_ON_ONCE(blkg->refcnt <= 0);
200 if (!--blkg->refcnt)
201 __blkg_release(blkg);
202}
203
edcb0722
TH
204/**
205 * blkg_stat_add - add a value to a blkg_stat
206 * @stat: target blkg_stat
207 * @val: value to add
208 *
209 * Add @val to @stat. The caller is responsible for synchronizing calls to
210 * this function.
211 */
212static inline void blkg_stat_add(struct blkg_stat *stat, uint64_t val)
213{
214 u64_stats_update_begin(&stat->syncp);
215 stat->cnt += val;
216 u64_stats_update_end(&stat->syncp);
217}
218
219/**
220 * blkg_stat_read - read the current value of a blkg_stat
221 * @stat: blkg_stat to read
222 *
223 * Read the current value of @stat. This function can be called without
224 * synchroniztion and takes care of u64 atomicity.
225 */
226static inline uint64_t blkg_stat_read(struct blkg_stat *stat)
227{
228 unsigned int start;
229 uint64_t v;
230
231 do {
232 start = u64_stats_fetch_begin(&stat->syncp);
233 v = stat->cnt;
234 } while (u64_stats_fetch_retry(&stat->syncp, start));
235
236 return v;
237}
238
239/**
240 * blkg_stat_reset - reset a blkg_stat
241 * @stat: blkg_stat to reset
242 */
243static inline void blkg_stat_reset(struct blkg_stat *stat)
244{
245 stat->cnt = 0;
246}
247
248/**
249 * blkg_rwstat_add - add a value to a blkg_rwstat
250 * @rwstat: target blkg_rwstat
251 * @rw: mask of REQ_{WRITE|SYNC}
252 * @val: value to add
253 *
254 * Add @val to @rwstat. The counters are chosen according to @rw. The
255 * caller is responsible for synchronizing calls to this function.
256 */
257static inline void blkg_rwstat_add(struct blkg_rwstat *rwstat,
258 int rw, uint64_t val)
259{
260 u64_stats_update_begin(&rwstat->syncp);
261
262 if (rw & REQ_WRITE)
263 rwstat->cnt[BLKG_RWSTAT_WRITE] += val;
264 else
265 rwstat->cnt[BLKG_RWSTAT_READ] += val;
266 if (rw & REQ_SYNC)
267 rwstat->cnt[BLKG_RWSTAT_SYNC] += val;
268 else
269 rwstat->cnt[BLKG_RWSTAT_ASYNC] += val;
270
271 u64_stats_update_end(&rwstat->syncp);
272}
273
274/**
275 * blkg_rwstat_read - read the current values of a blkg_rwstat
276 * @rwstat: blkg_rwstat to read
277 *
278 * Read the current snapshot of @rwstat and return it as the return value.
279 * This function can be called without synchronization and takes care of
280 * u64 atomicity.
281 */
c94bed89 282static inline struct blkg_rwstat blkg_rwstat_read(struct blkg_rwstat *rwstat)
edcb0722
TH
283{
284 unsigned int start;
285 struct blkg_rwstat tmp;
286
287 do {
288 start = u64_stats_fetch_begin(&rwstat->syncp);
289 tmp = *rwstat;
290 } while (u64_stats_fetch_retry(&rwstat->syncp, start));
291
292 return tmp;
293}
294
295/**
296 * blkg_rwstat_sum - read the total count of a blkg_rwstat
297 * @rwstat: blkg_rwstat to read
298 *
299 * Return the total count of @rwstat regardless of the IO direction. This
300 * function can be called without synchronization and takes care of u64
301 * atomicity.
302 */
303static inline uint64_t blkg_rwstat_sum(struct blkg_rwstat *rwstat)
304{
305 struct blkg_rwstat tmp = blkg_rwstat_read(rwstat);
306
307 return tmp.cnt[BLKG_RWSTAT_READ] + tmp.cnt[BLKG_RWSTAT_WRITE];
308}
309
310/**
311 * blkg_rwstat_reset - reset a blkg_rwstat
312 * @rwstat: blkg_rwstat to reset
313 */
314static inline void blkg_rwstat_reset(struct blkg_rwstat *rwstat)
315{
316 memset(rwstat->cnt, 0, sizeof(rwstat->cnt));
317}
318
2f5ea477
JA
319#else
320
321struct blkio_group {
322};
323
3e252066
VG
324struct blkio_policy_type {
325};
326
5efd6113
TH
327static inline int blkcg_init_queue(struct request_queue *q) { return 0; }
328static inline void blkcg_drain_queue(struct request_queue *q) { }
329static inline void blkcg_exit_queue(struct request_queue *q) { }
8bd435b3 330static inline int blkio_policy_register(struct blkio_policy_type *blkiop) { return 0; }
3e252066 331static inline void blkio_policy_unregister(struct blkio_policy_type *blkiop) { }
a2b1693b
TH
332static inline int blkcg_activate_policy(struct request_queue *q,
333 const struct blkio_policy_type *pol) { return 0; }
334static inline void blkcg_deactivate_policy(struct request_queue *q,
335 const struct blkio_policy_type *pol) { }
3e252066 336
0381411e
TH
337static inline void *blkg_to_pdata(struct blkio_group *blkg,
338 struct blkio_policy_type *pol) { return NULL; }
339static inline struct blkio_group *pdata_to_blkg(void *pdata,
340 struct blkio_policy_type *pol) { return NULL; }
afc24d49 341static inline char *blkg_path(struct blkio_group *blkg) { return NULL; }
1adaf3dd
TH
342static inline void blkg_get(struct blkio_group *blkg) { }
343static inline void blkg_put(struct blkio_group *blkg) { }
afc24d49 344
2f5ea477
JA
345#endif
346
32e380ae 347#ifdef CONFIG_BLK_CGROUP
31e4c28d
VG
348extern struct blkio_cgroup blkio_root_cgroup;
349extern struct blkio_cgroup *cgroup_to_blkio_cgroup(struct cgroup *cgroup);
4f85cb96 350extern struct blkio_cgroup *bio_blkio_cgroup(struct bio *bio);
cd1604fa 351extern struct blkio_group *blkg_lookup(struct blkio_cgroup *blkcg,
e8989fae 352 struct request_queue *q);
cd1604fa 353struct blkio_group *blkg_lookup_create(struct blkio_cgroup *blkcg,
3c96cb32 354 struct request_queue *q);
31e4c28d 355#else
2f5ea477 356struct cgroup;
31e4c28d
VG
357static inline struct blkio_cgroup *
358cgroup_to_blkio_cgroup(struct cgroup *cgroup) { return NULL; }
70087dc3 359static inline struct blkio_cgroup *
4f85cb96 360bio_blkio_cgroup(struct bio *bio) { return NULL; }
31e4c28d 361
cd1604fa
TH
362static inline struct blkio_group *blkg_lookup(struct blkio_cgroup *blkcg,
363 void *key) { return NULL; }
31e4c28d
VG
364#endif
365#endif /* _BLK_CGROUP_H */