From: Hans Verkuil <hverkuil@xs4all.nl>
To: Ian Molton <ian.molton@codethink.co.uk>, linux-media@vger.kernel.org
Cc: linux-kernel@lists.codethink.co.uk, g.liakhovetski@gmx.de,
m.chehab@samsung.com, vladimir.barinov@cogentembedded.com,
magnus.damm@gmail.com, horms@verge.net.au,
linux-sh@vger.kernel.org
Subject: Re: [PATCH 3/4] media: rcar_vin: Fix race condition terminating stream
Date: Mon, 04 Aug 2014 21:27:24 +0000 [thread overview]
Message-ID: <53DFFABC.9000800@xs4all.nl> (raw)
In-Reply-To: <1404812474-7627-4-git-send-email-ian.molton@codethink.co.uk>
On 07/08/2014 11:41 AM, Ian Molton wrote:
> This patch fixes a race condition whereby a frame being captured may generate an
> interrupt between requesting capture to halt and freeing buffers.
>
> This condition is exposed by the earlier patch that explicitly calls
> vb2_buffer_done() during stop streaming.
>
> The solution is to wait for capture to finish prior to finalising these buffers.
>
> Signed-off-by: Ian Molton <ian.molton@codethink.co.uk>
> Signed-off-by: William Towle <william.towle@codethink.co.uk>
> ---
> drivers/media/platform/soc_camera/rcar_vin.c | 43 ++++++++++++++++++----------
> 1 file changed, 28 insertions(+), 15 deletions(-)
>
> diff --git a/drivers/media/platform/soc_camera/rcar_vin.c b/drivers/media/platform/soc_camera/rcar_vin.c
> index 06ce705..aeda4e2 100644
> --- a/drivers/media/platform/soc_camera/rcar_vin.c
> +++ b/drivers/media/platform/soc_camera/rcar_vin.c
> @@ -455,6 +455,29 @@ error:
> vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
> }
>
> +/*
> + * Wait for capture to stop and all in-flight buffers to be finished with by
> + * the video hardware. This must be called under &priv->lock
> + *
> + */
> +static void rcar_vin_wait_stop_streaming(struct rcar_vin_priv *priv)
> +{
> + while (priv->state != STOPPED) {
> +
> + /* issue stop if running */
> + if (priv->state = RUNNING)
> + rcar_vin_request_capture_stop(priv);
> +
> + /* wait until capturing has been stopped */
> + if (priv->state = STOPPING) {
> + priv->request_to_stop = true;
> + spin_unlock_irq(&priv->lock);
> + wait_for_completion(&priv->capture_stop);
> + spin_lock_irq(&priv->lock);
> + }
> + }
> +}
> +
> static void rcar_vin_videobuf_release(struct vb2_buffer *vb)
> {
> struct soc_camera_device *icd = soc_camera_from_vb2q(vb->vb2_queue);
> @@ -462,7 +485,6 @@ static void rcar_vin_videobuf_release(struct vb2_buffer *vb)
> struct rcar_vin_priv *priv = ici->priv;
> unsigned int i;
> int buf_in_use = 0;
> -
> spin_lock_irq(&priv->lock);
>
> /* Is the buffer in use by the VIN hardware? */
> @@ -474,20 +496,8 @@ static void rcar_vin_videobuf_release(struct vb2_buffer *vb)
> }
>
> if (buf_in_use) {
> - while (priv->state != STOPPED) {
> -
> - /* issue stop if running */
> - if (priv->state = RUNNING)
> - rcar_vin_request_capture_stop(priv);
> -
> - /* wait until capturing has been stopped */
> - if (priv->state = STOPPING) {
> - priv->request_to_stop = true;
> - spin_unlock_irq(&priv->lock);
> - wait_for_completion(&priv->capture_stop);
> - spin_lock_irq(&priv->lock);
> - }
> - }
> + rcar_vin_wait_stop_streaming(priv);
> +
Why on earth would videobuf_release call stop_streaming()?
You start streaming in the start_streaming op, not in the buf_queue op. If you
need a certain minimum of buffers before start_streaming can be called, then just
set min_buffers_needed in struct vb2_queue.
And stop streaming happens in stop_streaming. The various vb2 queue ops should just
do what the op name says. That way everything works nicely together and it makes
your driver much easier to understand.
Sorry I am late in reviewing this, but I only now stumbled on these patches.
Regards,
Hans
> /*
> * Capturing has now stopped. The buffer we have been asked
> * to release could be any of the current buffers in use, so
> @@ -517,12 +527,15 @@ static void rcar_vin_stop_streaming(struct vb2_queue *vq)
>
> spin_lock_irq(&priv->lock);
>
> + rcar_vin_wait_stop_streaming(priv);
> +
> for (i = 0; i < vq->num_buffers; ++i)
> if (vq->bufs[i]->state = VB2_BUF_STATE_ACTIVE)
> vb2_buffer_done(vq->bufs[i], VB2_BUF_STATE_ERROR);
>
> list_for_each_safe(buf_head, tmp, &priv->capture)
> list_del_init(buf_head);
> +
> spin_unlock_irq(&priv->lock);
> }
>
>
WARNING: multiple messages have this Message-ID (diff)
From: Hans Verkuil <hverkuil@xs4all.nl>
To: Ian Molton <ian.molton@codethink.co.uk>, linux-media@vger.kernel.org
Cc: linux-kernel@lists.codethink.co.uk, g.liakhovetski@gmx.de,
m.chehab@samsung.com, vladimir.barinov@cogentembedded.com,
magnus.damm@gmail.com, horms@verge.net.au,
linux-sh@vger.kernel.org
Subject: Re: [PATCH 3/4] media: rcar_vin: Fix race condition terminating stream
Date: Mon, 04 Aug 2014 23:27:24 +0200 [thread overview]
Message-ID: <53DFFABC.9000800@xs4all.nl> (raw)
In-Reply-To: <1404812474-7627-4-git-send-email-ian.molton@codethink.co.uk>
On 07/08/2014 11:41 AM, Ian Molton wrote:
> This patch fixes a race condition whereby a frame being captured may generate an
> interrupt between requesting capture to halt and freeing buffers.
>
> This condition is exposed by the earlier patch that explicitly calls
> vb2_buffer_done() during stop streaming.
>
> The solution is to wait for capture to finish prior to finalising these buffers.
>
> Signed-off-by: Ian Molton <ian.molton@codethink.co.uk>
> Signed-off-by: William Towle <william.towle@codethink.co.uk>
> ---
> drivers/media/platform/soc_camera/rcar_vin.c | 43 ++++++++++++++++++----------
> 1 file changed, 28 insertions(+), 15 deletions(-)
>
> diff --git a/drivers/media/platform/soc_camera/rcar_vin.c b/drivers/media/platform/soc_camera/rcar_vin.c
> index 06ce705..aeda4e2 100644
> --- a/drivers/media/platform/soc_camera/rcar_vin.c
> +++ b/drivers/media/platform/soc_camera/rcar_vin.c
> @@ -455,6 +455,29 @@ error:
> vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
> }
>
> +/*
> + * Wait for capture to stop and all in-flight buffers to be finished with by
> + * the video hardware. This must be called under &priv->lock
> + *
> + */
> +static void rcar_vin_wait_stop_streaming(struct rcar_vin_priv *priv)
> +{
> + while (priv->state != STOPPED) {
> +
> + /* issue stop if running */
> + if (priv->state == RUNNING)
> + rcar_vin_request_capture_stop(priv);
> +
> + /* wait until capturing has been stopped */
> + if (priv->state == STOPPING) {
> + priv->request_to_stop = true;
> + spin_unlock_irq(&priv->lock);
> + wait_for_completion(&priv->capture_stop);
> + spin_lock_irq(&priv->lock);
> + }
> + }
> +}
> +
> static void rcar_vin_videobuf_release(struct vb2_buffer *vb)
> {
> struct soc_camera_device *icd = soc_camera_from_vb2q(vb->vb2_queue);
> @@ -462,7 +485,6 @@ static void rcar_vin_videobuf_release(struct vb2_buffer *vb)
> struct rcar_vin_priv *priv = ici->priv;
> unsigned int i;
> int buf_in_use = 0;
> -
> spin_lock_irq(&priv->lock);
>
> /* Is the buffer in use by the VIN hardware? */
> @@ -474,20 +496,8 @@ static void rcar_vin_videobuf_release(struct vb2_buffer *vb)
> }
>
> if (buf_in_use) {
> - while (priv->state != STOPPED) {
> -
> - /* issue stop if running */
> - if (priv->state == RUNNING)
> - rcar_vin_request_capture_stop(priv);
> -
> - /* wait until capturing has been stopped */
> - if (priv->state == STOPPING) {
> - priv->request_to_stop = true;
> - spin_unlock_irq(&priv->lock);
> - wait_for_completion(&priv->capture_stop);
> - spin_lock_irq(&priv->lock);
> - }
> - }
> + rcar_vin_wait_stop_streaming(priv);
> +
Why on earth would videobuf_release call stop_streaming()?
You start streaming in the start_streaming op, not in the buf_queue op. If you
need a certain minimum of buffers before start_streaming can be called, then just
set min_buffers_needed in struct vb2_queue.
And stop streaming happens in stop_streaming. The various vb2 queue ops should just
do what the op name says. That way everything works nicely together and it makes
your driver much easier to understand.
Sorry I am late in reviewing this, but I only now stumbled on these patches.
Regards,
Hans
> /*
> * Capturing has now stopped. The buffer we have been asked
> * to release could be any of the current buffers in use, so
> @@ -517,12 +527,15 @@ static void rcar_vin_stop_streaming(struct vb2_queue *vq)
>
> spin_lock_irq(&priv->lock);
>
> + rcar_vin_wait_stop_streaming(priv);
> +
> for (i = 0; i < vq->num_buffers; ++i)
> if (vq->bufs[i]->state == VB2_BUF_STATE_ACTIVE)
> vb2_buffer_done(vq->bufs[i], VB2_BUF_STATE_ERROR);
>
> list_for_each_safe(buf_head, tmp, &priv->capture)
> list_del_init(buf_head);
> +
> spin_unlock_irq(&priv->lock);
> }
>
>
next prev parent reply other threads:[~2014-08-04 21:27 UTC|newest]
Thread overview: 39+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-07-08 9:41 Resend: [PATCH 0/4] rcar_vin: fix soc_camera WARN_ON() issues Ian Molton
2014-07-08 9:41 ` Ian Molton
2014-07-08 9:41 ` [PATCH 1/4] media: rcar_vin: Dont aggressively retire buffers Ian Molton
2014-07-08 9:41 ` Ian Molton
2014-07-08 9:41 ` [PATCH 2/4] media: rcar_vin: Ensure all in-flight buffers are returned to error state before stoppin Ian Molton
2014-07-08 9:41 ` [PATCH 2/4] media: rcar_vin: Ensure all in-flight buffers are returned to error state before stopping Ian Molton
2014-08-04 20:39 ` [PATCH 2/4] media: rcar_vin: Ensure all in-flight buffers are returned to error state before sto Sergei Shtylyov
2014-08-04 20:39 ` [PATCH 2/4] media: rcar_vin: Ensure all in-flight buffers are returned to error state before stopping Sergei Shtylyov
2014-08-13 17:30 ` [PATCH 2/4] media: rcar_vin: Ensure all in-flight buffers are returned to error state before sto Ian Molton
2014-08-13 17:30 ` [PATCH 2/4] media: rcar_vin: Ensure all in-flight buffers are returned to error state before stopping Ian Molton
2014-08-13 17:35 ` [PATCH 2/4] media: rcar_vin: Ensure all in-flight buffers are returned to error state before sto Sergei Shtylyov
2014-08-13 17:35 ` [PATCH 2/4] media: rcar_vin: Ensure all in-flight buffers are returned to error state before stopping Sergei Shtylyov
2014-09-20 6:59 ` [PATCH 2/4] media: rcar_vin: Ensure all in-flight buffers are returned to error state before sto Guennadi Liakhovetski
2014-09-20 6:59 ` [PATCH 2/4] media: rcar_vin: Ensure all in-flight buffers are returned to error state before stopping Guennadi Liakhovetski
2014-08-04 21:22 ` [PATCH 2/4] media: rcar_vin: Ensure all in-flight buffers are returned to error state before sto Hans Verkuil
2014-08-04 21:22 ` [PATCH 2/4] media: rcar_vin: Ensure all in-flight buffers are returned to error state before stopping Hans Verkuil
2015-01-18 20:32 ` [PATCH 2/4] media: rcar_vin: Ensure all in-flight buffers are returned to error state before sto Guennadi Liakhovetski
2015-01-18 20:32 ` [PATCH 2/4] media: rcar_vin: Ensure all in-flight buffers are returned to error state before stopping Guennadi Liakhovetski
2014-07-08 9:41 ` [PATCH 3/4] media: rcar_vin: Fix race condition terminating stream Ian Molton
2014-07-08 9:41 ` Ian Molton
2014-07-08 16:09 ` Sergei Shtylyov
2014-07-08 16:09 ` Sergei Shtylyov
2014-07-09 8:16 ` Simon Horman
2014-07-09 8:16 ` Simon Horman
2014-07-10 10:15 ` Ian Molton
2014-07-10 10:15 ` Ian Molton
2014-08-01 22:16 ` Sergei Shtylyov
2014-08-01 22:16 ` Sergei Shtylyov
2014-08-04 21:27 ` Hans Verkuil [this message]
2014-08-04 21:27 ` Hans Verkuil
2014-08-11 11:23 ` Ian Molton
2014-08-11 11:23 ` Ian Molton
2014-08-11 12:14 ` Hans Verkuil
2014-08-11 12:14 ` Hans Verkuil
2014-07-08 9:41 ` [PATCH 4/4] media: rcar_vin: Clean up rcar_vin_irq Ian Molton
2014-07-08 9:41 ` Ian Molton
2014-08-04 20:00 ` Resend: [PATCH 0/4] rcar_vin: fix soc_camera WARN_ON() issues Sergei Shtylyov
2014-08-04 20:00 ` Sergei Shtylyov
-- strict thread matches above, loose matches on Subject: below --
2014-07-07 16:37 Ian Molton
2014-07-07 16:37 ` [PATCH 3/4] media: rcar_vin: Fix race condition terminating stream Ian Molton
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=53DFFABC.9000800@xs4all.nl \
--to=hverkuil@xs4all.nl \
--cc=g.liakhovetski@gmx.de \
--cc=horms@verge.net.au \
--cc=ian.molton@codethink.co.uk \
--cc=linux-kernel@lists.codethink.co.uk \
--cc=linux-media@vger.kernel.org \
--cc=linux-sh@vger.kernel.org \
--cc=m.chehab@samsung.com \
--cc=magnus.damm@gmail.com \
--cc=vladimir.barinov@cogentembedded.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.