Received: by 2002:a05:7412:1e0b:b0:fc:a2b0:25d7 with SMTP id kr11csp320709rdb; Thu, 15 Feb 2024 00:42:40 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCUSQp90lIluPmptQU7nBnBWUp8TOQm9BraRgX8VstXbCXWNtw/Um5G/tKTQ0hiMcgoZKaRR3Uj4rMHEpyAgULKx+PxU7LHiyrjwLA7VbQ== X-Google-Smtp-Source: AGHT+IGp6cHceLpf1q6Qk30tFVrvktfGQgeLucP2i1DQwSyi+lfCXGPYFlOhMdxp9PX0V4pKsvYx X-Received: by 2002:a05:622a:148f:b0:42d:aad5:a450 with SMTP id t15-20020a05622a148f00b0042daad5a450mr1455477qtx.16.1707986560501; Thu, 15 Feb 2024 00:42:40 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707986560; cv=pass; d=google.com; s=arc-20160816; b=Cxm6vU4oRxdAOY8bX4uBZgaLMzQlALdEjhzfpZevVw7HqxtmsdrPlQHSDfLnlkjHDM PbXadal7xxBZTTJX+epyw0QeygWYyNKJy+bKWXsq0ZsXdsSH2O2gSGcdpBM/Oj+iniuU jmVV7w+qE3bWOiLekPTdjs7/CIT631NohbUGVYlOZylt466loPY79dDhssa/VWDx/ZFi cylmKnnW0tcEjMeyIA8XSxtf/6tPmbudNfODOJAKlj1ZqJ0G8C5UQALGLIGrY/Sst//h XNwgjNOZx6os17Q4OWxATWkDq7y7ZuI53LjOIj4aoPwNLuNqCJat7/kG7tqji4c8M7fj 6qLA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:date:message-id; bh=SQXNeTES6MdpYv/Rayopt6I0OkcMsSIx0hJRyB77EVQ=; fh=iJ6IqYoehqT1hHTj2cTTL420ePt3/oV12of2mhElZ2Q=; b=a4JcrT6QefcMEff2AYUfGajKWl2mnreWkCtRuvC/if/4lXpvjMkw7yMNaDzIDWvS9/ mIbvazrGxy+X+quM0XS1nQ0KhXVZt7FfPSTctGxWNLcfWgUUG2FBQW+kGGRHPGx3I8pb eB4s07QCqGCkBRCWqx9TA8O6ejGctJkrtn0xhVxqYcJpYFdLZuEJnhmaUrpi56eE0AbI qzXzCAmqFIv4t3TUx68MEnnWA1V7SFu7dvaLTnskeVIKuyqAsJKTzBFSdt2SqBFn7JBZ gC5AnDspXY5pUweTpZl4NoOrrjYSg3/BXsLNDtVReDwloxPBQWgKAODf/eZx5iJsv7eE O9Dw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1); spf=pass (google.com: domain of linux-kernel+bounces-66437-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-66437-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=xs4all.nl Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id 10-20020ac8564a000000b0042c410d4447si869532qtt.312.2024.02.15.00.42.40 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 15 Feb 2024 00:42:40 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-66437-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; arc=pass (i=1); spf=pass (google.com: domain of linux-kernel+bounces-66437-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-66437-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=xs4all.nl Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id E76A61C2231E for ; Thu, 15 Feb 2024 08:42:06 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id F11C413AC9; Thu, 15 Feb 2024 08:41:56 +0000 (UTC) Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 74A38134AE; Thu, 15 Feb 2024 08:41:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707986516; cv=none; b=IsJTngyAQF4MMmrb+sAX330cnmzk6zRAXFcgnIc990pp1w028OaX9NpkLGZpRD74kEp6dEQWlRTtUNlOIM5ODz45/JtcwNFkGZzXGkQSdJHf6SGWTovxxMWk4mE4y9oSlOav1kWkhLx0e6h6smCIGTL4pQXdgEl6ad+jaft7gMM= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707986516; c=relaxed/simple; bh=PcyfrcT0IrCS8x6Y/PVw4INOqDeIejn279MqRbP73tM=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=GrrmWbf/ETatMxXkbHSMHIzQ0kMLUytfsahg4KC44zrAa1xtZna5SvF6Rl1IxA7DNl1AFxleWCEeit8bJV5wOQ1c5WAPuRm7FS/bCn38jp6DUlOMAHFrOvuNyxVD3hI4HqrnRS6Q95gx4DlqDCOavoIV4nIaRQx5uCkjwm+4L5A= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 7F2E5C433C7; Thu, 15 Feb 2024 08:41:54 +0000 (UTC) Message-ID: <27ef9490-a56b-46bf-84bd-bc2ec08896af@xs4all.nl> Date: Thu, 15 Feb 2024 09:41:53 +0100 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH] media: v4l2-mem2mem: fix mem order in last buf Content-Language: en-US, nl To: Randy Li , Nicolas Dufresne Cc: mchehab@kernel.org, Hsia-Jun Li , sebastian.fricke@collabora.com, alexious@zju.edu.cn, linux-media@vger.kernel.org, linux-kernel@vger.kernel.org References: <20240210180414.49184-1-randy.li@synaptics.com> <1f80b5ea-1209-438f-b07f-3a4a308ee35d@soulik.info> From: Hans Verkuil In-Reply-To: <1f80b5ea-1209-438f-b07f-3a4a308ee35d@soulik.info> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit On 15/02/2024 04:16, Randy Li wrote: > > On 2024/2/15 04:38, Nicolas Dufresne wrote: >> Hi, >> >>>   media: v4l2-mem2mem: fix mem order in last buf >> mem order ? Did you mean call order ? > std::memory_order >> >> Le dimanche 11 février 2024 à 02:04 +0800, Hsia-Jun Li a écrit : >>> From: "Hsia-Jun(Randy) Li" >>> >>> The has_stopped property in struct v4l2_m2m_ctx is operated >>> without a lock protecction. Then the userspace calls to >>                   protection   When ?                   ~~ > Access to those 3 booleans you mentioned later. >>> v4l2_m2m_encoder_cmd()/v4l2_m2m_decoder_cmd() may lead to >>> a critical section issue. >> As there is no locking, there is no critical section, perhaps a better phrasing >> could help. > > "concurrent accesses to shared resources can lead to unexpected or erroneous behavior, so parts of the program where the shared resource is accessed need to be protected in ways that avoid the > concurrent access." > > It didn't say we need a lock here. > >>> Signed-off-by: Hsia-Jun(Randy) Li >>> --- >>>   drivers/media/v4l2-core/v4l2-mem2mem.c | 4 ++-- >>>   1 file changed, 2 insertions(+), 2 deletions(-) >>> >>> diff --git a/drivers/media/v4l2-core/v4l2-mem2mem.c b/drivers/media/v4l2-core/v4l2-mem2mem.c >>> index 75517134a5e9..f1de71031e02 100644 >>> --- a/drivers/media/v4l2-core/v4l2-mem2mem.c >>> +++ b/drivers/media/v4l2-core/v4l2-mem2mem.c >>> @@ -635,9 +635,9 @@ void v4l2_m2m_last_buffer_done(struct v4l2_m2m_ctx *m2m_ctx, >>>                      struct vb2_v4l2_buffer *vbuf) >>>   { >>>       vbuf->flags |= V4L2_BUF_FLAG_LAST; >>> -    vb2_buffer_done(&vbuf->vb2_buf, VB2_BUF_STATE_DONE); >>> - >>>       v4l2_m2m_mark_stopped(m2m_ctx); >>> + >>> +    vb2_buffer_done(&vbuf->vb2_buf, VB2_BUF_STATE_DONE); >> While it most likely fix the issue while testing, since userspace most likely >> polls on that queue and don't touch the driver until the poll was signalled, I >> strongly believe this is insufficient. When I look at vicodec and wave5, they >> both add a layer of locking on top of the mem2mem framework to fix this issue. > > Maybe a memory barrier is enough. Since vb2_buffer_done() itself would invoke the (spin)lock operation. > > When the poll() returns in userspace, the future operation for those three boolean variables won't happen before the lock. > >> I think this is unfortunate, but v4l2_m2m_mark_stopped() is backed by 3 booleans >> accessed in many places that aren't in any known atomic context. I think it >> would be nice to remove the spurious locking in drivers and try and fix this >> issue in the framework itself. > I tend to not introduce more locks here. There is a spinlock in m2m_ctx which is a pain in the ass, something we could reuse it to save our CPU but it just can't be access. I think the root cause is something else. Let me say first of all that swapping the order of the two calls does make sense: before returning the buffer you want to mark the queue as stopped. But the real problem is that for drivers using the mem2mem framework the streaming ioctls can be serialized with a different lock than the VIDIOC_DE/ENCODER_CMD ioctls. The reason for that is that those two ioctls are not marked with INFO_FL_QUEUE, but I think they should. These ioctls are really part of the streaming ioctls and should all use the same lock. Note that for many drivers the same mutex is used for the streaming ioctls as for all other ioctls, but it looks like at least the venus driver uses separate mutexes. With that change in v4l2-core/v4l2-ioctl.c I don't believe any locking is needed, since it should always be serialized by the same top-level mutex. The v4l2_ioctl_get_lock() function in v4l2-ioctl.c is the one that selects which mutex to use for a given ioctl. Regards, Hans >> >> Nicolas >> >>>   } >>>   EXPORT_SYMBOL_GPL(v4l2_m2m_last_buffer_done); >>>   >