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 7A74E2DFA26; Sun, 1 Jun 2025 23:36:03 +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=1748820963; cv=none; b=Si/2r1xYUBobDidDH6M3QrRlCIJFXMu8hQyu6bJgvMNEwy7Ufo0YDjOR3m5DD0qA4eRbXj0m2/xyAYeAVN46xEw2GYvYq7HHsTXIpfae8uFziI2+kIKAsmvaUYINaGckJb1zswV71+MeAcF+kgcOknXOrBf9cApbYovECoe6zsI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1748820963; c=relaxed/simple; bh=IZ+z1lIb/H3UILfOjOUl48oOH9Bj8Y4IUC32rGWTLyA=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version:Content-Type; b=QYmcVSKkYJBi2d6i/Bgtd9DosvyvKMSlNjIrE3ZeSy6jfd5djnYmi5N6L/fxSoLbssd1/D/GZ/jV9eSUWLz0Y5+pa88oZ7Cu3+YAJ/HEjro3JSuhLugwZEf+RubyAeB1rLe6FisPLutpmgCtnnjiduuDmnFgtYCInG8ACdQrTJo= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=JX6Ybclb; 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="JX6Ybclb" Received: by smtp.kernel.org (Postfix) with ESMTPSA id AB3C1C4CEE7; Sun, 1 Jun 2025 23:36:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1748820963; bh=IZ+z1lIb/H3UILfOjOUl48oOH9Bj8Y4IUC32rGWTLyA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=JX6YbclbUyIbNHwRq4630gI6+V6euHIcP5p3DGK9HoKISuB9B06bEMqKh4ogaWT/j JPkVqqtAHG/4K1KoBHCJY7+VmqbOdRz+sABcVyfTvgMbK5A03FgvhW844xiDDfIHdb /1/PnxKOt7CVW9p2w+tKeTg5dgIJEidcXHN8Qo9fl+lwcn3tNxemMYLFP9+4oxkl2I s2D57P9j3ieDR/bWr2bRsr9rzW474NExfX4W7NJQLAQvVh32DY0gBuT+dnAjYqO59e v7MZkphXh2sZznYXbUWFpiccx3af5OIua62xaEVmofxd6FGkT2xAI1mBXBw83bxYSl n0G1osplzmL0A== From: Sasha Levin To: patches@lists.linux.dev, stable@vger.kernel.org Cc: Nicolas Dufresne , Hans Verkuil , Sasha Levin , ezequiel@vanguardiasur.com.ar, mchehab@kernel.org, gregkh@linuxfoundation.org, linux-media@vger.kernel.org, linux-rockchip@lists.infradead.org, linux-staging@lists.linux.dev, linux-kernel@vger.kernel.org Subject: [PATCH AUTOSEL 6.12 47/93] media: rkvdec: Initialize the m2m context before the controls Date: Sun, 1 Jun 2025 19:33:14 -0400 Message-Id: <20250601233402.3512823-47-sashal@kernel.org> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20250601233402.3512823-1-sashal@kernel.org> References: <20250601233402.3512823-1-sashal@kernel.org> Precedence: bulk X-Mailing-List: linux-staging@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore X-stable-base: Linux 6.12.31 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit From: Nicolas Dufresne [ Upstream commit d43d7db3c8a1868dcbc6cb8de90a3cdf309d6cbb ] Setting up the control handler calls into .s_ctrl ops. While validating the controls the ops may need to access some of the context state, which could lead to a crash if not properly initialized. Signed-off-by: Nicolas Dufresne Signed-off-by: Hans Verkuil Signed-off-by: Sasha Levin --- Now let me analyze the specific changes proposed in the commit: ## Analysis **YES** This commit should be backported to stable kernel trees. Here's my extensive analysis: ### Core Issue Analysis The commit addresses a critical initialization order bug that can lead to crashes. The problem occurs in the `rkvdec_open()` function at lines 822-830 in the current code: 1. **Current problematic order:** - Line 822: `rkvdec_init_ctrls(ctx)` is called first - Line 826-830: `v4l2_m2m_ctx_init()` is called second 2. **The problem:** According to the commit message and my analysis of the kernel documentation, `v4l2_ctrl_handler_setup()` (called inside `rkvdec_init_ctrls()`) calls `.s_ctrl` for all controls unconditionally to initialize hardware to default values. The `.s_ctrl` handlers may need to access the m2m context state, but if the m2m context (`ctx->fh.m2m_ctx`) isn't initialized yet, this can cause a crash or undefined behavior. ### Code Changes Analysis The fix is minimal and surgical: - **Lines moved:** The initialization order is swapped - m2m context initialization moves before control handler setup - **Error handling updated:** The error handling paths are correctly updated to match the new initialization order - **No functional changes:** The fix doesn't change driver functionality, only initialization sequence ### Why This Should Be Backported 1. **Fixes a real crash bug:** This addresses a potential crash scenario that affects users 2. **Small and contained:** The change is minimal - just reordering initialization and updating error paths 3. **Low regression risk:** Moving m2m init before control init is safer since m2m context doesn't depend on controls, but controls may depend on m2m context 4. **Follows established patterns:** Similar commit #2 in the historical examples shows a "YES" backport for proper initialization order in V4L2 drivers 5. **Critical subsystem:** Media drivers crashing can affect user applications ### Comparison with Historical Commits - **Similar to commit #2 (YES):** Also fixes initialization order in V4L2 media driver to prevent crashes - **Similar to commit #4 (YES):** Media driver fix that's low-risk and contained - **Unlike commits #1, #3, #5 (NO):** This actually fixes a bug rather than just cleanup/code style improvements ### Stable Tree Criteria Met ✅ **Important bugfix:** Prevents crashes ✅ **Minimal risk:** Simple reordering with no side effects ✅ **Small change:** Only affects initialization sequence ✅ **No architectural changes:** Pure bug fix ✅ **Confined to subsystem:** Only affects rkvdec driver The fix follows the stable tree rules perfectly - it's an important crash prevention fix with minimal regression risk, making it an ideal backport candidate. drivers/staging/media/rkvdec/rkvdec.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/drivers/staging/media/rkvdec/rkvdec.c b/drivers/staging/media/rkvdec/rkvdec.c index ac398b5a97360..838ff0ee0bbb4 100644 --- a/drivers/staging/media/rkvdec/rkvdec.c +++ b/drivers/staging/media/rkvdec/rkvdec.c @@ -821,24 +821,24 @@ static int rkvdec_open(struct file *filp) rkvdec_reset_decoded_fmt(ctx); v4l2_fh_init(&ctx->fh, video_devdata(filp)); - ret = rkvdec_init_ctrls(ctx); - if (ret) - goto err_free_ctx; - ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(rkvdec->m2m_dev, ctx, rkvdec_queue_init); if (IS_ERR(ctx->fh.m2m_ctx)) { ret = PTR_ERR(ctx->fh.m2m_ctx); - goto err_cleanup_ctrls; + goto err_free_ctx; } + ret = rkvdec_init_ctrls(ctx); + if (ret) + goto err_cleanup_m2m_ctx; + filp->private_data = &ctx->fh; v4l2_fh_add(&ctx->fh); return 0; -err_cleanup_ctrls: - v4l2_ctrl_handler_free(&ctx->ctrl_hdl); +err_cleanup_m2m_ctx: + v4l2_m2m_ctx_release(ctx->fh.m2m_ctx); err_free_ctx: kfree(ctx); -- 2.39.5