diff options
author | Tejun Heo <tj@kernel.org> | 2012-03-05 16:15:06 -0500 |
---|---|---|
committer | Jens Axboe <axboe@kernel.dk> | 2012-03-06 15:27:22 -0500 |
commit | cd1604fab4f95f7cfc227d3955fd7ae14da61f38 (patch) | |
tree | 021881faedc1c2468730f9f54d364083e70dce76 /block/blk-cgroup.c | |
parent | f51b802c17e2a21926b29911493f5e7ddf6eee87 (diff) |
blkcg: factor out blkio_group creation
Currently both blk-throttle and cfq-iosched implement their own
blkio_group creation code in throtl_get_tg() and cfq_get_cfqg(). This
patch factors out the common code into blkg_lookup_create(), which
returns ERR_PTR value so that transitional failures due to queue
bypass can be distinguished from other failures.
* New plkio_policy_ops methods blkio_alloc_group_fn() and
blkio_link_group_fn added. Both are transitional and will be
removed once the blkg management code is fully moved into
blk-cgroup.c.
* blkio_alloc_group_fn() allocates policy-specific blkg which is
usually a larger data structure with blkg as the first entry and
intiailizes it. Note that initialization of blkg proper, including
percpu stats, is responsibility of blk-cgroup proper.
Note that default config (weight, bps...) initialization is done
from this method; otherwise, we end up violating locking order
between blkcg and q locks via blkcg_get_CONF() functions.
* blkio_link_group_fn() is called under queue_lock and responsible for
linking the blkg to the queue. blkcg side is handled by blk-cgroup
proper.
* The common blkg creation function is named blkg_lookup_create() and
blkiocg_lookup_group() is renamed to blkg_lookup() for consistency.
Also, throtl / cfq related functions are similarly [re]named for
consistency.
This simplifies blkcg policy implementations and enables further
cleanup.
-v2: Vivek noticed that blkg_lookup_create() incorrectly tested
blk_queue_dead() instead of blk_queue_bypass() leading a user of
the function ending up creating a new blkg on bypassing queue.
This is a bug introduced while relocating bypass patches before
this one. Fixed.
-v3: ERR_PTR patch folded into this one. @for_root added to
blkg_lookup_create() to allow creating root group on a bypassed
queue during elevator switch.
Signed-off-by: Tejun Heo <tj@kernel.org>
Cc: Vivek Goyal <vgoyal@redhat.com>
Signed-off-by: Jens Axboe <axboe@kernel.dk>
Diffstat (limited to 'block/blk-cgroup.c')
-rw-r--r-- | block/blk-cgroup.c | 117 |
1 files changed, 86 insertions, 31 deletions
diff --git a/block/blk-cgroup.c b/block/blk-cgroup.c index f1b08d3cba55..bc9891496318 100644 --- a/block/blk-cgroup.c +++ b/block/blk-cgroup.c | |||
@@ -465,38 +465,93 @@ void blkiocg_update_io_merged_stats(struct blkio_group *blkg, bool direction, | |||
465 | } | 465 | } |
466 | EXPORT_SYMBOL_GPL(blkiocg_update_io_merged_stats); | 466 | EXPORT_SYMBOL_GPL(blkiocg_update_io_merged_stats); |
467 | 467 | ||
468 | /* | 468 | struct blkio_group *blkg_lookup_create(struct blkio_cgroup *blkcg, |
469 | * This function allocates the per cpu stats for blkio_group. Should be called | 469 | struct request_queue *q, |
470 | * from sleepable context as alloc_per_cpu() requires that. | 470 | enum blkio_policy_id plid, |
471 | */ | 471 | bool for_root) |
472 | int blkio_alloc_blkg_stats(struct blkio_group *blkg) | 472 | __releases(q->queue_lock) __acquires(q->queue_lock) |
473 | { | 473 | { |
474 | /* Allocate memory for per cpu stats */ | 474 | struct blkio_policy_type *pol = blkio_policy[plid]; |
475 | blkg->stats_cpu = alloc_percpu(struct blkio_group_stats_cpu); | 475 | struct blkio_group *blkg, *new_blkg; |
476 | if (!blkg->stats_cpu) | ||
477 | return -ENOMEM; | ||
478 | return 0; | ||
479 | } | ||
480 | EXPORT_SYMBOL_GPL(blkio_alloc_blkg_stats); | ||
481 | 476 | ||
482 | void blkiocg_add_blkio_group(struct blkio_cgroup *blkcg, | 477 | WARN_ON_ONCE(!rcu_read_lock_held()); |
483 | struct blkio_group *blkg, struct request_queue *q, dev_t dev, | 478 | lockdep_assert_held(q->queue_lock); |
484 | enum blkio_policy_id plid) | ||
485 | { | ||
486 | unsigned long flags; | ||
487 | 479 | ||
488 | spin_lock_irqsave(&blkcg->lock, flags); | 480 | /* |
489 | spin_lock_init(&blkg->stats_lock); | 481 | * This could be the first entry point of blkcg implementation and |
490 | rcu_assign_pointer(blkg->q, q); | 482 | * we shouldn't allow anything to go through for a bypassing queue. |
491 | blkg->blkcg_id = css_id(&blkcg->css); | 483 | * The following can be removed if blkg lookup is guaranteed to |
484 | * fail on a bypassing queue. | ||
485 | */ | ||
486 | if (unlikely(blk_queue_bypass(q)) && !for_root) | ||
487 | return ERR_PTR(blk_queue_dead(q) ? -EINVAL : -EBUSY); | ||
488 | |||
489 | blkg = blkg_lookup(blkcg, q, plid); | ||
490 | if (blkg) | ||
491 | return blkg; | ||
492 | |||
493 | if (!css_tryget(&blkcg->css)) | ||
494 | return ERR_PTR(-EINVAL); | ||
495 | |||
496 | /* | ||
497 | * Allocate and initialize. | ||
498 | * | ||
499 | * FIXME: The following is broken. Percpu memory allocation | ||
500 | * requires %GFP_KERNEL context and can't be performed from IO | ||
501 | * path. Allocation here should inherently be atomic and the | ||
502 | * following lock dancing can be removed once the broken percpu | ||
503 | * allocation is fixed. | ||
504 | */ | ||
505 | spin_unlock_irq(q->queue_lock); | ||
506 | rcu_read_unlock(); | ||
507 | |||
508 | new_blkg = pol->ops.blkio_alloc_group_fn(q, blkcg); | ||
509 | if (new_blkg) { | ||
510 | new_blkg->stats_cpu = alloc_percpu(struct blkio_group_stats_cpu); | ||
511 | |||
512 | spin_lock_init(&new_blkg->stats_lock); | ||
513 | rcu_assign_pointer(new_blkg->q, q); | ||
514 | new_blkg->blkcg_id = css_id(&blkcg->css); | ||
515 | new_blkg->plid = plid; | ||
516 | cgroup_path(blkcg->css.cgroup, new_blkg->path, | ||
517 | sizeof(new_blkg->path)); | ||
518 | } | ||
519 | |||
520 | rcu_read_lock(); | ||
521 | spin_lock_irq(q->queue_lock); | ||
522 | css_put(&blkcg->css); | ||
523 | |||
524 | /* did bypass get turned on inbetween? */ | ||
525 | if (unlikely(blk_queue_bypass(q)) && !for_root) { | ||
526 | blkg = ERR_PTR(blk_queue_dead(q) ? -EINVAL : -EBUSY); | ||
527 | goto out; | ||
528 | } | ||
529 | |||
530 | /* did someone beat us to it? */ | ||
531 | blkg = blkg_lookup(blkcg, q, plid); | ||
532 | if (unlikely(blkg)) | ||
533 | goto out; | ||
534 | |||
535 | /* did alloc fail? */ | ||
536 | if (unlikely(!new_blkg || !new_blkg->stats_cpu)) { | ||
537 | blkg = ERR_PTR(-ENOMEM); | ||
538 | goto out; | ||
539 | } | ||
540 | |||
541 | /* insert */ | ||
542 | spin_lock(&blkcg->lock); | ||
543 | swap(blkg, new_blkg); | ||
492 | hlist_add_head_rcu(&blkg->blkcg_node, &blkcg->blkg_list); | 544 | hlist_add_head_rcu(&blkg->blkcg_node, &blkcg->blkg_list); |
493 | blkg->plid = plid; | 545 | pol->ops.blkio_link_group_fn(q, blkg); |
494 | spin_unlock_irqrestore(&blkcg->lock, flags); | 546 | spin_unlock(&blkcg->lock); |
495 | /* Need to take css reference ? */ | 547 | out: |
496 | cgroup_path(blkcg->css.cgroup, blkg->path, sizeof(blkg->path)); | 548 | if (new_blkg) { |
497 | blkg->dev = dev; | 549 | free_percpu(new_blkg->stats_cpu); |
550 | kfree(new_blkg); | ||
551 | } | ||
552 | return blkg; | ||
498 | } | 553 | } |
499 | EXPORT_SYMBOL_GPL(blkiocg_add_blkio_group); | 554 | EXPORT_SYMBOL_GPL(blkg_lookup_create); |
500 | 555 | ||
501 | static void __blkiocg_del_blkio_group(struct blkio_group *blkg) | 556 | static void __blkiocg_del_blkio_group(struct blkio_group *blkg) |
502 | { | 557 | { |
@@ -533,9 +588,9 @@ int blkiocg_del_blkio_group(struct blkio_group *blkg) | |||
533 | EXPORT_SYMBOL_GPL(blkiocg_del_blkio_group); | 588 | EXPORT_SYMBOL_GPL(blkiocg_del_blkio_group); |
534 | 589 | ||
535 | /* called under rcu_read_lock(). */ | 590 | /* called under rcu_read_lock(). */ |
536 | struct blkio_group *blkiocg_lookup_group(struct blkio_cgroup *blkcg, | 591 | struct blkio_group *blkg_lookup(struct blkio_cgroup *blkcg, |
537 | struct request_queue *q, | 592 | struct request_queue *q, |
538 | enum blkio_policy_id plid) | 593 | enum blkio_policy_id plid) |
539 | { | 594 | { |
540 | struct blkio_group *blkg; | 595 | struct blkio_group *blkg; |
541 | struct hlist_node *n; | 596 | struct hlist_node *n; |
@@ -545,7 +600,7 @@ struct blkio_group *blkiocg_lookup_group(struct blkio_cgroup *blkcg, | |||
545 | return blkg; | 600 | return blkg; |
546 | return NULL; | 601 | return NULL; |
547 | } | 602 | } |
548 | EXPORT_SYMBOL_GPL(blkiocg_lookup_group); | 603 | EXPORT_SYMBOL_GPL(blkg_lookup); |
549 | 604 | ||
550 | void blkg_destroy_all(struct request_queue *q) | 605 | void blkg_destroy_all(struct request_queue *q) |
551 | { | 606 | { |