Skip to contentSkip to conversation

Sysfs cleanups

PATCH In review Eric W. Biederman 8 participants 200 messages May 20, 2009 – Jun 4, 2009 Page 2 of 2

Subsystems (3)
lore
Share & tools
lore
maintainer
#78 lore raw

Re: [PATCH 04/24] sysfs: Normalize removing sysfs directories.

In reply to Eric W. Biederman
Quoted 46 lines · Eric W. Biederman
Quoted 38 lines · James Bottomley
Quoted 30 lines · Eric W. Biederman
Quoted 19 lines · Tejun Heo
Quoted 9 lines · Eric W. Biederman

Also, I'm quite uncomfortable with these things

Quoted 5 lines

being done in non-atomic manner. It can be made to work but things like this can lead to subtle race conditions and with the kind of layering we put on top of sysfs (kobject, driver model, driver midlayers and so on), it isn't all that easy to verify what's going on, so NACK for this one.

Total nonsense.

Quoted 8 lines

Mucking about with sysfs after we start deleting a directory is a bug. At worst my change makes a buggy race slightly less deterministic.

I am not ready to consider keeping the current unnecessary atomic removal step. That unnecessary atomicity makes the following patches more difficult, and requires a lot of unnecessary retesting.

What do you think the extra unnecessary atomicity helps protect?

Quoted 8 lines

It's just not a clean API. When people are trying to code things way up in the stack, they aren't likely to look up the code to see what assumptions are being made especially when the stack is deep and complex and sysfs is near the bottom of the tall stack. IMHO implementing the usually expected semantics at this depth is worth every effort. It's just good implementation style which might look like wasted effort but will harden the stack in the long run. Plus, it's not like making it atomic is difficult or anything.

Quoted 6 lines

I guess we are going to have to disagree on this one.

My take is simply that a correct user has to wait until no one else can find the kobject before calling kobject_del. At which point races are impossible, and it doesn't matter if sysfs_mutex is held across the entire operation.

Quoted 5 lines

