Skip to content

Commit 0c1dcc1

Browse files
author
Ming Lei
committed
blk-cgroup: improve policy registration error handling
JIRA: https://issues.redhat.com/browse/RHEL-112997 commit e1a0202 Author: Chen Linxuan <chenlinxuan@uniontech.com> Date: Mon Mar 17 10:29:24 2025 +0800 blk-cgroup: improve policy registration error handling This patch improve the returned error code of blkcg_policy_register(). 1. Move the validation check for cpd/pd_alloc_fn and cpd/pd_free_fn function pairs to the start of blkcg_policy_register(). This ensures we immediately return -EINVAL if the function pairs are not correctly provided, rather than returning -ENOSPC after locking and unlocking mutexes unnecessarily. Those locks should not contention any problems, as error of policy registration is a super cold path. 2. Return -ENOMEM when cpd_alloc_fn() failed. Co-authored-by: Wen Tao <wentao@uniontech.com> Signed-off-by: Wen Tao <wentao@uniontech.com> Signed-off-by: Chen Linxuan <chenlinxuan@uniontech.com> Reviewed-by: Michal Koutný <mkoutny@suse.com> Acked-by: Tejun Heo <tj@kernel.org> Reviewed-by: Yu Kuai <yukuai3@huawei.com> Link: https://lore.kernel.org/r/3E333A73B6B6DFC0+20250317022924.150907-1-chenlinxuan@uniontech.com Signed-off-by: Jens Axboe <axboe@kernel.dk> Signed-off-by: Ming Lei <ming.lei@redhat.com>
1 parent 976f370 commit 0c1dcc1

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

block/blk-cgroup.c

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1728,27 +1728,27 @@ int blkcg_policy_register(struct blkcg_policy *pol)
17281728
struct blkcg *blkcg;
17291729
int i, ret;
17301730

1731+
/*
1732+
* Make sure cpd/pd_alloc_fn and cpd/pd_free_fn in pairs, and policy
1733+
* without pd_alloc_fn/pd_free_fn can't be activated.
1734+
*/
1735+
if ((!pol->cpd_alloc_fn ^ !pol->cpd_free_fn) ||
1736+
(!pol->pd_alloc_fn ^ !pol->pd_free_fn))
1737+
return -EINVAL;
1738+
17311739
mutex_lock(&blkcg_pol_register_mutex);
17321740
mutex_lock(&blkcg_pol_mutex);
17331741

17341742
/* find an empty slot */
1735-
ret = -ENOSPC;
17361743
for (i = 0; i < BLKCG_MAX_POLS; i++)
17371744
if (!blkcg_policy[i])
17381745
break;
17391746
if (i >= BLKCG_MAX_POLS) {
17401747
pr_warn("blkcg_policy_register: BLKCG_MAX_POLS too small\n");
1748+
ret = -ENOSPC;
17411749
goto err_unlock;
17421750
}
17431751

1744-
/*
1745-
* Make sure cpd/pd_alloc_fn and cpd/pd_free_fn in pairs, and policy
1746-
* without pd_alloc_fn/pd_free_fn can't be activated.
1747-
*/
1748-
if ((!pol->cpd_alloc_fn ^ !pol->cpd_free_fn) ||
1749-
(!pol->pd_alloc_fn ^ !pol->pd_free_fn))
1750-
goto err_unlock;
1751-
17521752
/* register @pol */
17531753
pol->plid = i;
17541754
blkcg_policy[pol->plid] = pol;
@@ -1759,8 +1759,10 @@ int blkcg_policy_register(struct blkcg_policy *pol)
17591759
struct blkcg_policy_data *cpd;
17601760

17611761
cpd = pol->cpd_alloc_fn(GFP_KERNEL);
1762-
if (!cpd)
1762+
if (!cpd) {
1763+
ret = -ENOMEM;
17631764
goto err_free_cpds;
1765+
}
17641766

17651767
blkcg->cpd[pol->plid] = cpd;
17661768
cpd->blkcg = blkcg;

0 commit comments

Comments
 (0)