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