From patchwork Tue Jul 8 09:41:13 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Molton X-Patchwork-Id: 4503431 Return-Path: X-Original-To: patchwork-linux-sh@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork2.web.kernel.org (Postfix) with ESMTP id 2971EBEEAA for ; Tue, 8 Jul 2014 09:41:43 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 4AEFA20170 for ; Tue, 8 Jul 2014 09:41:42 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id C40CB202AE for ; Tue, 8 Jul 2014 09:41:37 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753893AbaGHJlg (ORCPT ); Tue, 8 Jul 2014 05:41:36 -0400 Received: from ducie-dc1.codethink.co.uk ([185.25.241.215]:37465 "EHLO ducie-dc1.codethink.co.uk" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S1753825AbaGHJlg (ORCPT ); Tue, 8 Jul 2014 05:41:36 -0400 Received: from localhost (localhost [127.0.0.1]) by ducie-dc1.codethink.co.uk (Postfix) with ESMTP id CED54470398; Tue, 8 Jul 2014 10:41:34 +0100 (BST) X-Virus-Scanned: Debian amavisd-new at ducie-dc1.codethink.co.uk Received: from ducie-dc1.codethink.co.uk ([127.0.0.1]) by localhost (ducie-dc1.codethink.co.uk [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id lm64-gzIi78s; Tue, 8 Jul 2014 10:41:29 +0100 (BST) Received: from snark.dyn.ducie.codethink.co.uk (snark.dyn.ducie.codethink.co.uk [10.24.1.196]) by ducie-dc1.codethink.co.uk (Postfix) with ESMTPS id 248EE46E7E3; Tue, 8 Jul 2014 10:41:19 +0100 (BST) Received: from ian by snark.dyn.ducie.codethink.co.uk with local (Exim 4.82) (envelope-from ) id 1X4Rtl-000202-5M; Tue, 08 Jul 2014 10:41:17 +0100 From: Ian Molton To: linux-media@vger.kernel.org Cc: linux-kernel@lists.codethink.co.uk, ian.molton@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: [PATCH 3/4] media: rcar_vin: Fix race condition terminating stream Date: Tue, 8 Jul 2014 10:41:13 +0100 Message-Id: <1404812474-7627-4-git-send-email-ian.molton@codethink.co.uk> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1404812474-7627-1-git-send-email-ian.molton@codethink.co.uk> References: <1404812474-7627-1-git-send-email-ian.molton@codethink.co.uk> Sender: linux-sh-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-sh@vger.kernel.org X-Spam-Status: No, score=-7.6 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=ham version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP 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 Signed-off-by: William Towle --- 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); + /* * 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); }