2013-10-08 20:20:22

by Bjorn Helgaas

[permalink] [raw]
Subject: [PATCH 0/2] Remove ksets from sysfs eagerly

With CONFIG_DEBUG_KOBJECT_RELEASE=y, kobject release is delayed even after
the last reference to the object is dropped.

With this turned on, Veaceslav and Zdenek noticed that when we enable,
disable, and re-enable MSIs, we often see errors like "sysfs: cannot create
duplicate filename '/devices/pci0000:00/.../msi_irqs'". This happens
when we unload and reload drivers that use MSI.

The reason is that the MSI disable path uses kset_unregister(dev->msi_kset),
which doesn't remove the kset from sysfs until the the kobject release
function is called. With CONFIG_DEBUG_KOBJECT_RELEASE, the release is
obviously delayed, but even without it, the release will be delayed if
somebody has the kset sysfs file open when kset_unregister() is called.

We could work around this by explicitly calling
"kobject_del(&dev->msi_kset->kobj)", but that seems clunky and other
kset_unregister() users are likely to have similar problems.

The idea of this patch is to make kset_unregister() unlink the kset from
sysfs immediately, before dropping the kset reference.

---

Bjorn Helgaas (2):
kobject: remove kset from sysfs immediately in kset_unregister()
kobject: fix kset sample error path


Documentation/kobject.txt | 3 ++-
lib/kobject.c | 1 +
samples/kobject/kset-example.c | 1 +
3 files changed, 4 insertions(+), 1 deletion(-)


2013-10-08 20:20:45

by Bjorn Helgaas

[permalink] [raw]
Subject: [PATCH 1/2] kobject: remove kset from sysfs immediately in kset_unregister()

There's no explicit "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.

Signed-off-by: Bjorn Helgaas <[email protected]>
---
Documentation/kobject.txt | 3 ++-
lib/kobject.c | 1 +
2 files changed, 3 insertions(+), 1 deletion(-)

diff --git a/Documentation/kobject.txt b/Documentation/kobject.txt
index c5182bb..8e8b501 100644
--- a/Documentation/kobject.txt
+++ b/Documentation/kobject.txt
@@ -342,7 +342,8 @@ 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, after the kset
+reference count goes to zero, releases it.

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 9621751..9098992 100644
--- a/lib/kobject.c
+++ b/lib/kobject.c
@@ -753,6 +753,7 @@ void kset_unregister(struct kset *k)
{
if (!k)
return;
+ kobject_del(&k->kobj);
kobject_put(&k->kobj);
}

2013-10-08 20:26:25

by Bjorn Helgaas

[permalink] [raw]
Subject: [PATCH 2/2] kobject: fix kset sample error path

Previously, example_init() leaked a kset if any of the object creations
failed. This fixes the leak by calling kset_unregister() in the error
path.

Signed-off-by: Bjorn Helgaas <[email protected]>
---
samples/kobject/kset-example.c | 1 +
1 file changed, 1 insertion(+)

diff --git a/samples/kobject/kset-example.c b/samples/kobject/kset-example.c
index d0c687f..5dce351 100644
--- a/samples/kobject/kset-example.c
+++ b/samples/kobject/kset-example.c
@@ -262,6 +262,7 @@ baz_error:
bar_error:
destroy_foo_obj(foo_obj);
foo_error:
+ kset_unregister(example_kset);
return -EINVAL;
}