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