From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-10.0 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH, MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id B2662C433E3 for ; Thu, 13 Aug 2020 13:10:12 +0000 (UTC) Received: from merlin.infradead.org (merlin.infradead.org [205.233.59.134]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 7B88E207DA for ; Thu, 13 Aug 2020 13:10:12 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.b="eAq+Ktv2"; dkim=fail reason="signature verification failed" (1024-bit key) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="cW6rUTgl" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 7B88E207DA Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=ideasonboard.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-rockchip-bounces+linux-rockchip=archiver.kernel.org@lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=merlin.20170209; h=Sender:Content-Transfer-Encoding: Content-Type:Cc:List-Subscribe:List-Help:List-Post:List-Archive: List-Unsubscribe:List-Id:In-Reply-To:MIME-Version:References:Message-ID: Subject:To:From:Date:Reply-To:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=4bEWbtImL6mnF5qojvk26+55Pgnpo46jqNBWzZ7E2nU=; b=eAq+Ktv2X00IvNWiI499eCzf/ o64fY7F8GicQvEZmrdSZZhFKVluAdr/l+Tm+/DENEaorgzZDPsjLDNhzRsmziLV0i9+11UOYIO/aY DzfF5tmNjXZGnz8LwnwL1pRRmULu1C9mdULGUHtXwF20xSBJzZA7EuAKa5SRIybpwiwqordsG2L9R Clc3qL49ymUmno50ss5c/rqdVCQ8tgi8AGqFqt7qelEOOIDbVeYYYVpE6DOm1kTyRGEgPQ408mFt3 kfAaWieEqjXuTeZJUTzS2Ksv4/VPymch9A3lAtEVfaSogK42PbfAZYpkubOUh7fUsaDPuMMVI2R3d 7lXR3mSmw==; Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1k6Czh-0000kQ-JK; Thu, 13 Aug 2020 13:10:09 +0000 Received: from perceval.ideasonboard.com ([213.167.242.64]) by merlin.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1k6Cze-0000jO-P5 for linux-rockchip@lists.infradead.org; Thu, 13 Aug 2020 13:10:07 +0000 Received: from pendragon.ideasonboard.com (62-78-145-57.bb.dnainternet.fi [62.78.145.57]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 3901480C; Thu, 13 Aug 2020 15:10:04 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1597324204; bh=rkyxbulFjuvXkf+OK5ZqXHOYMV3dor0xfrpa2QqmZs8=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=cW6rUTglzMFTMYYANXjcpp5E19NsuA5SrOlDQRpLBurDZLm5Fgrd/lNhCkjjeHWRx SqSvdbef8rdkuPgyjDeBeKs6RKrw6wcD2e/cV/d5m/mmgDr5LQ0UtcLGW8DS2nt2n/ ELktOYXvPSEsW/62bqDrnkMh10vCmeHiYbEfvcUA= Date: Thu, 13 Aug 2020 16:09:50 +0300 From: Laurent Pinchart To: Tomasz Figa Subject: Re: [PATCH 3/3] media: staging: rkisp1: params: in 'stop_streaming' don't release the lock while returning buffers Message-ID: <20200813130950.GI6057@pendragon.ideasonboard.com> References: <16ea1aba-0b0d-6bcf-8e72-5e253ead9218@arm.com> <4d90a5d1-25a7-ab49-f3e5-2d54b6b8ecba@collabora.com> <20200813105314.GA6258@pendragon.ideasonboard.com> <20200813130220.GH6057@pendragon.ideasonboard.com> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20200813_091006_961886_265A3CD0 X-CRM114-Status: GOOD ( 41.77 ) X-BeenThere: linux-rockchip@lists.infradead.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Upstream kernel work for Rockchip platforms List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kernel@collabora.com, Dafna Hirschfeld , Ezequiel Garcia , Dafna Hirschfeld , Hans Verkuil , "open list:ARM/Rockchip SoC..." , Helen Koike , Sakari Ailus , Mauro Carvalho Chehab , Robin Murphy , Linux Media Mailing List Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "Linux-rockchip" Errors-To: linux-rockchip-bounces+linux-rockchip=archiver.kernel.org@lists.infradead.org Hi Tomasz, On Thu, Aug 13, 2020 at 03:05:09PM +0200, Tomasz Figa wrote: > On Thu, Aug 13, 2020 at 3:02 PM Laurent Pinchart wrote: > > On Thu, Aug 13, 2020 at 02:50:17PM +0200, Tomasz Figa wrote: > >> On Thu, Aug 13, 2020 at 12:53 PM Laurent Pinchart wrote: > >>> On Thu, Aug 13, 2020 at 12:44:35PM +0200, Dafna Hirschfeld wrote: > >>>> Am 26.06.20 um 18:58 schrieb Robin Murphy: > >>>>> On 2020-06-26 16:59, Tomasz Figa wrote: > >>>>>> On Fri, Jun 26, 2020 at 5:48 PM Dafna Hirschfeld wrote: > >>>>>>> On 26.06.20 16:03, Tomasz Figa wrote: > >>>>>>>> On Fri, Jun 26, 2020 at 3:32 PM Robin Murphy wrote: > >>>>>>>>> On 2020-06-25 18:42, Dafna Hirschfeld wrote: > >>>>>>>>>> In the stop_streaming callback 'rkisp1_params_vb2_stop_streaming' > >>>>>>>>>> there is no need to release the lock 'config_lock' and then acquire > >>>>>>>>>> it again at each iteration when returning all buffers. > >>>>>>>>>> This is because the stream is about to end and there is no need > >>>>>>>>>> to let the isr access a buffer. > >>>>>>>>>> > >>>>>>>>>> Signed-off-by: Dafna Hirschfeld > >>>>>>>>>> --- > >>>>>>>>>> drivers/staging/media/rkisp1/rkisp1-params.c | 7 +------ > >>>>>>>>>> 1 file changed, 1 insertion(+), 6 deletions(-) > >>>>>>>>>> > >>>>>>>>>> diff --git a/drivers/staging/media/rkisp1/rkisp1-params.c b/drivers/staging/media/rkisp1/rkisp1-params.c > >>>>>>>>>> index bf006dbeee2d..5169b02731f1 100644 > >>>>>>>>>> --- a/drivers/staging/media/rkisp1/rkisp1-params.c > >>>>>>>>>> +++ b/drivers/staging/media/rkisp1/rkisp1-params.c > >>>>>>>>>> @@ -1488,19 +1488,13 @@ static void rkisp1_params_vb2_stop_streaming(struct vb2_queue *vq) > >>>>>>>>>> /* stop params input firstly */ > >>>>>>>>>> spin_lock_irqsave(¶ms->config_lock, flags); > >>>>>>>>>> params->is_streaming = false; > >>>>>>>>>> - spin_unlock_irqrestore(¶ms->config_lock, flags); > >>>>>>>>>> > >>>>>>>>>> for (i = 0; i < RKISP1_ISP_PARAMS_REQ_BUFS_MAX; i++) { > >>>>>>>>>> - spin_lock_irqsave(¶ms->config_lock, flags); > >>>>>>>>>> if (!list_empty(¶ms->params)) { > >>>>>>>>>> buf = list_first_entry(¶ms->params, > >>>>>>>>>> struct rkisp1_buffer, queue); > >>>>>>>>>> list_del(&buf->queue); > >>>>>>>>>> - spin_unlock_irqrestore(¶ms->config_lock, > >>>>>>>>>> - flags); > >>>>>>>>>> } else { > >>>>>>>>>> - spin_unlock_irqrestore(¶ms->config_lock, > >>>>>>>>>> - flags); > >>>>>>>>>> break; > >>>>>>>>>> } > >>>>>>>>> > >>>>>>>>> Just skimming through out of idle curiosity I was going to comment that > >>>>>>>>> if you end up with this pattern: > >>>>>>>>> > >>>>>>>>> if (!x) { > >>>>>>>>> //do stuff > >>>>>>>>> } else { > >>>>>>>>> break; > >>>>>>>>> } > >>>>>>>>> > >>>>>>>>> it would be better as: > >>>>>>>>> > >>>>>>>>> if (x) > >>>>>>>>> break; > >>>>>>>>> //do stuff > >>>>>>>>> > >>>>>>>>> However I then went and looked at the whole function and frankly it's a > >>>>>>>>> bit of a WTF. As best I could decipher, this whole crazy loop appears to > >>>>>>>>> be a baroque reinvention of: > >>>>>>>>> > >>>>>>>>> list_for_each_entry_safe(¶ms->params, ..., buf) { > >>>>>>>>> list_del(&buf->queue); > >>>>>>>>> vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR); > >>>>>>>>> } > >>>>>>> Hi, indeed this is a much simpler implementation, greping 'return_all_buffers' > >>>>>>> I see that many drivers implement it this way. > >>>>>>> thanks! > >>>>>>> > >>>>>>>>> > >>>>>>>>> (assuming from context that the list should never contain more than > >>>>>>>>> RKISP1_ISP_PARAMS_REQ_BUFS_MAX entries in the first place) > >>>>>>>> > >>>>>>>> Or if we want to avoid disabling the interrupts for the whole > >>>>>>>> iteration, we could use list_splice() to move all the entries of > >>>>>>> > >>>>>>> But this code runs when userspace asks to stop the streaming so I don't > >>>>>>> think it is important at that stage to allow the interrupts. > >>>>>> > >>>>>> It's generally a good practice to reduce the time spent with > >>>>>> interrupts disabled. Disabling the interrupts prevents the system from > >>>>>> handling external events, including timer interrupts, and scheduling > >>>>>> higher priority tasks, including real time ones. How much the system > >>>>>> runs with interrupts disabled is one of the factors determining the > >>>>>> general system latency. > >>>>> > >>>>> Right, with the way we handle interrupt affinity on Arm an IRQ can't > >>>>> target multiple CPUs in hardware, so any time spent with IRQs > >>>>> disabled might be preventing other devices' interrupts from being > >>>>> taken even if they're not explicitly affine to the current CPU. > >>>>> > >>>>> Now that I've looked, it appears that vb2_buffer_done() might end up > >>>>> performing a DMA sync on the buffers, which, if it has to do > >>>>> order-of-megabytes worth of cache maintenance for large frames, is > >>>>> the kind of relatively slow operation that really doesn't want to be > >>>>> done with IRQs disabled (or under a lock at all, ideally) unless it > >>>>> absolutely *has* to be. If the lock is only needed here to protect > >>>>> modifications to the params list itself, then moving the whole list > >>>>> at once to do the cleanup "offline" sounds like a great idea to me. > >>> > >>> Ouch. > >>> > >>>> ok, that might be a problem in v4l2 in general since vb2_buffer_done > >>>> is actually often used inside an irq handler > >>> > >>> Correct. The DMA sync should be moved to DQBUF time, there shouldn't be > >>> any reason to do it in the IRQ handler. I thought this had already been > >>> fixed :-( > >> > >> For reference, there was a patch [1] proposed, but it moved the > >> synchronization to a wrong place in the sequence, already after the > >> .buf_finish queue callback, ending up breaking the drivers which need > >> to access the buffer contents there. > >> > >> [1] https://patchwork.linuxtv.org/project/linux-media/patch/1494255810-12672-4-git-send-email-sakari.ailus@linux.intel.com/ > > > > I think we need to fix the drivers. We just can't do cache sync in IRQ > > context by default because a few drivers need to access the buffer > > contents. Those drivers should instead deffer access to a work queue, > > and sync explicitly. We could possibly provide helpers for that, making > > it transparent if a queue flag is set. > > The drivers don't access the buffers explicitly from the IRQ. The vb2 > queue .buf_finish callback is called at DQBUF time. It was just the > patch mentioned that moved it to a part of DQBUF executed too late. Aahhh it's better than I thought :-) Shouldn't be too hard to fix then. Thanks for refreshing my memory. -- Regards, Laurent Pinchart _______________________________________________ Linux-rockchip mailing list Linux-rockchip@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-rockchip