mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Jianyu Zhan <nasa4836@gmail.com>
To: tj@kernel.org, gregkh@linuxfoundation.org
Cc: linux-kernel@vger.kernel.org, nasa4836@gmail.com,
	Jianyu Zhan <Jianyu.Zhan@emc.com>
Subject: [PATCH RFC] sysfs: fix the race of "parent deleted before child added"
Date: Fri,  1 Aug 2014 19:18:12 +0800	[thread overview]
Message-ID: <1406891892-10992-1-git-send-email-nasa4836@gmail.com> (raw)

From: Jianyu Zhan <Jianyu.Zhan@emc.com>

I've met such a race conditon the same as what commit 3a198886
("sysfs: handle 'parent deleted before child added'") tackled.

The senario got triggered under a torturing test of quick disk
removal and plugging.

The forementioned commit 3a198886 didn't really fix this race,
it just narrows down the racy window.

A proper fix for this is to pin the parent kernfs_node on the
dir_entry creation point (sysfs_create_dir_ns), and to de-pin the parent
on the dir_entry deletion point(sysfs_remove_dir).

Though Tejun has added a comment in sysfs_remove_dir to warn such a
race and leave the option to upper layer. But wrt correct logic point,
I think it is good to add such pin behavior.

Note. another commit 26ea12de("kobject: grab an extra reference on kobject->sd
to allow duplicate deletes") also adds such a pin at the kobject layer.
But this commit tackles different issue - it pins an entry(not its parent)
after its creation, to allow duplicate deletions.

Signed-off-by: Jianyu Zhan <nasa4836@gmail.com>
---
 fs/sysfs/dir.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)

diff --git a/fs/sysfs/dir.c b/fs/sysfs/dir.c
index 0b45ff4..0ac82cd 100644
--- a/fs/sysfs/dir.c
+++ b/fs/sysfs/dir.c
@@ -49,7 +49,12 @@ int sysfs_create_dir_ns(struct kobject *kobj, const void *ns)
 	else
 		parent = sysfs_root_kn;
 
-	if (!parent)
+	/*
+	 * We might race with the parent node removal.
+	 * To avoid such race and for logic correctness,
+	 * we pin the parent node here.
+	 */
+	if (!sysfs_get(parent))
 		return -ENOENT;
 
 	kn = kernfs_create_dir_ns(parent, kobject_name(kobj),
@@ -94,6 +99,10 @@ void sysfs_remove_dir(struct kobject *kobj)
 
 	if (kn) {
 		WARN_ON_ONCE(kernfs_type(kn) != KERNFS_DIR);
+		/*
+		 * Drop our pin on parent
+		 */
+		sysfs_put(kn->parent);
 		kernfs_remove(kn);
 	}
 }
-- 
2.0.1.472.g6f92e5f


             reply	other threads:[~2014-08-01 11:18 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-08-01 11:18 Jianyu Zhan [this message]
2014-08-01 12:50 ` Tejun Heo
2014-08-01 14:31   ` Jianyu Zhan
2014-08-01 15:18     ` Tejun Heo
2014-08-01 15:37       ` Jianyu Zhan
2014-08-01 16:54 ` Dan Williams

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=1406891892-10992-1-git-send-email-nasa4836@gmail.com \
    --to=nasa4836@gmail.com \
    --cc=Jianyu.Zhan@emc.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=tj@kernel.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®