All of lore.kernel.org
 help / color / mirror / Atom feed
From: Hans Verkuil <hverkuil+cisco@kernel.org>
To: "Niklas Söderlund" <niklas.soderlund+renesas@ragnatech.se>,
	"Lars-Peter Clausen" <lars@metafoo.de>,
	"Mauro Carvalho Chehab" <mchehab@kernel.org>,
	"Hans Verkuil" <hverkuil@xs4all.nl>,
	"Laurent Pinchart" <laurent.pinchart@ideasonboard.com>,
	linux-media@vger.kernel.org
Cc: linux-renesas-soc@vger.kernel.org,
	Laurent Pinchart <laurent.pinchart+renesas@ideasonboard.com>
Subject: Re: [PATCH v2 04/11] media: adv7180: Use v4l2-ctrls core to handle s_ctrl locking
Date: Wed, 3 Sep 2025 10:28:18 +0200	[thread overview]
Message-ID: <797bd6af-2246-4fdf-ab46-e39ad6b69cbe@kernel.org> (raw)
In-Reply-To: <20250828160654.1467762-5-niklas.soderlund+renesas@ragnatech.se>

On 28/08/2025 18:06, Niklas Söderlund wrote:
> Instead of handling the state lock ourself in .s_ctrl use the v4l2-ctrls
> core to handle it for us. This will allow us later to use the unlocked
> __v4l2_ctrl_handler_setup() in initialization code where the state lock
> is already held.
> 
> Add a lockdep assert to demonstrate the mutex must be held when setting
> controls.
> 
> There is no functional change.
> 
> Signed-off-by: Niklas Söderlund <niklas.soderlund+renesas@ragnatech.se>
> Reviewed-by: Laurent Pinchart <laurent.pinchart+renesas@ideasonboard.com>
> ---
>  drivers/media/i2c/adv7180.c | 8 ++++----
>  1 file changed, 4 insertions(+), 4 deletions(-)
> 
> diff --git a/drivers/media/i2c/adv7180.c b/drivers/media/i2c/adv7180.c
> index 9dbd33c4a30c..7b0387151c3a 100644
> --- a/drivers/media/i2c/adv7180.c
> +++ b/drivers/media/i2c/adv7180.c
> @@ -601,11 +601,11 @@ static int adv7180_s_ctrl(struct v4l2_ctrl *ctrl)
>  {
>  	struct v4l2_subdev *sd = to_adv7180_sd(ctrl);
>  	struct adv7180_state *state = to_state(sd);
> -	int ret = mutex_lock_interruptible(&state->mutex);
> +	int ret = 0;
>  	int val;
>  
> -	if (ret)
> -		return ret;
> +	lockdep_assert_held(&state->mutex);
> +
>  	val = ctrl->val;
>  	switch (ctrl->id) {
>  	case V4L2_CID_BRIGHTNESS:
> @@ -647,7 +647,6 @@ static int adv7180_s_ctrl(struct v4l2_ctrl *ctrl)
>  		ret = -EINVAL;
>  	}
>  
> -	mutex_unlock(&state->mutex);
>  	return ret;
>  }
>  
> @@ -668,6 +667,7 @@ static const struct v4l2_ctrl_config adv7180_ctrl_fast_switch = {
>  static int adv7180_init_controls(struct adv7180_state *state)
>  {
>  	v4l2_ctrl_handler_init(&state->ctrl_hdl, 4);
> +	state->ctrl_hdl.lock = &state->mutex;

While perfectly legal, I would really like to avoid drivers messing with internal
fields of the v4l2_ctrl_handler structure. I wondered why I hadn't noticed this
construct before, and it is primarily used in sensor drivers, which I typically
don't review.

What I would prefer to see is a new function: v4l2_ctrl_handler_init_with_mutex()
where the mutex pointer is passed as an extra argument.

And a static inline for the old function like this:

static inline int v4l2_ctrl_handler_init(struct v4l2_ctrl_handler *hdl,
					 unsigned nr_of_controls_hint)
{
	mutex_init(&hdl->_lock);
	return v4l2_ctrl_handler_init_with_mutex(hdl, nr_of_controls_hint, &hdl->_lock);
}

(it's actually a bit more work due to LOCKDEP class handling)

If a driver uses v4l2_ctrl_handler_init_with_mutex then hdl->_lock is never inited
(and will typically be all zeroes), so any use of that lock will cause errors.

v4l2_ctrl_handler_init_with_mutex() could actually check if the mutex is != _lock
and zero _lock explicitly, clearly marking it as unused.

If you prefer to do this as a follow-up series (also updating existing drivers
that use this), then that would be fine.

Regards,

	Hans

>  
>  	v4l2_ctrl_new_std(&state->ctrl_hdl, &adv7180_ctrl_ops,
>  			  V4L2_CID_BRIGHTNESS, ADV7180_BRI_MIN,


  reply	other threads:[~2025-09-03  8:28 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-08-28 16:06 [PATCH v2 00/11] media: adv7180: Improve the control over decoder power Niklas Söderlund
2025-08-28 16:06 ` [PATCH v2 01/11] media: adv7180: Move adv7180_set_power() and init_device() Niklas Söderlund
2025-08-28 16:06 ` [PATCH v2 02/11] media: adv7180: Add missing lock in suspend callback Niklas Söderlund
2025-08-28 16:06 ` [PATCH v2 03/11] media: adv7180: Move state mutex handling outside init_device() Niklas Söderlund
2025-08-28 16:06 ` [PATCH v2 04/11] media: adv7180: Use v4l2-ctrls core to handle s_ctrl locking Niklas Söderlund
2025-09-03  8:28   ` Hans Verkuil [this message]
2025-08-28 16:06 ` [PATCH v2 05/11] media: adv7180: Setup controls every time the device is reset Niklas Söderlund
2025-08-28 16:06 ` [PATCH v2 06/11] media: adv7180: Power down decoder when configuring the device Niklas Söderlund
2025-09-03  8:30   ` Hans Verkuil
2025-08-28 16:06 ` [PATCH v2 07/11] media: adv7180: Split device initialization and reset Niklas Söderlund
2025-08-28 16:06 ` [PATCH v2 08/11] media: adv7180: Remove the s_power callback Niklas Söderlund
2025-08-28 16:06 ` [PATCH v2 09/11] media: adv7180: Do not write format to device in set_fmt Niklas Söderlund
2025-08-28 16:06 ` [PATCH v2 10/11] media: adv7180: Only validate format in s_std Niklas Söderlund
2025-08-28 16:06 ` [PATCH v2 11/11] media: adv7180: Only validate format in querystd Niklas Söderlund
2025-09-03  8:46 ` [PATCH v2 00/11] media: adv7180: Improve the control over decoder power Hans Verkuil
2025-09-03 14:35   ` Niklas Söderlund
2025-09-03 21:29     ` Hans Verkuil

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=797bd6af-2246-4fdf-ab46-e39ad6b69cbe@kernel.org \
    --to=hverkuil+cisco@kernel.org \
    --cc=hverkuil@xs4all.nl \
    --cc=lars@metafoo.de \
    --cc=laurent.pinchart+renesas@ideasonboard.com \
    --cc=laurent.pinchart@ideasonboard.com \
    --cc=linux-media@vger.kernel.org \
    --cc=linux-renesas-soc@vger.kernel.org \
    --cc=mchehab@kernel.org \
    --cc=niklas.soderlund+renesas@ragnatech.se \
    /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.