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>
18 #include <linux/seq_file.h>
20 enum blkio_policy_id {
21 BLKIO_POLICY_PROP = 0, /* Proportional Bandwidth division */
22 BLKIO_POLICY_THROTL, /* Throttling */
27 /* Max limits for throttle policy */
28 #define THROTL_IOPS_MAX UINT_MAX
30 #ifdef CONFIG_BLK_CGROUP
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)
37 enum blkg_rwstat_type {
44 BLKG_RWSTAT_TOTAL = BLKG_RWSTAT_NR,
48 struct cgroup_subsys_state css;
51 struct hlist_head blkg_list;
53 /* for policies to test whether associated blkcg has changed */
58 struct u64_stats_sync syncp;
63 struct u64_stats_sync syncp;
64 uint64_t cnt[BLKG_RWSTAT_NR];
67 struct blkio_group_stats {
68 /* number of ios merged */
69 struct blkg_rwstat merged;
70 /* total time spent on device in ns, may not be accurate w/ queueing */
71 struct blkg_rwstat service_time;
72 /* total time spent waiting in scheduler queue in ns */
73 struct blkg_rwstat wait_time;
74 /* number of IOs queued up */
75 struct blkg_rwstat queued;
76 /* total disk time and nr sectors dispatched by this group */
77 struct blkg_stat time;
78 #ifdef CONFIG_DEBUG_BLK_CGROUP
79 /* time not charged to this cgroup */
80 struct blkg_stat unaccounted_time;
81 /* sum of number of ios queued across all samples */
82 struct blkg_stat avg_queue_size_sum;
83 /* count of samples taken for average */
84 struct blkg_stat avg_queue_size_samples;
85 /* how many times this group has been removed from service tree */
86 struct blkg_stat dequeue;
87 /* total time spent waiting for it to be assigned a timeslice. */
88 struct blkg_stat group_wait_time;
89 /* time spent idling for this blkio_group */
90 struct blkg_stat idle_time;
91 /* total time with empty current active q with other requests queued */
92 struct blkg_stat empty_time;
93 /* fields after this shouldn't be cleared on stat reset */
94 uint64_t start_group_wait_time;
95 uint64_t start_idle_time;
96 uint64_t start_empty_time;
101 /* Per cpu blkio group stats */
102 struct blkio_group_stats_cpu {
103 /* total bytes transferred */
104 struct blkg_rwstat service_bytes;
105 /* total IOs serviced, post merge */
106 struct blkg_rwstat serviced;
107 /* total sectors transferred */
108 struct blkg_stat sectors;
111 struct blkio_group_conf {
117 /* per-blkg per-policy data */
118 struct blkg_policy_data {
119 /* the blkg this per-policy data belongs to */
120 struct blkio_group *blkg;
123 struct blkio_group_conf conf;
125 struct blkio_group_stats stats;
126 /* Per cpu stats pointer */
127 struct blkio_group_stats_cpu __percpu *stats_cpu;
129 /* pol->pdata_size bytes of private data used by policy impl */
130 char pdata[] __aligned(__alignof__(unsigned long long));
134 /* Pointer to the associated request_queue */
135 struct request_queue *q;
136 struct list_head q_node;
137 struct hlist_node blkcg_node;
138 struct blkio_cgroup *blkcg;
139 /* Store cgroup path */
141 /* reference count */
144 struct blkg_policy_data *pd[BLKIO_NR_POLICIES];
146 /* List of blkg waiting for per cpu stats memory to be allocated */
147 struct list_head alloc_node;
148 struct rcu_head rcu_head;
151 typedef void (blkio_init_group_fn)(struct blkio_group *blkg);
153 struct blkio_policy_ops {
154 blkio_init_group_fn *blkio_init_group_fn;
157 struct blkio_policy_type {
158 struct list_head list;
159 struct blkio_policy_ops ops;
160 enum blkio_policy_id plid;
161 size_t pdata_size; /* policy specific private data size */
162 struct cftype *cftypes; /* cgroup files for the policy */
165 extern int blkcg_init_queue(struct request_queue *q);
166 extern void blkcg_drain_queue(struct request_queue *q);
167 extern void blkcg_exit_queue(struct request_queue *q);
169 /* Blkio controller policy registration */
170 extern void blkio_policy_register(struct blkio_policy_type *);
171 extern void blkio_policy_unregister(struct blkio_policy_type *);
172 extern void blkg_destroy_all(struct request_queue *q, bool destroy_root);
173 extern void update_root_blkg_pd(struct request_queue *q,
174 enum blkio_policy_id plid);
176 void blkcg_print_blkgs(struct seq_file *sf, struct blkio_cgroup *blkcg,
177 u64 (*prfill)(struct seq_file *, struct blkg_policy_data *, int),
178 int pol, int data, bool show_total);
179 u64 __blkg_prfill_u64(struct seq_file *sf, struct blkg_policy_data *pd, u64 v);
180 u64 __blkg_prfill_rwstat(struct seq_file *sf, struct blkg_policy_data *pd,
181 const struct blkg_rwstat *rwstat);
182 int blkcg_print_stat(struct cgroup *cgrp, struct cftype *cft,
183 struct seq_file *sf);
184 int blkcg_print_rwstat(struct cgroup *cgrp, struct cftype *cft,
185 struct seq_file *sf);
186 int blkcg_print_cpu_stat(struct cgroup *cgrp, struct cftype *cft,
187 struct seq_file *sf);
188 int blkcg_print_cpu_rwstat(struct cgroup *cgrp, struct cftype *cft,
189 struct seq_file *sf);
191 struct blkg_conf_ctx {
192 struct gendisk *disk;
193 struct blkio_group *blkg;
197 int blkg_conf_prep(struct blkio_cgroup *blkcg, const char *input,
198 struct blkg_conf_ctx *ctx);
199 void blkg_conf_finish(struct blkg_conf_ctx *ctx);
203 * blkg_to_pdata - get policy private data
204 * @blkg: blkg of interest
205 * @pol: policy of interest
207 * Return pointer to private data associated with the @blkg-@pol pair.
209 static inline void *blkg_to_pdata(struct blkio_group *blkg,
210 struct blkio_policy_type *pol)
212 return blkg ? blkg->pd[pol->plid]->pdata : NULL;
216 * pdata_to_blkg - get blkg associated with policy private data
217 * @pdata: policy private data of interest
219 * @pdata is policy private data. Determine the blkg it's associated with.
221 static inline struct blkio_group *pdata_to_blkg(void *pdata)
224 struct blkg_policy_data *pd =
225 container_of(pdata, struct blkg_policy_data, pdata);
231 static inline char *blkg_path(struct blkio_group *blkg)
237 * blkg_get - get a blkg reference
240 * The caller should be holding queue_lock and an existing reference.
242 static inline void blkg_get(struct blkio_group *blkg)
244 lockdep_assert_held(blkg->q->queue_lock);
245 WARN_ON_ONCE(!blkg->refcnt);
249 void __blkg_release(struct blkio_group *blkg);
252 * blkg_put - put a blkg reference
255 * The caller should be holding queue_lock.
257 static inline void blkg_put(struct blkio_group *blkg)
259 lockdep_assert_held(blkg->q->queue_lock);
260 WARN_ON_ONCE(blkg->refcnt <= 0);
262 __blkg_release(blkg);
266 * blkg_stat_add - add a value to a blkg_stat
267 * @stat: target blkg_stat
270 * Add @val to @stat. The caller is responsible for synchronizing calls to
273 static inline void blkg_stat_add(struct blkg_stat *stat, uint64_t val)
275 u64_stats_update_begin(&stat->syncp);
277 u64_stats_update_end(&stat->syncp);
281 * blkg_stat_read - read the current value of a blkg_stat
282 * @stat: blkg_stat to read
284 * Read the current value of @stat. This function can be called without
285 * synchroniztion and takes care of u64 atomicity.
287 static inline uint64_t blkg_stat_read(struct blkg_stat *stat)
293 start = u64_stats_fetch_begin(&stat->syncp);
295 } while (u64_stats_fetch_retry(&stat->syncp, start));
301 * blkg_stat_reset - reset a blkg_stat
302 * @stat: blkg_stat to reset
304 static inline void blkg_stat_reset(struct blkg_stat *stat)
310 * blkg_rwstat_add - add a value to a blkg_rwstat
311 * @rwstat: target blkg_rwstat
312 * @rw: mask of REQ_{WRITE|SYNC}
315 * Add @val to @rwstat. The counters are chosen according to @rw. The
316 * caller is responsible for synchronizing calls to this function.
318 static inline void blkg_rwstat_add(struct blkg_rwstat *rwstat,
319 int rw, uint64_t val)
321 u64_stats_update_begin(&rwstat->syncp);
324 rwstat->cnt[BLKG_RWSTAT_WRITE] += val;
326 rwstat->cnt[BLKG_RWSTAT_READ] += val;
328 rwstat->cnt[BLKG_RWSTAT_SYNC] += val;
330 rwstat->cnt[BLKG_RWSTAT_ASYNC] += val;
332 u64_stats_update_end(&rwstat->syncp);
336 * blkg_rwstat_read - read the current values of a blkg_rwstat
337 * @rwstat: blkg_rwstat to read
339 * Read the current snapshot of @rwstat and return it as the return value.
340 * This function can be called without synchronization and takes care of
343 static struct blkg_rwstat blkg_rwstat_read(struct blkg_rwstat *rwstat)
346 struct blkg_rwstat tmp;
349 start = u64_stats_fetch_begin(&rwstat->syncp);
351 } while (u64_stats_fetch_retry(&rwstat->syncp, start));
357 * blkg_rwstat_sum - read the total count of a blkg_rwstat
358 * @rwstat: blkg_rwstat to read
360 * Return the total count of @rwstat regardless of the IO direction. This
361 * function can be called without synchronization and takes care of u64
364 static inline uint64_t blkg_rwstat_sum(struct blkg_rwstat *rwstat)
366 struct blkg_rwstat tmp = blkg_rwstat_read(rwstat);
368 return tmp.cnt[BLKG_RWSTAT_READ] + tmp.cnt[BLKG_RWSTAT_WRITE];
372 * blkg_rwstat_reset - reset a blkg_rwstat
373 * @rwstat: blkg_rwstat to reset
375 static inline void blkg_rwstat_reset(struct blkg_rwstat *rwstat)
377 memset(rwstat->cnt, 0, sizeof(rwstat->cnt));
385 struct blkio_policy_type {
388 static inline int blkcg_init_queue(struct request_queue *q) { return 0; }
389 static inline void blkcg_drain_queue(struct request_queue *q) { }
390 static inline void blkcg_exit_queue(struct request_queue *q) { }
391 static inline void blkio_policy_register(struct blkio_policy_type *blkiop) { }
392 static inline void blkio_policy_unregister(struct blkio_policy_type *blkiop) { }
393 static inline void blkg_destroy_all(struct request_queue *q,
394 bool destory_root) { }
395 static inline void update_root_blkg_pd(struct request_queue *q,
396 enum blkio_policy_id plid) { }
398 static inline void *blkg_to_pdata(struct blkio_group *blkg,
399 struct blkio_policy_type *pol) { return NULL; }
400 static inline struct blkio_group *pdata_to_blkg(void *pdata,
401 struct blkio_policy_type *pol) { return NULL; }
402 static inline char *blkg_path(struct blkio_group *blkg) { return NULL; }
403 static inline void blkg_get(struct blkio_group *blkg) { }
404 static inline void blkg_put(struct blkio_group *blkg) { }
408 #define BLKIO_WEIGHT_MIN 10
409 #define BLKIO_WEIGHT_MAX 1000
410 #define BLKIO_WEIGHT_DEFAULT 500
412 #ifdef CONFIG_BLK_CGROUP
413 extern struct blkio_cgroup blkio_root_cgroup;
414 extern struct blkio_cgroup *cgroup_to_blkio_cgroup(struct cgroup *cgroup);
415 extern struct blkio_cgroup *bio_blkio_cgroup(struct bio *bio);
416 extern struct blkio_group *blkg_lookup(struct blkio_cgroup *blkcg,
417 struct request_queue *q);
418 struct blkio_group *blkg_lookup_create(struct blkio_cgroup *blkcg,
419 struct request_queue *q,
423 static inline struct blkio_cgroup *
424 cgroup_to_blkio_cgroup(struct cgroup *cgroup) { return NULL; }
425 static inline struct blkio_cgroup *
426 bio_blkio_cgroup(struct bio *bio) { return NULL; }
428 static inline struct blkio_group *blkg_lookup(struct blkio_cgroup *blkcg,
429 void *key) { return NULL; }
431 #endif /* _BLK_CGROUP_H */