From nobody Sun May 24 18:40:59 2026 Received: from mail-lf1-f43.google.com (mail-lf1-f43.google.com [209.85.167.43]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B202330216D for ; Sat, 23 May 2026 16:53:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.43 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779555224; cv=none; b=ZDrzBVeYYVInTxchd6vd0tD/d04Xu8dTylqIiCf09DoMPZmXJutPI+N6riPiLgGWxpHJpqSOEia69apDl4TNRLoy7VzBc2ADs3VUxltAk22sT89tqhSaD9wunjn1QDh6uXCBXOlrQDqCOBz4muKnPIvtcNC5a+S3EZou0aFfXXc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779555224; c=relaxed/simple; bh=som4gKyO96dhfc0ih3Y9ugIff6/5J1vxdGe58HMxGPg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=l9k7WoPCXTr7tCgNdvyIUjEerwkj1iPaTYsL0woX/fbbLfO9xfmnRf68usiO+XChxe/RMYudiKG3PB5xAO6IvGf/WKJG1vR+NbaBU6MozGdBaSQkkzqjLeaboakh4K7k3y4wP55R6+Lb263O4pEAcNq0jyf4lc32AQ7R5PDyS24= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=LZOJIAyf; arc=none smtp.client-ip=209.85.167.43 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.b="LZOJIAyf" Received: by mail-lf1-f43.google.com with SMTP id 2adb3069b0e04-5a742b8b72eso9558945e87.1 for ; Sat, 23 May 2026 09:53:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1779555220; x=1780160020; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=utnaPVtluIiTyEzwOhIMr0EOUnXAQ6TiN8kOxttnpoQ=; b=LZOJIAyf4mfLtwE2dXfVaB3lTmmoSi4T//KX4/gyBmnmdD15NHNgbIkaZa16M314jZ gRylO7Ku79zYnlkPnEWWsGacu5TfuaYMoHS08iU22EaS3leNquXEHkDFlQnD6/xYwVHk ocq99hcZdYDpOn/i3rrtpUCkKbDOYrm2UxI7bq8X9WB8bjTKi1I3YbKC2Qy6DOBUP7N+ lHRSNvONDZJiJwErmzZB5ySlpUIg0zE4WD+mynZOJM6r1DgsTkTxZUmUr/DFsc8Wz9iE xTInViUA2BFQIK6fP0SxqEoyMlZjnWmbX+Rwfyh7ku2hz5CJRI0S1KHkgPXGUKBdwjnX ZtHw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1779555220; x=1780160020; h=content-transfer-encoding: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; bh=utnaPVtluIiTyEzwOhIMr0EOUnXAQ6TiN8kOxttnpoQ=; b=fu7Vh2ISAbskMws5dhqYAlntLUIZAkEI4F4CtptMruBOfW3MvTzXD8KSNMZPBBzkrt SKLTBVUM5dChosOqqZ1BTVUyjOqf9kQ50JOHG8rNxA/4N0BLQMpw59DG5EHItcFvTQ3R 600OtpMzba96nVM/SAYtXdMofITHNdw3xHRmHTbVmrAbDYpVOjTarhqV9oSrW8siCvJD cuRvKCyNjqoh9BxeyAjgG5gC6L6FGCmyBy5KeHC+B8xjOY7cFTwIYHNzvlrqnOqB1izq Cw/iMaQml5YaS4KBle871pjgUOd0Heo0zRJT2TVoNMlG++jSUx/E7ZSY80O0cEfR65WJ sLTg== X-Forwarded-Encrypted: i=1; AFNElJ8ZvHfC7PohioOSJTIA11/yZMYhAmgr7XdB99L4OsZajw75jW9uUkF5cVfTlLd5+f2SDQLprGpSKyUjy+E=@vger.kernel.org X-Gm-Message-State: AOJu0YyYJ9qYfaJY8OzTzZA4ItisEghimQ+OJ4l2qJsVetnfeRTAdtmP MMXZ9S2VE3tyJkxXubIfxBKhiqCHel41dAJU1V30twqOS8+H2gMAQWW5rVlHsPV6MB3Yv+Pf X-Gm-Gg: Acq92OEnG/TRx7AOHZpWqtRKQdBTigyPSRawSmvH4lh/3ZuZUrry5qnYVeWWlT3PINB oEGsqYU0RP3Kca9+YBGHKdYxuLHBusXYMvuvgaURebSn0CjsSruFxl4tMYgW86AmoKCKR2XFtWg 5nD2VwX1+Uizh1SMubLodNxEpSP4M8rNfruGv9XLL7OuiOAO6AHD0u44gAlcY6WV9mYfjiNzhz4 CKHnvrDovRgC07ECZWNFmmjelHhdyN9cG1SuNcm5zhx6Na6PvnigFwy9oXVR6fyesrjJFsUI7Dd klKWQvgJuLBOMx9lBTfz3H8rND6oWnjxESCvUfLSp9/Jn1wwFdXcTeTDA0X/8ViPMjIENLA9abl 9W22hv2jPVXQHukbd/ZFH4pZwRhm864k9W+lBQX8Ceb+1+8nnTt5l11cwwC9g8uQpHOx8dDlrWQ E9wuDZfa+yJSPGoJfbSDtzDko0tyxYAiszjSY/hFjno+Bl1wB/0uZqjfOZBvM= X-Received: by 2002:a05:6512:2147:b0:5aa:b6b:93c3 with SMTP id 2adb3069b0e04-5aa323aaec3mr1730104e87.45.1779555219626; Sat, 23 May 2026 09:53:39 -0700 (PDT) Received: from va-HP-Pavilion-Desktop-595-p0xxx.mshome.net ([193.0.150.248]) by smtp.gmail.com with ESMTPSA id 2adb3069b0e04-5aa32ceb558sm1293839e87.41.2026.05.23.09.53.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 23 May 2026 09:53:39 -0700 (PDT) From: Valery Borovsky To: mchehab@kernel.org, crope@iki.fi Cc: hverkuil+cisco@kernel.org, linux-media@vger.kernel.org, stable@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v2] media: rtl2832_sdr: use vb2_video_unregister_device() on remove to fix DMA leak Date: Sat, 23 May 2026 19:53:37 +0300 Message-ID: <20260523165337.286141-1-vebohr@gmail.com> X-Mailer: git-send-email 2.51.0 In-Reply-To: <3c427dde-54c5-4a63-bcab-dd0079593ba1@kernel.org> References: <3c427dde-54c5-4a63-bcab-dd0079593ba1@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" rtl2832_sdr_remove() runs on USB disconnect and clears dev->udev to NULL before any pending streaming teardown has run. When user space later closes its file descriptor, vb2 calls rtl2832_sdr_stop_streaming() which in turn calls rtl2832_sdr_free_stream_bufs(). That helper releases each coherent buffer with: usb_free_coherent(dev->udev, dev->buf_size, dev->buf_list[dev->buf_num], dev->dma_addr[dev->buf_num]); usb_free_coherent() returns immediately when its dev argument is NULL, so every DMA stream buffer that was live at disconnect is silently leaked. The URBs allocated in rtl2832_sdr_alloc_urbs() outlive the device for the same reason. The rtl2832_sdr driver uses vb2_fop_release() in its file_operations, so replace video_unregister_device(&dev->vdev) with vb2_video_unregister_device(&dev->vdev) and move it before clearing dev->udev. vb2_video_unregister_device() releases the vb2 queue, which synchronously runs rtl2832_sdr_stop_streaming() if streaming is active, so URBs and coherent DMA stream buffers are freed while dev->udev is still valid. vb2_video_unregister_device() locks vdev->queue->lock (vb_queue_lock) internally, and stop_streaming() locks v4l2_lock, so the previous outer mutex_lock(&dev->vb_queue_lock) / mutex_lock(&dev->v4l2_lock) pair around the unregister sequence would self-deadlock and has been removed. A short v4l2_lock critical section around dev->udev =3D NULL remains so any ioctl path that still holds the file descriptor sees coherent state. Issue identified by automated review of the INV-003 series at https://sashiko.dev/ Fixes: 771138920eaf ("[media] rtl2832_sdr: Realtek RTL2832 SDR driver modul= e") Cc: stable@vger.kernel.org Suggested-by: Hans Verkuil Signed-off-by: Valery Borovsky --- Changes since v1 (https://lore.kernel.org/linux-media/20260513055745.146998-1-vebohr@gmail.c= om/): - Rewritten per Hans Verkuil's review (https://lore.kernel.org/linux-media/3c427dde-54c5-4a63-bcab-dd0079593ba1= @kernel.org/): replace video_unregister_device() with vb2_video_unregister_device() and move it before clearing dev->udev, instead of open-coding the URB/DMA teardown. vb2_video_unregister_device() releases the queue, which synchronously calls stop_streaming() while dev->udev is still valid, so the explicit rtl2832_sdr_kill_urbs() / rtl2832_sdr_free_urbs() / rtl2832_sdr_free_stream_bufs() block from v1 is no longer needed. - Dropped the outer mutex_lock(&dev->vb_queue_lock) / mutex_lock(&dev->v4l2_lock) around the unregister sequence: vb2_video_unregister_device() acquires vb_queue_lock internally and stop_streaming() acquires v4l2_lock, so holding either of those while calling the unregister helper self-deadlocks. - Rebased on media-committers/next. drivers/media/dvb-frontends/rtl2832_sdr.c | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/drivers/media/dvb-frontends/rtl2832_sdr.c b/drivers/media/dvb-= frontends/rtl2832_sdr.c index c564485e3bbb..c1f5f07c42a8 100644 --- a/drivers/media/dvb-frontends/rtl2832_sdr.c +++ b/drivers/media/dvb-frontends/rtl2832_sdr.c @@ -1477,14 +1477,22 @@ static void rtl2832_sdr_remove(struct platform_devi= ce *pdev) =20 dev_dbg(&pdev->dev, "\n"); =20 - mutex_lock(&dev->vb_queue_lock); + /* + * vb2_video_unregister_device() releases the vb2 queue, which + * triggers rtl2832_sdr_stop_streaming() if streaming is active. + * stop_streaming() uses dev->udev to free URBs and coherent DMA + * stream buffers via usb_free_coherent(), so it must run before + * dev->udev is cleared. vb2_video_unregister_device() locks + * vb_queue_lock internally and stop_streaming() locks v4l2_lock, + * so neither may be held by the caller. + */ + v4l2_device_disconnect(&dev->v4l2_dev); + vb2_video_unregister_device(&dev->vdev); + mutex_lock(&dev->v4l2_lock); - /* No need to keep the urbs around after disconnection */ dev->udev =3D NULL; - v4l2_device_disconnect(&dev->v4l2_dev); - video_unregister_device(&dev->vdev); mutex_unlock(&dev->v4l2_lock); - mutex_unlock(&dev->vb_queue_lock); + v4l2_device_put(&dev->v4l2_dev); module_put(pdev->dev.parent->driver->owner); } --=20 2.51.0