Skip to contentSkip to conversation

] videobuf2: Add missing lock held on vb2_fop_relase

PATCH Superseded Ricardo Ribalda Delgado 2 participants 3 messages Oct 31, 2013 – Nov 2, 2013

Subsystems (3)

v3 of 8 Superseded Latest is v8 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 ] videobuf2: Add missing lock held on vb2_fop_relase

From: Ricardo Ribalda <[email protected]>

vb2_fop_relase does not held the lock although it is modifying the queue->owner field.

This could lead to race conditions on the vb2_perform_io function when multiple applications are accessing the video device via read/write API:

[ 308.297741] BUG: unable to handle kernel NULL pointer dereference at
0000000000000260
[ 308.297759] IP: [<ffffffffa07a9fd2>] vb2_perform_fileio+0x372/0x610
[videobuf2_core]
[ 308.297794] PGD 159719067 PUD 158119067 PMD 0
[ 308.297812] Oops: 0000 #1 SMP
[ 308.297826] Modules linked in: qt5023_video videobuf2_dma_sg
qtec_xform videobuf2_vmalloc videobuf2_memops videobuf2_core
qtec_white qtec_mem gpio_xilinx qtec_cmosis qtec_pcie fglrx(PO)
spi_xilinx spi_bitbang qt5023
[ 308.297888] CPU: 1 PID: 2189 Comm: java Tainted: P O 3.11.0-qtec-standard #1
[ 308.297919] Hardware name: QTechnology QT5022/QT5022, BIOS
PM_2.1.0.309 X64 05/23/2013
[ 308.297952] task: ffff8801564e1690 ti: ffff88014dc02000 task.ti:
ffff88014dc02000
[ 308.297962] RIP: 0010:[<ffffffffa07a9fd2>] [<ffffffffa07a9fd2>]
vb2_perform_fileio+0x372/0x610 [videobuf2_core]
[ 308.297985] RSP: 0018:ffff88014dc03df8 EFLAGS: 00010202
[ 308.297995] RAX: 0000000000000000 RBX: ffff880158a23000 RCX: dead000000100100
[ 308.298003] RDX: 0000000000000000 RSI: dead000000200200 RDI: 0000000000000000
[ 308.298012] RBP: ffff88014dc03e58 R08: 0000000000000000 R09: 0000000000000001
[ 308.298020] R10: ffffea00051e8380 R11: ffff88014dc03fd8 R12: ffff880158a23070
[ 308.298029] R13: ffff8801549040b8 R14: 0000000000198000 R15: 0000000001887e60
[ 308.298040] FS: 00007f65130d5700(0000) GS:ffff88015ed00000(0000)
knlGS:0000000000000000
[ 308.298049] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033
[ 308.298057] CR2: 0000000000000260 CR3: 0000000159630000 CR4: 00000000000007e0
[ 308.298064] Stack:
[ 308.298071] ffff880156416c00 0000000000198000 0000000000000000
ffff880100000001
[ 308.298087] ffff88014dc03f50 00000000810a79ca 0002000000000001
ffff880154904718
[ 308.298101] ffff880156416c00 0000000000198000 ffff880154904338
ffff88014dc03f50
[ 308.298116] Call Trace:
[ 308.298143] [<ffffffffa07aa3c4>] vb2_read+0x14/0x20 [videobuf2_core]
[ 308.298198] [<ffffffffa07aa494>] vb2_fop_read+0xc4/0x120 [videobuf2_core]
[ 308.298252] [<ffffffff8154ee9e>] v4l2_read+0x7e/0xc0
[ 308.298296] [<ffffffff8116e639>] vfs_read+0xa9/0x160
[ 308.298312] [<ffffffff8116e882>] SyS_read+0x52/0xb0
[ 308.298328] [<ffffffff81784179>] tracesys+0xd0/0xd5
[ 308.298335] Code: e5 d6 ff ff 83 3d be 24 00 00 04 89 c2 4c 8b 45 b0
44 8b 4d b8 0f 8f 20 02 00 00 85 d2 75 32 83 83 78 03 00 00 01 4b 8b
44 c5 48 <8b> 88 60 02 00 00 85 c9 0f 84 b0 00 00 00 8b 40 58 89 c2 41
89
[ 308.298487] RIP [<ffffffffa07a9fd2>] vb2_perform_fileio+0x372/0x610
[videobuf2_core]
[ 308.298507] RSP <ffff88014dc03df8>
[ 308.298514] CR2: 0000000000000260
[ 308.298526] ---[ end trace e8f01717c96d1e41 ]---

v2: Add bug found by Sylvester Nawrocki

fimc-capture and fimc-lite where calling vb2_fop_release with the lock held. Therefore a new __vb2_fop_release function has been created to be used by drivers that overload the release function.

