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>
19 #include <linux/radix-tree.h>
21 /* Max limits for throttle policy */
22 #define THROTL_IOPS_MAX UINT_MAX
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
29 #ifdef CONFIG_BLK_CGROUP
31 enum blkg_rwstat_type {
38 BLKG_RWSTAT_TOTAL = BLKG_RWSTAT_NR,
44 struct cgroup_subsys_state css;
47 struct radix_tree_root blkg_tree;
48 struct blkcg_gq *blkg_hint;
49 struct hlist_head blkg_list;
51 /* for policies to test whether associated blkcg has changed */
54 /* TODO: per-policy storage in blkcg */
55 unsigned int cfq_weight; /* belongs to cfq */
59 struct u64_stats_sync syncp;
64 struct u64_stats_sync syncp;
65 uint64_t cnt[BLKG_RWSTAT_NR];
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.
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.
78 * Such private data must embed struct blkg_policy_data (pd) at the
79 * beginning and pd_size can't be smaller than pd.
81 struct blkg_policy_data {
82 /* the blkg this per-policy data belongs to */
83 struct blkcg_gq *blkg;
85 /* used during policy activation */
86 struct list_head alloc_node;
89 /* association between a blk cgroup and a request queue */
91 /* Pointer to the associated request_queue */
92 struct request_queue *q;
93 struct list_head q_node;
94 struct hlist_node blkcg_node;
99 struct blkg_policy_data *pd[BLKCG_MAX_POLS];
101 struct rcu_head rcu_head;
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);
108 struct blkcg_policy {
110 /* policy specific private data size */
112 /* cgroup files for the policy */
113 struct cftype *cftypes;
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;
121 extern struct blkcg blkcg_root;
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);
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);
132 /* Blkio controller policy registration */
133 int blkcg_policy_register(struct blkcg_policy *pol);
134 void blkcg_policy_unregister(struct blkcg_policy *pol);
135 int blkcg_activate_policy(struct request_queue *q,
136 const struct blkcg_policy *pol);
137 void blkcg_deactivate_policy(struct request_queue *q,
138 const struct blkcg_policy *pol);
140 void blkcg_print_blkgs(struct seq_file *sf, struct blkcg *blkcg,
141 u64 (*prfill)(struct seq_file *,
142 struct blkg_policy_data *, int),
143 const struct blkcg_policy *pol, int data,
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,
147 const struct blkg_rwstat *rwstat);
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,
152 struct blkg_conf_ctx {
153 struct gendisk *disk;
154 struct blkcg_gq *blkg;
158 int blkg_conf_prep(struct blkcg *blkcg, const struct blkcg_policy *pol,
159 const char *input, struct blkg_conf_ctx *ctx);
160 void blkg_conf_finish(struct blkg_conf_ctx *ctx);
164 * blkg_to_pdata - get policy private data
165 * @blkg: blkg of interest
166 * @pol: policy of interest
168 * Return pointer to private data associated with the @blkg-@pol pair.
170 static inline struct blkg_policy_data *blkg_to_pd(struct blkcg_gq *blkg,
171 struct blkcg_policy *pol)
173 return blkg ? blkg->pd[pol->plid] : NULL;
177 * pdata_to_blkg - get blkg associated with policy private data
178 * @pd: policy private data of interest
180 * @pd is policy private data. Determine the blkg it's associated with.
182 static inline struct blkcg_gq *pd_to_blkg(struct blkg_policy_data *pd)
184 return pd ? pd->blkg : NULL;
188 * blkg_path - format cgroup path of blkg
189 * @blkg: blkg of interest
190 * @buf: target buffer
191 * @buflen: target buffer length
193 * Format the path of the cgroup of @blkg into @buf.
195 static inline int blkg_path(struct blkcg_gq *blkg, char *buf, int buflen)
200 ret = cgroup_path(blkg->blkcg->css.cgroup, buf, buflen);
203 strncpy(buf, "<unavailable>", buflen);
208 * blkg_get - get a blkg reference
211 * The caller should be holding queue_lock and an existing reference.
213 static inline void blkg_get(struct blkcg_gq *blkg)
215 lockdep_assert_held(blkg->q->queue_lock);
216 WARN_ON_ONCE(!blkg->refcnt);
220 void __blkg_release(struct blkcg_gq *blkg);
223 * blkg_put - put a blkg reference
226 * The caller should be holding queue_lock.
228 static inline void blkg_put(struct blkcg_gq *blkg)
230 lockdep_assert_held(blkg->q->queue_lock);
231 WARN_ON_ONCE(blkg->refcnt <= 0);
233 __blkg_release(blkg);
237 * blkg_stat_add - add a value to a blkg_stat
238 * @stat: target blkg_stat
241 * Add @val to @stat. The caller is responsible for synchronizing calls to
244 static inline void blkg_stat_add(struct blkg_stat *stat, uint64_t val)
246 u64_stats_update_begin(&stat->syncp);
248 u64_stats_update_end(&stat->syncp);
252 * blkg_stat_read - read the current value of a blkg_stat
253 * @stat: blkg_stat to read
255 * Read the current value of @stat. This function can be called without
256 * synchroniztion and takes care of u64 atomicity.
258 static inline uint64_t blkg_stat_read(struct blkg_stat *stat)
264 start = u64_stats_fetch_begin(&stat->syncp);
266 } while (u64_stats_fetch_retry(&stat->syncp, start));
272 * blkg_stat_reset - reset a blkg_stat
273 * @stat: blkg_stat to reset
275 static inline void blkg_stat_reset(struct blkg_stat *stat)
281 * blkg_rwstat_add - add a value to a blkg_rwstat
282 * @rwstat: target blkg_rwstat
283 * @rw: mask of REQ_{WRITE|SYNC}
286 * Add @val to @rwstat. The counters are chosen according to @rw. The
287 * caller is responsible for synchronizing calls to this function.
289 static inline void blkg_rwstat_add(struct blkg_rwstat *rwstat,
290 int rw, uint64_t val)
292 u64_stats_update_begin(&rwstat->syncp);
295 rwstat->cnt[BLKG_RWSTAT_WRITE] += val;
297 rwstat->cnt[BLKG_RWSTAT_READ] += val;
299 rwstat->cnt[BLKG_RWSTAT_SYNC] += val;
301 rwstat->cnt[BLKG_RWSTAT_ASYNC] += val;
303 u64_stats_update_end(&rwstat->syncp);
307 * blkg_rwstat_read - read the current values of a blkg_rwstat
308 * @rwstat: blkg_rwstat to read
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
314 static inline struct blkg_rwstat blkg_rwstat_read(struct blkg_rwstat *rwstat)
317 struct blkg_rwstat tmp;
320 start = u64_stats_fetch_begin(&rwstat->syncp);
322 } while (u64_stats_fetch_retry(&rwstat->syncp, start));
328 * blkg_rwstat_sum - read the total count of a blkg_rwstat
329 * @rwstat: blkg_rwstat to read
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
335 static inline uint64_t blkg_rwstat_sum(struct blkg_rwstat *rwstat)
337 struct blkg_rwstat tmp = blkg_rwstat_read(rwstat);
339 return tmp.cnt[BLKG_RWSTAT_READ] + tmp.cnt[BLKG_RWSTAT_WRITE];
343 * blkg_rwstat_reset - reset a blkg_rwstat
344 * @rwstat: blkg_rwstat to reset
346 static inline void blkg_rwstat_reset(struct blkg_rwstat *rwstat)
348 memset(rwstat->cnt, 0, sizeof(rwstat->cnt));
351 #else /* CONFIG_BLK_CGROUP */
355 struct blkg_policy_data {
361 struct blkcg_policy {
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; }
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) { }
370 static inline int blkcg_policy_register(struct blkcg_policy *pol) { return 0; }
371 static inline void blkcg_policy_unregister(struct blkcg_policy *pol) { }
372 static inline int blkcg_activate_policy(struct request_queue *q,
373 const struct blkcg_policy *pol) { return 0; }
374 static inline void blkcg_deactivate_policy(struct request_queue *q,
375 const struct blkcg_policy *pol) { }
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; }
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) { }
384 #endif /* CONFIG_BLK_CGROUP */
385 #endif /* _BLK_CGROUP_H */