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 /* CFQ specific, out here for blkcg->cfq_weight */
31 #define CFQ_WEIGHT_MIN 10
32 #define CFQ_WEIGHT_MAX 1000
33 #define CFQ_WEIGHT_DEFAULT 500
35 #ifdef CONFIG_BLK_CGROUP
37 enum blkg_rwstat_type
{
44 BLKG_RWSTAT_TOTAL
= BLKG_RWSTAT_NR
,
48 struct cgroup_subsys_state css
;
50 struct hlist_head blkg_list
;
52 /* for policies to test whether associated blkcg has changed */
55 /* TODO: per-policy storage in blkio_cgroup */
56 unsigned int cfq_weight
; /* belongs to cfq */
60 struct u64_stats_sync syncp
;
65 struct u64_stats_sync syncp
;
66 uint64_t cnt
[BLKG_RWSTAT_NR
];
69 /* per-blkg per-policy data */
70 struct blkg_policy_data
{
71 /* the blkg this per-policy data belongs to */
72 struct blkio_group
*blkg
;
74 /* pol->pdata_size bytes of private data used by policy impl */
75 char pdata
[] __aligned(__alignof__(unsigned long long));
79 /* Pointer to the associated request_queue */
80 struct request_queue
*q
;
81 struct list_head q_node
;
82 struct hlist_node blkcg_node
;
83 struct blkio_cgroup
*blkcg
;
84 /* Store cgroup path */
89 struct blkg_policy_data
*pd
[BLKIO_NR_POLICIES
];
91 struct rcu_head rcu_head
;
94 typedef void (blkio_init_group_fn
)(struct blkio_group
*blkg
);
95 typedef void (blkio_exit_group_fn
)(struct blkio_group
*blkg
);
96 typedef void (blkio_reset_group_stats_fn
)(struct blkio_group
*blkg
);
98 struct blkio_policy_ops
{
99 blkio_init_group_fn
*blkio_init_group_fn
;
100 blkio_exit_group_fn
*blkio_exit_group_fn
;
101 blkio_reset_group_stats_fn
*blkio_reset_group_stats_fn
;
104 struct blkio_policy_type
{
105 struct blkio_policy_ops ops
;
106 enum blkio_policy_id plid
;
107 size_t pdata_size
; /* policy specific private data size */
108 struct cftype
*cftypes
; /* cgroup files for the policy */
111 extern int blkcg_init_queue(struct request_queue
*q
);
112 extern void blkcg_drain_queue(struct request_queue
*q
);
113 extern void blkcg_exit_queue(struct request_queue
*q
);
115 /* Blkio controller policy registration */
116 extern void blkio_policy_register(struct blkio_policy_type
*);
117 extern void blkio_policy_unregister(struct blkio_policy_type
*);
118 extern void blkg_destroy_all(struct request_queue
*q
, bool destroy_root
);
119 extern void update_root_blkg_pd(struct request_queue
*q
,
120 enum blkio_policy_id plid
);
122 void blkcg_print_blkgs(struct seq_file
*sf
, struct blkio_cgroup
*blkcg
,
123 u64 (*prfill
)(struct seq_file
*, void *, int),
124 int pol
, int data
, bool show_total
);
125 u64
__blkg_prfill_u64(struct seq_file
*sf
, void *pdata
, u64 v
);
126 u64
__blkg_prfill_rwstat(struct seq_file
*sf
, void *pdata
,
127 const struct blkg_rwstat
*rwstat
);
128 u64
blkg_prfill_stat(struct seq_file
*sf
, void *pdata
, int off
);
129 u64
blkg_prfill_rwstat(struct seq_file
*sf
, void *pdata
, int off
);
131 struct blkg_conf_ctx
{
132 struct gendisk
*disk
;
133 struct blkio_group
*blkg
;
137 int blkg_conf_prep(struct blkio_cgroup
*blkcg
, const char *input
,
138 struct blkg_conf_ctx
*ctx
);
139 void blkg_conf_finish(struct blkg_conf_ctx
*ctx
);
143 * blkg_to_pdata - get policy private data
144 * @blkg: blkg of interest
145 * @pol: policy of interest
147 * Return pointer to private data associated with the @blkg-@pol pair.
149 static inline void *blkg_to_pdata(struct blkio_group
*blkg
,
150 struct blkio_policy_type
*pol
)
152 return blkg
? blkg
->pd
[pol
->plid
]->pdata
: NULL
;
156 * pdata_to_blkg - get blkg associated with policy private data
157 * @pdata: policy private data of interest
159 * @pdata is policy private data. Determine the blkg it's associated with.
161 static inline struct blkio_group
*pdata_to_blkg(void *pdata
)
164 struct blkg_policy_data
*pd
=
165 container_of(pdata
, struct blkg_policy_data
, pdata
);
171 static inline char *blkg_path(struct blkio_group
*blkg
)
177 * blkg_get - get a blkg reference
180 * The caller should be holding queue_lock and an existing reference.
182 static inline void blkg_get(struct blkio_group
*blkg
)
184 lockdep_assert_held(blkg
->q
->queue_lock
);
185 WARN_ON_ONCE(!blkg
->refcnt
);
189 void __blkg_release(struct blkio_group
*blkg
);
192 * blkg_put - put a blkg reference
195 * The caller should be holding queue_lock.
197 static inline void blkg_put(struct blkio_group
*blkg
)
199 lockdep_assert_held(blkg
->q
->queue_lock
);
200 WARN_ON_ONCE(blkg
->refcnt
<= 0);
202 __blkg_release(blkg
);
206 * blkg_stat_add - add a value to a blkg_stat
207 * @stat: target blkg_stat
210 * Add @val to @stat. The caller is responsible for synchronizing calls to
213 static inline void blkg_stat_add(struct blkg_stat
*stat
, uint64_t val
)
215 u64_stats_update_begin(&stat
->syncp
);
217 u64_stats_update_end(&stat
->syncp
);
221 * blkg_stat_read - read the current value of a blkg_stat
222 * @stat: blkg_stat to read
224 * Read the current value of @stat. This function can be called without
225 * synchroniztion and takes care of u64 atomicity.
227 static inline uint64_t blkg_stat_read(struct blkg_stat
*stat
)
233 start
= u64_stats_fetch_begin(&stat
->syncp
);
235 } while (u64_stats_fetch_retry(&stat
->syncp
, start
));
241 * blkg_stat_reset - reset a blkg_stat
242 * @stat: blkg_stat to reset
244 static inline void blkg_stat_reset(struct blkg_stat
*stat
)
250 * blkg_rwstat_add - add a value to a blkg_rwstat
251 * @rwstat: target blkg_rwstat
252 * @rw: mask of REQ_{WRITE|SYNC}
255 * Add @val to @rwstat. The counters are chosen according to @rw. The
256 * caller is responsible for synchronizing calls to this function.
258 static inline void blkg_rwstat_add(struct blkg_rwstat
*rwstat
,
259 int rw
, uint64_t val
)
261 u64_stats_update_begin(&rwstat
->syncp
);
264 rwstat
->cnt
[BLKG_RWSTAT_WRITE
] += val
;
266 rwstat
->cnt
[BLKG_RWSTAT_READ
] += val
;
268 rwstat
->cnt
[BLKG_RWSTAT_SYNC
] += val
;
270 rwstat
->cnt
[BLKG_RWSTAT_ASYNC
] += val
;
272 u64_stats_update_end(&rwstat
->syncp
);
276 * blkg_rwstat_read - read the current values of a blkg_rwstat
277 * @rwstat: blkg_rwstat to read
279 * Read the current snapshot of @rwstat and return it as the return value.
280 * This function can be called without synchronization and takes care of
283 static struct blkg_rwstat
blkg_rwstat_read(struct blkg_rwstat
*rwstat
)
286 struct blkg_rwstat tmp
;
289 start
= u64_stats_fetch_begin(&rwstat
->syncp
);
291 } while (u64_stats_fetch_retry(&rwstat
->syncp
, start
));
297 * blkg_rwstat_sum - read the total count of a blkg_rwstat
298 * @rwstat: blkg_rwstat to read
300 * Return the total count of @rwstat regardless of the IO direction. This
301 * function can be called without synchronization and takes care of u64
304 static inline uint64_t blkg_rwstat_sum(struct blkg_rwstat
*rwstat
)
306 struct blkg_rwstat tmp
= blkg_rwstat_read(rwstat
);
308 return tmp
.cnt
[BLKG_RWSTAT_READ
] + tmp
.cnt
[BLKG_RWSTAT_WRITE
];
312 * blkg_rwstat_reset - reset a blkg_rwstat
313 * @rwstat: blkg_rwstat to reset
315 static inline void blkg_rwstat_reset(struct blkg_rwstat
*rwstat
)
317 memset(rwstat
->cnt
, 0, sizeof(rwstat
->cnt
));
325 struct blkio_policy_type
{
328 static inline int blkcg_init_queue(struct request_queue
*q
) { return 0; }
329 static inline void blkcg_drain_queue(struct request_queue
*q
) { }
330 static inline void blkcg_exit_queue(struct request_queue
*q
) { }
331 static inline void blkio_policy_register(struct blkio_policy_type
*blkiop
) { }
332 static inline void blkio_policy_unregister(struct blkio_policy_type
*blkiop
) { }
333 static inline void blkg_destroy_all(struct request_queue
*q
,
334 bool destory_root
) { }
335 static inline void update_root_blkg_pd(struct request_queue
*q
,
336 enum blkio_policy_id plid
) { }
338 static inline void *blkg_to_pdata(struct blkio_group
*blkg
,
339 struct blkio_policy_type
*pol
) { return NULL
; }
340 static inline struct blkio_group
*pdata_to_blkg(void *pdata
,
341 struct blkio_policy_type
*pol
) { return NULL
; }
342 static inline char *blkg_path(struct blkio_group
*blkg
) { return NULL
; }
343 static inline void blkg_get(struct blkio_group
*blkg
) { }
344 static inline void blkg_put(struct blkio_group
*blkg
) { }
348 #ifdef CONFIG_BLK_CGROUP
349 extern struct blkio_cgroup blkio_root_cgroup
;
350 extern struct blkio_cgroup
*cgroup_to_blkio_cgroup(struct cgroup
*cgroup
);
351 extern struct blkio_cgroup
*bio_blkio_cgroup(struct bio
*bio
);
352 extern struct blkio_group
*blkg_lookup(struct blkio_cgroup
*blkcg
,
353 struct request_queue
*q
);
354 struct blkio_group
*blkg_lookup_create(struct blkio_cgroup
*blkcg
,
355 struct request_queue
*q
,
359 static inline struct blkio_cgroup
*
360 cgroup_to_blkio_cgroup(struct cgroup
*cgroup
) { return NULL
; }
361 static inline struct blkio_cgroup
*
362 bio_blkio_cgroup(struct bio
*bio
) { return NULL
; }
364 static inline struct blkio_group
*blkg_lookup(struct blkio_cgroup
*blkcg
,
365 void *key
) { return NULL
; }
367 #endif /* _BLK_CGROUP_H */