Skip to contentSkip to conversation

+ fix-several-memory-leaks-in-cr_backlight_probe.patch added to -mm tree

PATCH RESEND New [email protected] 2 participants 2 messages Jul 20, 2007 – Aug 4, 2007 1 message not in the archive

Subsystems (2)
lore
Share & tools
lore

[email protected]

author tree notice
#0 lore raw

The patch titled
Fix several memory leaks in cr_backlight_probe()
has been added to the -mm tree. Its filename is
fix-several-memory-leaks-in-cr_backlight_probe.patch

*** Remember to use Documentation/SubmitChecklist when testing your code ***

See http://www.zip.com.au/~akpm/linux/patches/stuff/added-to-mm.txt to find out what to do about this

------------------------------------------------------
Subject: Fix several memory leaks in cr_backlight_probe()
From: Jesper Juhl <[email protected]>

After fixing the too small memory allocation in cr_backlight_probe() from drivers/video/backlight/cr_bllcd.c
(commit e3bbb3f05339de438faf54124f25c92e6fe4ac2e) I noticed that the Coverity checker also thought there were a few memory leaks in there. I took a closer look and confirmed that there were indeed several leaks.

At the start of the function we allocate storage for a 'struct cr_panel' and store the pointer in a variable named 'crp'.

Then we call pci_get_device() and pci_read_config_byte() and if either of them fail we return without freeing the memory allocated for the 'struct cr_panel'. These two leaks are easy to fix since we don't even use 'crp' for anything up to this point, so I simply moved the allocation further down in the function so it only happens just before we actually need it.

A bit further down we call backlight_device_register() and store the result in 'crp->cr_backlight_device'. In case of error we return 'crp->cr_backlight_device' from the function, thus leaking 'crp' itself. The same thing happens with the call to lcd_device_register(). To fix these two leaks I declare two new pointers to hold the return values, so that in case of error we can return the pointer (as before) but without leaking 'crp'.

Cc Thomas Hellstrom <[email protected]>
Cc Alan Hourihane <[email protected]>
Cc Richard Purdie <[email protected]>

---

 drivers/video/backlight/cr_bllcd.c |   30 +++++++++++++--------------
 1 files changed, 15 insertions(+), 15 deletions(-)
