Linux Media Controller development
 help / color / mirror / Atom feed
* [PATCH] media: bttv: Let the V4L2 core serialize the ioctls with btv->lock
@ 2026-12-04 18:49 Praneeth Balasubramanian
  0 siblings, 0 replies; only message in thread
From: Praneeth Balasubramanian @ 2026-12-04 18:49 UTC (permalink / raw)
  To: Mauro Carvalho Chehab, Hans Verkuil
  Cc: linux-media, linux-kernel, Praneeth Balasubramanian, stable

set_tvnorm() and set_input() must be called with btv->lock held, as
their comments say, but bttv_s_std() and bttv_s_input() call them
without it. Commit 587f0d5d6d44 ("[media] bttv: fix mutex use before
init (BZ#24602)") removed the driver's own locking from the ioctl
handlers, saying that the V4L2 core would serialize all ioctls from then
on, but it never set video_device->lock. Only the buffer queue uses
btv->lock (as q->lock since the vb2 conversion), so the core takes no
lock for VIDIOC_S_STD, VIDIOC_S_INPUT and the other ioctls that do not
touch the queue.

Two such calls can therefore run at the same time and leave
btv->tvnorm, btv->input, the cropping state, the timing registers and
the subdevice routing out of sync, for example with the chip programmed
for one standard while the driver reports another.

Set vfd->lock to btv->lock for the video, vbi and radio devices, so the
V4L2 core serializes all ioctls as intended. bttv_try_fmt_vbi_cap() and
bttv_s_fmt_vbi_cap() took btv->lock themselves; drop that, since the
core now holds it when they run.

The missing lock was found by a static check that turns the locking rule
in a function's comment into a Clang context-analysis requirement on its
callers.

Fixes: 587f0d5d6d44 ("[media] bttv: fix mutex use before init (BZ#24602)")
Cc: stable@vger.kernel.org
Signed-off-by: Praneeth Balasubramanian <praneethb2002@gmail.com>
---

Notes:
    This was found by a compiler-based check, part of research at UCSB SecLab.
    The locking rule stated in the kernel-doc comment of the called function
    was turned into a Clang context-analysis requirement (__must_hold) on that
    function, and Clang 23 with CONFIG_WARN_CONTEXT_ANALYSIS then checked every
    caller. An LLM was used only to turn the comment into a machine-readable
    rule; the report itself comes from the compiler. The patch and changelog
    were drafted with Claude Opus 5.5. I reviewed the patch and take
    responsibility for it.
    
    Testing: builds without new warnings with W=1 (x86_64, clang 23,
    allmodconfig). Not runtime-tested; I do not have this hardware, and I
    have not observed the race myself.

 drivers/media/pci/bt8xx/bttv-driver.c | 1 +
 drivers/media/pci/bt8xx/bttv-vbi.c    | 8 --------
 2 files changed, 1 insertion(+), 8 deletions(-)

diff --git a/drivers/media/pci/bt8xx/bttv-driver.c b/drivers/media/pci/bt8xx/bttv-driver.c
index 8b9ca59f9434..a9d5487cd5f7 100644
--- a/drivers/media/pci/bt8xx/bttv-driver.c
+++ b/drivers/media/pci/bt8xx/bttv-driver.c
@@ -3078,6 +3078,7 @@ static int vdev_init(struct bttv *btv, struct video_device *vfd,
 	struct vb2_queue *q;
 	*vfd = *template;
 	vfd->v4l2_dev = &btv->c.v4l2_dev;
+	vfd->lock = &btv->lock;
 	vfd->release = video_device_release_empty;
 	video_set_drvdata(vfd, btv);
 	snprintf(vfd->name, sizeof(vfd->name), "BT%d%s %s (%s)",
diff --git a/drivers/media/pci/bt8xx/bttv-vbi.c b/drivers/media/pci/bt8xx/bttv-vbi.c
index 0ca88a2400ee..c2ceda4a15a9 100644
--- a/drivers/media/pci/bt8xx/bttv-vbi.c
+++ b/drivers/media/pci/bt8xx/bttv-vbi.c
@@ -247,13 +247,9 @@ int bttv_try_fmt_vbi_cap(struct file *file, void *priv, struct v4l2_format *frt)
 	const struct bttv_tvnorm *tvnorm;
 	__s32 crop_start;
 
-	mutex_lock(&btv->lock);
-
 	tvnorm = &bttv_tvnorms[btv->tvnorm];
 	crop_start = btv->crop_start;
 
-	mutex_unlock(&btv->lock);
-
 	return try_fmt(&frt->fmt.vbi, tvnorm, crop_start);
 }
 
@@ -265,8 +261,6 @@ int bttv_s_fmt_vbi_cap(struct file *file, void *priv, struct v4l2_format *frt)
 	__s32 start1, end;
 	int rc;
 
-	mutex_lock(&btv->lock);
-
 	rc = -EBUSY;
 	if (btv->resources & RESOURCE_VBI)
 		goto fail;
@@ -295,8 +289,6 @@ int bttv_s_fmt_vbi_cap(struct file *file, void *priv, struct v4l2_format *frt)
 	rc = 0;
 
  fail:
-	mutex_unlock(&btv->lock);
-
 	return rc;
 }
 
-- 
2.25.1


^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2026-12-04 18:49 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-04 18:49 [PATCH] media: bttv: Let the V4L2 core serialize the ioctls with btv->lock Praneeth Balasubramanian

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox