linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Benjamin Herrenschmidt <benh@kernel.crashing.org>
To: Linus Torvalds <torvalds@linux-foundation.org>,
	Tejun Heo <tj@kernel.org>
Cc: Linux Kernel Mailing List <linux-kernel@vger.kernel.org>,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	"Eric W. Biederman" <ebiederm@xmission.com>,
	Joel Stanley <joel@jms.id.au>
Subject: Re: [PATCH 2/2] drivers: core: Remove glue dirs from sysfs earlier
Date: Tue, 03 Jul 2018 15:22:47 +1000	[thread overview]
Message-ID: <b15584ccdfcb3bf6d42665635aaa9150ed3244ba.camel@kernel.crashing.org> (raw)
In-Reply-To: <1303e511c44f639c562a02ae28aa530144277c99.camel@kernel.crashing.org>

On Tue, 2018-07-03 at 12:39 +1000, Benjamin Herrenschmidt wrote:
> On Mon, 2018-07-02 at 19:26 -0700, Linus Torvalds wrote:
> > On Mon, Jul 2, 2018 at 7:15 PM Linus Torvalds
> > <torvalds@linux-foundation.org> wrote:
> > > 
> > > It's whitespace-damaged on purpose. It's probably broken shit. DO NOT
> > > USE UNDER ANY CIRCUMSTANCES.  Think of it more as a "something like
> > > this might work, but probably doesn't". Maybe it gives you an idea,
> > > although that idea might be "Linus has finally lost it".
> > 
> > Even if it were to work, it should probably just be done inside kernfs
> > and exposed as some kind of "kernfs_remove_if_empty()" function.
> 
> That would be harder, we'd have to expose it via a
> kobject_del_if_empty() then.
> 
> Since we control completely when things are added/removed from the
> gluedir and have a big fat mutex for it, I don't think locking is much
> of an issue. At least in that specific case.

Ok, kernfs was a tad tougher to crack than I hoped but here's a
prototype lightly tested.

Tejun, Greg, does it look reasonable to you ?

Cheers,
Ben.

diff --git a/drivers/base/core.c b/drivers/base/core.c
index b610816eb887..9166f68276c6 100644
--- a/drivers/base/core.c
+++ b/drivers/base/core.c
@@ -1570,6 +1570,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/fs/kernfs/dir.c b/fs/kernfs/dir.c
index 89d1dc19340b..e4bec09bc602 100644
--- a/fs/kernfs/dir.c
+++ b/fs/kernfs/dir.c
@@ -1251,6 +1251,29 @@ void kernfs_activate(struct kernfs_node *kn)
 	mutex_unlock(&kernfs_mutex);
 }
 
+bool kernfs_has_children(struct kernfs_node *kn)
+{
+	bool has_children = false;
+	struct kernfs_node *pos;
+
+	/* Lockless shortcut */
+	if (RB_EMPTY_NODE(&kn->rb))
+		return false;
+
+	/* Now check for active children */
+	mutex_lock(&kernfs_mutex);
+	pos = NULL;
+	while ((pos = kernfs_next_descendant_post(pos, kn)) && pos != kn) {
+		if (kernfs_active(pos)) {
+			has_children = true;
+			break;
+		}
+	}
+	mutex_unlock(&kernfs_mutex);
+
+	return has_children;
+}
+
 static void __kernfs_remove(struct kernfs_node *kn)
 {
 	struct kernfs_node *pos;
diff --git a/include/linux/kernfs.h b/include/linux/kernfs.h
index ab25c8b6d9e3..776350ac1cbc 100644
--- a/include/linux/kernfs.h
+++ b/include/linux/kernfs.h
@@ -300,6 +300,12 @@ static inline bool kernfs_ns_enabled(struct kernfs_node *kn)
 	return kn->flags & KERNFS_NS;
 }
 
+/**
+ * kernfs_has_children - Returns whether a kernfs node has children.
+ * @kn: the node to test
+ */
+bool kernfs_has_children(struct kernfs_node *kn);
+
 int kernfs_name(struct kernfs_node *kn, char *buf, size_t buflen);
 int kernfs_path_from_node(struct kernfs_node *root_kn, struct kernfs_node *kn,
 			  char *buf, size_t buflen);
diff --git a/include/linux/kobject.h b/include/linux/kobject.h
index 7f6f93c3df9c..1e3294ab95f0 100644
--- a/include/linux/kobject.h
+++ b/include/linux/kobject.h
@@ -116,6 +116,17 @@ 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
+ */
+static inline bool kobject_has_children(struct kobject *kobj)
+{
+	WARN_ON_ONCE(kref_read(&kobj->kref) == 0);
+
+	return kobj->sd && kernfs_has_children(kobj->sd);
+}
+
 struct kobj_type {
 	void (*release)(struct kobject *kobj);
 	const struct sysfs_ops *sysfs_ops;


  reply	other threads:[~2018-07-03  5:23 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   ` [PATCH 2/2] drivers: core: Remove glue dirs from sysfs earlier Benjamin Herrenschmidt
2018-06-29 13:56     ` 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 [this message]
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=b15584ccdfcb3bf6d42665635aaa9150ed3244ba.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=tj@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;
as well as URLs for NNTP newsgroup(s).