Skip to contentSkip to conversation

devres: WARN() and return, don't crash on device_del() of uninitialized device

PATCH In review Benjamin Herrenschmidt 2 participants 3 messages May 25, 2009 – May 26, 2009 Driver core

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 devres: WARN() and return, don't crash on device_del() of uninitialized device

I just debugged an obscure crash caused by a device_del() of a all NULL'd out struct device (in usb-serial) and found that a patch like this one would have saved me time (in addition to improved chances of a bug report from users hitting similar driver bugs).

---

 drivers/base/devres.c |    5 +++++
 1 file changed, 5 insertions(+)
drivers/base/devres.c +5 -0
@@ -428,6 +428,11 @@ int devres_release_all(struct device *de
 {
 	unsigned long flags;
 
+	/* Looks like an uninitialized device structure */
+	if (dev->devres_head.next == NULL) {
+		WARN(1);
+		return -ENODEV;
+	}
 	spin_lock_irqsave(&dev->devres_lock, flags);
 	return release_nodes(dev, dev->devres_head.next, &dev->devres_head,
 			     flags);
#1 lore raw
Quoted 15 lines · On Mon, 25 May 2009 16:53:31 +1000 Benjamin Herrenschmidt…

I just debugged an obscure crash caused by a device_del() of a all NULL'd out struct device (in usb-serial) and found that a patch like this one would have saved me time (in addition to improved chances of a bug report from users hitting similar driver bugs).

---

 drivers/base/devres.c |    5 +++++
 1 file changed, 5 insertions(+)
drivers/base/devres.c +0 -0
@@ -428,6 +428,11 @@ int devres_release_all(struct device *de
 {

drivers/base/devres.c int devres_release_all(struct device *de · line 438

Quoted 10 lines
drivers/base/devres.c +5 -0
 	unsigned long flags;
 
+	/* Looks like an uninitialized device structure */
+	if (dev->devres_head.next == NULL) {
+		WARN(1);
+		return -ENODEV;
+	}
 	spin_lock_irqsave(&dev->devres_lock, flags);
 	return release_nodes(dev, dev->devres_head.next, &dev->devres_head,
 			     flags);

This could be coded a little more neatly as:

	/* Looks like an uninitialized device structure */
	if (WARN_ON(dev->devres_head.next == NULL))
		return -ENODEV;
#2 lore raw
Quoted 6 lines · Andrew Morton

This could be coded a little more neatly as:

        /* Looks like an uninitialized device structure */
        if (WARN_ON(dev->devres_head.next == NULL))
                return -ENODEV;

Oh I didn't know we could do that nowadays...

Cheers,
Ben.