Merge branch 'for-3.5' of ../cgroup into block/for-3.5/core-merged
[deliverable/linux.git] / block / blk-cgroup.h
CommitLineData
31e4c28d
VG
1#ifndef _BLK_CGROUP_H
2#define _BLK_CGROUP_H
3/*
4 * Common Block IO controller cgroup interface
5 *
6 * Based on ideas and code from CFQ, CFS and BFQ:
7 * Copyright (C) 2003 Jens Axboe <axboe@kernel.dk>
8 *
9 * Copyright (C) 2008 Fabio Checconi <fabio@gandalf.sssup.it>
10 * Paolo Valente <paolo.valente@unimore.it>
11 *
12 * Copyright (C) 2009 Vivek Goyal <vgoyal@redhat.com>
13 * Nauman Rafique <nauman@google.com>
14 */
15
16#include <linux/cgroup.h>
575969a0 17#include <linux/u64_stats_sync.h>
31e4c28d 18
062a644d
VG
19enum blkio_policy_id {
20 BLKIO_POLICY_PROP = 0, /* Proportional Bandwidth division */
4c9eefa1 21 BLKIO_POLICY_THROTL, /* Throttling */
035d10b2
TH
22
23 BLKIO_NR_POLICIES,
062a644d
VG
24};
25
9355aede
VG
26/* Max limits for throttle policy */
27#define THROTL_IOPS_MAX UINT_MAX
28
32e380ae 29#ifdef CONFIG_BLK_CGROUP
2f5ea477 30
84c124da 31enum stat_type {
5fe224d2
TH
32 /* Number of IOs merged */
33 BLKIO_STAT_MERGED,
84c124da
DS
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. */
5fe224d2 37 BLKIO_STAT_SERVICE_TIME,
84c124da
DS
38 /* Total time spent waiting in scheduler queue in ns */
39 BLKIO_STAT_WAIT_TIME,
cdc1184c
DS
40 /* Number of IOs queued up */
41 BLKIO_STAT_QUEUED,
c4c76a05 42
84c124da
DS
43 /* All the single valued stats go below this */
44 BLKIO_STAT_TIME,
a23e6869 45#ifdef CONFIG_DEBUG_BLK_CGROUP
167400d3
JT
46 /* Time not charged to this cgroup */
47 BLKIO_STAT_UNACCOUNTED_TIME,
cdc1184c 48 BLKIO_STAT_AVG_QUEUE_SIZE,
812df48d
DS
49 BLKIO_STAT_IDLE_TIME,
50 BLKIO_STAT_EMPTY_TIME,
51 BLKIO_STAT_GROUP_WAIT_TIME,
84c124da
DS
52 BLKIO_STAT_DEQUEUE
53#endif
54};
55
c4c76a05
TH
56/* Types lower than this live in stat_arr and have subtypes */
57#define BLKIO_STAT_ARR_NR (BLKIO_STAT_QUEUED + 1)
58
5624a4e4
VG
59/* Per cpu stats */
60enum stat_type_cpu {
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,
66 BLKIO_STAT_CPU_NR
67};
68
84c124da
DS
69enum stat_sub_type {
70 BLKIO_STAT_READ = 0,
71 BLKIO_STAT_WRITE,
72 BLKIO_STAT_SYNC,
73 BLKIO_STAT_ASYNC,
74 BLKIO_STAT_TOTAL
303a3acb
DS
75};
76
812df48d
DS
77/* blkg state flags */
78enum blkg_state_flags {
79 BLKG_waiting = 0,
80 BLKG_idling,
81 BLKG_empty,
82};
83
062a644d
VG
84/* cgroup files owned by proportional weight policy */
85enum 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,
90 BLKIO_PROP_time,
91 BLKIO_PROP_sectors,
167400d3 92 BLKIO_PROP_unaccounted_time,
062a644d
VG
93 BLKIO_PROP_io_service_time,
94 BLKIO_PROP_io_wait_time,
95 BLKIO_PROP_io_merged,
96 BLKIO_PROP_io_queued,
97 BLKIO_PROP_avg_queue_size,
98 BLKIO_PROP_group_wait_time,
99 BLKIO_PROP_idle_time,
100 BLKIO_PROP_empty_time,
101 BLKIO_PROP_dequeue,
102};
103
4c9eefa1
VG
104/* cgroup files owned by throttle policy */
105enum blkcg_file_name_throtl {
106 BLKIO_THROTL_read_bps_device,
107 BLKIO_THROTL_write_bps_device,
7702e8f4
VG
108 BLKIO_THROTL_read_iops_device,
109 BLKIO_THROTL_write_iops_device,
4c9eefa1
VG
110 BLKIO_THROTL_io_service_bytes,
111 BLKIO_THROTL_io_serviced,
112};
113
31e4c28d
VG
114struct blkio_cgroup {
115 struct cgroup_subsys_state css;
116 unsigned int weight;
117 spinlock_t lock;
118 struct hlist_head blkg_list;
9a9e8a26
TH
119
120 /* for policies to test whether associated blkcg has changed */
121 uint64_t id;
31e4c28d
VG
122};
123
303a3acb 124struct blkio_group_stats {
edf1b879 125 struct u64_stats_sync syncp;
303a3acb
DS
126 /* total disk time and nr sectors dispatched by this group */
127 uint64_t time;
c4c76a05 128 uint64_t stat_arr[BLKIO_STAT_ARR_NR][BLKIO_STAT_TOTAL];
303a3acb 129#ifdef CONFIG_DEBUG_BLK_CGROUP
a23e6869
VG
130 /* Time not charged to this cgroup */
131 uint64_t unaccounted_time;
132
cdc1184c
DS
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;
303a3acb
DS
137 /* How many times this group has been removed from service tree */
138 unsigned long dequeue;
812df48d
DS
139
140 /* Total time spent waiting for it to be assigned a timeslice. */
141 uint64_t group_wait_time;
812df48d
DS
142
143 /* Time spent idling for this blkio_group */
144 uint64_t idle_time;
812df48d
DS
145 /*
146 * Total time when we have requests queued and do not contain the
147 * current active queue.
148 */
149 uint64_t empty_time;
997a026c
TH
150
151 /* fields after this shouldn't be cleared on stat reset */
152 uint64_t start_group_wait_time;
153 uint64_t start_idle_time;
812df48d
DS
154 uint64_t start_empty_time;
155 uint16_t flags;
303a3acb
DS
156#endif
157};
158
997a026c
TH
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)
165#endif
166
5624a4e4
VG
167/* Per cpu blkio group stats */
168struct blkio_group_stats_cpu {
169 uint64_t sectors;
170 uint64_t stat_arr_cpu[BLKIO_STAT_CPU_NR][BLKIO_STAT_TOTAL];
575969a0 171 struct u64_stats_sync syncp;
5624a4e4
VG
172};
173
e56da7e2
TH
174struct blkio_group_conf {
175 unsigned int weight;
176 unsigned int iops[2];
177 u64 bps[2];
178};
179
0381411e
TH
180/* per-blkg per-policy data */
181struct blkg_policy_data {
182 /* the blkg this per-policy data belongs to */
183 struct blkio_group *blkg;
184
549d3aa8
TH
185 /* Configuration */
186 struct blkio_group_conf conf;
187
188 struct blkio_group_stats stats;
189 /* Per cpu stats pointer */
190 struct blkio_group_stats_cpu __percpu *stats_cpu;
191
0381411e
TH
192 /* pol->pdata_size bytes of private data used by policy impl */
193 char pdata[] __aligned(__alignof__(unsigned long long));
194};
195
31e4c28d 196struct blkio_group {
c875f4d0
TH
197 /* Pointer to the associated request_queue */
198 struct request_queue *q;
e8989fae 199 struct list_head q_node;
31e4c28d 200 struct hlist_node blkcg_node;
7ee9c562 201 struct blkio_cgroup *blkcg;
2868ef7b
VG
202 /* Store cgroup path */
203 char path[128];
1adaf3dd
TH
204 /* reference count */
205 int refcnt;
22084190 206
549d3aa8 207 struct blkg_policy_data *pd[BLKIO_NR_POLICIES];
1adaf3dd 208
1cd9e039
VG
209 /* List of blkg waiting for per cpu stats memory to be allocated */
210 struct list_head alloc_node;
1adaf3dd 211 struct rcu_head rcu_head;
31e4c28d
VG
212};
213
0381411e 214typedef void (blkio_init_group_fn)(struct blkio_group *blkg);
ca32aefc 215typedef void (blkio_update_group_weight_fn)(struct request_queue *q,
fe071437 216 struct blkio_group *blkg, unsigned int weight);
ca32aefc 217typedef void (blkio_update_group_read_bps_fn)(struct request_queue *q,
fe071437 218 struct blkio_group *blkg, u64 read_bps);
ca32aefc 219typedef void (blkio_update_group_write_bps_fn)(struct request_queue *q,
fe071437 220 struct blkio_group *blkg, u64 write_bps);
ca32aefc 221typedef void (blkio_update_group_read_iops_fn)(struct request_queue *q,
fe071437 222 struct blkio_group *blkg, unsigned int read_iops);
ca32aefc 223typedef void (blkio_update_group_write_iops_fn)(struct request_queue *q,
fe071437 224 struct blkio_group *blkg, unsigned int write_iops);
3e252066
VG
225
226struct blkio_policy_ops {
0381411e 227 blkio_init_group_fn *blkio_init_group_fn;
3e252066 228 blkio_update_group_weight_fn *blkio_update_group_weight_fn;
4c9eefa1
VG
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;
7702e8f4
VG
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;
3e252066
VG
233};
234
235struct blkio_policy_type {
236 struct list_head list;
237 struct blkio_policy_ops ops;
062a644d 238 enum blkio_policy_id plid;
0381411e 239 size_t pdata_size; /* policy specific private data size */
3e252066
VG
240};
241
5efd6113
TH
242extern int blkcg_init_queue(struct request_queue *q);
243extern void blkcg_drain_queue(struct request_queue *q);
244extern void blkcg_exit_queue(struct request_queue *q);
245
3e252066
VG
246/* Blkio controller policy registration */
247extern void blkio_policy_register(struct blkio_policy_type *);
248extern void blkio_policy_unregister(struct blkio_policy_type *);
e8989fae
TH
249extern void blkg_destroy_all(struct request_queue *q, bool destroy_root);
250extern void update_root_blkg_pd(struct request_queue *q,
251 enum blkio_policy_id plid);
3e252066 252
0381411e
TH
253/**
254 * blkg_to_pdata - get policy private data
255 * @blkg: blkg of interest
256 * @pol: policy of interest
257 *
258 * Return pointer to private data associated with the @blkg-@pol pair.
259 */
260static inline void *blkg_to_pdata(struct blkio_group *blkg,
261 struct blkio_policy_type *pol)
262{
549d3aa8 263 return blkg ? blkg->pd[pol->plid]->pdata : NULL;
0381411e
TH
264}
265
266/**
267 * pdata_to_blkg - get blkg associated with policy private data
268 * @pdata: policy private data of interest
269 * @pol: policy @pdata is for
270 *
271 * @pdata is policy private data for @pol. Determine the blkg it's
272 * associated with.
273 */
274static inline struct blkio_group *pdata_to_blkg(void *pdata,
275 struct blkio_policy_type *pol)
276{
277 if (pdata) {
278 struct blkg_policy_data *pd =
279 container_of(pdata, struct blkg_policy_data, pdata);
280 return pd->blkg;
281 }
282 return NULL;
283}
284
afc24d49
VG
285static inline char *blkg_path(struct blkio_group *blkg)
286{
287 return blkg->path;
288}
289
1adaf3dd
TH
290/**
291 * blkg_get - get a blkg reference
292 * @blkg: blkg to get
293 *
294 * The caller should be holding queue_lock and an existing reference.
295 */
296static inline void blkg_get(struct blkio_group *blkg)
297{
298 lockdep_assert_held(blkg->q->queue_lock);
299 WARN_ON_ONCE(!blkg->refcnt);
300 blkg->refcnt++;
301}
302
303void __blkg_release(struct blkio_group *blkg);
304
305/**
306 * blkg_put - put a blkg reference
307 * @blkg: blkg to put
308 *
309 * The caller should be holding queue_lock.
310 */
311static inline void blkg_put(struct blkio_group *blkg)
312{
313 lockdep_assert_held(blkg->q->queue_lock);
314 WARN_ON_ONCE(blkg->refcnt <= 0);
315 if (!--blkg->refcnt)
316 __blkg_release(blkg);
317}
318
2f5ea477
JA
319#else
320
321struct blkio_group {
322};
323
3e252066
VG
324struct blkio_policy_type {
325};
326
5efd6113
TH
327static inline int blkcg_init_queue(struct request_queue *q) { return 0; }
328static inline void blkcg_drain_queue(struct request_queue *q) { }
329static inline void blkcg_exit_queue(struct request_queue *q) { }
3e252066
VG
330static inline void blkio_policy_register(struct blkio_policy_type *blkiop) { }
331static inline void blkio_policy_unregister(struct blkio_policy_type *blkiop) { }
03aa264a 332static inline void blkg_destroy_all(struct request_queue *q,
03aa264a 333 bool destory_root) { }
e8989fae
TH
334static inline void update_root_blkg_pd(struct request_queue *q,
335 enum blkio_policy_id plid) { }
3e252066 336
0381411e
TH
337static inline void *blkg_to_pdata(struct blkio_group *blkg,
338 struct blkio_policy_type *pol) { return NULL; }
339static inline struct blkio_group *pdata_to_blkg(void *pdata,
340 struct blkio_policy_type *pol) { return NULL; }
afc24d49 341static inline char *blkg_path(struct blkio_group *blkg) { return NULL; }
1adaf3dd
TH
342static inline void blkg_get(struct blkio_group *blkg) { }
343static inline void blkg_put(struct blkio_group *blkg) { }
afc24d49 344
2f5ea477
JA
345#endif
346
df457f84 347#define BLKIO_WEIGHT_MIN 10
31e4c28d
VG
348#define BLKIO_WEIGHT_MAX 1000
349#define BLKIO_WEIGHT_DEFAULT 500
350
2868ef7b 351#ifdef CONFIG_DEBUG_BLK_CGROUP
c1768268
TH
352void blkiocg_update_avg_queue_size_stats(struct blkio_group *blkg,
353 struct blkio_policy_type *pol);
9195291e 354void blkiocg_update_dequeue_stats(struct blkio_group *blkg,
c1768268
TH
355 struct blkio_policy_type *pol,
356 unsigned long dequeue);
357void blkiocg_update_set_idle_time_stats(struct blkio_group *blkg,
358 struct blkio_policy_type *pol);
359void blkiocg_update_idle_time_stats(struct blkio_group *blkg,
360 struct blkio_policy_type *pol);
361void blkiocg_set_start_empty_time(struct blkio_group *blkg,
362 struct blkio_policy_type *pol);
812df48d
DS
363
364#define BLKG_FLAG_FNS(name) \
365static inline void blkio_mark_blkg_##name( \
366 struct blkio_group_stats *stats) \
367{ \
368 stats->flags |= (1 << BLKG_##name); \
369} \
370static inline void blkio_clear_blkg_##name( \
371 struct blkio_group_stats *stats) \
372{ \
373 stats->flags &= ~(1 << BLKG_##name); \
374} \
375static inline int blkio_blkg_##name(struct blkio_group_stats *stats) \
376{ \
377 return (stats->flags & (1 << BLKG_##name)) != 0; \
378} \
379
380BLKG_FLAG_FNS(waiting)
381BLKG_FLAG_FNS(idling)
382BLKG_FLAG_FNS(empty)
383#undef BLKG_FLAG_FNS
2868ef7b 384#else
c1768268
TH
385static inline void blkiocg_update_avg_queue_size_stats(struct blkio_group *blkg,
386 struct blkio_policy_type *pol) { }
9195291e 387static inline void blkiocg_update_dequeue_stats(struct blkio_group *blkg,
c1768268
TH
388 struct blkio_policy_type *pol, unsigned long dequeue) { }
389static inline void blkiocg_update_set_idle_time_stats(struct blkio_group *blkg,
390 struct blkio_policy_type *pol) { }
391static inline void blkiocg_update_idle_time_stats(struct blkio_group *blkg,
392 struct blkio_policy_type *pol) { }
393static inline void blkiocg_set_start_empty_time(struct blkio_group *blkg,
394 struct blkio_policy_type *pol) { }
2868ef7b
VG
395#endif
396
32e380ae 397#ifdef CONFIG_BLK_CGROUP
31e4c28d
VG
398extern struct blkio_cgroup blkio_root_cgroup;
399extern struct blkio_cgroup *cgroup_to_blkio_cgroup(struct cgroup *cgroup);
4f85cb96 400extern struct blkio_cgroup *bio_blkio_cgroup(struct bio *bio);
cd1604fa 401extern struct blkio_group *blkg_lookup(struct blkio_cgroup *blkcg,
e8989fae 402 struct request_queue *q);
cd1604fa
TH
403struct blkio_group *blkg_lookup_create(struct blkio_cgroup *blkcg,
404 struct request_queue *q,
405 enum blkio_policy_id plid,
406 bool for_root);
303a3acb 407void blkiocg_update_timeslice_used(struct blkio_group *blkg,
c1768268
TH
408 struct blkio_policy_type *pol,
409 unsigned long time,
410 unsigned long unaccounted_time);
411void blkiocg_update_dispatch_stats(struct blkio_group *blkg,
412 struct blkio_policy_type *pol,
413 uint64_t bytes, bool direction, bool sync);
84c124da 414void blkiocg_update_completion_stats(struct blkio_group *blkg,
c1768268
TH
415 struct blkio_policy_type *pol,
416 uint64_t start_time,
417 uint64_t io_start_time, bool direction,
418 bool sync);
419void blkiocg_update_io_merged_stats(struct blkio_group *blkg,
420 struct blkio_policy_type *pol,
421 bool direction, bool sync);
a11cdaa7 422void blkiocg_update_io_add_stats(struct blkio_group *blkg,
c1768268
TH
423 struct blkio_policy_type *pol,
424 struct blkio_group *curr_blkg, bool direction,
425 bool sync);
a11cdaa7 426void blkiocg_update_io_remove_stats(struct blkio_group *blkg,
c1768268
TH
427 struct blkio_policy_type *pol,
428 bool direction, bool sync);
31e4c28d 429#else
2f5ea477 430struct cgroup;
31e4c28d
VG
431static inline struct blkio_cgroup *
432cgroup_to_blkio_cgroup(struct cgroup *cgroup) { return NULL; }
70087dc3 433static inline struct blkio_cgroup *
4f85cb96 434bio_blkio_cgroup(struct bio *bio) { return NULL; }
31e4c28d 435
cd1604fa
TH
436static inline struct blkio_group *blkg_lookup(struct blkio_cgroup *blkcg,
437 void *key) { return NULL; }
303a3acb 438static inline void blkiocg_update_timeslice_used(struct blkio_group *blkg,
c1768268
TH
439 struct blkio_policy_type *pol, unsigned long time,
440 unsigned long unaccounted_time) { }
84c124da 441static inline void blkiocg_update_dispatch_stats(struct blkio_group *blkg,
c1768268
TH
442 struct blkio_policy_type *pol, uint64_t bytes,
443 bool direction, bool sync) { }
84c124da 444static inline void blkiocg_update_completion_stats(struct blkio_group *blkg,
c1768268
TH
445 struct blkio_policy_type *pol, uint64_t start_time,
446 uint64_t io_start_time, bool direction, bool sync) { }
812d4026 447static inline void blkiocg_update_io_merged_stats(struct blkio_group *blkg,
c1768268
TH
448 struct blkio_policy_type *pol, bool direction,
449 bool sync) { }
a11cdaa7 450static inline void blkiocg_update_io_add_stats(struct blkio_group *blkg,
c1768268
TH
451 struct blkio_policy_type *pol,
452 struct blkio_group *curr_blkg, bool direction,
453 bool sync) { }
a11cdaa7 454static inline void blkiocg_update_io_remove_stats(struct blkio_group *blkg,
c1768268
TH
455 struct blkio_policy_type *pol, bool direction,
456 bool sync) { }
31e4c28d
VG
457#endif
458#endif /* _BLK_CGROUP_H */
This page took 0.186152 seconds and 5 git commands to generate.