Skip to content

Commit

Permalink
kobject: grab an extra reference on kobject->sd to allow duplicate de…
Browse files Browse the repository at this point in the history
…letes

sysfs currently has a rather weird behavior regarding removals.  A
directory removal would delete all files directly under it but
wouldn't recurse into subdirectories, which, while a bit inconsistent,
seems to make sense at the first glance as each directory is
supposedly associated with a kobject and each kobject can take care of
the directory deletion; however, this doesn't really hold as we have
groups which can be directories without a kobject associated with it
and require explicit deletions.

We're in the process of separating out sysfs from kboject / driver
core and want a consistent behavior.  A removal should delete either
only the specified node or everything under it.  I think it is helpful
to support recursive atomic removal and later patches will implement
it.

Such change means that a sysfs_dirent associated with kobject may be
deleted before the kobject itself is removed if one of its ancestor
gets removed before it.  As sysfs_remove_dir() puts the base ref, we
may end up with dangling pointer on descendants.  This can be solved
by holding an extra reference on the sd from kobject.

Acquire an extra reference on the associated sysfs_dirent on directory
creation and put it after removal.

Signed-off-by: Tejun Heo <tj@kernel.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
  • Loading branch information
Tejun Heo authored and Greg Kroah-Hartman committed Oct 3, 2013
1 parent d69ac5a commit 26ea12d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
7 changes: 6 additions & 1 deletion fs/sysfs/dir.c
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,12 @@ void sysfs_remove_one(struct sysfs_addrm_cxt *acxt, struct sysfs_dirent *sd)
{
struct sysfs_inode_attrs *ps_iattr;

BUG_ON(sd->s_flags & SYSFS_FLAG_REMOVED);
/*
* Removal can be called multiple times on the same node. Only the
* first invocation is effective and puts the base ref.
*/
if (sd->s_flags & SYSFS_FLAG_REMOVED)
return;

sysfs_unlink_sibling(sd);

Expand Down
12 changes: 12 additions & 0 deletions lib/kobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,13 @@ static int create_dir(struct kobject *kobj)
if (error)
sysfs_remove_dir(kobj);
}

/*
* @kobj->sd may be deleted by an ancestor going away. Hold an
* extra reference so that it stays until @kobj is gone.
*/
sysfs_get(kobj->sd);

return error;
}

Expand Down Expand Up @@ -532,10 +539,15 @@ int kobject_move(struct kobject *kobj, struct kobject *new_parent)
*/
void kobject_del(struct kobject *kobj)
{
struct sysfs_dirent *sd;

if (!kobj)
return;

sd = kobj->sd;
sysfs_remove_dir(kobj);
sysfs_put(sd);

kobj->state_in_sysfs = 0;
kobj_kset_leave(kobj);
kobject_put(kobj->parent);
Expand Down

0 comments on commit 26ea12d

Please sign in to comment.