Received: by 2002:a05:7412:1e0b:b0:fc:a2b0:25d7 with SMTP id kr11csp1303748rdb; Fri, 16 Feb 2024 11:11:01 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCXqzy23fdDi2GLUgNZE33xs43j6jx0qx+qfUFs6LPMZGbm+i6bT64jvi3+1beW/8/w7+xkcEKLnlqDj5cwlklvgrvHep02l0wu6nEYz9A== X-Google-Smtp-Source: AGHT+IGvvn9iDGWYSEpbVFp8lf1N82lDEDgI4ANw5fA++eEIkDqL4tEnA/XOQZBiMUJ1LXCKbysu X-Received: by 2002:a05:6a00:2344:b0:6e0:38bf:28ea with SMTP id j4-20020a056a00234400b006e038bf28eamr5999856pfj.19.1708110661675; Fri, 16 Feb 2024 11:11:01 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708110661; cv=pass; d=google.com; s=arc-20160816; b=C2LZqptRJZO60SpuA+xqThVQSD5lGW3C0WHmDrFTUeDArZx4zsiCk3YUfsRvI8ZgKG 16b8JBhA/wzyyNUPXZM/IExn9PtYEQonFVBumBilItJFmGMNOI7dsXrKzuxkq+jJLO1j M/4gOo2/XZ5msUT7Z4ZGOpajdleHeqPQSVf27BD8YcxpGgzljYqJNBvDgW6yKN5u+Ck+ kSX+ljTZQmoZJsw2ceUqIgU3gdkzFcxoDN9MVdkFocBd7sAo2NLKbevmS/D/X1ctaiT0 5tCGKtWDxNstFn9XIwPMxkRTfP8DEjMvXup4h3pX5xt7O/f8taMcjWIso23UxlJqjen9 4z1w== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=mime-version:list-unsubscribe:list-subscribe:list-id:precedence :user-agent:content-transfer-encoding:autocrypt:references :in-reply-to:date:cc:to:from:subject:message-id:dkim-signature; bh=ltUfpC1q0yQ+TbZo5xp8HFWJH7GAwxI5pAFjR5rZI54=; fh=6uBjzZnjSq2VcjfrnWR/E4WM++bDyPa9E76sYtR0zYM=; b=RXDfF8tAx8cNhOV4lcG6xEq8i18vBPfN7IC7kX9qJuShlITwwV+DRx2ntaRP/ap07A VEvAM6XmoBbxYx7sPYcZfUXNrdL56en9TTapFzyXkn9wGx7/fM8et6YUVIb8LMfVqnGs 6Bfz3wuqyjIVXZEAuCi4UPiojjj571ulreMP+HERIwZrWARbu2QWDCJ9k4Ph8rA47biU 2YzdhVTfsJJifTDFciJj1yVz4BES8tLYk9a8syr3YCDTGynecuECD5suMuqSQdGQNxGu NvxQ6UnmGLSSWqWFxhlygWTGFItitYx4lN896qYp5edF4mfHEf54bINdQRnBPG7WCbYQ cp/Q==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@collabora.com header.s=mail header.b=5c3Tatqj; arc=pass (i=1 spf=pass spfdomain=collabora.com dkim=pass dkdomain=collabora.com dmarc=pass fromdomain=collabora.com); spf=pass (google.com: domain of linux-kernel+bounces-69231-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-69231-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=collabora.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id jc35-20020a056a006ca300b006e04639e9c2si338568pfb.330.2024.02.16.11.11.01 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 16 Feb 2024 11:11:01 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-69231-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@collabora.com header.s=mail header.b=5c3Tatqj; arc=pass (i=1 spf=pass spfdomain=collabora.com dkim=pass dkdomain=collabora.com dmarc=pass fromdomain=collabora.com); spf=pass (google.com: domain of linux-kernel+bounces-69231-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-69231-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=collabora.com 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 sv.mirrors.kernel.org (Postfix) with ESMTPS id 52BB628195E for ; Fri, 16 Feb 2024 19:09:51 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 61F27135A7A; Fri, 16 Feb 2024 19:09:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=collabora.com header.i=@collabora.com header.b="5c3Tatqj" Received: from madrid.collaboradmins.com (madrid.collaboradmins.com [46.235.227.194]) (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 8A6E7149DF2; Fri, 16 Feb 2024 19:09:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=46.235.227.194 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708110579; cv=none; b=Q5gk85H/zQuNVfF+U8gAP4Rm1LDzcrguygtVnR3BSELb7ITmxHzZj5APHfWQaRyjy5yV8TVjACHY6NOAkL6jBhcwtyXrcZnnnHC1yrba423EkFkw8w/4jRAMEZj2EIBzCEyFRB+mPoWcsMzp/tHLLMKo3VJ03YvibwMz2LyRjeQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708110579; c=relaxed/simple; bh=DeZ3Orr0jVVRVzTlscOZ+7ltViaJ+hZdtewLDmklOwg=; h=Message-ID:Subject:From:To:Cc:Date:In-Reply-To:References: Content-Type:MIME-Version; b=up4OKVa2JgTLoejMRZzRvdVeHxReUViedhKLMaVoWyw6CP/QikjJTXO4gMq4OLHnrInJMzMKpQa7KTglYNgFmLf0WVF7ULAL0gDzbZmmBSrAA7FRnL1/ngft1oEHNs+HXeqSdRtUAagL8cG+F3ZTasblRIEpR1xjONccFFhzC8A= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=collabora.com; spf=pass smtp.mailfrom=collabora.com; dkim=pass (2048-bit key) header.d=collabora.com header.i=@collabora.com header.b=5c3Tatqj; arc=none smtp.client-ip=46.235.227.194 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=collabora.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=collabora.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=collabora.com; s=mail; t=1708110575; bh=DeZ3Orr0jVVRVzTlscOZ+7ltViaJ+hZdtewLDmklOwg=; h=Subject:From:To:Cc:Date:In-Reply-To:References:From; b=5c3TatqjprbCEYgnLKI5ccgyqvIomNAlxyz4gVN28Pr62//sClUOYSz6R8Ocetnmw cjOmqGNSUoKN3YOpocUAZoyt0HSwATBCXXIvIzp3D0boUdflt0T2xyJv5xWLF8hI72 RPdx0I3dDgDicAFe2zVbHNd5iFjXackvVAGkSX+t8+KT9n3uBjG4xXdz8a6NSDjnwn /7871Kl03zGobmj4BTB4J0Yii1mPp40xiklrHpUDVy7IvwbzswTBcViKe1Hl2olP+a QDKla0yVyyMQjuTRO8yBHwWw3V14g+Ik8w96e5CgbOVHMe2VmGpEx/Oa7+priyVwsY R7zSAxX6jfd7Q== Received: from nicolas-tpx395.localdomain (cola.collaboradmins.com [195.201.22.229]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: nicolas) by madrid.collaboradmins.com (Postfix) with ESMTPSA id 9D6173780A0B; Fri, 16 Feb 2024 19:09:34 +0000 (UTC) Message-ID: <8d236cbfca8c4ea573d340109387fc804f1a89cd.camel@collabora.com> Subject: Re: [PATCH] media: v4l2-mem2mem: fix mem order in last buf From: Nicolas Dufresne To: Hans Verkuil , Randy Li 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 Date: Fri, 16 Feb 2024 14:09:31 -0500 In-Reply-To: <27ef9490-a56b-46bf-84bd-bc2ec08896af@xs4all.nl> References: <20240210180414.49184-1-randy.li@synaptics.com> <1f80b5ea-1209-438f-b07f-3a4a308ee35d@soulik.info> <27ef9490-a56b-46bf-84bd-bc2ec08896af@xs4all.nl> Autocrypt: addr=nicolas.dufresne@collabora.com; prefer-encrypt=mutual; keydata=mQGiBEUQN0MRBACQYceNSezSdMjx7sx6gwKkMghrrODgl3B0eXBTgNp6c431IfOOEsdvkoOh1kwoYcQgbg4MXw6beOltysX4e8fFWsiRkc2nvvRW9ir9kHDm49MkBLqaDjTqOkYKNMiurFW+gozpr/lUW15QqT6v68RYe0zRdtwGZqeLzX2LVuukGwCg4AISzswrrYHNV7vQLcbaUhPgIl0D+gILYT9TJgAEK4YHW+bFRcY+cgUFoLQqQayECMlctKoLOE69nIYOc/hDr9uih1wxrQ/yL0NJvQCohSPyoyLF9b2EuIGhQVp05XP7FzlTxhYvGO/DtO08ec85+bTfVBMV6eeY4MS3ZU+1z7ObD7Pf29YjyTehN2Dan6w1g2rBk5MoA/9nDocSlk4pbFpsYSFmVHsDiAOFje3+iY4ftVDKunKYWMhwRVBjAREOByBagmRau0cLEcElpf4hX5f978GoxSGIsiKoDAlXX+ICDOWC1/EXhEEmBR1gL0QJgiVviNyLfGJlZWnPjw6xhhmtHYWTDxBOP5peztyc2PqeKsLsLWzAr7RDTmljb2xhcyBEdWZyZXNuZSAoQi4gU2MuIEluZm9ybWF0aXF1ZS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Y29sYXMuZHVmcmVzbmVAdXNoZXJicm9va2UuY2E+iGAEExECACAFAkUQN0MCGwMGCwkIBwMCBBUCCAMEFgIDAQIeAQIXgAAKCRBxUwItrAaoHPTnAJ0WGgJJVspoctAvEcI00mtp5WAFGgCgr+E7ItOqZEHAs+xabBgknYZIFPW5Ag0ERRA3UhAIAJ0rxl2HsVg/nSOAUt7U/T/W+RKzVAlD9orCB0pRVvyWNxSr8MHcHmWCxykLuB34ouM4GuDVRKfGnqLzJRBfjs7Ax9K2FI3Odund9xpviLCt1jFC0K XL04RebrFT7xjDfocDaSLFvgxMVs/Jr2/ckKPId1oKvgYgt/o+MzUabKyFB8wIvq4GMtj3LoBKLCie2nCaSt7uVUt6q2t5bNWrd3lO6/mWn7YMc5Hsn33H9pS0+9szw6m3dG08eMKNueDlt72QxiYl2rhjzkT4ltKEkFgYBdyrtIj1UO6eX+YXb4E1rCMJrdjBSgqDPK1sWHC7gliy+izr+XTHuFwlfy8gBpsAAwUIAJJNus64gri4HAL632eqVpza83EphX1IuHzLi1LlMnQ9Tm7XKag46NhmJbOByMG33LwBsBdLjjHQSVkYZFWUifq+NWSFC/kqlb72vW8rBAv64+i3QdfxK9FWbweiRsPpvuHjJQuecbPDJpubLaxKbu2aqLCN5LuHXvdQr6KiXwabT+OJ9AJAqHG7q4IEzg4RNUVn9AS6L8bxqMSocjqpWNBCY2efCVd/c6k4Acv6jXu+wDAZEbWXK+71uaUHExhigBYBpiHGrobe32YlTVE/XEIzKKywhm/Hkn5YKWzumLte6xiD9JhKabmD7uqIvLt2twUpz4BdPzj0dvGlSmvFcaaISQQYEQIACQUCRRA3UgIbDAAKCRBxUwItrAaoHJLyAKDeS3AFowM3f1Y3OFU6XRCTKK2ZhwCfT/7P9WDjkkmiq5AfeOiwVlpuHtM= Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable User-Agent: Evolution 3.50.3 (3.50.3-1.fc39) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Le jeudi 15 f=C3=A9vrier 2024 =C3=A0 09:41 +0100, Hans Verkuil a =C3=A9crit= =C2=A0: > On 15/02/2024 04:16, Randy Li wrote: > >=20 > > On 2024/2/15 04:38, Nicolas Dufresne wrote: > > > Hi, > > >=20 > > > > =C2=A0 media: v4l2-mem2mem: fix mem order in last buf > > > mem order ? Did you mean call order ? > > std::memory_order > > >=20 > > > Le dimanche 11 f=C3=A9vrier 2024 =C3=A0 02:04 +0800, Hsia-Jun Li a = =C3=A9crit=C2=A0: > > > > From: "Hsia-Jun(Randy) Li" > > > >=20 > > > > The has_stopped property in struct v4l2_m2m_ctx is operated > > > > without a lock protecction. Then the userspace calls to > > > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 protection=C2=A0=C2=A0 When ?=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0 ~~ > > 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 bette= r phrasing > > > could help. > >=20 > > "concurrent accesses to shared resources can lead to unexpected or erro= neous behavior, so parts of the program where the shared resource is access= ed need to be protected in ways that avoid the > > concurrent access." > >=20 > > It didn't say we need a lock here. > >=20 > > > > Signed-off-by: Hsia-Jun(Randy) Li > > > > --- > > > > =C2=A0 drivers/media/v4l2-core/v4l2-mem2mem.c | 4 ++-- > > > > =C2=A0 1 file changed, 2 insertions(+), 2 deletions(-) > > > >=20 > > > > 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, > > > > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 struct vb2_v4l2_buff= er *vbuf) > > > > =C2=A0 { > > > > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 vbuf->flags |=3D V4L2_BUF_FLAG_LAST; > > > > -=C2=A0=C2=A0=C2=A0 vb2_buffer_done(&vbuf->vb2_buf, VB2_BUF_STATE_D= ONE); > > > > - > > > > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 v4l2_m2m_mark_stopped(m2m_ctx); > > > > + > > > > +=C2=A0=C2=A0=C2=A0 vb2_buffer_done(&vbuf->vb2_buf, VB2_BUF_STATE_D= ONE); > > > While it most likely fix the issue while testing, since userspace mos= t likely > > > polls on that queue and don't touch the driver until the poll was sig= nalled, I > > > strongly believe this is insufficient. When I look at vicodec and wav= e5, they > > > both add a layer of locking on top of the mem2mem framework to fix th= is issue. > >=20 > > Maybe a memory barrier is enough. Since vb2_buffer_done() itself would = invoke the (spin)lock operation. > >=20 > > When the poll() returns in userspace, the future operation for those th= ree boolean variables won't happen before the lock. > >=20 > > > 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 th= ink it > > > would be nice to remove the spurious locking in drivers and try and f= ix 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 bu= t it just can't be access. >=20 > I think the root cause is something else. >=20 > Let me say first of all that swapping the order of the two calls does mak= e sense: > before returning the buffer you want to mark the queue as stopped. >=20 > 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. >=20 > 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 io= ctls > and should all use the same lock.=20 >=20 > Note that for many drivers the same mutex is used for the streaming ioctl= s as for > all other ioctls, but it looks like at least the venus driver uses separa= te mutexes. >=20 > 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. >=20 > The v4l2_ioctl_get_lock() function in v4l2-ioctl.c is the one that select= s which > mutex to use for a given ioctl. There was no way to comply with the spec without accessing that state in th= e irq thread in Wave5. In that case, we need to decide if we continue or cancel a dynamic resolution change. if (!v4l2_m2m_has_stopped(m2m_ctx)) { switch_state(inst, VPU_INST_STATE_STOP); if (dec_info.sequence_changed) handle_dynamic_resolution_change(inst); else send_eos_event(inst); flag_last_buffer_done(inst); } That forced us to introduce a spinlock to protect that state in that driver= . As for locking cmd_start, that might help, its certainly a behaviour change= and will have to be taken with care. How does the ioctl lock interact with bloc= king QBUF notably ? Nicolas >=20 > Regards, >=20 > Hans >=20 > > >=20 > > > Nicolas > > >=20 > > > > =C2=A0 } > > > > =C2=A0 EXPORT_SYMBOL_GPL(v4l2_m2m_last_buffer_done); > > > > =C2=A0=20 > >=20 >=20 >=20