diff --git a/block/blk-cgroup.c b/block/blk-cgroup.c index 9ed93d91d754..2609f7294427 100644 --- a/block/blk-cgroup.c +++ b/block/blk-cgroup.c @@ -1727,26 +1727,26 @@ int blkcg_policy_register(struct blkcg_policy *pol) struct blkcg *blkcg; int i, ret; - mutex_lock(&blkcg_pol_register_mutex); - mutex_lock(&blkcg_pol_mutex); - - /* find an empty slot */ - ret = -ENOSPC; - for (i = 0; i < BLKCG_MAX_POLS; i++) - if (!blkcg_policy[i]) - break; - if (i >= BLKCG_MAX_POLS) { - pr_warn("blkcg_policy_register: BLKCG_MAX_POLS too small\n"); - goto err_unlock; - } - /* * Make sure cpd/pd_alloc_fn and cpd/pd_free_fn in pairs, and policy * without pd_alloc_fn/pd_free_fn can't be activated. */ if ((!pol->cpd_alloc_fn ^ !pol->cpd_free_fn) || (!pol->pd_alloc_fn ^ !pol->pd_free_fn)) + return -EINVAL; + + mutex_lock(&blkcg_pol_register_mutex); + mutex_lock(&blkcg_pol_mutex); + + /* find an empty slot */ + for (i = 0; i < BLKCG_MAX_POLS; i++) + if (!blkcg_policy[i]) + break; + if (i >= BLKCG_MAX_POLS) { + pr_warn("blkcg_policy_register: BLKCG_MAX_POLS too small\n"); + ret = -ENOSPC; goto err_unlock; + } /* register @pol */ pol->plid = i; @@ -1758,8 +1758,10 @@ int blkcg_policy_register(struct blkcg_policy *pol) struct blkcg_policy_data *cpd; cpd = pol->cpd_alloc_fn(GFP_KERNEL); - if (!cpd) + if (!cpd) { + ret = -ENOMEM; goto err_free_cpds; + } blkcg->cpd[pol->plid] = cpd; cpd->blkcg = blkcg;