Skip to content

Commit e1a0202

Browse files
black-deskwinnscode
authored andcommitted
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 <[email protected]> Signed-off-by: Wen Tao <[email protected]> Signed-off-by: Chen Linxuan <[email protected]> Reviewed-by: Michal Koutný <[email protected]> Acked-by: Tejun Heo <[email protected]> Reviewed-by: Yu Kuai <[email protected]> Link: https://lore.kernel.org/r/3E333A73B6B6DFC0+20250317022924.150907-1-chenlinxuan@uniontech.com Signed-off-by: Jens Axboe <[email protected]>
1 parent 86947bd commit e1a0202

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
@@ -1727,27 +1727,27 @@ int blkcg_policy_register(struct blkcg_policy *pol)
17271727
struct blkcg *blkcg;
17281728
int i, ret;
17291729

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

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

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

17601760
cpd = pol->cpd_alloc_fn(GFP_KERNEL);
1761-
if (!cpd)
1761+
if (!cpd) {
1762+
ret = -ENOMEM;
17621763
goto err_free_cpds;
1764+
}
17631765

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

0 commit comments

Comments
 (0)