Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

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>

authored by

Chen Linxuan
Wen Tao
and committed by
Jens Axboe
e1a0202c 86947bdc

+16 -14
+16 -14
block/blk-cgroup.c
··· 1727 1727 struct blkcg *blkcg; 1728 1728 int i, ret; 1729 1729 1730 - mutex_lock(&blkcg_pol_register_mutex); 1731 - mutex_lock(&blkcg_pol_mutex); 1732 - 1733 - /* find an empty slot */ 1734 - ret = -ENOSPC; 1735 - for (i = 0; i < BLKCG_MAX_POLS; i++) 1736 - if (!blkcg_policy[i]) 1737 - break; 1738 - if (i >= BLKCG_MAX_POLS) { 1739 - pr_warn("blkcg_policy_register: BLKCG_MAX_POLS too small\n"); 1740 - goto err_unlock; 1741 - } 1742 - 1743 1730 /* 1744 1731 * Make sure cpd/pd_alloc_fn and cpd/pd_free_fn in pairs, and policy 1745 1732 * without pd_alloc_fn/pd_free_fn can't be activated. 1746 1733 */ 1747 1734 if ((!pol->cpd_alloc_fn ^ !pol->cpd_free_fn) || 1748 1735 (!pol->pd_alloc_fn ^ !pol->pd_free_fn)) 1736 + return -EINVAL; 1737 + 1738 + mutex_lock(&blkcg_pol_register_mutex); 1739 + mutex_lock(&blkcg_pol_mutex); 1740 + 1741 + /* find an empty slot */ 1742 + for (i = 0; i < BLKCG_MAX_POLS; i++) 1743 + if (!blkcg_policy[i]) 1744 + break; 1745 + if (i >= BLKCG_MAX_POLS) { 1746 + pr_warn("blkcg_policy_register: BLKCG_MAX_POLS too small\n"); 1747 + ret = -ENOSPC; 1749 1748 goto err_unlock; 1749 + } 1750 1750 1751 1751 /* register @pol */ 1752 1752 pol->plid = i; ··· 1758 1758 struct blkcg_policy_data *cpd; 1759 1759 1760 1760 cpd = pol->cpd_alloc_fn(GFP_KERNEL); 1761 - if (!cpd) 1761 + if (!cpd) { 1762 + ret = -ENOMEM; 1762 1763 goto err_free_cpds; 1764 + } 1763 1765 1764 1766 blkcg->cpd[pol->plid] = cpd; 1765 1767 cpd->blkcg = blkcg;