Skip to contentSkip to conversation

Use kref for struct file.f_count refcounter

PATCH In review Ravikiran G Thirumalai 2 participants 4 messages Jul 26, 2004 – Jul 27, 2004

Subsystems (7)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Use kref for struct file.f_count refcounter

Andrew,
This patch makes use of the kref api for the struct file.f_count refcounter. This depends on the new kref apis kref_read and kref_put_last added by means of my earlier patch today.

Pls include.

Thanks,
Kiran

 drivers/net/ppp_generic.c |    4 ++--
 fs/affs/file.c            |    4 ++--
 fs/aio.c                  |    4 ++--
 fs/file_table.c           |   29 ++++++++++++++++++++---------
 fs/hfs/inode.c            |    4 ++--
 fs/hfsplus/inode.c        |    4 ++--
 include/linux/fs.h        |    7 ++++---
 security/selinux/hooks.c  |    2 +-
 8 files changed, 35 insertions(+), 23 deletions(-)
drivers/net/ppp_generic.c +2 -2
diff -ruN -X dontdiff2 linux-2.6.7/drivers/net/ppp_generic.c files_struct-kref-2.6.7/drivers/net/ppp_generic.c
@@ -525,12 +525,12 @@
 			if (file == ppp->owner)
 				ppp_shutdown_interface(ppp);
 		}
-		if (atomic_read(&file->f_count) <= 2) {
+		if (file_count(file) <= 2) {
 			ppp_release(inode, file);
 			err = 0;
 		} else
 			printk(KERN_DEBUG "PPPIOCDETACH file->f_count=%d\n",
-			       atomic_read(&file->f_count));
+			       file_count(file));
 		return err;
 	}
 