I'm afraid this one isn't a valid assumption. If you look in SCSI, you'll see we do get objects after they've been removed from visibility. We use it as part of the state model for how our objects work (objects removed from visibility are dying, but we still need them to be findable (and gettable).

Quoted 3 lines

I was not precise enough. It appears I overlooked the fact that kobject_del is not always called from kobject_put by way of kobject_release.

OK ... just so you understand, I'm thinking about the device model rather than kobjects. device_del() can't be called from release methods because they're often called from interrupt context and the mutex requirements in device_del() mean it needs user context.

Quoted 17 lines

Strictly the requirement is that after kobject_del we don't add, remove or otherwise manipulate sysfs attributes. That is we don't call any of:

sysfs_add_file
sysfs_create_file
sysfs_create_bin_file
sysfs_remove_file
sysfs_remove_bin_file
sysfs_create_link
sysfs_remove_link
sysfs_create_group
sysfs_remove_group
sysfs_create_subdir
sysfs_remove_subdir

Quoted 2 lines

Those all either oops or BUG today if you try it. So I can't see how a subsystem could depend on those working.

It doesn't; you've altered your requirement. We can fully buy into this new relaxed one.

Quoted 4 lines

Also there is sysfs_remove_dir (on a subdirectory) aka kobject_del on a child object after kobject_del on the parent object.

As best I can tell that only works by fluke today.

Yes, that's an artifact of the fact that the reference counted lifecycle is on release ... del just happens at a certain point in it. We don't hold any counters that tell us what the visibility of our children are, so it's possible to make a parent invisible by calling del simply because you don't know.

Quoted 7 lines
Quoted 3 lines

Now, this could be altered as part of an object lifetime rewrite of SCSI (and I suspect a few other subsystems) but it's certainly an open question of whether the pain is worth the gain.

I won't tell you that sysfs, the kobject layer, or the device layer are the best thing since sliced bread. I'm just trying to simplify the code and get the bugs out.

James

#79 lore raw

Re: [PATCH 04/24] sysfs: Normalize removing sysfs directories.

Quoted 11 lines · James Bottomley
Quoted 11 lines
Quoted 10 lines
Quoted 4 lines

My take is simply that a correct user has to wait until no one else can find the kobject before calling kobject_del. At which point races are impossible, and it doesn't matter if sysfs_mutex is held across the entire operation.

I'm afraid this one isn't a valid assumption. If you look in SCSI, you'll see we do get objects after they've been removed from visibility. We use it as part of the state model for how our objects work (objects removed from visibility are dying, but we still need them to be findable (and gettable).

Quoted 8 lines
Quoted 3 lines

I was not precise enough. It appears I overlooked the fact that kobject_del is not always called from kobject_put by way of kobject_release.

OK ... just so you understand, I'm thinking about the device model rather than kobjects. device_del() can't be called from release methods because they're often called from interrupt context and the mutex requirements in device_del() mean it needs user context.

Makes sense.

Quoted 17 lines
Quoted 17 lines

Strictly the requirement is that after kobject_del we don't add, remove or otherwise manipulate sysfs attributes. That is we don't call any of:

sysfs_add_file
sysfs_create_file
sysfs_create_bin_file
sysfs_remove_file
sysfs_remove_bin_file
sysfs_create_link
sysfs_remove_link
sysfs_create_group
sysfs_remove_group
sysfs_create_subdir
sysfs_remove_subdir

Quoted 5 lines
Quoted 2 lines

Those all either oops or BUG today if you try it. So I can't see how a subsystem could depend on those working.

It doesn't; you've altered your requirement. We can fully buy into this new relaxed one.

My apologies for misstating it earlier. Sometimes translating what is happening in sysfs up to the device model can be a bit of a challenge.

At the sysfs layer the requirement is all the same. Don't mess with a directory as or after you have deleted it.

To recap, my change that Tejun has a problem with is simply that I have refactored sysfs_remove_dir so that if there are directory entries present. A very fast observer in the kernel or in user space can see each directory entry being deleted individually. Before I delete the directory itself.

This is because I now drop and reacquire the sysfs_mutex in between each delete.

As the upper layers must already avoid messing with the attributes of a sysfs directory from the time we call kobject_del I don't see that this makes any difference to them.

Quoted 10 lines
Quoted 4 lines

Also there is sysfs_remove_dir (on a subdirectory) aka kobject_del on a child object after kobject_del on the parent object.

As best I can tell that only works by fluke today.

Yes, that's an artifact of the fact that the reference counted lifecycle is on release ... del just happens at a certain point in it. We don't hold any counters that tell us what the visibility of our children are, so it's possible to make a parent invisible by calling del simply because you don't know.

Strictly speaking my changes don't affect this part either except to issue a warning that something unexpected is going on.

Eric

#108 lore raw

patch driver-core-use-sysfs_rename_link-in-device_rename.patch added to gregkh-2.6 tree

In reply to Eric W. Biederman

This is a note to let you know that I've just added the patch titled

Subject: driver core: Use sysfs_rename_link in device_rename

to my gregkh-2.6 tree. Its filename is

driver-core-use-sysfs_rename_link-in-device_rename.patch

This tree can be found at
http://www.kernel.org/pub/linux/kernel/people/gregkh/gregkh-2.6/patches/

Quoted 1 line

From [email protected] Tue Jun 2 15:34:29 2009

From: "Eric W. Biederman" <[email protected]>
Date: Fri, 29 May 2009 13:19:12 -0700
Subject: driver core: Use sysfs_rename_link in device_rename
To: Andrew Morton <[email protected]>, Greg Kroah-Hartman <[email protected]>
Cc: Tejun Heo <[email protected]>, Cornelia Huck <[email protected]>, <[email protected]>, Kay Sievers <[email protected]>, Greg KH <[email protected]>, "Eric W. Biederman" <[email protected]>, "Eric W. Biederman" <[email protected]>
Message-ID: <[email protected]>

From: Eric W. Biederman <[email protected]>

Don't open code the renaming of symlinks in sysfs instead use the new helper function sysfs_rename_link

---
 drivers/base/core.c |   18 ++++++------------
 1 file changed, 6 insertions(+), 12 deletions(-)
drivers/base/core.c +6 -12
@@ -1546,22 +1546,16 @@ int device_rename(struct device *dev, ch
 	if (old_class_name) {
 		new_class_name = make_class_name(dev->class->name, &dev->kobj);
 		if (new_class_name) {
-			error = sysfs_create_link_nowarn(&dev->parent->kobj,
-							 &dev->kobj,
-							 new_class_name);
-			if (error)
-				goto out;
-			sysfs_remove_link(&dev->parent->kobj, old_class_name);
+			error = sysfs_rename_link(&dev->parent->kobj,
+						  &dev->kobj,
+						  old_class_name,
+						  new_class_name);
 		}
 	}
 #else
 	if (dev->class) {
-		error = sysfs_create_link_nowarn(&dev->class->p->class_subsys.kobj,
-						 &dev->kobj, dev_name(dev));
-		if (error)
-			goto out;
-		sysfs_remove_link(&dev->class->p->class_subsys.kobj,
-				  old_device_name);
+		error = sysfs_rename_link(&dev->class->p->class_subsys.kobj,
+					  &dev->kobj, old_device_name, new_name);
 	}
 #endif
 
#110 lore raw

patch sysfs-remove-now-unnecessary-error-reporting-suppression.patch added to gregkh-2.6 tree

In reply to Eric W. Biederman

This is a note to let you know that I've just added the patch titled

Subject: sysfs: Remove now unnecessary error reporting suppression.

to my gregkh-2.6 tree. Its filename is

sysfs-remove-now-unnecessary-error-reporting-suppression.patch

This tree can be found at
http://www.kernel.org/pub/linux/kernel/people/gregkh/gregkh-2.6/patches/

Quoted 1 line

From [email protected] Tue Jun 2 15:35:03 2009

From: "Eric W. Biederman" <[email protected]>
Date: Fri, 29 May 2009 13:19:13 -0700
Subject: sysfs: Remove now unnecessary error reporting suppression.
To: Andrew Morton <[email protected]>, Greg Kroah-Hartman <[email protected]>
Cc: Tejun Heo <[email protected]>, Cornelia Huck <[email protected]>, <[email protected]>, Kay Sievers <[email protected]>, Greg KH <[email protected]>, "Eric W. Biederman" <[email protected]>, "Eric W. Biederman" <[email protected]>
Message-ID: <[email protected]>

From: Eric W. Biederman <[email protected]>

Now that we use sysfs_rename_link in the places we previously used sysfs_create_link_nowarn we can remove sysfs_create_link_nowarn and all it's supporting infrastructure as it has no callers.

---
 fs/sysfs/dir.c     |   54 +++++++++++------------------------------------------
 fs/sysfs/symlink.c |   42 ++++++++---------------------------------
 fs/sysfs/sysfs.h   |    1 
 3 files changed, 21 insertions(+), 76 deletions(-)
fs/sysfs/dir.c +12 -42
@@ -397,43 +397,6 @@ void sysfs_addrm_start(struct sysfs_addr
 }
 
 /**
- *	__sysfs_add_one - add sysfs_dirent to parent without warning
- *	@acxt: addrm context to use
- *	@sd: sysfs_dirent to be added
- *
- *	Get @acxt->parent_sd and set sd->s_parent to it and increment
- *	nlink of parent inode if @sd is a directory and link into the
- *	children list of the parent.
- *
- *	This function should be called between calls to
- *	sysfs_addrm_start() and sysfs_addrm_finish() and should be
- *	passed the same @acxt as passed to sysfs_addrm_start().
- *
- *	LOCKING:
- *	Determined by sysfs_addrm_start().
- *
- *	RETURNS:
- *	0 on success, -EEXIST if entry with the given name already
- *	exists.
- */
-int __sysfs_add_one(struct sysfs_addrm_cxt *acxt, struct sysfs_dirent *sd)
-{
-	if (sysfs_find_dirent(acxt->parent_sd, sd->s_name))
-		return -EEXIST;
-
-	sd->s_parent = sysfs_get(acxt->parent_sd);
-
-	if (sysfs_type(sd) == SYSFS_DIR && acxt->parent_inode)
-		inc_nlink(acxt->parent_inode);
-
-	acxt->cnt++;
-
-	sysfs_link_sibling(sd);
-
-	return 0;
-}
-
-/**
  *	sysfs_pathname - return full path to sysfs dirent
  *	@sd: sysfs_dirent whose path we want
  *	@path: caller allocated buffer
@@ -475,10 +438,7 @@ static char *sysfs_pathname(struct sysfs
  */
 int sysfs_add_one(struct sysfs_addrm_cxt *acxt, struct sysfs_dirent *sd)
 {
-	int ret;
-
-	ret = __sysfs_add_one(acxt, sd);
-	if (ret == -EEXIST) {
+	if (sysfs_find_dirent(acxt->parent_sd, sd->s_name)) {
 		char *path = kzalloc(PATH_MAX, GFP_KERNEL);
 		WARN(1, KERN_WARNING
 		     "sysfs: cannot create duplicate filename '%s'\n",
@@ -486,9 +446,19 @@ int sysfs_add_one(struct sysfs_addrm_cxt
 		     strcat(strcat(sysfs_pathname(acxt->parent_sd, path), "/"),
 		            sd->s_name));
 		kfree(path);
+		return -EEXIST;
 	}
 
-	return ret;
+	sd->s_parent = sysfs_get(acxt->parent_sd);
+
+	if (sysfs_type(sd) == SYSFS_DIR && acxt->parent_inode)
+		inc_nlink(acxt->parent_inode);
+
+	acxt->cnt++;
+
+	sysfs_link_sibling(sd);
+
+	return 0;
 }
 
 /**
fs/sysfs/symlink.c +9 -33

60 lines not on this page · raw message

fs/sysfs/sysfs.h +0 -1
@@ -108,7 +108,6 @@ struct sysfs_dirent *sysfs_get_active_tw
 void sysfs_put_active_two(struct sysfs_dirent *sd);
 void sysfs_addrm_start(struct sysfs_addrm_cxt *acxt,
 		       struct sysfs_dirent *parent_sd);
-int __sysfs_add_one(struct sysfs_addrm_cxt *acxt, struct sysfs_dirent *sd);
 int sysfs_add_one(struct sysfs_addrm_cxt *acxt, struct sysfs_dirent *sd);
 void sysfs_remove_one(struct sysfs_addrm_cxt *acxt, struct sysfs_dirent *sd);
 void sysfs_addrm_finish(struct sysfs_addrm_cxt *acxt);
#106 lore raw

patch sysfs-implement-sysfs_rename_link.patch added to gregkh-2.6 tree

In reply to Eric W. Biederman

This is a note to let you know that I've just added the patch titled

Subject: sysfs: Implement sysfs_rename_link

to my gregkh-2.6 tree. Its filename is

sysfs-implement-sysfs_rename_link.patch

This tree can be found at
http://www.kernel.org/pub/linux/kernel/people/gregkh/gregkh-2.6/patches/

Quoted 1 line

From [email protected] Tue Jun 2 15:34:06 2009

From: "Eric W. Biederman" <[email protected]>
Date: Fri, 29 May 2009 13:19:11 -0700
Subject: sysfs: Implement sysfs_rename_link
To: Andrew Morton <[email protected]>, Greg Kroah-Hartman <[email protected]>
Cc: Tejun Heo <[email protected]>, Cornelia Huck <[email protected]>, <[email protected]>, Kay Sievers <[email protected]>, Greg KH <[email protected]>, "Eric W. Biederman" <[email protected]>, Benjamin Thery <[email protected]>, Daniel Lezcano <[email protected]>, "Eric W. Biederman" <[email protected]>
Message-ID: <[email protected]>

From: Eric W. Biederman <[email protected]>

Because of rename ordering problems we occassionally give false warnings about invalid sysfs operations, so implement a helper function for this common sysfs idiom.

This is a stripped down version of an earlier patch that also added sysfs_delete_link.

---
 fs/sysfs/symlink.c    |   16 ++++++++++++++++
 include/linux/sysfs.h |    9 +++++++++
 2 files changed, 25 insertions(+)
fs/sysfs/symlink.c +16 -0
@@ -122,6 +122,22 @@ void sysfs_remove_link(struct kobject * 
 	sysfs_hash_and_remove(parent_sd, name);
 }
 
+/**
+ *	sysfs_rename_link - rename symlink in object's directory.
+ *	@kobj:	object we're acting for.
+ *	@targ:	object we're pointing to.
+ *	@old:	previous name of the symlink.
+ *	@new:	new name of the symlink.
+ *
+ *	A helper function for the common rename symlink idiom.
+ */
+int sysfs_rename_link(struct kobject *kobj, struct kobject *targ,
+			const char *old, const char *new)
+{
+	sysfs_remove_link(kobj, old);
+	return sysfs_create_link(kobj, targ, new);
+}
+
 static int sysfs_get_target_path(struct sysfs_dirent *parent_sd,
 				 struct sysfs_dirent *target_sd, char *path)
 {
include/linux/sysfs.h +9 -0
@@ -109,6 +109,9 @@ int __must_check sysfs_create_link_nowar
 					  const char *name);
 void sysfs_remove_link(struct kobject *kobj, const char *name);
 
+int sysfs_rename_link(struct kobject *kobj, struct kobject *target,
+			const char *old_name, const char *new_name);
+
 int __must_check sysfs_create_group(struct kobject *kobj,
 				    const struct attribute_group *grp);
 int sysfs_update_group(struct kobject *kobj,
@@ -202,6 +205,12 @@ static inline void sysfs_remove_link(str
 {
 }
 
+static inline int sysfs_rename_link(struct kobject *k, struct kobject *t,
+				    const char *old_name, const char *new_name)
+{
+	return 0;
+}
+
 static inline int sysfs_create_group(struct kobject *kobj,
 				     const struct attribute_group *grp)
 {
#112 lore raw

Re: [PATCH 04/26] sysfs: sysfs_remove_dir stop checking for bogus cases.

In reply to Eric W. Biederman
Quoted 4 lines · Eric W. Biederman

From: Eric W. Biederman <[email protected]>

kobj->sd can not be NULL in sysfs_remove_dir.

Quoted 8 lines

sysfs_remove_dir is only called from kobject_add (to clean up after failure) and from kobject_del at the end of a kobject's life. In both cases kobject_add has already called sysfs_create_dir successfully. The only writers of kobj->sd are sysfs_create_dir on sucess and sysfs_remove_dir when it clears the kobj just before deleting the directory.

Which means at the time sysfs_remove_dir is called kobj->sd will be valid.

Yeah, we would hope so.

But as we have been forced to add many checks like this into the driver core to handle those "no one could ever call this" type problems that have been springing up over time, I am hesitant to remove this check.

Why do you want to remove it, what is the problem here you are trying to solve?

thanks,

greg k-h

#113 lore raw

Re: [PATCH 04/26] sysfs: sysfs_remove_dir stop checking for bogus cases.

Quoted 14 lines · Greg KH
Quoted 4 lines · Eric W. Biederman

From: Eric W. Biederman <[email protected]>

kobj->sd can not be NULL in sysfs_remove_dir.

Quoted 8 lines

sysfs_remove_dir is only called from kobject_add (to clean up after failure) and from kobject_del at the end of a kobject's life. In both cases kobject_add has already called sysfs_create_dir successfully. The only writers of kobj->sd are sysfs_create_dir on sucess and sysfs_remove_dir when it clears the kobj just before deleting the directory.

Which means at the time sysfs_remove_dir is called kobj->sd will be valid.

Quoted 8 lines

Yeah, we would hope so.

But as we have been forced to add many checks like this into the driver core to handle those "no one could ever call this" type problems that have been springing up over time, I am hesitant to remove this check.

Why do you want to remove it, what is the problem here you are trying to solve?

I'm cleaning up and simplifying the code.

In this particular instance the check is actually wrong. There is no
valid code path that depends on that behavior. Which means the test
is both useless and actively obscures the code.

Making it a BUG_ON would be valid.

The other side of it is I realized I had been running for months without that check. I had deleted it as an oversight. Then when the code churn was small enough and Tejun asked me about it. I split this hunk out into it's own separate patch. As I figured it deserved it's own patch so the change could stand out and be reviewed. Since my review of the code path showed no valid use case I kept it.

Eric