Skip to content

Commit 457e490

Browse files
Tahsin Erdoganaxboe
authored andcommitted
blkcg: allocate struct blkcg_gq outside request queue spinlock
blkg_conf_prep() currently calls blkg_lookup_create() while holding request queue spinlock. This means allocating memory for struct blkcg_gq has to be made non-blocking. This causes occasional -ENOMEM failures in call paths like below: pcpu_alloc+0x68f/0x710 __alloc_percpu_gfp+0xd/0x10 __percpu_counter_init+0x55/0xc0 cfq_pd_alloc+0x3b2/0x4e0 blkg_alloc+0x187/0x230 blkg_create+0x489/0x670 blkg_lookup_create+0x9a/0x230 blkg_conf_prep+0x1fb/0x240 __cfqg_set_weight_device.isra.105+0x5c/0x180 cfq_set_weight_on_dfl+0x69/0xc0 cgroup_file_write+0x39/0x1c0 kernfs_fop_write+0x13f/0x1d0 __vfs_write+0x23/0x120 vfs_write+0xc2/0x1f0 SyS_write+0x44/0xb0 entry_SYSCALL_64_fastpath+0x18/0xad In the code path above, percpu allocator cannot call vmalloc() due to queue spinlock. A failure in this call path gives grief to tools which are trying to configure io weights. We see occasional failures happen shortly after reboots even when system is not under any memory pressure. Machines with a lot of cpus are more vulnerable to this condition. Do struct blkcg_gq allocations outside the queue spinlock to allow blocking during memory allocations. Suggested-by: Tejun Heo <tj@kernel.org> Signed-off-by: Tahsin Erdogan <tahsin@google.com> Acked-by: Tejun Heo <tj@kernel.org> Signed-off-by: Jens Axboe <axboe@fb.com>
1 parent d708f0d commit 457e490

1 file changed

Lines changed: 98 additions & 25 deletions

File tree

‎block/blk-cgroup.c‎

Lines changed: 98 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -772,6 +772,27 @@ struct blkg_rwstat blkg_rwstat_recursive_sum(struct blkcg_gq *blkg,
772772
}
773773
EXPORT_SYMBOL_GPL(blkg_rwstat_recursive_sum);
774774

