From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.173 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796377579; b=GDDziL+Epa+J+gLZlVVYpIuqTaDrBRCkk8STKNyLQiViNfUGgtyMseslwm9OM4gav2TC 6+p7+kM2uDDHBK3F4uA6sasBHPhLYoFbINTBxr2G6O1E4dIAfrUD0RsZp6HMnM37oi/Vk x/UxtBLMynUp7TaLnz1M+Rj9g7Xar7xLvXjnuInzvNxS1rLyeoAEPfzD+M8F8kXtUU7L3 SOBX4eRtqcGNv3NLp3tIXLy4FNCLe99YFOUQ58Yxwk2bS3PDacHVnH45MWnedvLvlhRzQ cZHe4Q3mNu0hDQ9JaD5FAcNKw/JbKwV9EbTZo0/Vl3sCbet7ejAK3Hwlb11pS1FP10Q== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796377579; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:To:Cc:Subject:Date:Message-Id:X-Mailer:In-Reply-To:References: MIME-Version:Content-Type:Content-Transfer-Encoding; bh=OeoG4Wl8MsyLYnYDyhaWj6Np9sSgheHgz7N5vNylHjc=; b=yGZPSEUWIRbTic/wTQt3gX0nd81/Nr+XlVZrQC9G9V4eG6dJsBX5sC4nfXxylpR4fLyU 4wTk/kV5xiA4ZAhvFsxcixvBagpEDOoZg9tDq5CgroM3sf+nYwrjLP0ZWTknvomPyqsCz s6Kyug11E3ksvS5M0eJYaVqziA2IF3AE+voFrcl3q6uPIJ9Dy6l4ytji8QHz836M7Gvyt oA8r3obcTcfXLVKUIVcW4kdnZCDY9j16QnNkhp+NNHgIp81Ho251YJhM/4pKaG/U2p3r6 ypwk+xerUSL1ghsPZpWvxMvV+rvK+Jt3+yB82+IAo3Oy1vZO947m8gkagXnlR5yxNOA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=YePzKLjz; arc=none smtp.remote-ip=74.125.82.173 Received: from mail-dy1-f173.google.com (mail-dy1-f173.google.com [74.125.82.173]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4CE57606A330 for ; Fri, 4 Dec 2026 09:46:19 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 4CE57606A330 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=YePzKLjz Received: by mail-dy1-f173.google.com with SMTP id 5a478bee46e88-38699cf08dfso895601eec.1 for ; Fri, 04 Dec 2026 01:46:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796377578; x=1796982378; darn=vger.kernel.org; h=content-transfer-encoding:content-type:mime-version:references :in-reply-to:message-id:date:subject:cc:to:from:from:to:cc:subject :date:message-id:reply-to:content-type; bh=OeoG4Wl8MsyLYnYDyhaWj6Np9sSgheHgz7N5vNylHjc=; b=YePzKLjz3SC/hldTeyIukyz4GPFYEVUNwZflcg79CdAlvt6Iq86v3Qo9pe53wDMilJ Sk/mtPqmMlZZFjtgkv4D2zWWF4fAeIOqFrNEMPUKWTJ74KN8EO7nLdWQJ7pXex2BqHI5 R+a2vXZ1jL0oON8ikhJKxFNYW2JVmKHAOj/ayOIlR+pw+okP7uQGCdJi67puknKN6Kkg 3wGi1rqygUwPJejNQ0rkjyDkifwDoKKGhHA117vNistiYevowivIQmYHJzyL4E+48dSP qensN0fTRYofEq0+yKp5nRsNKy6/6DX2XbdLTMo/3dq7QkdnKJrY0SjDLX2tjQwTtcLi E+yQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796377578; x=1796982378; h=content-transfer-encoding:content-type:mime-version:references :in-reply-to:message-id:date:subject:cc:to:from:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=OeoG4Wl8MsyLYnYDyhaWj6Np9sSgheHgz7N5vNylHjc=; b=0NTxm/rHfYNMmCn2v/gbpB6dY5pjGpVoOZtHe/21k5zvXExM+jMlx8ryDVw5X06JeU YdIbFdslQ+coNNQEnjKMCePWQg2arlkeg3daIjWSS0ySprCoCnNSYMhzPoGrlkfmEkfa jIBXzNBSZJDgVxcojXwB9vapAygM6exH1xo9ogWAHO5oqhLB7TiVjCblX5jJePfG7FEp NWIIFiYCEYbltYmlohR9lZxtlvKVedV60Mr87gnSwck+zpH8DU59YWlfkTKhSdzwm9qx vi17yrB8Q6+/2xorqBoLh2e7MoztA8WNEPALaTWe6HFDh6C2A3sldOUEnrgPAaEcaREE 8Y0Q== X-Forwarded-Encrypted: i=1; AL+hErHJhLuqHOjCRoHyZtQ/zUpGF9PEhph7sRhdFrK2vdkBvPIzsAIu2Rxixfh6y4RJJ34LQNYFoBg=@vger.kernel.org X-Gm-Message-State: APuU4Z8jZoaM5o3MzApt/CbRxtbKmHyUjq0CXbMSEv+kiHmcap2UhIyy 7iIukxs4uQ15He6RJDMj5iqHuxmSY2CUfMx3WdOIPrgs9xV2mTqjxQWK X-Gm-Gg: AZFdTZczu3MN5HwhqyAAdqbaTozt86N3Brun7rOLHvUF0/fXrSw0bzAustrjWmIFWVg LTMtGlXIF3HoW1wk64K3kJmRatzkl7w5jC4K9N3xEvbPPyn7E/dPn6R0gPwaD1Wer/Atj9/bXac 32g8ol2XGbIX6phtGrxPZYXx3PizVOT6d6eM2LYWIkOwIso2BCR9+S3hFk7DzeU9JA49E7FsP2T EjiQzjNY2AMBpf18pRAPbVskqDQgWPUZ4hp8+yLYw/8HN6nPPwE7gBOuZsI/0lrn1W5elJrAXlo sxVyHHZ37/UAXnVdhkzP+IZRCq+3MTlBRhXopIRtdeQRrJ+S7orYaRLtKZyNdRoncfJKNmIVv8J hfKJHbl9EUJ83YTHLbWzCZh8jlvQflW2lTHNHhVgvAVB+/H2N6WFxdDjd2rsg87Gaio16pqKOje 3LYTlmT3YHEFPrkwR/937xI246Q+TV30xAe6pd4h5xGQxyooTtARpp6r0dYOEtC3PA+opI4Emgk DKKeDFiPInVEdWFBNzsabTdGP8lj/wa/AREAzwK6CjSw7OQRZu42+Phm3P0+kCXEvok/xeRqwNG SvV4suMLFyCXgGDTtoiJ3UvXPGBRYfIj6MdzWVNG4+l8HJSQeMgKLscvK2s= X-Received: by 2002:a05:7300:d215:b0:386:bc40:1508 with SMTP id 5a478bee46e88-387520ee54emr1517166eec.14.1796377578061; Fri, 04 Dec 2026 01:46:18 -0800 (PST) Received: from XP-PC-huhb1.xiaopeng.local ([98.98.112.197]) by smtp.gmail.com with ESMTPSA id 5a478bee46e88-3875207adecsm7035009eec.3.2026.12.04.01.46.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Dec 2026 01:46:17 -0800 (PST) From: Hongbing Hu To: Laurent Pinchart Cc: Hans de Goede , Mauro Carvalho Chehab , Kieran Bingham , Ricardo Ribalda , 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 Date: Fri, 4 Dec 2026 17:39:47 +0800 Message-Id: <20261204093947.2612-1-huhb04@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20261203161918.GC575190@killaraus.ideasonboard.com> References: <20261009063432.1781-1-huhb04@gmail.com> <20261203161918.GC575190@killaraus.ideasonboard.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-Transfer-Encoding: 8bit On Fri, Dec 04, 2026 at 01:19:18AM +0900, Laurent Pinchart wrote: > 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 ? The crash was reported by an end user in normal daily use, and I have the original kernel log from the device. Setup: MediaTek MT8676, Android 14 vendor kernel based on 6.1.115 UVC camera at 1-1.2.3:1.1 (idVendor=345f, idProduct=2132), bulk streaming. UVC device info: New USB device found, idVendor=345f, idProduct=2132, bcdDevice=21.00 New USB device strings: Mfr=1, Product=5, SerialNumber=7 Product: Haigibis Manufacturer: MACROSILICON SerialNumber: 20210621 Found UVC 1.00 device Haigibis (345f:2132) The device rebooted once, after ~52 hours of uptime, while the camera was in use. The log shows: [188166.491411] uvcvideo 1-1.2.3:1.1: Non-zero status (-71) in video completion handler. ... (repeated 5 times) [188166.493670] list_del corruption. next->prev should be ffffff8239588fb0, but was ffffff837ce977b0. (next=ffffff837ce92fb0) [188166.493712] kernel BUG at lib/list_debug.c:64! ... [188166.517717] __list_del_entry_valid+0xcc/0xd4 [188166.517724] uvc_queue_cancel+0x88/0xdc [188166.517732] uvc_video_complete+0x1b0/0x1e0 [188166.517739] __usb_hcd_giveback_urb+0xb0/0x138 ... [188167.149087] Kernel panic - not syncing: Oops - BUG: Fatal exception in interrupt The trigger was a burst of -EPROTO (-71) URB completions, which take the non-zero status path in uvc_video_complete() to uvc_queue_cancel(queue, urb->status == -ESHUTDOWN). As the status is -EPROTO, the queue is not marked disconnected, so userspace keeps re-queueing the cancelled buffers while memcpy workers are still running on them. Cancel then walked an already corrupted irqqueue and BUGged in list_del, panicking the device from softirq context. (44 seconds earlier, after a first -71 burst, the camera had re-enumerated, so the cancel path can be hit repeatedly.) The same log line is also direct evidence of the duplicate list insertion. The node being unlinked, 0xffffff8239588fb0, still had ->next == 0xffffff837ce92fb0 (the "next=" value; the same address is in x23 of the register dump). But that successor's ->prev had been rewritten to 0xffffff837ce977b0, a third node. So two nodes point at the successor while its own ->prev names only one of them -- the state a second list_add_tail() of an already-linked node leaves behind. __list_add() rewires new->prev/new->next and prev->next, but it never touches the old predecessor, which keeps pointing at the re-linked buffer; a normal list_del()/list_add() pair cannot produce that. All irqqueue mutations (uvc_buffer_queue(), uvc_queue_buffer_requeue(), uvc_queue_cancel(), uvc_queue_next_buffer()) are serialized by queue->irqlock, so this chain was not built by concurrent list operations -- it was already inconsistent when cancel() took the lock. The driver path that re-links a buffer without unlinking it first is the stale completion: uvc_queue_buffer_complete() -> uvc_queue_buffer_requeue() -> uvc_buffer_queue() -> list_add_tail() on a buffer that userspace has already QBUF'ed again. uvc_buffer_queue() also calls kref_init(), resetting the refcount that the still-pending memcpy is waiting on, which is how an old kref_put() can drive the count back to zero and re-enter the completion path at all. The insertion itself is silent: CONFIG_DEBUG_LIST is enabled on this kernel (the list_del() sanity check fired), but __list_add_valid() only validates the two neighbours and does not check whether "new" is already linked, so a re-inserted buffer that is not the current tail passes it. The damage only becomes visible at the next list_del(), which is the BUG in uvc_queue_cancel(). This is also why there is no list_add warning anywhere in the log. With this patch, cancel() marks the buffers cancelled and drops the queue reference instead of returning them while copies are still pending; the buffer is completed only when the last memcpy releases its reference, so it can no longer be requeued after userspace has QBUF'ed it again. This is an intermittent, timing-dependent race: we have seen the failure only once, and we cannot reproduce it on demand. It came from an end user's device in ordinary use, where running KASAN, lockdep or synthetic stress tests is not possible. The user is a consumer, not a test lab: the camera is used through normal applications, with no stress script. With this patch backported to the same 6.1.115-based vendor kernel, the user has continued normal use for about one month without a reboot. The evidence is admittedly indirect -- the crash signature matches this race exactly, but the fix so far has only been validated by the absence of a recurrence in real-world use. > > > 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 Thanks, Hongbing