[PATCH] blk-cgroup: validate alloc/free function pairs at the start of blkcg_policy_register()

Chen Linxuan posted 1 patch 12 months ago
block/blk-cgroup.c | 16 ++++++++--------
1 file changed, 8 insertions(+), 8 deletions(-)
[PATCH] blk-cgroup: validate alloc/free function pairs at the start of blkcg_policy_register()
Posted by Chen Linxuan 12 months ago
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.

Co-authored-by: Wen Tao <wentao@uniontech.com>
Signed-off-by: Wen Tao <wentao@uniontech.com>
Signed-off-by: Chen Linxuan <chenlinxuan@uniontech.com>
---
 block/blk-cgroup.c | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)

diff --git a/block/blk-cgroup.c b/block/blk-cgroup.c
index 9ed93d91d754..81c166ee003b 100644
--- a/block/blk-cgroup.c
+++ b/block/blk-cgroup.c
@@ -1727,6 +1727,14 @@ int blkcg_policy_register(struct blkcg_policy *pol)
 	struct blkcg *blkcg;
 	int i, ret;
 
+	/*
+	 * 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);
 
@@ -1740,14 +1748,6 @@ int blkcg_policy_register(struct blkcg_policy *pol)
 		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))
-		goto err_unlock;
-
 	/* register @pol */
 	pol->plid = i;
 	blkcg_policy[pol->plid] = pol;
-- 
2.43.0
Re: [PATCH] blk-cgroup: validate alloc/free function pairs at the start of blkcg_policy_register()
Posted by Michal Koutný 12 months ago
Hello Linxuan.

On Mon, Feb 10, 2025 at 11:18:27AM +0800, Chen Linxuan <chenlinxuan@uniontech.com> wrote:
> 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.
> 
> Co-authored-by: Wen Tao <wentao@uniontech.com>
> Signed-off-by: Wen Tao <wentao@uniontech.com>
> Signed-off-by: Chen Linxuan <chenlinxuan@uniontech.com>

If you consider those locks contention a problem (policy registrations
are "only" boot time, possibly module load time), then it's good to refer

Fixes: e84010732225c ("blkcg: add sanity check for blkcg policy operations")

> ---
>  block/blk-cgroup.c | 16 ++++++++--------
>  1 file changed, 8 insertions(+), 8 deletions(-)

But it's correct,
Reviewed-by: Michal Koutný <mkoutny@suse.com>
Re: [PATCH] blk-cgroup: validate alloc/free function pairs at the start of blkcg_policy_register()
Posted by Yu Kuai 12 months ago
Hi,

在 2025/02/11 21:57, Michal Koutný 写道:
> Hello Linxuan.
> 
> On Mon, Feb 10, 2025 at 11:18:27AM +0800, Chen Linxuan <chenlinxuan@uniontech.com> wrote:
>> 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.
>>
>> Co-authored-by: Wen Tao <wentao@uniontech.com>
>> Signed-off-by: Wen Tao <wentao@uniontech.com>
>> Signed-off-by: Chen Linxuan <chenlinxuan@uniontech.com>
> 
> If you consider those locks contention a problem (policy registrations
> are "only" boot time, possibly module load time), then it's good to refer
> 
> Fixes: e84010732225c ("blkcg: add sanity check for blkcg policy operations")

This is super cold path, so I don't think it's a problem.
> 
>> ---
>>   block/blk-cgroup.c | 16 ++++++++--------
>>   1 file changed, 8 insertions(+), 8 deletions(-)
> 
> But it's correct,
> Reviewed-by: Michal Koutný <mkoutny@suse.com>
> 

Since we're here, can you also change the return value to -ENOMEM from
error path err_free_cpds?

Thanks,
Kuai