775+
/* Performs queue bypass and policy enabled checks then looks up blkg. */
776+
static struct blkcg_gq *blkg_lookup_check(struct blkcg *blkcg,
777+
const struct blkcg_policy *pol,
778+
struct request_queue *q)
779+
{
780+
WARN_ON_ONCE(!rcu_read_lock_held());
781+
lockdep_assert_held(q->queue_lock);
782+
783+
if (!blkcg_policy_enabled(q, pol))
784+
return ERR_PTR(-EOPNOTSUPP);
785+
786+
/*
787+
* This could be the first entry point of blkcg implementation and
788+
* we shouldn't allow anything to go through for a bypassing queue.
789+
*/
790+
if (unlikely(blk_queue_bypass(q)))
791+
return ERR_PTR(blk_queue_dying(q) ? -ENODEV : -EBUSY);
792+
793+
return __blkg_lookup(blkcg, q, true /* update_hint */);
794+
}
795+
775796
/**
776797
* blkg_conf_prep - parse and prepare for per-blkg config update
777798
* @blkcg: target block cgroup
@@ -789,6 +810,7 @@ int blkg_conf_prep(struct blkcg *blkcg, const struct blkcg_policy *pol,
789810
__acquires(rcu) __acquires(disk->queue->queue_lock)
790811
{
791812
struct gendisk *disk;
813+
struct request_queue *q;
792814
struct blkcg_gq *blkg;
793815
struct module *owner;
794816
unsigned int major, minor;
@@ -807,44 +829,95 @@ int blkg_conf_prep(struct blkcg *blkcg, const struct blkcg_policy *pol,
807829
if (!disk)
808830
return -ENODEV;
809831
if (part) {
810-
owner = disk->fops->owner;
811-
put_disk(disk);
812-
module_put(owner);
813-
return -ENODEV;
832+
ret = -ENODEV;
833+
goto fail;
814834
}
815835

816-
rcu_read_lock();
817-
spin_lock_irq(disk->queue->queue_lock);
836+
q = disk->queue;
818837

819-
if (blkcg_policy_enabled(disk->queue, pol))
820-
blkg = blkg_lookup_create(blkcg, disk->queue);
821-
else
822-
blkg = ERR_PTR(-EOPNOTSUPP);
838+
rcu_read_lock();
839+
spin_lock_irq(q->queue_lock);
823840

841+
blkg = blkg_lookup_check(blkcg, pol, q);
824842
if (IS_ERR(blkg)) {
825843
ret = PTR_ERR(blkg);
844+
goto fail_unlock;
845+
}
846+
847+
if (blkg)
848+
goto success;
849+
850+
/*
851+
* Create blkgs walking down from blkcg_root to @blkcg, so that all
852+
* non-root blkgs have access to their parents.
853+
*/
854+
while (true) {
855+
struct blkcg *pos = blkcg;
856+
struct blkcg *parent;
857+
struct blkcg_gq *new_blkg;
858+
859+
parent = blkcg_parent(blkcg);
860+
while (parent && !__blkg_lookup(parent, q, false)) {
861+
pos = parent;
862+
parent = blkcg_parent(parent);
863+
}
864+
865+
/* Drop locks to do new blkg allocation with GFP_KERNEL. */
866+
spin_unlock_irq(q->queue_lock);
826867
rcu_read_unlock();
827-
spin_unlock_irq(disk->queue->queue_lock);
828-
owner = disk->fops->owner;
829-
put_disk(disk);
830-
module_put(owner);
831-
/*
832-
* If queue was bypassing, we should retry. Do so after a
833-
* short msleep(). It isn't strictly necessary but queue
834-
* can be bypassing for some time and it's always nice to
835-
* avoid busy looping.
836-
*/
837-
if (ret == -EBUSY) {
838-
msleep(10);
839-
ret = restart_syscall();
868+
869+
new_blkg = blkg_alloc(pos, q, GFP_KERNEL);
870+
if (unlikely(!new_blkg)) {
871+
ret = -ENOMEM;
872+
goto fail;
840873
}
841-
return ret;
842-
}
843874

875+
rcu_read_lock();
876+
spin_lock_irq(q->queue_lock);
877+
878+
blkg = blkg_lookup_check(pos, pol, q);
879+
if (IS_ERR(blkg)) {
880+
ret = PTR_ERR(blkg);
881+
goto fail_unlock;
882+
}
883+
884+
if (blkg) {
885+
blkg_free(new_blkg);
886+
} else {
887+
blkg = blkg_create(pos, q, new_blkg);
888+
if (unlikely(IS_ERR(blkg))) {
889+
ret = PTR_ERR(blkg);
890+
goto fail_unlock;
891+
}
892+
}
893+
894+
if (pos == blkcg)
895+
goto success;
896+
}
897+
success:
844898
ctx->disk = disk;
845899
ctx->blkg = blkg;
846900
ctx->body = body;
847901
return 0;
902+
903+
fail_unlock:
904+
spin_unlock_irq(q->queue_lock);
905+
rcu_read_unlock();
906+
fail:
907+
owner = disk->fops->owner;
908+
put_disk(disk);
909+
module_put(owner);
910+
/*
911+
* If queue was bypassing, we should retry. Do so after a
912+
* short msleep(). It isn't strictly necessary but queue
913+
* can be bypassing for some time and it's always nice to
914+
* avoid busy looping.
915+
*/
916+
if (ret == -EBUSY) {
917+
msleep(10);
918+
ret = restart_syscall();
919+
}
920+
return ret;
848921
}
849922
EXPORT_SYMBOL_GPL(blkg_conf_prep);
850923

0 commit comments

Comments
 (0)