Skip to contentSkip to conversation

media: davinci: vpif_capture: fix memory leak in vpif_probe()

PATCH In review Young Xiao 2 participants 3 messages May 29, 2019 – Jun 4, 2019 Media (V4L/DVB)

lore
Share & tools
lore

Young Xiao

author
#0 lore raw

PATCH 1/1 media: davinci: vpif_capture: fix memory leak in vpif_probe()

If vpif_probe() fails on vpif_probe_complete(), then memory allocated at initialize_vpif() for global vpif_obj.dev[i] become unreleased.

The patch adds deallocation of vpif_obj.dev[i] on the error path.

---
 drivers/media/platform/davinci/vpif_capture.c | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)
drivers/media/platform/davinci/vpif_capture.c +14 -2
index b5aacb0..63e6ec4 100644
@@ -1621,6 +1621,14 @@ vpif_capture_get_pdata(struct platform_device *pdev)
 	return NULL;
 }
 
+static void free_vpif_objs(void)
+{
+	int i;
+
+	for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++)
+		kfree(vpif_obj.dev[i]);
+}
+
 /**
  * vpif_probe : This function probes the vpif capture driver
  * @pdev: platform device pointer
@@ -1701,7 +1709,10 @@ static __init int vpif_probe(struct platform_device *pdev)
 				  "registered sub device %s\n",
 				   subdevdata->name);
 		}
-		vpif_probe_complete();
+		err = vpif_probe_complete();
+		if (err) {
+			goto probe_subdev_out;
+		}
 	} else {
 		vpif_obj.notifier.ops = &vpif_async_ops;
 		err = v4l2_async_notifier_register(&vpif_obj.v4l2_dev,
@@ -1722,6 +1733,7 @@ static __init int vpif_probe(struct platform_device *pdev)
 	v4l2_device_unregister(&vpif_obj.v4l2_dev);
 cleanup:
 	v4l2_async_notifier_cleanup(&vpif_obj.notifier);
+	free_vpif_objs();
 
 	return err;
 }
@@ -1748,8 +1760,8 @@ static int vpif_remove(struct platform_device *device)
 		ch = vpif_obj.dev[i];
 		/* Unregister video device */
 		video_unregister_device(&ch->video_dev);
-		kfree(vpif_obj.dev[i]);
 	}
+	free_vpif_objs()
 	return 0;
 }
 
Signature · 1 line
2.7.4
#1 lore raw
Quoted 31 lines · Young Xiao

If vpif_probe() fails on vpif_probe_complete(), then memory allocated at initialize_vpif() for global vpif_obj.dev[i] become unreleased.

The patch adds deallocation of vpif_obj.dev[i] on the error path.

---
 drivers/media/platform/davinci/vpif_capture.c | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)
drivers/media/platform/davinci/vpif_capture.c +8 -0
index b5aacb0..63e6ec4 100644
@@ -1621,6 +1621,14 @@ vpif_capture_get_pdata(struct platform_device *pdev)
 	return NULL;
 }
 