v3: Comments by Sylvester Nawrocki and Mauro Carvalho Chehab

Use vb2_fop_release_locked instead of __vb2_fop_release

---
 drivers/media/platform/exynos4-is/fimc-capture.c |  2 +-
 drivers/media/platform/exynos4-is/fimc-lite.c    |  2 +-
 drivers/media/v4l2-core/videobuf2-core.c         | 24 +++++++++++++++++++++++-
 include/media/videobuf2-core.h                   |  1 +
 4 files changed, 26 insertions(+), 3 deletions(-)
drivers/media/platform/exynos4-is/fimc-capture.c +1 -1
index fb27ff7..c3c3b3b 100644
@@ -549,7 +549,7 @@ static int fimc_capture_release(struct file *file)
 		vc->streaming = false;
 	}
 
-	ret = vb2_fop_release(file);
+	ret = vb2_fop_release_locked(file);
 
 	if (close) {
 		clear_bit(ST_CAPT_BUSY, &fimc->state);
drivers/media/platform/exynos4-is/fimc-lite.c +1 -1
index e5798f7..b8d417f 100644
@@ -546,7 +546,7 @@ static int fimc_lite_release(struct file *file)
 		mutex_unlock(&entity->parent->graph_mutex);
 	}
 
-	vb2_fop_release(file);
+	vb2_fop_release_locked(file);
 	pm_runtime_put(&fimc->pdev->dev);
 	clear_bit(ST_FLITE_SUSPENDED, &fimc->state);
 
drivers/media/v4l2-core/videobuf2-core.c +23 -1
index 594c75e..06e6dbd 100644
@@ -2619,18 +2619,40 @@ int vb2_fop_mmap(struct file *file, struct vm_area_struct *vma)
 }
 EXPORT_SYMBOL_GPL(vb2_fop_mmap);
 
-int vb2_fop_release(struct file *file)
+int __vb2_fop_release(struct file *file, bool lock_is_held)
 {
 	struct video_device *vdev = video_devdata(file);
+	struct mutex *lock;
 
 	if (file->private_data == vdev->queue->owner) {
+		if (lock_is_held)
+			lock = NULL;
+		else
+			lock = vdev->queue->lock ?
+				vdev->queue->lock : vdev->lock;
+		if (lock)
+			mutex_lock(lock);
 		vb2_queue_release(vdev->queue);
 		vdev->queue->owner = NULL;
+		if (lock)
+			mutex_unlock(lock);
 	}
 	return v4l2_fh_release(file);
 }
+EXPORT_SYMBOL_GPL(__vb2_fop_release);
+
+int vb2_fop_release(struct file *file)
+{
+	return __vb2_fop_release(file, false);
+}
 EXPORT_SYMBOL_GPL(vb2_fop_release);
 
+int vb2_fop_release_locked(struct file *file)
+{
+	return __vb2_fop_release(file, true);
+}
+EXPORT_SYMBOL_GPL(vb2_fop_release_locked);
+
 ssize_t vb2_fop_write(struct file *file, char __user *buf,
 		size_t count, loff_t *ppos)
 {
include/media/videobuf2-core.h +1 -0
index 6781258..49a6a06 100644
@@ -491,6 +491,7 @@ int vb2_ioctl_expbuf(struct file *file, void *priv,
 
 int vb2_fop_mmap(struct file *file, struct vm_area_struct *vma);
 int vb2_fop_release(struct file *file);
+int vb2_fop_release_locked(struct file *file);
 ssize_t vb2_fop_write(struct file *file, char __user *buf,
 		size_t count, loff_t *ppos);
 ssize_t vb2_fop_read(struct file *file, char __user *buf,
Signature · 1 line
1.8.4.rc3
#1 lore raw

PATCH v3 1/1 ] videobuf2: Add missing lock held on vb2_fop_relase

Hi Ricardo,

Quoted 8 lines · Ricardo Ribalda Delgado

From: Ricardo Ribalda<[email protected]>

vb2_fop_relase does not held the lock although it is modifying the queue->owner field.

This could lead to race conditions on the vb2_perform_io function when multiple applications are accessing the video device via read/write API:

[...]

Quoted 1 line

v2: Add bug found by Sylvester Nawrocki

"v2: Add fix for a bug found..." ? :)

Quoted 7 lines

fimc-capture and fimc-lite where calling vb2_fop_release with the lock held. Therefore a new __vb2_fop_release function has been created to be used by drivers that overload the release function.

v3: Comments by Sylvester Nawrocki and Mauro Carvalho Chehab

Use vb2_fop_release_locked instead of __vb2_fop_release

Such notes normally go after the scissors line ("---") after Signed-off-by lines.

Quoted 2 lines

Is this duplication really needed ?

Quoted 13 lines

---

  drivers/media/platform/exynos4-is/fimc-capture.c |  2 +-
  drivers/media/platform/exynos4-is/fimc-lite.c    |  2 +-
  drivers/media/v4l2-core/videobuf2-core.c         | 24 +++++++++++++++++++++++-
  include/media/videobuf2-core.h                   |  1 +
  4 files changed, 26 insertions(+), 3 deletions(-)
drivers/media/platform/exynos4-is/fimc-capture.c +0 -0
index fb27ff7..c3c3b3b 100644

drivers/media/platform/exynos4-is/fimc-capture.c static int fimc_capture_release(struct file *file) · line 552

Quoted 5 lines
drivers/media/platform/exynos4-is/fimc-capture.c +1 -1
@@ -549,7 +549,7 @@ static int fimc_capture_release(struct file *file)
  		vc->streaming = false;
  	}
 
