All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v1 0/2] media: v4l2-subdev: Fix link validation issue
@ 2023-11-13 10:17 Laurent Pinchart
  2023-11-13 10:17 ` [PATCH v1 1/2] media: v4l2-subdev: Drop unreacheable warning Laurent Pinchart
  2023-11-13 10:17 ` [PATCH v1 2/2] media: v4l2-subdev: Relax warnings in link validation Laurent Pinchart
  0 siblings, 2 replies; 10+ messages in thread
From: Laurent Pinchart @ 2023-11-13 10:17 UTC (permalink / raw)
  To: linux-media; +Cc: Tomi Valkeinen, Sakari Ailus, Kieran Bingham

Hello,

This small patch series addresses an incorrect (in my opinion) warning
printed by the v4l2_subdev_link_validate() helper. Patch 1/2 is a small
drive-by cleanup, and the important change is in 2/2. Please see the
patch commit message for details.

The series has been tested with the VSP1 driver, and correctly gets rid
of the warning.

Laurent Pinchart (2):
  media: v4l2-subdev: Drop unreacheable warning
  media: v4l2-subdev: Relax warnings in link validation

 drivers/media/v4l2-core/v4l2-subdev.c | 38 ++++++++++++++++-----------
 1 file changed, 23 insertions(+), 15 deletions(-)


base-commit: b85ea95d086471afb4ad062012a4d73cd328fa86
-- 
Regards,

Laurent Pinchart


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [PATCH v1 1/2] media: v4l2-subdev: Drop unreacheable warning
  2023-11-13 10:17 [PATCH v1 0/2] media: v4l2-subdev: Fix link validation issue Laurent Pinchart
@ 2023-11-13 10:17 ` Laurent Pinchart
  2023-11-13 10:42   ` Sakari Ailus
  2023-11-13 11:41   ` Tomi Valkeinen
  2023-11-13 10:17 ` [PATCH v1 2/2] media: v4l2-subdev: Relax warnings in link validation Laurent Pinchart
  1 sibling, 2 replies; 10+ messages in thread
From: Laurent Pinchart @ 2023-11-13 10:17 UTC (permalink / raw)
  To: linux-media; +Cc: Tomi Valkeinen, Sakari Ailus, Kieran Bingham

The v4l2_subdev_link_validate_get_format() function warns if the pad
given as argument belongs to a non-subdev entity. This can't happen, as
the function is called from v4l2_subdev_link_validate() only (indirectly
through v4l2_subdev_link_validate_locked()), and that function ensures
that both ends of the link are subdevs.

Signed-off-by: Laurent Pinchart <laurent.pinchart+renesas@ideasonboard.com>
---
 drivers/media/v4l2-core/v4l2-subdev.c | 8 --------
 1 file changed, 8 deletions(-)