+static void free_vpif_objs(void)
+{
+	int i;
+
+	for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++)
+		kfree(vpif_obj.dev[i]);
+}
+
 /**
  * vpif_probe : This function probes the vpif capture driver
  * @pdev: platform device pointer

drivers/media/platform/davinci/vpif_capture.c static __init int vpif_probe(struct platform_device *pdev) · line 1715

Quoted 8 lines
drivers/media/platform/davinci/vpif_capture.c +4 -1
@@ -1701,7 +1709,10 @@ static __init int vpif_probe(struct platform_device *pdev)
 				  "registered sub device %s\n",
 				   subdevdata->name);
 		}
-		vpif_probe_complete();
+		err = vpif_probe_complete();
+		if (err) {
+			goto probe_subdev_out;
+		}

No need for { and } as per kernel coding style.

drivers/media/platform/davinci/vpif_capture.c static __init int vpif_probe(struct platform_device *pdev) · line 1736

Quoted 8 lines
drivers/media/platform/davinci/vpif_capture.c +1 -0
 	} else {
 		vpif_obj.notifier.ops = &vpif_async_ops;
 		err = v4l2_async_notifier_register(&vpif_obj.v4l2_dev,
@@ -1722,6 +1733,7 @@ static __init int vpif_probe(struct platform_device *pdev)
 	v4l2_device_unregister(&vpif_obj.v4l2_dev);
 cleanup:
 	v4l2_async_notifier_cleanup(&vpif_obj.notifier);
+	free_vpif_objs();

This would break the case where initialize_vpif() returns an error, since initialize_vpif already frees these objects on error.

In this case the easiest way of doing this is to just return if initialize_vpif returns an error. No need to clean up anything.

Regards,

Hans

Quoted 14 lines
drivers/media/platform/davinci/vpif_capture.c +1 -1
 	return err;
 }
@@ -1748,8 +1760,8 @@ static int vpif_remove(struct platform_device *device)
 		ch = vpif_obj.dev[i];
 		/* Unregister video device */
 		video_unregister_device(&ch->video_dev);
-		kfree(vpif_obj.dev[i]);
 	}
+	free_vpif_objs()
 	return 0;
 }
 
author
#2 lore raw

Yes, you are correct. I will fix the issue and resubmit the patch again.

Quoted 78 lines · Hans Verkuil
Quoted 31 lines · Young Xiao

If vpif_probe() fails on vpif_probe_complete(), then memory allocated at initialize_vpif() for global vpif_obj.dev[i] become unreleased.

The patch adds deallocation of vpif_obj.dev[i] on the error path.

---
 drivers/media/platform/davinci/vpif_capture.c | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)
drivers/media/platform/davinci/vpif_capture.c +8 -0
index b5aacb0..63e6ec4 100644
@@ -1621,6 +1621,14 @@ vpif_capture_get_pdata(struct platform_device *pdev)
      return NULL;
 }
 
+static void free_vpif_objs(void)
+{
+     int i;
+
+     for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++)
+             kfree(vpif_obj.dev[i]);
+}
+
 /**
  * vpif_probe : This function probes the vpif capture driver
  * @pdev: platform device pointer

drivers/media/platform/davinci/vpif_capture.c static __init int vpif_probe(struct platform_device *pdev) · line 1715

Quoted 8 lines
drivers/media/platform/davinci/vpif_capture.c +4 -1
@@ -1701,7 +1709,10 @@ static __init int vpif_probe(struct platform_device *pdev)
                                "registered sub device %s\n",
                                 subdevdata->name);
              }
-             vpif_probe_complete();
+             err = vpif_probe_complete();
+             if (err) {
+                     goto probe_subdev_out;
+             }

No need for { and } as per kernel coding style.

drivers/media/platform/davinci/vpif_capture.c static __init int vpif_probe(struct platform_device *pdev) · line 1736

Quoted 8 lines
drivers/media/platform/davinci/vpif_capture.c +1 -0
      } else {
              vpif_obj.notifier.ops = &vpif_async_ops;
              err = v4l2_async_notifier_register(&vpif_obj.v4l2_dev,
@@ -1722,6 +1733,7 @@ static __init int vpif_probe(struct platform_device *pdev)
      v4l2_device_unregister(&vpif_obj.v4l2_dev);
 cleanup:
      v4l2_async_notifier_cleanup(&vpif_obj.notifier);
+     free_vpif_objs();

This would break the case where initialize_vpif() returns an error, since initialize_vpif already frees these objects on error.

In this case the easiest way of doing this is to just return if initialize_vpif returns an error. No need to clean up anything.

Regards,

Hans

Quoted 14 lines
drivers/media/platform/davinci/vpif_capture.c +1 -1
      return err;
 }
@@ -1748,8 +1760,8 @@ static int vpif_remove(struct platform_device *device)
              ch = vpif_obj.dev[i];
              /* Unregister video device */
              video_unregister_device(&ch->video_dev);
-             kfree(vpif_obj.dev[i]);
      }
+     free_vpif_objs()
      return 0;
 }