]>
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> | |
12 | */ | |
13 | #include <linux/ioprio.h> | |
22084190 | 14 | #include <linux/kdev_t.h> |
9d6a986c | 15 | #include <linux/module.h> |
accee785 | 16 | #include <linux/err.h> |
9195291e | 17 | #include <linux/blkdev.h> |
5a0e3ad6 | 18 | #include <linux/slab.h> |
34d0f179 | 19 | #include <linux/genhd.h> |
72e06c25 | 20 | #include <linux/delay.h> |
9a9e8a26 | 21 | #include <linux/atomic.h> |
72e06c25 | 22 | #include "blk-cgroup.h" |
5efd6113 | 23 | #include "blk.h" |
3e252066 | 24 | |
84c124da DS |
25 | #define MAX_KEY_LEN 100 |
26 | ||
bc0d6501 | 27 | static DEFINE_MUTEX(blkcg_pol_mutex); |
923adde1 TH |
28 | static DEFINE_MUTEX(all_q_mutex); |
29 | static LIST_HEAD(all_q_list); | |
30 | ||
3381cb8d | 31 | struct blkio_cgroup blkio_root_cgroup = { .cfq_weight = 2 * CFQ_WEIGHT_DEFAULT }; |
9d6a986c VG |
32 | EXPORT_SYMBOL_GPL(blkio_root_cgroup); |
33 | ||
8bd435b3 | 34 | static struct blkio_policy_type *blkio_policy[BLKCG_MAX_POLS]; |
035d10b2 | 35 | |
31e4c28d VG |
36 | struct blkio_cgroup *cgroup_to_blkio_cgroup(struct cgroup *cgroup) |
37 | { | |
38 | return container_of(cgroup_subsys_state(cgroup, blkio_subsys_id), | |
39 | struct blkio_cgroup, css); | |
40 | } | |
9d6a986c | 41 | EXPORT_SYMBOL_GPL(cgroup_to_blkio_cgroup); |
31e4c28d | 42 | |
4f85cb96 | 43 | static struct blkio_cgroup *task_blkio_cgroup(struct task_struct *tsk) |
70087dc3 VG |
44 | { |
45 | return container_of(task_subsys_state(tsk, blkio_subsys_id), | |
46 | struct blkio_cgroup, css); | |
47 | } | |
4f85cb96 TH |
48 | |
49 | struct blkio_cgroup *bio_blkio_cgroup(struct bio *bio) | |
50 | { | |
51 | if (bio && bio->bi_css) | |
52 | return container_of(bio->bi_css, struct blkio_cgroup, css); | |
53 | return task_blkio_cgroup(current); | |
54 | } | |
55 | EXPORT_SYMBOL_GPL(bio_blkio_cgroup); | |
70087dc3 | 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 | */ | |
63 | static void blkg_free(struct blkio_group *blkg) | |
64 | { | |
e8989fae | 65 | int i; |
549d3aa8 TH |
66 | |
67 | if (!blkg) | |
68 | return; | |
69 | ||
8bd435b3 | 70 | for (i = 0; i < BLKCG_MAX_POLS; i++) { |
9ade5ea4 | 71 | struct blkio_policy_type *pol = blkio_policy[i]; |
e8989fae TH |
72 | struct blkg_policy_data *pd = blkg->pd[i]; |
73 | ||
9ade5ea4 TH |
74 | if (!pd) |
75 | continue; | |
76 | ||
77 | if (pol && pol->ops.blkio_exit_group_fn) | |
78 | pol->ops.blkio_exit_group_fn(blkg); | |
79 | ||
9ade5ea4 | 80 | kfree(pd); |
0381411e | 81 | } |
e8989fae | 82 | |
549d3aa8 | 83 | kfree(blkg); |
0381411e TH |
84 | } |
85 | ||
86 | /** | |
87 | * blkg_alloc - allocate a blkg | |
88 | * @blkcg: block cgroup the new blkg is associated with | |
89 | * @q: request_queue the new blkg is associated with | |
0381411e | 90 | * |
e8989fae | 91 | * Allocate a new blkg assocating @blkcg and @q. |
0381411e TH |
92 | */ |
93 | static struct blkio_group *blkg_alloc(struct blkio_cgroup *blkcg, | |
e8989fae | 94 | struct request_queue *q) |
0381411e TH |
95 | { |
96 | struct blkio_group *blkg; | |
e8989fae | 97 | int i; |
0381411e TH |
98 | |
99 | /* alloc and init base part */ | |
100 | blkg = kzalloc_node(sizeof(*blkg), GFP_ATOMIC, q->node); | |
101 | if (!blkg) | |
102 | return NULL; | |
103 | ||
c875f4d0 | 104 | blkg->q = q; |
e8989fae | 105 | INIT_LIST_HEAD(&blkg->q_node); |
0381411e | 106 | blkg->blkcg = blkcg; |
1adaf3dd | 107 | blkg->refcnt = 1; |
0381411e TH |
108 | cgroup_path(blkcg->css.cgroup, blkg->path, sizeof(blkg->path)); |
109 | ||
8bd435b3 | 110 | for (i = 0; i < BLKCG_MAX_POLS; i++) { |
e8989fae TH |
111 | struct blkio_policy_type *pol = blkio_policy[i]; |
112 | struct blkg_policy_data *pd; | |
0381411e | 113 | |
e8989fae TH |
114 | if (!pol) |
115 | continue; | |
116 | ||
117 | /* alloc per-policy data and attach it to blkg */ | |
118 | pd = kzalloc_node(sizeof(*pd) + pol->pdata_size, GFP_ATOMIC, | |
119 | q->node); | |
120 | if (!pd) { | |
121 | blkg_free(blkg); | |
122 | return NULL; | |
123 | } | |
549d3aa8 | 124 | |
e8989fae TH |
125 | blkg->pd[i] = pd; |
126 | pd->blkg = blkg; | |
0381411e TH |
127 | } |
128 | ||
549d3aa8 | 129 | /* invoke per-policy init */ |
8bd435b3 | 130 | for (i = 0; i < BLKCG_MAX_POLS; i++) { |
e8989fae TH |
131 | struct blkio_policy_type *pol = blkio_policy[i]; |
132 | ||
133 | if (pol) | |
134 | pol->ops.blkio_init_group_fn(blkg); | |
135 | } | |
136 | ||
0381411e TH |
137 | return blkg; |
138 | } | |
139 | ||
cd1604fa TH |
140 | struct blkio_group *blkg_lookup_create(struct blkio_cgroup *blkcg, |
141 | struct request_queue *q, | |
cd1604fa TH |
142 | bool for_root) |
143 | __releases(q->queue_lock) __acquires(q->queue_lock) | |
5624a4e4 | 144 | { |
1cd9e039 | 145 | struct blkio_group *blkg; |
5624a4e4 | 146 | |
cd1604fa TH |
147 | WARN_ON_ONCE(!rcu_read_lock_held()); |
148 | lockdep_assert_held(q->queue_lock); | |
149 | ||
150 | /* | |
151 | * This could be the first entry point of blkcg implementation and | |
152 | * we shouldn't allow anything to go through for a bypassing queue. | |
153 | * The following can be removed if blkg lookup is guaranteed to | |
154 | * fail on a bypassing queue. | |
155 | */ | |
156 | if (unlikely(blk_queue_bypass(q)) && !for_root) | |
157 | return ERR_PTR(blk_queue_dead(q) ? -EINVAL : -EBUSY); | |
158 | ||
e8989fae | 159 | blkg = blkg_lookup(blkcg, q); |
cd1604fa TH |
160 | if (blkg) |
161 | return blkg; | |
162 | ||
7ee9c562 | 163 | /* blkg holds a reference to blkcg */ |
cd1604fa TH |
164 | if (!css_tryget(&blkcg->css)) |
165 | return ERR_PTR(-EINVAL); | |
166 | ||
167 | /* | |
168 | * Allocate and initialize. | |
cd1604fa | 169 | */ |
1cd9e039 | 170 | blkg = blkg_alloc(blkcg, q); |
cd1604fa TH |
171 | |
172 | /* did alloc fail? */ | |
1cd9e039 | 173 | if (unlikely(!blkg)) { |
cd1604fa TH |
174 | blkg = ERR_PTR(-ENOMEM); |
175 | goto out; | |
176 | } | |
177 | ||
178 | /* insert */ | |
179 | spin_lock(&blkcg->lock); | |
31e4c28d | 180 | hlist_add_head_rcu(&blkg->blkcg_node, &blkcg->blkg_list); |
e8989fae | 181 | list_add(&blkg->q_node, &q->blkg_list); |
cd1604fa TH |
182 | spin_unlock(&blkcg->lock); |
183 | out: | |
cd1604fa | 184 | return blkg; |
31e4c28d | 185 | } |
cd1604fa | 186 | EXPORT_SYMBOL_GPL(blkg_lookup_create); |
31e4c28d | 187 | |
31e4c28d | 188 | /* called under rcu_read_lock(). */ |
cd1604fa | 189 | struct blkio_group *blkg_lookup(struct blkio_cgroup *blkcg, |
e8989fae | 190 | struct request_queue *q) |
31e4c28d VG |
191 | { |
192 | struct blkio_group *blkg; | |
193 | struct hlist_node *n; | |
31e4c28d | 194 | |
ca32aefc | 195 | hlist_for_each_entry_rcu(blkg, n, &blkcg->blkg_list, blkcg_node) |
e8989fae | 196 | if (blkg->q == q) |
31e4c28d | 197 | return blkg; |
31e4c28d VG |
198 | return NULL; |
199 | } | |
cd1604fa | 200 | EXPORT_SYMBOL_GPL(blkg_lookup); |
31e4c28d | 201 | |
e8989fae | 202 | static void blkg_destroy(struct blkio_group *blkg) |
03aa264a TH |
203 | { |
204 | struct request_queue *q = blkg->q; | |
9f13ef67 | 205 | struct blkio_cgroup *blkcg = blkg->blkcg; |
03aa264a TH |
206 | |
207 | lockdep_assert_held(q->queue_lock); | |
9f13ef67 | 208 | lockdep_assert_held(&blkcg->lock); |
03aa264a TH |
209 | |
210 | /* Something wrong if we are trying to remove same group twice */ | |
e8989fae | 211 | WARN_ON_ONCE(list_empty(&blkg->q_node)); |
9f13ef67 | 212 | WARN_ON_ONCE(hlist_unhashed(&blkg->blkcg_node)); |
e8989fae | 213 | list_del_init(&blkg->q_node); |
9f13ef67 | 214 | hlist_del_init_rcu(&blkg->blkcg_node); |
03aa264a | 215 | |
03aa264a TH |
216 | /* |
217 | * Put the reference taken at the time of creation so that when all | |
218 | * queues are gone, group can be destroyed. | |
219 | */ | |
220 | blkg_put(blkg); | |
221 | } | |
222 | ||
e8989fae TH |
223 | /* |
224 | * XXX: This updates blkg policy data in-place for root blkg, which is | |
225 | * necessary across elevator switch and policy registration as root blkgs | |
226 | * aren't shot down. This broken and racy implementation is temporary. | |
227 | * Eventually, blkg shoot down will be replaced by proper in-place update. | |
228 | */ | |
ec399347 TH |
229 | void update_root_blkg_pd(struct request_queue *q, |
230 | const struct blkio_policy_type *pol) | |
e8989fae | 231 | { |
e8989fae TH |
232 | struct blkio_group *blkg = blkg_lookup(&blkio_root_cgroup, q); |
233 | struct blkg_policy_data *pd; | |
234 | ||
235 | if (!blkg) | |
236 | return; | |
237 | ||
ec399347 TH |
238 | kfree(blkg->pd[pol->plid]); |
239 | blkg->pd[pol->plid] = NULL; | |
e8989fae TH |
240 | |
241 | if (!pol) | |
242 | return; | |
243 | ||
244 | pd = kzalloc(sizeof(*pd) + pol->pdata_size, GFP_KERNEL); | |
245 | WARN_ON_ONCE(!pd); | |
246 | ||
ec399347 | 247 | blkg->pd[pol->plid] = pd; |
e8989fae TH |
248 | pd->blkg = blkg; |
249 | pol->ops.blkio_init_group_fn(blkg); | |
250 | } | |
251 | EXPORT_SYMBOL_GPL(update_root_blkg_pd); | |
252 | ||
9f13ef67 TH |
253 | /** |
254 | * blkg_destroy_all - destroy all blkgs associated with a request_queue | |
255 | * @q: request_queue of interest | |
256 | * @destroy_root: whether to destroy root blkg or not | |
257 | * | |
258 | * Destroy blkgs associated with @q. If @destroy_root is %true, all are | |
259 | * destroyed; otherwise, root blkg is left alone. | |
260 | */ | |
e8989fae | 261 | void blkg_destroy_all(struct request_queue *q, bool destroy_root) |
72e06c25 | 262 | { |
03aa264a | 263 | struct blkio_group *blkg, *n; |
72e06c25 | 264 | |
9f13ef67 | 265 | spin_lock_irq(q->queue_lock); |
72e06c25 | 266 | |
9f13ef67 TH |
267 | list_for_each_entry_safe(blkg, n, &q->blkg_list, q_node) { |
268 | struct blkio_cgroup *blkcg = blkg->blkcg; | |
72e06c25 | 269 | |
9f13ef67 TH |
270 | /* skip root? */ |
271 | if (!destroy_root && blkg->blkcg == &blkio_root_cgroup) | |
272 | continue; | |
72e06c25 | 273 | |
9f13ef67 TH |
274 | spin_lock(&blkcg->lock); |
275 | blkg_destroy(blkg); | |
276 | spin_unlock(&blkcg->lock); | |
72e06c25 | 277 | } |
9f13ef67 TH |
278 | |
279 | spin_unlock_irq(q->queue_lock); | |
72e06c25 | 280 | } |
03aa264a | 281 | EXPORT_SYMBOL_GPL(blkg_destroy_all); |
72e06c25 | 282 | |
1adaf3dd TH |
283 | static void blkg_rcu_free(struct rcu_head *rcu_head) |
284 | { | |
285 | blkg_free(container_of(rcu_head, struct blkio_group, rcu_head)); | |
286 | } | |
287 | ||
288 | void __blkg_release(struct blkio_group *blkg) | |
289 | { | |
290 | /* release the extra blkcg reference this blkg has been holding */ | |
291 | css_put(&blkg->blkcg->css); | |
292 | ||
293 | /* | |
294 | * A group is freed in rcu manner. But having an rcu lock does not | |
295 | * mean that one can access all the fields of blkg and assume these | |
296 | * are valid. For example, don't try to follow throtl_data and | |
297 | * request queue links. | |
298 | * | |
299 | * Having a reference to blkg under an rcu allows acess to only | |
300 | * values local to groups like group stats and group rate limits | |
301 | */ | |
302 | call_rcu(&blkg->rcu_head, blkg_rcu_free); | |
303 | } | |
304 | EXPORT_SYMBOL_GPL(__blkg_release); | |
305 | ||
303a3acb | 306 | static int |
84c124da | 307 | blkiocg_reset_stats(struct cgroup *cgroup, struct cftype *cftype, u64 val) |
303a3acb | 308 | { |
997a026c | 309 | struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgroup); |
303a3acb DS |
310 | struct blkio_group *blkg; |
311 | struct hlist_node *n; | |
bc0d6501 | 312 | int i; |
303a3acb | 313 | |
bc0d6501 | 314 | mutex_lock(&blkcg_pol_mutex); |
303a3acb | 315 | spin_lock_irq(&blkcg->lock); |
997a026c TH |
316 | |
317 | /* | |
318 | * Note that stat reset is racy - it doesn't synchronize against | |
319 | * stat updates. This is a debug feature which shouldn't exist | |
320 | * anyway. If you get hit by a race, retry. | |
321 | */ | |
303a3acb | 322 | hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) { |
8bd435b3 | 323 | for (i = 0; i < BLKCG_MAX_POLS; i++) { |
bc0d6501 | 324 | struct blkio_policy_type *pol = blkio_policy[i]; |
549d3aa8 | 325 | |
bc0d6501 | 326 | if (pol && pol->ops.blkio_reset_group_stats_fn) |
9ade5ea4 | 327 | pol->ops.blkio_reset_group_stats_fn(blkg); |
bc0d6501 | 328 | } |
303a3acb | 329 | } |
f0bdc8cd | 330 | |
303a3acb | 331 | spin_unlock_irq(&blkcg->lock); |
bc0d6501 | 332 | mutex_unlock(&blkcg_pol_mutex); |
303a3acb DS |
333 | return 0; |
334 | } | |
335 | ||
d3d32e69 | 336 | static const char *blkg_dev_name(struct blkio_group *blkg) |
303a3acb | 337 | { |
d3d32e69 TH |
338 | /* some drivers (floppy) instantiate a queue w/o disk registered */ |
339 | if (blkg->q->backing_dev_info.dev) | |
340 | return dev_name(blkg->q->backing_dev_info.dev); | |
341 | return NULL; | |
303a3acb DS |
342 | } |
343 | ||
d3d32e69 TH |
344 | /** |
345 | * blkcg_print_blkgs - helper for printing per-blkg data | |
346 | * @sf: seq_file to print to | |
347 | * @blkcg: blkcg of interest | |
348 | * @prfill: fill function to print out a blkg | |
349 | * @pol: policy in question | |
350 | * @data: data to be passed to @prfill | |
351 | * @show_total: to print out sum of prfill return values or not | |
352 | * | |
353 | * This function invokes @prfill on each blkg of @blkcg if pd for the | |
354 | * policy specified by @pol exists. @prfill is invoked with @sf, the | |
355 | * policy data and @data. If @show_total is %true, the sum of the return | |
356 | * values from @prfill is printed with "Total" label at the end. | |
357 | * | |
358 | * This is to be used to construct print functions for | |
359 | * cftype->read_seq_string method. | |
360 | */ | |
829fdb50 | 361 | void blkcg_print_blkgs(struct seq_file *sf, struct blkio_cgroup *blkcg, |
d366e7ec | 362 | u64 (*prfill)(struct seq_file *, void *, int), |
ec399347 TH |
363 | const struct blkio_policy_type *pol, int data, |
364 | bool show_total) | |
5624a4e4 | 365 | { |
d3d32e69 TH |
366 | struct blkio_group *blkg; |
367 | struct hlist_node *n; | |
368 | u64 total = 0; | |
5624a4e4 | 369 | |
d3d32e69 TH |
370 | spin_lock_irq(&blkcg->lock); |
371 | hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) | |
ec399347 TH |
372 | if (blkg->pd[pol->plid]) |
373 | total += prfill(sf, blkg->pd[pol->plid]->pdata, data); | |
d3d32e69 TH |
374 | spin_unlock_irq(&blkcg->lock); |
375 | ||
376 | if (show_total) | |
377 | seq_printf(sf, "Total %llu\n", (unsigned long long)total); | |
378 | } | |
829fdb50 | 379 | EXPORT_SYMBOL_GPL(blkcg_print_blkgs); |
d3d32e69 TH |
380 | |
381 | /** | |
382 | * __blkg_prfill_u64 - prfill helper for a single u64 value | |
383 | * @sf: seq_file to print to | |
d366e7ec | 384 | * @pdata: policy private data of interest |
d3d32e69 TH |
385 | * @v: value to print |
386 | * | |
d366e7ec | 387 | * Print @v to @sf for the device assocaited with @pdata. |
d3d32e69 | 388 | */ |
d366e7ec | 389 | u64 __blkg_prfill_u64(struct seq_file *sf, void *pdata, u64 v) |
d3d32e69 | 390 | { |
d366e7ec | 391 | const char *dname = blkg_dev_name(pdata_to_blkg(pdata)); |
d3d32e69 TH |
392 | |
393 | if (!dname) | |
394 | return 0; | |
395 | ||
396 | seq_printf(sf, "%s %llu\n", dname, (unsigned long long)v); | |
397 | return v; | |
398 | } | |
829fdb50 | 399 | EXPORT_SYMBOL_GPL(__blkg_prfill_u64); |
d3d32e69 TH |
400 | |
401 | /** | |
402 | * __blkg_prfill_rwstat - prfill helper for a blkg_rwstat | |
403 | * @sf: seq_file to print to | |
d366e7ec | 404 | * @pdata: policy private data of interest |
d3d32e69 TH |
405 | * @rwstat: rwstat to print |
406 | * | |
d366e7ec | 407 | * Print @rwstat to @sf for the device assocaited with @pdata. |
d3d32e69 | 408 | */ |
d366e7ec | 409 | u64 __blkg_prfill_rwstat(struct seq_file *sf, void *pdata, |
829fdb50 | 410 | const struct blkg_rwstat *rwstat) |
d3d32e69 TH |
411 | { |
412 | static const char *rwstr[] = { | |
413 | [BLKG_RWSTAT_READ] = "Read", | |
414 | [BLKG_RWSTAT_WRITE] = "Write", | |
415 | [BLKG_RWSTAT_SYNC] = "Sync", | |
416 | [BLKG_RWSTAT_ASYNC] = "Async", | |
417 | }; | |
d366e7ec | 418 | const char *dname = blkg_dev_name(pdata_to_blkg(pdata)); |
d3d32e69 TH |
419 | u64 v; |
420 | int i; | |
421 | ||
422 | if (!dname) | |
423 | return 0; | |
424 | ||
425 | for (i = 0; i < BLKG_RWSTAT_NR; i++) | |
426 | seq_printf(sf, "%s %s %llu\n", dname, rwstr[i], | |
427 | (unsigned long long)rwstat->cnt[i]); | |
428 | ||
429 | v = rwstat->cnt[BLKG_RWSTAT_READ] + rwstat->cnt[BLKG_RWSTAT_WRITE]; | |
430 | seq_printf(sf, "%s Total %llu\n", dname, (unsigned long long)v); | |
431 | return v; | |
432 | } | |
433 | ||
5bc4afb1 TH |
434 | /** |
435 | * blkg_prfill_stat - prfill callback for blkg_stat | |
436 | * @sf: seq_file to print to | |
437 | * @pdata: policy private data of interest | |
438 | * @off: offset to the blkg_stat in @pdata | |
439 | * | |
440 | * prfill callback for printing a blkg_stat. | |
441 | */ | |
442 | u64 blkg_prfill_stat(struct seq_file *sf, void *pdata, int off) | |
d3d32e69 | 443 | { |
d366e7ec | 444 | return __blkg_prfill_u64(sf, pdata, blkg_stat_read(pdata + off)); |
d3d32e69 | 445 | } |
5bc4afb1 | 446 | EXPORT_SYMBOL_GPL(blkg_prfill_stat); |
d3d32e69 | 447 | |
5bc4afb1 TH |
448 | /** |
449 | * blkg_prfill_rwstat - prfill callback for blkg_rwstat | |
450 | * @sf: seq_file to print to | |
451 | * @pdata: policy private data of interest | |
452 | * @off: offset to the blkg_rwstat in @pdata | |
453 | * | |
454 | * prfill callback for printing a blkg_rwstat. | |
455 | */ | |
456 | u64 blkg_prfill_rwstat(struct seq_file *sf, void *pdata, int off) | |
d3d32e69 | 457 | { |
d366e7ec | 458 | struct blkg_rwstat rwstat = blkg_rwstat_read(pdata + off); |
d3d32e69 | 459 | |
d366e7ec | 460 | return __blkg_prfill_rwstat(sf, pdata, &rwstat); |
d3d32e69 | 461 | } |
5bc4afb1 | 462 | EXPORT_SYMBOL_GPL(blkg_prfill_rwstat); |
d3d32e69 | 463 | |
3a8b31d3 TH |
464 | /** |
465 | * blkg_conf_prep - parse and prepare for per-blkg config update | |
466 | * @blkcg: target block cgroup | |
da8b0662 | 467 | * @pol: target policy |
3a8b31d3 TH |
468 | * @input: input string |
469 | * @ctx: blkg_conf_ctx to be filled | |
470 | * | |
471 | * Parse per-blkg config update from @input and initialize @ctx with the | |
472 | * result. @ctx->blkg points to the blkg to be updated and @ctx->v the new | |
da8b0662 TH |
473 | * value. This function returns with RCU read lock and queue lock held and |
474 | * must be paired with blkg_conf_finish(). | |
3a8b31d3 | 475 | */ |
da8b0662 TH |
476 | int blkg_conf_prep(struct blkio_cgroup *blkcg, |
477 | const struct blkio_policy_type *pol, const char *input, | |
829fdb50 | 478 | struct blkg_conf_ctx *ctx) |
da8b0662 | 479 | __acquires(rcu) __acquires(disk->queue->queue_lock) |
34d0f179 | 480 | { |
3a8b31d3 TH |
481 | struct gendisk *disk; |
482 | struct blkio_group *blkg; | |
726fa694 TH |
483 | unsigned int major, minor; |
484 | unsigned long long v; | |
485 | int part, ret; | |
34d0f179 | 486 | |
726fa694 TH |
487 | if (sscanf(input, "%u:%u %llu", &major, &minor, &v) != 3) |
488 | return -EINVAL; | |
3a8b31d3 | 489 | |
726fa694 | 490 | disk = get_gendisk(MKDEV(major, minor), &part); |
4bfd482e | 491 | if (!disk || part) |
726fa694 | 492 | return -EINVAL; |
e56da7e2 TH |
493 | |
494 | rcu_read_lock(); | |
4bfd482e | 495 | spin_lock_irq(disk->queue->queue_lock); |
da8b0662 | 496 | |
aaec55a0 | 497 | blkg = blkg_lookup_create(blkcg, disk->queue, false); |
e56da7e2 | 498 | |
4bfd482e TH |
499 | if (IS_ERR(blkg)) { |
500 | ret = PTR_ERR(blkg); | |
3a8b31d3 | 501 | rcu_read_unlock(); |
da8b0662 | 502 | spin_unlock_irq(disk->queue->queue_lock); |
3a8b31d3 TH |
503 | put_disk(disk); |
504 | /* | |
505 | * If queue was bypassing, we should retry. Do so after a | |
506 | * short msleep(). It isn't strictly necessary but queue | |
507 | * can be bypassing for some time and it's always nice to | |
508 | * avoid busy looping. | |
509 | */ | |
510 | if (ret == -EBUSY) { | |
511 | msleep(10); | |
512 | ret = restart_syscall(); | |
7702e8f4 | 513 | } |
726fa694 | 514 | return ret; |
062a644d | 515 | } |
3a8b31d3 TH |
516 | |
517 | ctx->disk = disk; | |
518 | ctx->blkg = blkg; | |
726fa694 TH |
519 | ctx->v = v; |
520 | return 0; | |
34d0f179 | 521 | } |
829fdb50 | 522 | EXPORT_SYMBOL_GPL(blkg_conf_prep); |
34d0f179 | 523 | |
3a8b31d3 TH |
524 | /** |
525 | * blkg_conf_finish - finish up per-blkg config update | |
526 | * @ctx: blkg_conf_ctx intiailized by blkg_conf_prep() | |
527 | * | |
528 | * Finish up after per-blkg config update. This function must be paired | |
529 | * with blkg_conf_prep(). | |
530 | */ | |
829fdb50 | 531 | void blkg_conf_finish(struct blkg_conf_ctx *ctx) |
da8b0662 | 532 | __releases(ctx->disk->queue->queue_lock) __releases(rcu) |
34d0f179 | 533 | { |
da8b0662 | 534 | spin_unlock_irq(ctx->disk->queue->queue_lock); |
3a8b31d3 TH |
535 | rcu_read_unlock(); |
536 | put_disk(ctx->disk); | |
34d0f179 | 537 | } |
829fdb50 | 538 | EXPORT_SYMBOL_GPL(blkg_conf_finish); |
34d0f179 | 539 | |
31e4c28d | 540 | struct cftype blkio_files[] = { |
84c124da DS |
541 | { |
542 | .name = "reset_stats", | |
543 | .write_u64 = blkiocg_reset_stats, | |
22084190 | 544 | }, |
4baf6e33 | 545 | { } /* terminate */ |
31e4c28d VG |
546 | }; |
547 | ||
9f13ef67 TH |
548 | /** |
549 | * blkiocg_pre_destroy - cgroup pre_destroy callback | |
9f13ef67 TH |
550 | * @cgroup: cgroup of interest |
551 | * | |
552 | * This function is called when @cgroup is about to go away and responsible | |
553 | * for shooting down all blkgs associated with @cgroup. blkgs should be | |
554 | * removed while holding both q and blkcg locks. As blkcg lock is nested | |
555 | * inside q lock, this function performs reverse double lock dancing. | |
556 | * | |
557 | * This is the blkcg counterpart of ioc_release_fn(). | |
558 | */ | |
959d851c | 559 | static int blkiocg_pre_destroy(struct cgroup *cgroup) |
31e4c28d VG |
560 | { |
561 | struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgroup); | |
b1c35769 | 562 | |
9f13ef67 | 563 | spin_lock_irq(&blkcg->lock); |
7ee9c562 | 564 | |
9f13ef67 TH |
565 | while (!hlist_empty(&blkcg->blkg_list)) { |
566 | struct blkio_group *blkg = hlist_entry(blkcg->blkg_list.first, | |
567 | struct blkio_group, blkcg_node); | |
c875f4d0 | 568 | struct request_queue *q = blkg->q; |
b1c35769 | 569 | |
9f13ef67 TH |
570 | if (spin_trylock(q->queue_lock)) { |
571 | blkg_destroy(blkg); | |
572 | spin_unlock(q->queue_lock); | |
573 | } else { | |
574 | spin_unlock_irq(&blkcg->lock); | |
9f13ef67 | 575 | cpu_relax(); |
a5567932 | 576 | spin_lock_irq(&blkcg->lock); |
0f3942a3 | 577 | } |
9f13ef67 | 578 | } |
b1c35769 | 579 | |
9f13ef67 | 580 | spin_unlock_irq(&blkcg->lock); |
7ee9c562 TH |
581 | return 0; |
582 | } | |
583 | ||
959d851c | 584 | static void blkiocg_destroy(struct cgroup *cgroup) |
7ee9c562 TH |
585 | { |
586 | struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgroup); | |
587 | ||
67523c48 BB |
588 | if (blkcg != &blkio_root_cgroup) |
589 | kfree(blkcg); | |
31e4c28d VG |
590 | } |
591 | ||
761b3ef5 | 592 | static struct cgroup_subsys_state *blkiocg_create(struct cgroup *cgroup) |
31e4c28d | 593 | { |
9a9e8a26 | 594 | static atomic64_t id_seq = ATOMIC64_INIT(0); |
0341509f LZ |
595 | struct blkio_cgroup *blkcg; |
596 | struct cgroup *parent = cgroup->parent; | |
31e4c28d | 597 | |
0341509f | 598 | if (!parent) { |
31e4c28d VG |
599 | blkcg = &blkio_root_cgroup; |
600 | goto done; | |
601 | } | |
602 | ||
31e4c28d VG |
603 | blkcg = kzalloc(sizeof(*blkcg), GFP_KERNEL); |
604 | if (!blkcg) | |
605 | return ERR_PTR(-ENOMEM); | |
606 | ||
3381cb8d | 607 | blkcg->cfq_weight = CFQ_WEIGHT_DEFAULT; |
9a9e8a26 | 608 | blkcg->id = atomic64_inc_return(&id_seq); /* root is 0, start from 1 */ |
31e4c28d VG |
609 | done: |
610 | spin_lock_init(&blkcg->lock); | |
611 | INIT_HLIST_HEAD(&blkcg->blkg_list); | |
612 | ||
613 | return &blkcg->css; | |
614 | } | |
615 | ||
5efd6113 TH |
616 | /** |
617 | * blkcg_init_queue - initialize blkcg part of request queue | |
618 | * @q: request_queue to initialize | |
619 | * | |
620 | * Called from blk_alloc_queue_node(). Responsible for initializing blkcg | |
621 | * part of new request_queue @q. | |
622 | * | |
623 | * RETURNS: | |
624 | * 0 on success, -errno on failure. | |
625 | */ | |
626 | int blkcg_init_queue(struct request_queue *q) | |
627 | { | |
923adde1 TH |
628 | int ret; |
629 | ||
5efd6113 TH |
630 | might_sleep(); |
631 | ||
923adde1 TH |
632 | ret = blk_throtl_init(q); |
633 | if (ret) | |
634 | return ret; | |
635 | ||
636 | mutex_lock(&all_q_mutex); | |
637 | INIT_LIST_HEAD(&q->all_q_node); | |
638 | list_add_tail(&q->all_q_node, &all_q_list); | |
639 | mutex_unlock(&all_q_mutex); | |
640 | ||
641 | return 0; | |
5efd6113 TH |
642 | } |
643 | ||
644 | /** | |
645 | * blkcg_drain_queue - drain blkcg part of request_queue | |
646 | * @q: request_queue to drain | |
647 | * | |
648 | * Called from blk_drain_queue(). Responsible for draining blkcg part. | |
649 | */ | |
650 | void blkcg_drain_queue(struct request_queue *q) | |
651 | { | |
652 | lockdep_assert_held(q->queue_lock); | |
653 | ||
654 | blk_throtl_drain(q); | |
655 | } | |
656 | ||
657 | /** | |
658 | * blkcg_exit_queue - exit and release blkcg part of request_queue | |
659 | * @q: request_queue being released | |
660 | * | |
661 | * Called from blk_release_queue(). Responsible for exiting blkcg part. | |
662 | */ | |
663 | void blkcg_exit_queue(struct request_queue *q) | |
664 | { | |
923adde1 TH |
665 | mutex_lock(&all_q_mutex); |
666 | list_del_init(&q->all_q_node); | |
667 | mutex_unlock(&all_q_mutex); | |
668 | ||
e8989fae TH |
669 | blkg_destroy_all(q, true); |
670 | ||
5efd6113 TH |
671 | blk_throtl_exit(q); |
672 | } | |
673 | ||
31e4c28d VG |
674 | /* |
675 | * We cannot support shared io contexts, as we have no mean to support | |
676 | * two tasks with the same ioc in two different groups without major rework | |
677 | * of the main cic data structures. For now we allow a task to change | |
678 | * its cgroup only if it's the only owner of its ioc. | |
679 | */ | |
761b3ef5 | 680 | static int blkiocg_can_attach(struct cgroup *cgrp, struct cgroup_taskset *tset) |
31e4c28d | 681 | { |
bb9d97b6 | 682 | struct task_struct *task; |
31e4c28d VG |
683 | struct io_context *ioc; |
684 | int ret = 0; | |
685 | ||
686 | /* task_lock() is needed to avoid races with exit_io_context() */ | |
bb9d97b6 TH |
687 | cgroup_taskset_for_each(task, cgrp, tset) { |
688 | task_lock(task); | |
689 | ioc = task->io_context; | |
690 | if (ioc && atomic_read(&ioc->nr_tasks) > 1) | |
691 | ret = -EINVAL; | |
692 | task_unlock(task); | |
693 | if (ret) | |
694 | break; | |
695 | } | |
31e4c28d VG |
696 | return ret; |
697 | } | |
698 | ||
923adde1 TH |
699 | static void blkcg_bypass_start(void) |
700 | __acquires(&all_q_mutex) | |
701 | { | |
702 | struct request_queue *q; | |
703 | ||
704 | mutex_lock(&all_q_mutex); | |
705 | ||
706 | list_for_each_entry(q, &all_q_list, all_q_node) { | |
707 | blk_queue_bypass_start(q); | |
e8989fae | 708 | blkg_destroy_all(q, false); |
923adde1 TH |
709 | } |
710 | } | |
711 | ||
712 | static void blkcg_bypass_end(void) | |
713 | __releases(&all_q_mutex) | |
714 | { | |
715 | struct request_queue *q; | |
716 | ||
717 | list_for_each_entry(q, &all_q_list, all_q_node) | |
718 | blk_queue_bypass_end(q); | |
719 | ||
720 | mutex_unlock(&all_q_mutex); | |
721 | } | |
722 | ||
676f7c8f TH |
723 | struct cgroup_subsys blkio_subsys = { |
724 | .name = "blkio", | |
725 | .create = blkiocg_create, | |
726 | .can_attach = blkiocg_can_attach, | |
959d851c | 727 | .pre_destroy = blkiocg_pre_destroy, |
676f7c8f | 728 | .destroy = blkiocg_destroy, |
676f7c8f | 729 | .subsys_id = blkio_subsys_id, |
4baf6e33 | 730 | .base_cftypes = blkio_files, |
676f7c8f TH |
731 | .module = THIS_MODULE, |
732 | }; | |
733 | EXPORT_SYMBOL_GPL(blkio_subsys); | |
734 | ||
8bd435b3 TH |
735 | /** |
736 | * blkio_policy_register - register a blkcg policy | |
737 | * @blkiop: blkcg policy to register | |
738 | * | |
739 | * Register @blkiop with blkcg core. Might sleep and @blkiop may be | |
740 | * modified on successful registration. Returns 0 on success and -errno on | |
741 | * failure. | |
742 | */ | |
743 | int blkio_policy_register(struct blkio_policy_type *blkiop) | |
3e252066 | 744 | { |
e8989fae | 745 | struct request_queue *q; |
8bd435b3 | 746 | int i, ret; |
e8989fae | 747 | |
bc0d6501 TH |
748 | mutex_lock(&blkcg_pol_mutex); |
749 | ||
8bd435b3 TH |
750 | /* find an empty slot */ |
751 | ret = -ENOSPC; | |
752 | for (i = 0; i < BLKCG_MAX_POLS; i++) | |
753 | if (!blkio_policy[i]) | |
754 | break; | |
755 | if (i >= BLKCG_MAX_POLS) | |
756 | goto out_unlock; | |
035d10b2 | 757 | |
8bd435b3 TH |
758 | /* register and update blkgs */ |
759 | blkiop->plid = i; | |
760 | blkio_policy[i] = blkiop; | |
761 | ||
762 | blkcg_bypass_start(); | |
e8989fae | 763 | list_for_each_entry(q, &all_q_list, all_q_node) |
ec399347 | 764 | update_root_blkg_pd(q, blkiop); |
923adde1 | 765 | blkcg_bypass_end(); |
44ea53de | 766 | |
8bd435b3 | 767 | /* everything is in place, add intf files for the new policy */ |
44ea53de TH |
768 | if (blkiop->cftypes) |
769 | WARN_ON(cgroup_add_cftypes(&blkio_subsys, blkiop->cftypes)); | |
8bd435b3 TH |
770 | ret = 0; |
771 | out_unlock: | |
bc0d6501 | 772 | mutex_unlock(&blkcg_pol_mutex); |
8bd435b3 | 773 | return ret; |
3e252066 VG |
774 | } |
775 | EXPORT_SYMBOL_GPL(blkio_policy_register); | |
776 | ||
8bd435b3 TH |
777 | /** |
778 | * blkiop_policy_unregister - unregister a blkcg policy | |
779 | * @blkiop: blkcg policy to unregister | |
780 | * | |
781 | * Undo blkio_policy_register(@blkiop). Might sleep. | |
782 | */ | |
3e252066 VG |
783 | void blkio_policy_unregister(struct blkio_policy_type *blkiop) |
784 | { | |
e8989fae TH |
785 | struct request_queue *q; |
786 | ||
bc0d6501 TH |
787 | mutex_lock(&blkcg_pol_mutex); |
788 | ||
8bd435b3 TH |
789 | if (WARN_ON(blkio_policy[blkiop->plid] != blkiop)) |
790 | goto out_unlock; | |
791 | ||
792 | /* kill the intf files first */ | |
44ea53de TH |
793 | if (blkiop->cftypes) |
794 | cgroup_rm_cftypes(&blkio_subsys, blkiop->cftypes); | |
795 | ||
8bd435b3 | 796 | /* unregister and update blkgs */ |
035d10b2 | 797 | blkio_policy[blkiop->plid] = NULL; |
035d10b2 | 798 | |
8bd435b3 | 799 | blkcg_bypass_start(); |
e8989fae | 800 | list_for_each_entry(q, &all_q_list, all_q_node) |
ec399347 | 801 | update_root_blkg_pd(q, blkiop); |
923adde1 | 802 | blkcg_bypass_end(); |
8bd435b3 | 803 | out_unlock: |
bc0d6501 | 804 | mutex_unlock(&blkcg_pol_mutex); |
3e252066 VG |
805 | } |
806 | EXPORT_SYMBOL_GPL(blkio_policy_unregister); |