Skip to contentSkip to conversation

media: am437x-vpfe: Fix a race condition during release

PATCH Superseded Benoit Parrot 3 participants 3 messages Jun 29, 2015 – Jul 13, 2015

Subsystems (2)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 media: am437x-vpfe: Fix a race condition during release

There was a race condition where during cleanup/release operation on-going streaming would cause a kernel panic because the hardware module was disabled prematurely with IRQ still pending.

Fixes 417d2e507edc ("[media] media: platform: add VPFE capture driver support for AM437X")
Cc <[email protected]> # v4.0+

---
Changes since v2:
- fix the stable commit reference syntax

 drivers/media/platform/am437x/am437x-vpfe.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
drivers/media/platform/am437x/am437x-vpfe.c +9 -2
index a30cc2f7e4f1..eb25c43da126 100644
@@ -1185,14 +1185,21 @@ static int vpfe_initialize_device(struct vpfe_device *vpfe)
 static int vpfe_release(struct file *file)
 {
 	struct vpfe_device *vpfe = video_drvdata(file);
+	bool fh_singular = v4l2_fh_is_singular_file(file);
 	int ret;
 
 	mutex_lock(&vpfe->lock);
 
-	if (v4l2_fh_is_singular_file(file))
-		vpfe_ccdc_close(&vpfe->ccdc, vpfe->pdev);
+	/* the release helper will cleanup any on-going streaming */
 	ret = _vb2_fop_release(file, NULL);
 
+	/*
+	 * If this was the last open file.
+	 * Then de-initialize hw module.
+	 */
+	if (fh_singular)
+		vpfe_ccdc_close(&vpfe->ccdc, vpfe->pdev);
+
 	mutex_unlock(&vpfe->lock);
 
 	return ret;
Signature · 1 line
1.8.5.1
Acked-by on v3 1/1
#1 lore raw
Quoted 7 lines · Benoit Parrot

There was a race condition where during cleanup/release operation on-going streaming would cause a kernel panic because the hardware module was disabled prematurely with IRQ still pending.

Fixes 417d2e507edc ("[media] media: platform: add VPFE capture driver support for AM437X")
Cc <[email protected]> # v4.0+

Cheers,
--Prabhakar Lad

Quoted 38 lines

---
Changes since v2:
- fix the stable commit reference syntax

 drivers/media/platform/am437x/am437x-vpfe.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
drivers/media/platform/am437x/am437x-vpfe.c +9 -3
index a30cc2f7e4f1..eb25c43da126 100644
@@ -1185,14 +1185,21 @@ static int vpfe_initialize_device(struct vpfe_device *vpfe)
 static int vpfe_release(struct file *file)
 {
        struct vpfe_device *vpfe = video_drvdata(file);
+       bool fh_singular = v4l2_fh_is_singular_file(file);
        int ret;
 
        mutex_lock(&vpfe->lock);
 
-       if (v4l2_fh_is_singular_file(file))
-               vpfe_ccdc_close(&vpfe->ccdc, vpfe->pdev);
+       /* the release helper will cleanup any on-going streaming */
        ret = _vb2_fop_release(file, NULL);
 
+       /*
+        * If this was the last open file.
+        * Then de-initialize hw module.
+        */
+       if (fh_singular)
+               vpfe_ccdc_close(&vpfe->ccdc, vpfe->pdev);
+
        mutex_unlock(&vpfe->lock);
 
        return ret;
--

1.8.5.1

#2 lore raw
In reply to Benoit Parrot
Quoted 19 lines · Benoit Parrot

There was a race condition where during cleanup/release operation on-going streaming would cause a kernel panic because the hardware module was disabled prematurely with IRQ still pending.

Fixes 417d2e507edc ("[media] media: platform: add VPFE capture driver support for AM437X")
Cc <[email protected]> # v4.0+

---
Changes since v2:
- fix the stable commit reference syntax

 drivers/media/platform/am437x/am437x-vpfe.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
drivers/media/platform/am437x/am437x-vpfe.c +0 -0
index a30cc2f7e4f1..eb25c43da126 100644

drivers/media/platform/am437x/am437x-vpfe.c static int vpfe_initialize_device(struct vpfe_device *vpfe) · line 1188

Quoted 4 lines
drivers/media/platform/am437x/am437x-vpfe.c +1 -0
@@ -1185,14 +1185,21 @@ static int vpfe_initialize_device(struct vpfe_device *vpfe)
 static int vpfe_release(struct file *file)
 {
 	struct vpfe_device *vpfe = video_drvdata(file);
+	bool fh_singular = v4l2_fh_is_singular_file(file);

Close, but no cigar.

The assignment to fh_singular should be moved inside the mutex. Right now there still is a race condition between setting fh_singular and taking the lock.

Regards,

Hans

Quoted 20 lines
drivers/media/platform/am437x/am437x-vpfe.c +8 -2
 	int ret;
 
 	mutex_lock(&vpfe->lock);
 
-	if (v4l2_fh_is_singular_file(file))
-		vpfe_ccdc_close(&vpfe->ccdc, vpfe->pdev);
+	/* the release helper will cleanup any on-going streaming */
 	ret = _vb2_fop_release(file, NULL);
 
+	/*
+	 * If this was the last open file.
+	 * Then de-initialize hw module.
+	 */
+	if (fh_singular)
+		vpfe_ccdc_close(&vpfe->ccdc, vpfe->pdev);
+
 	mutex_unlock(&vpfe->lock);
 
 	return ret;