[RFC,v3,2/3] media: added managed v4l2 control initialization

Message ID 1368692074-483-3-git-send-email-a.hajda@samsung.com (mailing list archive)
State Superseded, archived
Delegated to: Laurent Pinchart
Headers

Commit Message

Andrzej Hajda May 16, 2013, 8:14 a.m. UTC
  This patch adds managed version of initialization
function for v4l2 control handler.

Signed-off-by: Andrzej Hajda <a.hajda@samsung.com>
Reviewed-by: Sylwester Nawrocki <s.nawrocki@samsung.com>
Signed-off-by: Kyungmin Park <kyungmin.park@samsung.com>
---
v3:
	- removed managed cleanup
v2:
	- added missing struct device forward declaration,
	- corrected few comments
---
 drivers/media/v4l2-core/v4l2-ctrls.c |   32 ++++++++++++++++++++++++++++++++
 include/media/v4l2-ctrls.h           |   16 ++++++++++++++++
 2 files changed, 48 insertions(+)
  

Comments

Sakari Ailus May 16, 2013, 10:34 p.m. UTC | #1
Hi Andrzej,

Thanks for the patchset!

On Thu, May 16, 2013 at 10:14:33AM +0200, Andrzej Hajda wrote:
> This patch adds managed version of initialization
> function for v4l2 control handler.
> 
> Signed-off-by: Andrzej Hajda <a.hajda@samsung.com>
> Reviewed-by: Sylwester Nawrocki <s.nawrocki@samsung.com>
> Signed-off-by: Kyungmin Park <kyungmin.park@samsung.com>
> ---
> v3:
> 	- removed managed cleanup
> v2:
> 	- added missing struct device forward declaration,
> 	- corrected few comments
> ---
>  drivers/media/v4l2-core/v4l2-ctrls.c |   32 ++++++++++++++++++++++++++++++++
>  include/media/v4l2-ctrls.h           |   16 ++++++++++++++++
>  2 files changed, 48 insertions(+)
> 
> diff --git a/drivers/media/v4l2-core/v4l2-ctrls.c b/drivers/media/v4l2-core/v4l2-ctrls.c
> index ebb8e48..f47ccfa 100644
> --- a/drivers/media/v4l2-core/v4l2-ctrls.c
> +++ b/drivers/media/v4l2-core/v4l2-ctrls.c
> @@ -1421,6 +1421,38 @@ void v4l2_ctrl_handler_free(struct v4l2_ctrl_handler *hdl)
>  }
>  EXPORT_SYMBOL(v4l2_ctrl_handler_free);
>  
> +static void devm_v4l2_ctrl_handler_release(struct device *dev, void *res)
> +{
> +	struct v4l2_ctrl_handler **hdl = res;
> +
> +	v4l2_ctrl_handler_free(*hdl);

v4l2_ctrl_handler_free() acquires hdl->mutex which is independent of the
existence of hdl. By default hdl->lock is in the handler, but it may also be
elsewhere, e.g. in a driver-specific device struct such as struct
smiapp_sensor defined in drivers/media/i2c/smiapp/smiapp.h. I wonder if
anything guarantees that hdl->mutex still exists at the time the device is
removed.

I have to say I don't think it's neither meaningful to acquire that mutex in
v4l2_ctrl_handler_free(), though, since the whole going to be freed next
anyway: reference counting would be needed to prevent bad things from
happening, in case the drivers wouldn't take care of that.

> +}
> +
  
Andrzej Hajda May 20, 2013, 2:24 p.m. UTC | #2
Hi Sakari,

Thanks for the review.


On 17.05.2013 00:34, Sakari Ailus wrote:
> Hi Andrzej,
>
> Thanks for the patchset!
>
> On Thu, May 16, 2013 at 10:14:33AM +0200, Andrzej Hajda wrote:
>> This patch adds managed version of initialization
>> function for v4l2 control handler.
>>
>> Signed-off-by: Andrzej Hajda <a.hajda@samsung.com>
>> Reviewed-by: Sylwester Nawrocki <s.nawrocki@samsung.com>
>> Signed-off-by: Kyungmin Park <kyungmin.park@samsung.com>
>> ---
>> v3:
>> 	- removed managed cleanup
>> v2:
>> 	- added missing struct device forward declaration,
>> 	- corrected few comments
>> ---
>>  drivers/media/v4l2-core/v4l2-ctrls.c |   32 ++++++++++++++++++++++++++++++++
>>  include/media/v4l2-ctrls.h           |   16 ++++++++++++++++
>>  2 files changed, 48 insertions(+)
>>
>> diff --git a/drivers/media/v4l2-core/v4l2-ctrls.c b/drivers/media/v4l2-core/v4l2-ctrls.c
>> index ebb8e48..f47ccfa 100644
>> --- a/drivers/media/v4l2-core/v4l2-ctrls.c
>> +++ b/drivers/media/v4l2-core/v4l2-ctrls.c
>> @@ -1421,6 +1421,38 @@ void v4l2_ctrl_handler_free(struct v4l2_ctrl_handler *hdl)
>>  }
>>  EXPORT_SYMBOL(v4l2_ctrl_handler_free);
>>  
>> +static void devm_v4l2_ctrl_handler_release(struct device *dev, void *res)
>> +{
>> +	struct v4l2_ctrl_handler **hdl = res;
>> +
>> +	v4l2_ctrl_handler_free(*hdl);
> v4l2_ctrl_handler_free() acquires hdl->mutex which is independent of the
> existence of hdl. By default hdl->lock is in the handler, but it may also be
> elsewhere, e.g. in a driver-specific device struct such as struct
> smiapp_sensor defined in drivers/media/i2c/smiapp/smiapp.h. I wonder if
> anything guarantees that hdl->mutex still exists at the time the device is
> removed.
IMO in general if somebody provides custom mutex he becomes responsible
for validity of that mutex during v4l2_ctrl_handler usage.
In the particular case of smiapp if we replace v4l2_ctrl_handler_init with
devm version and remove v4l2_ctrl_handler_free calls it seems to be OK:
- mutex is in devm allocated memory chunk acquired at the beginning of
probe,
- mutex is initialized before devm_v4l2_ctrl_handler_init,
- there is no mutex_destroy call - ie the mutex is valid until the
memory is freed,
- memory free is called after v4l2_ctrl_handler_free - devm
'destructors' are
called in order reverse to devm_* 'constructor' calls.

Anyway in cases when devm_* usage would cause errors
we can still use non devm_* versions.
>
> I have to say I don't think it's neither meaningful to acquire that mutex in
> v4l2_ctrl_handler_free(), though, since the whole going to be freed next
> anyway: reference counting would be needed to prevent bad things from
> happening, in case the drivers wouldn't take care of that.
I do not understand what do you mean exactly. Could you please explain
it more?
What do you want to reference count?


Regards
Andrzej
>
>> +}
>> +

