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