-	ret = vb2_fop_release(file);
+	ret = vb2_fop_release_locked(file);

I'm personally not happy with such a change. It is still not obvious if "locked" means that this function takes the lock internally or it should be called with the lock held. How about sticking to the common practice and instead naming it __vb2_fop_release() ?

Quoted 35 lines
drivers/media/platform/exynos4-is/fimc-capture.c +0 -0
  	if (close) {
  		clear_bit(ST_CAPT_BUSY,&fimc->state);
drivers/media/platform/exynos4-is/fimc-lite.c +1 -1
index e5798f7..b8d417f 100644
@@ -546,7 +546,7 @@ static int fimc_lite_release(struct file *file)
  		mutex_unlock(&entity->parent->graph_mutex);
  	}
 
-	vb2_fop_release(file);
+	vb2_fop_release_locked(file);
  	pm_runtime_put(&fimc->pdev->dev);
  	clear_bit(ST_FLITE_SUSPENDED,&fimc->state);
 
drivers/media/v4l2-core/videobuf2-core.c +7 -1
index 594c75e..06e6dbd 100644
@@ -2619,18 +2619,40 @@ int vb2_fop_mmap(struct file *file, struct vm_area_struct *vma)
  }
  EXPORT_SYMBOL_GPL(vb2_fop_mmap);
 
-int vb2_fop_release(struct file *file)
+int __vb2_fop_release(struct file *file, bool lock_is_held)
  {
  	struct video_device *vdev = video_devdata(file);
+	struct mutex *lock;
 
  	if (file->private_data == vdev->queue->owner) {
+		if (lock_is_held)
+			lock = NULL;
+		else
+			lock = vdev->queue->lock ?
+				vdev->queue->lock : vdev->lock;

drivers/media/v4l2-core/videobuf2-core.c int vb2_fop_mmap(struct file *file, struct vm_area_struct *vma) · line 2642

Quoted 10 lines
drivers/media/v4l2-core/videobuf2-core.c +5 -0
+		if (lock)
+			mutex_lock(lock);
  		vb2_queue_release(vdev->queue);
  		vdev->queue->owner = NULL;
+		if (lock)
+			mutex_unlock(lock);
  	}
  	return v4l2_fh_release(file);
  }
+EXPORT_SYMBOL_GPL(__vb2_fop_release);

We don't need to export this function, do we ?

drivers/media/v4l2-core/videobuf2-core.c int vb2_fop_mmap(struct file *file, struct vm_area_struct *vma) · line 2654

Quoted 11 lines
drivers/media/v4l2-core/videobuf2-core.c +9 -0
+int vb2_fop_release(struct file *file)
+{
+	return __vb2_fop_release(file, false);
+}
  EXPORT_SYMBOL_GPL(vb2_fop_release);
 
+int vb2_fop_release_locked(struct file *file)
+{
+	return __vb2_fop_release(file, true);
+}
+EXPORT_SYMBOL_GPL(vb2_fop_release_locked);

--
Thanks,
Sylwester

#2 lore raw

PATCH v3 1/1 ] videobuf2: Add missing lock held on vb2_fop_relase

Hello Sylwester

Thanks for your comments. There is a new patch: v4! :)

Quoted 14 lines · Sylwester Nawrocki

Hi Ricardo,

Quoted 9 lines · Ricardo Ribalda Delgado

From: Ricardo Ribalda<[email protected]>

vb2_fop_relase does not held the lock although it is modifying the queue->owner field.

This could lead to race conditions on the vb2_perform_io function when multiple applications are accessing the video device via read/write API:

Quoted 6 lines

[...]

Quoted 1 line

v2: Add bug found by Sylvester Nawrocki

"v2: Add fix for a bug found..." ? :)

In Spanish it makes sense. it is fixed now, thanks

Quoted 7 lines
Quoted 7 lines

fimc-capture and fimc-lite where calling vb2_fop_release with the lock held.
Therefore a new __vb2_fop_release function has been created to be used by drivers that overload the release function.

