From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=213.167.242.64 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796314767; b=w4K06+wmYko3mJuBnmgn2TyINA4V5hsLi5H+LYDuDS73LvloG7YEQyQQPKVF4rpnpqNH ZZ07fDb3wu5MH4ZOkPQqZ6OOuP29gIJc3rjB+0wFS+sEuDcDRvPUjj7xIQP/YprAiLHOL bmhE27IN43sWHPkbDD8ZiL++wlNS/C7xqA7Wor7Fkxnl3IOVx9VvjNAZW1HpiwBKw7ikD MoKI7860ziv998Gna8vsIZrkmDV/i/OpKxFyUEGrau0MKioAY0oiJOByRXDTSYXvAZCWg CEkq9DRPGdxCwicc/1BkoyE8A3RPSzz6+Nyu8CIf27MFP88fdP74rPCRAY4fm1JWJaA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796314767; h=DMARC-Filter:Received:DKIM-Signature:Date:From:To:Cc:Subject: Message-ID:References:MIME-Version:Content-Type:Content-Disposition: In-Reply-To; bh=JX3G8keEw1PlNhpz+zAl6B8J2/nlKq4dDuLGBMSPfhs=; b=ZqZDh7pceiiwuTgVRVNYrOOp6PlK/mI727ETz3g7LIMWoaj/HaOp7cQqOhVR5j5KxWLN om7AZ+RMVSJT381nMH7aondsdSIoOdPNbVUFdZy4NzPKrA1+CMbhJkLA6mRzQCX9xa7SU YI2AJgzFZh4DvHd1TanfDb5UaYsXbpD0NpsftYWzC/bBJMFleJBF9vLBF15Z65FKmrBQ3 NPKNdr/SSq/IThZhPlex0ysh3GbiAUE/uBcgen7ujVaOqORPTxk9pbRGYSuoAmzab5Xd5 ghkCx5R/oA/B2a9vWMLTXE3aSfkuK78cMEcBz+9cCBC3BHBBiBrOeahaPDd07mi/ELQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=ideasonboard.com; spf=pass smtp.mailfrom=ideasonboard.com; dkim=pass header.d=ideasonboard.com header.i=@ideasonboard.com header.a=rsa-sha256 header.s=mail header.b=XB2rVDDP; arc=none smtp.remote-ip=213.167.242.64 Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 08223606A682; Thu, 3 Dec 2026 16:19:25 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 08223606A682 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=ideasonboard.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=ideasonboard.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=ideasonboard.com header.i=@ideasonboard.com header.a=rsa-sha256 header.s=mail header.b=XB2rVDDP Received: from killaraus.ideasonboard.com (p773008-ipoe.ipoe.ocn.ne.jp [153.242.130.7]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 3EF95D0; Thu, 3 Dec 2026 17:16:42 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1796314604; bh=6kKAVv0ryv1YrIXMvDz2sh1cJ2rSncR6JAnpDKjHz+A=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=XB2rVDDP4xBDJQaTBbmdrsIcY17BPeoGn3PklyPteObuXMy8U1USUKCSS53qql4jH u1L1pXqI3S/rk89v58egoh+oU18FiLWfnCGl6K1eY7wkunR0QmfDFVdXa4rrW7RSMi 3kppoSrDz0ROPikN9oivL1/kw+3EtUTjuPmE5DGo= Date: Fri, 4 Dec 2026 01:19:18 +0900 From: Laurent Pinchart To: Hongbing Hu Cc: hansg@kernel.org, mchehab@kernel.org, kieran.bingham@ideasonboard.com, ribalda@chromium.org, linux-media@vger.kernel.org, linux5-kernel@vger.kernel.org, stable@vger.kernel.org Subject: Re: [PATCH v4] media: uvcvideo: defer cancelled buffer completion until copies finish Message-ID: <20261203161918.GC575190@killaraus.ideasonboard.com> References: <20261009063432.1781-1-huhb04@gmail.com> X-Mailing-List: stable@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <20261009063432.1781-1-huhb04@gmail.com> On Fri, Oct 09, 2026 at 02:34:32PM +0800, Hongbing Hu wrote: > The UVC driver processes frames in URBs. A frame usually is divided > into multiple URBs. The URB handler parses the header and the > metadata, but leaves the expensive memcpy to a workqueue. > > At any given moment, while streaming we will have: > - the current frame with (0-N) async memcpys waiting to happen. The > current frame is in queue->irqqueue. It has a refcnt of 1 + N memcpys > - Previous frames with (N) async memcpys waiting to happen. They are > not in queue->irqqueue. They have a refcnt of N memcpys > > With the current behaviour, when a URB completes with an error we flush > the queue->irqqueue irrespective of whether the buffers have any > pending memcpys. > > There can be situations where the memcpy occurs after we have flushed > the frames and they are returned to the user. Instead, we should > exploit the refcnt mechanism to return the frames only after the memcpys > have been completed. How did you test this patch ? > Fixes: 01e90464e42e ("media: uvcvideo: queue: Support asynchronous buffer handling") > Cc: stable@vger.kernel.org > Signed-off-by: Hongbing Hu > Reviewed-by: Ricardo Ribalda > --- > v4: > - Reworded the commit message as suggested during review. > - Documented in uvc_queue_buffer_complete() that it may be called with > queue->irqlock held when buf->cancelled is true. > > drivers/media/usb/uvc/uvc_queue.c | 24 ++++++++++++++++++++++-- > drivers/media/usb/uvc/uvcvideo.h | 1 + > 2 files changed, 23 insertions(+), 2 deletions(-) > > diff --git a/drivers/media/usb/uvc/uvc_queue.c b/drivers/media/usb/uvc/uvc_queue.c > index 3c002c8f44..c491735cfe 100644 > --- a/drivers/media/usb/uvc/uvc_queue.c > +++ b/drivers/media/usb/uvc/uvc_queue.c > @@ -122,6 +122,7 @@ static int uvc_buffer_prepare(struct vb2_buffer *vb) > > buf->state = UVC_BUF_STATE_QUEUED; > buf->error = 0; > + buf->cancelled = false; > buf->mem = vb2_plane_vaddr(vb, 0); > buf->length = vb2_plane_size(vb, 0); > if (vb->type != V4L2_BUF_TYPE_VIDEO_OUTPUT) > @@ -289,10 +290,17 @@ int uvc_queue_init(struct uvc_streaming *stream, struct uvc_video_queue *queue, > */ > void uvc_queue_cancel(struct uvc_video_queue *queue, int disconnect) > { > + struct uvc_buffer *buf; > unsigned long flags; > > spin_lock_irqsave(&queue->irqlock, flags); > - __uvc_queue_return_buffers(queue, UVC_BUF_STATE_ERROR); > + while (!list_empty(&queue->irqqueue)) { > + buf = list_first_entry(&queue->irqqueue, struct uvc_buffer, queue); > + list_del(&buf->queue); > + buf->error = 1; > + buf->cancelled = true; > + uvc_queue_buffer_release(buf); > + } > /* > * This must be protected by the irqlock spinlock to avoid race > * conditions between uvc_buffer_queue and the disconnection event that > @@ -350,13 +358,25 @@ static void uvc_queue_buffer_requeue(struct uvc_video_queue *queue, > uvc_buffer_queue(&buf->buf.vb2_buf); > } > > +/* > + * kref release callback for uvc_buffer. > + * > + * If buf->cancelled is true, this function may be called with > + * queue->irqlock held, as uvc_queue_cancel() releases the queue's > + * reference on cancelled buffers while holding the lock. > + */ > static void uvc_queue_buffer_complete(struct kref *ref) > { > struct uvc_buffer *buf = container_of(ref, struct uvc_buffer, ref); > struct vb2_buffer *vb = &buf->buf.vb2_buf; > struct uvc_video_queue *queue = vb2_get_drv_priv(vb->vb2_queue); > > - if (buf->error && !uvc_no_drop_param) { > + /* > + * Buffers cancelled from uvc_queue_cancel() are forced to complete as > + * errors. They must not be requeued by the corrupted-frame policy even > + * when buf->error is set. > + */ > + if (!buf->cancelled && buf->error && !uvc_no_drop_param) { > uvc_queue_buffer_requeue(queue, buf); > return; > } > diff --git a/drivers/media/usb/uvc/uvcvideo.h b/drivers/media/usb/uvc/uvcvideo.h > index b6bcee4a22..1b611715fa 100644 > --- a/drivers/media/usb/uvc/uvcvideo.h > +++ b/drivers/media/usb/uvc/uvcvideo.h > @@ -317,6 +317,7 @@ struct uvc_buffer { > > enum uvc_buffer_state state; > unsigned int error; > + bool cancelled; > > void *mem; > unsigned int length; -- Regards, Laurent Pinchart