4 * Common Block IO controller cgroup interface
6 * Based on ideas and code from CFQ, CFS and BFQ:
7 * Copyright (C) 2003 Jens Axboe <axboe@kernel.dk>
9 * Copyright (C) 2008 Fabio Checconi <fabio@gandalf.sssup.it>
10 * Paolo Valente <paolo.valente@unimore.it>
12 * Copyright (C) 2009 Vivek Goyal <vgoyal@redhat.com>
13 * Nauman Rafique <nauman@google.com>
16 #include <linux/cgroup.h>
17 #include <linux/u64_stats_sync.h>
19 enum blkio_policy_id {
20 BLKIO_POLICY_PROP = 0, /* Proportional Bandwidth division */
21 BLKIO_POLICY_THROTL, /* Throttling */
26 /* Max limits for throttle policy */
27 #define THROTL_IOPS_MAX UINT_MAX
29 #ifdef CONFIG_BLK_CGROUP
31 /* cft->private [un]packing for stat printing */
32 #define BLKCG_STAT_PRIV(pol, off) (((unsigned)(pol) << 16) | (off))
33 #define BLKCG_STAT_POL(prv) ((unsigned)(prv) >> 16)
34 #define BLKCG_STAT_OFF(prv) ((unsigned)(prv) & 0xffff)
36 enum blkg_rwstat_type {
43 BLKG_RWSTAT_TOTAL = BLKG_RWSTAT_NR,
46 /* blkg state flags */
47 enum blkg_state_flags {
54 struct cgroup_subsys_state css;
57 struct hlist_head blkg_list;
59 /* for policies to test whether associated blkcg has changed */
64 struct u64_stats_sync syncp;
69 struct u64_stats_sync syncp;
70 uint64_t cnt[BLKG_RWSTAT_NR];
73 struct blkio_group_stats {
74 /* number of ios merged */
75 struct blkg_rwstat merged;
76 /* total time spent on device in ns, may not be accurate w/ queueing */
77 struct blkg_rwstat service_time;
78 /* total time spent waiting in scheduler queue in ns */
79 struct blkg_rwstat wait_time;
80 /* number of IOs queued up */
81 struct blkg_rwstat queued;
82 /* total disk time and nr sectors dispatched by this group */
83 struct blkg_stat time;
84 #ifdef CONFIG_DEBUG_BLK_CGROUP
85 /* time not charged to this cgroup */
86 struct blkg_stat unaccounted_time;
87 /* sum of number of ios queued across all samples */
88 struct blkg_stat avg_queue_size_sum;
89 /* count of samples taken for average */
90 struct blkg_stat avg_queue_size_samples;
91 /* how many times this group has been removed from service tree */
92 struct blkg_stat dequeue;
93 /* total time spent waiting for it to be assigned a timeslice. */
94 struct blkg_stat group_wait_time;
95 /* time spent idling for this blkio_group */
96 struct blkg_stat idle_time;
97 /* total time with empty current active q with other requests queued */
98 struct blkg_stat empty_time;
99 /* fields after this shouldn't be cleared on stat reset */
100 uint64_t start_group_wait_time;
101 uint64_t start_idle_time;
102 uint64_t start_empty_time;
107 /* Per cpu blkio group stats */
108 struct blkio_group_stats_cpu {
109 /* total bytes transferred */
110 struct blkg_rwstat service_bytes;
111 /* total IOs serviced, post merge */
112 struct blkg_rwstat serviced;
113 /* total sectors transferred */
114 struct blkg_stat sectors;
117 struct blkio_group_conf {
123 /* per-blkg per-policy data */
124 struct blkg_policy_data {
125 /* the blkg this per-policy data belongs to */
126 struct blkio_group *blkg;
129 struct blkio_group_conf conf;
131 struct blkio_group_stats stats;
132 /* Per cpu stats pointer */
133 struct blkio_group_stats_cpu __percpu *stats_cpu;
135 /* pol->pdata_size bytes of private data used by policy impl */
136 char pdata[] __aligned(__alignof__(unsigned long long));
140 /* Pointer to the associated request_queue */
141 struct request_queue *q;
142 struct list_head q_node;
143 struct hlist_node blkcg_node;
144 struct blkio_cgroup *blkcg;
145 /* Store cgroup path */
147 /* reference count */
150 struct blkg_policy_data *pd[BLKIO_NR_POLICIES];
152 /* List of blkg waiting for per cpu stats memory to be allocated */
153 struct list_head alloc_node;
154 struct rcu_head rcu_head;
157 typedef void (blkio_init_group_fn)(struct blkio_group *blkg);
158 typedef void (blkio_update_group_weight_fn)(struct request_queue *q,
159 struct blkio_group *blkg, unsigned int weight);
160 typedef void (blkio_update_group_read_bps_fn)(struct request_queue *q,
161 struct blkio_group *blkg, u64 read_bps);
162 typedef void (blkio_update_group_write_bps_fn)(struct request_queue *q,
163 struct blkio_group *blkg, u64 write_bps);
164 typedef void (blkio_update_group_read_iops_fn)(struct request_queue *q,
165 struct blkio_group *blkg, unsigned int read_iops);
166 typedef void (blkio_update_group_write_iops_fn)(struct request_queue *q,
167 struct blkio_group *blkg, unsigned int write_iops);
169 struct blkio_policy_ops {
170 blkio_init_group_fn *blkio_init_group_fn;
171 blkio_update_group_weight_fn *blkio_update_group_weight_fn;
172 blkio_update_group_read_bps_fn *blkio_update_group_read_bps_fn;
173 blkio_update_group_write_bps_fn *blkio_update_group_write_bps_fn;
174 blkio_update_group_read_iops_fn *blkio_update_group_read_iops_fn;
175 blkio_update_group_write_iops_fn *blkio_update_group_write_iops_fn;
178 struct blkio_policy_type {
179 struct list_head list;
180 struct blkio_policy_ops ops;
181 enum blkio_policy_id plid;
182 size_t pdata_size; /* policy specific private data size */
185 extern int blkcg_init_queue(struct request_queue *q);
186 extern void blkcg_drain_queue(struct request_queue *q);
187 extern void blkcg_exit_queue(struct request_queue *q);
189 /* Blkio controller policy registration */
190 extern void blkio_policy_register(struct blkio_policy_type *);
191 extern void blkio_policy_unregister(struct blkio_policy_type *);
192 extern void blkg_destroy_all(struct request_queue *q, bool destroy_root);
193 extern void update_root_blkg_pd(struct request_queue *q,
194 enum blkio_policy_id plid);
197 * blkg_to_pdata - get policy private data
198 * @blkg: blkg of interest
199 * @pol: policy of interest
201 * Return pointer to private data associated with the @blkg-@pol pair.
203 static inline void *blkg_to_pdata(struct blkio_group *blkg,
204 struct blkio_policy_type *pol)
206 return blkg ? blkg->pd[pol->plid]->pdata : NULL;
210 * pdata_to_blkg - get blkg associated with policy private data
211 * @pdata: policy private data of interest
213 * @pdata is policy private data. Determine the blkg it's associated with.
215 static inline struct blkio_group *pdata_to_blkg(void *pdata)
218 struct blkg_policy_data *pd =
219 container_of(pdata, struct blkg_policy_data, pdata);
225 static inline char *blkg_path(struct blkio_group *blkg)
231 * blkg_get - get a blkg reference
234 * The caller should be holding queue_lock and an existing reference.
236 static inline void blkg_get(struct blkio_group *blkg)
238 lockdep_assert_held(blkg->q->queue_lock);
239 WARN_ON_ONCE(!blkg->refcnt);
243 void __blkg_release(struct blkio_group *blkg);
246 * blkg_put - put a blkg reference
249 * The caller should be holding queue_lock.
251 static inline void blkg_put(struct blkio_group *blkg)
253 lockdep_assert_held(blkg->q->queue_lock);
254 WARN_ON_ONCE(blkg->refcnt <= 0);
256 __blkg_release(blkg);
260 * blkg_stat_add - add a value to a blkg_stat
261 * @stat: target blkg_stat
264 * Add @val to @stat. The caller is responsible for synchronizing calls to
267 static inline void blkg_stat_add(struct blkg_stat *stat, uint64_t val)
269 u64_stats_update_begin(&stat->syncp);
271 u64_stats_update_end(&stat->syncp);
275 * blkg_stat_read - read the current value of a blkg_stat
276 * @stat: blkg_stat to read
278 * Read the current value of @stat. This function can be called without
279 * synchroniztion and takes care of u64 atomicity.
281 static inline uint64_t blkg_stat_read(struct blkg_stat *stat)
287 start = u64_stats_fetch_begin(&stat->syncp);
289 } while (u64_stats_fetch_retry(&stat->syncp, start));
295 * blkg_stat_reset - reset a blkg_stat
296 * @stat: blkg_stat to reset
298 static inline void blkg_stat_reset(struct blkg_stat *stat)
304 * blkg_rwstat_add - add a value to a blkg_rwstat
305 * @rwstat: target blkg_rwstat
306 * @rw: mask of REQ_{WRITE|SYNC}
309 * Add @val to @rwstat. The counters are chosen according to @rw. The
310 * caller is responsible for synchronizing calls to this function.
312 static inline void blkg_rwstat_add(struct blkg_rwstat *rwstat,
313 int rw, uint64_t val)
315 u64_stats_update_begin(&rwstat->syncp);
318 rwstat->cnt[BLKG_RWSTAT_WRITE] += val;
320 rwstat->cnt[BLKG_RWSTAT_READ] += val;
322 rwstat->cnt[BLKG_RWSTAT_SYNC] += val;
324 rwstat->cnt[BLKG_RWSTAT_ASYNC] += val;
326 u64_stats_update_end(&rwstat->syncp);
330 * blkg_rwstat_read - read the current values of a blkg_rwstat
331 * @rwstat: blkg_rwstat to read
333 * Read the current snapshot of @rwstat and return it as the return value.
334 * This function can be called without synchronization and takes care of
337 static struct blkg_rwstat blkg_rwstat_read(struct blkg_rwstat *rwstat)
340 struct blkg_rwstat tmp;
343 start = u64_stats_fetch_begin(&rwstat->syncp);
345 } while (u64_stats_fetch_retry(&rwstat->syncp, start));
351 * blkg_rwstat_sum - read the total count of a blkg_rwstat
352 * @rwstat: blkg_rwstat to read
354 * Return the total count of @rwstat regardless of the IO direction. This
355 * function can be called without synchronization and takes care of u64
358 static inline uint64_t blkg_rwstat_sum(struct blkg_rwstat *rwstat)
360 struct blkg_rwstat tmp = blkg_rwstat_read(rwstat);
362 return tmp.cnt[BLKG_RWSTAT_READ] + tmp.cnt[BLKG_RWSTAT_WRITE];
366 * blkg_rwstat_reset - reset a blkg_rwstat
367 * @rwstat: blkg_rwstat to reset
369 static inline void blkg_rwstat_reset(struct blkg_rwstat *rwstat)
371 memset(rwstat->cnt, 0, sizeof(rwstat->cnt));
379 struct blkio_policy_type {
382 static inline int blkcg_init_queue(struct request_queue *q) { return 0; }
383 static inline void blkcg_drain_queue(struct request_queue *q) { }
384 static inline void blkcg_exit_queue(struct request_queue *q) { }
385 static inline void blkio_policy_register(struct blkio_policy_type *blkiop) { }
386 static inline void blkio_policy_unregister(struct blkio_policy_type *blkiop) { }
387 static inline void blkg_destroy_all(struct request_queue *q,
388 bool destory_root) { }
389 static inline void update_root_blkg_pd(struct request_queue *q,
390 enum blkio_policy_id plid) { }
392 static inline void *blkg_to_pdata(struct blkio_group *blkg,
393 struct blkio_policy_type *pol) { return NULL; }
394 static inline struct blkio_group *pdata_to_blkg(void *pdata,
395 struct blkio_policy_type *pol) { return NULL; }
396 static inline char *blkg_path(struct blkio_group *blkg) { return NULL; }
397 static inline void blkg_get(struct blkio_group *blkg) { }
398 static inline void blkg_put(struct blkio_group *blkg) { }
402 #define BLKIO_WEIGHT_MIN 10
403 #define BLKIO_WEIGHT_MAX 1000
404 #define BLKIO_WEIGHT_DEFAULT 500
406 #ifdef CONFIG_DEBUG_BLK_CGROUP
407 void blkiocg_update_avg_queue_size_stats(struct blkio_group *blkg,
408 struct blkio_policy_type *pol);
409 void blkiocg_update_dequeue_stats(struct blkio_group *blkg,
410 struct blkio_policy_type *pol,
411 unsigned long dequeue);
412 void blkiocg_update_set_idle_time_stats(struct blkio_group *blkg,
413 struct blkio_policy_type *pol);
414 void blkiocg_update_idle_time_stats(struct blkio_group *blkg,
415 struct blkio_policy_type *pol);
416 void blkiocg_set_start_empty_time(struct blkio_group *blkg,
417 struct blkio_policy_type *pol);
419 #define BLKG_FLAG_FNS(name) \
420 static inline void blkio_mark_blkg_##name( \
421 struct blkio_group_stats *stats) \
423 stats->flags |= (1 << BLKG_##name); \
425 static inline void blkio_clear_blkg_##name( \
426 struct blkio_group_stats *stats) \
428 stats->flags &= ~(1 << BLKG_##name); \
430 static inline int blkio_blkg_##name(struct blkio_group_stats *stats) \
432 return (stats->flags & (1 << BLKG_##name)) != 0; \
435 BLKG_FLAG_FNS(waiting)
436 BLKG_FLAG_FNS(idling)
440 static inline void blkiocg_update_avg_queue_size_stats(struct blkio_group *blkg,
441 struct blkio_policy_type *pol) { }
442 static inline void blkiocg_update_dequeue_stats(struct blkio_group *blkg,
443 struct blkio_policy_type *pol, unsigned long dequeue) { }
444 static inline void blkiocg_update_set_idle_time_stats(struct blkio_group *blkg,
445 struct blkio_policy_type *pol) { }
446 static inline void blkiocg_update_idle_time_stats(struct blkio_group *blkg,
447 struct blkio_policy_type *pol) { }
448 static inline void blkiocg_set_start_empty_time(struct blkio_group *blkg,
449 struct blkio_policy_type *pol) { }
452 #ifdef CONFIG_BLK_CGROUP
453 extern struct blkio_cgroup blkio_root_cgroup;
454 extern struct blkio_cgroup *cgroup_to_blkio_cgroup(struct cgroup *cgroup);
455 extern struct blkio_cgroup *bio_blkio_cgroup(struct bio *bio);
456 extern struct blkio_group *blkg_lookup(struct blkio_cgroup *blkcg,
457 struct request_queue *q);
458 struct blkio_group *blkg_lookup_create(struct blkio_cgroup *blkcg,
459 struct request_queue *q,
461 void blkiocg_update_timeslice_used(struct blkio_group *blkg,
462 struct blkio_policy_type *pol,
464 unsigned long unaccounted_time);
465 void blkiocg_update_dispatch_stats(struct blkio_group *blkg,
466 struct blkio_policy_type *pol,
467 uint64_t bytes, bool direction, bool sync);
468 void blkiocg_update_completion_stats(struct blkio_group *blkg,
469 struct blkio_policy_type *pol,
471 uint64_t io_start_time, bool direction,
473 void blkiocg_update_io_merged_stats(struct blkio_group *blkg,
474 struct blkio_policy_type *pol,
475 bool direction, bool sync);
476 void blkiocg_update_io_add_stats(struct blkio_group *blkg,
477 struct blkio_policy_type *pol,
478 struct blkio_group *curr_blkg, bool direction,
480 void blkiocg_update_io_remove_stats(struct blkio_group *blkg,
481 struct blkio_policy_type *pol,
482 bool direction, bool sync);
485 static inline struct blkio_cgroup *
486 cgroup_to_blkio_cgroup(struct cgroup *cgroup) { return NULL; }
487 static inline struct blkio_cgroup *
488 bio_blkio_cgroup(struct bio *bio) { return NULL; }
490 static inline struct blkio_group *blkg_lookup(struct blkio_cgroup *blkcg,
491 void *key) { return NULL; }
492 static inline void blkiocg_update_timeslice_used(struct blkio_group *blkg,
493 struct blkio_policy_type *pol, unsigned long time,
494 unsigned long unaccounted_time) { }
495 static inline void blkiocg_update_dispatch_stats(struct blkio_group *blkg,
496 struct blkio_policy_type *pol, uint64_t bytes,
497 bool direction, bool sync) { }
498 static inline void blkiocg_update_completion_stats(struct blkio_group *blkg,
499 struct blkio_policy_type *pol, uint64_t start_time,
500 uint64_t io_start_time, bool direction, bool sync) { }
501 static inline void blkiocg_update_io_merged_stats(struct blkio_group *blkg,
502 struct blkio_policy_type *pol, bool direction,
504 static inline void blkiocg_update_io_add_stats(struct blkio_group *blkg,
505 struct blkio_policy_type *pol,
506 struct blkio_group *curr_blkg, bool direction,
508 static inline void blkiocg_update_io_remove_stats(struct blkio_group *blkg,
509 struct blkio_policy_type *pol, bool direction,
512 #endif /* _BLK_CGROUP_H */