--
To unsubscribe from this list: send the line "unsubscribe linux-media" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
  
Hans Verkuil May 23, 2013, 10:39 a.m. UTC | #3
On Fri 17 May 2013 00:34:51 Sakari Ailus wrote:
> Hi Andrzej,
> 
> Thanks for the patchset!
> 
> On Thu, May 16, 2013 at 10:14:33AM +0200, Andrzej Hajda wrote:
> > This patch adds managed version of initialization
> > function for v4l2 control handler.
> > 
> > Signed-off-by: Andrzej Hajda <a.hajda@samsung.com>
> > Reviewed-by: Sylwester Nawrocki <s.nawrocki@samsung.com>
> > Signed-off-by: Kyungmin Park <kyungmin.park@samsung.com>
> > ---
> > v3:
> > 	- removed managed cleanup
> > v2:
> > 	- added missing struct device forward declaration,
> > 	- corrected few comments
> > ---
> >  drivers/media/v4l2-core/v4l2-ctrls.c |   32 ++++++++++++++++++++++++++++++++
> >  include/media/v4l2-ctrls.h           |   16 ++++++++++++++++
> >  2 files changed, 48 insertions(+)
> > 
> > diff --git a/drivers/media/v4l2-core/v4l2-ctrls.c b/drivers/media/v4l2-core/v4l2-ctrls.c
> > index ebb8e48..f47ccfa 100644
> > --- a/drivers/media/v4l2-core/v4l2-ctrls.c
> > +++ b/drivers/media/v4l2-core/v4l2-ctrls.c
> > @@ -1421,6 +1421,38 @@ void v4l2_ctrl_handler_free(struct v4l2_ctrl_handler *hdl)
> >  }
> >  EXPORT_SYMBOL(v4l2_ctrl_handler_free);
> >  
> > +static void devm_v4l2_ctrl_handler_release(struct device *dev, void *res)
> > +{
> > +	struct v4l2_ctrl_handler **hdl = res;
> > +
> > +	v4l2_ctrl_handler_free(*hdl);
> 
> v4l2_ctrl_handler_free() acquires hdl->mutex which is independent of the
> existence of hdl. By default hdl->lock is in the handler, but it may also be
> elsewhere, e.g. in a driver-specific device struct such as struct
> smiapp_sensor defined in drivers/media/i2c/smiapp/smiapp.h. I wonder if
> anything guarantees that hdl->mutex still exists at the time the device is
> removed.

If it is a driver-managed lock, then the driver should also be responsible for
that lock during the life-time of the control handler. I think that is a fair
assumption.

> I have to say I don't think it's neither meaningful to acquire that mutex in
> v4l2_ctrl_handler_free(), though, since the whole going to be freed next
> anyway: reference counting would be needed to prevent bad things from
> happening, in case the drivers wouldn't take care of that.

It's probably not meaningful today, but it might become meaningful in the
future. And in any case, not taking the lock when manipulating internal
lists is very unexpected even though it might work with today's use cases.

Regards,

	Hans
--
To unsubscribe from this list: send the line "unsubscribe linux-media" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
  
Hans Verkuil May 23, 2013, 10:40 a.m. UTC | #4
On Thu 16 May 2013 10:14:33 Andrzej Hajda wrote:
> This patch adds managed version of initialization
> function for v4l2 control handler.
> 
> Signed-off-by: Andrzej Hajda <a.hajda@samsung.com>
> Reviewed-by: Sylwester Nawrocki <s.nawrocki@samsung.com>
> Signed-off-by: Kyungmin Park <kyungmin.park@samsung.com>

Acked-by: Hans Verkuil <hans.verkuil@cisco.com>

Regards,

	Hans

> ---
> v3:
> 	- removed managed cleanup
> v2:
> 	- added missing struct device forward declaration,
> 	- corrected few comments
> ---
>  drivers/media/v4l2-core/v4l2-ctrls.c |   32 ++++++++++++++++++++++++++++++++
>  include/media/v4l2-ctrls.h           |   16 ++++++++++++++++
>  2 files changed, 48 insertions(+)
> 
> diff --git a/drivers/media/v4l2-core/v4l2-ctrls.c b/drivers/media/v4l2-core/v4l2-ctrls.c
> index ebb8e48..f47ccfa 100644
> --- a/drivers/media/v4l2-core/v4l2-ctrls.c
> +++ b/drivers/media/v4l2-core/v4l2-ctrls.c
> @@ -1421,6 +1421,38 @@ void v4l2_ctrl_handler_free(struct v4l2_ctrl_handler *hdl)
>  }
>  EXPORT_SYMBOL(v4l2_ctrl_handler_free);
>  
> +static void devm_v4l2_ctrl_handler_release(struct device *dev, void *res)
> +{
> +	struct v4l2_ctrl_handler **hdl = res;
> +
> +	v4l2_ctrl_handler_free(*hdl);
> +}
> +
> +int devm_v4l2_ctrl_handler_init(struct device *dev,
> +				struct v4l2_ctrl_handler *hdl,
> +				unsigned nr_of_controls_hint)
> +{
> +	struct v4l2_ctrl_handler **dr;
> +	int rc;
> +
> +	dr = devres_alloc(devm_v4l2_ctrl_handler_release, sizeof(*dr),
> +			  GFP_KERNEL);
> +	if (!dr)
> +		return -ENOMEM;
> +
> +	rc = v4l2_ctrl_handler_init(hdl, nr_of_controls_hint);
> +	if (rc) {
> +		devres_free(dr);
> +		return rc;
> +	}
> +
> +	*dr = hdl;
> +	devres_add(dev, dr);
> +
> +	return 0;
> +}
> +EXPORT_SYMBOL_GPL(devm_v4l2_ctrl_handler_init);
> +
>  /* For backwards compatibility: V4L2_CID_PRIVATE_BASE should no longer
>     be used except in G_CTRL, S_CTRL, QUERYCTRL and QUERYMENU when dealing
>     with applications that do not use the NEXT_CTRL flag.
> diff --git a/include/media/v4l2-ctrls.h b/include/media/v4l2-ctrls.h
> index 7343a27..169443f 100644
> --- a/include/media/v4l2-ctrls.h
> +++ b/include/media/v4l2-ctrls.h
> @@ -25,6 +25,7 @@
>  #include <linux/videodev2.h>
>  
>  /* forward references */
> +struct device;
>  struct file;
>  struct v4l2_ctrl_handler;
>  struct v4l2_ctrl_helper;
> @@ -306,6 +307,21 @@ int v4l2_ctrl_handler_init_class(struct v4l2_ctrl_handler *hdl,
>    */
>  void v4l2_ctrl_handler_free(struct v4l2_ctrl_handler *hdl);
>  
> +/*
> + * devm_v4l2_ctrl_handler_init - managed control handler initialization
> + *
> + * @dev: Device the @hdl belongs to.
> + * @hdl:	The control handler.
> + * @nr_of_controls_hint: A hint of how many controls this handler is
> + *		expected to refer to.
> + *
> + * This is a managed version of v4l2_ctrl_handler_init. Handler initialized with
> + * this function will be automatically cleaned up on driver detach.
> + */
> +int devm_v4l2_ctrl_handler_init(struct device *dev,
> +				struct v4l2_ctrl_handler *hdl,
> +				unsigned nr_of_controls_hint);
> +
>  /** v4l2_ctrl_handler_setup() - Call the s_ctrl op for all controls belonging
>    * to the handler to initialize the hardware to the current control values.
>    * @hdl:	The control handler.
> 
--
To unsubscribe from this list: send the line "unsubscribe linux-media" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
  
Sakari Ailus May 31, 2013, 1:08 a.m. UTC | #5
Hi Hans,

Hans Verkuil wrote:
> On Fri 17 May 2013 00:34:51 Sakari Ailus wrote:
>> Hi Andrzej,
>>
>> Thanks for the patchset!
>>
>> On Thu, May 16, 2013 at 10:14:33AM +0200, Andrzej Hajda wrote:
>>> This patch adds managed version of initialization
>>> function for v4l2 control handler.
>>>
>>> Signed-off-by: Andrzej Hajda <a.hajda@samsung.com>
>>> Reviewed-by: Sylwester Nawrocki <s.nawrocki@samsung.com>
>>> Signed-off-by: Kyungmin Park <kyungmin.park@samsung.com>
>>> ---
>>> v3:
>>> 	- removed managed cleanup
>>> v2:
>>> 	- added missing struct device forward declaration,
>>> 	- corrected few comments
>>> ---
>>>   drivers/media/v4l2-core/v4l2-ctrls.c |   32 ++++++++++++++++++++++++++++++++
>>>   include/media/v4l2-ctrls.h           |   16 ++++++++++++++++
>>>   2 files changed, 48 insertions(+)
>>>
>>> diff --git a/drivers/media/v4l2-core/v4l2-ctrls.c b/drivers/media/v4l2-core/v4l2-ctrls.c
>>> index ebb8e48..f47ccfa 100644
>>> --- a/drivers/media/v4l2-core/v4l2-ctrls.c
>>> +++ b/drivers/media/v4l2-core/v4l2-ctrls.c
>>> @@ -1421,6 +1421,38 @@ void v4l2_ctrl_handler_free(struct v4l2_ctrl_handler *hdl)
>>>   }
>>>   EXPORT_SYMBOL(v4l2_ctrl_handler_free);
>>>
>>> +static void devm_v4l2_ctrl_handler_release(struct device *dev, void *res)
>>> +{
>>> +	struct v4l2_ctrl_handler **hdl = res;
>>> +
>>> +	v4l2_ctrl_handler_free(*hdl);
>>
>> v4l2_ctrl_handler_free() acquires hdl->mutex which is independent of the
>> existence of hdl. By default hdl->lock is in the handler, but it may also be
>> elsewhere, e.g. in a driver-specific device struct such as struct
>> smiapp_sensor defined in drivers/media/i2c/smiapp/smiapp.h. I wonder if
>> anything guarantees that hdl->mutex still exists at the time the device is
>> removed.
>
> If it is a driver-managed lock, then the driver should also be responsible for
> that lock during the life-time of the control handler. I think that is a fair
> assumption.

Agreed.

>> I have to say I don't think it's neither meaningful to acquire that mutex in
>> v4l2_ctrl_handler_free(), though, since the whole going to be freed next
>> anyway: reference counting would be needed to prevent bad things from
>> happening, in case the drivers wouldn't take care of that.
>
> It's probably not meaningful today, but it might become meaningful in the
> future. And in any case, not taking the lock when manipulating internal
> lists is very unexpected even though it might work with today's use cases.

I simply don't think it's meaningful to acquire a lock related to an 
object when that object is being destroyed. If something else was 
holding that lock, you should not have begun destroying that object in 
the first place. This could be solved by reference counting the handler 
which I don't think is needed.

I'd just shout out loud about this rather than hiding such potential 
bug, i.e. replace mutex_lock() and mutex_unlock() in the function by 
WARN_ON(mutex_is_lock()).

But that should be a separate patch.
  
Sakari Ailus May 31, 2013, 1:10 a.m. UTC | #6
Hi Andrzej,

Andrzej Hajda wrote:
>> I have to say I don't think it's neither meaningful to acquire that mutex in
>> v4l2_ctrl_handler_free(), though, since the whole going to be freed next
>> anyway: reference counting would be needed to prevent bad things from
>> happening, in case the drivers wouldn't take care of that.
> I do not understand what do you mean exactly. Could you please explain
> it more?
> What do you want to reference count?

I don't want to, but simply acquiring a lock which is a part of an 
object being destroyed makes no sense. If something else has a reference 
to the object you're screwed anyway; acquiring the lock does not help.
  
Hans Verkuil May 31, 2013, 7:59 a.m. UTC | #7
On Fri May 31 2013 03:08:33 Sakari Ailus wrote:
> Hi Hans,
> 
> Hans Verkuil wrote:
> > On Fri 17 May 2013 00:34:51 Sakari Ailus wrote:
> >> Hi Andrzej,
> >>
> >> Thanks for the patchset!
> >>
> >> On Thu, May 16, 2013 at 10:14:33AM +0200, Andrzej Hajda wrote:
> >>> This patch adds managed version of initialization
> >>> function for v4l2 control handler.
> >>>
> >>> Signed-off-by: Andrzej Hajda <a.hajda@samsung.com>
> >>> Reviewed-by: Sylwester Nawrocki <s.nawrocki@samsung.com>
> >>> Signed-off-by: Kyungmin Park <kyungmin.park@samsung.com>
> >>> ---
> >>> v3:
> >>> 	- removed managed cleanup
> >>> v2:
> >>> 	- added missing struct device forward declaration,
> >>> 	- corrected few comments
> >>> ---
> >>>   drivers/media/v4l2-core/v4l2-ctrls.c |   32 ++++++++++++++++++++++++++++++++
> >>>   include/media/v4l2-ctrls.h           |   16 ++++++++++++++++
> >>>   2 files changed, 48 insertions(+)
> >>>
> >>> diff --git a/drivers/media/v4l2-core/v4l2-ctrls.c b/drivers/media/v4l2-core/v4l2-ctrls.c
> >>> index ebb8e48..f47ccfa 100644
> >>> --- a/drivers/media/v4l2-core/v4l2-ctrls.c
> >>> +++ b/drivers/media/v4l2-core/v4l2-ctrls.c
> >>> @@ -1421,6 +1421,38 @@ void v4l2_ctrl_handler_free(struct v4l2_ctrl_handler *hdl)
> >>>   }
> >>>   EXPORT_SYMBOL(v4l2_ctrl_handler_free);
> >>>
> >>> +static void devm_v4l2_ctrl_handler_release(struct device *dev, void *res)
> >>> +{
> >>> +	struct v4l2_ctrl_handler **hdl = res;
> >>> +
> >>> +	v4l2_ctrl_handler_free(*hdl);
> >>
> >> v4l2_ctrl_handler_free() acquires hdl->mutex which is independent of the
> >> existence of hdl. By default hdl->lock is in the handler, but it may also be
> >> elsewhere, e.g. in a driver-specific device struct such as struct
> >> smiapp_sensor defined in drivers/media/i2c/smiapp/smiapp.h. I wonder if
> >> anything guarantees that hdl->mutex still exists at the time the device is
> >> removed.
> >
> > If it is a driver-managed lock, then the driver should also be responsible for
> > that lock during the life-time of the control handler. I think that is a fair
> > assumption.
> 
> Agreed.
> 
> >> I have to say I don't think it's neither meaningful to acquire that mutex in
> >> v4l2_ctrl_handler_free(), though, since the whole going to be freed next
> >> anyway: reference counting would be needed to prevent bad things from
> >> happening, in case the drivers wouldn't take care of that.
> >
> > It's probably not meaningful today, but it might become meaningful in the
> > future. And in any case, not taking the lock when manipulating internal
> > lists is very unexpected even though it might work with today's use cases.
> 
> I simply don't think it's meaningful to acquire a lock related to an 
> object when that object is being destroyed. If something else was 
> holding that lock, you should not have begun destroying that object in 
> the first place. This could be solved by reference counting the handler 
> which I don't think is needed.

Right now the way controls are set up is very static, but in the future I
expect to see more dynamical behavior (I'm thinking of FPGAs supporting
partial reconfiguration). In cases like that it you do want to take the
lock preventing others from making modifications while the handler is
freed. I am well aware that much more work will have to be done if we want
to support such scenarios, but it is one reason why I would like to keep
the lock there.

Regards,

	Hans

> I'd just shout out loud about this rather than hiding such potential 
> bug, i.e. replace mutex_lock() and mutex_unlock() in the function by 
> WARN_ON(mutex_is_lock()).
> 
> But that should be a separate patch.
> 
> 
--
To unsubscribe from this list: send the line "unsubscribe linux-media" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
  
Sakari Ailus June 6, 2013, 9:41 p.m. UTC | #8
Hi Andrzej and Hans,

On Fri, May 31, 2013 at 09:59:11AM +0200, Hans Verkuil wrote:
> On Fri May 31 2013 03:08:33 Sakari Ailus wrote:
> > Hi Hans,
> > 
> > Hans Verkuil wrote:
> > > On Fri 17 May 2013 00:34:51 Sakari Ailus wrote:
> > >> Hi Andrzej,
> > >>
> > >> Thanks for the patchset!
> > >>
> > >> On Thu, May 16, 2013 at 10:14:33AM +0200, Andrzej Hajda wrote:
> > >>> This patch adds managed version of initialization
> > >>> function for v4l2 control handler.
> > >>>
> > >>> Signed-off-by: Andrzej Hajda <a.hajda@samsung.com>
> > >>> Reviewed-by: Sylwester Nawrocki <s.nawrocki@samsung.com>
> > >>> Signed-off-by: Kyungmin Park <kyungmin.park@samsung.com>
> > >>> ---
> > >>> v3:
> > >>> 	- removed managed cleanup
> > >>> v2:
> > >>> 	- added missing struct device forward declaration,
> > >>> 	- corrected few comments
> > >>> ---
> > >>>   drivers/media/v4l2-core/v4l2-ctrls.c |   32 ++++++++++++++++++++++++++++++++
> > >>>   include/media/v4l2-ctrls.h           |   16 ++++++++++++++++
> > >>>   2 files changed, 48 insertions(+)
> > >>>
> > >>> diff --git a/drivers/media/v4l2-core/v4l2-ctrls.c b/drivers/media/v4l2-core/v4l2-ctrls.c
> > >>> index ebb8e48..f47ccfa 100644
> > >>> --- a/drivers/media/v4l2-core/v4l2-ctrls.c
> > >>> +++ b/drivers/media/v4l2-core/v4l2-ctrls.c
> > >>> @@ -1421,6 +1421,38 @@ void v4l2_ctrl_handler_free(struct v4l2_ctrl_handler *hdl)
> > >>>   }
> > >>>   EXPORT_SYMBOL(v4l2_ctrl_handler_free);
> > >>>
> > >>> +static void devm_v4l2_ctrl_handler_release(struct device *dev, void *res)
> > >>> +{
> > >>> +	struct v4l2_ctrl_handler **hdl = res;
> > >>> +
> > >>> +	v4l2_ctrl_handler_free(*hdl);
> > >>
> > >> v4l2_ctrl_handler_free() acquires hdl->mutex which is independent of the
> > >> existence of hdl. By default hdl->lock is in the handler, but it may also be
> > >> elsewhere, e.g. in a driver-specific device struct such as struct
> > >> smiapp_sensor defined in drivers/media/i2c/smiapp/smiapp.h. I wonder if
> > >> anything guarantees that hdl->mutex still exists at the time the device is
> > >> removed.
> > >
> > > If it is a driver-managed lock, then the driver should also be responsible for
> > > that lock during the life-time of the control handler. I think that is a fair
> > > assumption.
> > 
> > Agreed.
> > 
> > >> I have to say I don't think it's neither meaningful to acquire that mutex in
> > >> v4l2_ctrl_handler_free(), though, since the whole going to be freed next
> > >> anyway: reference counting would be needed to prevent bad things from
> > >> happening, in case the drivers wouldn't take care of that.
> > >
> > > It's probably not meaningful today, but it might become meaningful in the
> > > future. And in any case, not taking the lock when manipulating internal
> > > lists is very unexpected even though it might work with today's use cases.
> > 
> > I simply don't think it's meaningful to acquire a lock related to an 
> > object when that object is being destroyed. If something else was 
> > holding that lock, you should not have begun destroying that object in 
> > the first place. This could be solved by reference counting the handler 
> > which I don't think is needed.
> 
> Right now the way controls are set up is very static, but in the future I
> expect to see more dynamical behavior (I'm thinking of FPGAs supporting
> partial reconfiguration). In cases like that it you do want to take the
> lock preventing others from making modifications while the handler is
> freed. I am well aware that much more work will have to be done if we want
> to support such scenarios, but it is one reason why I would like to keep
> the lock there.

I'm ok with that.

How about adding a note to the comment above devm_v4l2_ctrl_handler_init()
that the function cannot be used with drivers that wish to use their own
lock?
  
Sylwester Nawrocki June 9, 2013, 6:05 p.m. UTC | #9
Hi Sakari,

On 06/06/2013 11:41 PM, Sakari Ailus wrote:
...
>>>>>> diff --git a/drivers/media/v4l2-core/v4l2-ctrls.c b/drivers/media/v4l2-core/v4l2-ctrls.c
>>>>>> index ebb8e48..f47ccfa 100644
>>>>>> --- a/drivers/media/v4l2-core/v4l2-ctrls.c
>>>>>> +++ b/drivers/media/v4l2-core/v4l2-ctrls.c
>>>>>> @@ -1421,6 +1421,38 @@ void v4l2_ctrl_handler_free(struct v4l2_ctrl_handler *hdl)
>>>>>>    }
>>>>>>    EXPORT_SYMBOL(v4l2_ctrl_handler_free);
>>>>>>
>>>>>> +static void devm_v4l2_ctrl_handler_release(struct device *dev, void *res)
>>>>>> +{
>>>>>> +	struct v4l2_ctrl_handler **hdl = res;
>>>>>> +
>>>>>> +	v4l2_ctrl_handler_free(*hdl);
>>>>>
>>>>> v4l2_ctrl_handler_free() acquires hdl->mutex which is independent of the
>>>>> existence of hdl. By default hdl->lock is in the handler, but it may also be
>>>>> elsewhere, e.g. in a driver-specific device struct such as struct
>>>>> smiapp_sensor defined in drivers/media/i2c/smiapp/smiapp.h. I wonder if
>>>>> anything guarantees that hdl->mutex still exists at the time the device is
>>>>> removed.
>>>>
>>>> If it is a driver-managed lock, then the driver should also be responsible for
>>>> that lock during the life-time of the control handler. I think that is a fair
>>>> assumption.
>>>
>>> Agreed.
>>>
>>>>> I have to say I don't think it's neither meaningful to acquire that mutex in
>>>>> v4l2_ctrl_handler_free(), though, since the whole going to be freed next
>>>>> anyway: reference counting would be needed to prevent bad things from
>>>>> happening, in case the drivers wouldn't take care of that.
>>>>
>>>> It's probably not meaningful today, but it might become meaningful in the
>>>> future. And in any case, not taking the lock when manipulating internal
>>>> lists is very unexpected even though it might work with today's use cases.
>>>
>>> I simply don't think it's meaningful to acquire a lock related to an
>>> object when that object is being destroyed. If something else was
>>> holding that lock, you should not have begun destroying that object in
>>> the first place. This could be solved by reference counting the handler
>>> which I don't think is needed.
>>
>> Right now the way controls are set up is very static, but in the future I
>> expect to see more dynamical behavior (I'm thinking of FPGAs supporting
>> partial reconfiguration). In cases like that it you do want to take the
>> lock preventing others from making modifications while the handler is
>> freed. I am well aware that much more work will have to be done if we want
>> to support such scenarios, but it is one reason why I would like to keep
>> the lock there.
>
> I'm ok with that.
>
> How about adding a note to the comment above devm_v4l2_ctrl_handler_init()
> that the function cannot be used with drivers that wish to use their own
> lock?

But wouldn't it be a false statement ? The driver can still control the
cleanup sequence by properly ordering the initialization sequence. So as
long as it is ensured the mutex is destroyed after the controls handler
(IOW, the mutex is created before the controls handler using the devm_*
API) everything should be fine, shouldn't it ?


Regards,
Sylwester
--
To unsubscribe from this list: send the line "unsubscribe linux-media" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
  
Hans Verkuil June 10, 2013, 1:36 p.m. UTC | #10
On Sun June 9 2013 20:05:33 Sylwester Nawrocki wrote:
> Hi Sakari,
> 
> On 06/06/2013 11:41 PM, Sakari Ailus wrote:
> ...
> >>>>>> diff --git a/drivers/media/v4l2-core/v4l2-ctrls.c b/drivers/media/v4l2-core/v4l2-ctrls.c
> >>>>>> index ebb8e48..f47ccfa 100644
> >>>>>> --- a/drivers/media/v4l2-core/v4l2-ctrls.c
> >>>>>> +++ b/drivers/media/v4l2-core/v4l2-ctrls.c
> >>>>>> @@ -1421,6 +1421,38 @@ void v4l2_ctrl_handler_free(struct v4l2_ctrl_handler *hdl)
> >>>>>>    }
> >>>>>>    EXPORT_SYMBOL(v4l2_ctrl_handler_free);
> >>>>>>
> >>>>>> +static void devm_v4l2_ctrl_handler_release(struct device *dev, void *res)
> >>>>>> +{
> >>>>>> +	struct v4l2_ctrl_handler **hdl = res;
> >>>>>> +
> >>>>>> +	v4l2_ctrl_handler_free(*hdl);
> >>>>>
> >>>>> v4l2_ctrl_handler_free() acquires hdl->mutex which is independent of the
> >>>>> existence of hdl. By default hdl->lock is in the handler, but it may also be
> >>>>> elsewhere, e.g. in a driver-specific device struct such as struct
> >>>>> smiapp_sensor defined in drivers/media/i2c/smiapp/smiapp.h. I wonder if
> >>>>> anything guarantees that hdl->mutex still exists at the time the device is
> >>>>> removed.
> >>>>
> >>>> If it is a driver-managed lock, then the driver should also be responsible for
> >>>> that lock during the life-time of the control handler. I think that is a fair
> >>>> assumption.
> >>>
> >>> Agreed.
> >>>
> >>>>> I have to say I don't think it's neither meaningful to acquire that mutex in
> >>>>> v4l2_ctrl_handler_free(), though, since the whole going to be freed next
> >>>>> anyway: reference counting would be needed to prevent bad things from
> >>>>> happening, in case the drivers wouldn't take care of that.
> >>>>
> >>>> It's probably not meaningful today, but it might become meaningful in the
> >>>> future. And in any case, not taking the lock when manipulating internal
> >>>> lists is very unexpected even though it might work with today's use cases.
> >>>
> >>> I simply don't think it's meaningful to acquire a lock related to an
> >>> object when that object is being destroyed. If something else was
> >>> holding that lock, you should not have begun destroying that object in
> >>> the first place. This could be solved by reference counting the handler
> >>> which I don't think is needed.
> >>
> >> Right now the way controls are set up is very static, but in the future I
> >> expect to see more dynamical behavior (I'm thinking of FPGAs supporting
> >> partial reconfiguration). In cases like that it you do want to take the
> >> lock preventing others from making modifications while the handler is
> >> freed. I am well aware that much more work will have to be done if we want
> >> to support such scenarios, but it is one reason why I would like to keep
> >> the lock there.
> >
> > I'm ok with that.
> >
> > How about adding a note to the comment above devm_v4l2_ctrl_handler_init()
> > that the function cannot be used with drivers that wish to use their own
> > lock?
> 
> But wouldn't it be a false statement ? The driver can still control the
> cleanup sequence by properly ordering the initialization sequence. So as
> long as it is ensured the mutex is destroyed after the controls handler
> (IOW, the mutex is created before the controls handler using the devm_*
> API) everything should be fine, shouldn't it ?

It should indeed. I really don't see the problem here.

Regards,

	Hans
--
To unsubscribe from this list: send the line "unsubscribe linux-media" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
  
Laurent Pinchart June 18, 2013, 10:04 p.m. UTC | #11
Hi Andrzej,

Thank you for the patch.

On Thursday 16 May 2013 10:14:33 Andrzej Hajda wrote:
> This patch adds managed version of initialization
> function for v4l2 control handler.
> 
> Signed-off-by: Andrzej Hajda <a.hajda@samsung.com>
> Reviewed-by: Sylwester Nawrocki <s.nawrocki@samsung.com>
> Signed-off-by: Kyungmin Park <kyungmin.park@samsung.com>

Acked-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>

> ---
> v3:
> 	- removed managed cleanup
> v2:
> 	- added missing struct device forward declaration,
> 	- corrected few comments
> ---
>  drivers/media/v4l2-core/v4l2-ctrls.c |   32 +++++++++++++++++++++++++++++++
>  include/media/v4l2-ctrls.h           |   16 ++++++++++++++++
>  2 files changed, 48 insertions(+)
> 
> diff --git a/drivers/media/v4l2-core/v4l2-ctrls.c
> b/drivers/media/v4l2-core/v4l2-ctrls.c index ebb8e48..f47ccfa 100644
> --- a/drivers/media/v4l2-core/v4l2-ctrls.c
> +++ b/drivers/media/v4l2-core/v4l2-ctrls.c
> @@ -1421,6 +1421,38 @@ void v4l2_ctrl_handler_free(struct v4l2_ctrl_handler
> *hdl) }
>  EXPORT_SYMBOL(v4l2_ctrl_handler_free);
> 
> +static void devm_v4l2_ctrl_handler_release(struct device *dev, void *res)
> +{
> +	struct v4l2_ctrl_handler **hdl = res;
> +
> +	v4l2_ctrl_handler_free(*hdl);
> +}
> +
> +int devm_v4l2_ctrl_handler_init(struct device *dev,
> +				struct v4l2_ctrl_handler *hdl,
> +				unsigned nr_of_controls_hint)
> +{
> +	struct v4l2_ctrl_handler **dr;
> +	int rc;
> +
> +	dr = devres_alloc(devm_v4l2_ctrl_handler_release, sizeof(*dr),
> +			  GFP_KERNEL);
> +	if (!dr)
> +		return -ENOMEM;
> +
> +	rc = v4l2_ctrl_handler_init(hdl, nr_of_controls_hint);
> +	if (rc) {
> +		devres_free(dr);
> +		return rc;
> +	}
> +
> +	*dr = hdl;
> +	devres_add(dev, dr);
> +
> +	return 0;
> +}
> +EXPORT_SYMBOL_GPL(devm_v4l2_ctrl_handler_init);
> +
>  /* For backwards compatibility: V4L2_CID_PRIVATE_BASE should no longer
>     be used except in G_CTRL, S_CTRL, QUERYCTRL and QUERYMENU when dealing
>     with applications that do not use the NEXT_CTRL flag.
> diff --git a/include/media/v4l2-ctrls.h b/include/media/v4l2-ctrls.h
> index 7343a27..169443f 100644
> --- a/include/media/v4l2-ctrls.h
> +++ b/include/media/v4l2-ctrls.h
> @@ -25,6 +25,7 @@
>  #include <linux/videodev2.h>
> 
>  /* forward references */
> +struct device;
>  struct file;
>  struct v4l2_ctrl_handler;
>  struct v4l2_ctrl_helper;
> @@ -306,6 +307,21 @@ int v4l2_ctrl_handler_init_class(struct
> v4l2_ctrl_handler *hdl, */
>  void v4l2_ctrl_handler_free(struct v4l2_ctrl_handler *hdl);
> 
> +/*
> + * devm_v4l2_ctrl_handler_init - managed control handler initialization
> + *
> + * @dev: Device the @hdl belongs to.
> + * @hdl:	The control handler.
> + * @nr_of_controls_hint: A hint of how many controls this handler is
> + *		expected to refer to.
> + *
> + * This is a managed version of v4l2_ctrl_handler_init. Handler initialized
> with + * this function will be automatically cleaned up on driver detach. +
> */
> +int devm_v4l2_ctrl_handler_init(struct device *dev,
> +				struct v4l2_ctrl_handler *hdl,
> +				unsigned nr_of_controls_hint);
> +
>  /** v4l2_ctrl_handler_setup() - Call the s_ctrl op for all controls
> belonging * to the handler to initialize the hardware to the current
> control values. * @hdl:	The control handler.
  

Patch

diff --git a/drivers/media/v4l2-core/v4l2-ctrls.c b/drivers/media/v4l2-core/v4l2-ctrls.c
index ebb8e48..f47ccfa 100644
--- a/drivers/media/v4l2-core/v4l2-ctrls.c
+++ b/drivers/media/v4l2-core/v4l2-ctrls.c
@@ -1421,6 +1421,38 @@  void v4l2_ctrl_handler_free(struct v4l2_ctrl_handler *hdl)
 }
 EXPORT_SYMBOL(v4l2_ctrl_handler_free);
 
+static void devm_v4l2_ctrl_handler_release(struct device *dev, void *res)
+{
+	struct v4l2_ctrl_handler **hdl = res;
+
+	v4l2_ctrl_handler_free(*hdl);
+}
+
+int devm_v4l2_ctrl_handler_init(struct device *dev,
+				struct v4l2_ctrl_handler *hdl,
+				unsigned nr_of_controls_hint)
+{
+	struct v4l2_ctrl_handler **dr;
+	int rc;
+
+	dr = devres_alloc(devm_v4l2_ctrl_handler_release, sizeof(*dr),
+			  GFP_KERNEL);
+	if (!dr)
+		return -ENOMEM;
+
+	rc = v4l2_ctrl_handler_init(hdl, nr_of_controls_hint);
+	if (rc) {
+		devres_free(dr);
+		return rc;
+	}
+
+	*dr = hdl;
+	devres_add(dev, dr);
+
+	return 0;
+}
+EXPORT_SYMBOL_GPL(devm_v4l2_ctrl_handler_init);
+
 /* For backwards compatibility: V4L2_CID_PRIVATE_BASE should no longer
    be used except in G_CTRL, S_CTRL, QUERYCTRL and QUERYMENU when dealing
    with applications that do not use the NEXT_CTRL flag.
diff --git a/include/media/v4l2-ctrls.h b/include/media/v4l2-ctrls.h
index 7343a27..169443f 100644
--- a/include/media/v4l2-ctrls.h
+++ b/include/media/v4l2-ctrls.h
@@ -25,6 +25,7 @@ 
 #include <linux/videodev2.h>
 
 /* forward references */
+struct device;
 struct file;
 struct v4l2_ctrl_handler;
 struct v4l2_ctrl_helper;
@@ -306,6 +307,21 @@  int v4l2_ctrl_handler_init_class(struct v4l2_ctrl_handler *hdl,
   */
 void v4l2_ctrl_handler_free(struct v4l2_ctrl_handler *hdl);
 
+/*
+ * devm_v4l2_ctrl_handler_init - managed control handler initialization
+ *
+ * @dev: Device the @hdl belongs to.
+ * @hdl:	The control handler.
+ * @nr_of_controls_hint: A hint of how many controls this handler is
+ *		expected to refer to.
+ *
+ * This is a managed version of v4l2_ctrl_handler_init. Handler initialized with
+ * this function will be automatically cleaned up on driver detach.
+ */
+int devm_v4l2_ctrl_handler_init(struct device *dev,
+				struct v4l2_ctrl_handler *hdl,
+				unsigned nr_of_controls_hint);
+
 /** v4l2_ctrl_handler_setup() - Call the s_ctrl op for all controls belonging
   * to the handler to initialize the hardware to the current control values.
   * @hdl:	The control handler.