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
,
47 /* blkg state flags */
48 enum blkg_state_flags
{
55 struct cgroup_subsys_state css
;
58 struct hlist_head blkg_list
;
60 /* for policies to test whether associated blkcg has changed */
65 struct u64_stats_sync syncp
;
70 struct u64_stats_sync syncp
;
71 uint64_t cnt
[BLKG_RWSTAT_NR
];
74 struct blkio_group_stats
{
75 /* number of ios merged */
76 struct blkg_rwstat merged
;
77 /* total time spent on device in ns, may not be accurate w/ queueing */
78 struct blkg_rwstat service_time
;
79 /* total time spent waiting in scheduler queue in ns */
80 struct blkg_rwstat wait_time
;
81 /* number of IOs queued up */
82 struct blkg_rwstat queued
;
83 /* total disk time and nr sectors dispatched by this group */
84 struct blkg_stat time
;
85 #ifdef CONFIG_DEBUG_BLK_CGROUP
86 /* time not charged to this cgroup */
87 struct blkg_stat unaccounted_time
;
88 /* sum of number of ios queued across all samples */
89 struct blkg_stat avg_queue_size_sum
;
90 /* count of samples taken for average */
91 struct blkg_stat avg_queue_size_samples
;
92 /* how many times this group has been removed from service tree */
93 struct blkg_stat dequeue
;
94 /* total time spent waiting for it to be assigned a timeslice. */
95 struct blkg_stat group_wait_time
;
96 /* time spent idling for this blkio_group */
97 struct blkg_stat idle_time
;
98 /* total time with empty current active q with other requests queued */
99 struct blkg_stat empty_time
;
100 /* fields after this shouldn't be cleared on stat reset */
101 uint64_t start_group_wait_time
;
102 uint64_t start_idle_time
;
103 uint64_t start_empty_time
;
108 /* Per cpu blkio group stats */
109 struct blkio_group_stats_cpu
{
110 /* total bytes transferred */
111 struct blkg_rwstat service_bytes
;
112 /* total IOs serviced, post merge */
113 struct blkg_rwstat serviced
;
114 /* total sectors transferred */
115 struct blkg_stat sectors
;
118 struct blkio_group_conf
{
124 /* per-blkg per-policy data */
125 struct blkg_policy_data
{
126 /* the blkg this per-policy data belongs to */
127 struct blkio_group
*blkg
;
130 struct blkio_group_conf conf
;
132 struct blkio_group_stats stats
;
133 /* Per cpu stats pointer */
134 struct blkio_group_stats_cpu __percpu
*stats_cpu
;
136 /* pol->pdata_size bytes of private data used by policy impl */
137 char pdata
[] __aligned(__alignof__(unsigned long long));
141 /* Pointer to the associated request_queue */
142 struct request_queue
*q
;
143 struct list_head q_node
;
144 struct hlist_node blkcg_node
;
145 struct blkio_cgroup
*blkcg
;
146 /* Store cgroup path */
148 /* reference count */
151 struct blkg_policy_data
*pd
[BLKIO_NR_POLICIES
];
153 /* List of blkg waiting for per cpu stats memory to be allocated */
154 struct list_head alloc_node
;
155 struct rcu_head rcu_head
;
158 typedef void (blkio_init_group_fn
)(struct blkio_group
*blkg
);
160 struct blkio_policy_ops
{
161 blkio_init_group_fn
*blkio_init_group_fn
;
164 struct blkio_policy_type
{
165 struct list_head list
;
166 struct blkio_policy_ops ops
;
167 enum blkio_policy_id plid
;
168 size_t pdata_size
; /* policy specific private data size */
169 struct cftype
*cftypes
; /* cgroup files for the policy */
172 extern int blkcg_init_queue(struct request_queue
*q
);
173 extern void blkcg_drain_queue(struct request_queue
*q
);
174 extern void blkcg_exit_queue(struct request_queue
*q
);
176 /* Blkio controller policy registration */
177 extern void blkio_policy_register(struct blkio_policy_type
*);
178 extern void blkio_policy_unregister(struct blkio_policy_type
*);
179 extern void blkg_destroy_all(struct request_queue
*q
, bool destroy_root
);
180 extern void update_root_blkg_pd(struct request_queue
*q
,
181 enum blkio_policy_id plid
);
183 void blkcg_print_blkgs(struct seq_file
*sf
, struct blkio_cgroup
*blkcg
,
184 u64 (*prfill
)(struct seq_file
*, struct blkg_policy_data
*, int),
185 int pol
, int data
, bool show_total
);
186 u64
__blkg_prfill_u64(struct seq_file
*sf
, struct blkg_policy_data
*pd
, u64 v
);
187 u64
__blkg_prfill_rwstat(struct seq_file
*sf
, struct blkg_policy_data
*pd
,
188 const struct blkg_rwstat
*rwstat
);
189 int blkcg_print_stat(struct cgroup
*cgrp
, struct cftype
*cft
,
190 struct seq_file
*sf
);
191 int blkcg_print_rwstat(struct cgroup
*cgrp
, struct cftype
*cft
,
192 struct seq_file
*sf
);
193 int blkcg_print_cpu_stat(struct cgroup
*cgrp
, struct cftype
*cft
,
194 struct seq_file
*sf
);
195 int blkcg_print_cpu_rwstat(struct cgroup
*cgrp
, struct cftype
*cft
,
196 struct seq_file
*sf
);
198 struct blkg_conf_ctx
{
199 struct gendisk
*disk
;
200 struct blkio_group
*blkg
;
204 int blkg_conf_prep(struct blkio_cgroup
*blkcg
, const char *input
,
205 struct blkg_conf_ctx
*ctx
);
206 void blkg_conf_finish(struct blkg_conf_ctx
*ctx
);
210 * blkg_to_pdata - get policy private data
211 * @blkg: blkg of interest
212 * @pol: policy of interest
214 * Return pointer to private data associated with the @blkg-@pol pair.
216 static inline void *blkg_to_pdata(struct blkio_group
*blkg
,
217 struct blkio_policy_type
*pol
)
219 return blkg
? blkg
->pd
[pol
->plid
]->pdata
: NULL
;
223 * pdata_to_blkg - get blkg associated with policy private data
224 * @pdata: policy private data of interest
226 * @pdata is policy private data. Determine the blkg it's associated with.
228 static inline struct blkio_group
*pdata_to_blkg(void *pdata
)
231 struct blkg_policy_data
*pd
=
232 container_of(pdata
, struct blkg_policy_data
, pdata
);
238 static inline char *blkg_path(struct blkio_group
*blkg
)
244 * blkg_get - get a blkg reference
247 * The caller should be holding queue_lock and an existing reference.
249 static inline void blkg_get(struct blkio_group
*blkg
)
251 lockdep_assert_held(blkg
->q
->queue_lock
);
252 WARN_ON_ONCE(!blkg
->refcnt
);
256 void __blkg_release(struct blkio_group
*blkg
);
259 * blkg_put - put a blkg reference
262 * The caller should be holding queue_lock.
264 static inline void blkg_put(struct blkio_group
*blkg
)
266 lockdep_assert_held(blkg
->q
->queue_lock
);
267 WARN_ON_ONCE(blkg
->refcnt
<= 0);
269 __blkg_release(blkg
);
273 * blkg_stat_add - add a value to a blkg_stat
274 * @stat: target blkg_stat
277 * Add @val to @stat. The caller is responsible for synchronizing calls to
280 static inline void blkg_stat_add(struct blkg_stat
*stat
, uint64_t val
)
282 u64_stats_update_begin(&stat
->syncp
);
284 u64_stats_update_end(&stat
->syncp
);
288 * blkg_stat_read - read the current value of a blkg_stat
289 * @stat: blkg_stat to read
291 * Read the current value of @stat. This function can be called without
292 * synchroniztion and takes care of u64 atomicity.
294 static inline uint64_t blkg_stat_read(struct blkg_stat
*stat
)
300 start
= u64_stats_fetch_begin(&stat
->syncp
);
302 } while (u64_stats_fetch_retry(&stat
->syncp
, start
));
308 * blkg_stat_reset - reset a blkg_stat
309 * @stat: blkg_stat to reset
311 static inline void blkg_stat_reset(struct blkg_stat
*stat
)
317 * blkg_rwstat_add - add a value to a blkg_rwstat
318 * @rwstat: target blkg_rwstat
319 * @rw: mask of REQ_{WRITE|SYNC}
322 * Add @val to @rwstat. The counters are chosen according to @rw. The
323 * caller is responsible for synchronizing calls to this function.
325 static inline void blkg_rwstat_add(struct blkg_rwstat
*rwstat
,
326 int rw
, uint64_t val
)
328 u64_stats_update_begin(&rwstat
->syncp
);
331 rwstat
->cnt
[BLKG_RWSTAT_WRITE
] += val
;
333 rwstat
->cnt
[BLKG_RWSTAT_READ
] += val
;
335 rwstat
->cnt
[BLKG_RWSTAT_SYNC
] += val
;
337 rwstat
->cnt
[BLKG_RWSTAT_ASYNC
] += val
;
339 u64_stats_update_end(&rwstat
->syncp
);
343 * blkg_rwstat_read - read the current values of a blkg_rwstat
344 * @rwstat: blkg_rwstat to read
346 * Read the current snapshot of @rwstat and return it as the return value.
347 * This function can be called without synchronization and takes care of
350 static struct blkg_rwstat
blkg_rwstat_read(struct blkg_rwstat
*rwstat
)
353 struct blkg_rwstat tmp
;
356 start
= u64_stats_fetch_begin(&rwstat
->syncp
);
358 } while (u64_stats_fetch_retry(&rwstat
->syncp
, start
));
364 * blkg_rwstat_sum - read the total count of a blkg_rwstat
365 * @rwstat: blkg_rwstat to read
367 * Return the total count of @rwstat regardless of the IO direction. This
368 * function can be called without synchronization and takes care of u64
371 static inline uint64_t blkg_rwstat_sum(struct blkg_rwstat
*rwstat
)
373 struct blkg_rwstat tmp
= blkg_rwstat_read(rwstat
);
375 return tmp
.cnt
[BLKG_RWSTAT_READ
] + tmp
.cnt
[BLKG_RWSTAT_WRITE
];
379 * blkg_rwstat_reset - reset a blkg_rwstat
380 * @rwstat: blkg_rwstat to reset
382 static inline void blkg_rwstat_reset(struct blkg_rwstat
*rwstat
)
384 memset(rwstat
->cnt
, 0, sizeof(rwstat
->cnt
));
392 struct blkio_policy_type
{
395 static inline int blkcg_init_queue(struct request_queue
*q
) { return 0; }
396 static inline void blkcg_drain_queue(struct request_queue
*q
) { }
397 static inline void blkcg_exit_queue(struct request_queue
*q
) { }
398 static inline void blkio_policy_register(struct blkio_policy_type
*blkiop
) { }
399 static inline void blkio_policy_unregister(struct blkio_policy_type
*blkiop
) { }
400 static inline void blkg_destroy_all(struct request_queue
*q
,
401 bool destory_root
) { }
402 static inline void update_root_blkg_pd(struct request_queue
*q
,
403 enum blkio_policy_id plid
) { }
405 static inline void *blkg_to_pdata(struct blkio_group
*blkg
,
406 struct blkio_policy_type
*pol
) { return NULL
; }
407 static inline struct blkio_group
*pdata_to_blkg(void *pdata
,
408 struct blkio_policy_type
*pol
) { return NULL
; }
409 static inline char *blkg_path(struct blkio_group
*blkg
) { return NULL
; }
410 static inline void blkg_get(struct blkio_group
*blkg
) { }
411 static inline void blkg_put(struct blkio_group
*blkg
) { }
415 #define BLKIO_WEIGHT_MIN 10
416 #define BLKIO_WEIGHT_MAX 1000
417 #define BLKIO_WEIGHT_DEFAULT 500
419 #ifdef CONFIG_DEBUG_BLK_CGROUP
420 void blkiocg_update_avg_queue_size_stats(struct blkio_group
*blkg
,
421 struct blkio_policy_type
*pol
);
422 void blkiocg_update_dequeue_stats(struct blkio_group
*blkg
,
423 struct blkio_policy_type
*pol
,
424 unsigned long dequeue
);
425 void blkiocg_update_set_idle_time_stats(struct blkio_group
*blkg
,
426 struct blkio_policy_type
*pol
);
427 void blkiocg_update_idle_time_stats(struct blkio_group
*blkg
,
428 struct blkio_policy_type
*pol
);
429 void blkiocg_set_start_empty_time(struct blkio_group
*blkg
,
430 struct blkio_policy_type
*pol
);
432 #define BLKG_FLAG_FNS(name) \
433 static inline void blkio_mark_blkg_##name( \
434 struct blkio_group_stats *stats) \
436 stats->flags |= (1 << BLKG_##name); \
438 static inline void blkio_clear_blkg_##name( \
439 struct blkio_group_stats *stats) \
441 stats->flags &= ~(1 << BLKG_##name); \
443 static inline int blkio_blkg_##name(struct blkio_group_stats *stats) \
445 return (stats->flags & (1 << BLKG_##name)) != 0; \
448 BLKG_FLAG_FNS(waiting)
449 BLKG_FLAG_FNS(idling
)
453 static inline void blkiocg_update_avg_queue_size_stats(struct blkio_group
*blkg
,
454 struct blkio_policy_type
*pol
) { }
455 static inline void blkiocg_update_dequeue_stats(struct blkio_group
*blkg
,
456 struct blkio_policy_type
*pol
, unsigned long dequeue
) { }
457 static inline void blkiocg_update_set_idle_time_stats(struct blkio_group
*blkg
,
458 struct blkio_policy_type
*pol
) { }
459 static inline void blkiocg_update_idle_time_stats(struct blkio_group
*blkg
,
460 struct blkio_policy_type
*pol
) { }
461 static inline void blkiocg_set_start_empty_time(struct blkio_group
*blkg
,
462 struct blkio_policy_type
*pol
) { }
465 #ifdef CONFIG_BLK_CGROUP
466 extern struct blkio_cgroup blkio_root_cgroup
;
467 extern struct blkio_cgroup
*cgroup_to_blkio_cgroup(struct cgroup
*cgroup
);
468 extern struct blkio_cgroup
*bio_blkio_cgroup(struct bio
*bio
);
469 extern struct blkio_group
*blkg_lookup(struct blkio_cgroup
*blkcg
,
470 struct request_queue
*q
);
471 struct blkio_group
*blkg_lookup_create(struct blkio_cgroup
*blkcg
,
472 struct request_queue
*q
,
474 void blkiocg_update_timeslice_used(struct blkio_group
*blkg
,
475 struct blkio_policy_type
*pol
,
477 unsigned long unaccounted_time
);
478 void blkiocg_update_dispatch_stats(struct blkio_group
*blkg
,
479 struct blkio_policy_type
*pol
,
480 uint64_t bytes
, bool direction
, bool sync
);
481 void blkiocg_update_completion_stats(struct blkio_group
*blkg
,
482 struct blkio_policy_type
*pol
,
484 uint64_t io_start_time
, bool direction
,
486 void blkiocg_update_io_merged_stats(struct blkio_group
*blkg
,
487 struct blkio_policy_type
*pol
,
488 bool direction
, bool sync
);
489 void blkiocg_update_io_add_stats(struct blkio_group
*blkg
,
490 struct blkio_policy_type
*pol
,
491 struct blkio_group
*curr_blkg
, bool direction
,
493 void blkiocg_update_io_remove_stats(struct blkio_group
*blkg
,
494 struct blkio_policy_type
*pol
,
495 bool direction
, bool sync
);
498 static inline struct blkio_cgroup
*
499 cgroup_to_blkio_cgroup(struct cgroup
*cgroup
) { return NULL
; }
500 static inline struct blkio_cgroup
*
501 bio_blkio_cgroup(struct bio
*bio
) { return NULL
; }
503 static inline struct blkio_group
*blkg_lookup(struct blkio_cgroup
*blkcg
,
504 void *key
) { return NULL
; }
505 static inline void blkiocg_update_timeslice_used(struct blkio_group
*blkg
,
506 struct blkio_policy_type
*pol
, unsigned long time
,
507 unsigned long unaccounted_time
) { }
508 static inline void blkiocg_update_dispatch_stats(struct blkio_group
*blkg
,
509 struct blkio_policy_type
*pol
, uint64_t bytes
,
510 bool direction
, bool sync
) { }
511 static inline void blkiocg_update_completion_stats(struct blkio_group
*blkg
,
512 struct blkio_policy_type
*pol
, uint64_t start_time
,
513 uint64_t io_start_time
, bool direction
, bool sync
) { }
514 static inline void blkiocg_update_io_merged_stats(struct blkio_group
*blkg
,
515 struct blkio_policy_type
*pol
, bool direction
,
517 static inline void blkiocg_update_io_add_stats(struct blkio_group
*blkg
,
518 struct blkio_policy_type
*pol
,
519 struct blkio_group
*curr_blkg
, bool direction
,
521 static inline void blkiocg_update_io_remove_stats(struct blkio_group
*blkg
,
522 struct blkio_policy_type
*pol
, bool direction
,
525 #endif /* _BLK_CGROUP_H */