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 /* CFQ specific, out here for blkcg->cfq_weight */
33 #define CFQ_WEIGHT_MIN 10
34 #define CFQ_WEIGHT_MAX 1000
35 #define CFQ_WEIGHT_DEFAULT 500
37 /* cft->private [un]packing for stat printing */
38 #define BLKCG_STAT_PRIV(pol, off) (((unsigned)(pol) << 16) | (off))
39 #define BLKCG_STAT_POL(prv) ((unsigned)(prv) >> 16)
40 #define BLKCG_STAT_OFF(prv) ((unsigned)(prv) & 0xffff)
42 enum blkg_rwstat_type {
49 BLKG_RWSTAT_TOTAL = BLKG_RWSTAT_NR,
53 struct cgroup_subsys_state css;
55 struct hlist_head blkg_list;
57 /* for policies to test whether associated blkcg has changed */
60 /* TODO: per-policy storage in blkio_cgroup */
61 unsigned int cfq_weight; /* belongs to cfq */
65 struct u64_stats_sync syncp;
70 struct u64_stats_sync syncp;
71 uint64_t cnt[BLKG_RWSTAT_NR];
74 struct blkio_group_conf {
79 /* per-blkg per-policy data */
80 struct blkg_policy_data {
81 /* the blkg this per-policy data belongs to */
82 struct blkio_group *blkg;
85 struct blkio_group_conf conf;
87 /* pol->pdata_size bytes of private data used by policy impl */
88 char pdata[] __aligned(__alignof__(unsigned long long));
92 /* Pointer to the associated request_queue */
93 struct request_queue *q;
94 struct list_head q_node;
95 struct hlist_node blkcg_node;
96 struct blkio_cgroup *blkcg;
97 /* Store cgroup path */
102 struct blkg_policy_data *pd[BLKIO_NR_POLICIES];
104 struct rcu_head rcu_head;
107 typedef void (blkio_init_group_fn)(struct blkio_group *blkg);
108 typedef void (blkio_exit_group_fn)(struct blkio_group *blkg);
109 typedef void (blkio_reset_group_stats_fn)(struct blkio_group *blkg);
111 struct blkio_policy_ops {
112 blkio_init_group_fn *blkio_init_group_fn;
113 blkio_exit_group_fn *blkio_exit_group_fn;
114 blkio_reset_group_stats_fn *blkio_reset_group_stats_fn;
117 struct blkio_policy_type {
118 struct list_head list;
119 struct blkio_policy_ops ops;
120 enum blkio_policy_id plid;
121 size_t pdata_size; /* policy specific private data size */
122 struct cftype *cftypes; /* cgroup files for the policy */
125 extern int blkcg_init_queue(struct request_queue *q);
126 extern void blkcg_drain_queue(struct request_queue *q);
127 extern void blkcg_exit_queue(struct request_queue *q);
129 /* Blkio controller policy registration */
130 extern void blkio_policy_register(struct blkio_policy_type *);
131 extern void blkio_policy_unregister(struct blkio_policy_type *);
132 extern void blkg_destroy_all(struct request_queue *q, bool destroy_root);
133 extern void update_root_blkg_pd(struct request_queue *q,
134 enum blkio_policy_id plid);
136 void blkcg_print_blkgs(struct seq_file *sf, struct blkio_cgroup *blkcg,
137 u64 (*prfill)(struct seq_file *, struct blkg_policy_data *, int),
138 int pol, int data, bool show_total);
139 u64 __blkg_prfill_u64(struct seq_file *sf, struct blkg_policy_data *pd, u64 v);
140 u64 __blkg_prfill_rwstat(struct seq_file *sf, struct blkg_policy_data *pd,
141 const struct blkg_rwstat *rwstat);
142 int blkcg_print_stat(struct cgroup *cgrp, struct cftype *cft,
143 struct seq_file *sf);
144 int blkcg_print_rwstat(struct cgroup *cgrp, struct cftype *cft,
145 struct seq_file *sf);
147 struct blkg_conf_ctx {
148 struct gendisk *disk;
149 struct blkio_group *blkg;
153 int blkg_conf_prep(struct blkio_cgroup *blkcg, const char *input,
154 struct blkg_conf_ctx *ctx);
155 void blkg_conf_finish(struct blkg_conf_ctx *ctx);
159 * blkg_to_pdata - get policy private data
160 * @blkg: blkg of interest
161 * @pol: policy of interest
163 * Return pointer to private data associated with the @blkg-@pol pair.
165 static inline void *blkg_to_pdata(struct blkio_group *blkg,
166 struct blkio_policy_type *pol)
168 return blkg ? blkg->pd[pol->plid]->pdata : NULL;
172 * pdata_to_blkg - get blkg associated with policy private data
173 * @pdata: policy private data of interest
175 * @pdata is policy private data. Determine the blkg it's associated with.
177 static inline struct blkio_group *pdata_to_blkg(void *pdata)
180 struct blkg_policy_data *pd =
181 container_of(pdata, struct blkg_policy_data, pdata);
187 static inline char *blkg_path(struct blkio_group *blkg)
193 * blkg_get - get a blkg reference
196 * The caller should be holding queue_lock and an existing reference.
198 static inline void blkg_get(struct blkio_group *blkg)
200 lockdep_assert_held(blkg->q->queue_lock);
201 WARN_ON_ONCE(!blkg->refcnt);
205 void __blkg_release(struct blkio_group *blkg);
208 * blkg_put - put a blkg reference
211 * The caller should be holding queue_lock.
213 static inline void blkg_put(struct blkio_group *blkg)
215 lockdep_assert_held(blkg->q->queue_lock);
216 WARN_ON_ONCE(blkg->refcnt <= 0);
218 __blkg_release(blkg);
222 * blkg_stat_add - add a value to a blkg_stat
223 * @stat: target blkg_stat
226 * Add @val to @stat. The caller is responsible for synchronizing calls to
229 static inline void blkg_stat_add(struct blkg_stat *stat, uint64_t val)
231 u64_stats_update_begin(&stat->syncp);
233 u64_stats_update_end(&stat->syncp);
237 * blkg_stat_read - read the current value of a blkg_stat
238 * @stat: blkg_stat to read
240 * Read the current value of @stat. This function can be called without
241 * synchroniztion and takes care of u64 atomicity.
243 static inline uint64_t blkg_stat_read(struct blkg_stat *stat)
249 start = u64_stats_fetch_begin(&stat->syncp);
251 } while (u64_stats_fetch_retry(&stat->syncp, start));
257 * blkg_stat_reset - reset a blkg_stat
258 * @stat: blkg_stat to reset
260 static inline void blkg_stat_reset(struct blkg_stat *stat)
266 * blkg_rwstat_add - add a value to a blkg_rwstat
267 * @rwstat: target blkg_rwstat
268 * @rw: mask of REQ_{WRITE|SYNC}
271 * Add @val to @rwstat. The counters are chosen according to @rw. The
272 * caller is responsible for synchronizing calls to this function.
274 static inline void blkg_rwstat_add(struct blkg_rwstat *rwstat,
275 int rw, uint64_t val)
277 u64_stats_update_begin(&rwstat->syncp);
280 rwstat->cnt[BLKG_RWSTAT_WRITE] += val;
282 rwstat->cnt[BLKG_RWSTAT_READ] += val;
284 rwstat->cnt[BLKG_RWSTAT_SYNC] += val;
286 rwstat->cnt[BLKG_RWSTAT_ASYNC] += val;
288 u64_stats_update_end(&rwstat->syncp);
292 * blkg_rwstat_read - read the current values of a blkg_rwstat
293 * @rwstat: blkg_rwstat to read
295 * Read the current snapshot of @rwstat and return it as the return value.
296 * This function can be called without synchronization and takes care of
299 static struct blkg_rwstat blkg_rwstat_read(struct blkg_rwstat *rwstat)
302 struct blkg_rwstat tmp;
305 start = u64_stats_fetch_begin(&rwstat->syncp);
307 } while (u64_stats_fetch_retry(&rwstat->syncp, start));
313 * blkg_rwstat_sum - read the total count of a blkg_rwstat
314 * @rwstat: blkg_rwstat to read
316 * Return the total count of @rwstat regardless of the IO direction. This
317 * function can be called without synchronization and takes care of u64
320 static inline uint64_t blkg_rwstat_sum(struct blkg_rwstat *rwstat)
322 struct blkg_rwstat tmp = blkg_rwstat_read(rwstat);
324 return tmp.cnt[BLKG_RWSTAT_READ] + tmp.cnt[BLKG_RWSTAT_WRITE];
328 * blkg_rwstat_reset - reset a blkg_rwstat
329 * @rwstat: blkg_rwstat to reset
331 static inline void blkg_rwstat_reset(struct blkg_rwstat *rwstat)
333 memset(rwstat->cnt, 0, sizeof(rwstat->cnt));
341 struct blkio_policy_type {
344 static inline int blkcg_init_queue(struct request_queue *q) { return 0; }
345 static inline void blkcg_drain_queue(struct request_queue *q) { }
346 static inline void blkcg_exit_queue(struct request_queue *q) { }
347 static inline void blkio_policy_register(struct blkio_policy_type *blkiop) { }
348 static inline void blkio_policy_unregister(struct blkio_policy_type *blkiop) { }
349 static inline void blkg_destroy_all(struct request_queue *q,
350 bool destory_root) { }
351 static inline void update_root_blkg_pd(struct request_queue *q,
352 enum blkio_policy_id plid) { }
354 static inline void *blkg_to_pdata(struct blkio_group *blkg,
355 struct blkio_policy_type *pol) { return NULL; }
356 static inline struct blkio_group *pdata_to_blkg(void *pdata,
357 struct blkio_policy_type *pol) { return NULL; }
358 static inline char *blkg_path(struct blkio_group *blkg) { return NULL; }
359 static inline void blkg_get(struct blkio_group *blkg) { }
360 static inline void blkg_put(struct blkio_group *blkg) { }
364 #ifdef CONFIG_BLK_CGROUP
365 extern struct blkio_cgroup blkio_root_cgroup;
366 extern struct blkio_cgroup *cgroup_to_blkio_cgroup(struct cgroup *cgroup);
367 extern struct blkio_cgroup *bio_blkio_cgroup(struct bio *bio);
368 extern struct blkio_group *blkg_lookup(struct blkio_cgroup *blkcg,
369 struct request_queue *q);
370 struct blkio_group *blkg_lookup_create(struct blkio_cgroup *blkcg,
371 struct request_queue *q,
375 static inline struct blkio_cgroup *
376 cgroup_to_blkio_cgroup(struct cgroup *cgroup) { return NULL; }
377 static inline struct blkio_cgroup *
378 bio_blkio_cgroup(struct bio *bio) { return NULL; }
380 static inline struct blkio_group *blkg_lookup(struct blkio_cgroup *blkcg,
381 void *key) { return NULL; }
383 #endif /* _BLK_CGROUP_H */