]>
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], | |
542 | (unsigned long long)rwstat->cnt[i]); | |
543 | ||
544 | v = rwstat->cnt[BLKG_RWSTAT_READ] + rwstat->cnt[BLKG_RWSTAT_WRITE]; | |
545 | seq_printf(sf, "%s Total %llu\n", dname, (unsigned long long)v); | |
546 | return v; | |
547 | } | |
b50da39f | 548 | EXPORT_SYMBOL_GPL(__blkg_prfill_rwstat); |
d3d32e69 | 549 | |
5bc4afb1 TH |
550 | /** |
551 | * blkg_prfill_stat - prfill callback for blkg_stat | |
552 | * @sf: seq_file to print to | |
f95a04af TH |
553 | * @pd: policy private data of interest |
554 | * @off: offset to the blkg_stat in @pd | |
5bc4afb1 TH |
555 | * |
556 | * prfill callback for printing a blkg_stat. | |
557 | */ | |
f95a04af | 558 | u64 blkg_prfill_stat(struct seq_file *sf, struct blkg_policy_data *pd, int off) |
d3d32e69 | 559 | { |
f95a04af | 560 | return __blkg_prfill_u64(sf, pd, blkg_stat_read((void *)pd + off)); |
d3d32e69 | 561 | } |
5bc4afb1 | 562 | EXPORT_SYMBOL_GPL(blkg_prfill_stat); |
d3d32e69 | 563 | |
5bc4afb1 TH |
564 | /** |
565 | * blkg_prfill_rwstat - prfill callback for blkg_rwstat | |
566 | * @sf: seq_file to print to | |
f95a04af TH |
567 | * @pd: policy private data of interest |
568 | * @off: offset to the blkg_rwstat in @pd | |
5bc4afb1 TH |
569 | * |
570 | * prfill callback for printing a blkg_rwstat. | |
571 | */ | |
f95a04af TH |
572 | u64 blkg_prfill_rwstat(struct seq_file *sf, struct blkg_policy_data *pd, |
573 | int off) | |
d3d32e69 | 574 | { |
f95a04af | 575 | struct blkg_rwstat rwstat = blkg_rwstat_read((void *)pd + off); |
d3d32e69 | 576 | |
f95a04af | 577 | return __blkg_prfill_rwstat(sf, pd, &rwstat); |
d3d32e69 | 578 | } |
5bc4afb1 | 579 | EXPORT_SYMBOL_GPL(blkg_prfill_rwstat); |
d3d32e69 | 580 | |
16b3de66 TH |
581 | /** |
582 | * blkg_stat_recursive_sum - collect hierarchical blkg_stat | |
583 | * @pd: policy private data of interest | |
584 | * @off: offset to the blkg_stat in @pd | |
585 | * | |
586 | * Collect the blkg_stat specified by @off from @pd and all its online | |
e6269c44 | 587 | * descendants and their aux counts. The caller must be holding the queue |
16b3de66 TH |
588 | * lock for online tests. |
589 | */ | |
590 | u64 blkg_stat_recursive_sum(struct blkg_policy_data *pd, int off) | |
591 | { | |
592 | struct blkcg_policy *pol = blkcg_policy[pd->plid]; | |
593 | struct blkcg_gq *pos_blkg; | |
492eb21b | 594 | struct cgroup_subsys_state *pos_css; |
bd8815a6 | 595 | u64 sum = 0; |
16b3de66 TH |
596 | |
597 | lockdep_assert_held(pd->blkg->q->queue_lock); | |
598 | ||
16b3de66 | 599 | rcu_read_lock(); |
492eb21b | 600 | blkg_for_each_descendant_pre(pos_blkg, pos_css, pd_to_blkg(pd)) { |
16b3de66 TH |
601 | struct blkg_policy_data *pos_pd = blkg_to_pd(pos_blkg, pol); |
602 | struct blkg_stat *stat = (void *)pos_pd + off; | |
603 | ||
604 | if (pos_blkg->online) | |
e6269c44 TH |
605 | sum += blkg_stat_read(stat) + |
606 | atomic64_read(&stat->aux_cnt); | |
16b3de66 TH |
607 | } |
608 | rcu_read_unlock(); | |
609 | ||
610 | return sum; | |
611 | } | |
612 | EXPORT_SYMBOL_GPL(blkg_stat_recursive_sum); | |
613 | ||
614 | /** | |
615 | * blkg_rwstat_recursive_sum - collect hierarchical blkg_rwstat | |
616 | * @pd: policy private data of interest | |
617 | * @off: offset to the blkg_stat in @pd | |
618 | * | |
619 | * Collect the blkg_rwstat specified by @off from @pd and all its online | |
e6269c44 | 620 | * descendants and their aux counts. The caller must be holding the queue |
16b3de66 TH |
621 | * lock for online tests. |
622 | */ | |
623 | struct blkg_rwstat blkg_rwstat_recursive_sum(struct blkg_policy_data *pd, | |
624 | int off) | |
625 | { | |
626 | struct blkcg_policy *pol = blkcg_policy[pd->plid]; | |
627 | struct blkcg_gq *pos_blkg; | |
492eb21b | 628 | struct cgroup_subsys_state *pos_css; |
bd8815a6 | 629 | struct blkg_rwstat sum = { }; |
16b3de66 TH |
630 | int i; |
631 | ||
632 | lockdep_assert_held(pd->blkg->q->queue_lock); | |
633 | ||
16b3de66 | 634 | rcu_read_lock(); |
492eb21b | 635 | blkg_for_each_descendant_pre(pos_blkg, pos_css, pd_to_blkg(pd)) { |
16b3de66 TH |
636 | struct blkg_policy_data *pos_pd = blkg_to_pd(pos_blkg, pol); |
637 | struct blkg_rwstat *rwstat = (void *)pos_pd + off; | |
638 | struct blkg_rwstat tmp; | |
639 | ||
640 | if (!pos_blkg->online) | |
641 | continue; | |
642 | ||
643 | tmp = blkg_rwstat_read(rwstat); | |
644 | ||
645 | for (i = 0; i < BLKG_RWSTAT_NR; i++) | |
e6269c44 TH |
646 | sum.cnt[i] += tmp.cnt[i] + |
647 | atomic64_read(&rwstat->aux_cnt[i]); | |
16b3de66 TH |
648 | } |
649 | rcu_read_unlock(); | |
650 | ||
651 | return sum; | |
652 | } | |
653 | EXPORT_SYMBOL_GPL(blkg_rwstat_recursive_sum); | |
654 | ||
3a8b31d3 TH |
655 | /** |
656 | * blkg_conf_prep - parse and prepare for per-blkg config update | |
657 | * @blkcg: target block cgroup | |
da8b0662 | 658 | * @pol: target policy |
3a8b31d3 TH |
659 | * @input: input string |
660 | * @ctx: blkg_conf_ctx to be filled | |
661 | * | |
662 | * Parse per-blkg config update from @input and initialize @ctx with the | |
663 | * result. @ctx->blkg points to the blkg to be updated and @ctx->v the new | |
da8b0662 TH |
664 | * value. This function returns with RCU read lock and queue lock held and |
665 | * must be paired with blkg_conf_finish(). | |
3a8b31d3 | 666 | */ |
3c798398 TH |
667 | int blkg_conf_prep(struct blkcg *blkcg, const struct blkcg_policy *pol, |
668 | const char *input, struct blkg_conf_ctx *ctx) | |
da8b0662 | 669 | __acquires(rcu) __acquires(disk->queue->queue_lock) |
34d0f179 | 670 | { |
3a8b31d3 | 671 | struct gendisk *disk; |
3c798398 | 672 | struct blkcg_gq *blkg; |
726fa694 TH |
673 | unsigned int major, minor; |
674 | unsigned long long v; | |
675 | int part, ret; | |
34d0f179 | 676 | |
726fa694 TH |
677 | if (sscanf(input, "%u:%u %llu", &major, &minor, &v) != 3) |
678 | return -EINVAL; | |
3a8b31d3 | 679 | |
726fa694 | 680 | disk = get_gendisk(MKDEV(major, minor), &part); |
5f6c2d2b | 681 | if (!disk) |
726fa694 | 682 | return -EINVAL; |
5f6c2d2b TH |
683 | if (part) { |
684 | put_disk(disk); | |
685 | return -EINVAL; | |
686 | } | |
e56da7e2 TH |
687 | |
688 | rcu_read_lock(); | |
4bfd482e | 689 | spin_lock_irq(disk->queue->queue_lock); |
da8b0662 | 690 | |
a2b1693b | 691 | if (blkcg_policy_enabled(disk->queue, pol)) |
3c96cb32 | 692 | blkg = blkg_lookup_create(blkcg, disk->queue); |
a2b1693b TH |
693 | else |
694 | blkg = ERR_PTR(-EINVAL); | |
e56da7e2 | 695 | |
4bfd482e TH |
696 | if (IS_ERR(blkg)) { |
697 | ret = PTR_ERR(blkg); | |
3a8b31d3 | 698 | rcu_read_unlock(); |
da8b0662 | 699 | spin_unlock_irq(disk->queue->queue_lock); |
3a8b31d3 TH |
700 | put_disk(disk); |
701 | /* | |
702 | * If queue was bypassing, we should retry. Do so after a | |
703 | * short msleep(). It isn't strictly necessary but queue | |
704 | * can be bypassing for some time and it's always nice to | |
705 | * avoid busy looping. | |
706 | */ | |
707 | if (ret == -EBUSY) { | |
708 | msleep(10); | |
709 | ret = restart_syscall(); | |
7702e8f4 | 710 | } |
726fa694 | 711 | return ret; |
062a644d | 712 | } |
3a8b31d3 TH |
713 | |
714 | ctx->disk = disk; | |
715 | ctx->blkg = blkg; | |
726fa694 TH |
716 | ctx->v = v; |
717 | return 0; | |
34d0f179 | 718 | } |
829fdb50 | 719 | EXPORT_SYMBOL_GPL(blkg_conf_prep); |
34d0f179 | 720 | |
3a8b31d3 TH |
721 | /** |
722 | * blkg_conf_finish - finish up per-blkg config update | |
723 | * @ctx: blkg_conf_ctx intiailized by blkg_conf_prep() | |
724 | * | |
725 | * Finish up after per-blkg config update. This function must be paired | |
726 | * with blkg_conf_prep(). | |
727 | */ | |
829fdb50 | 728 | void blkg_conf_finish(struct blkg_conf_ctx *ctx) |
da8b0662 | 729 | __releases(ctx->disk->queue->queue_lock) __releases(rcu) |
34d0f179 | 730 | { |
da8b0662 | 731 | spin_unlock_irq(ctx->disk->queue->queue_lock); |
3a8b31d3 TH |
732 | rcu_read_unlock(); |
733 | put_disk(ctx->disk); | |
34d0f179 | 734 | } |
829fdb50 | 735 | EXPORT_SYMBOL_GPL(blkg_conf_finish); |
34d0f179 | 736 | |
3c798398 | 737 | struct cftype blkcg_files[] = { |
84c124da DS |
738 | { |
739 | .name = "reset_stats", | |
3c798398 | 740 | .write_u64 = blkcg_reset_stats, |
22084190 | 741 | }, |
4baf6e33 | 742 | { } /* terminate */ |
31e4c28d VG |
743 | }; |
744 | ||
9f13ef67 | 745 | /** |
92fb9748 | 746 | * blkcg_css_offline - cgroup css_offline callback |
eb95419b | 747 | * @css: css of interest |
9f13ef67 | 748 | * |
eb95419b TH |
749 | * This function is called when @css is about to go away and responsible |
750 | * for shooting down all blkgs associated with @css. blkgs should be | |
9f13ef67 TH |
751 | * removed while holding both q and blkcg locks. As blkcg lock is nested |
752 | * inside q lock, this function performs reverse double lock dancing. | |
753 | * | |
754 | * This is the blkcg counterpart of ioc_release_fn(). | |
755 | */ | |
eb95419b | 756 | static void blkcg_css_offline(struct cgroup_subsys_state *css) |
31e4c28d | 757 | { |
eb95419b | 758 | struct blkcg *blkcg = css_to_blkcg(css); |
b1c35769 | 759 | |
9f13ef67 | 760 | spin_lock_irq(&blkcg->lock); |
7ee9c562 | 761 | |
9f13ef67 | 762 | while (!hlist_empty(&blkcg->blkg_list)) { |
3c798398 TH |
763 | struct blkcg_gq *blkg = hlist_entry(blkcg->blkg_list.first, |
764 | struct blkcg_gq, blkcg_node); | |
c875f4d0 | 765 | struct request_queue *q = blkg->q; |
b1c35769 | 766 | |
9f13ef67 TH |
767 | if (spin_trylock(q->queue_lock)) { |
768 | blkg_destroy(blkg); | |
769 | spin_unlock(q->queue_lock); | |
770 | } else { | |
771 | spin_unlock_irq(&blkcg->lock); | |
9f13ef67 | 772 | cpu_relax(); |
a5567932 | 773 | spin_lock_irq(&blkcg->lock); |
0f3942a3 | 774 | } |
9f13ef67 | 775 | } |
b1c35769 | 776 | |
9f13ef67 | 777 | spin_unlock_irq(&blkcg->lock); |
52ebea74 TH |
778 | |
779 | wb_blkcg_offline(blkcg); | |
7ee9c562 TH |
780 | } |
781 | ||
eb95419b | 782 | static void blkcg_css_free(struct cgroup_subsys_state *css) |
7ee9c562 | 783 | { |
eb95419b | 784 | struct blkcg *blkcg = css_to_blkcg(css); |
bc915e61 | 785 | int i; |
7ee9c562 | 786 | |
7876f930 | 787 | mutex_lock(&blkcg_pol_mutex); |
e4a9bde9 | 788 | |
7876f930 | 789 | list_del(&blkcg->all_blkcgs_node); |
7876f930 | 790 | |
bc915e61 | 791 | for (i = 0; i < BLKCG_MAX_POLS; i++) |
e4a9bde9 TH |
792 | if (blkcg->cpd[i]) |
793 | blkcg_policy[i]->cpd_free_fn(blkcg->cpd[i]); | |
794 | ||
795 | mutex_unlock(&blkcg_pol_mutex); | |
796 | ||
bc915e61 | 797 | kfree(blkcg); |
31e4c28d VG |
798 | } |
799 | ||
eb95419b TH |
800 | static struct cgroup_subsys_state * |
801 | blkcg_css_alloc(struct cgroup_subsys_state *parent_css) | |
31e4c28d | 802 | { |
3c798398 | 803 | struct blkcg *blkcg; |
e48453c3 AA |
804 | struct cgroup_subsys_state *ret; |
805 | int i; | |
31e4c28d | 806 | |
7876f930 TH |
807 | mutex_lock(&blkcg_pol_mutex); |
808 | ||
eb95419b | 809 | if (!parent_css) { |
3c798398 | 810 | blkcg = &blkcg_root; |
bc915e61 TH |
811 | } else { |
812 | blkcg = kzalloc(sizeof(*blkcg), GFP_KERNEL); | |
813 | if (!blkcg) { | |
814 | ret = ERR_PTR(-ENOMEM); | |
815 | goto free_blkcg; | |
816 | } | |
e48453c3 AA |
817 | } |
818 | ||
819 | for (i = 0; i < BLKCG_MAX_POLS ; i++) { | |
820 | struct blkcg_policy *pol = blkcg_policy[i]; | |
821 | struct blkcg_policy_data *cpd; | |
822 | ||
823 | /* | |
824 | * If the policy hasn't been attached yet, wait for it | |
825 | * to be attached before doing anything else. Otherwise, | |
826 | * check if the policy requires any specific per-cgroup | |
827 | * data: if it does, allocate and initialize it. | |
828 | */ | |
e4a9bde9 | 829 | if (!pol || !pol->cpd_alloc_fn) |
e48453c3 AA |
830 | continue; |
831 | ||
e4a9bde9 | 832 | cpd = pol->cpd_alloc_fn(GFP_KERNEL); |
e48453c3 AA |
833 | if (!cpd) { |
834 | ret = ERR_PTR(-ENOMEM); | |
835 | goto free_pd_blkcg; | |
836 | } | |
81437648 TH |
837 | blkcg->cpd[i] = cpd; |
838 | cpd->blkcg = blkcg; | |
e48453c3 | 839 | cpd->plid = i; |
e4a9bde9 TH |
840 | if (pol->cpd_init_fn) |
841 | pol->cpd_init_fn(cpd); | |
e48453c3 | 842 | } |
31e4c28d | 843 | |
31e4c28d | 844 | spin_lock_init(&blkcg->lock); |
d93a11f1 | 845 | INIT_RADIX_TREE(&blkcg->blkg_tree, GFP_NOWAIT); |
31e4c28d | 846 | INIT_HLIST_HEAD(&blkcg->blkg_list); |
52ebea74 TH |
847 | #ifdef CONFIG_CGROUP_WRITEBACK |
848 | INIT_LIST_HEAD(&blkcg->cgwb_list); | |
849 | #endif | |
7876f930 TH |
850 | list_add_tail(&blkcg->all_blkcgs_node, &all_blkcgs); |
851 | ||
852 | mutex_unlock(&blkcg_pol_mutex); | |
31e4c28d | 853 | return &blkcg->css; |
e48453c3 AA |
854 | |
855 | free_pd_blkcg: | |
856 | for (i--; i >= 0; i--) | |
e4a9bde9 TH |
857 | if (blkcg->cpd[i]) |
858 | blkcg_policy[i]->cpd_free_fn(blkcg->cpd[i]); | |
e48453c3 AA |
859 | free_blkcg: |
860 | kfree(blkcg); | |
7876f930 | 861 | mutex_unlock(&blkcg_pol_mutex); |
e48453c3 | 862 | return ret; |
31e4c28d VG |
863 | } |
864 | ||
5efd6113 TH |
865 | /** |
866 | * blkcg_init_queue - initialize blkcg part of request queue | |
867 | * @q: request_queue to initialize | |
868 | * | |
869 | * Called from blk_alloc_queue_node(). Responsible for initializing blkcg | |
870 | * part of new request_queue @q. | |
871 | * | |
872 | * RETURNS: | |
873 | * 0 on success, -errno on failure. | |
874 | */ | |
875 | int blkcg_init_queue(struct request_queue *q) | |
876 | { | |
ec13b1d6 TH |
877 | struct blkcg_gq *new_blkg, *blkg; |
878 | bool preloaded; | |
879 | int ret; | |
880 | ||
881 | new_blkg = blkg_alloc(&blkcg_root, q, GFP_KERNEL); | |
882 | if (!new_blkg) | |
883 | return -ENOMEM; | |
884 | ||
885 | preloaded = !radix_tree_preload(GFP_KERNEL); | |
5efd6113 | 886 | |
ec13b1d6 TH |
887 | /* |
888 | * Make sure the root blkg exists and count the existing blkgs. As | |
889 | * @q is bypassing at this point, blkg_lookup_create() can't be | |
890 | * used. Open code insertion. | |
891 | */ | |
892 | rcu_read_lock(); | |
893 | spin_lock_irq(q->queue_lock); | |
894 | blkg = blkg_create(&blkcg_root, q, new_blkg); | |
895 | spin_unlock_irq(q->queue_lock); | |
896 | rcu_read_unlock(); | |
897 | ||
898 | if (preloaded) | |
899 | radix_tree_preload_end(); | |
900 | ||
901 | if (IS_ERR(blkg)) { | |
994b7832 | 902 | blkg_free(new_blkg); |
ec13b1d6 TH |
903 | return PTR_ERR(blkg); |
904 | } | |
905 | ||
906 | q->root_blkg = blkg; | |
907 | q->root_rl.blkg = blkg; | |
5efd6113 | 908 | |
ec13b1d6 TH |
909 | ret = blk_throtl_init(q); |
910 | if (ret) { | |
911 | spin_lock_irq(q->queue_lock); | |
912 | blkg_destroy_all(q); | |
913 | spin_unlock_irq(q->queue_lock); | |
914 | } | |
915 | return ret; | |
5efd6113 TH |
916 | } |
917 | ||
918 | /** | |
919 | * blkcg_drain_queue - drain blkcg part of request_queue | |
920 | * @q: request_queue to drain | |
921 | * | |
922 | * Called from blk_drain_queue(). Responsible for draining blkcg part. | |
923 | */ | |
924 | void blkcg_drain_queue(struct request_queue *q) | |
925 | { | |
926 | lockdep_assert_held(q->queue_lock); | |
927 | ||
0b462c89 TH |
928 | /* |
929 | * @q could be exiting and already have destroyed all blkgs as | |
930 | * indicated by NULL root_blkg. If so, don't confuse policies. | |
931 | */ | |
932 | if (!q->root_blkg) | |
933 | return; | |
934 | ||
5efd6113 TH |
935 | blk_throtl_drain(q); |
936 | } | |
937 | ||
938 | /** | |
939 | * blkcg_exit_queue - exit and release blkcg part of request_queue | |
940 | * @q: request_queue being released | |
941 | * | |
942 | * Called from blk_release_queue(). Responsible for exiting blkcg part. | |
943 | */ | |
944 | void blkcg_exit_queue(struct request_queue *q) | |
945 | { | |
6d18b008 | 946 | spin_lock_irq(q->queue_lock); |
3c96cb32 | 947 | blkg_destroy_all(q); |
6d18b008 TH |
948 | spin_unlock_irq(q->queue_lock); |
949 | ||
5efd6113 TH |
950 | blk_throtl_exit(q); |
951 | } | |
952 | ||
31e4c28d VG |
953 | /* |
954 | * We cannot support shared io contexts, as we have no mean to support | |
955 | * two tasks with the same ioc in two different groups without major rework | |
956 | * of the main cic data structures. For now we allow a task to change | |
957 | * its cgroup only if it's the only owner of its ioc. | |
958 | */ | |
eb95419b TH |
959 | static int blkcg_can_attach(struct cgroup_subsys_state *css, |
960 | struct cgroup_taskset *tset) | |
31e4c28d | 961 | { |
bb9d97b6 | 962 | struct task_struct *task; |
31e4c28d VG |
963 | struct io_context *ioc; |
964 | int ret = 0; | |
965 | ||
966 | /* task_lock() is needed to avoid races with exit_io_context() */ | |
924f0d9a | 967 | cgroup_taskset_for_each(task, tset) { |
bb9d97b6 TH |
968 | task_lock(task); |
969 | ioc = task->io_context; | |
970 | if (ioc && atomic_read(&ioc->nr_tasks) > 1) | |
971 | ret = -EINVAL; | |
972 | task_unlock(task); | |
973 | if (ret) | |
974 | break; | |
975 | } | |
31e4c28d VG |
976 | return ret; |
977 | } | |
978 | ||
073219e9 | 979 | struct cgroup_subsys blkio_cgrp_subsys = { |
92fb9748 TH |
980 | .css_alloc = blkcg_css_alloc, |
981 | .css_offline = blkcg_css_offline, | |
982 | .css_free = blkcg_css_free, | |
3c798398 | 983 | .can_attach = blkcg_can_attach, |
5577964e | 984 | .legacy_cftypes = blkcg_files, |
1ced953b TH |
985 | #ifdef CONFIG_MEMCG |
986 | /* | |
987 | * This ensures that, if available, memcg is automatically enabled | |
988 | * together on the default hierarchy so that the owner cgroup can | |
989 | * be retrieved from writeback pages. | |
990 | */ | |
991 | .depends_on = 1 << memory_cgrp_id, | |
992 | #endif | |
676f7c8f | 993 | }; |
073219e9 | 994 | EXPORT_SYMBOL_GPL(blkio_cgrp_subsys); |
676f7c8f | 995 | |
a2b1693b TH |
996 | /** |
997 | * blkcg_activate_policy - activate a blkcg policy on a request_queue | |
998 | * @q: request_queue of interest | |
999 | * @pol: blkcg policy to activate | |
1000 | * | |
1001 | * Activate @pol on @q. Requires %GFP_KERNEL context. @q goes through | |
1002 | * bypass mode to populate its blkgs with policy_data for @pol. | |
1003 | * | |
1004 | * Activation happens with @q bypassed, so nobody would be accessing blkgs | |
1005 | * from IO path. Update of each blkg is protected by both queue and blkcg | |
1006 | * locks so that holding either lock and testing blkcg_policy_enabled() is | |
1007 | * always enough for dereferencing policy data. | |
1008 | * | |
1009 | * The caller is responsible for synchronizing [de]activations and policy | |
1010 | * [un]registerations. Returns 0 on success, -errno on failure. | |
1011 | */ | |
1012 | int blkcg_activate_policy(struct request_queue *q, | |
3c798398 | 1013 | const struct blkcg_policy *pol) |
a2b1693b | 1014 | { |
4c55f4f9 | 1015 | struct blkg_policy_data *pd_prealloc = NULL; |
ec13b1d6 | 1016 | struct blkcg_gq *blkg; |
4c55f4f9 | 1017 | int ret; |
a2b1693b TH |
1018 | |
1019 | if (blkcg_policy_enabled(q, pol)) | |
1020 | return 0; | |
1021 | ||
1022 | blk_queue_bypass_start(q); | |
4c55f4f9 TH |
1023 | pd_prealloc: |
1024 | if (!pd_prealloc) { | |
001bea73 | 1025 | pd_prealloc = pol->pd_alloc_fn(GFP_KERNEL, q->node); |
4c55f4f9 | 1026 | if (!pd_prealloc) { |
a2b1693b | 1027 | ret = -ENOMEM; |
4c55f4f9 | 1028 | goto out_bypass_end; |
a2b1693b | 1029 | } |
a2b1693b TH |
1030 | } |
1031 | ||
a2b1693b TH |
1032 | spin_lock_irq(q->queue_lock); |
1033 | ||
1034 | list_for_each_entry(blkg, &q->blkg_list, q_node) { | |
4c55f4f9 TH |
1035 | struct blkg_policy_data *pd; |
1036 | ||
1037 | if (blkg->pd[pol->plid]) | |
1038 | continue; | |
a2b1693b | 1039 | |
001bea73 | 1040 | pd = pol->pd_alloc_fn(GFP_NOWAIT, q->node); |
4c55f4f9 TH |
1041 | if (!pd) |
1042 | swap(pd, pd_prealloc); | |
1043 | if (!pd) { | |
1044 | spin_unlock_irq(q->queue_lock); | |
1045 | goto pd_prealloc; | |
1046 | } | |
a2b1693b TH |
1047 | |
1048 | blkg->pd[pol->plid] = pd; | |
1049 | pd->blkg = blkg; | |
b276a876 | 1050 | pd->plid = pol->plid; |
3e418710 | 1051 | if (pol->pd_init_fn) |
a9520cd6 | 1052 | pol->pd_init_fn(pd); |
a2b1693b TH |
1053 | } |
1054 | ||
1055 | __set_bit(pol->plid, q->blkcg_pols); | |
1056 | ret = 0; | |
4c55f4f9 | 1057 | |
a2b1693b | 1058 | spin_unlock_irq(q->queue_lock); |
4c55f4f9 | 1059 | out_bypass_end: |
a2b1693b | 1060 | blk_queue_bypass_end(q); |
001bea73 TH |
1061 | if (pd_prealloc) |
1062 | pol->pd_free_fn(pd_prealloc); | |
a2b1693b TH |
1063 | return ret; |
1064 | } | |
1065 | EXPORT_SYMBOL_GPL(blkcg_activate_policy); | |
1066 | ||
1067 | /** | |
1068 | * blkcg_deactivate_policy - deactivate a blkcg policy on a request_queue | |
1069 | * @q: request_queue of interest | |
1070 | * @pol: blkcg policy to deactivate | |
1071 | * | |
1072 | * Deactivate @pol on @q. Follows the same synchronization rules as | |
1073 | * blkcg_activate_policy(). | |
1074 | */ | |
1075 | void blkcg_deactivate_policy(struct request_queue *q, | |
3c798398 | 1076 | const struct blkcg_policy *pol) |
a2b1693b | 1077 | { |
3c798398 | 1078 | struct blkcg_gq *blkg; |
a2b1693b TH |
1079 | |
1080 | if (!blkcg_policy_enabled(q, pol)) | |
1081 | return; | |
1082 | ||
1083 | blk_queue_bypass_start(q); | |
1084 | spin_lock_irq(q->queue_lock); | |
1085 | ||
1086 | __clear_bit(pol->plid, q->blkcg_pols); | |
1087 | ||
1088 | list_for_each_entry(blkg, &q->blkg_list, q_node) { | |
1089 | /* grab blkcg lock too while removing @pd from @blkg */ | |
1090 | spin_lock(&blkg->blkcg->lock); | |
1091 | ||
001bea73 | 1092 | if (blkg->pd[pol->plid]) { |
a9520cd6 TH |
1093 | if (pol->pd_offline_fn) |
1094 | pol->pd_offline_fn(blkg->pd[pol->plid]); | |
001bea73 TH |
1095 | pol->pd_free_fn(blkg->pd[pol->plid]); |
1096 | blkg->pd[pol->plid] = NULL; | |
1097 | } | |
a2b1693b TH |
1098 | |
1099 | spin_unlock(&blkg->blkcg->lock); | |
1100 | } | |
1101 | ||
1102 | spin_unlock_irq(q->queue_lock); | |
1103 | blk_queue_bypass_end(q); | |
1104 | } | |
1105 | EXPORT_SYMBOL_GPL(blkcg_deactivate_policy); | |
1106 | ||
8bd435b3 | 1107 | /** |
3c798398 TH |
1108 | * blkcg_policy_register - register a blkcg policy |
1109 | * @pol: blkcg policy to register | |
8bd435b3 | 1110 | * |
3c798398 TH |
1111 | * Register @pol with blkcg core. Might sleep and @pol may be modified on |
1112 | * successful registration. Returns 0 on success and -errno on failure. | |
8bd435b3 | 1113 | */ |
d5bf0291 | 1114 | int blkcg_policy_register(struct blkcg_policy *pol) |
3e252066 | 1115 | { |
06b285bd | 1116 | struct blkcg *blkcg; |
8bd435b3 | 1117 | int i, ret; |
e8989fae | 1118 | |
838f13bf | 1119 | mutex_lock(&blkcg_pol_register_mutex); |
bc0d6501 TH |
1120 | mutex_lock(&blkcg_pol_mutex); |
1121 | ||
8bd435b3 TH |
1122 | /* find an empty slot */ |
1123 | ret = -ENOSPC; | |
1124 | for (i = 0; i < BLKCG_MAX_POLS; i++) | |
3c798398 | 1125 | if (!blkcg_policy[i]) |
8bd435b3 TH |
1126 | break; |
1127 | if (i >= BLKCG_MAX_POLS) | |
838f13bf | 1128 | goto err_unlock; |
035d10b2 | 1129 | |
06b285bd | 1130 | /* register @pol */ |
3c798398 | 1131 | pol->plid = i; |
06b285bd TH |
1132 | blkcg_policy[pol->plid] = pol; |
1133 | ||
1134 | /* allocate and install cpd's */ | |
e4a9bde9 | 1135 | if (pol->cpd_alloc_fn) { |
06b285bd TH |
1136 | list_for_each_entry(blkcg, &all_blkcgs, all_blkcgs_node) { |
1137 | struct blkcg_policy_data *cpd; | |
1138 | ||
e4a9bde9 | 1139 | cpd = pol->cpd_alloc_fn(GFP_KERNEL); |
06b285bd TH |
1140 | if (!cpd) { |
1141 | mutex_unlock(&blkcg_pol_mutex); | |
1142 | goto err_free_cpds; | |
1143 | } | |
1144 | ||
81437648 TH |
1145 | blkcg->cpd[pol->plid] = cpd; |
1146 | cpd->blkcg = blkcg; | |
06b285bd | 1147 | cpd->plid = pol->plid; |
81437648 | 1148 | pol->cpd_init_fn(cpd); |
06b285bd TH |
1149 | } |
1150 | } | |
1151 | ||
838f13bf | 1152 | mutex_unlock(&blkcg_pol_mutex); |
8bd435b3 | 1153 | |
8bd435b3 | 1154 | /* everything is in place, add intf files for the new policy */ |
3c798398 | 1155 | if (pol->cftypes) |
2cf669a5 TH |
1156 | WARN_ON(cgroup_add_legacy_cftypes(&blkio_cgrp_subsys, |
1157 | pol->cftypes)); | |
838f13bf TH |
1158 | mutex_unlock(&blkcg_pol_register_mutex); |
1159 | return 0; | |
1160 | ||
06b285bd | 1161 | err_free_cpds: |
e4a9bde9 | 1162 | if (pol->cpd_alloc_fn) { |
06b285bd | 1163 | list_for_each_entry(blkcg, &all_blkcgs, all_blkcgs_node) { |
e4a9bde9 TH |
1164 | if (blkcg->cpd[pol->plid]) { |
1165 | pol->cpd_free_fn(blkcg->cpd[pol->plid]); | |
1166 | blkcg->cpd[pol->plid] = NULL; | |
1167 | } | |
06b285bd TH |
1168 | } |
1169 | } | |
1170 | blkcg_policy[pol->plid] = NULL; | |
838f13bf | 1171 | err_unlock: |
bc0d6501 | 1172 | mutex_unlock(&blkcg_pol_mutex); |
838f13bf | 1173 | mutex_unlock(&blkcg_pol_register_mutex); |
8bd435b3 | 1174 | return ret; |
3e252066 | 1175 | } |
3c798398 | 1176 | EXPORT_SYMBOL_GPL(blkcg_policy_register); |
3e252066 | 1177 | |
8bd435b3 | 1178 | /** |
3c798398 TH |
1179 | * blkcg_policy_unregister - unregister a blkcg policy |
1180 | * @pol: blkcg policy to unregister | |
8bd435b3 | 1181 | * |
3c798398 | 1182 | * Undo blkcg_policy_register(@pol). Might sleep. |
8bd435b3 | 1183 | */ |
3c798398 | 1184 | void blkcg_policy_unregister(struct blkcg_policy *pol) |
3e252066 | 1185 | { |
06b285bd TH |
1186 | struct blkcg *blkcg; |
1187 | ||
838f13bf | 1188 | mutex_lock(&blkcg_pol_register_mutex); |
bc0d6501 | 1189 | |
3c798398 | 1190 | if (WARN_ON(blkcg_policy[pol->plid] != pol)) |
8bd435b3 TH |
1191 | goto out_unlock; |
1192 | ||
1193 | /* kill the intf files first */ | |
3c798398 | 1194 | if (pol->cftypes) |
2bb566cb | 1195 | cgroup_rm_cftypes(pol->cftypes); |
44ea53de | 1196 | |
06b285bd | 1197 | /* remove cpds and unregister */ |
838f13bf | 1198 | mutex_lock(&blkcg_pol_mutex); |
06b285bd | 1199 | |
e4a9bde9 | 1200 | if (pol->cpd_alloc_fn) { |
06b285bd | 1201 | list_for_each_entry(blkcg, &all_blkcgs, all_blkcgs_node) { |
e4a9bde9 TH |
1202 | if (blkcg->cpd[pol->plid]) { |
1203 | pol->cpd_free_fn(blkcg->cpd[pol->plid]); | |
1204 | blkcg->cpd[pol->plid] = NULL; | |
1205 | } | |
06b285bd TH |
1206 | } |
1207 | } | |
3c798398 | 1208 | blkcg_policy[pol->plid] = NULL; |
06b285bd | 1209 | |
bc0d6501 | 1210 | mutex_unlock(&blkcg_pol_mutex); |
838f13bf TH |
1211 | out_unlock: |
1212 | mutex_unlock(&blkcg_pol_register_mutex); | |
3e252066 | 1213 | } |
3c798398 | 1214 | EXPORT_SYMBOL_GPL(blkcg_policy_unregister); |