From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1F62F2989BC; Mon, 5 May 2025 22:42:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746484927; cv=none; b=tNkheTeaGqMfGKBYCqLZw0lVwK6wRuCGdXnK4xByHqwBx2pgnzR/cDW30JYkwbfiT3K3qn+rrEuit2JDfFGywUre8yvTfu8DwfSbkKAgel/TILu+KFcVQSof/t3jly0u1r+qGyyZIV7QziEFU5lOL8iHCUmUwHswwVc70vhXtYA= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746484927; c=relaxed/simple; bh=B2Sst9Y20fMXhTIfWJF5SBOWaQ6eWmMfQDIXffAOvlA=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version:Content-Type; b=G1kS+X8bYaI0gUkGKHcqs1946APNgJtYaypJRec557OmcY3OtOBfGNKWooVe7HIN5Jo84EhjBbUBmUqZyTg6vjBmSyLjP4hH88Uf6zi0DFkbMy/b30KEcU86AHnh1SpSmZxP1IS2ZbnTz1m7v3hvyzYw8XxYZ7t8ctinTMcA0mU= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Hb9nWJx9; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="Hb9nWJx9" Received: by smtp.kernel.org (Postfix) with ESMTPSA id AF4D9C4CEE4; Mon, 5 May 2025 22:42:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1746484927; bh=B2Sst9Y20fMXhTIfWJF5SBOWaQ6eWmMfQDIXffAOvlA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Hb9nWJx9jAkW7fLAF2692dwSeRTezWjTDm4XI3G1MwvkxqTfiqLZJzaeTpGO7PViQ Kja52wHCuDjLc7ZF4hCyKX3Bt03BTEzmveAPGdJOh15dqY6UesNtLJV6Hj84oAXzjc pinorqfuxIiGPKhzdoQ9HZlNO9om9gbZ9XMULaOb3kwBAq1/Ok3oI2dwXGWb0lFrPo S954TqUduuQxE1RIsX2GtSXdoNTHRqFDUjnbcGxwWI/1z7LceuuWwjOBn50F7c5WgN ymbBBIz3ifpNBhznbL6sLEmgp7b8Ga3aMY1YwrOJz+D6YJPZWbr9Ob+foy7LGHthf7 zqx3koGf9LNOg== From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Chen Linxuan , Wen Tao , =?UTF-8?q?Michal=20Koutn=C3=BD?= , Tejun Heo , Yu Kuai , Jens Axboe , Sasha Levin , josef@toxicpanda.com, cgroups@vger.kernel.org, linux-block@vger.kernel.org Subject: [PATCH AUTOSEL 6.12 081/486] blk-cgroup: improve policy registration error handling Date: Mon, 5 May 2025 18:32:37 -0400 Message-Id: <20250505223922.2682012-81-sashal@kernel.org> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20250505223922.2682012-1-sashal@kernel.org> References: <20250505223922.2682012-1-sashal@kernel.org> Precedence: bulk X-Mailing-List: stable@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-stable: review X-Patchwork-Hint: Ignore X-stable-base: Linux 6.12.26 Content-Transfer-Encoding: 8bit From: Chen Linxuan [ Upstream commit e1a0202c6bfda24002a3ae2115154fa90104c649 ] 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 Signed-off-by: Wen Tao Signed-off-by: Chen Linxuan Reviewed-by: Michal Koutný Acked-by: Tejun Heo Reviewed-by: Yu Kuai Link: https://lore.kernel.org/r/3E333A73B6B6DFC0+20250317022924.150907-1-chenlinxuan@uniontech.com Signed-off-by: Jens Axboe Signed-off-by: Sasha Levin --- block/blk-cgroup.c | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/block/blk-cgroup.c b/block/blk-cgroup.c index f1cf7f2909f3a..643d6bf66522e 100644 --- a/block/blk-cgroup.c +++ b/block/blk-cgroup.c @@ -1725,27 +1725,27 @@ 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); /* find an empty slot */ - ret = -ENOSPC; for (i = 0; i < BLKCG_MAX_POLS; i++) if (!blkcg_policy[i]) break; if (i >= BLKCG_MAX_POLS) { pr_warn("blkcg_policy_register: BLKCG_MAX_POLS too small\n"); + ret = -ENOSPC; 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; @@ -1756,8 +1756,10 @@ int blkcg_policy_register(struct blkcg_policy *pol) struct blkcg_policy_data *cpd; cpd = pol->cpd_alloc_fn(GFP_KERNEL); - if (!cpd) + if (!cpd) { + ret = -ENOMEM; goto err_free_cpds; + } blkcg->cpd[pol->plid] = cpd; cpd->blkcg = blkcg; -- 2.39.5