Skip to content

Commit c817a67

Browse files
Russell Kinggregkh
authored andcommitted
kobject: delayed kobject release: help find buggy drivers
Implement debugging for kobject release functions. kobjects are reference counted, so the drop of the last reference to them is not predictable. However, the common case is for the last reference to be the kobject's removal from a subsystem, which results in the release function being immediately called. This can hide subtle bugs, which can occur when another thread holds a reference to the kobject at the same time that a kobject is removed. This results in the release method being delayed. In order to make these kinds of problems more visible, the following patch implements a delayed release; this has the effect that the release function will be out of order with respect to the removal of the kobject in the same manner that it would be if a reference was being held. This provides us with an easy way to allow driver writers to debug their drivers and fix otherwise hidden problems. Signed-off-by: Russell King <rmk+kernel@arm.linux.org.uk> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 7c42721 commit c817a67

3 files changed

Lines changed: 42 additions & 3 deletions

File tree

‎include/linux/kobject.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include <linux/kernel.h>
2727
#include <linux/wait.h>
2828
#include <linux/atomic.h>
29+
#include <linux/workqueue.h>
2930

3031
#define UEVENT_HELPER_PATH_LEN 256
3132
#define UEVENT_NUM_ENVP 32 /* number of env pointers */
@@ -65,6 +66,9 @@ struct kobject {
6566
struct kobj_type *ktype;
6667
struct sysfs_dirent *sd;
6768
struct kref kref;
69+
#ifdef CONFIG_DEBUG_KOBJECT_RELEASE
70+
struct delayed_work release;
71+
#endif
6872
unsigned int state_initialized:1;
6973
unsigned int state_in_sysfs:1;
7074
unsigned int state_add_uevent_sent:1;

‎lib/Kconfig.debug‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -981,6 +981,25 @@ config DEBUG_KOBJECT
981981
If you say Y here, some extra kobject debugging messages will be sent
982982
to the syslog.
983983

984+
config DEBUG_KOBJECT_RELEASE
985+
bool "kobject release debugging"
986+
depends on DEBUG_KERNEL
987+
help
988+
kobjects are reference counted objects. This means that their
989+
last reference count put is not predictable, and the kobject can
990+
live on past the point at which a driver decides to drop it's
991+
initial reference to the kobject gained on allocation. An
992+
example of this would be a struct device which has just been
993+
unregistered.
994+
995+
However, some buggy drivers assume that after such an operation,
996+
the memory backing the kobject can be immediately freed. This
997+
goes completely against the principles of a refcounted object.
998+
999+
If you say Y here, the kernel will delay the release of kobjects
1000+
on the last reference count to improve the visibility of this
1001+
kind of kobject release bug.
1002+
9841003
config HAVE_DEBUG_BUGVERBOSE
9851004
bool
9861005

‎lib/kobject.c‎

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -545,8 +545,8 @@ static void kobject_cleanup(struct kobject *kobj)
545545
struct kobj_type *t = get_ktype(kobj);
546546
const char *name = kobj->name;
547547

548-
pr_debug("kobject: '%s' (%p): %s\n",
549-
kobject_name(kobj), kobj, __func__);
548+
pr_debug("kobject: '%s' (%p): %s, parent %p\n",
549+
kobject_name(kobj), kobj, __func__, kobj->parent);
550550

551551
if (t && !t->release)
552552
pr_debug("kobject: '%s' (%p): does not have a release() "
@@ -580,9 +580,25 @@ static void kobject_cleanup(struct kobject *kobj)
580580
}
581581
}
582582

583+
#ifdef CONFIG_DEBUG_KOBJECT_RELEASE
584+
static void kobject_delayed_cleanup(struct work_struct *work)
585+
{
586+
kobject_cleanup(container_of(to_delayed_work(work),
587+
struct kobject, release));
588+
}
589+
#endif
590+
583591
static void kobject_release(struct kref *kref)
584592
{
585-
kobject_cleanup(container_of(kref, struct kobject, kref));
593+
struct kobject *kobj = container_of(kref, struct kobject, kref);
594+
#ifdef CONFIG_DEBUG_KOBJECT_RELEASE
595+
pr_debug("kobject: '%s' (%p): %s, parent %p (delayed)\n",
596+
kobject_name(kobj), kobj, __func__, kobj->parent);
597+
INIT_DELAYED_WORK(&kobj->release, kobject_delayed_cleanup);
598+
schedule_delayed_work(&kobj->release, HZ);
599+
#else
600+
kobject_cleanup(kobj);
601+
#endif
586602
}
587603

588604
/**

0 commit comments

Comments
 (0)