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

From: Chen Linxuan
Date: Sun Feb 09 2025 - 22:20:21 EST


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@xxxxxxxxxxxxx>
Signed-off-by: Wen Tao <wentao@xxxxxxxxxxxxx>
Signed-off-by: Chen Linxuan <chenlinxuan@xxxxxxxxxxxxx>
---
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