drivers/video/backlight/cr_bllcd.c renamed from drivers/video/backlight/cr_bllcd.c~fix-several-memory-leaks-in-cr_backlight_probe +15 -15
diff -puN drivers/video/backlight/cr_bllcd.c~fix-several-memory-leaks-in-cr_backlight_probe drivers/video/backlight/cr_bllcd.c
@@ -171,13 +171,11 @@ static struct lcd_ops cr_lcd_ops = {
 
 static int cr_backlight_probe(struct platform_device *pdev)
 {
+	struct backlight_device *bdp;
+	struct lcd_device *ldp;
 	struct cr_panel *crp;
 	u8 dev_en;
 
-	crp = kzalloc(sizeof(*crp), GFP_KERNEL);
-	if (crp == NULL)
-		return -ENOMEM;
-
 	lpc_dev = pci_get_device(PCI_VENDOR_ID_INTEL,
 					CRVML_DEVICE_LPC, NULL);
 	if (!lpc_dev) {
@@ -193,27 +191,29 @@ static int cr_backlight_probe(struct pla
 		return -ENODEV;
 	}
 
-	crp->cr_backlight_device = backlight_device_register("cr-backlight",
-							     &pdev->dev, NULL,
-							     &cr_backlight_ops);
-	if (IS_ERR(crp->cr_backlight_device)) {
+	bdp = backlight_device_register("cr-backlight",
+					&pdev->dev, NULL,
+					&cr_backlight_ops);
+	if (IS_ERR(bdp)) {
 		pci_dev_put(lpc_dev);
-		return PTR_ERR(crp->cr_backlight_device);
+		return PTR_ERR(bdp);
 	}
 
-	crp->cr_lcd_device = lcd_device_register("cr-lcd",
-							&pdev->dev, NULL,
-							&cr_lcd_ops);
-
-	if (IS_ERR(crp->cr_lcd_device)) {
+	ldp = lcd_device_register("cr-lcd", &pdev->dev, &cr_lcd_ops);
+	if (IS_ERR(ldp)) {
 		pci_dev_put(lpc_dev);
-		return PTR_ERR(crp->cr_backlight_device);
+		return PTR_ERR(bdp);
 	}
 
 	pci_read_config_dword(lpc_dev, CRVML_REG_GPIOBAR,
 			      &gpio_bar);
 	gpio_bar &= ~0x3F;
 
+	crp = kzalloc(sizeof(*crp), GFP_KERNEL);
+	if (crp == NULL)
+		return -ENOMEM;
+	crp->cr_backlight_device = bdp;
+	crp->cr_lcd_device = ldp;
 	crp->cr_backlight_device->props.power = FB_BLANK_UNBLANK;
 	crp->cr_backlight_device->props.brightness = 0;
 	crp->cr_backlight_device->props.max_brightness = 0;

_

Patches currently in -mm which might be from [email protected] are

origin.patch
memory-leak-in-tpm_ascii_bios_measurements_open-fix.patch fix-acpi_ev_pci_config_region_setup-to-avoid-memory-leak.patch fix-use-after-free--double-free-bug-in-amd_create_gatt_pages--amd_free_gatt_pages.patch git-gfs2-nmw.patch
git-mtd.patch
fix-several-memory-leaks-in-cr_backlight_probe.patch
git-xfs.patch
ipmi-fix-mem-leak-in-try_init_dmi.patch fix-a-use-after-free-bug-in-kernel-userspace-relay-file-support.patch floppy-do-a-very-minimal-style-cleanup-of-the-floppy-driver.patch floppy-remove-dead-commented-out-code-from-floppy-driver.patch floppy-remove-register-keyword-use-from-floppy-driver.patch

A message that is not in the archive (sent off-list or not yet received): [email protected]

#2 lore raw

PATCH RESEND 1/1 Fix several memory leaks in cr_backlight_probe() - take2

This is a resend of a patch I originally submitted on 23/7-2007 23:32. That patch was an updated version of one originally submitted on 21-Jul-2007 00:56.

The original patch made a brief appearance in -mm, but was dropped due to concerns raised by Richard Purdie that it didn't fix all issues.

Here are Richard's comments:

Quoted 5 lines

The problems you've spotted are valid however the error paths are still broken both before and after this patch since there is no call to backlight_device_unregister() and/or lcd_device_unregister() in the probe function.

The following patch fixes the same problems as the original and also tries to fix the stuff that Richard pointed out. Hopefully this time it is perfect :-)

Here's the new version of the patch :

After fixing the too small memory allocation in cr_backlight_probe() from drivers/video/backlight/cr_bllcd.c
(commit e3bbb3f05339de438faf54124f25c92e6fe4ac2e) I noticed that the Coverity checker also thought there were a few memory leaks in there. I took a closer look and confirmed that there were indeed several leaks.

At the start of the function we allocate storage for a 'struct cr_panel' and store the pointer in a variable named 'crp'.

Then we call pci_get_device() and pci_read_config_byte() and if either of them fail we return without freeing the memory allocated for the 'struct cr_panel'. These two leaks are easy to fix since we don't even use 'crp' for anything up to this point, so I simply moved the allocation further down in the function so it only happens just before we actually need it.

A bit further down we call backlight_device_register() and store the result in 'crp->cr_backlight_device'. In case of error we return 'crp->cr_backlight_device' from the function, thus leaking 'crp' itself. The same thing happens with the call to lcd_device_register(). To fix these two leaks I declare two new pointers to hold the return values, so that in case of error we can return the pointer (as before) but without leaking 'crp'.

This version of the patch also adds missing backlight_device_unregister() / lcd_device_unregister() / pci_dev_put() calls to error paths.
Thanks to Richard Purdie <[email protected]> for noticing.

Please consider merging.

Note: Due to lack of hardware the patch has only been compile tested.

---

 drivers/video/backlight/cr_bllcd.c |   35 ++++++++++++++++++++---------------
 1 files changed, 20 insertions(+), 15 deletions(-)
drivers/video/backlight/cr_bllcd.c +20 -15
index b7904da..92e201e 100644
@@ -171,13 +171,11 @@ static struct lcd_ops cr_lcd_ops = {
 
 static int cr_backlight_probe(struct platform_device *pdev)
 {
+	struct backlight_device *bdp;
+	struct lcd_device *ldp;
 	struct cr_panel *crp;
 	u8 dev_en;
 
-	crp = kzalloc(sizeof(*crp), GFP_KERNEL);
-	if (crp == NULL)
-		return -ENOMEM;
-
 	lpc_dev = pci_get_device(PCI_VENDOR_ID_INTEL,
 					CRVML_DEVICE_LPC, NULL);
 	if (!lpc_dev) {
@@ -193,27 +191,34 @@ static int cr_backlight_probe(struct platform_device *pdev)
 		return -ENODEV;
 	}
 
-	crp->cr_backlight_device = backlight_device_register("cr-backlight",
-							     &pdev->dev, NULL,
-							     &cr_backlight_ops);
-	if (IS_ERR(crp->cr_backlight_device)) {
+	bdp = backlight_device_register("cr-backlight",
+					&pdev->dev, NULL, &cr_backlight_ops);
+	if (IS_ERR(bdp)) {
 		pci_dev_put(lpc_dev);
-		return PTR_ERR(crp->cr_backlight_device);
+		return PTR_ERR(bdp);
 	}
 
-	crp->cr_lcd_device = lcd_device_register("cr-lcd",
-							&pdev->dev, NULL,
-							&cr_lcd_ops);
-
-	if (IS_ERR(crp->cr_lcd_device)) {
+	ldp = lcd_device_register("cr-lcd", &pdev->dev, NULL, &cr_lcd_ops);
+	if (IS_ERR(ldp)) {
+		backlight_device_unregister(bdp);
 		pci_dev_put(lpc_dev);
-		return PTR_ERR(crp->cr_backlight_device);
+		return PTR_ERR(bdp);
 	}
 
 	pci_read_config_dword(lpc_dev, CRVML_REG_GPIOBAR,
 			      &gpio_bar);
 	gpio_bar &= ~0x3F;
 
+	crp = kzalloc(sizeof(*crp), GFP_KERNEL);
+	if (!crp) {
+		lcd_device_unregister(ldp);
+		backlight_device_unregister(bdp);
+		pci_dev_put(lpc_dev);
+		return -ENOMEM;
+	}
+
+	crp->cr_backlight_device = bdp;
+	crp->cr_lcd_device = ldp;
 	crp->cr_backlight_device->props.power = FB_BLANK_UNBLANK;
 	crp->cr_backlight_device->props.brightness = 0;
 	crp->cr_backlight_device->props.max_brightness = 0;