+++ /dev/null
-From e26a9797c0a2aedfabe42b6ddecb49408e09604c Mon Sep 17 00:00:00 2001
-From: Benjamin Herrenschmidt <benh@kernel.crashing.org>
-Date: Mon, 28 Jan 2019 09:31:30 -0800
-Subject: drivers: core: Remove glue dirs from sysfs earlier
-
-commit 726e41097920a73e4c7c33385dcc0debb1281e18 upstream
-
-For devices with a class, we create a "glue" directory between
-the parent device and the new device with the class name.
-
-This directory is never "explicitely" removed when empty however,
-this is left to the implicit sysfs removal done by kobject_release()
-when the object loses its last reference via kobject_put().
-
-This is problematic because as long as it's not been removed from
-sysfs, it is still present in the class kset and in sysfs directory
-structure.
-
-The presence in the class kset exposes a use after free bug fixed
-by the previous patch, but the presence in sysfs means that until
-the kobject is released, which can take a while (especially with
-kobject debugging), any attempt at re-creating such as binding a
-new device for that class/parent pair, will result in a sysfs
-duplicate file name error.
-
-This fixes it by instead doing an explicit kobject_del() when
-the glue dir is empty, by keeping track of the number of
-child devices of the gluedir.
-
-This is made easy by the fact that all glue dir operations are
-done with a global mutex, and there's already a function
-(cleanup_glue_dir) called in all the right places taking that
-mutex that can be enhanced for this. It appears that this was
-in fact the intent of the function, but the implementation was
-wrong.
-
-Backport Note: kref_read() is not present in 4.4. Hence,
-use atomic_read(&kref.refcount) instead of kref_read(&kref).
-
-Signed-off-by: Benjamin Herrenschmidt <benh@kernel.crashing.org>
-Acked-by: Linus Torvalds <torvalds@linux-foundation.org>
-Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
-Signed-off-by: Zubin Mithra <zsm@chromium.org>
-Signed-off-by: Sasha Levin <sashal@kernel.org>
----
- drivers/base/core.c | 2 ++
- include/linux/kobject.h | 17 +++++++++++++++++
- 2 files changed, 19 insertions(+)
-
-diff --git a/drivers/base/core.c b/drivers/base/core.c
-index 049ccc070ce5..cb5718d2669e 100644
---- a/drivers/base/core.c
-+++ b/drivers/base/core.c
-@@ -862,6 +862,8 @@ static void cleanup_glue_dir(struct device *dev, struct kobject *glue_dir)
- return;
-
- mutex_lock(&gdp_mutex);
-+ if (!kobject_has_children(glue_dir))
-+ kobject_del(glue_dir);
- kobject_put(glue_dir);
- mutex_unlock(&gdp_mutex);
- }
-diff --git a/include/linux/kobject.h b/include/linux/kobject.h
-index e6284591599e..5957c6a3fd7f 100644
---- a/include/linux/kobject.h
-+++ b/include/linux/kobject.h
-@@ -113,6 +113,23 @@ extern void kobject_put(struct kobject *kobj);
- extern const void *kobject_namespace(struct kobject *kobj);
- extern char *kobject_get_path(struct kobject *kobj, gfp_t flag);
-
-+/**
-+ * kobject_has_children - Returns whether a kobject has children.
-+ * @kobj: the object to test
-+ *
-+ * This will return whether a kobject has other kobjects as children.
-+ *
-+ * It does NOT account for the presence of attribute files, only sub
-+ * directories. It also assumes there is no concurrent addition or
-+ * removal of such children, and thus relies on external locking.
-+ */
-+static inline bool kobject_has_children(struct kobject *kobj)
-+{
-+ WARN_ON_ONCE(atomic_read(&kobj->kref.refcount) == 0);
-+
-+ return kobj->sd && kobj->sd->dir.subdirs;
-+}
-+
- struct kobj_type {
- void (*release)(struct kobject *kobj);
- const struct sysfs_ops *sysfs_ops;
---
-2.19.1
-