From: Benjamin Herrenschmidt <benh@kernel.crashing.org>
To: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
"Eric W. Biederman" <ebiederm@xmission.com>,
Joel Stanley <joel@jms.id.au>,
"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>
Subject: [PATCH 2/2] drivers: core: Remove glue dirs from sysfs earlier
Date: Fri, 29 Jun 2018 12:21:58 +1000 [thread overview]
Message-ID: <ac64e45950c8e3df26fd50e7b251d5895c1d2201.camel@kernel.crashing.org> (raw)
In-Reply-To: <CA+55aFxR0qg0yY-NWnH0DDruVWw8qRqp8=CRLq13p=TyxosJKw@mail.gmail.com>
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 kobjects when
they are released on the last 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.
Signed-off-by: Benjamin Herrenschmidt <benh@kernel.crashing.org>
---
(Adding lkml, I just realized I completely forgot to CC it in
the first place on this whole conversation, blame the 1am debugging
session)
drivers/base/core.c | 17 +++++++++++++++++
1 file changed, 17 insertions(+)
diff --git a/drivers/base/core.c b/drivers/base/core.c
index e9eff2099896..66e15daa9980 100644
--- a/drivers/base/core.c
+++ b/drivers/base/core.c
@@ -1436,6 +1436,13 @@ struct kobject *virtual_device_parent(struct device *dev)
struct class_dir {
struct kobject kobj;
struct class *class;
+
+ /*
+ * This counts the number of children of the gluedir in
+ * order to "cleanly" kobject_del() it when it becomes
+ * empty. It is protected by the gdb_mutex
+ */
+ unsigned int child_count;
};
#define to_class_dir(obj) container_of(obj, struct class_dir, kobj)
@@ -1470,6 +1477,7 @@ class_dir_create_and_add(struct class *class, struct kobject *parent_kobj)
return NULL;
dir->class = class;
+ dir->child_count = 1;
kobject_init(&dir->kobj, &class_dir_ktype);
dir->kobj.kset = &class->p->glue_dirs;
@@ -1526,6 +1534,8 @@ static struct kobject *get_device_parent(struct device *dev,
}
spin_unlock(&dev->class->p->glue_dirs.list_lock);
if (kobj) {
+ struct class_dir *class_dir = to_class_dir(kobj);
+ class_dir->child_count++;
mutex_unlock(&gdp_mutex);
return kobj;
}
@@ -1567,11 +1577,18 @@ static inline struct kobject *get_glue_dir(struct device *dev)
*/
static void cleanup_glue_dir(struct device *dev, struct kobject *glue_dir)
{
+ struct class_dir *class_dir = to_class_dir(glue_dir);
+
/* see if we live in a "glue" directory */
if (!live_in_glue_dir(glue_dir, dev))
return;
mutex_lock(&gdp_mutex);
+ if (WARN_ON(class_dir->child_count == 0))
+ goto bail;
+ if (--class_dir->child_count == 0)
+ kobject_del(glue_dir);
+ bail:
kobject_put(glue_dir);
mutex_unlock(&gdp_mutex);
}
next prev parent reply other threads:[~2018-06-29 3:04 UTC|newest]
Thread overview: 42+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <c40fe912fe008b1b531a3867e8784ed79d68023e.camel@kernel.crashing.org>
[not found] ` <CA+55aFxR0qg0yY-NWnH0DDruVWw8qRqp8=CRLq13p=TyxosJKw@mail.gmail.com>
2018-06-29 2:21 ` [PATCH 1/2] drivers: core: Don't try to use a dead glue_dir Benjamin Herrenschmidt
2018-06-30 19:45 ` Linus Torvalds
2018-07-07 16:48 ` Greg Kroah-Hartman
2018-07-09 23:44 ` Benjamin Herrenschmidt
2018-07-10 14:55 ` Greg Kroah-Hartman
2018-07-10 23:32 ` Benjamin Herrenschmidt
2018-07-10 23:55 ` Linus Torvalds
2018-07-11 0:07 ` Benjamin Herrenschmidt
2018-07-21 7:53 ` Greg Kroah-Hartman
2018-07-23 0:35 ` Benjamin Herrenschmidt
2018-07-07 16:51 ` Greg Kroah-Hartman
2018-07-09 23:50 ` Benjamin Herrenschmidt
2018-06-29 2:21 ` Benjamin Herrenschmidt [this message]
2018-06-29 13:56 ` [PATCH 2/2] drivers: core: Remove glue dirs from sysfs earlier Linus Torvalds
2018-06-29 13:57 ` Linus Torvalds
2018-06-30 1:04 ` Benjamin Herrenschmidt
2018-06-30 3:51 ` Benjamin Herrenschmidt
[not found] ` <edc7b03b9550ddcf1291ebf5a6dafd24f4455c23.camel@kernel.crashing.org>
[not found] ` <CA+55aFxS7OVEN5XrxceC5ibz780mhn-qRa50w1gVFjsz2JjMbw@mail.gmail.com>
[not found] ` <7eb06b499f2be366cf68c6b6588b16c603e6a567.camel@kernel.crashing.org>
2018-07-01 2:07 ` Linus Torvalds
2018-07-01 2:18 ` Linus Torvalds
2018-07-01 3:49 ` Benjamin Herrenschmidt
2018-07-01 3:42 ` Benjamin Herrenschmidt
2018-07-01 3:57 ` Linus Torvalds
2018-07-01 7:16 ` Benjamin Herrenschmidt
2018-07-01 17:04 ` Linus Torvalds
2018-07-01 23:36 ` Benjamin Herrenschmidt
2018-07-02 10:23 ` Benjamin Herrenschmidt
2018-07-02 19:24 ` Linus Torvalds
2018-07-03 0:57 ` Benjamin Herrenschmidt
2018-07-03 2:15 ` Linus Torvalds
2018-07-03 2:26 ` Linus Torvalds
2018-07-03 2:39 ` Benjamin Herrenschmidt
2018-07-03 5:22 ` Benjamin Herrenschmidt
2018-07-03 15:46 ` Tejun Heo
2018-07-04 1:10 ` Benjamin Herrenschmidt
[not found] ` <CA+55aFzKmzC-2_6+RsRRu9KfK_r=UGgLN2Q0hSNBV=ScGR7=8g@mail.gmail.com>
[not found] ` <6e3ca577f8dd5f3621d1054447d3f928a73dfcf9.camel@kernel.crashing.org>
[not found] ` <CA+55aFy+ZSu5cPzk887N-ZgXqvTB=Bp1JQYMWT1SZY81MqLH6Q@mail.gmail.com>
[not found] ` <1bc873980e7f63291fbe19dbc7e1607b8e126241.camel@kernel.crashing.org>
[not found] ` <20180707164241.GB16279@kroah.com>
[not found] ` <CA+55aFx-UX8nxewRFFWdBgYfPqfipnxaqJuJCUni9h4JvhoPFw@mail.gmail.com>
2018-07-10 0:29 ` [PATCH v2 " Benjamin Herrenschmidt
2018-07-10 0:33 ` Linus Torvalds
2018-07-10 1:37 ` Benjamin Herrenschmidt
2018-07-10 14:55 ` Greg Kroah-Hartman
2018-07-10 23:31 ` Benjamin Herrenschmidt
2018-07-03 2:37 ` [PATCH " Benjamin Herrenschmidt
2018-07-02 10:22 ` Benjamin Herrenschmidt
2018-07-01 3:52 ` Benjamin Herrenschmidt
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=ac64e45950c8e3df26fd50e7b251d5895c1d2201.camel@kernel.crashing.org \
--to=benh@kernel.crashing.org \
--cc=ebiederm@xmission.com \
--cc=gregkh@linuxfoundation.org \
--cc=joel@jms.id.au \
--cc=linux-kernel@vger.kernel.org \
--cc=torvalds@linux-foundation.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®