All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Dave Stevenson" <dave.stevenson@raspberrypi.com>
Cc: robh@kernel.org, conor+dt@kernel.org, devicetree@vger.kernel.org
Subject: Re: [PATCH v2 20/20] media: imx355: Remove storing cur_mode in the state
Date: Tue, 07 Jul 2026 17:37:35 +0000	[thread overview]
Message-ID: <20260707173735.86FE91F000E9@smtp.kernel.org> (raw)
In-Reply-To: <20260707-media-imx355-v2-20-1683ec07b897@raspberrypi.com>

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] The `imx355_set_stream` function and its callee `imx355_start_streaming` execute without acquiring the subdevice active state lock, leading to `lockdep_assert_held` failures and data races with controls.
- [High] Use-after-free of subdev active state in `imx355_set_ctrl()` during device unbind due to incorrect cleanup ordering.
--

--- Patch [20]: [PATCH v2 20/20] media: imx355: Remove storing cur_mode in the state ---
commit d3278174d03d9c643934fc21a9e1c67e3a90a3fb
Author: Dave Stevenson <dave.stevenson@raspberrypi.com>

media: imx355: Remove storing cur_mode in the state

All the information for the mode is now stored within
the subdev state, so configure the sensor based on that.

> diff --git a/drivers/media/i2c/imx355.c b/drivers/media/i2c/imx355.c
> index bb620ebb074f7..f31833e5d1d12 100644
> --- a/drivers/media/i2c/imx355.c
> +++ b/drivers/media/i2c/imx355.c
> @@ -594,6 +591,8 @@ static int imx355_set_ctrl(struct v4l2_ctrl *ctrl)
>  {
>  	struct imx355 *imx355 = container_of(ctrl->handler,
>  					     struct imx355, ctrl_handler);
> +	const struct v4l2_mbus_framefmt *format = NULL;
> +	struct v4l2_subdev_state *state;
>  	s64 max;
>  	int ret;
>  
> @@ -604,7 +603,7 @@ static int imx355_set_ctrl(struct v4l2_ctrl *ctrl)
>  	switch (ctrl->id) {
>  	case V4L2_CID_VBLANK:
>  		/* Update max exposure while meeting expected vblanking */
> -		max = imx355->cur_mode->height + ctrl->val - IMX355_EXPOSURE_OFFSET;
> +		max = format->height + ctrl->val - IMX355_EXPOSURE_OFFSET;

[Severity: High]
Can this dereference a freed active state?

If the driver is unbound (e.g. via sysfs) while a userspace process
concurrently accesses a v4l2 control, imx355_remove() is called.

In imx355_remove(), the active state is freed before the control handler:

imx355_remove()
    v4l2_subdev_cleanup(sd);
    media_entity_cleanup(&sd->entity);
    v4l2_ctrl_handler_free(sd->ctrl_handler);

This leaves a window where the concurrent ioctl can acquire the control lock,
enter imx355_set_ctrl(), retrieve the now-freed active state, and trigger a
use-after-free when reading format->height.

[ ... ]

> @@ -809,9 +804,11 @@ static int imx355_entity_init_state(struct v4l2_subdev *subdev,
>  /* Start streaming */
>  static int imx355_start_streaming(struct imx355 *imx355)
>  {
> -	const struct imx355_reg_list *reg_list;
> +	const struct v4l2_mbus_framefmt *fmt;
> +	struct v4l2_subdev_state *state;
>  	const struct imx355_mode *mode;
>  	int lane_idx = imx355->hwcfg->num_lanes == 4 ? 0 : 1;
> +	struct v4l2_rect *crop;
>  	u64 link_bitrate;
>  	u8 binning_mode;
>  	int ret = 0;
> @@ -820,25 +817,29 @@ static int imx355_start_streaming(struct imx355 *imx355)
>  	cci_multi_reg_write(imx355->regmap, imx355_global_regs,
>  			    ARRAY_SIZE(imx355_global_regs), &ret);
>  
> -	/* Apply default values of current mode */
> -	mode = imx355->cur_mode;
> -	reg_list = &mode->reg_list;
> -	cci_multi_reg_write(imx355->regmap, reg_list->regs,
> -			    reg_list->num_of_regs, &ret);
> +	/* Apply values of current mode */
> +	state = v4l2_subdev_get_locked_active_state(&imx355->sd);

[Severity: High]
Does this code execute without acquiring the subdevice active state lock?

Calling v4l2_subdev_get_locked_active_state() will trigger a lockdep failure
if the lock is not held.

This can happen when starting the video stream via userspace invokes the
.s_stream callback:

imx355_set_stream()
    imx355_start_streaming()
        state = v4l2_subdev_get_locked_active_state(&imx355->sd);

Since imx355_set_stream() doesn't acquire the lock before calling
imx355_start_streaming(), this triggers a lockdep failure.

Additionally, the missing lock leaves the subsequent calls to
__v4l2_ctrl_handler_setup() and __v4l2_ctrl_grab() unlocked, exposing a data
race with concurrent userspace control updates.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260707-media-imx355-v2-0-1683ec07b897@raspberrypi.com?part=20

      reply	other threads:[~2026-07-07 17:37 UTC|newest]

Thread overview: 34+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-07 17:04 [PATCH v2 00/20] media/imx355: General code cleanups, and adding support for 2 lane operation Dave Stevenson
2026-07-07 17:04 ` [PATCH v2 01/20] media: i2c: imx355: Add support for 24 MHz external clock Dave Stevenson
2026-07-07 17:04 ` [PATCH v2 02/20] dt-bindings: media: imx355: Allow 2 CSI2 data lane output Dave Stevenson
2026-07-08  7:07   ` Krzysztof Kozlowski
2026-07-07 17:04 ` [PATCH v2 03/20] media: imx355: Remove duplicated registers from the mode tables Dave Stevenson
2026-07-07 17:14   ` sashiko-bot
2026-07-07 17:04 ` [PATCH v2 04/20] media: imx355: Remove setting FRM_LENGTH_LINES in the mode regs Dave Stevenson
2026-07-07 17:04 ` [PATCH v2 05/20] media: imx355: Programmatically set the crop parameters for each mode Dave Stevenson
2026-07-07 17:18   ` sashiko-bot
2026-07-07 17:04 ` [PATCH v2 06/20] media: imx355: Set register LINE_LENGTH_PCK programmatically Dave Stevenson
2026-07-07 17:12   ` sashiko-bot
2026-07-07 17:04 ` [PATCH v2 07/20] media: imx355: Set binning mode registers programmatically Dave Stevenson
2026-07-07 17:15   ` sashiko-bot
2026-07-07 17:04 ` [PATCH v2 08/20] media: imx355: Remove link_freq_index from each mode as ununsed Dave Stevenson
2026-07-07 17:04 ` [PATCH v2 09/20] media: imx355: pixel_rate never changes, so don't recompute Dave Stevenson
2026-07-07 17:15   ` sashiko-bot
2026-07-07 17:04 ` [PATCH v2 10/20] media: imx355: Remove redundant fll_min, and implement fixed offset Dave Stevenson
2026-07-07 17:18   ` sashiko-bot
2026-07-07 17:04 ` [PATCH v2 11/20] media: imx355: Add support for get_selection Dave Stevenson
2026-07-07 17:18   ` sashiko-bot
2026-07-07 17:04 ` [PATCH v2 12/20] media: imx355: Use pm_runtime autosuspend_delay Dave Stevenson
2026-07-07 17:21   ` sashiko-bot
2026-07-07 17:04 ` [PATCH v2 13/20] media: imx355: Convert to new CCI register access helpers Dave Stevenson
2026-07-07 17:21   ` sashiko-bot
2026-07-07 17:04 ` [PATCH v2 14/20] media: imx355: Set the colorspace in the format Dave Stevenson
2026-07-07 17:16   ` sashiko-bot
2026-07-07 17:04 ` [PATCH v2 15/20] media: imx355: Define the exposure offset, and use that define Dave Stevenson
2026-07-07 17:04 ` [PATCH v2 16/20] media: imx355: Use NULL ctrl_ops for HBLANK as it is a read-only control Dave Stevenson
2026-07-07 17:04 ` [PATCH v2 17/20] media: imx355: Compute link frequency from PLL setup Dave Stevenson
2026-07-07 17:04 ` [PATCH v2 18/20] media: imx355: Support 2 lane readout Dave Stevenson
2026-07-07 17:04 ` [PATCH v2 19/20] media: imx355: Switch to using the subdev state Dave Stevenson
2026-07-07 19:34   ` sashiko-bot
2026-07-07 17:04 ` [PATCH v2 20/20] media: imx355: Remove storing cur_mode in the state Dave Stevenson
2026-07-07 17:37   ` sashiko-bot [this message]

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=20260707173735.86FE91F000E9@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=conor+dt@kernel.org \
    --cc=dave.stevenson@raspberrypi.com \
    --cc=devicetree@vger.kernel.org \
    --cc=robh@kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.