[PATCH 02/12] driver core: add debug-objects debug for device-drivers

From: Konstantin Khlebnikov
Date: Fri Dec 14 2012 - 06:02:30 EST


CONFIG_DEBUG_OBJECTS_DRIVERS together with CONFIG_DEBUG_OBJECTS_FREE can catch
unloading device driver modules without proper unregistering.

Signed-off-by: Konstantin Khlebnikov <khlebnikov@xxxxxxxxxx>
Cc: Greg Kroah-Hartman <gregkh@xxxxxxxxxxxxxxxxxxx>
Cc: Thomas Gleixner <tglx@xxxxxxxxxxxxx>
---
drivers/base/driver.c | 34 ++++++++++++++++++++++++++++++++++
lib/Kconfig.debug | 7 +++++++
2 files changed, 41 insertions(+)

diff --git a/drivers/base/driver.c b/drivers/base/driver.c
index 974e301..7eec027 100644
--- a/drivers/base/driver.c
+++ b/drivers/base/driver.c
@@ -15,8 +15,40 @@
#include <linux/errno.h>
#include <linux/slab.h>
#include <linux/string.h>
+#include <linux/debugobjects.h>
#include "base.h"

+#ifdef CONFIG_DEBUG_OBJECTS_DRIVERS
+
+static void * debug_driver_hint(void *addr)
+{
+ struct device_driver *drv = addr;
+
+ return drv->probe;
+}
+
+struct debug_obj_descr driver_debug_descr = {
+ .name = "device_driver",
+ .debug_hint = debug_driver_hint,
+};
+
+static inline void debug_driver_register(struct device_driver *drv)
+{
+ debug_object_init(drv, &driver_debug_descr);
+ debug_object_activate(drv, &driver_debug_descr);
+}
+
+static inline void debug_driver_unregister(struct device_driver *drv)
+{
+ debug_object_deactivate(drv, &driver_debug_descr);
+ debug_object_free(drv, &driver_debug_descr);
+}
+
+#else /* CONFIG_DEBUG_OBJECTS_DRIVERS */
+static inline void debug_driver_register(struct device_driver *drv) { }
+static inline void debug_driver_unregister(struct device_driver *drv) { }
+#endif /* CONFIG_DEBUG_OBJECTS_DRIVERS */
+
static struct device *next_device(struct klist_iter *i)
{
struct klist_node *n = klist_next(i);
@@ -190,6 +222,7 @@ int driver_register(struct device_driver *drv)
return ret;
}
kobject_uevent(&drv->p->kobj, KOBJ_ADD);
+ debug_driver_register(drv);

return ret;
}
@@ -209,6 +242,7 @@ void driver_unregister(struct device_driver *drv)
}
driver_remove_groups(drv, drv->groups);
bus_remove_driver(drv);
+ debug_driver_unregister(drv);
}
EXPORT_SYMBOL_GPL(driver_unregister);

diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug
index 3a35309..f5aee2d 100644
--- a/lib/Kconfig.debug
+++ b/lib/Kconfig.debug
@@ -404,6 +404,13 @@ config DEBUG_OBJECTS_PERCPU_COUNTER
percpu counter routines to track the life time of percpu counter
objects and validate the percpu counter operations.

+config DEBUG_OBJECTS_DRIVERS
+ bool "Debug device driver objects"
+ depends on DEBUG_OBJECTS
+ help
+ Enable this to turn on debugging device drivers structures. Together
+ with DEBUG_OBJECTS_FREE this can catch freeing registered drivers.
+
config DEBUG_OBJECTS_ENABLE_DEFAULT
int "debug_objects bootup default value (0-1)"
range 0 1

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/