Skip to contentSkip to conversation

sysfs: Take sysfs_mutex when fetching the root inode.

PATCH In review Eric W. Biederman 3 participants 7 messages Jan 21, 2009 – Feb 2, 2009 Driver core

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 sysfs: Take sysfs_mutex when fetching the root inode.

sysfs_get_inode ultimately calls sysfs_count_nlink when the a directory inode is fectched. sysfs_count_nlink needs to be called under the sysfs_mutex to guard against the unlikely but possible scenario that the root directory is changing as we are counting the number entries in it, and just in general to be consistent.

---
 fs/sysfs/mount.c |    2 ++
 1 files changed, 2 insertions(+), 0 deletions(-)
fs/sysfs/mount.c +2 -0
index ab343e3..f3e34fd 100644
@@ -53,7 +53,9 @@ static int sysfs_fill_super(struct super_block *sb, void *data, int silent)
 	sysfs_sb = sb;
 
 	/* get root inode, initialize and unlock it */
+	mutex_lock(&sysfs_mutex);
 	inode = sysfs_get_inode(&sysfs_root);
+	mutex_unlock(&sysfs_mutex);
 	if (!inode) {
 		pr_debug("sysfs: could not get root inode\n");
 		return -ENOMEM;
Signature · 1 line
1.5.6.3
#1 lore raw

PATCH 1/1 sysfs: Reference sysfs_dirent from sysfs inodes.

The sysfs_dirent serves as both an inode and a directory entry for sysfs. To prevent the sysfs inode numbers from being freed prematurely hold a reference to sysfs_dirent from the sysfs inode.

---
 fs/sysfs/inode.c |   10 ++++++++++
 fs/sysfs/mount.c |    1 +
 fs/sysfs/sysfs.h |    1 +
 3 files changed, 12 insertions(+), 0 deletions(-)
fs/sysfs/inode.c +10 -0
index dfa3d94..dd269df 100644
@@ -147,6 +147,7 @@ static void sysfs_init_inode(struct sysfs_dirent *sd, struct inode *inode)
 {
 	struct bin_attribute *bin_attr;
 
+	inode->i_private = sysfs_get(sd);
 	inode->i_mapping->a_ops = &sysfs_aops;
 	inode->i_mapping->backing_dev_info = &sysfs_backing_dev_info;
 	inode->i_op = &sysfs_inode_operations;
@@ -214,6 +215,15 @@ struct inode * sysfs_get_inode(struct sysfs_dirent *sd)
 	return inode;
 }
 
+void sysfs_delete_inode(struct inode *inode)
+{
+	struct sysfs_dirent *sd  = inode->i_private;
+
+	truncate_inode_pages(&inode->i_data, 0);
+	clear_inode(inode);
+	sysfs_put(sd);
+}
+
 int sysfs_hash_and_remove(struct sysfs_dirent *dir_sd, const char *name)
 {
 	struct sysfs_addrm_cxt acxt;
fs/sysfs/mount.c +1 -0
index f3e34fd..59fcd58 100644
@@ -30,6 +30,7 @@ struct kmem_cache *sysfs_dir_cachep;
 static const struct super_operations sysfs_ops = {
 	.statfs		= simple_statfs,
 	.drop_inode	= generic_delete_inode,
+	.delete_inode	= sysfs_delete_inode,
 };
 
 struct sysfs_dirent sysfs_root = {
fs/sysfs/sysfs.h +1 -0
index 93c6d6b..9055d04 100644
@@ -145,6 +145,7 @@ static inline void __sysfs_put(struct sysfs_dirent *sd)
  * inode.c
  */
 struct inode *sysfs_get_inode(struct sysfs_dirent *sd);
+void sysfs_delete_inode(struct inode *inode);
 int sysfs_setattr(struct dentry *dentry, struct iattr *iattr);
 int sysfs_hash_and_remove(struct sysfs_dirent *dir_sd, const char *name);
 int sysfs_inode_init(void);
Signature · 1 line
1.5.6.3
Acked-by on v1 1/1
#6 lore raw
In reply to Eric W. Biederman
Quoted 8 lines · Eric W. Biederman

sysfs_get_inode ultimately calls sysfs_count_nlink when the a directory inode is fectched. sysfs_count_nlink needs to be called under the sysfs_mutex to guard against the unlikely but possible scenario that the root directory is changing as we are counting the number entries in it, and just in general to be consistent.

Thanks.

Signature · 1 line
tejun
#2 lore raw

Re: [PATCH] sysfs: Reference sysfs_dirent from sysfs inodes.

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

The sysfs_dirent serves as both an inode and a directory entry for sysfs. To prevent the sysfs inode numbers from being freed prematurely hold a reference to sysfs_dirent from the sysfs inode.

Thanks for working on this. Can you please add a comment explaining it on top of sysfs_delete_inode()?

Signature · 1 line
tejun
#3 lore raw

Re: [PATCH] sysfs: Reference sysfs_dirent from sysfs inodes.

Quoted 9 lines · On Fri, 23 Jan 2009 15:33:48 +0900 Tejun Heo <[email protected]> wrote:
Quoted 5 lines · Eric W. Biederman

The sysfs_dirent serves as both an inode and a directory entry for sysfs. To prevent the sysfs inode numbers from being freed prematurely hold a reference to sysfs_dirent from the sysfs inode.

Thanks for working on this. Can you please add a comment explaining it on top of sysfs_delete_inode()?

Like this?

fs/sysfs/inode.c renamed from fs/sysfs/inode.c~sysfs-reference-sysfs_dirent-from-sysfs-inodes-fix +7 -0
@@ -215,6 +215,13 @@ struct inode * sysfs_get_inode(struct sy
 	return inode;
 }
 
+/*
+ * The sysfs_dirent serves as both an inode and a directory entry for sysfs.
+ * To prevent the sysfs inode numbers from being freed prematurely we take a
+ * reference to sysfs_dirent from the sysfs inode.  A
+ * super_operations.delete_inode() implementation is needed to drop that
+ * reference upon inode destruction.
+ */
 void sysfs_delete_inode(struct inode *inode)
 {
 	struct sysfs_dirent *sd  = inode->i_private;

_

#4 lore raw

Re: [PATCH] sysfs: Reference sysfs_dirent from sysfs inodes.

Quoted 20 lines · Andrew Morton
Quoted 8 lines · On Fri, 23 Jan 2009 15:33:48 +0900 Tejun Heo <[email protected]> wrote:
Quoted 5 lines · Eric W. Biederman

The sysfs_dirent serves as both an inode and a directory entry for sysfs. To prevent the sysfs inode numbers from being freed prematurely hold a reference to sysfs_dirent from the sysfs inode.

Thanks for working on this. Can you please add a comment explaining it on top of sysfs_delete_inode()?

Like this?

fs/sysfs/inode.c renamed from fs/sysfs/inode.c~sysfs-reference-sysfs_dirent-from-sysfs-inodes-fix +0 -0
@@ -215,6 +215,13 @@ struct inode * sysfs_get_inode(struct sy
 	return inode;
 }
 

fs/sysfs/inode.c struct inode * sysfs_get_inode(struct sy · line 227

Quoted 10 lines
fs/sysfs/inode.c renamed from fs/sysfs/inode.c~sysfs-reference-sysfs_dirent-from-sysfs-inodes-fix +7 -0
+/*
+ * The sysfs_dirent serves as both an inode and a directory entry for sysfs.
+ * To prevent the sysfs inode numbers from being freed prematurely we take a
+ * reference to sysfs_dirent from the sysfs inode.  A
+ * super_operations.delete_inode() implementation is needed to drop that
+ * reference upon inode destruction.
+ */
 void sysfs_delete_inode(struct inode *inode)
 {
 	struct sysfs_dirent *sd  = inode->i_private;

Yeap, looks good to me. :-)

Thanks.

Signature · 1 line
tejun
#5 lore raw

Re: [PATCH] sysfs: Reference sysfs_dirent from sysfs inodes.

Quoted 21 lines · Tejun Heo
Quoted 20 lines · Andrew Morton
Quoted 8 lines · On Fri, 23 Jan 2009 15:33:48 +0900 Tejun Heo <[email protected]> wrote:
Quoted 5 lines · Eric W. Biederman

The sysfs_dirent serves as both an inode and a directory entry for sysfs. To prevent the sysfs inode numbers from being freed prematurely hold a reference to sysfs_dirent from the sysfs inode.

Thanks for working on this. Can you please add a comment explaining it on top of sysfs_delete_inode()?

Like this?

fs/sysfs/inode.c renamed from fs/sysfs/inode.c~sysfs-reference-sysfs_dirent-from-sysfs-inodes-fix +0 -0
@@ -215,6 +215,13 @@ struct inode * sysfs_get_inode(struct sy
 	return inode;
 }
 
Quoted 12 lines

fs/sysfs/inode.c struct inode * sysfs_get_inode(struct sy · line 227

Quoted 10 lines
fs/sysfs/inode.c renamed from fs/sysfs/inode.c~sysfs-reference-sysfs_dirent-from-sysfs-inodes-fix +7 -0
+/*
+ * The sysfs_dirent serves as both an inode and a directory entry for sysfs.
+ * To prevent the sysfs inode numbers from being freed prematurely we take a
+ * reference to sysfs_dirent from the sysfs inode.  A
+ * super_operations.delete_inode() implementation is needed to drop that
+ * reference upon inode destruction.
+ */
 void sysfs_delete_inode(struct inode *inode)
 {
 	struct sysfs_dirent *sd  = inode->i_private;

Yeap, looks good to me. :-)

Thanks guys.

Eric