]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blob - block/blk-cgroup.h
ba64b28575711d9b9a1b620244e1392dc97fcf46
[mirror_ubuntu-zesty-kernel.git] / block / blk-cgroup.h
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>
17 #include <linux/u64_stats_sync.h>
18 #include <linux/seq_file.h>
19
20 enum blkio_policy_id {
21 BLKIO_POLICY_PROP = 0, /* Proportional Bandwidth division */
22 BLKIO_POLICY_THROTL, /* Throttling */
23
24 BLKIO_NR_POLICIES,
25 };
26
27 /* Max limits for throttle policy */
28 #define THROTL_IOPS_MAX UINT_MAX
29
30 #ifdef CONFIG_BLK_CGROUP
31
32 /* cft->private [un]packing for stat printing */
33 #define BLKCG_STAT_PRIV(pol, off) (((unsigned)(pol) << 16) | (off))
34 #define BLKCG_STAT_POL(prv) ((unsigned)(prv) >> 16)
35 #define BLKCG_STAT_OFF(prv) ((unsigned)(prv) & 0xffff)
36
37 enum blkg_rwstat_type {
38 BLKG_RWSTAT_READ,
39 BLKG_RWSTAT_WRITE,
40 BLKG_RWSTAT_SYNC,
41 BLKG_RWSTAT_ASYNC,
42
43 BLKG_RWSTAT_NR,
44 BLKG_RWSTAT_TOTAL = BLKG_RWSTAT_NR,
45 };
46
47 /* blkg state flags */
48 enum blkg_state_flags {
49 BLKG_waiting = 0,
50 BLKG_idling,
51 BLKG_empty,
52 };
53
54 struct blkio_cgroup {
55 struct cgroup_subsys_state css;
56 unsigned int weight;
57 spinlock_t lock;
58 struct hlist_head blkg_list;
59
60 /* for policies to test whether associated blkcg has changed */
61 uint64_t id;
62 };
63
64 struct blkg_stat {
65 struct u64_stats_sync syncp;
66 uint64_t cnt;
67 };
68
69 struct blkg_rwstat {
70 struct u64_stats_sync syncp;
71 uint64_t cnt[BLKG_RWSTAT_NR];
72 };
73
74 struct blkio_group_stats {
75 /* number of ios merged */
76 struct blkg_rwstat merged;
77 /* total time spent on device in ns, may not be accurate w/ queueing */
78 struct blkg_rwstat service_time;
79 /* total time spent waiting in scheduler queue in ns */
80 struct blkg_rwstat wait_time;
81 /* number of IOs queued up */
82 struct blkg_rwstat queued;
83 /* total disk time and nr sectors dispatched by this group */
84 struct blkg_stat time;
85 #ifdef CONFIG_DEBUG_BLK_CGROUP
86 /* time not charged to this cgroup */
87 struct blkg_stat unaccounted_time;
88 /* sum of number of ios queued across all samples */
89 struct blkg_stat avg_queue_size_sum;
90 /* count of samples taken for average */
91 struct blkg_stat avg_queue_size_samples;
92 /* how many times this group has been removed from service tree */
93 struct blkg_stat dequeue;
94 /* total time spent waiting for it to be assigned a timeslice. */
95 struct blkg_stat group_wait_time;
96 /* time spent idling for this blkio_group */
97 struct blkg_stat idle_time;
98 /* total time with empty current active q with other requests queued */
99 struct blkg_stat empty_time;
100 /* fields after this shouldn't be cleared on stat reset */
101 uint64_t start_group_wait_time;
102 uint64_t start_idle_time;
103 uint64_t start_empty_time;
104 uint16_t flags;
105 #endif
106 };
107
108 /* Per cpu blkio group stats */
109 struct blkio_group_stats_cpu {
110 /* total bytes transferred */
111 struct blkg_rwstat service_bytes;
112 /* total IOs serviced, post merge */
113 struct blkg_rwstat serviced;
114 /* total sectors transferred */
115 struct blkg_stat sectors;
116 };
117
118 struct blkio_group_conf {
119 unsigned int weight;
120 u64 iops[2];
121 u64 bps[2];
122 };
123
124 /* per-blkg per-policy data */
125 struct blkg_policy_data {
126 /* the blkg this per-policy data belongs to */
127 struct blkio_group *blkg;
128
129 /* Configuration */
130 struct blkio_group_conf conf;
131
132 struct blkio_group_stats stats;
133 /* Per cpu stats pointer */
134 struct blkio_group_stats_cpu __percpu *stats_cpu;
135
136 /* pol->pdata_size bytes of private data used by policy impl */
137 char pdata[] __aligned(__alignof__(unsigned long long));
138 };
139
140 struct blkio_group {
141 /* Pointer to the associated request_queue */
142 struct request_queue *q;
143 struct list_head q_node;
144 struct hlist_node blkcg_node;
145 struct blkio_cgroup *blkcg;
146 /* Store cgroup path */
147 char path[128];
148 /* reference count */
149 int refcnt;
150
151 struct blkg_policy_data *pd[BLKIO_NR_POLICIES];
152
153 /* List of blkg waiting for per cpu stats memory to be allocated */
154 struct list_head alloc_node;
155 struct rcu_head rcu_head;
156 };
157
158 typedef void (blkio_init_group_fn)(struct blkio_group *blkg);
159
160 struct blkio_policy_ops {
161 blkio_init_group_fn *blkio_init_group_fn;
162 };
163
164 struct blkio_policy_type {
165 struct list_head list;
166 struct blkio_policy_ops ops;
167 enum blkio_policy_id plid;
168 size_t pdata_size; /* policy specific private data size */
169 struct cftype *cftypes; /* cgroup files for the policy */
170 };
171
172 extern int blkcg_init_queue(struct request_queue *q);
173 extern void blkcg_drain_queue(struct request_queue *q);
174 extern void blkcg_exit_queue(struct request_queue *q);
175
176 /* Blkio controller policy registration */
177 extern void blkio_policy_register(struct blkio_policy_type *);
178 extern void blkio_policy_unregister(struct blkio_policy_type *);
179 extern void blkg_destroy_all(struct request_queue *q, bool destroy_root);
180 extern void update_root_blkg_pd(struct request_queue *q,
181 enum blkio_policy_id plid);
182
183 void blkcg_print_blkgs(struct seq_file *sf, struct blkio_cgroup *blkcg,
184 u64 (*prfill)(struct seq_file *, struct blkg_policy_data *, int),
185 int pol, int data, bool show_total);
186 u64 __blkg_prfill_u64(struct seq_file *sf, struct blkg_policy_data *pd, u64 v);
187 u64 __blkg_prfill_rwstat(struct seq_file *sf, struct blkg_policy_data *pd,
188 const struct blkg_rwstat *rwstat);
189 int blkcg_print_stat(struct cgroup *cgrp, struct cftype *cft,
190 struct seq_file *sf);
191 int blkcg_print_rwstat(struct cgroup *cgrp, struct cftype *cft,
192 struct seq_file *sf);
193 int blkcg_print_cpu_stat(struct cgroup *cgrp, struct cftype *cft,
194 struct seq_file *sf);
195 int blkcg_print_cpu_rwstat(struct cgroup *cgrp, struct cftype *cft,
196 struct seq_file *sf);
197
198 struct blkg_conf_ctx {
199 struct gendisk *disk;
200 struct blkio_group *blkg;
201 u64 v;
202 };
203
204 int blkg_conf_prep(struct blkio_cgroup *blkcg, const char *input,
205 struct blkg_conf_ctx *ctx);
206 void blkg_conf_finish(struct blkg_conf_ctx *ctx);
207
208
209 /**
210 * blkg_to_pdata - get policy private data
211 * @blkg: blkg of interest
212 * @pol: policy of interest
213 *
214 * Return pointer to private data associated with the @blkg-@pol pair.
215 */
216 static inline void *blkg_to_pdata(struct blkio_group *blkg,
217 struct blkio_policy_type *pol)
218 {
219 return blkg ? blkg->pd[pol->plid]->pdata : NULL;
220 }
221
222 /**
223 * pdata_to_blkg - get blkg associated with policy private data
224 * @pdata: policy private data of interest
225 *
226 * @pdata is policy private data. Determine the blkg it's associated with.
227 */
228 static inline struct blkio_group *pdata_to_blkg(void *pdata)
229 {
230 if (pdata) {
231 struct blkg_policy_data *pd =
232 container_of(pdata, struct blkg_policy_data, pdata);
233 return pd->blkg;
234 }
235 return NULL;
236 }
237
238 static inline char *blkg_path(struct blkio_group *blkg)
239 {
240 return blkg->path;
241 }
242
243 /**
244 * blkg_get - get a blkg reference
245 * @blkg: blkg to get
246 *
247 * The caller should be holding queue_lock and an existing reference.
248 */
249 static inline void blkg_get(struct blkio_group *blkg)
250 {
251 lockdep_assert_held(blkg->q->queue_lock);
252 WARN_ON_ONCE(!blkg->refcnt);
253 blkg->refcnt++;
254 }
255
256 void __blkg_release(struct blkio_group *blkg);
257
258 /**
259 * blkg_put - put a blkg reference
260 * @blkg: blkg to put
261 *
262 * The caller should be holding queue_lock.
263 */
264 static inline void blkg_put(struct blkio_group *blkg)
265 {
266 lockdep_assert_held(blkg->q->queue_lock);
267 WARN_ON_ONCE(blkg->refcnt <= 0);
268 if (!--blkg->refcnt)
269 __blkg_release(blkg);
270 }
271
272 /**
273 * blkg_stat_add - add a value to a blkg_stat
274 * @stat: target blkg_stat
275 * @val: value to add
276 *
277 * Add @val to @stat. The caller is responsible for synchronizing calls to
278 * this function.
279 */
280 static inline void blkg_stat_add(struct blkg_stat *stat, uint64_t val)
281 {
282 u64_stats_update_begin(&stat->syncp);
283 stat->cnt += val;
284 u64_stats_update_end(&stat->syncp);
285 }
286
287 /**
288 * blkg_stat_read - read the current value of a blkg_stat
289 * @stat: blkg_stat to read
290 *
291 * Read the current value of @stat. This function can be called without
292 * synchroniztion and takes care of u64 atomicity.
293 */
294 static inline uint64_t blkg_stat_read(struct blkg_stat *stat)
295 {
296 unsigned int start;
297 uint64_t v;
298
299 do {
300 start = u64_stats_fetch_begin(&stat->syncp);
301 v = stat->cnt;
302 } while (u64_stats_fetch_retry(&stat->syncp, start));
303
304 return v;
305 }
306
307 /**
308 * blkg_stat_reset - reset a blkg_stat
309 * @stat: blkg_stat to reset
310 */
311 static inline void blkg_stat_reset(struct blkg_stat *stat)
312 {
313 stat->cnt = 0;
314 }
315
316 /**
317 * blkg_rwstat_add - add a value to a blkg_rwstat
318 * @rwstat: target blkg_rwstat
319 * @rw: mask of REQ_{WRITE|SYNC}
320 * @val: value to add
321 *
322 * Add @val to @rwstat. The counters are chosen according to @rw. The
323 * caller is responsible for synchronizing calls to this function.
324 */
325 static inline void blkg_rwstat_add(struct blkg_rwstat *rwstat,
326 int rw, uint64_t val)
327 {
328 u64_stats_update_begin(&rwstat->syncp);
329
330 if (rw & REQ_WRITE)
331 rwstat->cnt[BLKG_RWSTAT_WRITE] += val;
332 else
333 rwstat->cnt[BLKG_RWSTAT_READ] += val;
334 if (rw & REQ_SYNC)
335 rwstat->cnt[BLKG_RWSTAT_SYNC] += val;
336 else
337 rwstat->cnt[BLKG_RWSTAT_ASYNC] += val;
338
339 u64_stats_update_end(&rwstat->syncp);
340 }
341
342 /**
343 * blkg_rwstat_read - read the current values of a blkg_rwstat
344 * @rwstat: blkg_rwstat to read
345 *
346 * Read the current snapshot of @rwstat and return it as the return value.
347 * This function can be called without synchronization and takes care of
348 * u64 atomicity.
349 */
350 static struct blkg_rwstat blkg_rwstat_read(struct blkg_rwstat *rwstat)
351 {
352 unsigned int start;
353 struct blkg_rwstat tmp;
354
355 do {
356 start = u64_stats_fetch_begin(&rwstat->syncp);
357 tmp = *rwstat;
358 } while (u64_stats_fetch_retry(&rwstat->syncp, start));
359
360 return tmp;
361 }
362
363 /**
364 * blkg_rwstat_sum - read the total count of a blkg_rwstat
365 * @rwstat: blkg_rwstat to read
366 *
367 * Return the total count of @rwstat regardless of the IO direction. This
368 * function can be called without synchronization and takes care of u64
369 * atomicity.
370 */
371 static inline uint64_t blkg_rwstat_sum(struct blkg_rwstat *rwstat)
372 {
373 struct blkg_rwstat tmp = blkg_rwstat_read(rwstat);
374
375 return tmp.cnt[BLKG_RWSTAT_READ] + tmp.cnt[BLKG_RWSTAT_WRITE];
376 }
377
378 /**
379 * blkg_rwstat_reset - reset a blkg_rwstat
380 * @rwstat: blkg_rwstat to reset
381 */
382 static inline void blkg_rwstat_reset(struct blkg_rwstat *rwstat)
383 {
384 memset(rwstat->cnt, 0, sizeof(rwstat->cnt));
385 }
386
387 #else
388
389 struct blkio_group {
390 };
391
392 struct blkio_policy_type {
393 };
394
395 static inline int blkcg_init_queue(struct request_queue *q) { return 0; }
396 static inline void blkcg_drain_queue(struct request_queue *q) { }
397 static inline void blkcg_exit_queue(struct request_queue *q) { }
398 static inline void blkio_policy_register(struct blkio_policy_type *blkiop) { }
399 static inline void blkio_policy_unregister(struct blkio_policy_type *blkiop) { }
400 static inline void blkg_destroy_all(struct request_queue *q,
401 bool destory_root) { }
402 static inline void update_root_blkg_pd(struct request_queue *q,
403 enum blkio_policy_id plid) { }
404
405 static inline void *blkg_to_pdata(struct blkio_group *blkg,
406 struct blkio_policy_type *pol) { return NULL; }
407 static inline struct blkio_group *pdata_to_blkg(void *pdata,
408 struct blkio_policy_type *pol) { return NULL; }
409 static inline char *blkg_path(struct blkio_group *blkg) { return NULL; }
410 static inline void blkg_get(struct blkio_group *blkg) { }
411 static inline void blkg_put(struct blkio_group *blkg) { }
412
413 #endif
414
415 #define BLKIO_WEIGHT_MIN 10
416 #define BLKIO_WEIGHT_MAX 1000
417 #define BLKIO_WEIGHT_DEFAULT 500
418
419 #ifdef CONFIG_DEBUG_BLK_CGROUP
420 void blkiocg_update_avg_queue_size_stats(struct blkio_group *blkg,
421 struct blkio_policy_type *pol);
422 void blkiocg_update_dequeue_stats(struct blkio_group *blkg,
423 struct blkio_policy_type *pol,
424 unsigned long dequeue);
425 void blkiocg_update_set_idle_time_stats(struct blkio_group *blkg,
426 struct blkio_policy_type *pol);
427 void blkiocg_update_idle_time_stats(struct blkio_group *blkg,
428 struct blkio_policy_type *pol);
429 void blkiocg_set_start_empty_time(struct blkio_group *blkg,
430 struct blkio_policy_type *pol);
431
432 #define BLKG_FLAG_FNS(name) \
433 static inline void blkio_mark_blkg_##name( \
434 struct blkio_group_stats *stats) \
435 { \
436 stats->flags |= (1 << BLKG_##name); \
437 } \
438 static inline void blkio_clear_blkg_##name( \
439 struct blkio_group_stats *stats) \
440 { \
441 stats->flags &= ~(1 << BLKG_##name); \
442 } \
443 static inline int blkio_blkg_##name(struct blkio_group_stats *stats) \
444 { \
445 return (stats->flags & (1 << BLKG_##name)) != 0; \
446 } \
447
448 BLKG_FLAG_FNS(waiting)
449 BLKG_FLAG_FNS(idling)
450 BLKG_FLAG_FNS(empty)
451 #undef BLKG_FLAG_FNS
452 #else
453 static inline void blkiocg_update_avg_queue_size_stats(struct blkio_group *blkg,
454 struct blkio_policy_type *pol) { }
455 static inline void blkiocg_update_dequeue_stats(struct blkio_group *blkg,
456 struct blkio_policy_type *pol, unsigned long dequeue) { }
457 static inline void blkiocg_update_set_idle_time_stats(struct blkio_group *blkg,
458 struct blkio_policy_type *pol) { }
459 static inline void blkiocg_update_idle_time_stats(struct blkio_group *blkg,
460 struct blkio_policy_type *pol) { }
461 static inline void blkiocg_set_start_empty_time(struct blkio_group *blkg,
462 struct blkio_policy_type *pol) { }
463 #endif
464
465 #ifdef CONFIG_BLK_CGROUP
466 extern struct blkio_cgroup blkio_root_cgroup;
467 extern struct blkio_cgroup *cgroup_to_blkio_cgroup(struct cgroup *cgroup);
468 extern struct blkio_cgroup *bio_blkio_cgroup(struct bio *bio);
469 extern struct blkio_group *blkg_lookup(struct blkio_cgroup *blkcg,
470 struct request_queue *q);
471 struct blkio_group *blkg_lookup_create(struct blkio_cgroup *blkcg,
472 struct request_queue *q,
473 bool for_root);
474 void blkiocg_update_timeslice_used(struct blkio_group *blkg,
475 struct blkio_policy_type *pol,
476 unsigned long time,
477 unsigned long unaccounted_time);
478 void blkiocg_update_dispatch_stats(struct blkio_group *blkg,
479 struct blkio_policy_type *pol,
480 uint64_t bytes, bool direction, bool sync);
481 void blkiocg_update_completion_stats(struct blkio_group *blkg,
482 struct blkio_policy_type *pol,
483 uint64_t start_time,
484 uint64_t io_start_time, bool direction,
485 bool sync);
486 void blkiocg_update_io_merged_stats(struct blkio_group *blkg,
487 struct blkio_policy_type *pol,
488 bool direction, bool sync);
489 void blkiocg_update_io_add_stats(struct blkio_group *blkg,
490 struct blkio_policy_type *pol,
491 struct blkio_group *curr_blkg, bool direction,
492 bool sync);
493 void blkiocg_update_io_remove_stats(struct blkio_group *blkg,
494 struct blkio_policy_type *pol,
495 bool direction, bool sync);
496 #else
497 struct cgroup;
498 static inline struct blkio_cgroup *
499 cgroup_to_blkio_cgroup(struct cgroup *cgroup) { return NULL; }
500 static inline struct blkio_cgroup *
501 bio_blkio_cgroup(struct bio *bio) { return NULL; }
502
503 static inline struct blkio_group *blkg_lookup(struct blkio_cgroup *blkcg,
504 void *key) { return NULL; }
505 static inline void blkiocg_update_timeslice_used(struct blkio_group *blkg,
506 struct blkio_policy_type *pol, unsigned long time,
507 unsigned long unaccounted_time) { }
508 static inline void blkiocg_update_dispatch_stats(struct blkio_group *blkg,
509 struct blkio_policy_type *pol, uint64_t bytes,
510 bool direction, bool sync) { }
511 static inline void blkiocg_update_completion_stats(struct blkio_group *blkg,
512 struct blkio_policy_type *pol, uint64_t start_time,
513 uint64_t io_start_time, bool direction, bool sync) { }
514 static inline void blkiocg_update_io_merged_stats(struct blkio_group *blkg,
515 struct blkio_policy_type *pol, bool direction,
516 bool sync) { }
517 static inline void blkiocg_update_io_add_stats(struct blkio_group *blkg,
518 struct blkio_policy_type *pol,
519 struct blkio_group *curr_blkg, bool direction,
520 bool sync) { }
521 static inline void blkiocg_update_io_remove_stats(struct blkio_group *blkg,
522 struct blkio_policy_type *pol, bool direction,
523 bool sync) { }
524 #endif
525 #endif /* _BLK_CGROUP_H */