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 blkio_cgroup */
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
];
62 /* per-blkg per-policy data */
63 struct blkg_policy_data
{
64 /* the blkg this per-policy data belongs to */
65 struct blkio_group
*blkg
;
67 /* pol->pdata_size bytes of private data used by policy impl */
68 char pdata
[] __aligned(__alignof__(unsigned long long));
72 /* Pointer to the associated request_queue */
73 struct request_queue
*q
;
74 struct list_head q_node
;
75 struct hlist_node blkcg_node
;
76 struct blkio_cgroup
*blkcg
;
77 /* Store cgroup path */
82 struct blkg_policy_data
*pd
[BLKCG_MAX_POLS
];
84 struct rcu_head rcu_head
;
87 typedef void (blkio_init_group_fn
)(struct blkio_group
*blkg
);
88 typedef void (blkio_exit_group_fn
)(struct blkio_group
*blkg
);
89 typedef void (blkio_reset_group_stats_fn
)(struct blkio_group
*blkg
);
91 struct blkio_policy_ops
{
92 blkio_init_group_fn
*blkio_init_group_fn
;
93 blkio_exit_group_fn
*blkio_exit_group_fn
;
94 blkio_reset_group_stats_fn
*blkio_reset_group_stats_fn
;
97 struct blkio_policy_type
{
98 struct blkio_policy_ops ops
;
100 size_t pdata_size
; /* policy specific private data size */
101 struct cftype
*cftypes
; /* cgroup files for the policy */
104 extern int blkcg_init_queue(struct request_queue
*q
);
105 extern void blkcg_drain_queue(struct request_queue
*q
);
106 extern void blkcg_exit_queue(struct request_queue
*q
);
108 /* Blkio controller policy registration */
109 extern int blkio_policy_register(struct blkio_policy_type
*);
110 extern void blkio_policy_unregister(struct blkio_policy_type
*);
111 extern void blkg_destroy_all(struct request_queue
*q
, bool destroy_root
);
112 extern void update_root_blkg_pd(struct request_queue
*q
,
113 const struct blkio_policy_type
*pol
);
115 void blkcg_print_blkgs(struct seq_file
*sf
, struct blkio_cgroup
*blkcg
,
116 u64 (*prfill
)(struct seq_file
*, void *, int),
117 const struct blkio_policy_type
*pol
, int data
,
119 u64
__blkg_prfill_u64(struct seq_file
*sf
, void *pdata
, u64 v
);
120 u64
__blkg_prfill_rwstat(struct seq_file
*sf
, void *pdata
,
121 const struct blkg_rwstat
*rwstat
);
122 u64
blkg_prfill_stat(struct seq_file
*sf
, void *pdata
, int off
);
123 u64
blkg_prfill_rwstat(struct seq_file
*sf
, void *pdata
, int off
);
125 struct blkg_conf_ctx
{
126 struct gendisk
*disk
;
127 struct blkio_group
*blkg
;
131 int blkg_conf_prep(struct blkio_cgroup
*blkcg
, const char *input
,
132 struct blkg_conf_ctx
*ctx
);
133 void blkg_conf_finish(struct blkg_conf_ctx
*ctx
);
137 * blkg_to_pdata - get policy private data
138 * @blkg: blkg of interest
139 * @pol: policy of interest
141 * Return pointer to private data associated with the @blkg-@pol pair.
143 static inline void *blkg_to_pdata(struct blkio_group
*blkg
,
144 struct blkio_policy_type
*pol
)
146 return blkg
? blkg
->pd
[pol
->plid
]->pdata
: NULL
;
150 * pdata_to_blkg - get blkg associated with policy private data
151 * @pdata: policy private data of interest
153 * @pdata is policy private data. Determine the blkg it's associated with.
155 static inline struct blkio_group
*pdata_to_blkg(void *pdata
)
158 struct blkg_policy_data
*pd
=
159 container_of(pdata
, struct blkg_policy_data
, pdata
);
165 static inline char *blkg_path(struct blkio_group
*blkg
)
171 * blkg_get - get a blkg reference
174 * The caller should be holding queue_lock and an existing reference.
176 static inline void blkg_get(struct blkio_group
*blkg
)
178 lockdep_assert_held(blkg
->q
->queue_lock
);
179 WARN_ON_ONCE(!blkg
->refcnt
);
183 void __blkg_release(struct blkio_group
*blkg
);
186 * blkg_put - put a blkg reference
189 * The caller should be holding queue_lock.
191 static inline void blkg_put(struct blkio_group
*blkg
)
193 lockdep_assert_held(blkg
->q
->queue_lock
);
194 WARN_ON_ONCE(blkg
->refcnt
<= 0);
196 __blkg_release(blkg
);
200 * blkg_stat_add - add a value to a blkg_stat
201 * @stat: target blkg_stat
204 * Add @val to @stat. The caller is responsible for synchronizing calls to
207 static inline void blkg_stat_add(struct blkg_stat
*stat
, uint64_t val
)
209 u64_stats_update_begin(&stat
->syncp
);
211 u64_stats_update_end(&stat
->syncp
);
215 * blkg_stat_read - read the current value of a blkg_stat
216 * @stat: blkg_stat to read
218 * Read the current value of @stat. This function can be called without
219 * synchroniztion and takes care of u64 atomicity.
221 static inline uint64_t blkg_stat_read(struct blkg_stat
*stat
)
227 start
= u64_stats_fetch_begin(&stat
->syncp
);
229 } while (u64_stats_fetch_retry(&stat
->syncp
, start
));
235 * blkg_stat_reset - reset a blkg_stat
236 * @stat: blkg_stat to reset
238 static inline void blkg_stat_reset(struct blkg_stat
*stat
)
244 * blkg_rwstat_add - add a value to a blkg_rwstat
245 * @rwstat: target blkg_rwstat
246 * @rw: mask of REQ_{WRITE|SYNC}
249 * Add @val to @rwstat. The counters are chosen according to @rw. The
250 * caller is responsible for synchronizing calls to this function.
252 static inline void blkg_rwstat_add(struct blkg_rwstat
*rwstat
,
253 int rw
, uint64_t val
)
255 u64_stats_update_begin(&rwstat
->syncp
);
258 rwstat
->cnt
[BLKG_RWSTAT_WRITE
] += val
;
260 rwstat
->cnt
[BLKG_RWSTAT_READ
] += val
;
262 rwstat
->cnt
[BLKG_RWSTAT_SYNC
] += val
;
264 rwstat
->cnt
[BLKG_RWSTAT_ASYNC
] += val
;
266 u64_stats_update_end(&rwstat
->syncp
);
270 * blkg_rwstat_read - read the current values of a blkg_rwstat
271 * @rwstat: blkg_rwstat to read
273 * Read the current snapshot of @rwstat and return it as the return value.
274 * This function can be called without synchronization and takes care of
277 static struct blkg_rwstat
blkg_rwstat_read(struct blkg_rwstat
*rwstat
)
280 struct blkg_rwstat tmp
;
283 start
= u64_stats_fetch_begin(&rwstat
->syncp
);
285 } while (u64_stats_fetch_retry(&rwstat
->syncp
, start
));
291 * blkg_rwstat_sum - read the total count of a blkg_rwstat
292 * @rwstat: blkg_rwstat to read
294 * Return the total count of @rwstat regardless of the IO direction. This
295 * function can be called without synchronization and takes care of u64
298 static inline uint64_t blkg_rwstat_sum(struct blkg_rwstat
*rwstat
)
300 struct blkg_rwstat tmp
= blkg_rwstat_read(rwstat
);
302 return tmp
.cnt
[BLKG_RWSTAT_READ
] + tmp
.cnt
[BLKG_RWSTAT_WRITE
];
306 * blkg_rwstat_reset - reset a blkg_rwstat
307 * @rwstat: blkg_rwstat to reset
309 static inline void blkg_rwstat_reset(struct blkg_rwstat
*rwstat
)
311 memset(rwstat
->cnt
, 0, sizeof(rwstat
->cnt
));
319 struct blkio_policy_type
{
322 static inline int blkcg_init_queue(struct request_queue
*q
) { return 0; }
323 static inline void blkcg_drain_queue(struct request_queue
*q
) { }
324 static inline void blkcg_exit_queue(struct request_queue
*q
) { }
325 static inline int blkio_policy_register(struct blkio_policy_type
*blkiop
) { return 0; }
326 static inline void blkio_policy_unregister(struct blkio_policy_type
*blkiop
) { }
327 static inline void blkg_destroy_all(struct request_queue
*q
,
328 bool destory_root
) { }
329 static inline void update_root_blkg_pd(struct request_queue
*q
,
330 const struct blkio_policy_type
*pol
) { }
332 static inline void *blkg_to_pdata(struct blkio_group
*blkg
,
333 struct blkio_policy_type
*pol
) { return NULL
; }
334 static inline struct blkio_group
*pdata_to_blkg(void *pdata
,
335 struct blkio_policy_type
*pol
) { return NULL
; }
336 static inline char *blkg_path(struct blkio_group
*blkg
) { return NULL
; }
337 static inline void blkg_get(struct blkio_group
*blkg
) { }
338 static inline void blkg_put(struct blkio_group
*blkg
) { }
342 #ifdef CONFIG_BLK_CGROUP
343 extern struct blkio_cgroup blkio_root_cgroup
;
344 extern struct blkio_cgroup
*cgroup_to_blkio_cgroup(struct cgroup
*cgroup
);
345 extern struct blkio_cgroup
*bio_blkio_cgroup(struct bio
*bio
);
346 extern struct blkio_group
*blkg_lookup(struct blkio_cgroup
*blkcg
,
347 struct request_queue
*q
);
348 struct blkio_group
*blkg_lookup_create(struct blkio_cgroup
*blkcg
,
349 struct request_queue
*q
,
353 static inline struct blkio_cgroup
*
354 cgroup_to_blkio_cgroup(struct cgroup
*cgroup
) { return NULL
; }
355 static inline struct blkio_cgroup
*
356 bio_blkio_cgroup(struct bio
*bio
) { return NULL
; }
358 static inline struct blkio_group
*blkg_lookup(struct blkio_cgroup
*blkcg
,
359 void *key
) { return NULL
; }
361 #endif /* _BLK_CGROUP_H */