fs/affs/file.c +2 -2
diff -ruN -X dontdiff2 linux-2.6.7/fs/affs/file.c files_struct-kref-2.6.7/fs/affs/file.c
@@ -62,7 +62,7 @@
 static int
 affs_file_open(struct inode *inode, struct file *filp)
 {
-	if (atomic_read(&filp->f_count) != 1)
+	if (file_count(filp) != 1)
 		return 0;
 	pr_debug("AFFS: open(%d)\n", AFFS_I(inode)->i_opencnt);
 	AFFS_I(inode)->i_opencnt++;
@@ -72,7 +72,7 @@
 static int
 affs_file_release(struct inode *inode, struct file *filp)
 {
-	if (atomic_read(&filp->f_count) != 0)
+	if (file_count(filp) != 0)
 		return 0;
 	pr_debug("AFFS: release(%d)\n", AFFS_I(inode)->i_opencnt);
 	AFFS_I(inode)->i_opencnt--;
fs/aio.c +2 -2
diff -ruN -X dontdiff2 linux-2.6.7/fs/aio.c files_struct-kref-2.6.7/fs/aio.c
@@ -475,7 +475,7 @@
 static int __aio_put_req(struct kioctx *ctx, struct kiocb *req)
 {
 	dprintk(KERN_DEBUG "aio_put(%p): f_count=%d\n",
-		req, atomic_read(&req->ki_filp->f_count));
+		req, file_count(req->ki_filp));
 
 	req->ki_users --;
 	if (unlikely(req->ki_users < 0))
@@ -489,7 +489,7 @@
 	/* Must be done under the lock to serialise against cancellation.
 	 * Call this aio_fput as it duplicates fput via the fput_work.
 	 */
-	if (unlikely(atomic_dec_and_test(&req->ki_filp->f_count))) {
+	if (unlikely(kref_put_last(&req->ki_filp->f_count))) {
 		get_ioctx(ctx);
 		spin_lock(&fput_lock);
 		list_add(&req->ki_list, &fput_head);
fs/file_table.c +20 -9
diff -ruN -X dontdiff2 linux-2.6.7/fs/file_table.c files_struct-kref-2.6.7/fs/file_table.c
@@ -81,7 +81,7 @@
 				goto fail;
 			}
 			eventpoll_init_file(f);
-			atomic_set(&f->f_count, 1);
+			kref_init(&f->f_count);
 			f->f_uid = current->fsuid;
 			f->f_gid = current->fsgid;
 			f->f_owner.lock = RW_LOCK_UNLOCKED;
@@ -118,7 +118,7 @@
 	eventpoll_init_file(filp);
 	filp->f_flags  = flags;
 	filp->f_mode   = (flags+1) & O_ACCMODE;
-	atomic_set(&filp->f_count, 1);
+	kref_init(&filp->f_count);
 	filp->f_dentry = dentry;
 	filp->f_mapping = dentry->d_inode->i_mapping;
 	filp->f_uid    = current->fsuid;
@@ -152,10 +152,17 @@
 
 EXPORT_SYMBOL(close_private_file);
 
+#define kref_to_file(kref) container_of(kref, struct file, f_count)
+
+static void __fput_kref(struct kref *kref)
+{
+	struct file *file = kref_to_file(kref);
+	__fput(file);
+}
+
 void fastcall fput(struct file *file)
 {
-	if (atomic_dec_and_test(&file->f_count))
-		__fput(file);
+	kref_put(&file->f_count, __fput_kref);
 }
 
 EXPORT_SYMBOL(fput);
@@ -235,13 +242,17 @@
 }
 
 
+static void put_filp_kref(struct kref *kref)
+{
+	struct file *file = kref_to_file(kref);
+	security_file_free(file);
+	file_kill(file);
+	file_free(file);
+}
+	
 void put_filp(struct file *file)
 {
-	if (atomic_dec_and_test(&file->f_count)) {
-		security_file_free(file);
-		file_kill(file);
-		file_free(file);
-	}
+	kref_put(&file->f_count, put_filp_kref);
 }
 
 EXPORT_SYMBOL(put_filp);
fs/hfs/inode.c +2 -2
diff -ruN -X dontdiff2 linux-2.6.7/fs/hfs/inode.c files_struct-kref-2.6.7/fs/hfs/inode.c
@@ -523,7 +523,7 @@
 {
 	if (HFS_IS_RSRC(inode))
 		inode = HFS_I(inode)->rsrc_inode;
-	if (atomic_read(&file->f_count) != 1)
+	if (file_count(file) != 1)
 		return 0;
 	atomic_inc(&HFS_I(inode)->opencnt);
 	return 0;
@@ -535,7 +535,7 @@
 
 	if (HFS_IS_RSRC(inode))
 		inode = HFS_I(inode)->rsrc_inode;
-	if (atomic_read(&file->f_count) != 0)
+	if (file_count(file) != 0)
 		return 0;
 	if (atomic_dec_and_test(&HFS_I(inode)->opencnt)) {
 		down(&inode->i_sem);
fs/hfsplus/inode.c +2 -2
diff -ruN -X dontdiff2 linux-2.6.7/fs/hfsplus/inode.c files_struct-kref-2.6.7/fs/hfsplus/inode.c
@@ -268,7 +268,7 @@
 {
 	if (HFSPLUS_IS_RSRC(inode))
 		inode = HFSPLUS_I(inode).rsrc_inode;
-	if (atomic_read(&file->f_count) != 1)
+	if (file_count(file) != 1)
 		return 0;
 	atomic_inc(&HFSPLUS_I(inode).opencnt);
 	return 0;
@@ -280,7 +280,7 @@
 
 	if (HFSPLUS_IS_RSRC(inode))
 		inode = HFSPLUS_I(inode).rsrc_inode;
-	if (atomic_read(&file->f_count) != 0)
+	if (file_count(file) != 0)
 		return 0;
 	if (atomic_dec_and_test(&HFSPLUS_I(inode).opencnt)) {
 		down(&inode->i_sem);
include/linux/fs.h +4 -3
diff -ruN -X dontdiff2 linux-2.6.7/include/linux/fs.h files_struct-kref-2.6.7/include/linux/fs.h
@@ -18,6 +18,7 @@
 #include <linux/cache.h>
 #include <linux/prio_tree.h>
 #include <linux/kobject.h>
+#include <linux/kref.h>
 #include <asm/atomic.h>
 
 struct iovec;
@@ -558,7 +559,7 @@
 	struct dentry		*f_dentry;
 	struct vfsmount         *f_vfsmnt;
 	struct file_operations	*f_op;
-	atomic_t		f_count;
+	struct kref		f_count;
 	unsigned int 		f_flags;
 	mode_t			f_mode;
 	int			f_error;
@@ -584,8 +585,8 @@
 #define file_list_lock() spin_lock(&files_lock);
 #define file_list_unlock() spin_unlock(&files_lock);
 
-#define get_file(x)	atomic_inc(&(x)->f_count)
-#define file_count(x)	atomic_read(&(x)->f_count)
+#define get_file(x)	kref_get(&(x)->f_count)
+#define file_count(x)	kref_read(&(x)->f_count)
 
 /* Initialize and open a private file and allocate its security structure. */
 extern int open_private_file(struct file *, struct dentry *, int);
security/selinux/hooks.c +1 -1
diff -ruN -X dontdiff2 linux-2.6.7/security/selinux/hooks.c files_struct-kref-2.6.7/security/selinux/hooks.c
@@ -1811,7 +1811,7 @@
 						continue;
 					}
 					if (devnull) {
-						atomic_inc(&devnull->f_count);
+						kref_get(&devnull->f_count);
 					} else {
 						devnull = open_devnull();
 						if (!devnull) {
#1 lore raw
Quoted 5 lines · Ravikiran G Thirumalai

This patch makes use of the kref api for the
struct file.f_count refcounter. This depends
on the new kref apis kref_read and kref_put_last
added by means of my earlier patch today.

Sorry, but I can't really see how this improves anything. It'll slow things down infinitesimally and it forces the reader to look elsewhere in the tree to see what's going on.

#2 lore raw
Quoted 11 lines · Andrew Morton
Quoted 5 lines · Ravikiran G Thirumalai

This patch makes use of the kref api for the
struct file.f_count refcounter. This depends
on the new kref apis kref_read and kref_put_last
added by means of my earlier patch today.

Sorry, but I can't really see how this improves anything. It'll slow things down infinitesimally and it forces the reader to look elsewhere in the tree to see what's going on.

It doesn't improve anything in terms of performance or anything. It just makes use of the kref api for refcounting. My next patchset will be to extend the kref api to do lockfree refcounting, and eliminate use of files_struct.file_lock on the reader side (lock free fd lookup) . That improves performance for fd lookups -- for threaded workloads which do lot of io. This was the step by step approach I am following to do lockfree refcounting as was agreed earlier.

I can do a patch to just extend kref api for lockfree refcounting and use them for for the lock free fd lookup patch directly if you like to see it that way.

Thanks,
Kiran

#3 lore raw
Quoted 14 lines · Ravikiran G Thirumalai
Quoted 11 lines · Andrew Morton
Quoted 5 lines · Ravikiran G Thirumalai

This patch makes use of the kref api for the
struct file.f_count refcounter. This depends
on the new kref apis kref_read and kref_put_last
added by means of my earlier patch today.

Sorry, but I can't really see how this improves anything. It'll slow things down infinitesimally and it forces the reader to look elsewhere in the tree to see what's going on.

Quoted 4 lines

It doesn't improve anything in terms of performance or anything. It just makes use of the kref api for refcounting. My next patchset will be to extend the kref api to do lockfree refcounting, and eliminate use of files_struct.file_lock on the reader side (lock free fd lookup) .

That would have been a useful thing to have mentioned in the description of these patches, no?

Quoted 3 lines

I can do a patch to just extend kref api for lockfree refcounting and use them for for the lock free fd lookup patch directly if you like to see it that way.

uh, whatever you think is best will suit at this time.

Please send all the patches, (as in: every single one) at the same time, with complete descriptions. Thanks.