Message ID | 1368692074-483-3-git-send-email-a.hajda@samsung.com (mailing list archive) |
---|---|
State | Superseded, archived |
Delegated to: | Laurent Pinchart |
Headers |
Received: from mail.tu-berlin.de ([130.149.7.33]) by www.linuxtv.org with esmtp (Exim 4.72) (envelope-from <linux-media-owner@vger.kernel.org>) id 1UctLO-0003Sz-F9; Thu, 16 May 2013 10:15:22 +0200 X-tubIT-Incoming-IP: 209.132.180.67 Received: from vger.kernel.org ([209.132.180.67]) by mail.tu-berlin.de (exim-4.72/mailfrontend-8) with esmtp id 1UctLM-0005v1-kN; Thu, 16 May 2013 10:15:22 +0200 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1756162Ab3EPIPP (ORCPT <rfc822;mkrufky@linuxtv.org> + 1 other); Thu, 16 May 2013 04:15:15 -0400 Received: from mailout4.w1.samsung.com ([210.118.77.14]:32066 "EHLO mailout4.w1.samsung.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1755310Ab3EPIPI (ORCPT <rfc822;linux-media@vger.kernel.org>); Thu, 16 May 2013 04:15:08 -0400 Received: from eucpsbgm2.samsung.com (unknown [203.254.199.245]) by mailout4.w1.samsung.com (Oracle Communications Messaging Server 7u4-24.01(7.0.4.24.0) 64bit (built Nov 17 2011)) with ESMTP id <0MMV00189TKYHT40@mailout4.w1.samsung.com> for linux-media@vger.kernel.org; Thu, 16 May 2013 09:15:01 +0100 (BST) X-AuditID: cbfec7f5-b7f376d000001ec6-89-519495841866 Received: from eusync2.samsung.com ( [203.254.199.212]) by eucpsbgm2.samsung.com (EUCPMTA) with SMTP id 4D.1D.07878.48594915; Thu, 16 May 2013 09:15:00 +0100 (BST) Received: from AMDC1061.digital.local ([106.116.147.88]) by eusync2.samsung.com (Oracle Communications Messaging Server 7u4-23.01 (7.0.4.23.0) 64bit (built Aug 10 2011)) with ESMTPA id <0MMV00JNCTKOHX00@eusync2.samsung.com>; Thu, 16 May 2013 09:15:00 +0100 (BST) From: Andrzej Hajda <a.hajda@samsung.com> To: linux-media@vger.kernel.org Cc: Laurent Pinchart <laurent.pinchart@ideasonboard.com>, Sylwester Nawrocki <s.nawrocki@samsung.com>, Sakari Ailus <sakari.ailus@iki.fi>, Kyungmin Park <kyungmin.park@samsung.com>, hj210.choi@samsung.com, sw0312.kim@samsung.com, Andrzej Hajda <a.hajda@samsung.com> Subject: [PATCH RFC v3 2/3] media: added managed v4l2 control initialization Date: Thu, 16 May 2013 10:14:33 +0200 Message-id: <1368692074-483-3-git-send-email-a.hajda@samsung.com> X-Mailer: git-send-email 1.7.10.4 In-reply-to: <1368692074-483-1-git-send-email-a.hajda@samsung.com> References: <1368692074-483-1-git-send-email-a.hajda@samsung.com> X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFvrCJMWRmVeSWpSXmKPExsVy+t/xK7otU6cEGjzot7S4te4cq8XjjdeY Lc42vWG36Jy4hN2iZ8NWVovDb9pZLc7sX8lmMWPySzYHDo/ZHTNZPQ5/Xcji0bdlFaPH501y ASxRXDYpqTmZZalF+nYJXBlrdz1kLngvXvHr2WamBsYXwl2MnBwSAiYSf189ZoKwxSQu3FvP BmILCSxllPi42a+LkQvI7mOS2D7/IgtIgk1AU+Lv5ptgRSIC8hJPem+wgRQxC0xkkrj6dxmQ w8EhLOAjsf9TBEgNi4CqRMuZSSwgYV4BR4l3W9UhdilKdD+bADaGU8BJYvXyR1B7HSW+3uhl nMDIu4CRYRWjaGppckFxUnqukV5xYm5xaV66XnJ+7iZGSEh93cG49JjVIUYBDkYlHl7L6ZMD hVgTy4orcw8xSnAwK4nwzkidEijEm5JYWZValB9fVJqTWnyIkYmDU6qB8fCWJKXwX9P3r7QI kdtdt+moj0Kny9tVjEoR8R73vpm/nLrvyw5xMwU9Ky4/d8+Tv7ZapO1vlNTW/hGX0Vy3hUfz 4sc27Sv7/z/y0rQ+cCVZS66HwzfafZdFqKHSYo8J7NPbr8+cPXXZPd/YSaJqss6P332+aVDz PnpJzdnyGNFjZgu2/z3WpsRSnJFoqMVcVJwIAL4aitYHAgAA Sender: linux-media-owner@vger.kernel.org Precedence: bulk List-ID: <linux-media.vger.kernel.org> X-Mailing-List: linux-media@vger.kernel.org X-PMX-Version: 6.0.0.2142326, Antispam-Engine: 2.7.2.2107409, Antispam-Data: 2013.5.16.80021 X-PMX-Spam: Gauge=IIIIIIIII, Probability=9%, Report=' HTML_NO_HTTP 0.1, MULTIPLE_RCPTS 0.1, HTML_00_10 0.05, BODY_SIZE_3000_3999 0, BODY_SIZE_5000_LESS 0, BODY_SIZE_7000_LESS 0, URI_ENDS_IN_HTML 0, __ANY_URI 0, __CP_URI_IN_BODY 0, __HAS_FROM 0, __HAS_HTML 0, __HAS_MSGID 0, __HAS_X_MAILER 0, __HAS_X_MAILING_LIST 0, __IN_REP_TO 0, __MIME_TEXT_ONLY 0, __MULTIPLE_RCPTS_CC_X2 0, __SANE_MSGID 0, __TO_MALFORMED_2 0, __TO_NO_NAME 0, __URI_NO_WWW 0, __URI_NS ' |
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
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. > +} > +
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
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
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
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.
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.
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
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?
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
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
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.
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.