* [PATCH] media: nxp: imx8-isi: add m2m usage_count check in streamoff
@ 2024-09-20 14:27 Laurentiu Palcu
2024-09-23 17:50 ` Jacopo Mondi
0 siblings, 1 reply; 4+ messages in thread
From: Laurentiu Palcu @ 2024-09-20 14:27 UTC (permalink / raw)
To: Laurent Pinchart, Mauro Carvalho Chehab, Shawn Guo, Sascha Hauer,
Pengutronix Kernel Team, Fabio Estevam, Guoniu Zhou, Jacopo Mondi,
Christian Hemp
Cc: Laurentiu Palcu, Stefan Riedmueller, linux-media, imx,
linux-arm-kernel, linux-kernel
Currently, if streamon/streamoff calls are imbalanced we can end up
with a negative ISI m2m usage_count. When that happens, the next
streamon call will not enable the ISI m2m channel.
So, instead of throwing a warning in streamoff when usage_count drops
below 0, just make sure we don't get there.
Fixes: cf21f328fcafac ("media: nxp: Add i.MX8 ISI driver")
Signed-off-by: Laurentiu Palcu <laurentiu.palcu@oss.nxp.com>
---
drivers/media/platform/nxp/imx8-isi/imx8-isi-m2m.c | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)
diff --git a/drivers/media/platform/nxp/imx8-isi/imx8-isi-m2m.c b/drivers/media/platform/nxp/imx8-isi/imx8-isi-m2m.c
index 9745d6219a166..b71195a3ba256 100644
--- a/drivers/media/platform/nxp/imx8-isi/imx8-isi-m2m.c
+++ b/drivers/media/platform/nxp/imx8-isi/imx8-isi-m2m.c
@@ -575,6 +575,9 @@ static int mxc_isi_m2m_streamoff(struct file *file, void *fh,
mutex_lock(&m2m->lock);
+ if (m2m->usage_count == 0)
+ goto unlock;
+
/*
* If the last context is this one, reset it to make sure the device
* will be reconfigured when streaming is restarted.
@@ -594,8 +597,7 @@ static int mxc_isi_m2m_streamoff(struct file *file, void *fh,
mxc_isi_channel_release(m2m->pipe);
}
- WARN_ON(m2m->usage_count < 0);
-
+unlock:
mutex_unlock(&m2m->lock);
return 0;
--
2.34.1
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH] media: nxp: imx8-isi: add m2m usage_count check in streamoff
2024-09-20 14:27 [PATCH] media: nxp: imx8-isi: add m2m usage_count check in streamoff Laurentiu Palcu
@ 2024-09-23 17:50 ` Jacopo Mondi
2024-09-23 21:52 ` Laurent Pinchart
0 siblings, 1 reply; 4+ messages in thread
From: Jacopo Mondi @ 2024-09-23 17:50 UTC (permalink / raw)
To: Laurentiu Palcu
Cc: Laurent Pinchart, Mauro Carvalho Chehab, Shawn Guo, Sascha Hauer,
Pengutronix Kernel Team, Fabio Estevam, Guoniu Zhou, Jacopo Mondi,
Christian Hemp, Stefan Riedmueller, linux-media, imx,
linux-arm-kernel, linux-kernel
Hi Laurentiu
On Fri, Sep 20, 2024 at 05:27:15PM GMT, Laurentiu Palcu wrote:
> Currently, if streamon/streamoff calls are imbalanced we can end up
> with a negative ISI m2m usage_count. When that happens, the next
> streamon call will not enable the ISI m2m channel.
>
> So, instead of throwing a warning in streamoff when usage_count drops
> below 0, just make sure we don't get there.
Isn't the whole purpose of the WARN() to highlight something's wrong
with userspace ? I think it's expected to have the same number of
streamon and streamoff calls, do you have any idea why it might not be
happening ?
Thanks
j
>
> Fixes: cf21f328fcafac ("media: nxp: Add i.MX8 ISI driver")
> Signed-off-by: Laurentiu Palcu <laurentiu.palcu@oss.nxp.com>
> ---
> drivers/media/platform/nxp/imx8-isi/imx8-isi-m2m.c | 6 ++++--
> 1 file changed, 4 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/media/platform/nxp/imx8-isi/imx8-isi-m2m.c b/drivers/media/platform/nxp/imx8-isi/imx8-isi-m2m.c
> index 9745d6219a166..b71195a3ba256 100644
> --- a/drivers/media/platform/nxp/imx8-isi/imx8-isi-m2m.c
> +++ b/drivers/media/platform/nxp/imx8-isi/imx8-isi-m2m.c
> @@ -575,6 +575,9 @@ static int mxc_isi_m2m_streamoff(struct file *file, void *fh,
>
> mutex_lock(&m2m->lock);
>
> + if (m2m->usage_count == 0)
> + goto unlock;
> +
> /*
> * If the last context is this one, reset it to make sure the device
> * will be reconfigured when streaming is restarted.
> @@ -594,8 +597,7 @@ static int mxc_isi_m2m_streamoff(struct file *file, void *fh,
> mxc_isi_channel_release(m2m->pipe);
> }
>
> - WARN_ON(m2m->usage_count < 0);
> -
> +unlock:
> mutex_unlock(&m2m->lock);
>
> return 0;
> --
> 2.34.1
>
>
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] media: nxp: imx8-isi: add m2m usage_count check in streamoff
2024-09-23 17:50 ` Jacopo Mondi
@ 2024-09-23 21:52 ` Laurent Pinchart
2024-09-24 7:41 ` Laurentiu Palcu
0 siblings, 1 reply; 4+ messages in thread
From: Laurent Pinchart @ 2024-09-23 21:52 UTC (permalink / raw)
To: Jacopo Mondi
Cc: Laurentiu Palcu, Mauro Carvalho Chehab, Shawn Guo, Sascha Hauer,
Pengutronix Kernel Team, Fabio Estevam, Guoniu Zhou, Jacopo Mondi,
Christian Hemp, Stefan Riedmueller, linux-media, imx,
linux-arm-kernel, linux-kernel
On Mon, Sep 23, 2024 at 07:50:18PM +0200, Jacopo Mondi wrote:
> On Fri, Sep 20, 2024 at 05:27:15PM GMT, Laurentiu Palcu wrote:
> > Currently, if streamon/streamoff calls are imbalanced we can end up
> > with a negative ISI m2m usage_count. When that happens, the next
> > streamon call will not enable the ISI m2m channel.
> >
> > So, instead of throwing a warning in streamoff when usage_count drops
> > below 0, just make sure we don't get there.
>
> Isn't the whole purpose of the WARN() to highlight something's wrong
> with userspace ? I think it's expected to have the same number of
> streamon and streamoff calls, do you have any idea why it might not be
> happening ?
WARN() is much too strong to report userspace problems.
> > Fixes: cf21f328fcafac ("media: nxp: Add i.MX8 ISI driver")
> > Signed-off-by: Laurentiu Palcu <laurentiu.palcu@oss.nxp.com>
> > ---
> > drivers/media/platform/nxp/imx8-isi/imx8-isi-m2m.c | 6 ++++--
> > 1 file changed, 4 insertions(+), 2 deletions(-)
> >
> > diff --git a/drivers/media/platform/nxp/imx8-isi/imx8-isi-m2m.c b/drivers/media/platform/nxp/imx8-isi/imx8-isi-m2m.c
> > index 9745d6219a166..b71195a3ba256 100644
> > --- a/drivers/media/platform/nxp/imx8-isi/imx8-isi-m2m.c
> > +++ b/drivers/media/platform/nxp/imx8-isi/imx8-isi-m2m.c
> > @@ -575,6 +575,9 @@ static int mxc_isi_m2m_streamoff(struct file *file, void *fh,
> >
> > mutex_lock(&m2m->lock);
> >
> > + if (m2m->usage_count == 0)
> > + goto unlock;
This isn't right. Userspace can still call VIDIOC_STREAMOFF while not
streaming, which will decrement the usage count when it shouldn't. The
right fix is to return from mxc_isi_m2m_streamoff() without decrementing
the usage count if the particular context the function is called for is
not streaming. You will possibly need to also modify
mxc_isi_m2m_streamon() to make sure the two functions won't race each
other.
Could you work on an improved patch ? Please let me know if you need
help, it's important to fix this issue.
> > +
> > /*
> > * If the last context is this one, reset it to make sure the device
> > * will be reconfigured when streaming is restarted.
> > @@ -594,8 +597,7 @@ static int mxc_isi_m2m_streamoff(struct file *file, void *fh,
> > mxc_isi_channel_release(m2m->pipe);
> > }
> >
> > - WARN_ON(m2m->usage_count < 0);
> > -
> > +unlock:
> > mutex_unlock(&m2m->lock);
> >
> > return 0;
--
Regards,
Laurent Pinchart
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] media: nxp: imx8-isi: add m2m usage_count check in streamoff
2024-09-23 21:52 ` Laurent Pinchart
@ 2024-09-24 7:41 ` Laurentiu Palcu
0 siblings, 0 replies; 4+ messages in thread
From: Laurentiu Palcu @ 2024-09-24 7:41 UTC (permalink / raw)
To: Laurent Pinchart
Cc: Jacopo Mondi, Mauro Carvalho Chehab, Shawn Guo, Sascha Hauer,
Pengutronix Kernel Team, Fabio Estevam, Guoniu Zhou, Jacopo Mondi,
Christian Hemp, Stefan Riedmueller, linux-media, imx,
linux-arm-kernel, linux-kernel
Hi Laurent,
On Tue, Sep 24, 2024 at 12:52:46AM +0300, Laurent Pinchart wrote:
> On Mon, Sep 23, 2024 at 07:50:18PM +0200, Jacopo Mondi wrote:
> > On Fri, Sep 20, 2024 at 05:27:15PM GMT, Laurentiu Palcu wrote:
> > > Currently, if streamon/streamoff calls are imbalanced we can end up
> > > with a negative ISI m2m usage_count. When that happens, the next
> > > streamon call will not enable the ISI m2m channel.
> > >
> > > So, instead of throwing a warning in streamoff when usage_count drops
> > > below 0, just make sure we don't get there.
> >
> > Isn't the whole purpose of the WARN() to highlight something's wrong
> > with userspace ? I think it's expected to have the same number of
> > streamon and streamoff calls, do you have any idea why it might not be
> > happening ?
>
> WARN() is much too strong to report userspace problems.
>
> > > Fixes: cf21f328fcafac ("media: nxp: Add i.MX8 ISI driver")
> > > Signed-off-by: Laurentiu Palcu <laurentiu.palcu@oss.nxp.com>
> > > ---
> > > drivers/media/platform/nxp/imx8-isi/imx8-isi-m2m.c | 6 ++++--
> > > 1 file changed, 4 insertions(+), 2 deletions(-)
> > >
> > > diff --git a/drivers/media/platform/nxp/imx8-isi/imx8-isi-m2m.c b/drivers/media/platform/nxp/imx8-isi/imx8-isi-m2m.c
> > > index 9745d6219a166..b71195a3ba256 100644
> > > --- a/drivers/media/platform/nxp/imx8-isi/imx8-isi-m2m.c
> > > +++ b/drivers/media/platform/nxp/imx8-isi/imx8-isi-m2m.c
> > > @@ -575,6 +575,9 @@ static int mxc_isi_m2m_streamoff(struct file *file, void *fh,
> > >
> > > mutex_lock(&m2m->lock);
> > >
> > > + if (m2m->usage_count == 0)
> > > + goto unlock;
>
> This isn't right. Userspace can still call VIDIOC_STREAMOFF while not
> streaming, which will decrement the usage count when it shouldn't.
Hmm, I didn't consider that... :/
> The right fix is to return from mxc_isi_m2m_streamoff() without
> decrementing the usage count if the particular context the function is
> called for is not streaming. You will possibly need to also modify
> mxc_isi_m2m_streamon() to make sure the two functions won't race each
> other.
>
> Could you work on an improved patch ?
I'll give it a shot.
> Please let me know if you need help, it's important to fix this issue.
Thanks, I'll ping you on IRC if I get stuck.
Laurentiu
>
> > > +
> > > /*
> > > * If the last context is this one, reset it to make sure the device
> > > * will be reconfigured when streaming is restarted.
> > > @@ -594,8 +597,7 @@ static int mxc_isi_m2m_streamoff(struct file *file, void *fh,
> > > mxc_isi_channel_release(m2m->pipe);
> > > }
> > >
> > > - WARN_ON(m2m->usage_count < 0);
> > > -
> > > +unlock:
> > > mutex_unlock(&m2m->lock);
> > >
> > > return 0;
>
> --
> Regards,
>
> Laurent Pinchart
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2024-09-24 7:41 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-09-20 14:27 [PATCH] media: nxp: imx8-isi: add m2m usage_count check in streamoff Laurentiu Palcu
2024-09-23 17:50 ` Jacopo Mondi
2024-09-23 21:52 ` Laurent Pinchart
2024-09-24 7:41 ` Laurentiu Palcu
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).