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 /* Max limits for throttle policy */
21 #define THROTL_IOPS_MAX UINT_MAX
23 /* CFQ specific, out here for blkcg->cfq_weight */
24 #define CFQ_WEIGHT_MIN 10
25 #define CFQ_WEIGHT_MAX 1000
26 #define CFQ_WEIGHT_DEFAULT 500
28 #ifdef CONFIG_BLK_CGROUP
30 enum blkg_rwstat_type {
37 BLKG_RWSTAT_TOTAL = BLKG_RWSTAT_NR,
41 struct cgroup_subsys_state css;
43 struct hlist_head blkg_list;
45 /* for policies to test whether associated blkcg has changed */
48 /* TODO: per-policy storage in blkcg */
49 unsigned int cfq_weight; /* belongs to cfq */
53 struct u64_stats_sync syncp;
58 struct u64_stats_sync syncp;
59 uint64_t cnt[BLKG_RWSTAT_NR];
63 * A blkcg_gq (blkg) is association between a block cgroup (blkcg) and a
64 * request_queue (q). This is used by blkcg policies which need to track
65 * information per blkcg - q pair.
67 * There can be multiple active blkcg policies and each has its private
68 * data on each blkg, the size of which is determined by
69 * blkcg_policy->pd_size. blkcg core allocates and frees such areas
70 * together with blkg and invokes pd_init/exit_fn() methods.
72 * Such private data must embed struct blkg_policy_data (pd) at the
73 * beginning and pd_size can't be smaller than pd.
75 struct blkg_policy_data {
76 /* the blkg this per-policy data belongs to */
77 struct blkcg_gq *blkg;
79 /* used during policy activation */
80 struct list_head alloc_node;
83 /* association between a blk cgroup and a request queue */
85 /* Pointer to the associated request_queue */
86 struct request_queue *q;
87 struct list_head q_node;
88 struct hlist_node blkcg_node;
93 struct blkg_policy_data *pd[BLKCG_MAX_POLS];
95 struct rcu_head rcu_head;
98 typedef void (blkcg_pol_init_pd_fn)(struct blkcg_gq *blkg);
99 typedef void (blkcg_pol_exit_pd_fn)(struct blkcg_gq *blkg);
100 typedef void (blkcg_pol_reset_pd_stats_fn)(struct blkcg_gq *blkg);
102 struct blkcg_policy {
104 /* policy specific private data size */
106 /* cgroup files for the policy */
107 struct cftype *cftypes;
110 blkcg_pol_init_pd_fn *pd_init_fn;
111 blkcg_pol_exit_pd_fn *pd_exit_fn;
112 blkcg_pol_reset_pd_stats_fn *pd_reset_stats_fn;
115 extern struct blkcg blkcg_root;
117 struct blkcg *cgroup_to_blkcg(struct cgroup *cgroup);
118 struct blkcg *bio_blkcg(struct bio *bio);
119 struct blkcg_gq *blkg_lookup(struct blkcg *blkcg, struct request_queue *q);
120 struct blkcg_gq *blkg_lookup_create(struct blkcg *blkcg,
121 struct request_queue *q);
122 int blkcg_init_queue(struct request_queue *q);
123 void blkcg_drain_queue(struct request_queue *q);
124 void blkcg_exit_queue(struct request_queue *q);
126 /* Blkio controller policy registration */
127 int blkcg_policy_register(struct blkcg_policy *pol);
128 void blkcg_policy_unregister(struct blkcg_policy *pol);
129 int blkcg_activate_policy(struct request_queue *q,
130 const struct blkcg_policy *pol);
131 void blkcg_deactivate_policy(struct request_queue *q,
132 const struct blkcg_policy *pol);
134 void blkcg_print_blkgs(struct seq_file *sf, struct blkcg *blkcg,
135 u64 (*prfill)(struct seq_file *,
136 struct blkg_policy_data *, int),
137 const struct blkcg_policy *pol, int data,
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 u64 blkg_prfill_stat(struct seq_file *sf, struct blkg_policy_data *pd, int off);
143 u64 blkg_prfill_rwstat(struct seq_file *sf, struct blkg_policy_data *pd,
146 struct blkg_conf_ctx {
147 struct gendisk *disk;
148 struct blkcg_gq *blkg;
152 int blkg_conf_prep(struct blkcg *blkcg, const struct blkcg_policy *pol,
153 const char *input, struct blkg_conf_ctx *ctx);
154 void blkg_conf_finish(struct blkg_conf_ctx *ctx);
158 * blkg_to_pdata - get policy private data
159 * @blkg: blkg of interest
160 * @pol: policy of interest
162 * Return pointer to private data associated with the @blkg-@pol pair.
164 static inline struct blkg_policy_data *blkg_to_pd(struct blkcg_gq *blkg,
165 struct blkcg_policy *pol)
167 return blkg ? blkg->pd[pol->plid] : NULL;
171 * pdata_to_blkg - get blkg associated with policy private data
172 * @pd: policy private data of interest
174 * @pd is policy private data. Determine the blkg it's associated with.
176 static inline struct blkcg_gq *pd_to_blkg(struct blkg_policy_data *pd)
178 return pd ? pd->blkg : NULL;
182 * blkg_path - format cgroup path of blkg
183 * @blkg: blkg of interest
184 * @buf: target buffer
185 * @buflen: target buffer length
187 * Format the path of the cgroup of @blkg into @buf.
189 static inline int blkg_path(struct blkcg_gq *blkg, char *buf, int buflen)
194 ret = cgroup_path(blkg->blkcg->css.cgroup, buf, buflen);
197 strncpy(buf, "<unavailable>", buflen);
202 * blkg_get - get a blkg reference
205 * The caller should be holding queue_lock and an existing reference.
207 static inline void blkg_get(struct blkcg_gq *blkg)
209 lockdep_assert_held(blkg->q->queue_lock);
210 WARN_ON_ONCE(!blkg->refcnt);
214 void __blkg_release(struct blkcg_gq *blkg);
217 * blkg_put - put a blkg reference
220 * The caller should be holding queue_lock.
222 static inline void blkg_put(struct blkcg_gq *blkg)
224 lockdep_assert_held(blkg->q->queue_lock);
225 WARN_ON_ONCE(blkg->refcnt <= 0);
227 __blkg_release(blkg);
231 * blkg_stat_add - add a value to a blkg_stat
232 * @stat: target blkg_stat
235 * Add @val to @stat. The caller is responsible for synchronizing calls to
238 static inline void blkg_stat_add(struct blkg_stat *stat, uint64_t val)
240 u64_stats_update_begin(&stat->syncp);
242 u64_stats_update_end(&stat->syncp);
246 * blkg_stat_read - read the current value of a blkg_stat
247 * @stat: blkg_stat to read
249 * Read the current value of @stat. This function can be called without
250 * synchroniztion and takes care of u64 atomicity.
252 static inline uint64_t blkg_stat_read(struct blkg_stat *stat)
258 start = u64_stats_fetch_begin(&stat->syncp);
260 } while (u64_stats_fetch_retry(&stat->syncp, start));
266 * blkg_stat_reset - reset a blkg_stat
267 * @stat: blkg_stat to reset
269 static inline void blkg_stat_reset(struct blkg_stat *stat)
275 * blkg_rwstat_add - add a value to a blkg_rwstat
276 * @rwstat: target blkg_rwstat
277 * @rw: mask of REQ_{WRITE|SYNC}
280 * Add @val to @rwstat. The counters are chosen according to @rw. The
281 * caller is responsible for synchronizing calls to this function.
283 static inline void blkg_rwstat_add(struct blkg_rwstat *rwstat,
284 int rw, uint64_t val)
286 u64_stats_update_begin(&rwstat->syncp);
289 rwstat->cnt[BLKG_RWSTAT_WRITE] += val;
291 rwstat->cnt[BLKG_RWSTAT_READ] += val;
293 rwstat->cnt[BLKG_RWSTAT_SYNC] += val;
295 rwstat->cnt[BLKG_RWSTAT_ASYNC] += val;
297 u64_stats_update_end(&rwstat->syncp);
301 * blkg_rwstat_read - read the current values of a blkg_rwstat
302 * @rwstat: blkg_rwstat to read
304 * Read the current snapshot of @rwstat and return it as the return value.
305 * This function can be called without synchronization and takes care of
308 static inline struct blkg_rwstat blkg_rwstat_read(struct blkg_rwstat *rwstat)
311 struct blkg_rwstat tmp;
314 start = u64_stats_fetch_begin(&rwstat->syncp);
316 } while (u64_stats_fetch_retry(&rwstat->syncp, start));
322 * blkg_rwstat_sum - read the total count of a blkg_rwstat
323 * @rwstat: blkg_rwstat to read
325 * Return the total count of @rwstat regardless of the IO direction. This
326 * function can be called without synchronization and takes care of u64
329 static inline uint64_t blkg_rwstat_sum(struct blkg_rwstat *rwstat)
331 struct blkg_rwstat tmp = blkg_rwstat_read(rwstat);
333 return tmp.cnt[BLKG_RWSTAT_READ] + tmp.cnt[BLKG_RWSTAT_WRITE];
337 * blkg_rwstat_reset - reset a blkg_rwstat
338 * @rwstat: blkg_rwstat to reset
340 static inline void blkg_rwstat_reset(struct blkg_rwstat *rwstat)
342 memset(rwstat->cnt, 0, sizeof(rwstat->cnt));
345 #else /* CONFIG_BLK_CGROUP */
349 struct blkg_policy_data {
355 struct blkcg_policy {
358 static inline struct blkcg *cgroup_to_blkcg(struct cgroup *cgroup) { return NULL; }
359 static inline struct blkcg *bio_blkcg(struct bio *bio) { return NULL; }
360 static inline struct blkcg_gq *blkg_lookup(struct blkcg *blkcg, void *key) { return NULL; }
361 static inline int blkcg_init_queue(struct request_queue *q) { return 0; }
362 static inline void blkcg_drain_queue(struct request_queue *q) { }
363 static inline void blkcg_exit_queue(struct request_queue *q) { }
364 static inline int blkcg_policy_register(struct blkcg_policy *pol) { return 0; }
365 static inline void blkcg_policy_unregister(struct blkcg_policy *pol) { }
366 static inline int blkcg_activate_policy(struct request_queue *q,
367 const struct blkcg_policy *pol) { return 0; }
368 static inline void blkcg_deactivate_policy(struct request_queue *q,
369 const struct blkcg_policy *pol) { }
371 static inline struct blkg_policy_data *blkg_to_pd(struct blkcg_gq *blkg,
372 struct blkcg_policy *pol) { return NULL; }
373 static inline struct blkcg_gq *pd_to_blkg(struct blkg_policy_data *pd) { return NULL; }
374 static inline char *blkg_path(struct blkcg_gq *blkg) { return NULL; }
375 static inline void blkg_get(struct blkcg_gq *blkg) { }
376 static inline void blkg_put(struct blkcg_gq *blkg) { }
378 #endif /* CONFIG_BLK_CGROUP */
379 #endif /* _BLK_CGROUP_H */