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
32 /* Number of IOs merged */
34 /* Total time spent (in ns) between request dispatch to the driver and
35 * request completion for IOs doen by this cgroup. This may not be
36 * accurate when NCQ is turned on. */
37 BLKIO_STAT_SERVICE_TIME,
38 /* Total time spent waiting in scheduler queue in ns */
40 /* Number of IOs queued up */
43 /* All the single valued stats go below this */
45 #ifdef CONFIG_DEBUG_BLK_CGROUP
46 /* Time not charged to this cgroup */
47 BLKIO_STAT_UNACCOUNTED_TIME,
48 BLKIO_STAT_AVG_QUEUE_SIZE,
50 BLKIO_STAT_EMPTY_TIME,
51 BLKIO_STAT_GROUP_WAIT_TIME,
56 /* Types lower than this live in stat_arr and have subtypes */
57 #define BLKIO_STAT_ARR_NR (BLKIO_STAT_QUEUED + 1)
61 BLKIO_STAT_CPU_SECTORS,
62 /* Total bytes transferred */
63 BLKIO_STAT_CPU_SERVICE_BYTES,
64 /* Total IOs serviced, post merge */
65 BLKIO_STAT_CPU_SERVICED,
77 /* blkg state flags */
78 enum blkg_state_flags {
84 /* cgroup files owned by proportional weight policy */
85 enum blkcg_file_name_prop {
86 BLKIO_PROP_weight = 1,
87 BLKIO_PROP_weight_device,
88 BLKIO_PROP_io_service_bytes,
89 BLKIO_PROP_io_serviced,
92 BLKIO_PROP_unaccounted_time,
93 BLKIO_PROP_io_service_time,
94 BLKIO_PROP_io_wait_time,
97 BLKIO_PROP_avg_queue_size,
98 BLKIO_PROP_group_wait_time,
100 BLKIO_PROP_empty_time,
104 /* cgroup files owned by throttle policy */
105 enum blkcg_file_name_throtl {
106 BLKIO_THROTL_read_bps_device,
107 BLKIO_THROTL_write_bps_device,
108 BLKIO_THROTL_read_iops_device,
109 BLKIO_THROTL_write_iops_device,
110 BLKIO_THROTL_io_service_bytes,
111 BLKIO_THROTL_io_serviced,
114 struct blkio_cgroup {
115 struct cgroup_subsys_state css;
118 struct hlist_head blkg_list;
120 /* for policies to test whether associated blkcg has changed */
124 struct blkio_group_stats {
125 struct u64_stats_sync syncp;
126 /* total disk time and nr sectors dispatched by this group */
128 uint64_t stat_arr[BLKIO_STAT_ARR_NR][BLKIO_STAT_TOTAL];
129 #ifdef CONFIG_DEBUG_BLK_CGROUP
130 /* Time not charged to this cgroup */
131 uint64_t unaccounted_time;
133 /* Sum of number of IOs queued across all samples */
134 uint64_t avg_queue_size_sum;
135 /* Count of samples taken for average */
136 uint64_t avg_queue_size_samples;
137 /* How many times this group has been removed from service tree */
138 unsigned long dequeue;
140 /* Total time spent waiting for it to be assigned a timeslice. */
141 uint64_t group_wait_time;
143 /* Time spent idling for this blkio_group */
146 * Total time when we have requests queued and do not contain the
147 * current active queue.
151 /* fields after this shouldn't be cleared on stat reset */
152 uint64_t start_group_wait_time;
153 uint64_t start_idle_time;
154 uint64_t start_empty_time;
159 #ifdef CONFIG_DEBUG_BLK_CGROUP
160 #define BLKG_STATS_DEBUG_CLEAR_START \
161 offsetof(struct blkio_group_stats, unaccounted_time)
162 #define BLKG_STATS_DEBUG_CLEAR_SIZE \
163 (offsetof(struct blkio_group_stats, start_group_wait_time) - \
164 BLKG_STATS_DEBUG_CLEAR_START)
167 /* Per cpu blkio group stats */
168 struct blkio_group_stats_cpu {
170 uint64_t stat_arr_cpu[BLKIO_STAT_CPU_NR][BLKIO_STAT_TOTAL];
171 struct u64_stats_sync syncp;
174 struct blkio_group_conf {
176 unsigned int iops[2];
180 /* per-blkg per-policy data */
181 struct blkg_policy_data {
182 /* the blkg this per-policy data belongs to */
183 struct blkio_group *blkg;
186 struct blkio_group_conf conf;
188 struct blkio_group_stats stats;
189 /* Per cpu stats pointer */
190 struct blkio_group_stats_cpu __percpu *stats_cpu;
192 /* pol->pdata_size bytes of private data used by policy impl */
193 char pdata[] __aligned(__alignof__(unsigned long long));
197 /* Pointer to the associated request_queue */
198 struct request_queue *q;
199 struct list_head q_node;
200 struct hlist_node blkcg_node;
201 struct blkio_cgroup *blkcg;
202 /* Store cgroup path */
204 /* reference count */
207 struct blkg_policy_data *pd[BLKIO_NR_POLICIES];
209 /* List of blkg waiting for per cpu stats memory to be allocated */
210 struct list_head alloc_node;
211 struct rcu_head rcu_head;
214 typedef void (blkio_init_group_fn)(struct blkio_group *blkg);
215 typedef void (blkio_update_group_weight_fn)(struct request_queue *q,
216 struct blkio_group *blkg, unsigned int weight);
217 typedef void (blkio_update_group_read_bps_fn)(struct request_queue *q,
218 struct blkio_group *blkg, u64 read_bps);
219 typedef void (blkio_update_group_write_bps_fn)(struct request_queue *q,
220 struct blkio_group *blkg, u64 write_bps);
221 typedef void (blkio_update_group_read_iops_fn)(struct request_queue *q,
222 struct blkio_group *blkg, unsigned int read_iops);
223 typedef void (blkio_update_group_write_iops_fn)(struct request_queue *q,
224 struct blkio_group *blkg, unsigned int write_iops);
226 struct blkio_policy_ops {
227 blkio_init_group_fn *blkio_init_group_fn;
228 blkio_update_group_weight_fn *blkio_update_group_weight_fn;
229 blkio_update_group_read_bps_fn *blkio_update_group_read_bps_fn;
230 blkio_update_group_write_bps_fn *blkio_update_group_write_bps_fn;
231 blkio_update_group_read_iops_fn *blkio_update_group_read_iops_fn;
232 blkio_update_group_write_iops_fn *blkio_update_group_write_iops_fn;
235 struct blkio_policy_type {
236 struct list_head list;
237 struct blkio_policy_ops ops;
238 enum blkio_policy_id plid;
239 size_t pdata_size; /* policy specific private data size */
242 extern int blkcg_init_queue(struct request_queue *q);
243 extern void blkcg_drain_queue(struct request_queue *q);
244 extern void blkcg_exit_queue(struct request_queue *q);
246 /* Blkio controller policy registration */
247 extern void blkio_policy_register(struct blkio_policy_type *);
248 extern void blkio_policy_unregister(struct blkio_policy_type *);
249 extern void blkg_destroy_all(struct request_queue *q, bool destroy_root);
250 extern void update_root_blkg_pd(struct request_queue *q,
251 enum blkio_policy_id plid);
254 * blkg_to_pdata - get policy private data
255 * @blkg: blkg of interest
256 * @pol: policy of interest
258 * Return pointer to private data associated with the @blkg-@pol pair.
260 static inline void *blkg_to_pdata(struct blkio_group *blkg,
261 struct blkio_policy_type *pol)
263 return blkg ? blkg->pd[pol->plid]->pdata : NULL;
267 * pdata_to_blkg - get blkg associated with policy private data
268 * @pdata: policy private data of interest
269 * @pol: policy @pdata is for
271 * @pdata is policy private data for @pol. Determine the blkg it's
274 static inline struct blkio_group *pdata_to_blkg(void *pdata,
275 struct blkio_policy_type *pol)
278 struct blkg_policy_data *pd =
279 container_of(pdata, struct blkg_policy_data, pdata);
285 static inline char *blkg_path(struct blkio_group *blkg)
291 * blkg_get - get a blkg reference
294 * The caller should be holding queue_lock and an existing reference.
296 static inline void blkg_get(struct blkio_group *blkg)
298 lockdep_assert_held(blkg->q->queue_lock);
299 WARN_ON_ONCE(!blkg->refcnt);
303 void __blkg_release(struct blkio_group *blkg);
306 * blkg_put - put a blkg reference
309 * The caller should be holding queue_lock.
311 static inline void blkg_put(struct blkio_group *blkg)
313 lockdep_assert_held(blkg->q->queue_lock);
314 WARN_ON_ONCE(blkg->refcnt <= 0);
316 __blkg_release(blkg);
324 struct blkio_policy_type {
327 static inline int blkcg_init_queue(struct request_queue *q) { return 0; }
328 static inline void blkcg_drain_queue(struct request_queue *q) { }
329 static inline void blkcg_exit_queue(struct request_queue *q) { }
330 static inline void blkio_policy_register(struct blkio_policy_type *blkiop) { }
331 static inline void blkio_policy_unregister(struct blkio_policy_type *blkiop) { }
332 static inline void blkg_destroy_all(struct request_queue *q,
333 bool destory_root) { }
334 static inline void update_root_blkg_pd(struct request_queue *q,
335 enum blkio_policy_id plid) { }
337 static inline void *blkg_to_pdata(struct blkio_group *blkg,
338 struct blkio_policy_type *pol) { return NULL; }
339 static inline struct blkio_group *pdata_to_blkg(void *pdata,
340 struct blkio_policy_type *pol) { return NULL; }
341 static inline char *blkg_path(struct blkio_group *blkg) { return NULL; }
342 static inline void blkg_get(struct blkio_group *blkg) { }
343 static inline void blkg_put(struct blkio_group *blkg) { }
347 #define BLKIO_WEIGHT_MIN 10
348 #define BLKIO_WEIGHT_MAX 1000
349 #define BLKIO_WEIGHT_DEFAULT 500
351 #ifdef CONFIG_DEBUG_BLK_CGROUP
352 void blkiocg_update_avg_queue_size_stats(struct blkio_group *blkg,
353 struct blkio_policy_type *pol);
354 void blkiocg_update_dequeue_stats(struct blkio_group *blkg,
355 struct blkio_policy_type *pol,
356 unsigned long dequeue);
357 void blkiocg_update_set_idle_time_stats(struct blkio_group *blkg,
358 struct blkio_policy_type *pol);
359 void blkiocg_update_idle_time_stats(struct blkio_group *blkg,
360 struct blkio_policy_type *pol);
361 void blkiocg_set_start_empty_time(struct blkio_group *blkg,
362 struct blkio_policy_type *pol);
364 #define BLKG_FLAG_FNS(name) \
365 static inline void blkio_mark_blkg_##name( \
366 struct blkio_group_stats *stats) \
368 stats->flags |= (1 << BLKG_##name); \
370 static inline void blkio_clear_blkg_##name( \
371 struct blkio_group_stats *stats) \
373 stats->flags &= ~(1 << BLKG_##name); \
375 static inline int blkio_blkg_##name(struct blkio_group_stats *stats) \
377 return (stats->flags & (1 << BLKG_##name)) != 0; \
380 BLKG_FLAG_FNS(waiting)
381 BLKG_FLAG_FNS(idling)
385 static inline void blkiocg_update_avg_queue_size_stats(struct blkio_group *blkg,
386 struct blkio_policy_type *pol) { }
387 static inline void blkiocg_update_dequeue_stats(struct blkio_group *blkg,
388 struct blkio_policy_type *pol, unsigned long dequeue) { }
389 static inline void blkiocg_update_set_idle_time_stats(struct blkio_group *blkg,
390 struct blkio_policy_type *pol) { }
391 static inline void blkiocg_update_idle_time_stats(struct blkio_group *blkg,
392 struct blkio_policy_type *pol) { }
393 static inline void blkiocg_set_start_empty_time(struct blkio_group *blkg,
394 struct blkio_policy_type *pol) { }
397 #ifdef CONFIG_BLK_CGROUP
398 extern struct blkio_cgroup blkio_root_cgroup;
399 extern struct blkio_cgroup *cgroup_to_blkio_cgroup(struct cgroup *cgroup);
400 extern struct blkio_cgroup *bio_blkio_cgroup(struct bio *bio);
401 extern struct blkio_group *blkg_lookup(struct blkio_cgroup *blkcg,
402 struct request_queue *q);
403 struct blkio_group *blkg_lookup_create(struct blkio_cgroup *blkcg,
404 struct request_queue *q,
405 enum blkio_policy_id plid,
407 void blkiocg_update_timeslice_used(struct blkio_group *blkg,
408 struct blkio_policy_type *pol,
410 unsigned long unaccounted_time);
411 void blkiocg_update_dispatch_stats(struct blkio_group *blkg,
412 struct blkio_policy_type *pol,
413 uint64_t bytes, bool direction, bool sync);
414 void blkiocg_update_completion_stats(struct blkio_group *blkg,
415 struct blkio_policy_type *pol,
417 uint64_t io_start_time, bool direction,
419 void blkiocg_update_io_merged_stats(struct blkio_group *blkg,
420 struct blkio_policy_type *pol,
421 bool direction, bool sync);
422 void blkiocg_update_io_add_stats(struct blkio_group *blkg,
423 struct blkio_policy_type *pol,
424 struct blkio_group *curr_blkg, bool direction,
426 void blkiocg_update_io_remove_stats(struct blkio_group *blkg,
427 struct blkio_policy_type *pol,
428 bool direction, bool sync);
431 static inline struct blkio_cgroup *
432 cgroup_to_blkio_cgroup(struct cgroup *cgroup) { return NULL; }
433 static inline struct blkio_cgroup *
434 bio_blkio_cgroup(struct bio *bio) { return NULL; }
436 static inline struct blkio_group *blkg_lookup(struct blkio_cgroup *blkcg,
437 void *key) { return NULL; }
438 static inline void blkiocg_update_timeslice_used(struct blkio_group *blkg,
439 struct blkio_policy_type *pol, unsigned long time,
440 unsigned long unaccounted_time) { }
441 static inline void blkiocg_update_dispatch_stats(struct blkio_group *blkg,
442 struct blkio_policy_type *pol, uint64_t bytes,
443 bool direction, bool sync) { }
444 static inline void blkiocg_update_completion_stats(struct blkio_group *blkg,
445 struct blkio_policy_type *pol, uint64_t start_time,
446 uint64_t io_start_time, bool direction, bool sync) { }
447 static inline void blkiocg_update_io_merged_stats(struct blkio_group *blkg,
448 struct blkio_policy_type *pol, bool direction,
450 static inline void blkiocg_update_io_add_stats(struct blkio_group *blkg,
451 struct blkio_policy_type *pol,
452 struct blkio_group *curr_blkg, bool direction,
454 static inline void blkiocg_update_io_remove_stats(struct blkio_group *blkg,
455 struct blkio_policy_type *pol, bool direction,
458 #endif /* _BLK_CGROUP_H */