nfsd: convert nfs4_file searches to use RCU
Subsystems (2)
PATCH 1/1 nfsd: convert nfs4_file searches to use RCU
The global state_lock protects the file_hashtbl, and that has the potential to be a scalability bottleneck.
Address this by making the file_hashtbl use RCU. Add a rcu_head to the nfs4_file and use that when freeing ones that have been hashed.
Convert find_file to use a lockless lookup. Convert find_or_add_file to attempt a lockless lookup first, and then fall back to doing the "normal" locked search and insert if that fails to find anything.
--- fs/nfsd/nfs4state.c | 36 +++++++++++++++++++++++++++--------- fs/nfsd/state.h | 1 + 2 files changed, 28 insertions(+), 9 deletions(-)
fs/nfsd/nfs4state.c +27 -9
@@ -280,15 +280,22 @@ static void nfsd4_free_file(struct nfs4_file *f)kmem_cache_free (file_slab, f); } +static void nfsd4_free_file_rcu (struct rcu_head *rcu) +{ + struct nfs4_file *fp = container_of(rcu, struct nfs4_file, fi_rcu); + +nfsd4_free_file (fp); +} +static inline void put_nfs4_file (struct nfs4_file *fi) {might_lock (&state_lock);if (atomic_dec_and_lock (&fi->fi_ref, &state_lock)) { -hlist_del (&fi->fi_hash); +hlist_del_rcu (&fi->fi_hash);spin_unlock (&state_lock); -nfsd4_free_file (fi); +call_rcu (&fi->fi_rcu, nfsd4_free_file_rcu); } }
@@ -3073,7 +3080,7 @@ static void nfsd4_init_file(struct nfs4_file *fp, struct knfsd_fh *fh) fp->fi_share_deny =0 ;memset (fp->fi_fds,0 ,sizeof (fp->fi_fds));memset (fp->fi_access,0 ,sizeof (fp->fi_access)); -hlist_add_head (&fp->fi_hash, &file_hashtbl[hashval]); +hlist_add_head_rcu (&fp->fi_hash, &file_hashtbl[hashval]); }void
@@ -3313,12 +3320,19 @@ find_file_locked(struct knfsd_fh *fh)static struct nfs4_file *find_file (struct knfsd_fh *fh) { -struct nfs4_file *fp; +struct nfs4_file *fp, *ret =NULL ; +unsigned int hashval =file_hashval (fh); -spin_lock (&state_lock); - fp =find_file_locked (fh); -spin_unlock (&state_lock); -return fp; +rcu_read_lock (); +hlist_for_each_entry_rcu (fp, &file_hashtbl[hashval], fi_hash) { +if (nfsd_fh_match (&fp->fi_fhandle, fh)) { +if (atomic_inc_not_zero (&fp->fi_ref)) + ret = fp; +break ; + } + } +rcu_read_unlock (); +return ret; } static struct nfs4_file *
@@ -3326,9 +3340,13 @@ find_or_add_file(struct nfs4_file *new, struct knfsd_fh *fh) {struct nfs4_file *fp; + fp =find_file (fh); +if (fp) +return fp; +spin_lock (&state_lock); fp =find_file_locked (fh); -if (fp ==NULL ) { +if (likely (fp ==NULL )) {nfsd4_init_file (new, fh); fp = new; }
fs/nfsd/state.h +1 -0
@@ -490,6 +490,7 @@ struct nfs4_file {atomic_t fi_access[2 ];u32 fi_share_deny;struct file *fi_deleg_file; +struct rcu_head fi_rcu;atomic_t fi_delegees;struct knfsd_fh fi_fhandle;bool fi_had_conflict;
Signature · 1 line
1.9.3
Quoted 21 lines · Jeff Layton
The global
state_lockprotects thefile_hashtbl, and that has the potential to be a scalability bottleneck.Address this by making the
file_hashtbluse RCU. Add arcu_headto thenfs4_fileand use that when freeing ones that have been hashed.Convert
find_fileto use a lockless lookup. Convertfind_or_add_fileto attempt a lockless lookup first, and then fall back to doing the "normal" locked search and insert if that fails to find anything.Signed-off-by Jeff Layton <[email protected]>--- fs/nfsd/nfs4state.c | 36 +++++++++++++++++++++++++++--------- fs/nfsd/state.h | 1 + 2 files changed, 28 insertions(+), 9 deletions(-)fs/nfsd/nfs4state.c +0
-0
fs/nfsd/nfs4state.c static void nfsd4_free_file(struct nfs4_file *f) · line 287
Quoted 8 lines
fs/nfsd/nfs4state.c +5
-0@@ -280,15 +280,22 @@ static void nfsd4_free_file(struct nfs4_file *f)kmem_cache_free (file_slab, f); } +static void nfsd4_free_file_rcu (struct rcu_head *rcu) +{ + struct nfs4_file *fp = container_of(rcu, struct nfs4_file, fi_rcu); + +nfsd4_free_file (fp);
You might as well kill the pointless nfsd4_free_file wrapper while you're at it.
Quoted 12 lines
fs/nfsd/nfs4state.c +2
-5@@ -3313,12 +3320,19 @@ find_file_locked(struct knfsd_fh *fh)static struct nfs4_file *find_file (struct knfsd_fh *fh) { -struct nfs4_file *fp; +struct nfs4_file *fp, *ret =NULL ; +unsigned int hashval =file_hashval (fh); -spin_lock (&state_lock); - fp =find_file_locked (fh); -spin_unlock (&state_lock); -return fp;
fs/nfsd/nfs4state.c find_file_locked(struct knfsd_fh *fh) · line 3335
Quoted 10 lines
fs/nfsd/nfs4state.c +10
-0+rcu_read_lock (); +hlist_for_each_entry_rcu (fp, &file_hashtbl[hashval], fi_hash) { +if (nfsd_fh_match (&fp->fi_fhandle, fh)) { +if (atomic_inc_not_zero (&fp->fi_ref)) + ret = fp; +break ; + } + } +rcu_read_unlock (); +return ret;
I think it would be better to just switch find_file_locked ti use hlist_for_each_entry_rcu instead of duplicating it.
fs/nfsd/state.h struct nfs4_file { · line 493
Quoted 9 lines
fs/nfsd/state.h +1
-0@@ -490,6 +490,7 @@ struct nfs4_file {atomic_t fi_access[2 ];u32 fi_share_deny;struct file *fi_deleg_file; +struct rcu_head fi_rcu;
Can we union this over a field that's guaranteed to be unused on a file that has been unhashed?
Also a slightly related question: Is the small fixes size hash table still fine for the workloads where the RCU access matters? It seems like we should aim for a more scalable data structure to look up the files. It also irks me a bit how this duplicates the inode cache, which for some filesystems (e.g. XFS) already is very scalable.
Quoted 25 lines · On Tue, 21 Oct 2014 03:40:13 -0700 Christoph Hellwig <[email protected]> wrote:
Quoted 24 lines · Jeff Layton
The global
state_lockprotects thefile_hashtbl, and that has the potential to be a scalability bottleneck.Address this by making the
file_hashtbluse RCU. Add arcu_headto thenfs4_fileand use that when freeing ones that have been hashed.Convert
find_fileto use a lockless lookup. Convertfind_or_add_fileto attempt a lockless lookup first, and then fall back to doing the "normal" locked search and insert if that fails to find anything.Signed-off-by Jeff Layton <[email protected]>--- fs/nfsd/nfs4state.c | 36 +++++++++++++++++++++++++++--------- fs/nfsd/state.h | 1 + 2 files changed, 28 insertions(+), 9 deletions(-)fs/nfsd/nfs4state.c +0
-0@@ -280,15 +280,22 @@ static void nfsd4_free_file(struct nfs4_file *f)kmem_cache_free (file_slab, f); }
Quoted 9 lines
fs/nfsd/nfs4state.c static void nfsd4_free_file(struct nfs4_file *f) · line 287
Quoted 5 lines
fs/nfsd/nfs4state.c +5
-0+static void nfsd4_free_file_rcu (struct rcu_head *rcu) +{ + struct nfs4_file *fp = container_of(rcu, struct nfs4_file, fi_rcu); + +nfsd4_free_file (fp);You might as well kill the pointless
nfsd4_free_filewrapper while you're at it.
Hmm, ok. There is one place that still calls it, but we can just make that use kmem_cache_free.
Quoted 17 lines
Quoted 17 lines
fs/nfsd/nfs4state.c +7
-5@@ -3313,12 +3320,19 @@ find_file_locked(struct knfsd_fh *fh)static struct nfs4_file *find_file (struct knfsd_fh *fh) { -struct nfs4_file *fp; +struct nfs4_file *fp, *ret =NULL ; +unsigned int hashval =file_hashval (fh); -spin_lock (&state_lock); - fp =find_file_locked (fh); -spin_unlock (&state_lock); -return fp; +rcu_read_lock (); +hlist_for_each_entry_rcu (fp, &file_hashtbl[hashval], fi_hash) { +if (nfsd_fh_match (&fp->fi_fhandle, fh)) { +if (atomic_inc_not_zero (&fp->fi_ref)) + ret = fp;
Quoted 9 lines
fs/nfsd/nfs4state.c find_file_locked(struct knfsd_fh *fh) · line 3335
Quoted 5 lines
fs/nfsd/nfs4state.c +5
-0+break ; + } + } +rcu_read_unlock (); +return ret;I think it would be better to just switch
find_file_lockedti usehlist_for_each_entry_rcuinstead of duplicating it.
I'll have to think about that. We do have to do an atomic_inc_not_zero if we're doing an unlocked search, but that's not really necessary if the spinlock is held. I guess it won't hurt in that case, so we should be able to merge the two functions. I'll respin and do that...
Quoted 4 lines
Quoted 4 lines
fs/nfsd/state.h +0
-0
Quoted 9 lines
fs/nfsd/state.h struct nfs4_file { · line 493
Quoted 5 lines
fs/nfsd/state.h +1
-0@@ -490,6 +490,7 @@ struct nfs4_file {atomic_t fi_access[2 ];u32 fi_share_deny;struct file *fi_deleg_file; +struct rcu_head fi_rcu;Can we union this over a field that's guaranteed to be unused on a file that has been unhashed?
Yeah, that's probably fine. Suggestions on what to union it with?
struct callback_head is two pointers, so maybe we can use one of the list_heads (fi_delegations maybe?).
Quoted 7 lines
Also a slightly related question: Is the small fixes size hash table still fine for the workloads where the RCU access matters? It seems like we should aim for a more scalable data structure to look up the files. It also irks me a bit how this duplicates the inode cache, which for some filesystems (e.g. XFS) already is very scalable.
TBH, I haven't done any real performance measurements on this hashtable. The main impetus for this patch was to clear the way for some changes that I'm doing for some pnfsd-related work (I need to be able to walk a list of nfs4_files w/o holding a spinlock).
I think it makes sense to allow searching for nfs4_files w/o holding a lock. It's unlikely to hurt performance, and may help it.
If we do want to change to a different type of structure I'd be fine with that, but would prefer that it be RCU-friendly. What sort of structure did you have in mind?
Thanks for the review so far!
Signature · 1 line
Jeff Layton <[email protected]>
Quoted 4 lines · Jeff Layton
Yeah, that's probably fine. Suggestions on what to union it with?
struct
callback_headis two pointers, so maybe we can use one of thelist_heads(fi_delegationsmaybe?).
Sounds reasonable to me.
Quoted 3 lines
If we do want to change to a different type of structure I'd be fine with that, but would prefer that it be RCU-friendly. What sort of structure did you have in mind?
For the XFS inode lookup we use multiple radix trees, which work very well for how inode numbers work in XFS with a clear allocation group component, and usual clustering of inode numbers. A simple radix tree might work fine for NFSd, or maybe the new resizable hash tables from the networking folks?
PATCH v2 1/1 nfsd: convert nfs4_file searches to use RCU
In reply to Jeff LaytonThe global state_lock protects the file_hashtbl, and that has the potential to be a scalability bottleneck.
Address this by making the file_hashtbl use RCU. Add a rcu_head to the nfs4_file and use that when freeing ones that have been hashed. In order to conserve space, we union the fi_rcu field with the fi_delegations list_head which must be clear by the time the last reference to the file is dropped.
Convert find_file_locked to use RCU lookup primitives and not to require that the state_lock be held, and convert find_file to do a lockless lookup. Convert find_or_add_file to attempt a lockless lookup first, and then fall back to doing a locked search and insert if that fails to find anything.
Also, minimize the number of times we need to calculate the hash value by passing it in as an argument to the search and insert functions, and optimize the order of arguments in nfsd4_init_file.
--- fs/nfsd/nfs4state.c | 51 +++++++++++++++++++++++++++++---------------------- fs/nfsd/state.h | 5 ++++- 2 files changed, 33 insertions(+), 23 deletions(-)
fs/nfsd/nfs4state.c +29 -22
@@ -275,9 +275,11 @@ opaque_hashval(const void *ptr, int nbytes)return x; } -static void nfsd4_free_file (struct nfs4_file *f) +static void nfsd4_free_file_rcu (struct rcu_head *rcu) { -kmem_cache_free (file_slab, f); + struct nfs4_file *fp = container_of(rcu, struct nfs4_file, fi_rcu); + +kmem_cache_free (file_slab, fp); } static inline void
@@ -286,9 +288,10 @@ put_nfs4_file(struct nfs4_file *fi)might_lock (&state_lock);if (atomic_dec_and_lock (&fi->fi_ref, &state_lock)) { -hlist_del (&fi->fi_hash); +hlist_del_rcu (&fi->fi_hash);spin_unlock (&state_lock); -nfsd4_free_file (fi); +WARN_ON_ONCE (!list_empty (&fi->fi_delegations)); +call_rcu (&fi->fi_rcu, nfsd4_free_file_rcu); } }
@@ -3057,10 +3060,9 @@ static struct nfs4_file *nfsd4_alloc_file(void) }/* OPEN Share state helper functions */ -static void nfsd4_init_file (struct nfs4_file *fp,struct knfsd_fh *fh) +static void nfsd4_init_file (struct knfsd_fh *fh,unsigned int hashval, +struct nfs4_file *fp) { -unsigned int hashval =file_hashval (fh); -lockdep_assert_held (&state_lock);atomic_set (&fp->fi_ref,1 );
@@ -3073,7 +3075,7 @@ static void nfsd4_init_file(struct nfs4_file *fp, struct knfsd_fh *fh) fp->fi_share_deny =0 ;memset (fp->fi_fds,0 ,sizeof (fp->fi_fds));memset (fp->fi_access,0 ,sizeof (fp->fi_access)); -hlist_add_head (&fp->fi_hash, &file_hashtbl[hashval]); +hlist_add_head_rcu (&fp->fi_hash, &file_hashtbl[hashval]); }void
@@ -3294,17 +3296,14 @@ move_to_close_lru(struct nfs4_ol_stateid *s, struct net *net)/* search file_hashtbl[] for file */ static struct nfs4_file * -find_file_locked (struct knfsd_fh *fh) +find_file_locked (struct knfsd_fh *fh,unsigned int hashval) { -unsigned int hashval =file_hashval (fh);struct nfs4_file *fp; -lockdep_assert_held (&state_lock); - -hlist_for_each_entry (fp, &file_hashtbl[hashval], fi_hash) { +hlist_for_each_entry_rcu (fp, &file_hashtbl[hashval], fi_hash) {if (nfsd_fh_match (&fp->fi_fhandle, fh)) { -get_nfs4_file (fp); -return fp; +if (atomic_inc_not_zero (&fp->fi_ref)) +return fp; } }return NULL ;
@@ -3314,10 +3313,11 @@ static struct nfs4_file * find_file(struct knfsd_fh *fh) {struct nfs4_file *fp; +unsigned int hashval =file_hashval (fh); -spin_lock (&state_lock); - fp =find_file_locked (fh); -spin_unlock (&state_lock); +rcu_read_lock (); + fp =find_file_locked (fh, hashval); +rcu_read_unlock ();return fp; }
@@ -3325,11 +3325,18 @@ static struct nfs4_file * find_or_add_file(struct nfs4_file *new, struct knfsd_fh *fh) { struct nfs4_file *fp; +unsigned int hashval =file_hashval (fh); + +rcu_read_lock (); + fp =find_file_locked (fh, hashval); +rcu_read_unlock (); +if (fp) +return fp;spin_lock (&state_lock); - fp =find_file_locked (fh); -if (fp ==NULL ) { -nfsd4_init_file (new, fh); + fp =find_file_locked (fh, hashval); +if (likely (fp ==NULL )) { +nfsd4_init_file (fh, hashval, new); fp = new; }spin_unlock (&state_lock);
@@ -4127,7 +4134,7 @@ void nfsd4_cleanup_open_state(struct nfsd4_compound_state *cstate,nfs4_put_stateowner (so); }if (open->op_file) -nfsd4_free_file (open->op_file); +kmem_cache_free (file_slab, open->op_file);if (open->op_stp)nfs4_put_stid (&open->op_stp->st_stid); }
fs/nfsd/state.h +4 -1
@@ -477,7 +477,10 @@ struct nfs4_file {spinlock_t fi_lock;struct hlist_node fi_hash;/* hash on fi_fhandle */ struct list_head fi_stateids; -struct list_head fi_delegations; +union { +struct list_head fi_delegations; +struct rcu_head fi_rcu; + };/* One each for O_RDONLY, O_WRONLY, O_RDWR: */ struct file * fi_fds[3 ];/*
Signature · 1 line
1.9.3
Looks good,
Quoted 15 lines · Jeff Layton
The global
state_lockprotects thefile_hashtbl, and that has the potential to be a scalability bottleneck.Address this by making the
file_hashtbluse RCU. Add arcu_headto thenfs4_fileand use that when freeing ones that have been hashed. In order to conserve space, we union thefi_rcufield with thefi_delegationslist_headwhich must be clear by the time the last reference to the file is dropped.Convert
find_file_lockedto use RCU lookup primitives and not to require that thestate_lockbe held, and convertfind_fileto do a lockless lookup. Convertfind_or_add_fileto attempt a lockless lookup first, and then fall back to doing a locked search and insert if that fails to find anything.
Quoted 3 lines
Also, minimize the number of times we need to calculate the hash value by passing it in as an argument to the search and insert functions, and optimize the order of arguments in
nfsd4_init_file.
OK, looks fine, thanks. Applying for 3.19.
In general though I'd like to see more evidence when we're making changes for performance reasons.
--b.
Quoted 148 lines
Cc Christoph Hellwig <[email protected]>Signed-off-by Jeff Layton <[email protected]>--- fs/nfsd/nfs4state.c | 51 +++++++++++++++++++++++++++++---------------------- fs/nfsd/state.h | 5 ++++- 2 files changed, 33 insertions(+), 23 deletions(-)fs/nfsd/nfs4state.c +29
-22@@ -275,9 +275,11 @@ opaque_hashval(const void *ptr, int nbytes)return x; } -static void nfsd4_free_file (struct nfs4_file *f) +static void nfsd4_free_file_rcu (struct rcu_head *rcu) { -kmem_cache_free (file_slab, f); + struct nfs4_file *fp = container_of(rcu, struct nfs4_file, fi_rcu); + +kmem_cache_free (file_slab, fp); } static inline void@@ -286,9 +288,10 @@ put_nfs4_file(struct nfs4_file *fi)might_lock (&state_lock);if (atomic_dec_and_lock (&fi->fi_ref, &state_lock)) { -hlist_del (&fi->fi_hash); +hlist_del_rcu (&fi->fi_hash);spin_unlock (&state_lock); -nfsd4_free_file (fi); +WARN_ON_ONCE (!list_empty (&fi->fi_delegations)); +call_rcu (&fi->fi_rcu, nfsd4_free_file_rcu); } }@@ -3057,10 +3060,9 @@ static struct nfs4_file *nfsd4_alloc_file(void) }/* OPEN Share state helper functions */ -static void nfsd4_init_file (struct nfs4_file *fp,struct knfsd_fh *fh) +static void nfsd4_init_file (struct knfsd_fh *fh,unsigned int hashval, +struct nfs4_file *fp) { -unsigned int hashval =file_hashval (fh); -lockdep_assert_held (&state_lock);atomic_set (&fp->fi_ref,1 );@@ -3073,7 +3075,7 @@ static void nfsd4_init_file(struct nfs4_file *fp, struct knfsd_fh *fh) fp->fi_share_deny =0 ;memset (fp->fi_fds,0 ,sizeof (fp->fi_fds));memset (fp->fi_access,0 ,sizeof (fp->fi_access)); -hlist_add_head (&fp->fi_hash, &file_hashtbl[hashval]); +hlist_add_head_rcu (&fp->fi_hash, &file_hashtbl[hashval]); }void @@ -3294,17 +3296,14 @@ move_to_close_lru(struct nfs4_ol_stateid *s, struct net *net)/* search file_hashtbl[] for file */ static struct nfs4_file * -find_file_locked (struct knfsd_fh *fh) +find_file_locked (struct knfsd_fh *fh,unsigned int hashval) { -unsigned int hashval =file_hashval (fh);struct nfs4_file *fp; -lockdep_assert_held (&state_lock); - -hlist_for_each_entry (fp, &file_hashtbl[hashval], fi_hash) { +hlist_for_each_entry_rcu (fp, &file_hashtbl[hashval], fi_hash) {if (nfsd_fh_match (&fp->fi_fhandle, fh)) { -get_nfs4_file (fp); -return fp; +if (atomic_inc_not_zero (&fp->fi_ref)) +return fp; } }return NULL ;@@ -3314,10 +3313,11 @@ static struct nfs4_file * find_file(struct knfsd_fh *fh) {struct nfs4_file *fp; +unsigned int hashval =file_hashval (fh); -spin_lock (&state_lock); - fp =find_file_locked (fh); -spin_unlock (&state_lock); +rcu_read_lock (); + fp =find_file_locked (fh, hashval); +rcu_read_unlock ();return fp; }@@ -3325,11 +3325,18 @@ static struct nfs4_file * find_or_add_file(struct nfs4_file *new, struct knfsd_fh *fh) { struct nfs4_file *fp; +unsigned int hashval =file_hashval (fh); + +rcu_read_lock (); + fp =find_file_locked (fh, hashval); +rcu_read_unlock (); +if (fp) +return fp;spin_lock (&state_lock); - fp =find_file_locked (fh); -if (fp ==NULL ) { -nfsd4_init_file (new, fh); + fp =find_file_locked (fh, hashval); +if (likely (fp ==NULL )) { +nfsd4_init_file (fh, hashval, new); fp = new; }spin_unlock (&state_lock);@@ -4127,7 +4134,7 @@ void nfsd4_cleanup_open_state(struct nfsd4_compound_state *cstate,nfs4_put_stateowner (so); }if (open->op_file) -nfsd4_free_file (open->op_file); +kmem_cache_free (file_slab, open->op_file);if (open->op_stp)nfs4_put_stid (&open->op_stp->st_stid); }fs/nfsd/state.h +4
-1@@ -477,7 +477,10 @@ struct nfs4_file {spinlock_t fi_lock;struct hlist_node fi_hash;/* hash on fi_fhandle */ struct list_head fi_stateids; -struct list_head fi_delegations; +union { +struct list_head fi_delegations; +struct rcu_head fi_rcu; + };/* One each for O_RDONLY, O_WRONLY, O_RDWR: */ struct file * fi_fds[3 ];/* Signature · 1 line
1.9.3