From: Maneesh Soni o The following pins the kobject when sysfs assigns dentry and inode to the kobject. This ensures that kobject is alive during the life time of the dentry and inode, and people holding ref. to the dentry can access the kobject without any problems. o The ref. taken for the kobject is released through dentry->d_op->d_iput() call when the dentry ref. count drops to zero and it is being freed. For this sysfs_dentry_operations is introduced. For testing one has to run the following test on a SMP box: 1) Do insmod/rmmod "dummy.o" network driver in a forever loop. 2) Parallely do "find /sys/class/net | xargs cat" also in a forever loop. --- fs/sysfs/dir.c | 15 ++++++++++++++- 1 files changed, 14 insertions(+), 1 deletion(-) diff -puN fs/sysfs/dir.c~sysfs-pin-kobject fs/sysfs/dir.c --- 25/fs/sysfs/dir.c~sysfs-pin-kobject 2004-01-08 21:02:10.000000000 -0800 +++ 25-akpm/fs/sysfs/dir.c 2004-01-08 21:02:10.000000000 -0800 @@ -20,6 +20,18 @@ static int init_dir(struct inode * inode return 0; } +static void sysfs_d_iput(struct dentry * dentry, struct inode * inode) +{ + struct kobject * kobj = dentry->d_fsdata; + + if (kobj) + kobject_put(kobj); + iput(inode); +} + +static struct dentry_operations sysfs_dentry_operations = { + .d_iput = &sysfs_d_iput, +}; static int create_dir(struct kobject * k, struct dentry * p, const char * n, struct dentry ** d) @@ -33,7 +45,8 @@ static int create_dir(struct kobject * k S_IFDIR| S_IRWXU | S_IRUGO | S_IXUGO, init_dir); if (!error) { - (*d)->d_fsdata = k; + (*d)->d_op = &sysfs_dentry_operations; + (*d)->d_fsdata = kobject_get(k); p->d_inode->i_nlink++; } dput(*d); _