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> |
a637120e | 19 | #include <linux/radix-tree.h> |
31e4c28d | 20 | |
9355aede VG |
21 | /* Max limits for throttle policy */ |
22 | #define THROTL_IOPS_MAX UINT_MAX | |
23 | ||
3381cb8d TH |
24 | /* 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 | ||
f48ec1d7 TH |
29 | #ifdef CONFIG_BLK_CGROUP |
30 | ||
edcb0722 TH |
31 | enum 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, | |
303a3acb DS |
39 | }; |
40 | ||
a637120e TH |
41 | struct blkcg_gq; |
42 | ||
3c798398 | 43 | struct blkcg { |
36558c8a TH |
44 | struct cgroup_subsys_state css; |
45 | spinlock_t lock; | |
a637120e TH |
46 | |
47 | struct radix_tree_root blkg_tree; | |
48 | struct blkcg_gq *blkg_hint; | |
36558c8a | 49 | struct hlist_head blkg_list; |
9a9e8a26 TH |
50 | |
51 | /* for policies to test whether associated blkcg has changed */ | |
36558c8a | 52 | uint64_t id; |
3381cb8d | 53 | |
3c798398 | 54 | /* TODO: per-policy storage in blkcg */ |
36558c8a | 55 | unsigned int cfq_weight; /* belongs to cfq */ |
31e4c28d VG |
56 | }; |
57 | ||
edcb0722 TH |
58 | struct blkg_stat { |
59 | struct u64_stats_sync syncp; | |
60 | uint64_t cnt; | |
61 | }; | |
62 | ||
63 | struct blkg_rwstat { | |
64 | struct u64_stats_sync syncp; | |
65 | uint64_t cnt[BLKG_RWSTAT_NR]; | |
66 | }; | |
67 | ||
f95a04af TH |
68 | /* |
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 | */ | |
0381411e TH |
81 | struct blkg_policy_data { |
82 | /* the blkg this per-policy data belongs to */ | |
3c798398 | 83 | struct blkcg_gq *blkg; |
0381411e | 84 | |
a2b1693b | 85 | /* used during policy activation */ |
36558c8a | 86 | struct list_head alloc_node; |
0381411e TH |
87 | }; |
88 | ||
3c798398 TH |
89 | /* association between a blk cgroup and a request queue */ |
90 | struct blkcg_gq { | |
c875f4d0 | 91 | /* Pointer to the associated request_queue */ |
36558c8a TH |
92 | struct request_queue *q; |
93 | struct list_head q_node; | |
94 | struct hlist_node blkcg_node; | |
3c798398 | 95 | struct blkcg *blkcg; |
1adaf3dd | 96 | /* reference count */ |
36558c8a | 97 | int refcnt; |
22084190 | 98 | |
36558c8a | 99 | struct blkg_policy_data *pd[BLKCG_MAX_POLS]; |
1adaf3dd | 100 | |
36558c8a | 101 | struct rcu_head rcu_head; |
31e4c28d VG |
102 | }; |
103 | ||
3c798398 TH |
104 | typedef void (blkcg_pol_init_pd_fn)(struct blkcg_gq *blkg); |
105 | typedef void (blkcg_pol_exit_pd_fn)(struct blkcg_gq *blkg); | |
106 | typedef void (blkcg_pol_reset_pd_stats_fn)(struct blkcg_gq *blkg); | |
3e252066 | 107 | |
3c798398 | 108 | struct blkcg_policy { |
36558c8a TH |
109 | int plid; |
110 | /* policy specific private data size */ | |
f95a04af | 111 | size_t pd_size; |
36558c8a TH |
112 | /* cgroup files for the policy */ |
113 | struct cftype *cftypes; | |
f9fcc2d3 TH |
114 | |
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; | |
3e252066 VG |
119 | }; |
120 | ||
3c798398 | 121 | extern struct blkcg blkcg_root; |
36558c8a | 122 | |
3c798398 TH |
123 | struct blkcg *cgroup_to_blkcg(struct cgroup *cgroup); |
124 | struct blkcg *bio_blkcg(struct bio *bio); | |
125 | struct blkcg_gq *blkg_lookup(struct blkcg *blkcg, struct request_queue *q); | |
126 | struct blkcg_gq *blkg_lookup_create(struct blkcg *blkcg, | |
127 | struct request_queue *q); | |
36558c8a TH |
128 | int blkcg_init_queue(struct request_queue *q); |
129 | void blkcg_drain_queue(struct request_queue *q); | |
130 | void blkcg_exit_queue(struct request_queue *q); | |
5efd6113 | 131 | |
3e252066 | 132 | /* Blkio controller policy registration */ |
3c798398 TH |
133 | int blkcg_policy_register(struct blkcg_policy *pol); |
134 | void blkcg_policy_unregister(struct blkcg_policy *pol); | |
36558c8a | 135 | int blkcg_activate_policy(struct request_queue *q, |
3c798398 | 136 | const struct blkcg_policy *pol); |
36558c8a | 137 | void blkcg_deactivate_policy(struct request_queue *q, |
3c798398 | 138 | const struct blkcg_policy *pol); |
3e252066 | 139 | |
3c798398 | 140 | void blkcg_print_blkgs(struct seq_file *sf, struct blkcg *blkcg, |
f95a04af TH |
141 | u64 (*prfill)(struct seq_file *, |
142 | struct blkg_policy_data *, int), | |
3c798398 | 143 | const struct blkcg_policy *pol, int data, |
ec399347 | 144 | bool show_total); |
f95a04af TH |
145 | u64 __blkg_prfill_u64(struct seq_file *sf, struct blkg_policy_data *pd, u64 v); |
146 | u64 __blkg_prfill_rwstat(struct seq_file *sf, struct blkg_policy_data *pd, | |
829fdb50 | 147 | const struct blkg_rwstat *rwstat); |
f95a04af TH |
148 | u64 blkg_prfill_stat(struct seq_file *sf, struct blkg_policy_data *pd, int off); |
149 | u64 blkg_prfill_rwstat(struct seq_file *sf, struct blkg_policy_data *pd, | |
150 | int off); | |
829fdb50 TH |
151 | |
152 | struct blkg_conf_ctx { | |
36558c8a | 153 | struct gendisk *disk; |
3c798398 | 154 | struct blkcg_gq *blkg; |
36558c8a | 155 | u64 v; |
829fdb50 TH |
156 | }; |
157 | ||
3c798398 TH |
158 | int blkg_conf_prep(struct blkcg *blkcg, const struct blkcg_policy *pol, |
159 | const char *input, struct blkg_conf_ctx *ctx); | |
829fdb50 TH |
160 | void blkg_conf_finish(struct blkg_conf_ctx *ctx); |
161 | ||
162 | ||
0381411e TH |
163 | /** |
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 | */ | |
f95a04af TH |
170 | static inline struct blkg_policy_data *blkg_to_pd(struct blkcg_gq *blkg, |
171 | struct blkcg_policy *pol) | |
0381411e | 172 | { |
f95a04af | 173 | return blkg ? blkg->pd[pol->plid] : NULL; |
0381411e TH |
174 | } |
175 | ||
176 | /** | |
177 | * pdata_to_blkg - get blkg associated with policy private data | |
f95a04af | 178 | * @pd: policy private data of interest |
0381411e | 179 | * |
f95a04af | 180 | * @pd is policy private data. Determine the blkg it's associated with. |
0381411e | 181 | */ |
f95a04af | 182 | static inline struct blkcg_gq *pd_to_blkg(struct blkg_policy_data *pd) |
0381411e | 183 | { |
f95a04af | 184 | return pd ? pd->blkg : NULL; |
0381411e TH |
185 | } |
186 | ||
54e7ed12 TH |
187 | /** |
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 | */ | |
3c798398 | 195 | static inline int blkg_path(struct blkcg_gq *blkg, char *buf, int buflen) |
afc24d49 | 196 | { |
54e7ed12 TH |
197 | 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; | |
afc24d49 VG |
205 | } |
206 | ||
1adaf3dd TH |
207 | /** |
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 | */ | |
3c798398 | 213 | static inline void blkg_get(struct blkcg_gq *blkg) |
1adaf3dd TH |
214 | { |
215 | lockdep_assert_held(blkg->q->queue_lock); | |
216 | WARN_ON_ONCE(!blkg->refcnt); | |
217 | blkg->refcnt++; | |
218 | } | |
219 | ||
3c798398 | 220 | void __blkg_release(struct blkcg_gq *blkg); |
1adaf3dd TH |
221 | |
222 | /** | |
223 | * blkg_put - put a blkg reference | |
224 | * @blkg: blkg to put | |
225 | * | |
226 | * The caller should be holding queue_lock. | |
227 | */ | |
3c798398 | 228 | static inline void blkg_put(struct blkcg_gq *blkg) |
1adaf3dd TH |
229 | { |
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 | ||
edcb0722 TH |
236 | /** |
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 | */ | |
244 | static 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 | */ | |
258 | static 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 | */ | |
275 | static 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 | */ | |
289 | static 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 | */ | |
c94bed89 | 314 | static inline struct blkg_rwstat blkg_rwstat_read(struct blkg_rwstat *rwstat) |
edcb0722 TH |
315 | { |
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 | */ | |
335 | static 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 | */ | |
346 | static inline void blkg_rwstat_reset(struct blkg_rwstat *rwstat) | |
347 | { | |
348 | memset(rwstat->cnt, 0, sizeof(rwstat->cnt)); | |
349 | } | |
350 | ||
36558c8a TH |
351 | #else /* CONFIG_BLK_CGROUP */ |
352 | ||
353 | struct cgroup; | |
2f5ea477 | 354 | |
f95a04af TH |
355 | struct blkg_policy_data { |
356 | }; | |
357 | ||
3c798398 | 358 | struct blkcg_gq { |
2f5ea477 JA |
359 | }; |
360 | ||
3c798398 | 361 | struct blkcg_policy { |
3e252066 VG |
362 | }; |
363 | ||
3c798398 TH |
364 | static inline struct blkcg *cgroup_to_blkcg(struct cgroup *cgroup) { return NULL; } |
365 | static inline struct blkcg *bio_blkcg(struct bio *bio) { return NULL; } | |
366 | static inline struct blkcg_gq *blkg_lookup(struct blkcg *blkcg, void *key) { return NULL; } | |
5efd6113 TH |
367 | static inline int blkcg_init_queue(struct request_queue *q) { return 0; } |
368 | static inline void blkcg_drain_queue(struct request_queue *q) { } | |
369 | static inline void blkcg_exit_queue(struct request_queue *q) { } | |
3c798398 TH |
370 | static inline int blkcg_policy_register(struct blkcg_policy *pol) { return 0; } |
371 | static inline void blkcg_policy_unregister(struct blkcg_policy *pol) { } | |
a2b1693b | 372 | static inline int blkcg_activate_policy(struct request_queue *q, |
3c798398 | 373 | const struct blkcg_policy *pol) { return 0; } |
a2b1693b | 374 | static inline void blkcg_deactivate_policy(struct request_queue *q, |
3c798398 TH |
375 | const struct blkcg_policy *pol) { } |
376 | ||
f95a04af TH |
377 | static inline struct blkg_policy_data *blkg_to_pd(struct blkcg_gq *blkg, |
378 | struct blkcg_policy *pol) { return NULL; } | |
379 | static inline struct blkcg_gq *pd_to_blkg(struct blkg_policy_data *pd) { return NULL; } | |
3c798398 TH |
380 | static inline char *blkg_path(struct blkcg_gq *blkg) { return NULL; } |
381 | static inline void blkg_get(struct blkcg_gq *blkg) { } | |
382 | static inline void blkg_put(struct blkcg_gq *blkg) { } | |
afc24d49 | 383 | |
36558c8a TH |
384 | #endif /* CONFIG_BLK_CGROUP */ |
385 | #endif /* _BLK_CGROUP_H */ |