Skip to contentSkip to conversation

eisa: call put_device if device_register fails

PATCH Bjorn Helgaas 4 participants 6 messages Dec 13, 2013 – Jan 7, 2014 4 messages not in the archive

Subsystems (3)
lore
Share & tools
lore

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

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

#2 lore raw
Quoted 5 lines · Levente Kurusa

We need to give up the last reference to edev->dev, so we need to call put_device().

I applied this and will push it through my PCI tree, since I don't think anybody really takes care of EISA these days.

I looked at other callers of device_register(), and most of them look like they have the same problem. Can you fix the others, too? This patch says "2/4", so maybe you already are, but I can't find the other patches in the series.

Bjorn

Quoted 23 lines
---
 drivers/eisa/eisa-bus.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/eisa/eisa-bus.c +3 -1
index 272a3ec..8842cde 100644
@@ -232,8 +232,10 @@ static int __init eisa_init_device(struct eisa_root_device *root,
 static int __init eisa_register_device(struct eisa_device *edev)
 {
 	int rc = device_register(&edev->dev);
-	if (rc)
+	if (rc) {
+		put_device(&edev->dev);
 		return rc;
+	}
 
 	rc = device_create_file(&edev->dev, &dev_attr_signature);
 	if (rc)
Signature · 1 line
1.8.3.1
#3 lore raw
Quoted 7 lines · Bjorn Helgaas
Quoted 5 lines · Levente Kurusa

We need to give up the last reference to edev->dev, so we need to call put_device().

Quoted 7 lines

I applied this and will push it through my PCI tree, since I don't think anybody really takes care of EISA these days.

I looked at other callers of device_register(), and most of them look like they have the same problem. Can you fix the others, too? This patch says "2/4", so maybe you already are, but I can't find the other patches in the series.

Yes, the patchset didn't hit LKML due to me being a noob with git-send-email. :-(

I will fix them, just sent this to verify that I am doing it right. :-)

Thanks for applying!

Signature · 2 lines
Regards,
Levente Kurusa

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

#5 lore raw

Re: [PATCH 1/4] net: phy: call put_device on device_register() failure

Quoted 7 lines · Levente Kurusa

It is required to call put_device() if device_register() fails, so that we give up the last reference to the device. Calling put_device allows for mdiobus_release to be executed, kfreeing the bus.

Cc Greg Kroah-Hartman <[email protected]>
Cc Grant Likely <[email protected]>

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

Acked-by
#7 lore raw

Re: [PATCH 3/4] backlight: lcd: call put_device if device_register fails

Quoted 8 lines · Levente Kurusa

Currently we kfree the container of the device which failed to register. This is wrong as the last reference is not given up with a put_device call. Also, now that we have put_device() callen, we no longer need the kfree as the new_ld->dev.release function will take care of kfreeing the associated memory.

(+cc Andrew Morton)

It looks good.
According to the comment of device_register, put_device() should be used, instead of directly freeing.

./drivers/base/core.c

* NOTE: _Never_ directly free @dev after calling this function, even
* if it returned an error! Always use put_device() to give up the
* reference initialized in this function instead.
*/
int device_register(struct device *dev)

{
        device_initialize(dev);
        return device_add(dev);
}

EXPORT_SYMBOL_GPL(device_register);

Levente Kurusa,
By the way, don't send the same mails three times, without any reason. It is the waste of traffic. :-(

Best regards,
Jingoo Han

Quoted 19 lines
---
 drivers/video/backlight/lcd.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/video/backlight/lcd.c +1 -2
index 93cf15e..7de847d 100644
@@ -228,7 +228,7 @@ struct lcd_device *lcd_device_register(const char *name, struct device *parent,
 
 	rc = device_register(&new_ld->dev);
 	if (rc) {
-		kfree(new_ld);
+		put_device(&new_ld->dev);
 		return ERR_PTR(rc);
 	}
 
--

1.8.3.1

#8 lore raw

Re: [PATCH 3/4] backlight: lcd: call put_device if device_register fails

Quoted 10 lines · Jingoo Han
Quoted 8 lines · Levente Kurusa

Currently we kfree the container of the device which failed to register. This is wrong as the last reference is not given up with a put_device call. Also, now that we have put_device() callen, we no longer need the kfree as the new_ld->dev.release function will take care of kfreeing the associated memory.

Quoted 7 lines

(+cc Andrew Morton)

It looks good.
According to the comment of device_register, put_device() should be used, instead of directly freeing.

Indeed, this is also mostly explained in [0/4]. Thanks for the Ack!

Quoted 1 line

[...]

Quoted 5 lines

Levente Kurusa,
By the way, don't send the same mails three times, without any reason. It is the waste of traffic. :-(

Yea, sorry about that I messed up my git's smtp config and hence most of the messages bounced off. It didn't even reach LKML. Sorry once more.

Signature · 2 lines
Regards,
Levente Kurusa
#9 lore raw

Re: [PATCH 3/4] backlight: lcd: call put_device if device_register fails

In reply to a message not in the archive
Quoted 17 lines · Levente Kurusa

Currently we kfree the container of the device which failed to register. This is wrong as the last reference is not given up with a put_device call. Also, now that we have put_device() callen, we no longer need the kfree as the new_ld->dev.release function will take care of kfreeing the associated memory.

---
 drivers/video/backlight/lcd.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/video/backlight/lcd.c +0 -0
index 93cf15e..7de847d 100644

drivers/video/backlight/lcd.c struct lcd_device *lcd_device_register(const char *name, struct device *parent, · line 233

Quoted 7 lines
drivers/video/backlight/lcd.c +1 -1
@@ -228,7 +228,7 @@ struct lcd_device *lcd_device_register(const char *name, struct device *parent,
 
 	rc = device_register(&new_ld->dev);
 	if (rc) {
-		kfree(new_ld);
+		put_device(&new_ld->dev);
 		return ERR_PTR(rc);
 	}

(+cc Andrew Morton)

Hi Levente Kurusa,

Would you fix the same thing for 'backlight.c' file?

./drivers/video/backlight/backlight.c
struct backlight_device *backlight_device_register(const char *name,

    .....
    rc = device_register(&new_bd->dev);
            if (rc) {
                    kfree(new_bd);
                    return ERR_PTR(rc);
            }

Best regards,
Jingoo Han