Quoted 7 lines
Quoted 3 lines

v3: Comments by Sylvester Nawrocki and Mauro Carvalho Chehab

Use vb2_fop_release_locked instead of __vb2_fop_release

Such notes normally go after the scissors line ("---") after Signed-off-by lines.

Fixed, thanks!

Quoted 7 lines
Quoted 4 lines

Is this duplication really needed ?

I have different slightly different git configuration in 2 computers. Fixed now

Quoted 21 lines
Quoted 21 lines

---

  drivers/media/platform/exynos4-is/fimc-capture.c |  2 +-
  drivers/media/platform/exynos4-is/fimc-lite.c    |  2 +-
  drivers/media/v4l2-core/videobuf2-core.c         | 24
+++++++++++++++++++++++-
  include/media/videobuf2-core.h                   |  1 +
  4 files changed, 26 insertions(+), 3 deletions(-)
drivers/media/platform/exynos4-is/fimc-capture.c +0 -0
index fb27ff7..c3c3b3b 100644
@@ -549,7 +549,7 @@ static int fimc_capture_release(struct file *file)
                vc->streaming = false;
        }
 
Quoted 8 lines

drivers/media/platform/exynos4-is/fimc-capture.c static int fimc_capture_release(struct file *file) · line 552

Quoted 2 lines
drivers/media/platform/exynos4-is/fimc-capture.c +1 -1
-       ret = vb2_fop_release(file);
+       ret = vb2_fop_release_locked(file);

I'm personally not happy with such a change. It is still not obvious if "locked" means that this function takes the lock internally or it should be called with the lock held. How about sticking to the common practice and instead naming it __vb2_fop_release() ?

I like the locked prefix, but it is a lost war :P. New version is named as __

Quoted 46 lines
Quoted 46 lines
drivers/media/platform/exynos4-is/fimc-capture.c +0 -0
        if (close) {
                clear_bit(ST_CAPT_BUSY,&fimc->state);
 
drivers/media/platform/exynos4-is/fimc-lite.c +1 -1
index e5798f7..b8d417f 100644
@@ -546,7 +546,7 @@ static int fimc_lite_release(struct file *file)
                mutex_unlock(&entity->parent->graph_mutex);
        }
 
-       vb2_fop_release(file);
+       vb2_fop_release_locked(file);
        pm_runtime_put(&fimc->pdev->dev);
        clear_bit(ST_FLITE_SUSPENDED,&fimc->state);
 
drivers/media/v4l2-core/videobuf2-core.c +10 -1
index 594c75e..06e6dbd 100644
@@ -2619,18 +2619,40 @@ int vb2_fop_mmap(struct file *file, struct
 vm_area_struct *vma)
  }
  EXPORT_SYMBOL_GPL(vb2_fop_mmap);
 
-int vb2_fop_release(struct file *file)
+int __vb2_fop_release(struct file *file, bool lock_is_held)
  {
        struct video_device *vdev = video_devdata(file);
+       struct mutex *lock;
 
        if (file->private_data == vdev->queue->owner) {
+               if (lock_is_held)
+                       lock = NULL;
+               else
+                       lock = vdev->queue->lock ?
+                               vdev->queue->lock : vdev->lock;
+               if (lock)
+                       mutex_lock(lock);
                vb2_queue_release(vdev->queue);
                vdev->queue->owner = NULL;
+               if (lock)
Quoted 8 lines

drivers/media/v4l2-core/videobuf2-core.c int vb2_fop_mmap(struct file *file, struct vm_area_struct *vma) · line 2642

Quoted 5 lines
drivers/media/v4l2-core/videobuf2-core.c +2 -0
+                       mutex_unlock(lock);
        }
        return v4l2_fh_release(file);
  }
+EXPORT_SYMBOL_GPL(__vb2_fop_release);

We don't need to export this function, do we ?

Not really. Fixed

Quoted 8 lines
Quoted 8 lines
drivers/media/v4l2-core/videobuf2-core.c +4 -0
+int vb2_fop_release(struct file *file)
+{
+       return __vb2_fop_release(file, false);
+}
  EXPORT_SYMBOL_GPL(vb2_fop_release);
 
Quoted 10 lines

drivers/media/v4l2-core/videobuf2-core.c int vb2_fop_mmap(struct file *file, struct vm_area_struct *vma) · line 2654

Quoted 5 lines
drivers/media/v4l2-core/videobuf2-core.c +5 -0
+int vb2_fop_release_locked(struct file *file)
+{
+       return __vb2_fop_release(file, true);
+}
+EXPORT_SYMBOL_GPL(vb2_fop_release_locked);

--
Thanks,
Sylwester

Thanks!

Signature · 1 line
Ricardo Ribalda