From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796054071; b=jDMxw2gBJoSBGE/Gkkemdq2oqsnvP1flc/ThkGlO+rhun8JooSVV8YZOTksCt8uuVnSQ eJsxdnrK/GL+CtArNfor4Y9icUZFlBOKiTRido7oWCIxeuuV/0Zr60F09wx79HL7zkev+ AhKlRjWQ5gbKN2FjG8oe7OIIU+LVtDGu0YQvB4YP+qpVgTt94L2dnYjPmtxKSq+3Mu0fq NjeGIb4G01SHfyvL8/3O0dbz1EF+DaZVHEmN6fe2c6F/kIvqe7cOOMjmkfSSSn1BISl8I n+GHGxM10ykHW6tbbrZ02c4Pr4bXGX2GTnk4SC2jiKnGNfRvtUyDmEi4V/I5SkgxnIQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796054071; h=Received:DKIM-Signature:Message-ID:Date:MIME-Version:User-Agent: Subject:To:Cc:References:From:Content-Language:In-Reply-To: Content-Type:Content-Transfer-Encoding; bh=qiuKikh+Q7bAuAcZTQgWRsbpDfRI1LkAjq1CGsJq5m8=; b=yi257TQI8VvDvFCcAU0L11zRNOPfC4DKvBhWkX2zNFYd0lY6XbNl8wHSyplvEWtnuD7Y DO8ghQz5fL4jGQ6zf7DAzYKdfydM8TvDDi1gmjNdqcnOoOBlDwRu+eVIt+79z7qon/f9g InD3Xt0/l4p+0LM5dw14D+ZQlFY8alHeuA1o0BTKgTzUgC4J+XPslzfRdP7WdE8CRZSXO N6YaoqW5bG0QORp597FSdF2Sa8npI2KPlf262Y1M5c+U4rpsg1S3iqnuaFAqb7X65oIXT X6hr1mAxJU0Y2wczzPuy6xKT1wssMcA9W/UaVD2zoqXR2ixtFZeW2EwoVPx7iu4JsEw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=EQI+DIbG; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5529E606A31C; Mon, 30 Nov 2026 15:54:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=EQI+DIbG Received: by smtp.kernel.org (Postfix) with ESMTPSA id 13D271F00897; Mon, 30 Nov 2026 15:54:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796054070; bh=qiuKikh+Q7bAuAcZTQgWRsbpDfRI1LkAjq1CGsJq5m8=; h=Date:Subject:To:Cc:References:From:In-Reply-To; b=EQI+DIbGi0XwsQ29b+pVTmkYt1I0P9U7Uwh/wBePchkuOx1/Z2+4IqKmyQitcLzA4 djT/VgJ4YFxSCGSuHw0HjB9EfFiDKFA2O1IVyTg7Xj5qEGvA00UcNViIxwFs8GVXbE Ed30GpZfmh0fbjJpACdfj78YoHblBXBJ0WD752lRxyIGoKGokzdb2upDd/SjYG6IZ1 1nTW05eqluszAANOyCLBGVod/KMpm7WRRZFVBpFQiEnWTcFKvWLvqpiQjLbiXR5jdC czP/rpEUOMj7UvqWVSphAK3dgkTPZl5ShORPMv0rbb3hbl5+xqmW4RPFYKLOsV91zf JD/Y4W25CQSUQ== Message-ID: <44ff4c5c-cce4-440c-8a6d-a576956fb31c@kernel.org> Date: Mon, 30 Nov 2026 16:54:26 +0100 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 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v4] media: uvcvideo: defer cancelled buffer completion until copies finish To: Hongbing Hu , laurent.pinchart@ideasonboard.com Cc: mchehab@kernel.org, kieran.bingham@ideasonboard.com, ribalda@chromium.org, linux-media@vger.kernel.org, linux5-kernel@vger.kernel.org, stable@vger.kernel.org References: <20261009063432.1781-1-huhb04@gmail.com> From: Hans de Goede Content-Language: en-US, nl In-Reply-To: <20261009063432.1781-1-huhb04@gmail.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit Hi, On 9-Oct-26 08:34, 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. > > Fixes: 01e90464e42e ("media: uvcvideo: queue: Support asynchronous buffer handling") > Cc: stable@vger.kernel.org > Signed-off-by: Hongbing Hu > Reviewed-by: Ricardo Ribalda Thank you for your patch. I have merged this into: https://gitlab.freedesktop.org/linux-media/users/uvc/-/commits/for-next/ Regards, Hans > --- > 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;