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