fuse: Further reducing contention of fc->lock
Subsystems (2)
v2 cover fuse: Further reducing contention of fc->lock
v2: Removed patch [1/6] from v1, and [4/6] from v1 is replaced
with new patch (which is [2/5] in this v2).
There was introduced fc->bg_lock to reduce the contention, and this patchset continues this efforts.
This patchset introduces per fuse_inode lock to protect inode metadata, synchronization with background writes, etc. All of the above is related to a single inode, and there is no a reason, that inodes are concurrents of each other to make some of actions exclusive.
So, here we introduce fuse_inode::lock spinlock and get rid of fc->lock in many places.
---
Kirill Tkhai (5):
fuse: Add fuse_inode argument to fuse_prepare_release()
fuse: Convert fuse_conn::attr_version into atomic64_t
fuse: Introduce fuse_inode::lock to protect write related fields and statistics
fuse: Protect fuse_inode::nlookup with fuse_inode::lock
fuse: Protect fuse_file::reserved_req via corresponding fuse_inode::lock
fs/fuse/cuse.c | 3 + fs/fuse/dev.c | 10 +++-- fs/fuse/dir.c | 54 ++++++++++---------------- fs/fuse/file.c | 111 ++++++++++++++++++++++++++++------------------------- fs/fuse/fuse_i.h | 23 ++++++++--- fs/fuse/inode.c | 17 +++++--- fs/fuse/readdir.c | 4 +- 7 files changed, 115 insertions(+), 107 deletions(-)
--
PATCH v2 1/5 fuse: Add fuse_inode argument to fuse_prepare_release()
Here is preparation for next patches, which introduce new fuse_inode::lock for protection fuse_file::write_entry linked into fuse_inode::write_files.
This patch just passes new argument to the function.
--- fs/fuse/cuse.c | 3 ++- fs/fuse/dir.c | 6 ++++-- fs/fuse/file.c | 10 ++++++---- fs/fuse/fuse_i.h | 2 +- 4 files changed, 13 insertions(+), 8 deletions(-)
fs/fuse/cuse.c +2 -1
@@ -141,10 +141,11 @@ static int cuse_open(struct inode *inode, struct file *file)static int cuse_release (struct inode *inode,struct file *file) { +struct fuse_inode *fi =get_fuse_inode (inode);struct fuse_file *ff = file->private_data;struct fuse_conn *fc = ff->fc; -fuse_sync_release (ff, file->f_flags); +fuse_sync_release (fi, ff, file->f_flags);fuse_conn_put (fc);return 0 ;
fs/fuse/dir.c +4 -2
@@ -400,6 +400,7 @@ static int fuse_create_open(struct inode *dir, struct dentry *entry,struct fuse_create_in inarg;struct fuse_open_out outopen;struct fuse_entry_out outentry; +struct fuse_inode *fi;struct fuse_file *ff;/* Userspace expects S_IFREG in create mode */
@@ -451,7 +452,7 @@ static int fuse_create_open(struct inode *dir, struct dentry *entry, &outentry.attr, entry_attr_timeout(&outentry), 0);if (!inode) { flags &= ~(O_CREAT |O_EXCL |O_TRUNC ); -fuse_sync_release (ff, flags); +fuse_sync_release (NULL , ff, flags);fuse_queue_forget (fc, forget, outentry.nodeid,1 ); err = -ENOMEM ; goto out_err;
@@ -462,7 +463,8 @@ static int fuse_create_open(struct inode *dir, struct dentry *entry,fuse_dir_changed (dir); err =finish_open (file, entry, generic_file_open);if (err) { -fuse_sync_release (ff, flags); + fi =get_fuse_inode (inode); +fuse_sync_release (fi, ff, flags); }else { file->private_data = ff;fuse_finish_open (inode, file);
fs/fuse/file.c +6 -4
@@ -224,7 +224,8 @@ int fuse_open_common(struct inode *inode, struct file *file, bool isdir)return err; } -static void fuse_prepare_release (struct fuse_file *ff,int flags,int opcode) +static void fuse_prepare_release (struct fuse_inode *fi,struct fuse_file *ff, +int flags,int opcode) {struct fuse_conn *fc = ff->fc;struct fuse_req *req = ff->reserved_req;
@@ -249,10 +250,11 @@ static void fuse_prepare_release(struct fuse_file *ff, int flags, int opcode)void fuse_release_common (struct file *file,int opcode) { +struct fuse_inode *fi =get_fuse_inode (file_inode (file));struct fuse_file *ff = file->private_data;struct fuse_req *req = ff->reserved_req; -fuse_prepare_release (ff, file->f_flags, opcode); +fuse_prepare_release (fi, ff, file->f_flags, opcode);if (ff->flock) {struct fuse_release_in *inarg = &req->misc.release.in;
@@ -294,10 +296,10 @@ static int fuse_release(struct inode *inode, struct file *file)return 0 ; } -void fuse_sync_release (struct fuse_file *ff,int flags) +void fuse_sync_release (struct fuse_inode *fi,struct fuse_file *ff,int flags) {WARN_ON (refcount_read (&ff->count) >1 ); -fuse_prepare_release (ff, flags,FUSE_RELEASE ); +fuse_prepare_release (fi, ff, flags,FUSE_RELEASE );/* * iput(NULL) is a no-op and since the refcount is 1 and everything's * synchronous, we are fine with not doing igrab() here"
fs/fuse/fuse_i.h +1 -1
@@ -817,7 +817,7 @@ struct fuse_file *fuse_file_alloc(struct fuse_conn *fc);void fuse_file_free (struct fuse_file *ff);void fuse_finish_open (struct inode *inode,struct file *file); -void fuse_sync_release (struct fuse_file *ff,int flags); +void fuse_sync_release (struct fuse_inode *fi,struct fuse_file *ff,int flags);/** * Send RELEASE or RELEASEDIR request
PATCH v2 2/5 fuse: Convert fuse_conn::attr_version into atomic64_t
In reply to Kirill TkhaiThis patch makes fuse_conn::attr_version of atomic64_t type, so fc->lock won't be needed to read or modify it anymore.
v2: New
--- fs/fuse/dir.c | 19 ++----------------- fs/fuse/file.c | 8 ++++---- fs/fuse/fuse_i.h | 9 ++++++--- fs/fuse/inode.c | 4 ++-- 4 files changed, 14 insertions(+), 26 deletions(-)
fs/fuse/dir.c +2 -17
@@ -149,21 +149,6 @@ static void fuse_lookup_init(struct fuse_conn *fc, struct fuse_args *args, args->out.args[0 ].value = outarg; } -u64 fuse_get_attr_version (struct fuse_conn *fc) -{ -u64 curr_version; - -/* -* The spin lock isn't actually needed on 64bit archs, but we -* don't yet care too much about such optimizations. -*/ -spin_lock (&fc->lock); - curr_version = fc->attr_version; -spin_unlock (&fc->lock); - -return curr_version; -} -/* * Check whether the dentry is still valid *
@@ -674,7 +659,7 @@ static int fuse_unlink(struct inode *dir, struct dentry *entry)struct fuse_inode *fi =get_fuse_inode (inode);spin_lock (&fc->lock); - fi->attr_version = ++fc->attr_version; + fi->attr_version =atomic64_inc_return (&fc->attr_version);/* * If i_nlink == 0 then unlink doesn't make sense, yet this can * happen if userspace filesystem is careless. It would be
@@ -828,7 +813,7 @@ static int fuse_link(struct dentry *entry, struct inode *newdir,struct fuse_inode *fi =get_fuse_inode (inode);spin_lock (&fc->lock); - fi->attr_version = ++fc->attr_version; + fi->attr_version =atomic64_inc_return (&fc->attr_version);inc_nlink (inode);spin_unlock (&fc->lock);fuse_invalidate_attr (inode);
fs/fuse/file.c +4 -4
@@ -187,7 +187,7 @@ void fuse_finish_open(struct inode *inode, struct file *file)struct fuse_inode *fi =get_fuse_inode (inode);spin_lock (&fc->lock); - fi->attr_version = ++fc->attr_version; + fi->attr_version =atomic64_inc_return (&fc->attr_version);i_size_write (inode,0 );spin_unlock (&fc->lock);fuse_invalidate_attr (inode);
@@ -599,7 +599,7 @@ static void fuse_aio_complete(struct fuse_io_priv *io, int err, ssize_t pos)struct fuse_inode *fi =get_fuse_inode (inode);spin_lock (&fc->lock); - fi->attr_version = ++fc->attr_version; + fi->attr_version =atomic64_inc_return (&fc->attr_version);spin_unlock (&fc->lock); }
@@ -678,7 +678,7 @@ static void fuse_read_update_size(struct inode *inode, loff_t size,spin_lock (&fc->lock);if (attr_ver == fi->attr_version && size < inode->i_size && !test_bit (FUSE_I_SIZE_UNSTABLE , &fi->state)) { - fi->attr_version = ++fc->attr_version; + fi->attr_version =atomic64_inc_return (&fc->attr_version);i_size_write (inode, size); }spin_unlock (&fc->lock);
@@ -997,7 +997,7 @@ bool fuse_write_update_size(struct inode *inode, loff_t pos)bool ret = false;spin_lock (&fc->lock); - fi->attr_version = ++fc->attr_version; + fi->attr_version =atomic64_inc_return (&fc->attr_version);if (pos > inode->i_size) {i_size_write (inode, pos); ret = true;
fs/fuse/fuse_i.h +6 -3
@@ -730,7 +730,7 @@ struct fuse_conn {struct fuse_req *destroy_req;/** Version counter for attribute changes */ -u64 attr_version; +atomic64_t attr_version;/** Called on final put */ void (*release)(struct fuse_conn *);
@@ -770,6 +770,11 @@ static inline int invalid_nodeid(u64 nodeid)return !nodeid || nodeid ==FUSE_ROOT_ID ; } +static inline u64 fuse_get_attr_version (struct fuse_conn *fc) +{ +return atomic64_read (&fc->attr_version); +} +/** Device operations */ extern const struct file_operations fuse_dev_operations;
@@ -1000,8 +1005,6 @@ void fuse_flush_writepages(struct inode *inode);void fuse_set_nowrite (struct inode *inode);void fuse_release_nowrite (struct inode *inode); -u64 fuse_get_attr_version (struct fuse_conn *fc); -/** * File-system tells the kernel to invalidate cache for the given node id. */
fs/fuse/inode.c +2 -2
@@ -164,7 +164,7 @@ void fuse_change_attributes_common(struct inode *inode, struct fuse_attr *attr,struct fuse_conn *fc =get_fuse_conn (inode);struct fuse_inode *fi =get_fuse_inode (inode); - fi->attr_version = ++fc->attr_version; + fi->attr_version =atomic64_inc_return (&fc->attr_version); fi->i_time = attr_valid;WRITE_ONCE (fi->inval_mask,0 );
@@ -625,7 +625,7 @@ void fuse_conn_init(struct fuse_conn *fc, struct user_namespace *user_ns) fc->blocked =0 ; fc->initialized =0 ; fc->connected =1 ; - fc->attr_version =1 ; +atomic64_set (&fc->attr_version,1 );get_random_bytes (&fc->scramble_key,sizeof (fc->scramble_key)); fc->pid_ns =get_pid_ns (task_active_pid_ns (current)); fc->user_ns =get_user_ns (user_ns);
PATCH v2 3/5 fuse: Introduce fuse_inode::lock to protect write related fields and statistics
In reply to Kirill TkhaiTo minimize contention of fuse_conn::lock, this patch introduces a new spinlock for protection fuse_inode metadata:
fuse_inode:: writectr writepages write_files queued_writes attr_version inode:: i_size i_nlink i_mtime i_ctime
Also, it protects the fields changed in fuse_change_attributes_common() (too many to list).
--- fs/fuse/dir.c | 25 +++++++-------- fs/fuse/file.c | 93 ++++++++++++++++++++++++++++-------------------------- fs/fuse/fuse_i.h | 7 +++- fs/fuse/inode.c | 9 +++-- 4 files changed, 72 insertions(+), 62 deletions(-)
fs/fuse/dir.c +12 -13
@@ -658,7 +658,7 @@ static int fuse_unlink(struct inode *dir, struct dentry *entry)struct inode *inode =d_inode (entry);struct fuse_inode *fi =get_fuse_inode (inode); -spin_lock (&fc->lock); +spin_lock (&fi->lock); fi->attr_version =atomic64_inc_return (&fc->attr_version);/* * If i_nlink == 0 then unlink doesn't make sense, yet this can
@@ -668,7 +668,7 @@ static int fuse_unlink(struct inode *dir, struct dentry *entry)*/ if (inode->i_nlink >0 )drop_nlink (inode); -spin_unlock (&fc->lock); +spin_unlock (&fi->lock);fuse_invalidate_attr (inode);fuse_dir_changed (dir);fuse_invalidate_entry_cache (entry);
@@ -812,10 +812,10 @@ static int fuse_link(struct dentry *entry, struct inode *newdir,if (!err) {struct fuse_inode *fi =get_fuse_inode (inode); -spin_lock (&fc->lock); +spin_lock (&fi->lock); fi->attr_version =atomic64_inc_return (&fc->attr_version);inc_nlink (inode); -spin_unlock (&fc->lock); +spin_unlock (&fi->lock);fuse_invalidate_attr (inode);fuse_update_ctime (inode); }else if (err == -EINTR ) {
@@ -1323,15 +1323,14 @@ static void iattr_to_fattr(struct fuse_conn *fc, struct iattr *iattr,*/ void fuse_set_nowrite (struct inode *inode) { -struct fuse_conn *fc =get_fuse_conn (inode);struct fuse_inode *fi =get_fuse_inode (inode);BUG_ON (!inode_is_locked (inode)); -spin_lock (&fc->lock); +spin_lock (&fi->lock);BUG_ON (fi->writectr <0 ); fi->writectr +=FUSE_NOWRITE ; -spin_unlock (&fc->lock); +spin_unlock (&fi->lock);wait_event (fi->page_waitq, fi->writectr ==FUSE_NOWRITE ); }
@@ -1352,11 +1351,11 @@ static void __fuse_release_nowrite(struct inode *inode)void fuse_release_nowrite (struct inode *inode) { -struct fuse_conn *fc =get_fuse_conn (inode); +struct fuse_inode *fi =get_fuse_inode (inode); -spin_lock (&fc->lock); +spin_lock (&fi->lock);__fuse_release_nowrite (inode); -spin_unlock (&fc->lock); +spin_unlock (&fi->lock); } static void fuse_setattr_fill(struct fuse_conn *fc, struct fuse_args *args,
@@ -1491,7 +1490,7 @@ int fuse_do_setattr(struct dentry *dentry, struct iattr *attr, goto error; } -spin_lock (&fc->lock); +spin_lock (&fi->lock);/* the kernel maintains i_mtime locally */ if (trust_local_cmtime) {if (attr->ia_valid &ATTR_MTIME )
@@ -1509,10 +1508,10 @@ int fuse_do_setattr(struct dentry *dentry, struct iattr *attr,i_size_write (inode, outarg.attr.size);if (is_truncate) { -/* NOTE: this may release/reacquire fc->lock */ +/* NOTE: this may release/reacquire fi->lock */ __fuse_release_nowrite (inode); } -spin_unlock (&fc->lock); +spin_unlock (&fi->lock);/* * Only call invalidate_inode_pages2() after removing
fs/fuse/file.c +48 -45
287 lines not on this page · raw message
fs/fuse/fuse_i.h +6 -1
@@ -96,7 +96,7 @@ struct fuse_inode {union {/* Write related fields (regular file only) */ struct { -/* Files usable in writepage. Protected by fc->lock */ +/* Files usable in writepage. Protected by fi->lock */ struct list_head write_files;/* Writepages pending on truncate or fsync */
@@ -144,6 +144,9 @@ struct fuse_inode {/** Lock for serializing lookup and readdir for back compatibility*/ struct mutex mutex; + +/** Lock to protect write related fields */ +spinlock_t lock; };/** FUSE inode state bits */
@@ -500,6 +503,8 @@ struct fuse_dev {* This structure is created, when the filesystem is mounted, and is * destroyed, when the client device is closed and the filesystem is * unmounted. +* +* Locking order: fuse_inode::lock -> fuse_conn::lock -> fuse_conn::bg_lock */ struct fuse_conn {/** Lock protecting accessess to members of this structure */
fs/fuse/inode.c +6 -3
@@ -97,6 +97,7 @@ static struct inode *fuse_alloc_inode(struct super_block *sb) fi->orig_ino =0 ; fi->state =0 ;mutex_init (&fi->mutex); +spin_lock_init (&fi->lock); fi->forget =fuse_alloc_forget ();if (!fi->forget) {kmem_cache_free (fuse_inode_cachep, inode);
@@ -164,6 +165,8 @@ void fuse_change_attributes_common(struct inode *inode, struct fuse_attr *attr,struct fuse_conn *fc =get_fuse_conn (inode);struct fuse_inode *fi =get_fuse_inode (inode); +lockdep_assert_held (&fi->lock); + fi->attr_version =atomic64_inc_return (&fc->attr_version); fi->i_time = attr_valid;WRITE_ONCE (fi->inval_mask,0 );
@@ -210,10 +213,10 @@ void fuse_change_attributes(struct inode *inode, struct fuse_attr *attr,loff_t oldsize;struct timespec64 old_mtime; -spin_lock (&fc->lock); +spin_lock (&fi->lock);if ((attr_version !=0 && fi->attr_version > attr_version) ||test_bit (FUSE_I_SIZE_UNSTABLE , &fi->state)) { -spin_unlock (&fc->lock); +spin_unlock (&fi->lock);return ; }
@@ -228,7 +231,7 @@ void fuse_change_attributes(struct inode *inode, struct fuse_attr *attr,*/ if (!is_wb || !S_ISREG (inode->i_mode))i_size_write (inode, attr->size); -spin_unlock (&fc->lock); +spin_unlock (&fi->lock);if (!is_wb &&S_ISREG (inode->i_mode)) {bool inval = false;
PATCH v2 4/5 fuse: Protect fuse_inode::nlookup with fuse_inode::lock
In reply to Kirill TkhaiThis continues previous patch and introduces the same protection for nlookup field.
--- fs/fuse/dir.c | 4 ++-- fs/fuse/inode.c | 4 ++-- fs/fuse/readdir.c | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-)
fs/fuse/dir.c +2 -2
@@ -207,9 +207,9 @@ static int fuse_dentry_revalidate(struct dentry *entry, unsigned int flags)fuse_queue_forget (fc, forget, outarg.nodeid,1 ); goto invalid; } -spin_lock (&fc->lock); +spin_lock (&fi->lock); fi->nlookup++; -spin_unlock (&fc->lock); +spin_unlock (&fi->lock); }kfree (forget);if (ret == -ENOMEM )
fs/fuse/inode.c +2 -2
@@ -326,9 +326,9 @@ struct inode *fuse_iget(struct super_block *sb, u64 nodeid, } fi =get_fuse_inode (inode); -spin_lock (&fc->lock); +spin_lock (&fi->lock); fi->nlookup++; -spin_unlock (&fc->lock); +spin_unlock (&fi->lock);fuse_change_attributes (inode, attr, attr_valid, attr_version);return inode;
fs/fuse/readdir.c +2 -2
@@ -213,9 +213,9 @@ static int fuse_direntplus_link(struct file *file, } fi =get_fuse_inode (inode); -spin_lock (&fc->lock); +spin_lock (&fi->lock); fi->nlookup++; -spin_unlock (&fc->lock); +spin_unlock (&fi->lock);forget_all_cached_acls (inode); fuse_change_attributes(inode, &o->attr,
PATCH v2 5/5 fuse: Protect fuse_file::reserved_req via corresponding fuse_inode::lock
In reply to Kirill TkhaiThis is rather natural action after previous patches, and it just decreases load of fc->lock.
--- fs/fuse/dev.c | 10 ++++++---- fs/fuse/fuse_i.h | 5 ++++- 2 files changed, 10 insertions(+), 5 deletions(-)
fs/fuse/dev.c +6 -4
@@ -247,17 +247,18 @@ static struct fuse_req *get_reserved_req(struct fuse_conn *fc, struct file *file) {struct fuse_req *req =NULL ; +struct fuse_inode *fi =get_fuse_inode (file_inode (file));struct fuse_file *ff = file->private_data;do {wait_event (fc->reserved_req_waitq, ff->reserved_req); -spin_lock (&fc->lock); +spin_lock (&fi->lock);if (ff->reserved_req) { req = ff->reserved_req; ff->reserved_req =NULL ; req->stolen_file =get_file (file); } -spin_unlock (&fc->lock); +spin_unlock (&fi->lock); }while (!req);return req;
@@ -269,16 +270,17 @@ static struct fuse_req *get_reserved_req(struct fuse_conn *fc,static void put_reserved_req (struct fuse_conn *fc,struct fuse_req *req) {struct file *file = req->stolen_file; +struct fuse_inode *fi =get_fuse_inode (file_inode (file));struct fuse_file *ff = file->private_data;WARN_ON (req->max_pages); -spin_lock (&fc->lock); +spin_lock (&fi->lock);memset (req,0 ,sizeof (*req));fuse_request_init (req,NULL ,NULL ,0 );BUG_ON (ff->reserved_req); ff->reserved_req = req;wake_up_all (&fc->reserved_req_waitq); -spin_unlock (&fc->lock); +spin_unlock (&fi->lock);fput (file); }
fs/fuse/fuse_i.h +4 -1
@@ -166,7 +166,10 @@ struct fuse_file {/** Fuse connection for this file */ struct fuse_conn *fc; -/** Request reserved for flush and release */ +/* +* Request reserved for flush and release. +* Modified under relative fuse_inode::lock. +*/ struct fuse_req *reserved_req;/** Kernel file handle guaranteed to be unique */
ping
Quoted 37 lines · Kirill Tkhai
v2: Removed patch [1/6] from v1, and [4/6] from v1 is replaced
with new patch (which is [2/5] in this v2).There was introduced
fc->bg_lockto reduce the contention, and this patchset continues this efforts.This patchset introduces per
fuse_inodelock to protect inode metadata, synchronization with background writes, etc. All of the above is related to a single inode, and there is no a reason, that inodes are concurrents of each other to make some of actions exclusive.So, here we introduce
fuse_inode::lockspinlock and get rid offc->lockin many places.---
Kirill Tkhai (5):
fuse: Addfuse_inodeargument tofuse_prepare_release()
fuse: Convertfuse_conn::attr_versionintoatomic64_t
fuse: Introducefuse_inode::lockto protect write related fields and statistics
fuse: Protectfuse_inode::nlookupwithfuse_inode::lock
fuse: Protectfuse_file::reserved_reqvia correspondingfuse_inode::lockfs/fuse/cuse.c | 3 + fs/fuse/dev.c | 10 +++-- fs/fuse/dir.c | 54 ++++++++++---------------- fs/fuse/file.c | 111 ++++++++++++++++++++++++++++------------------------- fs/fuse/fuse_i.h | 23 ++++++++--- fs/fuse/inode.c | 17 +++++--- fs/fuse/readdir.c | 4 +- 7 files changed, 115 insertions(+), 107 deletions(-)--
Signed-off-by Kirill Tkhai <[email protected]>
Quoted 18 lines · Kirill Tkhai
ping
Quoted 14 lines · Kirill Tkhai
v2: Removed patch [1/6] from v1, and [4/6] from v1 is replaced
with new patch (which is [2/5] in this v2).There was introduced
fc->bg_lockto reduce the contention, and this patchset continues this efforts.This patchset introduces per
fuse_inodelock to protect inode metadata, synchronization with background writes, etc. All of the above is related to a single inode, and there is no a reason, that inodes are concurrents of each other to make some of actions exclusive.So, here we introduce
fuse_inode::lockspinlock and get rid offc->lockin many places.
Pushed to fuse.git#for-next.
Thanks,
Miklos