All of lore.kernel.org
 help / color / mirror / Atom feed
From: Dennis Chen <xschen@tnsoft.com.cn>
To: linux-kernel@vger.kernel.org
Cc: gregkh@linuxfoundation.org, teheo@suse.de,
	xiyou.wangcong@gmail.com, Dennis Chen <xschen@tnsoft.com.cn>
Subject: [PATCH] race condition fixing in sysfs_create_dir
Date: Fri, 26 Jul 2013 17:49:06 +0800	[thread overview]
Message-ID: <51F24612.6030509@tnsoft.com.cn> (raw)

The patch is trying its best to avoid creating a dir under a parent dir which is removing from
the system:

     PATH0 (create a dir under 'PARENT/...')         PATH1 (remove the 'PARENT/...')
          sysfs_create_dir() {                         sysfs_remove_dir() {
          ...                                          ...
          if (kobj->parent)                            spin_lock(&sysfs_assoc_lock);
		parent_sd = kobj->parent->sd;  <----- kobj->sd = NULL;
	 else                                         spin_unlock(&sysfs_assoc_lock);
		parent_sd = &sysfs_root;

Suppose PATH1 enter the critical section first, then PATH0 begin to execute before kobj->sd
has been reset to NULL, possibly PATH0 will get a non-NULL parent_sd since lack of the
sysfs_assoc_lock protection in PATH0. In this case, PATH0 think it has a valid parent_sd which
can be freed by PATH1 in the followed, refer to the comments in the patch. Maybe we need
to figure out a perfect solution to solve the race condition, although the codes in question are
in slow path...

Signed-off-by: Dennis Chen <xschen@tnsoft.com.cn>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: Tejun Heo <teheo@suse.de>
Cc: Wang Cong <xiyou.wangcong@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 e068e74..114073d 100644
--- a/fs/sysfs/dir.c
+++ b/fs/sysfs/dir.c
@@ -746,13 +746,22 @@ int sysfs_create_dir(struct kobject * kobj)
         BUG_ON(!kobj);

+       spin_lock(&sysfs_assoc_lock);
	if (kobj->parent)
		parent_sd = kobj->parent->sd;
	else
		parent_sd = &sysfs_root;

-       if (!parent_sd)
+       if (!parent_sd) {
+               spin_unlock(&sysfs_assoc_lock);
                 return -ENOENT;
+       }
+       spin_unlock(&sysfs_assoc_lock);
+       /* TODO: although the sysfs is in a slow path, but in the operation
+       * followed, we still have a window to let the sysfs_remove_dir to
+       * free the memory space pointered by parent_sd till we inc its ref
+       * count in __sysfs_add_one()
+       */

         if (sysfs_ns_type(parent_sd))
                 ns = kobj->ktype->namespace(kobj);

-- 

1.7.10.4


             reply	other threads:[~2013-07-26  9:46 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-07-26  9:49 Dennis Chen [this message]
2013-07-26  9:59 ` [PATCH] race condition fixing in sysfs_create_dir Dennis Chen
2013-07-26 13:38   ` Tejun Heo
2013-07-26 13:41     ` Tejun Heo
2013-07-30  6:34     ` Dennis Chen
2013-07-30 14:10       ` Tejun Heo

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=51F24612.6030509@tnsoft.com.cn \
    --to=xschen@tnsoft.com.cn \
    --cc=gregkh@linuxfoundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=teheo@suse.de \
    --cc=xiyou.wangcong@gmail.com \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.