media: bt8xx: Fix a possible data race in buffer_queue()
Subsystems (2)
PATCH 1/1 media: bt8xx: Fix a possible data race in buffer_queue()
The variable btv->loop_irq is often protected by the lock btv->s_lock when is accessed. Here is an example in bttv_irq_timeout():
spin_lock_irqsave(&btv->s_lock,flags); ... btv->loop_irq = 0; ... spin_unlock_irqrestore(&btv->s_lock,flags);
However, it is accessed without holding the lock btv->s_lock in buffer_queue():
btv->loop_irq |= 1;
And thus a data race can occur.
To fix this possible data race, a lock and unlock pair is added when accessing the variable btv->loop_irq in buffer_queue().
--- drivers/media/pci/bt8xx/bttv-driver.c | 3 +++ 1 file changed, 3 insertions(+)
drivers/media/pci/bt8xx/bttv-driver.c +3 -0
@@ -1617,13 +1617,16 @@ buffer_queue(struct videobuf_queue *q, struct videobuf_buffer *vb) struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);struct bttv_fh *fh = q->priv_data;struct bttv *btv = fh->btv; +unsigned long flags; +spin_lock_irqsave (&btv->s_lock,flags); buf->vb.state =VIDEOBUF_QUEUED ;list_add_tail (&buf->vb.queue,&btv->capture);if (!btv->curr.frame_irq) { btv->loop_irq |=1 ;bttv_set_dma (btv,0x03 ); } +spin_unlock_irqrestore (&btv->s_lock,flags); }static void buffer_release (struct videobuf_queue *q,struct videobuf_buffer *vb)
Signature · 1 line
2.34.1
Quoted 9 lines · Tuo Li
The variable
btv->loop_irqis often protected by the lockbtv->s_lockwhen is accessed. Here is an example inbttv_irq_timeout():spin_lock_irqsave(&btv->s_lock,flags); ... btv->loop_irq = 0; ... spin_unlock_irqrestore(&btv->s_lock,flags);
Quoted 8 lines
However, it is accessed without holding the lock
btv->s_lockinbuffer_queue():btv->loop_irq |= 1;And thus a data race can occur.
To fix this possible data race, a lock and unlock pair is added when accessing the variablebtv->loop_irqinbuffer_queue().
hmm I am not sure why buffer_queue() didn't bother holding the btv->s_lock but in the new version of the bttv driver that I have been working on, the btv->s_lock is always held before changing the loop_irq value.
https://patchwork.linuxtv.org/project/linux-media/cover/[email protected]/
When the bttv driver is converted to vb2, and the work is almost done, the old buffer_queue() is totally eliminated, so I don't think it would make sense to apply this patch now.
Quoted 31 lines
Reported-by BassCheck <[email protected]>Signed-off-by Tuo Li <[email protected]>--- drivers/media/pci/bt8xx/bttv-driver.c | 3 +++ 1 file changed, 3 insertions(+)drivers/media/pci/bt8xx/bttv-driver.c +3
-0@@ -1617,13 +1617,16 @@ buffer_queue(struct videobuf_queue *q, struct videobuf_buffer *vb) struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);struct bttv_fh *fh = q->priv_data;struct bttv *btv = fh->btv; +unsigned long flags; +spin_lock_irqsave (&btv->s_lock,flags); buf->vb.state =VIDEOBUF_QUEUED ;list_add_tail (&buf->vb.queue,&btv->capture);if (!btv->curr.frame_irq) { btv->loop_irq |=1 ;bttv_set_dma (btv,0x03 ); } +spin_unlock_irqrestore (&btv->s_lock,flags); }static void buffer_release (struct videobuf_queue *q,struct videobuf_buffer *vb)Signature · 1 line
2.34.1