diff --git a/drivers/media/v4l2-core/v4l2-subdev.c b/drivers/media/v4l2-core/v4l2-subdev.c
index be86b906c985..67d43206ce32 100644
--- a/drivers/media/v4l2-core/v4l2-subdev.c
+++ b/drivers/media/v4l2-core/v4l2-subdev.c
@@ -1184,14 +1184,6 @@ v4l2_subdev_link_validate_get_format(struct media_pad *pad, u32 stream,
 	struct v4l2_subdev *sd;
 	int ret;
 
-	if (!is_media_entity_v4l2_subdev(pad->entity)) {
-		WARN(pad->entity->function != MEDIA_ENT_F_IO_V4L,
-		     "Driver bug! Wrong media entity type 0x%08x, entity %s\n",
-		     pad->entity->function, pad->entity->name);
-
-		return -EINVAL;
-	}
-
 	sd = media_entity_to_v4l2_subdev(pad->entity);
 
 	fmt->which = V4L2_SUBDEV_FORMAT_ACTIVE;
-- 
Regards,

Laurent Pinchart


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* [PATCH v1 2/2] media: v4l2-subdev: Relax warnings in link validation
  2023-11-13 10:17 [PATCH v1 0/2] media: v4l2-subdev: Fix link validation issue Laurent Pinchart
  2023-11-13 10:17 ` [PATCH v1 1/2] media: v4l2-subdev: Drop unreacheable warning Laurent Pinchart
@ 2023-11-13 10:17 ` Laurent Pinchart
  2023-11-13 11:06   ` Sakari Ailus
  2023-11-13 13:03   ` Tomi Valkeinen
  1 sibling, 2 replies; 10+ messages in thread
From: Laurent Pinchart @ 2023-11-13 10:17 UTC (permalink / raw)
  To: linux-media; +Cc: Tomi Valkeinen, Sakari Ailus, Kieran Bingham

Before v6.3, the v4l2_subdev_link_validate() helper would ignore links
whose source was a video device and sink a subdev. The helper was (and
still is) used by drivers in such cases, in particular for subdevs with
multiple sink pads, some connected to video devices and some to other
subdevs.

Then, commit a6b995ed03ff ("media: subdev: use streams in
v4l2_subdev_link_validate()") assumed the entities on the two sides of a
link are both subdevs, and caused crashes in drivers that use the helper
with subdev sink pads connected to video devices. Commit 55f1ecb11990
("media: v4l: subdev: Make link validation safer"), merged in v6.4,
fixed the crash by adding an explicit check with a pr_warn_once(),
mentioning a driver bug.

Links between a subdev and a video device need to be validated, and
v4l2_subdev_link_validate() can't handle that. Drivers typically handle
this validation manually at stream start time (either in the .streamon()
ioctl handler, or in the .start_streaming() vb2 queue operation),
instead of implementing a custom .link_validate() handler. Forbidding
usage of v4l2_subdev_link_validate() as the .link_validate() handler
would thus force all subdev drivers that mix source links to subdev and
video devices to implement a custom .link_validate() handler that
returns immediately for the video device links and call
v4l2_subdev_link_validate() for subdev links. This would create lots of
duplicated code for no real gain. Instead, relax the check in
v4l2_ctrl_modify_range() to ignore links from a video device to a subdev
without printing any warning.

Fixes: a6b995ed03ff ("media: subdev: use streams in v4l2_subdev_link_validate()")
Signed-off-by: Laurent Pinchart <laurent.pinchart+renesas@ideasonboard.com>
---
 drivers/media/v4l2-core/v4l2-subdev.c | 30 ++++++++++++++++++++-------
 1 file changed, 23 insertions(+), 7 deletions(-)

diff --git a/drivers/media/v4l2-core/v4l2-subdev.c b/drivers/media/v4l2-core/v4l2-subdev.c
index 67d43206ce32..b00be1d57e05 100644
--- a/drivers/media/v4l2-core/v4l2-subdev.c
+++ b/drivers/media/v4l2-core/v4l2-subdev.c
@@ -1356,15 +1356,31 @@ int v4l2_subdev_link_validate(struct media_link *link)
 	struct v4l2_subdev *source_sd, *sink_sd;
 	struct v4l2_subdev_state *source_state, *sink_state;
 	bool states_locked;
+	bool is_sink_subdev;
+	bool is_source_subdev;
 	int ret;
 
-	if (!is_media_entity_v4l2_subdev(link->sink->entity) ||
-	    !is_media_entity_v4l2_subdev(link->source->entity)) {
-		pr_warn_once("%s of link '%s':%u->'%s':%u is not a V4L2 sub-device, driver bug!\n",
-			     !is_media_entity_v4l2_subdev(link->sink->entity) ?
-			     "sink" : "source",
-			     link->source->entity->name, link->source->index,
-			     link->sink->entity->name, link->sink->index);
+	is_sink_subdev = is_media_entity_v4l2_subdev(link->sink->entity);
+	is_source_subdev = is_media_entity_v4l2_subdev(link->source->entity);
+
+	if (!is_sink_subdev || !is_source_subdev) {
+		/*
+		 * Do not print the warning if the source is a video device and
+		 * the sink a subdev. This is a valid use case, to allow usage
+		 * of this helper by subdev drivers that have multiple sink
+		 * pads, some connected to video devices and some connected to
+		 * other subdevs. The video device to subdev link is typically
+		 * validated manually by the driver at stream start time in such
+		 * cases.
+		 */
+		if (!is_sink_subdev ||
+		    !is_media_entity_v4l2_video_device(link->source->entity))
+			pr_warn_once("%s of link '%s':%u->'%s':%u is not a V4L2 sub-device, driver bug!\n",
+				     !is_sink_subdev ? "sink" : "source",
+				     link->source->entity->name,
+				     link->source->index,
+				     link->sink->entity->name,
+				     link->sink->index);
 		return 0;
 	}
 
-- 
Regards,

Laurent Pinchart


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* Re: [PATCH v1 1/2] media: v4l2-subdev: Drop unreacheable warning
  2023-11-13 10:17 ` [PATCH v1 1/2] media: v4l2-subdev: Drop unreacheable warning Laurent Pinchart
@ 2023-11-13 10:42   ` Sakari Ailus
  2023-11-13 10:50     ` Laurent Pinchart
  2023-11-13 11:41   ` Tomi Valkeinen
  1 sibling, 1 reply; 10+ messages in thread
From: Sakari Ailus @ 2023-11-13 10:42 UTC (permalink / raw)
  To: Laurent Pinchart; +Cc: linux-media, Tomi Valkeinen, Kieran Bingham

Hi Laurent,

On Mon, Nov 13, 2023 at 12:17:17PM +0200, Laurent Pinchart wrote:
> The v4l2_subdev_link_validate_get_format() function warns if the pad
> given as argument belongs to a non-subdev entity. This can't happen, as
> the function is called from v4l2_subdev_link_validate() only (indirectly
> through v4l2_subdev_link_validate_locked()), and that function ensures
> that both ends of the link are subdevs.
> 
> Signed-off-by: Laurent Pinchart <laurent.pinchart+renesas@ideasonboard.com>
> ---
>  drivers/media/v4l2-core/v4l2-subdev.c | 8 --------
>  1 file changed, 8 deletions(-)
> 
> diff --git a/drivers/media/v4l2-core/v4l2-subdev.c b/drivers/media/v4l2-core/v4l2-subdev.c
> index be86b906c985..67d43206ce32 100644
> --- a/drivers/media/v4l2-core/v4l2-subdev.c
> +++ b/drivers/media/v4l2-core/v4l2-subdev.c
> @@ -1184,14 +1184,6 @@ v4l2_subdev_link_validate_get_format(struct media_pad *pad, u32 stream,
>  	struct v4l2_subdev *sd;
>  	int ret;
>  
> -	if (!is_media_entity_v4l2_subdev(pad->entity)) {
> -		WARN(pad->entity->function != MEDIA_ENT_F_IO_V4L,
> -		     "Driver bug! Wrong media entity type 0x%08x, entity %s\n",
> -		     pad->entity->function, pad->entity->name);
> -
> -		return -EINVAL;
> -	}
> -
>  	sd = media_entity_to_v4l2_subdev(pad->entity);

It'd be good to check sd is non-NULL here, although pad presumably is
always a pad of a sub-device entity.

>  
>  	fmt->which = V4L2_SUBDEV_FORMAT_ACTIVE;

-- 
Regards,

Sakari Ailus

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH v1 1/2] media: v4l2-subdev: Drop unreacheable warning
  2023-11-13 10:42   ` Sakari Ailus
@ 2023-11-13 10:50     ` Laurent Pinchart
  0 siblings, 0 replies; 10+ messages in thread
From: Laurent Pinchart @ 2023-11-13 10:50 UTC (permalink / raw)
  To: Sakari Ailus; +Cc: linux-media, Tomi Valkeinen, Kieran Bingham

On Mon, Nov 13, 2023 at 10:42:16AM +0000, Sakari Ailus wrote:
> Hi Laurent,
> 
> On Mon, Nov 13, 2023 at 12:17:17PM +0200, Laurent Pinchart wrote:
> > The v4l2_subdev_link_validate_get_format() function warns if the pad
> > given as argument belongs to a non-subdev entity. This can't happen, as
> > the function is called from v4l2_subdev_link_validate() only (indirectly
> > through v4l2_subdev_link_validate_locked()), and that function ensures
> > that both ends of the link are subdevs.
> > 
> > Signed-off-by: Laurent Pinchart <laurent.pinchart+renesas@ideasonboard.com>
> > ---
> >  drivers/media/v4l2-core/v4l2-subdev.c | 8 --------
> >  1 file changed, 8 deletions(-)
> > 
> > diff --git a/drivers/media/v4l2-core/v4l2-subdev.c b/drivers/media/v4l2-core/v4l2-subdev.c
> > index be86b906c985..67d43206ce32 100644
> > --- a/drivers/media/v4l2-core/v4l2-subdev.c
> > +++ b/drivers/media/v4l2-core/v4l2-subdev.c
> > @@ -1184,14 +1184,6 @@ v4l2_subdev_link_validate_get_format(struct media_pad *pad, u32 stream,
> >  	struct v4l2_subdev *sd;
> >  	int ret;
> >  
> > -	if (!is_media_entity_v4l2_subdev(pad->entity)) {
> > -		WARN(pad->entity->function != MEDIA_ENT_F_IO_V4L,
> > -		     "Driver bug! Wrong media entity type 0x%08x, entity %s\n",
> > -		     pad->entity->function, pad->entity->name);
> > -
> > -		return -EINVAL;
> > -	}
> > -
> >  	sd = media_entity_to_v4l2_subdev(pad->entity);
> 
> It'd be good to check sd is non-NULL here, although pad presumably is
> always a pad of a sub-device entity.

It's guaranteed by the caller. Is there a specific reason you think a
check would be good ?

> >  
> >  	fmt->which = V4L2_SUBDEV_FORMAT_ACTIVE;

-- 
Regards,

Laurent Pinchart

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH v1 2/2] media: v4l2-subdev: Relax warnings in link validation
  2023-11-13 10:17 ` [PATCH v1 2/2] media: v4l2-subdev: Relax warnings in link validation Laurent Pinchart
@ 2023-11-13 11:06   ` Sakari Ailus
  2023-11-13 11:37     ` Laurent Pinchart
  2023-11-13 13:03   ` Tomi Valkeinen
  1 sibling, 1 reply; 10+ messages in thread
From: Sakari Ailus @ 2023-11-13 11:06 UTC (permalink / raw)
  To: Laurent Pinchart; +Cc: linux-media, Tomi Valkeinen, Kieran Bingham

Hi Laurent,

Thanks for the patch.

On Mon, Nov 13, 2023 at 12:17:18PM +0200, Laurent Pinchart wrote:
> Before v6.3, the v4l2_subdev_link_validate() helper would ignore links
> whose source was a video device and sink a subdev. The helper was (and
> still is) used by drivers in such cases, in particular for subdevs with
> multiple sink pads, some connected to video devices and some to other
> subdevs.
> 
> Then, commit a6b995ed03ff ("media: subdev: use streams in
> v4l2_subdev_link_validate()") assumed the entities on the two sides of a
> link are both subdevs, and caused crashes in drivers that use the helper
> with subdev sink pads connected to video devices. Commit 55f1ecb11990
> ("media: v4l: subdev: Make link validation safer"), merged in v6.4,
> fixed the crash by adding an explicit check with a pr_warn_once(),
> mentioning a driver bug.
> 
> Links between a subdev and a video device need to be validated, and
> v4l2_subdev_link_validate() can't handle that. Drivers typically handle
> this validation manually at stream start time (either in the .streamon()
> ioctl handler, or in the .start_streaming() vb2 queue operation),
> instead of implementing a custom .link_validate() handler. Forbidding

While some do the validation as part of the streamon callback, it'd be
nicer to move this to the link_validate callback instead: this is what the
callback is for. I'd presume not may drivers depend on
v4l2_subdev_link_validate() fail silently on non-subdevices as the issue
hasn't been reported before while the patch that seems to have broken this
was merged in 6.3.

Not failing silently in link_validate also ensures the validation gets
done: there have been drivers (more than one) that have simply missed the
link validation due to the issue (non-sub-device entity on one end) being
silently ignored by default.

> usage of v4l2_subdev_link_validate() as the .link_validate() handler
> would thus force all subdev drivers that mix source links to subdev and
> video devices to implement a custom .link_validate() handler that
> returns immediately for the video device links and call
> v4l2_subdev_link_validate() for subdev links. This would create lots of
> duplicated code for no real gain. Instead, relax the check in
> v4l2_ctrl_modify_range() to ignore links from a video device to a subdev
> without printing any warning.
> 
> Fixes: a6b995ed03ff ("media: subdev: use streams in v4l2_subdev_link_validate()")
> Signed-off-by: Laurent Pinchart <laurent.pinchart+renesas@ideasonboard.com>
> ---
>  drivers/media/v4l2-core/v4l2-subdev.c | 30 ++++++++++++++++++++-------
>  1 file changed, 23 insertions(+), 7 deletions(-)
> 
> diff --git a/drivers/media/v4l2-core/v4l2-subdev.c b/drivers/media/v4l2-core/v4l2-subdev.c
> index 67d43206ce32..b00be1d57e05 100644
> --- a/drivers/media/v4l2-core/v4l2-subdev.c
> +++ b/drivers/media/v4l2-core/v4l2-subdev.c
> @@ -1356,15 +1356,31 @@ int v4l2_subdev_link_validate(struct media_link *link)
>  	struct v4l2_subdev *source_sd, *sink_sd;
>  	struct v4l2_subdev_state *source_state, *sink_state;
>  	bool states_locked;
> +	bool is_sink_subdev;
> +	bool is_source_subdev;
>  	int ret;
>  
> -	if (!is_media_entity_v4l2_subdev(link->sink->entity) ||
> -	    !is_media_entity_v4l2_subdev(link->source->entity)) {
> -		pr_warn_once("%s of link '%s':%u->'%s':%u is not a V4L2 sub-device, driver bug!\n",
> -			     !is_media_entity_v4l2_subdev(link->sink->entity) ?
> -			     "sink" : "source",
> -			     link->source->entity->name, link->source->index,
> -			     link->sink->entity->name, link->sink->index);
> +	is_sink_subdev = is_media_entity_v4l2_subdev(link->sink->entity);
> +	is_source_subdev = is_media_entity_v4l2_subdev(link->source->entity);
> +
> +	if (!is_sink_subdev || !is_source_subdev) {
> +		/*
> +		 * Do not print the warning if the source is a video device and
> +		 * the sink a subdev. This is a valid use case, to allow usage
> +		 * of this helper by subdev drivers that have multiple sink
> +		 * pads, some connected to video devices and some connected to
> +		 * other subdevs. The video device to subdev link is typically
> +		 * validated manually by the driver at stream start time in such
> +		 * cases.
> +		 */
> +		if (!is_sink_subdev ||
> +		    !is_media_entity_v4l2_video_device(link->source->entity))
> +			pr_warn_once("%s of link '%s':%u->'%s':%u is not a V4L2 sub-device, driver bug!\n",
> +				     !is_sink_subdev ? "sink" : "source",
> +				     link->source->entity->name,
> +				     link->source->index,
> +				     link->sink->entity->name,
> +				     link->sink->index);
>  		return 0;
>  	}
>  

-- 
Kind regards,

Sakari Ailus

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH v1 2/2] media: v4l2-subdev: Relax warnings in link validation
  2023-11-13 11:06   ` Sakari Ailus
@ 2023-11-13 11:37     ` Laurent Pinchart
  2023-11-13 13:53       ` Sakari Ailus
  0 siblings, 1 reply; 10+ messages in thread
From: Laurent Pinchart @ 2023-11-13 11:37 UTC (permalink / raw)
  To: Sakari Ailus; +Cc: linux-media, Tomi Valkeinen, Kieran Bingham

On Mon, Nov 13, 2023 at 11:06:34AM +0000, Sakari Ailus wrote:
> Hi Laurent,
> 
> Thanks for the patch.
> 
> On Mon, Nov 13, 2023 at 12:17:18PM +0200, Laurent Pinchart wrote:
> > Before v6.3, the v4l2_subdev_link_validate() helper would ignore links
> > whose source was a video device and sink a subdev. The helper was (and
> > still is) used by drivers in such cases, in particular for subdevs with
> > multiple sink pads, some connected to video devices and some to other
> > subdevs.
> > 
> > Then, commit a6b995ed03ff ("media: subdev: use streams in
> > v4l2_subdev_link_validate()") assumed the entities on the two sides of a
> > link are both subdevs, and caused crashes in drivers that use the helper
> > with subdev sink pads connected to video devices. Commit 55f1ecb11990
> > ("media: v4l: subdev: Make link validation safer"), merged in v6.4,
> > fixed the crash by adding an explicit check with a pr_warn_once(),
> > mentioning a driver bug.
> > 
> > Links between a subdev and a video device need to be validated, and
> > v4l2_subdev_link_validate() can't handle that. Drivers typically handle
> > this validation manually at stream start time (either in the .streamon()
> > ioctl handler, or in the .start_streaming() vb2 queue operation),
> > instead of implementing a custom .link_validate() handler. Forbidding
> 
> While some do the validation as part of the streamon callback, it'd be
> nicer to move this to the link_validate callback instead: this is what the
> callback is for.

Is there any driver that does so already ?

> I'd presume not may drivers depend on
> v4l2_subdev_link_validate() fail silently on non-subdevices as the issue
> hasn't been reported before while the patch that seems to have broken this
> was merged in 6.3.

At least the VSP1 driver depends on it. The i.MX8MP ISI driver does as
well, it got merged in v6.4, so the crash in v6.3 didn't get noticed.
I'd be surprised if the omap3isp driver also didn't depend on this
behaviour when operating in M2M mode.

> Not failing silently in link_validate also ensures the validation gets
> done: there have been drivers (more than one) that have simply missed the
> link validation due to the issue (non-sub-device entity on one end) being
> silently ignored by default.

If there's a consensus that subdev to video device link validation need
to be done in the .link_validate() operation, then work needs to be done
to make that happen. I'd like to see at least one proof of concept
implemented, to make sure there are no unforeseen issues. Are you
volunteering ? :-)

In the meantime, most drivers (possibly all) validate that link manually
outside of the .link_validate() operation, so this is the de facto
typical mode of operation. I don't think it can be considered as a
driver bug until we make a reasonable effort to convert drivers to a new
model. It's not fair to just add a pr_warn_once() without having done
some of the work needed to move to a new model.

> > usage of v4l2_subdev_link_validate() as the .link_validate() handler
> > would thus force all subdev drivers that mix source links to subdev and
> > video devices to implement a custom .link_validate() handler that
> > returns immediately for the video device links and call
> > v4l2_subdev_link_validate() for subdev links. This would create lots of
> > duplicated code for no real gain. Instead, relax the check in
> > v4l2_ctrl_modify_range() to ignore links from a video device to a subdev
> > without printing any warning.
> > 
> > Fixes: a6b995ed03ff ("media: subdev: use streams in v4l2_subdev_link_validate()")
> > Signed-off-by: Laurent Pinchart <laurent.pinchart+renesas@ideasonboard.com>
> > ---
> >  drivers/media/v4l2-core/v4l2-subdev.c | 30 ++++++++++++++++++++-------
> >  1 file changed, 23 insertions(+), 7 deletions(-)
> > 
> > diff --git a/drivers/media/v4l2-core/v4l2-subdev.c b/drivers/media/v4l2-core/v4l2-subdev.c
> > index 67d43206ce32..b00be1d57e05 100644
> > --- a/drivers/media/v4l2-core/v4l2-subdev.c
> > +++ b/drivers/media/v4l2-core/v4l2-subdev.c
> > @@ -1356,15 +1356,31 @@ int v4l2_subdev_link_validate(struct media_link *link)
> >  	struct v4l2_subdev *source_sd, *sink_sd;
> >  	struct v4l2_subdev_state *source_state, *sink_state;
> >  	bool states_locked;
> > +	bool is_sink_subdev;
> > +	bool is_source_subdev;
> >  	int ret;
> >  
> > -	if (!is_media_entity_v4l2_subdev(link->sink->entity) ||
> > -	    !is_media_entity_v4l2_subdev(link->source->entity)) {
> > -		pr_warn_once("%s of link '%s':%u->'%s':%u is not a V4L2 sub-device, driver bug!\n",
> > -			     !is_media_entity_v4l2_subdev(link->sink->entity) ?
> > -			     "sink" : "source",
> > -			     link->source->entity->name, link->source->index,
> > -			     link->sink->entity->name, link->sink->index);
> > +	is_sink_subdev = is_media_entity_v4l2_subdev(link->sink->entity);
> > +	is_source_subdev = is_media_entity_v4l2_subdev(link->source->entity);
> > +
> > +	if (!is_sink_subdev || !is_source_subdev) {
> > +		/*
> > +		 * Do not print the warning if the source is a video device and
> > +		 * the sink a subdev. This is a valid use case, to allow usage
> > +		 * of this helper by subdev drivers that have multiple sink
> > +		 * pads, some connected to video devices and some connected to
> > +		 * other subdevs. The video device to subdev link is typically
> > +		 * validated manually by the driver at stream start time in such
> > +		 * cases.
> > +		 */
> > +		if (!is_sink_subdev ||
> > +		    !is_media_entity_v4l2_video_device(link->source->entity))
> > +			pr_warn_once("%s of link '%s':%u->'%s':%u is not a V4L2 sub-device, driver bug!\n",
> > +				     !is_sink_subdev ? "sink" : "source",
> > +				     link->source->entity->name,
> > +				     link->source->index,
> > +				     link->sink->entity->name,
> > +				     link->sink->index);
> >  		return 0;
> >  	}
> >  

-- 
Regards,

Laurent Pinchart

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH v1 1/2] media: v4l2-subdev: Drop unreacheable warning
  2023-11-13 10:17 ` [PATCH v1 1/2] media: v4l2-subdev: Drop unreacheable warning Laurent Pinchart
  2023-11-13 10:42   ` Sakari Ailus
@ 2023-11-13 11:41   ` Tomi Valkeinen
  1 sibling, 0 replies; 10+ messages in thread
From: Tomi Valkeinen @ 2023-11-13 11:41 UTC (permalink / raw)
  To: Laurent Pinchart, linux-media; +Cc: Sakari Ailus, Kieran Bingham

On 13/11/2023 12:17, Laurent Pinchart wrote:
> The v4l2_subdev_link_validate_get_format() function warns if the pad
> given as argument belongs to a non-subdev entity. This can't happen, as
> the function is called from v4l2_subdev_link_validate() only (indirectly
> through v4l2_subdev_link_validate_locked()), and that function ensures
> that both ends of the link are subdevs.
> 
> Signed-off-by: Laurent Pinchart <laurent.pinchart+renesas@ideasonboard.com>
> ---
>   drivers/media/v4l2-core/v4l2-subdev.c | 8 --------
>   1 file changed, 8 deletions(-)
> 
> diff --git a/drivers/media/v4l2-core/v4l2-subdev.c b/drivers/media/v4l2-core/v4l2-subdev.c
> index be86b906c985..67d43206ce32 100644
> --- a/drivers/media/v4l2-core/v4l2-subdev.c
> +++ b/drivers/media/v4l2-core/v4l2-subdev.c
> @@ -1184,14 +1184,6 @@ v4l2_subdev_link_validate_get_format(struct media_pad *pad, u32 stream,
>   	struct v4l2_subdev *sd;
>   	int ret;
>   
> -	if (!is_media_entity_v4l2_subdev(pad->entity)) {
> -		WARN(pad->entity->function != MEDIA_ENT_F_IO_V4L,
> -		     "Driver bug! Wrong media entity type 0x%08x, entity %s\n",
> -		     pad->entity->function, pad->entity->name);
> -
> -		return -EINVAL;
> -	}
> -
>   	sd = media_entity_to_v4l2_subdev(pad->entity);
>   
>   	fmt->which = V4L2_SUBDEV_FORMAT_ACTIVE;

At some point I also wondered why we need to check for non-subdev 
entity. But, it was there already, so I left it...

Reviewed-by: Tomi Valkeinen <tomi.valkeinen@ideasonboard.com>

  Tomi


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH v1 2/2] media: v4l2-subdev: Relax warnings in link validation
  2023-11-13 10:17 ` [PATCH v1 2/2] media: v4l2-subdev: Relax warnings in link validation Laurent Pinchart
  2023-11-13 11:06   ` Sakari Ailus
@ 2023-11-13 13:03   ` Tomi Valkeinen
  1 sibling, 0 replies; 10+ messages in thread
From: Tomi Valkeinen @ 2023-11-13 13:03 UTC (permalink / raw)
  To: Laurent Pinchart, linux-media; +Cc: Sakari Ailus, Kieran Bingham

On 13/11/2023 12:17, Laurent Pinchart wrote:
> Before v6.3, the v4l2_subdev_link_validate() helper would ignore links
> whose source was a video device and sink a subdev. The helper was (and

Didn't it also ignore links where the sink is a video device?

> still is) used by drivers in such cases, in particular for subdevs with
> multiple sink pads, some connected to video devices and some to other
> subdevs.
> 
> Then, commit a6b995ed03ff ("media: subdev: use streams in
> v4l2_subdev_link_validate()") assumed the entities on the two sides of a
> link are both subdevs, and caused crashes in drivers that use the helper
> with subdev sink pads connected to video devices. Commit 55f1ecb11990
> ("media: v4l: subdev: Make link validation safer"), merged in v6.4,
> fixed the crash by adding an explicit check with a pr_warn_once(),
> mentioning a driver bug.
> 
> Links between a subdev and a video device need to be validated, and
> v4l2_subdev_link_validate() can't handle that. Drivers typically handle
> this validation manually at stream start time (either in the .streamon()
> ioctl handler, or in the .start_streaming() vb2 queue operation),
> instead of implementing a custom .link_validate() handler. Forbidding
> usage of v4l2_subdev_link_validate() as the .link_validate() handler
> would thus force all subdev drivers that mix source links to subdev and
> video devices to implement a custom .link_validate() handler that
> returns immediately for the video device links and call
> v4l2_subdev_link_validate() for subdev links. This would create lots of
> duplicated code for no real gain. Instead, relax the check in
> v4l2_ctrl_modify_range() to ignore links from a video device to a subdev

v4l2_ctrl_modify_range? copy-paste error?

Should the check also be relaxed wrt. video device as a sink?

> without printing any warning.

As Sakari said, it would make sense to use .link_validate() to validate 
all links.

But if this warning is getting printed at the moment, then I think this 
is a valid fix (maybe with the sink side handled too, if needed).

  Tomi

> Fixes: a6b995ed03ff ("media: subdev: use streams in v4l2_subdev_link_validate()")
> Signed-off-by: Laurent Pinchart <laurent.pinchart+renesas@ideasonboard.com>
> ---
>   drivers/media/v4l2-core/v4l2-subdev.c | 30 ++++++++++++++++++++-------
>   1 file changed, 23 insertions(+), 7 deletions(-)
> 
> diff --git a/drivers/media/v4l2-core/v4l2-subdev.c b/drivers/media/v4l2-core/v4l2-subdev.c
> index 67d43206ce32..b00be1d57e05 100644
> --- a/drivers/media/v4l2-core/v4l2-subdev.c
> +++ b/drivers/media/v4l2-core/v4l2-subdev.c
> @@ -1356,15 +1356,31 @@ int v4l2_subdev_link_validate(struct media_link *link)
>   	struct v4l2_subdev *source_sd, *sink_sd;
>   	struct v4l2_subdev_state *source_state, *sink_state;
>   	bool states_locked;
> +	bool is_sink_subdev;
> +	bool is_source_subdev;
>   	int ret;
>   
> -	if (!is_media_entity_v4l2_subdev(link->sink->entity) ||
> -	    !is_media_entity_v4l2_subdev(link->source->entity)) {
> -		pr_warn_once("%s of link '%s':%u->'%s':%u is not a V4L2 sub-device, driver bug!\n",
> -			     !is_media_entity_v4l2_subdev(link->sink->entity) ?
> -			     "sink" : "source",
> -			     link->source->entity->name, link->source->index,
> -			     link->sink->entity->name, link->sink->index);
> +	is_sink_subdev = is_media_entity_v4l2_subdev(link->sink->entity);
> +	is_source_subdev = is_media_entity_v4l2_subdev(link->source->entity);
> +
> +	if (!is_sink_subdev || !is_source_subdev) {
> +		/*
> +		 * Do not print the warning if the source is a video device and
> +		 * the sink a subdev. This is a valid use case, to allow usage
> +		 * of this helper by subdev drivers that have multiple sink
> +		 * pads, some connected to video devices and some connected to
> +		 * other subdevs. The video device to subdev link is typically
> +		 * validated manually by the driver at stream start time in such
> +		 * cases.
> +		 */
> +		if (!is_sink_subdev ||
> +		    !is_media_entity_v4l2_video_device(link->source->entity))
> +			pr_warn_once("%s of link '%s':%u->'%s':%u is not a V4L2 sub-device, driver bug!\n",
> +				     !is_sink_subdev ? "sink" : "source",
> +				     link->source->entity->name,
> +				     link->source->index,
> +				     link->sink->entity->name,
> +				     link->sink->index);
>   		return 0;
>   	}
>   


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH v1 2/2] media: v4l2-subdev: Relax warnings in link validation
  2023-11-13 11:37     ` Laurent Pinchart
@ 2023-11-13 13:53       ` Sakari Ailus
  0 siblings, 0 replies; 10+ messages in thread
From: Sakari Ailus @ 2023-11-13 13:53 UTC (permalink / raw)
  To: Laurent Pinchart; +Cc: linux-media, Tomi Valkeinen, Kieran Bingham, hverkuil

Hi Laurent,

On Mon, Nov 13, 2023 at 01:37:54PM +0200, Laurent Pinchart wrote:
> On Mon, Nov 13, 2023 at 11:06:34AM +0000, Sakari Ailus wrote:
> > Hi Laurent,
> > 
> > Thanks for the patch.
> > 
> > On Mon, Nov 13, 2023 at 12:17:18PM +0200, Laurent Pinchart wrote:
> > > Before v6.3, the v4l2_subdev_link_validate() helper would ignore links
> > > whose source was a video device and sink a subdev. The helper was (and
> > > still is) used by drivers in such cases, in particular for subdevs with
> > > multiple sink pads, some connected to video devices and some to other
> > > subdevs.
> > > 
> > > Then, commit a6b995ed03ff ("media: subdev: use streams in
> > > v4l2_subdev_link_validate()") assumed the entities on the two sides of a
> > > link are both subdevs, and caused crashes in drivers that use the helper
> > > with subdev sink pads connected to video devices. Commit 55f1ecb11990
> > > ("media: v4l: subdev: Make link validation safer"), merged in v6.4,
> > > fixed the crash by adding an explicit check with a pr_warn_once(),
> > > mentioning a driver bug.
> > > 
> > > Links between a subdev and a video device need to be validated, and
> > > v4l2_subdev_link_validate() can't handle that. Drivers typically handle
> > > this validation manually at stream start time (either in the .streamon()
> > > ioctl handler, or in the .start_streaming() vb2 queue operation),
> > > instead of implementing a custom .link_validate() handler. Forbidding
> > 
> > While some do the validation as part of the streamon callback, it'd be
> > nicer to move this to the link_validate callback instead: this is what the
> > callback is for.
> 
> Is there any driver that does so already ?

Based on a quick look IPU3-CIO2 and vimc do that. I guess there indeed are
also other drivers besides the vsp driver that do not, on the other hand.
But this is what I would have expected drivers to do all along: the
link_validate callback is where you validate the formats across the link
match, independently of whether the link is between sub-devices or a
sub-device and something else.

> 
> > I'd presume not may drivers depend on
> > v4l2_subdev_link_validate() fail silently on non-subdevices as the issue
> > hasn't been reported before while the patch that seems to have broken this
> > was merged in 6.3.
> 
> At least the VSP1 driver depends on it. The i.MX8MP ISI driver does as
> well, it got merged in v6.4, so the crash in v6.3 didn't get noticed.
> I'd be surprised if the omap3isp driver also didn't depend on this
> behaviour when operating in M2M mode.
> 
> > Not failing silently in link_validate also ensures the validation gets
> > done: there have been drivers (more than one) that have simply missed the
> > link validation due to the issue (non-sub-device entity on one end) being
> > silently ignored by default.
> 
> If there's a consensus that subdev to video device link validation need
> to be done in the .link_validate() operation, then work needs to be done
> to make that happen. I'd like to see at least one proof of concept
> implemented, to make sure there are no unforeseen issues. Are you
> volunteering ? :-)

I'd hope the driver authors to provide fixes: they have the hardware to
test, too.

> 
> In the meantime, most drivers (possibly all) validate that link manually
> outside of the .link_validate() operation, so this is the de facto
> typical mode of operation. I don't think it can be considered as a

That is not entirely correct. While such drivers exist, these are still a
subset of all drivers (performing link validation between sub-device and
video nodes).

> driver bug until we make a reasonable effort to convert drivers to a new
> model. It's not fair to just add a pr_warn_once() without having done
> some of the work needed to move to a new model.

I don't think the model really got changed. The documentation implies this,
it does not explicitly document what the driver is expected to do. I can
write a documentation patch for this.

Given that there are surprisingly more than one driver that seem to depend
on this, at the moment, I'm fine with reverting back to ignoring the error.
But we should still print a warning to signal the driver should be fixed.

I can address the omap3isp driver. The IPU6 ISYS driver already uses
link_validate callback.

Also cc Hans.

> 
> > > usage of v4l2_subdev_link_validate() as the .link_validate() handler
> > > would thus force all subdev drivers that mix source links to subdev and
> > > video devices to implement a custom .link_validate() handler that
> > > returns immediately for the video device links and call
> > > v4l2_subdev_link_validate() for subdev links. This would create lots of
> > > duplicated code for no real gain. Instead, relax the check in
> > > v4l2_ctrl_modify_range() to ignore links from a video device to a subdev
> > > without printing any warning.
> > > 
> > > Fixes: a6b995ed03ff ("media: subdev: use streams in v4l2_subdev_link_validate()")
> > > Signed-off-by: Laurent Pinchart <laurent.pinchart+renesas@ideasonboard.com>
> > > ---
> > >  drivers/media/v4l2-core/v4l2-subdev.c | 30 ++++++++++++++++++++-------
> > >  1 file changed, 23 insertions(+), 7 deletions(-)
> > > 
> > > diff --git a/drivers/media/v4l2-core/v4l2-subdev.c b/drivers/media/v4l2-core/v4l2-subdev.c
> > > index 67d43206ce32..b00be1d57e05 100644
> > > --- a/drivers/media/v4l2-core/v4l2-subdev.c
> > > +++ b/drivers/media/v4l2-core/v4l2-subdev.c
> > > @@ -1356,15 +1356,31 @@ int v4l2_subdev_link_validate(struct media_link *link)
> > >  	struct v4l2_subdev *source_sd, *sink_sd;
> > >  	struct v4l2_subdev_state *source_state, *sink_state;
> > >  	bool states_locked;
> > > +	bool is_sink_subdev;
> > > +	bool is_source_subdev;
> > >  	int ret;
> > >  
> > > -	if (!is_media_entity_v4l2_subdev(link->sink->entity) ||
> > > -	    !is_media_entity_v4l2_subdev(link->source->entity)) {
> > > -		pr_warn_once("%s of link '%s':%u->'%s':%u is not a V4L2 sub-device, driver bug!\n",
> > > -			     !is_media_entity_v4l2_subdev(link->sink->entity) ?
> > > -			     "sink" : "source",
> > > -			     link->source->entity->name, link->source->index,
> > > -			     link->sink->entity->name, link->sink->index);
> > > +	is_sink_subdev = is_media_entity_v4l2_subdev(link->sink->entity);
> > > +	is_source_subdev = is_media_entity_v4l2_subdev(link->source->entity);
> > > +
> > > +	if (!is_sink_subdev || !is_source_subdev) {
> > > +		/*
> > > +		 * Do not print the warning if the source is a video device and
> > > +		 * the sink a subdev. This is a valid use case, to allow usage
> > > +		 * of this helper by subdev drivers that have multiple sink
> > > +		 * pads, some connected to video devices and some connected to
> > > +		 * other subdevs. The video device to subdev link is typically
> > > +		 * validated manually by the driver at stream start time in such
> > > +		 * cases.
> > > +		 */
> > > +		if (!is_sink_subdev ||
> > > +		    !is_media_entity_v4l2_video_device(link->source->entity))
> > > +			pr_warn_once("%s of link '%s':%u->'%s':%u is not a V4L2 sub-device, driver bug!\n",
> > > +				     !is_sink_subdev ? "sink" : "source",
> > > +				     link->source->entity->name,
> > > +				     link->source->index,
> > > +				     link->sink->entity->name,
> > > +				     link->sink->index);
> > >  		return 0;
> > >  	}
> > >  
> 

-- 
Regards,

Sakari Ailus

^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2023-11-13 13:53 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-11-13 10:17 [PATCH v1 0/2] media: v4l2-subdev: Fix link validation issue Laurent Pinchart
2023-11-13 10:17 ` [PATCH v1 1/2] media: v4l2-subdev: Drop unreacheable warning Laurent Pinchart
2023-11-13 10:42   ` Sakari Ailus
2023-11-13 10:50     ` Laurent Pinchart
2023-11-13 11:41   ` Tomi Valkeinen
2023-11-13 10:17 ` [PATCH v1 2/2] media: v4l2-subdev: Relax warnings in link validation Laurent Pinchart
2023-11-13 11:06   ` Sakari Ailus
2023-11-13 11:37     ` Laurent Pinchart
2023-11-13 13:53       ` Sakari Ailus
2023-11-13 13:03   ` Tomi Valkeinen

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.