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 blkcg */
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 blkcg_gq
*blkg
;
67 /* used during policy activation */
68 struct list_head alloc_node
;
70 /* pol->pdata_size bytes of private data used by policy impl */
71 char pdata
[] __aligned(__alignof__(unsigned long long));
74 /* association between a blk cgroup and a request queue */
76 /* Pointer to the associated request_queue */
77 struct request_queue
*q
;
78 struct list_head q_node
;
79 struct hlist_node blkcg_node
;
84 struct blkg_policy_data
*pd
[BLKCG_MAX_POLS
];
86 struct rcu_head rcu_head
;
89 typedef void (blkcg_pol_init_pd_fn
)(struct blkcg_gq
*blkg
);
90 typedef void (blkcg_pol_exit_pd_fn
)(struct blkcg_gq
*blkg
);
91 typedef void (blkcg_pol_reset_pd_stats_fn
)(struct blkcg_gq
*blkg
);
93 struct blkcg_policy_ops
{
94 blkcg_pol_init_pd_fn
*pd_init_fn
;
95 blkcg_pol_exit_pd_fn
*pd_exit_fn
;
96 blkcg_pol_reset_pd_stats_fn
*pd_reset_stats_fn
;
100 struct blkcg_policy_ops ops
;
102 /* policy specific private data size */
104 /* cgroup files for the policy */
105 struct cftype
*cftypes
;
108 extern struct blkcg blkcg_root
;
110 struct blkcg
*cgroup_to_blkcg(struct cgroup
*cgroup
);
111 struct blkcg
*bio_blkcg(struct bio
*bio
);
112 struct blkcg_gq
*blkg_lookup(struct blkcg
*blkcg
, struct request_queue
*q
);
113 struct blkcg_gq
*blkg_lookup_create(struct blkcg
*blkcg
,
114 struct request_queue
*q
);
115 int blkcg_init_queue(struct request_queue
*q
);
116 void blkcg_drain_queue(struct request_queue
*q
);
117 void blkcg_exit_queue(struct request_queue
*q
);
119 /* Blkio controller policy registration */
120 int blkcg_policy_register(struct blkcg_policy
*pol
);
121 void blkcg_policy_unregister(struct blkcg_policy
*pol
);
122 int blkcg_activate_policy(struct request_queue
*q
,
123 const struct blkcg_policy
*pol
);
124 void blkcg_deactivate_policy(struct request_queue
*q
,
125 const struct blkcg_policy
*pol
);
127 void blkcg_print_blkgs(struct seq_file
*sf
, struct blkcg
*blkcg
,
128 u64 (*prfill
)(struct seq_file
*, void *, int),
129 const struct blkcg_policy
*pol
, int data
,
131 u64
__blkg_prfill_u64(struct seq_file
*sf
, void *pdata
, u64 v
);
132 u64
__blkg_prfill_rwstat(struct seq_file
*sf
, void *pdata
,
133 const struct blkg_rwstat
*rwstat
);
134 u64
blkg_prfill_stat(struct seq_file
*sf
, void *pdata
, int off
);
135 u64
blkg_prfill_rwstat(struct seq_file
*sf
, void *pdata
, int off
);
137 struct blkg_conf_ctx
{
138 struct gendisk
*disk
;
139 struct blkcg_gq
*blkg
;
143 int blkg_conf_prep(struct blkcg
*blkcg
, const struct blkcg_policy
*pol
,
144 const char *input
, struct blkg_conf_ctx
*ctx
);
145 void blkg_conf_finish(struct blkg_conf_ctx
*ctx
);
149 * blkg_to_pdata - get policy private data
150 * @blkg: blkg of interest
151 * @pol: policy of interest
153 * Return pointer to private data associated with the @blkg-@pol pair.
155 static inline void *blkg_to_pdata(struct blkcg_gq
*blkg
,
156 struct blkcg_policy
*pol
)
158 return blkg
? blkg
->pd
[pol
->plid
]->pdata
: NULL
;
162 * pdata_to_blkg - get blkg associated with policy private data
163 * @pdata: policy private data of interest
165 * @pdata is policy private data. Determine the blkg it's associated with.
167 static inline struct blkcg_gq
*pdata_to_blkg(void *pdata
)
170 struct blkg_policy_data
*pd
=
171 container_of(pdata
, struct blkg_policy_data
, pdata
);
178 * blkg_path - format cgroup path of blkg
179 * @blkg: blkg of interest
180 * @buf: target buffer
181 * @buflen: target buffer length
183 * Format the path of the cgroup of @blkg into @buf.
185 static inline int blkg_path(struct blkcg_gq
*blkg
, char *buf
, int buflen
)
190 ret
= cgroup_path(blkg
->blkcg
->css
.cgroup
, buf
, buflen
);
193 strncpy(buf
, "<unavailable>", buflen
);
198 * blkg_get - get a blkg reference
201 * The caller should be holding queue_lock and an existing reference.
203 static inline void blkg_get(struct blkcg_gq
*blkg
)
205 lockdep_assert_held(blkg
->q
->queue_lock
);
206 WARN_ON_ONCE(!blkg
->refcnt
);
210 void __blkg_release(struct blkcg_gq
*blkg
);
213 * blkg_put - put a blkg reference
216 * The caller should be holding queue_lock.
218 static inline void blkg_put(struct blkcg_gq
*blkg
)
220 lockdep_assert_held(blkg
->q
->queue_lock
);
221 WARN_ON_ONCE(blkg
->refcnt
<= 0);
223 __blkg_release(blkg
);
227 * blkg_stat_add - add a value to a blkg_stat
228 * @stat: target blkg_stat
231 * Add @val to @stat. The caller is responsible for synchronizing calls to
234 static inline void blkg_stat_add(struct blkg_stat
*stat
, uint64_t val
)
236 u64_stats_update_begin(&stat
->syncp
);
238 u64_stats_update_end(&stat
->syncp
);
242 * blkg_stat_read - read the current value of a blkg_stat
243 * @stat: blkg_stat to read
245 * Read the current value of @stat. This function can be called without
246 * synchroniztion and takes care of u64 atomicity.
248 static inline uint64_t blkg_stat_read(struct blkg_stat
*stat
)
254 start
= u64_stats_fetch_begin(&stat
->syncp
);
256 } while (u64_stats_fetch_retry(&stat
->syncp
, start
));
262 * blkg_stat_reset - reset a blkg_stat
263 * @stat: blkg_stat to reset
265 static inline void blkg_stat_reset(struct blkg_stat
*stat
)
271 * blkg_rwstat_add - add a value to a blkg_rwstat
272 * @rwstat: target blkg_rwstat
273 * @rw: mask of REQ_{WRITE|SYNC}
276 * Add @val to @rwstat. The counters are chosen according to @rw. The
277 * caller is responsible for synchronizing calls to this function.
279 static inline void blkg_rwstat_add(struct blkg_rwstat
*rwstat
,
280 int rw
, uint64_t val
)
282 u64_stats_update_begin(&rwstat
->syncp
);
285 rwstat
->cnt
[BLKG_RWSTAT_WRITE
] += val
;
287 rwstat
->cnt
[BLKG_RWSTAT_READ
] += val
;
289 rwstat
->cnt
[BLKG_RWSTAT_SYNC
] += val
;
291 rwstat
->cnt
[BLKG_RWSTAT_ASYNC
] += val
;
293 u64_stats_update_end(&rwstat
->syncp
);
297 * blkg_rwstat_read - read the current values of a blkg_rwstat
298 * @rwstat: blkg_rwstat to read
300 * Read the current snapshot of @rwstat and return it as the return value.
301 * This function can be called without synchronization and takes care of
304 static inline struct blkg_rwstat
blkg_rwstat_read(struct blkg_rwstat
*rwstat
)
307 struct blkg_rwstat tmp
;
310 start
= u64_stats_fetch_begin(&rwstat
->syncp
);
312 } while (u64_stats_fetch_retry(&rwstat
->syncp
, start
));
318 * blkg_rwstat_sum - read the total count of a blkg_rwstat
319 * @rwstat: blkg_rwstat to read
321 * Return the total count of @rwstat regardless of the IO direction. This
322 * function can be called without synchronization and takes care of u64
325 static inline uint64_t blkg_rwstat_sum(struct blkg_rwstat
*rwstat
)
327 struct blkg_rwstat tmp
= blkg_rwstat_read(rwstat
);
329 return tmp
.cnt
[BLKG_RWSTAT_READ
] + tmp
.cnt
[BLKG_RWSTAT_WRITE
];
333 * blkg_rwstat_reset - reset a blkg_rwstat
334 * @rwstat: blkg_rwstat to reset
336 static inline void blkg_rwstat_reset(struct blkg_rwstat
*rwstat
)
338 memset(rwstat
->cnt
, 0, sizeof(rwstat
->cnt
));
341 #else /* CONFIG_BLK_CGROUP */
348 struct blkcg_policy
{
351 static inline struct blkcg
*cgroup_to_blkcg(struct cgroup
*cgroup
) { return NULL
; }
352 static inline struct blkcg
*bio_blkcg(struct bio
*bio
) { return NULL
; }
353 static inline struct blkcg_gq
*blkg_lookup(struct blkcg
*blkcg
, void *key
) { return NULL
; }
354 static inline int blkcg_init_queue(struct request_queue
*q
) { return 0; }
355 static inline void blkcg_drain_queue(struct request_queue
*q
) { }
356 static inline void blkcg_exit_queue(struct request_queue
*q
) { }
357 static inline int blkcg_policy_register(struct blkcg_policy
*pol
) { return 0; }
358 static inline void blkcg_policy_unregister(struct blkcg_policy
*pol
) { }
359 static inline int blkcg_activate_policy(struct request_queue
*q
,
360 const struct blkcg_policy
*pol
) { return 0; }
361 static inline void blkcg_deactivate_policy(struct request_queue
*q
,
362 const struct blkcg_policy
*pol
) { }
364 static inline void *blkg_to_pdata(struct blkcg_gq
*blkg
,
365 struct blkcg_policy
*pol
) { return NULL
; }
366 static inline struct blkcg_gq
*pdata_to_blkg(void *pdata
,
367 struct blkcg_policy
*pol
) { return NULL
; }
368 static inline char *blkg_path(struct blkcg_gq
*blkg
) { return NULL
; }
369 static inline void blkg_get(struct blkcg_gq
*blkg
) { }
370 static inline void blkg_put(struct blkcg_gq
*blkg
) { }
372 #endif /* CONFIG_BLK_CGROUP */
373 #endif /* _BLK_CGROUP_H */