From: Maneesh Soni The following patch fixes the race involved between unregistering a kobject and simultaneously opeing a corresponding attribute file in sysfs. Ideally sysfs should take a ref. to the kobject as long as it has dentries referring to the kobjects, but because of current limitations in module/kobject ref counting, sysfs's pinning of kobject leads to hang/delays in rmmod of certain modules. The patch checks for unhashed dentries in check_perm() while opening a sysfs file. If the dentry is still hashed then it goes ahead and takes the ref to kobject. This done under the per dentry lock. It does this in the inline routine sysfs_get_kobject(dentry). --- 25-akpm/fs/sysfs/bin.c | 2 +- 25-akpm/fs/sysfs/file.c | 2 +- 25-akpm/fs/sysfs/sysfs.h | 13 +++++++++++++ 3 files changed, 15 insertions(+), 2 deletions(-) diff -puN fs/sysfs/bin.c~sysfs-d_fsdata-race-fix-2 fs/sysfs/bin.c --- 25/fs/sysfs/bin.c~sysfs-d_fsdata-race-fix-2 2004-04-14 11:26:50.330680392 -0700 +++ 25-akpm/fs/sysfs/bin.c 2004-04-14 11:26:50.339679024 -0700 @@ -94,7 +94,7 @@ static ssize_t write(struct file * file, static int open(struct inode * inode, struct file * file) { - struct kobject * kobj = kobject_get(file->f_dentry->d_parent->d_fsdata); + struct kobject *kobj = sysfs_get_kobject(file->f_dentry->d_parent); struct bin_attribute * attr = file->f_dentry->d_fsdata; int error = -EINVAL; diff -puN fs/sysfs/file.c~sysfs-d_fsdata-race-fix-2 fs/sysfs/file.c --- 25/fs/sysfs/file.c~sysfs-d_fsdata-race-fix-2 2004-04-14 11:26:50.331680240 -0700 +++ 25-akpm/fs/sysfs/file.c 2004-04-14 11:26:50.338679176 -0700 @@ -238,7 +238,7 @@ sysfs_write_file(struct file *file, cons static int check_perm(struct inode * inode, struct file * file) { - struct kobject * kobj = kobject_get(file->f_dentry->d_parent->d_fsdata); + struct kobject *kobj = sysfs_get_kobject(file->f_dentry->d_parent); struct attribute * attr = file->f_dentry->d_fsdata; struct sysfs_buffer * buffer; struct sysfs_ops * ops = NULL; diff -puN fs/sysfs/sysfs.h~sysfs-d_fsdata-race-fix-2 fs/sysfs/sysfs.h --- 25/fs/sysfs/sysfs.h~sysfs-d_fsdata-race-fix-2 2004-04-14 11:26:50.333679936 -0700 +++ 25-akpm/fs/sysfs/sysfs.h 2004-04-14 11:26:50.337679328 -0700 @@ -11,3 +11,16 @@ extern void sysfs_hash_and_remove(struct extern int sysfs_create_subdir(struct kobject *, const char *, struct dentry **); extern void sysfs_remove_subdir(struct dentry *); + + +static inline struct kobject *sysfs_get_kobject(struct dentry *dentry) +{ + struct kobject * kobj = NULL; + + spin_lock(&dentry->d_lock); + if (!d_unhashed(dentry)) + kobj = kobject_get(dentry->d_fsdata); + spin_unlock(&dentry->d_lock); + + return kobj; +} _