From: Ming Qian <ming.qian@nxp.com>
To: Fan Wu <fanwu01@zju.edu.cn>,
Mirela Rabulea <mirela.rabulea@nxp.com>,
"mchehab@kernel.org" <mchehab@kernel.org>,
Frank Li <frank.li@nxp.com>
Cc: "shawnguo@kernel.org" <shawnguo@kernel.org>,
"s.hauer@pengutronix.de" <s.hauer@pengutronix.de>,
"kernel@pengutronix.de" <kernel@pengutronix.de>,
"festevam@gmail.com" <festevam@gmail.com>,
"imx@lists.linux.dev" <imx@lists.linux.dev>,
"linux-media@vger.kernel.org" <linux-media@vger.kernel.org>,
"linux-arm-kernel@lists.infradead.org"
<linux-arm-kernel@lists.infradead.org>,
"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>,
"stable@vger.kernel.org" <stable@vger.kernel.org>
Subject: RE: [EXT] [PATCH] media: imx-jpeg: cancel timeout worker when streaming stops
Date: Mon, 27 Jul 2026 05:59:35 +0000 [thread overview]
Message-ID: <AM9PR04MB8570B0F336F244FF5F96253CE7CC2@AM9PR04MB8570.eurprd04.prod.outlook.com> (raw)
In-Reply-To: <20260623103031.3051-1-fanwu01@zju.edu.cn>
>-----Original Message-----
>From: Fan Wu <fanwu01@zju.edu.cn>
>Sent: Tuesday, June 23, 2026 6:31 PM
>To: Mirela Rabulea <mirela.rabulea@nxp.com>; mchehab@kernel.org
>Cc: shawnguo@kernel.org; s.hauer@pengutronix.de; kernel@pengutronix.de;
>festevam@gmail.com; imx@lists.linux.dev; linux-media@vger.kernel.org;
>linux-arm-kernel@lists.infradead.org; linux-kernel@vger.kernel.org;
>stable@vger.kernel.org; Fan Wu <fanwu01@zju.edu.cn>
>Subject: [EXT] [PATCH] media: imx-jpeg: cancel timeout worker when streaming
>stops
>
>[You don't often get email from fanwu01@zju.edu.cn. Learn why this is
>important at https://aka.ms/LearnAboutSenderIdentification ]
>
>Caution: This is an external email. Please take care when clicking links or
>opening attachments. When in doubt, report the message using the 'Report
>this email' button
>
>
>Each per-fd context ctx owns a delayed_work (ctx->task_timer, callback
>mxc_jpeg_device_run_timeout) armed via schedule_delayed_work() at the
>end of mxc_jpeg_device_run() to recover a stalled encode/decode job. The
>only existing cancellation is cancel_delayed_work() in the frame-done IRQ
>handler, which de-queues a pending work item but does not wait for a callback
>that has already started, and it only runs when a frame completes.
>
>When the fd is closed while a job is in flight (the frame-done IRQ has not fired
>yet), nothing syncs the worker before mxc_jpeg_release() frees ctx with kfree()
>after v4l2_m2m_ctx_release(). A queued or executing
>mxc_jpeg_device_run_timeout() can then recover ctx through
>container_of(&ctx->task_timer) and dereference it (ctx->mxc_jpeg, slot_data,
>dev_warn) after ctx has been freed.
>
>Cancel the worker from mxc_jpeg_stop_streaming(). The cancel cannot live in
>mxc_jpeg_release(): mxc_jpeg_device_run() arms the timer while holding only
>hw_lock, not the mxc_jpeg->lock mutex that release holds, so a cancel in
>release could still race a concurrent mxc_jpeg_device_run() that re-arms the
>timer afterwards. mxc_jpeg_stop_streaming() instead runs inside
>v4l2_m2m_ctx_release() -> vb2_queue_release(), i.e. after
>v4l2_m2m_cancel_job() has set TRANS_ABORT and waited for any in-flight job
>to finish (so __v4l2_m2m_try_queue() will not queue and
>v4l2_m2m_try_run() will not run any further job for this context, which
>prevents
>mxc_jpeg_device_run() from re-arming the timer) and before the m2m context
>is freed. cancel_delayed_work_sync() removes a pending work item and waits
>for a running callback, so the worker can no longer race with the subsequent
>kfree(). The cancel is placed before the buffer-release loop so a concurrently
>running timeout callback cannot race with it over the same buffers. If the
>frame-done IRQ canceled a still-pending timer, this cancel is a no-op; if the
>timeout callback has already started, it waits for the callback to finish. The
>same mxc_jpeg_stop_streaming() call is also reached from
>VIDIOC_STREAMOFF, which drains the worker early, although STREAMOFF
>itself does not free ctx -- the use-after-free arises only when the fd is later
>closed.
>
>This bug was found by static analysis.
>
>Fixes: cfed9632ca8e ("media: imx-jpeg: Add a timeout mechanism for each
>frame")
>Cc: stable@vger.kernel.org
>Signed-off-by: Fan Wu <fanwu01@zju.edu.cn>
Reviewed-by: Ming Qian <ming.qian@oss.nxp.com>
>---
> drivers/media/platform/nxp/imx-jpeg/mxc-jpeg.c | 2 ++
> 1 file changed, 2 insertions(+)
>
>diff --git a/drivers/media/platform/nxp/imx-jpeg/mxc-jpeg.c
>b/drivers/media/platform/nxp/imx-jpeg/mxc-jpeg.c
>index 9e4a813489c0..d85a9d196269 100644
>--- a/drivers/media/platform/nxp/imx-jpeg/mxc-jpeg.c
>+++ b/drivers/media/platform/nxp/imx-jpeg/mxc-jpeg.c
>@@ -1735,6 +1735,8 @@ static void mxc_jpeg_stop_streaming(struct
>vb2_queue *q)
>
> dev_dbg(ctx->mxc_jpeg->dev, "Stop streaming ctx=%p", ctx);
>
>+ cancel_delayed_work_sync(&ctx->task_timer);
>+
> /* Release all active buffers */
> for (;;) {
> if (V4L2_TYPE_IS_OUTPUT(q->type))
>--
>2.34.1
>
next prev parent reply other threads:[~2026-07-27 5:59 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-06-23 10:30 Fan Wu
2026-07-27 5:59 ` Ming Qian [this message]
2026-07-27 14:18 ` Frank.Li
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=AM9PR04MB8570B0F336F244FF5F96253CE7CC2@AM9PR04MB8570.eurprd04.prod.outlook.com \
--to=ming.qian@nxp.com \
--cc=fanwu01@zju.edu.cn \
--cc=festevam@gmail.com \
--cc=frank.li@nxp.com \
--cc=imx@lists.linux.dev \
--cc=kernel@pengutronix.de \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-media@vger.kernel.org \
--cc=mchehab@kernel.org \
--cc=mirela.rabulea@nxp.com \
--cc=s.hauer@pengutronix.de \
--cc=shawnguo@kernel.org \
--cc=stable@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®