* [PATCH v2 1/2] kobject: delay kobject release for random time
2013-12-06 0:37 [PATCH v2 0/2] Remove ksets from sysfs eagerly Bjorn Helgaas
@ 2013-12-06 0:37 ` Bjorn Helgaas
2013-12-06 0:38 ` [PATCH v2 2/2] kobject: remove kset from sysfs immediately in kset_unregister() Bjorn Helgaas
1 sibling, 0 replies; 3+ messages in thread
From: Bjorn Helgaas @ 2013-12-06 0:37 UTC (permalink / raw)
To: Greg Kroah-Hartman
Cc: Veaceslav Falico, Russell King, Neil Horman, linux-pci,
linux-kernel, Zdenek Kabelac
When CONFIG_DEBUG_KOBJECT_RELEASE=y, delay kobject release functions for a
random time between 1 and 8 seconds, which effectively changes the order in
which they're called.
Signed-off-by: Bjorn Helgaas <bhelgaas@google.com>
---
lib/kobject.c | 10 +++++++---
1 file changed, 7 insertions(+), 3 deletions(-)
diff --git a/lib/kobject.c b/lib/kobject.c
index 962175134702..bc73312ac797 100644
--- a/lib/kobject.c
+++ b/lib/kobject.c
@@ -17,6 +17,7 @@
#include <linux/export.h>
#include <linux/stat.h>
#include <linux/slab.h>
+#include <linux/random.h>
/*
* populate_dir - populate directory with attributes.
@@ -592,10 +593,13 @@ static void kobject_release(struct kref *kref)
{
struct kobject *kobj = container_of(kref, struct kobject, kref);
#ifdef CONFIG_DEBUG_KOBJECT_RELEASE
- pr_debug("kobject: '%s' (%p): %s, parent %p (delayed)\n",
- kobject_name(kobj), kobj, __func__, kobj->parent);
+ unsigned long delay = HZ + HZ * (get_random_int() & 0x3);
+
+ pr_debug("kobject: '%s' (%p): %s, parent %p (delayed %ld)\n",
+ kobject_name(kobj), kobj, __func__, kobj->parent, delay);
INIT_DELAYED_WORK(&kobj->release, kobject_delayed_cleanup);
- schedule_delayed_work(&kobj->release, HZ);
+
+ schedule_delayed_work(&kobj->release, delay);
#else
kobject_cleanup(kobj);
#endif
^ permalink raw reply [flat|nested] 3+ messages in thread* [PATCH v2 2/2] kobject: remove kset from sysfs immediately in kset_unregister()
2013-12-06 0:37 [PATCH v2 0/2] Remove ksets from sysfs eagerly Bjorn Helgaas
2013-12-06 0:37 ` [PATCH v2 1/2] kobject: delay kobject release for random time Bjorn Helgaas
@ 2013-12-06 0:38 ` Bjorn Helgaas
1 sibling, 0 replies; 3+ messages in thread
From: Bjorn Helgaas @ 2013-12-06 0:38 UTC (permalink / raw)
To: Greg Kroah-Hartman
Cc: Veaceslav Falico, Russell King, Neil Horman, linux-pci,
linux-kernel, Zdenek Kabelac
There's no "unlink from sysfs" interface for ksets, so I think callers of
kset_unregister() expect the kset to be removed from sysfs immediately,
without waiting for the last reference to be released.
This patch makes the sysfs removal happen immediately, so the caller may
create a new kset with the same name as soon as kset_unregister() returns.
Without this, every caller has to call "kobject_del(&kset->kobj)" first
unless it knows it will never create a new kset with the same name.
This sometimes shows up on module unload and reload, where the reload fails
because it tries to create a kobject with the same name as one from the
original load that still exists. CONFIG_DEBUG_KOBJECT_RELEASE=y makes this
problem easier to hit.
Signed-off-by: Bjorn Helgaas <bhelgaas@google.com>
---
Documentation/kobject.txt | 5 ++++-
lib/kobject.c | 1 +
2 files changed, 5 insertions(+), 1 deletion(-)
diff --git a/Documentation/kobject.txt b/Documentation/kobject.txt
index c5182bb2c16c..f87241dfed87 100644
--- a/Documentation/kobject.txt
+++ b/Documentation/kobject.txt
@@ -342,7 +342,10 @@ kset use:
When you are finished with the kset, call:
void kset_unregister(struct kset *kset);
-to destroy it.
+to destroy it. This removes the kset from sysfs and decrements its reference
+count. When the reference count goes to zero, the kset will be released.
+Because other references to the kset may still exist, the release may happen
+after kset_unregister() returns.
An example of using a kset can be seen in the
samples/kobject/kset-example.c file in the kernel tree.
diff --git a/lib/kobject.c b/lib/kobject.c
index bc73312ac797..40e3b2c0abae 100644
--- a/lib/kobject.c
+++ b/lib/kobject.c
@@ -757,6 +757,7 @@ void kset_unregister(struct kset *k)
{
if (!k)
return;
+ kobject_del(&k->kobj);
kobject_put(&k->kobj);
}
^ permalink raw reply [flat|nested] 3+ messages in thread