]>
Commit | Line | Data |
---|---|---|
31e4c28d VG |
1 | /* |
2 | * Common Block IO controller cgroup interface | |
3 | * | |
4 | * Based on ideas and code from CFQ, CFS and BFQ: | |
5 | * Copyright (C) 2003 Jens Axboe <axboe@kernel.dk> | |
6 | * | |
7 | * Copyright (C) 2008 Fabio Checconi <fabio@gandalf.sssup.it> | |
8 | * Paolo Valente <paolo.valente@unimore.it> | |
9 | * | |
10 | * Copyright (C) 2009 Vivek Goyal <vgoyal@redhat.com> | |
11 | * Nauman Rafique <nauman@google.com> | |
e48453c3 AA |
12 | * |
13 | * For policy-specific per-blkcg data: | |
14 | * Copyright (C) 2015 Paolo Valente <paolo.valente@unimore.it> | |
15 | * Arianna Avanzini <avanzini.arianna@gmail.com> | |
31e4c28d VG |
16 | */ |
17 | #include <linux/ioprio.h> | |
22084190 | 18 | #include <linux/kdev_t.h> |
9d6a986c | 19 | #include <linux/module.h> |
accee785 | 20 | #include <linux/err.h> |
9195291e | 21 | #include <linux/blkdev.h> |
52ebea74 | 22 | #include <linux/backing-dev.h> |
5a0e3ad6 | 23 | #include <linux/slab.h> |
34d0f179 | 24 | #include <linux/genhd.h> |
72e06c25 | 25 | #include <linux/delay.h> |
9a9e8a26 | 26 | #include <linux/atomic.h> |
eea8f41c | 27 | #include <linux/blk-cgroup.h> |
5efd6113 | 28 | #include "blk.h" |
3e252066 | 29 | |
84c124da DS |
30 | #define MAX_KEY_LEN 100 |
31 | ||
838f13bf TH |
32 | /* |
33 | * blkcg_pol_mutex protects blkcg_policy[] and policy [de]activation. | |
34 | * blkcg_pol_register_mutex nests outside of it and synchronizes entire | |
35 | * policy [un]register operations including cgroup file additions / | |
36 | * removals. Putting cgroup file registration outside blkcg_pol_mutex | |
37 | * allows grabbing it from cgroup callbacks. | |
38 | */ | |
39 | static DEFINE_MUTEX(blkcg_pol_register_mutex); | |
bc0d6501 | 40 | static DEFINE_MUTEX(blkcg_pol_mutex); |
923adde1 | 41 | |
e48453c3 | 42 | struct blkcg blkcg_root; |
3c798398 | 43 | EXPORT_SYMBOL_GPL(blkcg_root); |
9d6a986c | 44 | |
496d5e75 TH |
45 | struct cgroup_subsys_state * const blkcg_root_css = &blkcg_root.css; |
46 | ||
3c798398 | 47 | static struct blkcg_policy *blkcg_policy[BLKCG_MAX_POLS]; |
035d10b2 | 48 | |
7876f930 TH |
49 | static LIST_HEAD(all_blkcgs); /* protected by blkcg_pol_mutex */ |
50 | ||
a2b1693b | 51 | static bool blkcg_policy_enabled(struct request_queue *q, |
3c798398 | 52 | const struct blkcg_policy *pol) |
a2b1693b TH |
53 | { |
54 | return pol && test_bit(pol->plid, q->blkcg_pols); | |
55 | } | |
56 | ||
0381411e TH |
57 | /** |
58 | * blkg_free - free a blkg | |
59 | * @blkg: blkg to free | |
60 | * | |
61 | * Free @blkg which may be partially allocated. | |
62 | */ | |
3c798398 | 63 | static void blkg_free(struct blkcg_gq *blkg) |
0381411e | 64 | { |
e8989fae | 65 | int i; |
549d3aa8 TH |
66 | |
67 | if (!blkg) | |
68 | return; | |
69 | ||
db613670 | 70 | for (i = 0; i < BLKCG_MAX_POLS; i++) |
001bea73 TH |
71 | if (blkg->pd[i]) |
72 | blkcg_policy[i]->pd_free_fn(blkg->pd[i]); | |
e8989fae | 73 | |
994b7832 TH |
74 | if (blkg->blkcg != &blkcg_root) |
75 | blk_exit_rl(&blkg->rl); | |
549d3aa8 | 76 | kfree(blkg); |
0381411e TH |
77 | } |
78 | ||
79 | /** | |
80 | * blkg_alloc - allocate a blkg | |
81 | * @blkcg: block cgroup the new blkg is associated with | |
82 | * @q: request_queue the new blkg is associated with | |
15974993 | 83 | * @gfp_mask: allocation mask to use |
0381411e | 84 | * |
e8989fae | 85 | * Allocate a new blkg assocating @blkcg and @q. |
0381411e | 86 | */ |
15974993 TH |
87 | static struct blkcg_gq *blkg_alloc(struct blkcg *blkcg, struct request_queue *q, |
88 | gfp_t gfp_mask) | |
0381411e | 89 | { |
3c798398 | 90 | struct blkcg_gq *blkg; |
e8989fae | 91 | int i; |
0381411e TH |
92 | |
93 | /* alloc and init base part */ | |
15974993 | 94 | blkg = kzalloc_node(sizeof(*blkg), gfp_mask, q->node); |
0381411e TH |
95 | if (!blkg) |
96 | return NULL; | |
97 | ||
c875f4d0 | 98 | blkg->q = q; |
e8989fae | 99 | INIT_LIST_HEAD(&blkg->q_node); |
0381411e | 100 | blkg->blkcg = blkcg; |
a5049a8a | 101 | atomic_set(&blkg->refcnt, 1); |
0381411e | 102 | |
a051661c TH |
103 | /* root blkg uses @q->root_rl, init rl only for !root blkgs */ |
104 | if (blkcg != &blkcg_root) { | |
105 | if (blk_init_rl(&blkg->rl, q, gfp_mask)) | |
106 | goto err_free; | |
107 | blkg->rl.blkg = blkg; | |
108 | } | |
109 | ||
8bd435b3 | 110 | for (i = 0; i < BLKCG_MAX_POLS; i++) { |
3c798398 | 111 | struct blkcg_policy *pol = blkcg_policy[i]; |
e8989fae | 112 | struct blkg_policy_data *pd; |
0381411e | 113 | |
a2b1693b | 114 | if (!blkcg_policy_enabled(q, pol)) |
e8989fae TH |
115 | continue; |
116 | ||
117 | /* alloc per-policy data and attach it to blkg */ | |
001bea73 | 118 | pd = pol->pd_alloc_fn(gfp_mask, q->node); |
a051661c TH |
119 | if (!pd) |
120 | goto err_free; | |
549d3aa8 | 121 | |
e8989fae TH |
122 | blkg->pd[i] = pd; |
123 | pd->blkg = blkg; | |
b276a876 | 124 | pd->plid = i; |
e8989fae TH |
125 | } |
126 | ||
0381411e | 127 | return blkg; |
a051661c TH |
128 | |
129 | err_free: | |
130 | blkg_free(blkg); | |
131 | return NULL; | |
0381411e TH |
132 | } |
133 | ||
24f29046 TH |
134 | struct blkcg_gq *blkg_lookup_slowpath(struct blkcg *blkcg, |
135 | struct request_queue *q, bool update_hint) | |
80fd9979 | 136 | { |
3c798398 | 137 | struct blkcg_gq *blkg; |
80fd9979 | 138 | |
a637120e | 139 | /* |
86cde6b6 TH |
140 | * Hint didn't match. Look up from the radix tree. Note that the |
141 | * hint can only be updated under queue_lock as otherwise @blkg | |
142 | * could have already been removed from blkg_tree. The caller is | |
143 | * responsible for grabbing queue_lock if @update_hint. | |
a637120e TH |
144 | */ |
145 | blkg = radix_tree_lookup(&blkcg->blkg_tree, q->id); | |
86cde6b6 TH |
146 | if (blkg && blkg->q == q) { |
147 | if (update_hint) { | |
148 | lockdep_assert_held(q->queue_lock); | |
149 | rcu_assign_pointer(blkcg->blkg_hint, blkg); | |
150 | } | |
a637120e | 151 | return blkg; |
86cde6b6 | 152 | } |
a637120e | 153 | |
80fd9979 TH |
154 | return NULL; |
155 | } | |
ae118896 | 156 | EXPORT_SYMBOL_GPL(blkg_lookup_slowpath); |
80fd9979 | 157 | |
15974993 TH |
158 | /* |
159 | * If @new_blkg is %NULL, this function tries to allocate a new one as | |
d93a11f1 | 160 | * necessary using %GFP_NOWAIT. @new_blkg is always consumed on return. |
15974993 | 161 | */ |
86cde6b6 TH |
162 | static struct blkcg_gq *blkg_create(struct blkcg *blkcg, |
163 | struct request_queue *q, | |
164 | struct blkcg_gq *new_blkg) | |
5624a4e4 | 165 | { |
3c798398 | 166 | struct blkcg_gq *blkg; |
ce7acfea | 167 | struct bdi_writeback_congested *wb_congested; |
f427d909 | 168 | int i, ret; |
5624a4e4 | 169 | |
cd1604fa TH |
170 | WARN_ON_ONCE(!rcu_read_lock_held()); |
171 | lockdep_assert_held(q->queue_lock); | |
172 | ||
7ee9c562 | 173 | /* blkg holds a reference to blkcg */ |
ec903c0c | 174 | if (!css_tryget_online(&blkcg->css)) { |
93e6d5d8 TH |
175 | ret = -EINVAL; |
176 | goto err_free_blkg; | |
15974993 | 177 | } |
cd1604fa | 178 | |
ce7acfea | 179 | wb_congested = wb_congested_get_create(&q->backing_dev_info, |
d93a11f1 | 180 | blkcg->css.id, GFP_NOWAIT); |
ce7acfea TH |
181 | if (!wb_congested) { |
182 | ret = -ENOMEM; | |
183 | goto err_put_css; | |
184 | } | |
185 | ||
496fb780 | 186 | /* allocate */ |
15974993 | 187 | if (!new_blkg) { |
d93a11f1 | 188 | new_blkg = blkg_alloc(blkcg, q, GFP_NOWAIT); |
15974993 | 189 | if (unlikely(!new_blkg)) { |
93e6d5d8 | 190 | ret = -ENOMEM; |
ce7acfea | 191 | goto err_put_congested; |
15974993 TH |
192 | } |
193 | } | |
194 | blkg = new_blkg; | |
ce7acfea | 195 | blkg->wb_congested = wb_congested; |
cd1604fa | 196 | |
db613670 | 197 | /* link parent */ |
3c547865 TH |
198 | if (blkcg_parent(blkcg)) { |
199 | blkg->parent = __blkg_lookup(blkcg_parent(blkcg), q, false); | |
200 | if (WARN_ON_ONCE(!blkg->parent)) { | |
2423c9c3 | 201 | ret = -EINVAL; |
ce7acfea | 202 | goto err_put_congested; |
3c547865 TH |
203 | } |
204 | blkg_get(blkg->parent); | |
205 | } | |
206 | ||
db613670 TH |
207 | /* invoke per-policy init */ |
208 | for (i = 0; i < BLKCG_MAX_POLS; i++) { | |
209 | struct blkcg_policy *pol = blkcg_policy[i]; | |
210 | ||
211 | if (blkg->pd[i] && pol->pd_init_fn) | |
a9520cd6 | 212 | pol->pd_init_fn(blkg->pd[i]); |
db613670 TH |
213 | } |
214 | ||
215 | /* insert */ | |
cd1604fa | 216 | spin_lock(&blkcg->lock); |
a637120e TH |
217 | ret = radix_tree_insert(&blkcg->blkg_tree, q->id, blkg); |
218 | if (likely(!ret)) { | |
219 | hlist_add_head_rcu(&blkg->blkcg_node, &blkcg->blkg_list); | |
220 | list_add(&blkg->q_node, &q->blkg_list); | |
f427d909 TH |
221 | |
222 | for (i = 0; i < BLKCG_MAX_POLS; i++) { | |
223 | struct blkcg_policy *pol = blkcg_policy[i]; | |
224 | ||
225 | if (blkg->pd[i] && pol->pd_online_fn) | |
a9520cd6 | 226 | pol->pd_online_fn(blkg->pd[i]); |
f427d909 | 227 | } |
a637120e | 228 | } |
f427d909 | 229 | blkg->online = true; |
cd1604fa | 230 | spin_unlock(&blkcg->lock); |
496fb780 | 231 | |
ec13b1d6 | 232 | if (!ret) |
a637120e | 233 | return blkg; |
15974993 | 234 | |
3c547865 TH |
235 | /* @blkg failed fully initialized, use the usual release path */ |
236 | blkg_put(blkg); | |
237 | return ERR_PTR(ret); | |
238 | ||
ce7acfea TH |
239 | err_put_congested: |
240 | wb_congested_put(wb_congested); | |
93e6d5d8 | 241 | err_put_css: |
496fb780 | 242 | css_put(&blkcg->css); |
93e6d5d8 | 243 | err_free_blkg: |
15974993 | 244 | blkg_free(new_blkg); |
93e6d5d8 | 245 | return ERR_PTR(ret); |
31e4c28d | 246 | } |
3c96cb32 | 247 | |
86cde6b6 TH |
248 | /** |
249 | * blkg_lookup_create - lookup blkg, try to create one if not there | |
250 | * @blkcg: blkcg of interest | |
251 | * @q: request_queue of interest | |
252 | * | |
253 | * Lookup blkg for the @blkcg - @q pair. If it doesn't exist, try to | |
3c547865 TH |
254 | * create one. blkg creation is performed recursively from blkcg_root such |
255 | * that all non-root blkg's have access to the parent blkg. This function | |
256 | * should be called under RCU read lock and @q->queue_lock. | |
86cde6b6 TH |
257 | * |
258 | * Returns pointer to the looked up or created blkg on success, ERR_PTR() | |
259 | * value on error. If @q is dead, returns ERR_PTR(-EINVAL). If @q is not | |
260 | * dead and bypassing, returns ERR_PTR(-EBUSY). | |
261 | */ | |
3c798398 TH |
262 | struct blkcg_gq *blkg_lookup_create(struct blkcg *blkcg, |
263 | struct request_queue *q) | |
3c96cb32 | 264 | { |
86cde6b6 TH |
265 | struct blkcg_gq *blkg; |
266 | ||
267 | WARN_ON_ONCE(!rcu_read_lock_held()); | |
268 | lockdep_assert_held(q->queue_lock); | |
269 | ||
3c96cb32 TH |
270 | /* |
271 | * This could be the first entry point of blkcg implementation and | |
272 | * we shouldn't allow anything to go through for a bypassing queue. | |
273 | */ | |
274 | if (unlikely(blk_queue_bypass(q))) | |
3f3299d5 | 275 | return ERR_PTR(blk_queue_dying(q) ? -EINVAL : -EBUSY); |
86cde6b6 TH |
276 | |
277 | blkg = __blkg_lookup(blkcg, q, true); | |
278 | if (blkg) | |
279 | return blkg; | |
280 | ||
3c547865 TH |
281 | /* |
282 | * Create blkgs walking down from blkcg_root to @blkcg, so that all | |
283 | * non-root blkgs have access to their parents. | |
284 | */ | |
285 | while (true) { | |
286 | struct blkcg *pos = blkcg; | |
287 | struct blkcg *parent = blkcg_parent(blkcg); | |
288 | ||
289 | while (parent && !__blkg_lookup(parent, q, false)) { | |
290 | pos = parent; | |
291 | parent = blkcg_parent(parent); | |
292 | } | |
293 | ||
294 | blkg = blkg_create(pos, q, NULL); | |
295 | if (pos == blkcg || IS_ERR(blkg)) | |
296 | return blkg; | |
297 | } | |
3c96cb32 | 298 | } |
31e4c28d | 299 | |
3c798398 | 300 | static void blkg_destroy(struct blkcg_gq *blkg) |
03aa264a | 301 | { |
3c798398 | 302 | struct blkcg *blkcg = blkg->blkcg; |
f427d909 | 303 | int i; |
03aa264a | 304 | |
27e1f9d1 | 305 | lockdep_assert_held(blkg->q->queue_lock); |
9f13ef67 | 306 | lockdep_assert_held(&blkcg->lock); |
03aa264a TH |
307 | |
308 | /* Something wrong if we are trying to remove same group twice */ | |
e8989fae | 309 | WARN_ON_ONCE(list_empty(&blkg->q_node)); |
9f13ef67 | 310 | WARN_ON_ONCE(hlist_unhashed(&blkg->blkcg_node)); |
a637120e | 311 | |
f427d909 TH |
312 | for (i = 0; i < BLKCG_MAX_POLS; i++) { |
313 | struct blkcg_policy *pol = blkcg_policy[i]; | |
314 | ||
315 | if (blkg->pd[i] && pol->pd_offline_fn) | |
a9520cd6 | 316 | pol->pd_offline_fn(blkg->pd[i]); |
f427d909 TH |
317 | } |
318 | blkg->online = false; | |
319 | ||
a637120e | 320 | radix_tree_delete(&blkcg->blkg_tree, blkg->q->id); |
e8989fae | 321 | list_del_init(&blkg->q_node); |
9f13ef67 | 322 | hlist_del_init_rcu(&blkg->blkcg_node); |
03aa264a | 323 | |
a637120e TH |
324 | /* |
325 | * Both setting lookup hint to and clearing it from @blkg are done | |
326 | * under queue_lock. If it's not pointing to @blkg now, it never | |
327 | * will. Hint assignment itself can race safely. | |
328 | */ | |
ec6c676a | 329 | if (rcu_access_pointer(blkcg->blkg_hint) == blkg) |
a637120e TH |
330 | rcu_assign_pointer(blkcg->blkg_hint, NULL); |
331 | ||
03aa264a TH |
332 | /* |
333 | * Put the reference taken at the time of creation so that when all | |
334 | * queues are gone, group can be destroyed. | |
335 | */ | |
336 | blkg_put(blkg); | |
337 | } | |
338 | ||
9f13ef67 TH |
339 | /** |
340 | * blkg_destroy_all - destroy all blkgs associated with a request_queue | |
341 | * @q: request_queue of interest | |
9f13ef67 | 342 | * |
3c96cb32 | 343 | * Destroy all blkgs associated with @q. |
9f13ef67 | 344 | */ |
3c96cb32 | 345 | static void blkg_destroy_all(struct request_queue *q) |
72e06c25 | 346 | { |
3c798398 | 347 | struct blkcg_gq *blkg, *n; |
72e06c25 | 348 | |
6d18b008 | 349 | lockdep_assert_held(q->queue_lock); |
72e06c25 | 350 | |
9f13ef67 | 351 | list_for_each_entry_safe(blkg, n, &q->blkg_list, q_node) { |
3c798398 | 352 | struct blkcg *blkcg = blkg->blkcg; |
72e06c25 | 353 | |
9f13ef67 TH |
354 | spin_lock(&blkcg->lock); |
355 | blkg_destroy(blkg); | |
356 | spin_unlock(&blkcg->lock); | |
72e06c25 TH |
357 | } |
358 | } | |
359 | ||
2a4fd070 TH |
360 | /* |
361 | * A group is RCU protected, but having an rcu lock does not mean that one | |
362 | * can access all the fields of blkg and assume these are valid. For | |
363 | * example, don't try to follow throtl_data and request queue links. | |
364 | * | |
365 | * Having a reference to blkg under an rcu allows accesses to only values | |
366 | * local to groups like group stats and group rate limits. | |
367 | */ | |
368 | void __blkg_release_rcu(struct rcu_head *rcu_head) | |
1adaf3dd | 369 | { |
2a4fd070 | 370 | struct blkcg_gq *blkg = container_of(rcu_head, struct blkcg_gq, rcu_head); |
db613670 | 371 | |
3c547865 | 372 | /* release the blkcg and parent blkg refs this blkg has been holding */ |
1adaf3dd | 373 | css_put(&blkg->blkcg->css); |
a5049a8a | 374 | if (blkg->parent) |
3c547865 | 375 | blkg_put(blkg->parent); |
1adaf3dd | 376 | |
ce7acfea TH |
377 | wb_congested_put(blkg->wb_congested); |
378 | ||
2a4fd070 | 379 | blkg_free(blkg); |
1adaf3dd | 380 | } |
2a4fd070 | 381 | EXPORT_SYMBOL_GPL(__blkg_release_rcu); |
1adaf3dd | 382 | |
a051661c TH |
383 | /* |
384 | * The next function used by blk_queue_for_each_rl(). It's a bit tricky | |
385 | * because the root blkg uses @q->root_rl instead of its own rl. | |
386 | */ | |
387 | struct request_list *__blk_queue_next_rl(struct request_list *rl, | |
388 | struct request_queue *q) | |
389 | { | |
390 | struct list_head *ent; | |
391 | struct blkcg_gq *blkg; | |
392 | ||
393 | /* | |
394 | * Determine the current blkg list_head. The first entry is | |
395 | * root_rl which is off @q->blkg_list and mapped to the head. | |
396 | */ | |
397 | if (rl == &q->root_rl) { | |
398 | ent = &q->blkg_list; | |
65c77fd9 JN |
399 | /* There are no more block groups, hence no request lists */ |
400 | if (list_empty(ent)) | |
401 | return NULL; | |
a051661c TH |
402 | } else { |
403 | blkg = container_of(rl, struct blkcg_gq, rl); | |
404 | ent = &blkg->q_node; | |
405 | } | |
406 | ||
407 | /* walk to the next list_head, skip root blkcg */ | |
408 | ent = ent->next; | |
409 | if (ent == &q->root_blkg->q_node) | |
410 | ent = ent->next; | |
411 | if (ent == &q->blkg_list) | |
412 | return NULL; | |
413 | ||
414 | blkg = container_of(ent, struct blkcg_gq, q_node); | |
415 | return &blkg->rl; | |
416 | } | |
417 | ||
182446d0 TH |
418 | static int blkcg_reset_stats(struct cgroup_subsys_state *css, |
419 | struct cftype *cftype, u64 val) | |
303a3acb | 420 | { |
182446d0 | 421 | struct blkcg *blkcg = css_to_blkcg(css); |
3c798398 | 422 | struct blkcg_gq *blkg; |
bc0d6501 | 423 | int i; |
303a3acb | 424 | |
838f13bf | 425 | mutex_lock(&blkcg_pol_mutex); |
303a3acb | 426 | spin_lock_irq(&blkcg->lock); |
997a026c TH |
427 | |
428 | /* | |
429 | * Note that stat reset is racy - it doesn't synchronize against | |
430 | * stat updates. This is a debug feature which shouldn't exist | |
431 | * anyway. If you get hit by a race, retry. | |
432 | */ | |
b67bfe0d | 433 | hlist_for_each_entry(blkg, &blkcg->blkg_list, blkcg_node) { |
8bd435b3 | 434 | for (i = 0; i < BLKCG_MAX_POLS; i++) { |
3c798398 | 435 | struct blkcg_policy *pol = blkcg_policy[i]; |
549d3aa8 | 436 | |
a9520cd6 TH |
437 | if (blkg->pd[i] && pol->pd_reset_stats_fn) |
438 | pol->pd_reset_stats_fn(blkg->pd[i]); | |
bc0d6501 | 439 | } |
303a3acb | 440 | } |
f0bdc8cd | 441 | |
303a3acb | 442 | spin_unlock_irq(&blkcg->lock); |
bc0d6501 | 443 | mutex_unlock(&blkcg_pol_mutex); |
303a3acb DS |
444 | return 0; |
445 | } | |
446 | ||
3c798398 | 447 | static const char *blkg_dev_name(struct blkcg_gq *blkg) |
303a3acb | 448 | { |
d3d32e69 TH |
449 | /* some drivers (floppy) instantiate a queue w/o disk registered */ |
450 | if (blkg->q->backing_dev_info.dev) | |
451 | return dev_name(blkg->q->backing_dev_info.dev); | |
452 | return NULL; | |
303a3acb DS |
453 | } |
454 | ||
d3d32e69 TH |
455 | /** |
456 | * blkcg_print_blkgs - helper for printing per-blkg data | |
457 | * @sf: seq_file to print to | |
458 | * @blkcg: blkcg of interest | |
459 | * @prfill: fill function to print out a blkg | |
460 | * @pol: policy in question | |
461 | * @data: data to be passed to @prfill | |
462 | * @show_total: to print out sum of prfill return values or not | |
463 | * | |
464 | * This function invokes @prfill on each blkg of @blkcg if pd for the | |
465 | * policy specified by @pol exists. @prfill is invoked with @sf, the | |
810ecfa7 TH |
466 | * policy data and @data and the matching queue lock held. If @show_total |
467 | * is %true, the sum of the return values from @prfill is printed with | |
468 | * "Total" label at the end. | |
d3d32e69 TH |
469 | * |
470 | * This is to be used to construct print functions for | |
471 | * cftype->read_seq_string method. | |
472 | */ | |
3c798398 | 473 | void blkcg_print_blkgs(struct seq_file *sf, struct blkcg *blkcg, |
f95a04af TH |
474 | u64 (*prfill)(struct seq_file *, |
475 | struct blkg_policy_data *, int), | |
3c798398 | 476 | const struct blkcg_policy *pol, int data, |
ec399347 | 477 | bool show_total) |
5624a4e4 | 478 | { |
3c798398 | 479 | struct blkcg_gq *blkg; |
d3d32e69 | 480 | u64 total = 0; |
5624a4e4 | 481 | |
810ecfa7 | 482 | rcu_read_lock(); |
ee89f812 | 483 | hlist_for_each_entry_rcu(blkg, &blkcg->blkg_list, blkcg_node) { |
810ecfa7 | 484 | spin_lock_irq(blkg->q->queue_lock); |
a2b1693b | 485 | if (blkcg_policy_enabled(blkg->q, pol)) |
f95a04af | 486 | total += prfill(sf, blkg->pd[pol->plid], data); |
810ecfa7 TH |
487 | spin_unlock_irq(blkg->q->queue_lock); |
488 | } | |
489 | rcu_read_unlock(); | |
d3d32e69 TH |
490 | |
491 | if (show_total) | |
492 | seq_printf(sf, "Total %llu\n", (unsigned long long)total); | |
493 | } | |
829fdb50 | 494 | EXPORT_SYMBOL_GPL(blkcg_print_blkgs); |
d3d32e69 TH |
495 | |
496 | /** | |
497 | * __blkg_prfill_u64 - prfill helper for a single u64 value | |
498 | * @sf: seq_file to print to | |
f95a04af | 499 | * @pd: policy private data of interest |
d3d32e69 TH |
500 | * @v: value to print |
501 | * | |
f95a04af | 502 | * Print @v to @sf for the device assocaited with @pd. |
d3d32e69 | 503 | */ |
f95a04af | 504 | u64 __blkg_prfill_u64(struct seq_file *sf, struct blkg_policy_data *pd, u64 v) |
d3d32e69 | 505 | { |
f95a04af | 506 | const char *dname = blkg_dev_name(pd->blkg); |
d3d32e69 TH |
507 | |
508 | if (!dname) | |
509 | return 0; | |
510 | ||
511 | seq_printf(sf, "%s %llu\n", dname, (unsigned long long)v); | |
512 | return v; | |
513 | } | |
829fdb50 | 514 | EXPORT_SYMBOL_GPL(__blkg_prfill_u64); |
d3d32e69 TH |
515 | |
516 | /** | |
517 | * __blkg_prfill_rwstat - prfill helper for a blkg_rwstat | |
518 | * @sf: seq_file to print to | |
f95a04af | 519 | * @pd: policy private data of interest |
d3d32e69 TH |
520 | * @rwstat: rwstat to print |
521 | * | |
f95a04af | 522 | * Print @rwstat to @sf for the device assocaited with @pd. |
d3d32e69 | 523 | */ |
f95a04af | 524 | u64 __blkg_prfill_rwstat(struct seq_file *sf, struct blkg_policy_data *pd, |
829fdb50 | 525 | const struct blkg_rwstat *rwstat) |
d3d32e69 TH |
526 | { |
527 | static const char *rwstr[] = { | |
528 | [BLKG_RWSTAT_READ] = "Read", | |
529 | [BLKG_RWSTAT_WRITE] = "Write", | |
530 | [BLKG_RWSTAT_SYNC] = "Sync", | |
531 | [BLKG_RWSTAT_ASYNC] = "Async", | |
532 | }; | |
f95a04af | 533 | const char *dname = blkg_dev_name(pd->blkg); |
d3d32e69 TH |
534 | u64 v; |
535 | int i; | |
536 | ||
537 | if (!dname) | |
538 | return 0; | |
539 | ||
540 | for (i = 0; i < BLKG_RWSTAT_NR; i++) | |
541 | seq_printf(sf, "%s %s %llu\n", dname, rwstr[i], | |
24bdb8ef | 542 | (unsigned long long)atomic64_read(&rwstat->aux_cnt[i])); |
d3d32e69 | 543 | |
24bdb8ef TH |
544 | v = atomic64_read(&rwstat->aux_cnt[BLKG_RWSTAT_READ]) + |
545 | atomic64_read(&rwstat->aux_cnt[BLKG_RWSTAT_WRITE]); | |
d3d32e69 TH |
546 | seq_printf(sf, "%s Total %llu\n", dname, (unsigned long long)v); |
547 | return v; | |
548 | } | |
b50da39f | 549 | EXPORT_SYMBOL_GPL(__blkg_prfill_rwstat); |
d3d32e69 | 550 | |
5bc4afb1 TH |
551 | /** |
552 | * blkg_prfill_stat - prfill callback for blkg_stat | |
553 | * @sf: seq_file to print to | |
f95a04af TH |
554 | * @pd: policy private data of interest |
555 | * @off: offset to the blkg_stat in @pd | |
5bc4afb1 TH |
556 | * |
557 | * prfill callback for printing a blkg_stat. | |
558 | */ | |
f95a04af | 559 | u64 blkg_prfill_stat(struct seq_file *sf, struct blkg_policy_data *pd, int off) |
d3d32e69 | 560 | { |
f95a04af | 561 | return __blkg_prfill_u64(sf, pd, blkg_stat_read((void *)pd + off)); |
d3d32e69 | 562 | } |
5bc4afb1 | 563 | EXPORT_SYMBOL_GPL(blkg_prfill_stat); |
d3d32e69 | 564 | |
5bc4afb1 TH |
565 | /** |
566 | * blkg_prfill_rwstat - prfill callback for blkg_rwstat | |
567 | * @sf: seq_file to print to | |
f95a04af TH |
568 | * @pd: policy private data of interest |
569 | * @off: offset to the blkg_rwstat in @pd | |
5bc4afb1 TH |
570 | * |
571 | * prfill callback for printing a blkg_rwstat. | |
572 | */ | |
f95a04af TH |
573 | u64 blkg_prfill_rwstat(struct seq_file *sf, struct blkg_policy_data *pd, |
574 | int off) | |
d3d32e69 | 575 | { |
f95a04af | 576 | struct blkg_rwstat rwstat = blkg_rwstat_read((void *)pd + off); |
d3d32e69 | 577 | |
f95a04af | 578 | return __blkg_prfill_rwstat(sf, pd, &rwstat); |
d3d32e69 | 579 | } |
5bc4afb1 | 580 | EXPORT_SYMBOL_GPL(blkg_prfill_rwstat); |
d3d32e69 | 581 | |
16b3de66 TH |
582 | /** |
583 | * blkg_stat_recursive_sum - collect hierarchical blkg_stat | |
f12c74ca TH |
584 | * @blkg: blkg of interest |
585 | * @pol: blkcg_policy which contains the blkg_stat | |
586 | * @off: offset to the blkg_stat in blkg_policy_data or @blkg | |
16b3de66 | 587 | * |
f12c74ca TH |
588 | * Collect the blkg_stat specified by @blkg, @pol and @off and all its |
589 | * online descendants and their aux counts. The caller must be holding the | |
590 | * queue lock for online tests. | |
591 | * | |
592 | * If @pol is NULL, blkg_stat is at @off bytes into @blkg; otherwise, it is | |
593 | * at @off bytes into @blkg's blkg_policy_data of the policy. | |
16b3de66 | 594 | */ |
f12c74ca TH |
595 | u64 blkg_stat_recursive_sum(struct blkcg_gq *blkg, |
596 | struct blkcg_policy *pol, int off) | |
16b3de66 | 597 | { |
16b3de66 | 598 | struct blkcg_gq *pos_blkg; |
492eb21b | 599 | struct cgroup_subsys_state *pos_css; |
bd8815a6 | 600 | u64 sum = 0; |
16b3de66 | 601 | |
f12c74ca | 602 | lockdep_assert_held(blkg->q->queue_lock); |
16b3de66 | 603 | |
16b3de66 | 604 | rcu_read_lock(); |
f12c74ca TH |
605 | blkg_for_each_descendant_pre(pos_blkg, pos_css, blkg) { |
606 | struct blkg_stat *stat; | |
607 | ||
608 | if (!pos_blkg->online) | |
609 | continue; | |
16b3de66 | 610 | |
f12c74ca TH |
611 | if (pol) |
612 | stat = (void *)blkg_to_pd(pos_blkg, pol) + off; | |
613 | else | |
614 | stat = (void *)blkg + off; | |
615 | ||
616 | sum += blkg_stat_read(stat) + atomic64_read(&stat->aux_cnt); | |
16b3de66 TH |
617 | } |
618 | rcu_read_unlock(); | |
619 | ||
620 | return sum; | |
621 | } | |
622 | EXPORT_SYMBOL_GPL(blkg_stat_recursive_sum); | |
623 | ||
624 | /** | |
625 | * blkg_rwstat_recursive_sum - collect hierarchical blkg_rwstat | |
f12c74ca TH |
626 | * @blkg: blkg of interest |
627 | * @pol: blkcg_policy which contains the blkg_rwstat | |
628 | * @off: offset to the blkg_rwstat in blkg_policy_data or @blkg | |
16b3de66 | 629 | * |
f12c74ca TH |
630 | * Collect the blkg_rwstat specified by @blkg, @pol and @off and all its |
631 | * online descendants and their aux counts. The caller must be holding the | |
632 | * queue lock for online tests. | |
633 | * | |
634 | * If @pol is NULL, blkg_rwstat is at @off bytes into @blkg; otherwise, it | |
635 | * is at @off bytes into @blkg's blkg_policy_data of the policy. | |
16b3de66 | 636 | */ |
f12c74ca TH |
637 | struct blkg_rwstat blkg_rwstat_recursive_sum(struct blkcg_gq *blkg, |
638 | struct blkcg_policy *pol, int off) | |
16b3de66 | 639 | { |
16b3de66 | 640 | struct blkcg_gq *pos_blkg; |
492eb21b | 641 | struct cgroup_subsys_state *pos_css; |
bd8815a6 | 642 | struct blkg_rwstat sum = { }; |
16b3de66 TH |
643 | int i; |
644 | ||
f12c74ca | 645 | lockdep_assert_held(blkg->q->queue_lock); |
16b3de66 | 646 | |
16b3de66 | 647 | rcu_read_lock(); |
f12c74ca TH |
648 | blkg_for_each_descendant_pre(pos_blkg, pos_css, blkg) { |
649 | struct blkg_rwstat *rwstat, tmp; | |
16b3de66 TH |
650 | |
651 | if (!pos_blkg->online) | |
652 | continue; | |
653 | ||
f12c74ca TH |
654 | if (pol) |
655 | rwstat = (void *)blkg_to_pd(pos_blkg, pol) + off; | |
656 | else | |
657 | rwstat = (void *)pos_blkg + off; | |
658 | ||
16b3de66 TH |
659 | tmp = blkg_rwstat_read(rwstat); |
660 | ||
661 | for (i = 0; i < BLKG_RWSTAT_NR; i++) | |
24bdb8ef TH |
662 | atomic64_add(atomic64_read(&tmp.aux_cnt[i]) + |
663 | atomic64_read(&rwstat->aux_cnt[i]), | |
664 | &sum.aux_cnt[i]); | |
16b3de66 TH |
665 | } |
666 | rcu_read_unlock(); | |
667 | ||
668 | return sum; | |
669 | } | |
670 | EXPORT_SYMBOL_GPL(blkg_rwstat_recursive_sum); | |
671 | ||
3a8b31d3 TH |
672 | /** |
673 | * blkg_conf_prep - parse and prepare for per-blkg config update | |
674 | * @blkcg: target block cgroup | |
da8b0662 | 675 | * @pol: target policy |
3a8b31d3 TH |
676 | * @input: input string |
677 | * @ctx: blkg_conf_ctx to be filled | |
678 | * | |
679 | * Parse per-blkg config update from @input and initialize @ctx with the | |
680 | * result. @ctx->blkg points to the blkg to be updated and @ctx->v the new | |
da8b0662 TH |
681 | * value. This function returns with RCU read lock and queue lock held and |
682 | * must be paired with blkg_conf_finish(). | |
3a8b31d3 | 683 | */ |
3c798398 TH |
684 | int blkg_conf_prep(struct blkcg *blkcg, const struct blkcg_policy *pol, |
685 | const char *input, struct blkg_conf_ctx *ctx) | |
da8b0662 | 686 | __acquires(rcu) __acquires(disk->queue->queue_lock) |
34d0f179 | 687 | { |
3a8b31d3 | 688 | struct gendisk *disk; |
3c798398 | 689 | struct blkcg_gq *blkg; |
726fa694 TH |
690 | unsigned int major, minor; |
691 | unsigned long long v; | |
692 | int part, ret; | |
34d0f179 | 693 | |
726fa694 TH |
694 | if (sscanf(input, "%u:%u %llu", &major, &minor, &v) != 3) |
695 | return -EINVAL; | |
3a8b31d3 | 696 | |
726fa694 | 697 | disk = get_gendisk(MKDEV(major, minor), &part); |
5f6c2d2b | 698 | if (!disk) |
726fa694 | 699 | return -EINVAL; |
5f6c2d2b TH |
700 | if (part) { |
701 | put_disk(disk); | |
702 | return -EINVAL; | |
703 | } | |
e56da7e2 TH |
704 | |
705 | rcu_read_lock(); | |
4bfd482e | 706 | spin_lock_irq(disk->queue->queue_lock); |
da8b0662 | 707 | |
a2b1693b | 708 | if (blkcg_policy_enabled(disk->queue, pol)) |
3c96cb32 | 709 | blkg = blkg_lookup_create(blkcg, disk->queue); |
a2b1693b TH |
710 | else |
711 | blkg = ERR_PTR(-EINVAL); | |
e56da7e2 | 712 | |
4bfd482e TH |
713 | if (IS_ERR(blkg)) { |
714 | ret = PTR_ERR(blkg); | |
3a8b31d3 | 715 | rcu_read_unlock(); |
da8b0662 | 716 | spin_unlock_irq(disk->queue->queue_lock); |
3a8b31d3 TH |
717 | put_disk(disk); |
718 | /* | |
719 | * If queue was bypassing, we should retry. Do so after a | |
720 | * short msleep(). It isn't strictly necessary but queue | |
721 | * can be bypassing for some time and it's always nice to | |
722 | * avoid busy looping. | |
723 | */ | |
724 | if (ret == -EBUSY) { | |
725 | msleep(10); | |
726 | ret = restart_syscall(); | |
7702e8f4 | 727 | } |
726fa694 | 728 | return ret; |
062a644d | 729 | } |
3a8b31d3 TH |
730 | |
731 | ctx->disk = disk; | |
732 | ctx->blkg = blkg; | |
726fa694 TH |
733 | ctx->v = v; |
734 | return 0; | |
34d0f179 | 735 | } |
829fdb50 | 736 | EXPORT_SYMBOL_GPL(blkg_conf_prep); |
34d0f179 | 737 | |
3a8b31d3 TH |
738 | /** |
739 | * blkg_conf_finish - finish up per-blkg config update | |
740 | * @ctx: blkg_conf_ctx intiailized by blkg_conf_prep() | |
741 | * | |
742 | * Finish up after per-blkg config update. This function must be paired | |
743 | * with blkg_conf_prep(). | |
744 | */ | |
829fdb50 | 745 | void blkg_conf_finish(struct blkg_conf_ctx *ctx) |
da8b0662 | 746 | __releases(ctx->disk->queue->queue_lock) __releases(rcu) |
34d0f179 | 747 | { |
da8b0662 | 748 | spin_unlock_irq(ctx->disk->queue->queue_lock); |
3a8b31d3 TH |
749 | rcu_read_unlock(); |
750 | put_disk(ctx->disk); | |
34d0f179 | 751 | } |
829fdb50 | 752 | EXPORT_SYMBOL_GPL(blkg_conf_finish); |
34d0f179 | 753 | |
3c798398 | 754 | struct cftype blkcg_files[] = { |
84c124da DS |
755 | { |
756 | .name = "reset_stats", | |
3c798398 | 757 | .write_u64 = blkcg_reset_stats, |
22084190 | 758 | }, |
4baf6e33 | 759 | { } /* terminate */ |
31e4c28d VG |
760 | }; |
761 | ||
9f13ef67 | 762 | /** |
92fb9748 | 763 | * blkcg_css_offline - cgroup css_offline callback |
eb95419b | 764 | * @css: css of interest |
9f13ef67 | 765 | * |
eb95419b TH |
766 | * This function is called when @css is about to go away and responsible |
767 | * for shooting down all blkgs associated with @css. blkgs should be | |
9f13ef67 TH |
768 | * removed while holding both q and blkcg locks. As blkcg lock is nested |
769 | * inside q lock, this function performs reverse double lock dancing. | |
770 | * | |
771 | * This is the blkcg counterpart of ioc_release_fn(). | |
772 | */ | |
eb95419b | 773 | static void blkcg_css_offline(struct cgroup_subsys_state *css) |
31e4c28d | 774 | { |
eb95419b | 775 | struct blkcg *blkcg = css_to_blkcg(css); |
b1c35769 | 776 | |
9f13ef67 | 777 | spin_lock_irq(&blkcg->lock); |
7ee9c562 | 778 | |
9f13ef67 | 779 | while (!hlist_empty(&blkcg->blkg_list)) { |
3c798398 TH |
780 | struct blkcg_gq *blkg = hlist_entry(blkcg->blkg_list.first, |
781 | struct blkcg_gq, blkcg_node); | |
c875f4d0 | 782 | struct request_queue *q = blkg->q; |
b1c35769 | 783 | |
9f13ef67 TH |
784 | if (spin_trylock(q->queue_lock)) { |
785 | blkg_destroy(blkg); | |
786 | spin_unlock(q->queue_lock); | |
787 | } else { | |
788 | spin_unlock_irq(&blkcg->lock); | |
9f13ef67 | 789 | cpu_relax(); |
a5567932 | 790 | spin_lock_irq(&blkcg->lock); |
0f3942a3 | 791 | } |
9f13ef67 | 792 | } |
b1c35769 | 793 | |
9f13ef67 | 794 | spin_unlock_irq(&blkcg->lock); |
52ebea74 TH |
795 | |
796 | wb_blkcg_offline(blkcg); | |
7ee9c562 TH |
797 | } |
798 | ||
eb95419b | 799 | static void blkcg_css_free(struct cgroup_subsys_state *css) |
7ee9c562 | 800 | { |
eb95419b | 801 | struct blkcg *blkcg = css_to_blkcg(css); |
bc915e61 | 802 | int i; |
7ee9c562 | 803 | |
7876f930 | 804 | mutex_lock(&blkcg_pol_mutex); |
e4a9bde9 | 805 | |
7876f930 | 806 | list_del(&blkcg->all_blkcgs_node); |
7876f930 | 807 | |
bc915e61 | 808 | for (i = 0; i < BLKCG_MAX_POLS; i++) |
e4a9bde9 TH |
809 | if (blkcg->cpd[i]) |
810 | blkcg_policy[i]->cpd_free_fn(blkcg->cpd[i]); | |
811 | ||
812 | mutex_unlock(&blkcg_pol_mutex); | |
813 | ||
bc915e61 | 814 | kfree(blkcg); |
31e4c28d VG |
815 | } |
816 | ||
eb95419b TH |
817 | static struct cgroup_subsys_state * |
818 | blkcg_css_alloc(struct cgroup_subsys_state *parent_css) | |
31e4c28d | 819 | { |
3c798398 | 820 | struct blkcg *blkcg; |
e48453c3 AA |
821 | struct cgroup_subsys_state *ret; |
822 | int i; | |
31e4c28d | 823 | |
7876f930 TH |
824 | mutex_lock(&blkcg_pol_mutex); |
825 | ||
eb95419b | 826 | if (!parent_css) { |
3c798398 | 827 | blkcg = &blkcg_root; |
bc915e61 TH |
828 | } else { |
829 | blkcg = kzalloc(sizeof(*blkcg), GFP_KERNEL); | |
830 | if (!blkcg) { | |
831 | ret = ERR_PTR(-ENOMEM); | |
832 | goto free_blkcg; | |
833 | } | |
e48453c3 AA |
834 | } |
835 | ||
836 | for (i = 0; i < BLKCG_MAX_POLS ; i++) { | |
837 | struct blkcg_policy *pol = blkcg_policy[i]; | |
838 | struct blkcg_policy_data *cpd; | |
839 | ||
840 | /* | |
841 | * If the policy hasn't been attached yet, wait for it | |
842 | * to be attached before doing anything else. Otherwise, | |
843 | * check if the policy requires any specific per-cgroup | |
844 | * data: if it does, allocate and initialize it. | |
845 | */ | |
e4a9bde9 | 846 | if (!pol || !pol->cpd_alloc_fn) |
e48453c3 AA |
847 | continue; |
848 | ||
e4a9bde9 | 849 | cpd = pol->cpd_alloc_fn(GFP_KERNEL); |
e48453c3 AA |
850 | if (!cpd) { |
851 | ret = ERR_PTR(-ENOMEM); | |
852 | goto free_pd_blkcg; | |
853 | } | |
81437648 TH |
854 | blkcg->cpd[i] = cpd; |
855 | cpd->blkcg = blkcg; | |
e48453c3 | 856 | cpd->plid = i; |
e4a9bde9 TH |
857 | if (pol->cpd_init_fn) |
858 | pol->cpd_init_fn(cpd); | |
e48453c3 | 859 | } |
31e4c28d | 860 | |
31e4c28d | 861 | spin_lock_init(&blkcg->lock); |
d93a11f1 | 862 | INIT_RADIX_TREE(&blkcg->blkg_tree, GFP_NOWAIT); |
31e4c28d | 863 | INIT_HLIST_HEAD(&blkcg->blkg_list); |
52ebea74 TH |
864 | #ifdef CONFIG_CGROUP_WRITEBACK |
865 | INIT_LIST_HEAD(&blkcg->cgwb_list); | |
866 | #endif | |
7876f930 TH |
867 | list_add_tail(&blkcg->all_blkcgs_node, &all_blkcgs); |
868 | ||
869 | mutex_unlock(&blkcg_pol_mutex); | |
31e4c28d | 870 | return &blkcg->css; |
e48453c3 AA |
871 | |
872 | free_pd_blkcg: | |
873 | for (i--; i >= 0; i--) | |
e4a9bde9 TH |
874 | if (blkcg->cpd[i]) |
875 | blkcg_policy[i]->cpd_free_fn(blkcg->cpd[i]); | |
e48453c3 AA |
876 | free_blkcg: |
877 | kfree(blkcg); | |
7876f930 | 878 | mutex_unlock(&blkcg_pol_mutex); |
e48453c3 | 879 | return ret; |
31e4c28d VG |
880 | } |
881 | ||
5efd6113 TH |
882 | /** |
883 | * blkcg_init_queue - initialize blkcg part of request queue | |
884 | * @q: request_queue to initialize | |
885 | * | |
886 | * Called from blk_alloc_queue_node(). Responsible for initializing blkcg | |
887 | * part of new request_queue @q. | |
888 | * | |
889 | * RETURNS: | |
890 | * 0 on success, -errno on failure. | |
891 | */ | |
892 | int blkcg_init_queue(struct request_queue *q) | |
893 | { | |
ec13b1d6 TH |
894 | struct blkcg_gq *new_blkg, *blkg; |
895 | bool preloaded; | |
896 | int ret; | |
897 | ||
898 | new_blkg = blkg_alloc(&blkcg_root, q, GFP_KERNEL); | |
899 | if (!new_blkg) | |
900 | return -ENOMEM; | |
901 | ||
902 | preloaded = !radix_tree_preload(GFP_KERNEL); | |
5efd6113 | 903 | |
ec13b1d6 TH |
904 | /* |
905 | * Make sure the root blkg exists and count the existing blkgs. As | |
906 | * @q is bypassing at this point, blkg_lookup_create() can't be | |
907 | * used. Open code insertion. | |
908 | */ | |
909 | rcu_read_lock(); | |
910 | spin_lock_irq(q->queue_lock); | |
911 | blkg = blkg_create(&blkcg_root, q, new_blkg); | |
912 | spin_unlock_irq(q->queue_lock); | |
913 | rcu_read_unlock(); | |
914 | ||
915 | if (preloaded) | |
916 | radix_tree_preload_end(); | |
917 | ||
918 | if (IS_ERR(blkg)) { | |
994b7832 | 919 | blkg_free(new_blkg); |
ec13b1d6 TH |
920 | return PTR_ERR(blkg); |
921 | } | |
922 | ||
923 | q->root_blkg = blkg; | |
924 | q->root_rl.blkg = blkg; | |
5efd6113 | 925 | |
ec13b1d6 TH |
926 | ret = blk_throtl_init(q); |
927 | if (ret) { | |
928 | spin_lock_irq(q->queue_lock); | |
929 | blkg_destroy_all(q); | |
930 | spin_unlock_irq(q->queue_lock); | |
931 | } | |
932 | return ret; | |
5efd6113 TH |
933 | } |
934 | ||
935 | /** | |
936 | * blkcg_drain_queue - drain blkcg part of request_queue | |
937 | * @q: request_queue to drain | |
938 | * | |
939 | * Called from blk_drain_queue(). Responsible for draining blkcg part. | |
940 | */ | |
941 | void blkcg_drain_queue(struct request_queue *q) | |
942 | { | |
943 | lockdep_assert_held(q->queue_lock); | |
944 | ||
0b462c89 TH |
945 | /* |
946 | * @q could be exiting and already have destroyed all blkgs as | |
947 | * indicated by NULL root_blkg. If so, don't confuse policies. | |
948 | */ | |
949 | if (!q->root_blkg) | |
950 | return; | |
951 | ||
5efd6113 TH |
952 | blk_throtl_drain(q); |
953 | } | |
954 | ||
955 | /** | |
956 | * blkcg_exit_queue - exit and release blkcg part of request_queue | |
957 | * @q: request_queue being released | |
958 | * | |
959 | * Called from blk_release_queue(). Responsible for exiting blkcg part. | |
960 | */ | |
961 | void blkcg_exit_queue(struct request_queue *q) | |
962 | { | |
6d18b008 | 963 | spin_lock_irq(q->queue_lock); |
3c96cb32 | 964 | blkg_destroy_all(q); |
6d18b008 TH |
965 | spin_unlock_irq(q->queue_lock); |
966 | ||
5efd6113 TH |
967 | blk_throtl_exit(q); |
968 | } | |
969 | ||
31e4c28d VG |
970 | /* |
971 | * We cannot support shared io contexts, as we have no mean to support | |
972 | * two tasks with the same ioc in two different groups without major rework | |
973 | * of the main cic data structures. For now we allow a task to change | |
974 | * its cgroup only if it's the only owner of its ioc. | |
975 | */ | |
eb95419b TH |
976 | static int blkcg_can_attach(struct cgroup_subsys_state *css, |
977 | struct cgroup_taskset *tset) | |
31e4c28d | 978 | { |
bb9d97b6 | 979 | struct task_struct *task; |
31e4c28d VG |
980 | struct io_context *ioc; |
981 | int ret = 0; | |
982 | ||
983 | /* task_lock() is needed to avoid races with exit_io_context() */ | |
924f0d9a | 984 | cgroup_taskset_for_each(task, tset) { |
bb9d97b6 TH |
985 | task_lock(task); |
986 | ioc = task->io_context; | |
987 | if (ioc && atomic_read(&ioc->nr_tasks) > 1) | |
988 | ret = -EINVAL; | |
989 | task_unlock(task); | |
990 | if (ret) | |
991 | break; | |
992 | } | |
31e4c28d VG |
993 | return ret; |
994 | } | |
995 | ||
073219e9 | 996 | struct cgroup_subsys blkio_cgrp_subsys = { |
92fb9748 TH |
997 | .css_alloc = blkcg_css_alloc, |
998 | .css_offline = blkcg_css_offline, | |
999 | .css_free = blkcg_css_free, | |
3c798398 | 1000 | .can_attach = blkcg_can_attach, |
5577964e | 1001 | .legacy_cftypes = blkcg_files, |
1ced953b TH |
1002 | #ifdef CONFIG_MEMCG |
1003 | /* | |
1004 | * This ensures that, if available, memcg is automatically enabled | |
1005 | * together on the default hierarchy so that the owner cgroup can | |
1006 | * be retrieved from writeback pages. | |
1007 | */ | |
1008 | .depends_on = 1 << memory_cgrp_id, | |
1009 | #endif | |
676f7c8f | 1010 | }; |
073219e9 | 1011 | EXPORT_SYMBOL_GPL(blkio_cgrp_subsys); |
676f7c8f | 1012 | |
a2b1693b TH |
1013 | /** |
1014 | * blkcg_activate_policy - activate a blkcg policy on a request_queue | |
1015 | * @q: request_queue of interest | |
1016 | * @pol: blkcg policy to activate | |
1017 | * | |
1018 | * Activate @pol on @q. Requires %GFP_KERNEL context. @q goes through | |
1019 | * bypass mode to populate its blkgs with policy_data for @pol. | |
1020 | * | |
1021 | * Activation happens with @q bypassed, so nobody would be accessing blkgs | |
1022 | * from IO path. Update of each blkg is protected by both queue and blkcg | |
1023 | * locks so that holding either lock and testing blkcg_policy_enabled() is | |
1024 | * always enough for dereferencing policy data. | |
1025 | * | |
1026 | * The caller is responsible for synchronizing [de]activations and policy | |
1027 | * [un]registerations. Returns 0 on success, -errno on failure. | |
1028 | */ | |
1029 | int blkcg_activate_policy(struct request_queue *q, | |
3c798398 | 1030 | const struct blkcg_policy *pol) |
a2b1693b | 1031 | { |
4c55f4f9 | 1032 | struct blkg_policy_data *pd_prealloc = NULL; |
ec13b1d6 | 1033 | struct blkcg_gq *blkg; |
4c55f4f9 | 1034 | int ret; |
a2b1693b TH |
1035 | |
1036 | if (blkcg_policy_enabled(q, pol)) | |
1037 | return 0; | |
1038 | ||
1039 | blk_queue_bypass_start(q); | |
4c55f4f9 TH |
1040 | pd_prealloc: |
1041 | if (!pd_prealloc) { | |
001bea73 | 1042 | pd_prealloc = pol->pd_alloc_fn(GFP_KERNEL, q->node); |
4c55f4f9 | 1043 | if (!pd_prealloc) { |
a2b1693b | 1044 | ret = -ENOMEM; |
4c55f4f9 | 1045 | goto out_bypass_end; |
a2b1693b | 1046 | } |
a2b1693b TH |
1047 | } |
1048 | ||
a2b1693b TH |
1049 | spin_lock_irq(q->queue_lock); |
1050 | ||
1051 | list_for_each_entry(blkg, &q->blkg_list, q_node) { | |
4c55f4f9 TH |
1052 | struct blkg_policy_data *pd; |
1053 | ||
1054 | if (blkg->pd[pol->plid]) | |
1055 | continue; | |
a2b1693b | 1056 | |
001bea73 | 1057 | pd = pol->pd_alloc_fn(GFP_NOWAIT, q->node); |
4c55f4f9 TH |
1058 | if (!pd) |
1059 | swap(pd, pd_prealloc); | |
1060 | if (!pd) { | |
1061 | spin_unlock_irq(q->queue_lock); | |
1062 | goto pd_prealloc; | |
1063 | } | |
a2b1693b TH |
1064 | |
1065 | blkg->pd[pol->plid] = pd; | |
1066 | pd->blkg = blkg; | |
b276a876 | 1067 | pd->plid = pol->plid; |
3e418710 | 1068 | if (pol->pd_init_fn) |
a9520cd6 | 1069 | pol->pd_init_fn(pd); |
a2b1693b TH |
1070 | } |
1071 | ||
1072 | __set_bit(pol->plid, q->blkcg_pols); | |
1073 | ret = 0; | |
4c55f4f9 | 1074 | |
a2b1693b | 1075 | spin_unlock_irq(q->queue_lock); |
4c55f4f9 | 1076 | out_bypass_end: |
a2b1693b | 1077 | blk_queue_bypass_end(q); |
001bea73 TH |
1078 | if (pd_prealloc) |
1079 | pol->pd_free_fn(pd_prealloc); | |
a2b1693b TH |
1080 | return ret; |
1081 | } | |
1082 | EXPORT_SYMBOL_GPL(blkcg_activate_policy); | |
1083 | ||
1084 | /** | |
1085 | * blkcg_deactivate_policy - deactivate a blkcg policy on a request_queue | |
1086 | * @q: request_queue of interest | |
1087 | * @pol: blkcg policy to deactivate | |
1088 | * | |
1089 | * Deactivate @pol on @q. Follows the same synchronization rules as | |
1090 | * blkcg_activate_policy(). | |
1091 | */ | |
1092 | void blkcg_deactivate_policy(struct request_queue *q, | |
3c798398 | 1093 | const struct blkcg_policy *pol) |
a2b1693b | 1094 | { |
3c798398 | 1095 | struct blkcg_gq *blkg; |
a2b1693b TH |
1096 | |
1097 | if (!blkcg_policy_enabled(q, pol)) | |
1098 | return; | |
1099 | ||
1100 | blk_queue_bypass_start(q); | |
1101 | spin_lock_irq(q->queue_lock); | |
1102 | ||
1103 | __clear_bit(pol->plid, q->blkcg_pols); | |
1104 | ||
1105 | list_for_each_entry(blkg, &q->blkg_list, q_node) { | |
1106 | /* grab blkcg lock too while removing @pd from @blkg */ | |
1107 | spin_lock(&blkg->blkcg->lock); | |
1108 | ||
001bea73 | 1109 | if (blkg->pd[pol->plid]) { |
a9520cd6 TH |
1110 | if (pol->pd_offline_fn) |
1111 | pol->pd_offline_fn(blkg->pd[pol->plid]); | |
001bea73 TH |
1112 | pol->pd_free_fn(blkg->pd[pol->plid]); |
1113 | blkg->pd[pol->plid] = NULL; | |
1114 | } | |
a2b1693b TH |
1115 | |
1116 | spin_unlock(&blkg->blkcg->lock); | |
1117 | } | |
1118 | ||
1119 | spin_unlock_irq(q->queue_lock); | |
1120 | blk_queue_bypass_end(q); | |
1121 | } | |
1122 | EXPORT_SYMBOL_GPL(blkcg_deactivate_policy); | |
1123 | ||
8bd435b3 | 1124 | /** |
3c798398 TH |
1125 | * blkcg_policy_register - register a blkcg policy |
1126 | * @pol: blkcg policy to register | |
8bd435b3 | 1127 | * |
3c798398 TH |
1128 | * Register @pol with blkcg core. Might sleep and @pol may be modified on |
1129 | * successful registration. Returns 0 on success and -errno on failure. | |
8bd435b3 | 1130 | */ |
d5bf0291 | 1131 | int blkcg_policy_register(struct blkcg_policy *pol) |
3e252066 | 1132 | { |
06b285bd | 1133 | struct blkcg *blkcg; |
8bd435b3 | 1134 | int i, ret; |
e8989fae | 1135 | |
838f13bf | 1136 | mutex_lock(&blkcg_pol_register_mutex); |
bc0d6501 TH |
1137 | mutex_lock(&blkcg_pol_mutex); |
1138 | ||
8bd435b3 TH |
1139 | /* find an empty slot */ |
1140 | ret = -ENOSPC; | |
1141 | for (i = 0; i < BLKCG_MAX_POLS; i++) | |
3c798398 | 1142 | if (!blkcg_policy[i]) |
8bd435b3 TH |
1143 | break; |
1144 | if (i >= BLKCG_MAX_POLS) | |
838f13bf | 1145 | goto err_unlock; |
035d10b2 | 1146 | |
06b285bd | 1147 | /* register @pol */ |
3c798398 | 1148 | pol->plid = i; |
06b285bd TH |
1149 | blkcg_policy[pol->plid] = pol; |
1150 | ||
1151 | /* allocate and install cpd's */ | |
e4a9bde9 | 1152 | if (pol->cpd_alloc_fn) { |
06b285bd TH |
1153 | list_for_each_entry(blkcg, &all_blkcgs, all_blkcgs_node) { |
1154 | struct blkcg_policy_data *cpd; | |
1155 | ||
e4a9bde9 | 1156 | cpd = pol->cpd_alloc_fn(GFP_KERNEL); |
06b285bd TH |
1157 | if (!cpd) { |
1158 | mutex_unlock(&blkcg_pol_mutex); | |
1159 | goto err_free_cpds; | |
1160 | } | |
1161 | ||
81437648 TH |
1162 | blkcg->cpd[pol->plid] = cpd; |
1163 | cpd->blkcg = blkcg; | |
06b285bd | 1164 | cpd->plid = pol->plid; |
81437648 | 1165 | pol->cpd_init_fn(cpd); |
06b285bd TH |
1166 | } |
1167 | } | |
1168 | ||
838f13bf | 1169 | mutex_unlock(&blkcg_pol_mutex); |
8bd435b3 | 1170 | |
8bd435b3 | 1171 | /* everything is in place, add intf files for the new policy */ |
3c798398 | 1172 | if (pol->cftypes) |
2cf669a5 TH |
1173 | WARN_ON(cgroup_add_legacy_cftypes(&blkio_cgrp_subsys, |
1174 | pol->cftypes)); | |
838f13bf TH |
1175 | mutex_unlock(&blkcg_pol_register_mutex); |
1176 | return 0; | |
1177 | ||
06b285bd | 1178 | err_free_cpds: |
e4a9bde9 | 1179 | if (pol->cpd_alloc_fn) { |
06b285bd | 1180 | list_for_each_entry(blkcg, &all_blkcgs, all_blkcgs_node) { |
e4a9bde9 TH |
1181 | if (blkcg->cpd[pol->plid]) { |
1182 | pol->cpd_free_fn(blkcg->cpd[pol->plid]); | |
1183 | blkcg->cpd[pol->plid] = NULL; | |
1184 | } | |
06b285bd TH |
1185 | } |
1186 | } | |
1187 | blkcg_policy[pol->plid] = NULL; | |
838f13bf | 1188 | err_unlock: |
bc0d6501 | 1189 | mutex_unlock(&blkcg_pol_mutex); |
838f13bf | 1190 | mutex_unlock(&blkcg_pol_register_mutex); |
8bd435b3 | 1191 | return ret; |
3e252066 | 1192 | } |
3c798398 | 1193 | EXPORT_SYMBOL_GPL(blkcg_policy_register); |
3e252066 | 1194 | |
8bd435b3 | 1195 | /** |
3c798398 TH |
1196 | * blkcg_policy_unregister - unregister a blkcg policy |
1197 | * @pol: blkcg policy to unregister | |
8bd435b3 | 1198 | * |
3c798398 | 1199 | * Undo blkcg_policy_register(@pol). Might sleep. |
8bd435b3 | 1200 | */ |
3c798398 | 1201 | void blkcg_policy_unregister(struct blkcg_policy *pol) |
3e252066 | 1202 | { |
06b285bd TH |
1203 | struct blkcg *blkcg; |
1204 | ||
838f13bf | 1205 | mutex_lock(&blkcg_pol_register_mutex); |
bc0d6501 | 1206 | |
3c798398 | 1207 | if (WARN_ON(blkcg_policy[pol->plid] != pol)) |
8bd435b3 TH |
1208 | goto out_unlock; |
1209 | ||
1210 | /* kill the intf files first */ | |
3c798398 | 1211 | if (pol->cftypes) |
2bb566cb | 1212 | cgroup_rm_cftypes(pol->cftypes); |
44ea53de | 1213 | |
06b285bd | 1214 | /* remove cpds and unregister */ |
838f13bf | 1215 | mutex_lock(&blkcg_pol_mutex); |
06b285bd | 1216 | |
e4a9bde9 | 1217 | if (pol->cpd_alloc_fn) { |
06b285bd | 1218 | list_for_each_entry(blkcg, &all_blkcgs, all_blkcgs_node) { |
e4a9bde9 TH |
1219 | if (blkcg->cpd[pol->plid]) { |
1220 | pol->cpd_free_fn(blkcg->cpd[pol->plid]); | |
1221 | blkcg->cpd[pol->plid] = NULL; | |
1222 | } | |
06b285bd TH |
1223 | } |
1224 | } | |
3c798398 | 1225 | blkcg_policy[pol->plid] = NULL; |
06b285bd | 1226 | |
bc0d6501 | 1227 | mutex_unlock(&blkcg_pol_mutex); |
838f13bf TH |
1228 | out_unlock: |
1229 | mutex_unlock(&blkcg_pol_register_mutex); | |
3e252066 | 1230 | } |
3c798398 | 1231 | EXPORT_SYMBOL_GPL(blkcg_policy_unregister); |