nfsd/filecache: add nfsd_file_acquire_gc_cached
Subsystems (2)
PATCH 1/1 nfsd/filecache: add nfsd_file_acquire_gc_cached
Rather than make nfsd_file_do_acquire() more complex (by training it to conditionally skip both fh_verify() and nfsd_file allocation and contruction) introduce nfsd_file_acquire_gc_cached() -- which duplicates the minimalist subset of nfsd_file_do_acquire() needed to achieve nfsd_file lookup using an opaque @inode_key.
nfsd_file_acquire_gc_cached() only returns a cached and GC'd nfsd_file obtained using the opaque @inode_key, established from a previous call to nfsd_file_do_acquire_local() that originally added the GC'd nfsd_file to the filecache.
Update nfsd_open_local_fh to store @inode_key in @nfs_fh so later calls can check if it maps to an open GC'd nfsd_file in the filecache using nfsd_file_acquire_gc_cached(). Its nfsd_file_lookup_locked() call will only find a match if @cred matches the nfsd_file's nf_cred.
And care is taken to clear the inode_key in nfsd_file_free() if the nfsd_file has a non-NULL nf_inodep (which is a pointer to the address of the opaque inode_key stored in the nfs_fh). This avoids any risk of re-using the old inode_key for a different nfsd_file.
This commit's cached nfsd_file lookup dramatically speeds up LOCALIO performance, especially for small 4K O_DIRECT IO, e.g.:
before: read: IOPS=376k, BW=1469MiB/s (1541MB/s)(28.7GiB/20001msec) after: read: IOPS=1037k, BW=4050MiB/s (4247MB/s)(79.1GiB/20002msec)
Note that LOCALIO calls nfsd_open_local_fh() for every IO it issues to the underlying filesystem using the returned nfsd_file. This is why caching the opaque 'inode_key' in 'struct nfs_fh' is so helpful for LOCALIO to quickly return the cached nfsd_file. Whereas regular NFS avoids fh_verify()'s costly duplicate lookups of the underlying filesystem's dentry by caching it in 'fh_dentry' of 'struct svc_fh'. LOCALIO cannot take the same approach, of storing the dentry, without creating object lifetime issues associated with dentry references holding server mounts open and preventing unmounts.
--- fs/nfs/inode.c | 3 ++ fs/nfs_common/nfslocalio.c | 2 +- fs/nfsd/filecache.c | 78 ++++++++++++++++++++++++++++++++++++++ fs/nfsd/filecache.h | 7 ++++ fs/nfsd/localio.c | 46 +++++++++++++++++++--- include/linux/nfs.h | 4 ++ include/linux/nfslocalio.h | 6 +-- 7 files changed, 136 insertions(+), 10 deletions(-)
fs/nfs/inode.c +3 -0
@@ -2413,6 +2413,9 @@ struct inode *nfs_alloc_inode(struct super_block *sb)#endif /* CONFIG_NFS_V4 */ #ifdef CONFIG_NFS_V4_2 nfsi->xattr_cache =NULL ; +#endif +#if IS_ENABLED (CONFIG_NFS_LOCALIO ) + nfsi->fh.inode_key =NULL ;#endif nfs_netfs_inode_init (nfsi);
fs/nfs_common/nfslocalio.c +1 -1
@@ -130,7 +130,7 @@ EXPORT_SYMBOL_GPL(nfs_uuid_invalidate_one_client);struct nfsd_file *nfs_open_local_fh (nfs_uuid_t *uuid,struct rpc_clnt *rpc_clnt,const struct cred *cred, -const struct nfs_fh *nfs_fh,const fmode_t fmode) +struct nfs_fh *nfs_fh,const fmode_t fmode) {struct net *net;struct nfsd_file *localio;
fs/nfsd/filecache.c +78 -0
96 lines not on this page · raw message
fs/nfsd/filecache.h +7 -0
@@ -47,6 +47,10 @@ struct nfsd_file {struct list_head nf_gc;struct rcu_head nf_rcu;ktime_t nf_birthtime; + +#if IS_ENABLED (CONFIG_NFS_LOCALIO ) +void ** nf_inodep; +#endif };int nfsd_file_cache_init (void );
@@ -71,5 +75,8 @@ __be32 nfsd_file_acquire_opened(struct svc_rqst *rqstp, struct svc_fh *fhp,__be32 nfsd_file_acquire_local (struct net *net,struct svc_cred *cred,struct auth_domain *client,struct svc_fh *fhp,unsigned int may_flags,struct nfsd_file **pnf); +__be32 nfsd_file_acquire_cached (struct net *net,const struct cred *cred, +void *inode_key,unsigned int may_flags, +struct nfsd_file **pnf);int nfsd_file_cache_stats_show (struct seq_file *m,void *v);#endif /* _FS_NFSD_FILECACHE_H */
fs/nfsd/localio.c +40 -6
73 lines not on this page · raw message
include/linux/nfs.h +4 -0
@@ -29,6 +29,10 @@struct nfs_fh {unsigned short size;unsigned char data[NFS_MAXFHSIZE ]; +#if IS_ENABLED (CONFIG_NFS_LOCALIO ) +/* 'inode_key' is an opaque key used for nfsd_file hash lookups */ +void * inode_key; +#endif };/*
include/linux/nfslocalio.h +3 -3
@@ -43,7 +43,7 @@ void nfs_uuid_invalidate_one_client(nfs_uuid_t *nfs_uuid);/* localio needs to map filehandle -> struct nfsd_file */ extern struct nfsd_file *nfsd_open_local_fh (struct net *,struct auth_domain *,struct rpc_clnt *, -const struct cred *,const struct nfs_fh *, +const struct cred *,struct nfs_fh *,const fmode_t )__must_hold (rcu);struct nfsd_localio_operations {
@@ -53,7 +53,7 @@ struct nfsd_localio_operations { struct auth_domain *, struct rpc_clnt *, const struct cred *, - const struct nfs_fh *, + struct nfs_fh *, const fmode_t);void (*nfsd_file_put_local)(struct nfsd_file *);struct file *(*nfsd_file_file)(struct nfsd_file *);
@@ -64,7 +64,7 @@ extern const struct nfsd_localio_operations *nfs_to;struct nfsd_file *nfs_open_local_fh (nfs_uuid_t *,struct rpc_clnt *,const struct cred *, -const struct nfs_fh *,const fmode_t ); +struct nfs_fh *,const fmode_t );static inline void nfs_to_nfsd_file_put_local (struct nfsd_file *localio) {
Signature · 1 line
2.44.0
Quoted 37 lines · Mike Snitzer
Rather than make
nfsd_file_do_acquire()more complex (by training it to conditionally skip bothfh_verify()andnfsd_fileallocation and contruction) introducenfsd_file_acquire_gc_cached()-- which duplicates the minimalist subset ofnfsd_file_do_acquire()needed to achievenfsd_filelookup using an opaque @inode_key.
nfsd_file_acquire_gc_cached()only returns a cached and GC'dnfsd_fileobtained using the opaque @inode_key, established from a previous call tonfsd_file_do_acquire_local()that originally added the GC'dnfsd_fileto the filecache.Update
nfsd_open_local_fhto store @inode_key in @nfs_fh so later calls can check if it maps to an open GC'dnfsd_filein the filecache usingnfsd_file_acquire_gc_cached(). Itsnfsd_file_lookup_locked()call will only find a match if @cred matches the nfsd_file'snf_cred.And care is taken to clear the
inode_keyinnfsd_file_free()if thenfsd_filehas a non-NULLnf_inodep(which is a pointer to the address of the opaqueinode_keystored in thenfs_fh). This avoids any risk of re-using the oldinode_keyfor a differentnfsd_file.This commit's cached
nfsd_filelookup dramatically speeds up LOCALIO performance, especially for small 4KO_DIRECTIO, e.g.:before: read: IOPS=376k, BW=1469MiB/s (1541MB/s)(28.7GiB/20001msec) after: read: IOPS=1037k, BW=4050MiB/s (4247MB/s)(79.1GiB/20002msec)Note that LOCALIO calls
nfsd_open_local_fh()for every IO it issues to the underlying filesystem using the returnednfsd_file. This is why caching the opaque 'inode_key' in 'structnfs_fh' is so helpful for LOCALIO to quickly return the cachednfsd_file. Whereas regular NFS avoids fh_verify()'s costly duplicate lookups of the underlying filesystem's dentry by caching it in 'fh_dentry' of 'structsvc_fh'. LOCALIO cannot take the same approach, of storing the dentry, without creating object lifetime issues associated with dentry references holding server mounts open and preventing unmounts.
Quoted 1 line
Signed-off-by Mike Snitzer <[email protected]>
I think this is a good idea. If we are to avoid a complete lookup for every IO we need some back-pointer from the nfsd filecache to something in nfs so that a cached lookup can be invalidated. Various other schemes have been suggested before. This one seems particularly simple.
I'm wondering about the request for a garbage-collected nfsd_file though. For NFSv3 that makes sense. For NFSv4 we would expect the file to already be open as a non-garbage-collected nfsd_file and opening it again seems wasteful. That doesn't need to be fixed for this patch and maybe doesn't need to be fixed at all, but it seemed worth highlighting.
More below
Quoted 42 lines
--- fs/nfs/inode.c | 3 ++ fs/nfs_common/nfslocalio.c | 2 +- fs/nfsd/filecache.c | 78 ++++++++++++++++++++++++++++++++++++++ fs/nfsd/filecache.h | 7 ++++ fs/nfsd/localio.c | 46 +++++++++++++++++++--- include/linux/nfs.h | 4 ++ include/linux/nfslocalio.h | 6 +-- 7 files changed, 136 insertions(+), 10 deletions(-)fs/nfs/inode.c +3
-0@@ -2413,6 +2413,9 @@ struct inode *nfs_alloc_inode(struct super_block *sb)#endif /* CONFIG_NFS_V4 */ #ifdef CONFIG_NFS_V4_2 nfsi->xattr_cache =NULL ; +#endif +#if IS_ENABLED (CONFIG_NFS_LOCALIO ) + nfsi->fh.inode_key =NULL ;#endif nfs_netfs_inode_init (nfsi);fs/nfs_common/nfslocalio.c +1
-1@@ -130,7 +130,7 @@ EXPORT_SYMBOL_GPL(nfs_uuid_invalidate_one_client);struct nfsd_file *nfs_open_local_fh (nfs_uuid_t *uuid,struct rpc_clnt *rpc_clnt,const struct cred *cred, -const struct nfs_fh *nfs_fh,const fmode_t fmode) +struct nfs_fh *nfs_fh,const fmode_t fmode) {struct net *net;struct nfsd_file *localio;fs/nfsd/filecache.c +0
-0
fs/nfsd/filecache.c nfsd_file_alloc(struct net *net, struct inode *inode, unsigned char need, · line 226
Quoted 6 lines
fs/nfsd/filecache.c +3
-0@@ -221,6 +221,9 @@ nfsd_file_alloc(struct net *net, struct inode *inode, unsigned char need,INIT_LIST_HEAD (&nf->nf_gc); nf->nf_birthtime =ktime_get (); nf->nf_file =NULL ; +#if IS_ENABLED (CONFIG_NFS_LOCALIO ) + nf->nf_inodep =NULL ; +#endif
All these "#if IS_ENABLED" are ugly. I wonder if we could get rid of them.
Using __GFP_ZERO for the alloc would work here, but might be an unwanted cost. Maybe nf_inodep could be ignored if nf_file is NULL.
fs/nfsd/filecache.c nfsd_file_free(struct nfsd_file *nf) · line 296
Quoted 13 lines
fs/nfsd/filecache.c +6
-0nf->nf_cred =get_current_cred (); nf->nf_net = net; nf->nf_flags = want_gc ?@@ -285,6 +288,12 @@ nfsd_file_free(struct nfsd_file *nf)nfsd_file_check_write_error (nf);nfsd_filp_close (nf->nf_file); } +#if IS_ENABLED (CONFIG_NFS_LOCALIO ) +if (nf->nf_inodep) { + *(nf->nf_inodep) =NULL ; + nf->nf_inodep =NULL ; + } +#endif
This one is harder to hide. We don't really need the final assignment though so maybe we could
#define NF_INODEP(nf) (nf->nf_inodep)
or
#define NF_INODEP(nf) (NULL)
in a header (where #if are more acceptable), then make this code:
if (NF_INODEP(nf))
*NF_INODEP(nf) = NULL;
Is that better or worse I wonder.
Quoted 44 lines
fs/nfsd/filecache.c +37
-0/* * If this item is still linked via nf_lru, that's a bug. @@ -1255,6 +1264,75 @@ nfsd_file_acquire_local(struct net *net, struct svc_cred *cred,return beres; } +/** +* nfsd_file_acquire_cached - Get cached GC'd open file using inode +* @net: The network namespace in which to perform a lookup +* @cred: the user credential with which to validate access +* @inode_key: inode to use as opaque lookup key +* @may_flags: NFSD_MAY_ settings for the file +* @pnf: OUT: found cached GC'd "struct nfsd_file" object +* +* Rather than make nfsd_file_do_acquire more complex (by training +* it to conditionally skip fh_verify(), nfsd_file allocation and +* contruction) duplicate the minimalist subset of it that is +* needed to achieve nfsd_file lookup using the opaque @inode_key. +* +* The nfsd_file object returned by this API is reference-counted +* and garbage-collected. The object is retained for a few +* seconds after the final nfsd_file_put() in case the caller +* wants to re-use it. +* +* Return values: +* %nfs_ok - @pnf points to an nfsd_file with its reference +* count boosted. +* +* On error, an nfsstat value in network byte order is returned. +*/ +__be32 +nfsd_file_acquire_cached (struct net *net,const struct cred *cred, +void *inode_key,unsigned int may_flags, +struct nfsd_file **pnf) +{ +unsigned char need = may_flags &NFSD_FILE_MAY_MASK ; +struct nfsd_file *nf; +__be32 status; + +rcu_read_lock (); + nf =nfsd_file_lookup_locked (net, cred, inode_key, need, true); +rcu_read_unlock (); +
fs/nfsd/filecache.c nfsd_file_acquire_local(struct net *net, struct svc_cred *cred, · line 1313
Quoted 10 lines
fs/nfsd/filecache.c +10
-0+if (unlikely (!nf)) +return nfserr_noent; + +/* +* If the nf is on the LRU then it holds an extra reference +* that must be put if it's removed. It had better not be +* the last one however, since we should hold another. +*/ +if (nfsd_file_lru_remove (nf)) +WARN_ON_ONCE (refcount_dec_and_test (&nf->nf_ref));
Just use refcount_dec(&nf->nf_ref). It will provide a warning of the count reaches zero. In general you should never need warnings when using refcount as it generates the needed warnings itself.
fs/nfsd/filecache.c nfsd_file_acquire_local(struct net *net, struct svc_cred *cred, · line 1319
Quoted 6 lines
fs/nfsd/filecache.c +6
-0+ +if (WARN_ON_ONCE (test_bit (NFSD_FILE_PENDING , &nf->nf_flags) || + !test_bit (NFSD_FILE_HASHED , &nf->nf_flags))) { + status = nfserr_inval; + goto error; + }
Do we really want the above? I guess you were following the pattern in nfsd_file_do_acquire() which waits for FILE_PENDING and then re-tests FILE_HASHED (nfsd_file_lookup_locked() already tested it).
I guess it doesn't hurt, but I'm not sure it helps.
Quoted 74 lines
fs/nfsd/filecache.c +16
-0+this_cpu_inc (nfsd_file_cache_hits); + + status =nfserrno (nfsd_open_break_lease (file_inode (nf->nf_file), may_flags)); +if (status != nfs_ok) { +error: +nfsd_file_put (nf); + nf =NULL ; + }else { +this_cpu_inc (nfsd_file_acquisitions); +nfsd_file_check_write_error (nf); + *pnf = nf; + } +trace_nfsd_file_acquire (NULL , inode_key, may_flags, nf, status); +return status; +} +/** * nfsd_file_acquire_opened - Get a struct nfsd_file using existing open file * @rqstp: the RPC transaction being executed fs/nfsd/filecache.h +7
-0@@ -47,6 +47,10 @@ struct nfsd_file {struct list_head nf_gc;struct rcu_head nf_rcu;ktime_t nf_birthtime; + +#if IS_ENABLED (CONFIG_NFS_LOCALIO ) +void ** nf_inodep; +#endif };int nfsd_file_cache_init (void );@@ -71,5 +75,8 @@ __be32 nfsd_file_acquire_opened(struct svc_rqst *rqstp, struct svc_fh *fhp,__be32 nfsd_file_acquire_local (struct net *net,struct svc_cred *cred,struct auth_domain *client,struct svc_fh *fhp,unsigned int may_flags,struct nfsd_file **pnf); +__be32 nfsd_file_acquire_cached (struct net *net,const struct cred *cred, +void *inode_key,unsigned int may_flags, +struct nfsd_file **pnf);int nfsd_file_cache_stats_show (struct seq_file *m,void *v);#endif /* _FS_NFSD_FILECACHE_H */ fs/nfsd/localio.c +2
-6@@ -58,33 +58,67 @@ void nfsd_localio_ops_init(void)struct nfsd_file *nfsd_open_local_fh (struct net *net,struct auth_domain *dom,struct rpc_clnt *rpc_clnt,const struct cred *cred, -const struct nfs_fh *nfs_fh,const fmode_t fmode) +struct nfs_fh *nfs_fh,const fmode_t fmode) {int mayflags =NFSD_MAY_LOCALIO ;struct svc_cred rq_cred;struct svc_fh fh;struct nfsd_file *localio; +void *nf_inode_key;__be32 beres;if (nfs_fh->size >NFS4_FHSIZE )return ERR_PTR (-EINVAL ); -/* nfs_fh -> svc_fh */ -fh_init (&fh,NFS4_FHSIZE ); - fh.fh_handle.fh_size = nfs_fh->size; -memcpy (fh.fh_handle.fh_raw, nfs_fh->data, nfs_fh->size); -if (fmode &FMODE_READ ) mayflags |=NFSD_MAY_READ ;if (fmode &FMODE_WRITE ) mayflags |=NFSD_MAY_WRITE ;
fs/nfsd/localio.c void nfsd_localio_ops_init(void) · line 87
Quoted 10 lines
fs/nfsd/localio.c +10
-0+/* +* Check if 'inode_key' stored in @nfs_fh maps to an +* open nfsd_file in the filecache (from a previous +* nfsd_open_local_fh). +* +* The 'inode_key' may have become stale (due to nfsd_file +* being free'd by filecache GC) so the lookup will fail +* gracefully by falling back to using nfsd_file_acquire_local(). +*/ + nf_inode_key =READ_ONCE (nfs_fh->inode_key);
I think you want the above in an rcu-locked region with nfsd_file_acquire_cached(). Else the inode could be freed and reallocated after the READ_ONCE and before the lookup.
Maybe pass the address if inode_key and have nfsd_file_acquire_cached() deref after getting rcu_read_lock().
Quoted 42 lines
fs/nfsd/localio.c +28
-0+if (nf_inode_key) { + beres =nfsd_file_acquire_cached (net, cred, + nf_inode_key, + mayflags, &localio); +if (beres == nfs_ok) +return localio; +/* +* reset stale nfs_fh->inode_key and fallthru +* to use normal nfsd_file_acquire_local(). +*/ + nfs_fh->inode_key =NULL ; + } + +/* nfs_fh -> svc_fh */ +fh_init (&fh,NFS4_FHSIZE ); + fh.fh_handle.fh_size = nfs_fh->size; +memcpy (fh.fh_handle.fh_raw, nfs_fh->data, nfs_fh->size); +svcauth_map_clnt_to_svc_cred_local (rpc_clnt, cred, &rq_cred); beres =nfsd_file_acquire_local (net, &rq_cred, dom, &fh, mayflags, &localio);if (beres) localio =ERR_PTR (nfs_stat_to_errno (be32_to_cpu (beres))); +else { +/* +* opaque 'inode_key' has a 1:1 mapping to both an +* nfsd_file and nfs_fh struct (And the nfs_fh is shared +* by all NFS client threads. So there is no risk of +* storing competing addresses in nfsd_file->nf_inodep +*/ + localio->nf_inodep = (void **) &nfs_fh->inode_key; + nfs_fh->inode_key = localio->nf_inode; + }fh_put (&fh);if (rq_cred.cr_group_info)include/linux/nfs.h +0
-0
include/linux/nfs.h line 35
Quoted 7 lines
include/linux/nfs.h +4
-0@@ -29,6 +29,10 @@struct nfs_fh {unsigned short size;unsigned char data[NFS_MAXFHSIZE ]; +#if IS_ENABLED (CONFIG_NFS_LOCALIO ) +/* 'inode_key' is an opaque key used for nfsd_file hash lookups */ +void * inode_key; +#endif
I wonder if this is really the right place to store the inode_key. 'struct nfs_fh' appears in lots of places and they only place where you wan the inode_key is inside the nfs_inode. Maybe store an augmented nfs_fh in there...
Thanks,
NeilBrown
Quoted 38 lines
include/linux/nfs.h +0
-0};/* include/linux/nfslocalio.h +3
-3@@ -43,7 +43,7 @@ void nfs_uuid_invalidate_one_client(nfs_uuid_t *nfs_uuid);/* localio needs to map filehandle -> struct nfsd_file */ extern struct nfsd_file *nfsd_open_local_fh (struct net *,struct auth_domain *,struct rpc_clnt *, -const struct cred *,const struct nfs_fh *, +const struct cred *,struct nfs_fh *,const fmode_t )__must_hold (rcu);struct nfsd_localio_operations {@@ -53,7 +53,7 @@ struct nfsd_localio_operations { struct auth_domain *, struct rpc_clnt *, const struct cred *, - const struct nfs_fh *, + struct nfs_fh *, const fmode_t);void (*nfsd_file_put_local)(struct nfsd_file *);struct file *(*nfsd_file_file)(struct nfsd_file *);@@ -64,7 +64,7 @@ extern const struct nfsd_localio_operations *nfs_to;struct nfsd_file *nfs_open_local_fh (nfs_uuid_t *,struct rpc_clnt *,const struct cred *, -const struct nfs_fh *,const fmode_t ); +struct nfs_fh *,const fmode_t );static inline void nfs_to_nfsd_file_put_local (struct nfsd_file *localio) {Signature · 1 line
2.44.0
Quoted 9 lines · NeilBrown
I'm wondering about the request for a garbage-collected
nfsd_filethough. For NFSv3 that makes sense. For NFSv4 we would expect the file to already be open as a non-garbage-collectednfsd_fileand opening it again seems wasteful. That doesn't need to be fixed for this patch and maybe doesn't need to be fixed at all, but it seemed worth highlighting.
I don't think using a GC'd nfsd_file for LOCALIO is a bug.
NFSv4 guarantees an OPEN to pin the nfsd_file, and a CLOSE (or lease expiry) to release it. There's no desire for or need for garbage collection.
NFSv3 and LOCALIO use each nfsd_file for the life of one I/O operation, and that nfsd_file is cached in the expectation that another I/O operation on the same file will happen with frequent temporarl proximity. Garbage collection is needed for both cases.
--
Chuck Lever
Quoted 20 lines · Chuck Lever III
Quoted 13 lines · NeilBrown
I'm wondering about the request for a garbage-collected
nfsd_filethough. For NFSv3 that makes sense. For NFSv4 we would expect the file
to already be open as a non-garbage-collectednfsd_fileand opening it
again seems wasteful. That doesn't need to be fixed for this patch and
maybe doesn't need to be fixed at all, but it seemed worth highlighting.I don't think using a GC'd
nfsd_filefor LOCALIO is a bug.NFSv4 guarantees an OPEN to pin the
nfsd_file, and a CLOSE (or lease expiry) to release it. There's no desire for or need for garbage collection.
Quoted 6 lines
NFSv3 and LOCALIO use each
nfsd_filefor the life of one I/O operation, and thatnfsd_fileis cached in the expectation that another I/O operation on the same file will happen with frequent temporarl proximity. Garbage collection is needed for both cases.
No. There is a huge difference between the two. In the case of NFSv3, the server has no idea whether or not there is further need for the file (stateless), whereas in the localio case, the client is able to tell exactly when the application holds the file open or not (stateful).
The only reason for jumping through all these hoops in the case of localio is the 'user may want to unmount' exception.
Is there any reason why we couldn't add a notification for that, to give knfsd the opportunity to clear out any open file state? The current approach appears to be flat out optimising for the exception.
Signature · 3 lines
Trond Myklebust Linux NFS client maintainer, Hammerspace [email protected]
Quoted 38 lines · Trond Myklebust
Quoted 20 lines · Chuck Lever III
Quoted 13 lines · NeilBrown
I'm wondering about the request for a garbage-collected
nfsd_filethough. For NFSv3 that makes sense. For NFSv4 we would expect the file
to already be open as a non-garbage-collectednfsd_fileand opening it
again seems wasteful. That doesn't need to be fixed for this patch and
maybe doesn't need to be fixed at all, but it seemed worth highlighting.I don't think using a GC'd
nfsd_filefor LOCALIO is a bug.NFSv4 guarantees an OPEN to pin the
nfsd_file, and a CLOSE (or lease expiry) to release it. There's no desire for or need for garbage collection.Quoted 6 lines
NFSv3 and LOCALIO use each
nfsd_filefor the life of one I/O operation, and thatnfsd_fileis cached in the expectation that another I/O operation on the same file will happen with frequent temporarl proximity. Garbage collection is needed for both cases.No. There is a huge difference between the two. In the case of NFSv3, the server has no idea whether or not there is further need for the file (stateless), whereas in the localio case, the client is able to tell exactly when the application holds the file open or not (stateful).
Quoted 6 lines
The only reason for jumping through all these hoops in the case of localio is the 'user may want to unmount' exception.
Is there any reason why we couldn't add a notification for that, to give knfsd the opportunity to clear out any open file state? The current approach appears to be flat out optimising for the exception.
We've discussed this privately. A notification callback is possible, but that's code that would have to be written, and using GC nfsd_files was an expedient for getting LOCALIO merged.
There are some corner cases that will need to be worked through to help determine whether a callback is truly a simpler design.
--
Chuck Lever
Jeff Layton
maintainerQuoted 34 lines · Trond Myklebust
Quoted 20 lines · Chuck Lever III
Quoted 13 lines · NeilBrown
I'm wondering about the request for a garbage-collected
nfsd_filethough. For NFSv3 that makes sense. For NFSv4 we would expect the file
to already be open as a non-garbage-collectednfsd_fileand opening it
again seems wasteful. That doesn't need to be fixed for this patch and
maybe doesn't need to be fixed at all, but it seemed worth highlighting.I don't think using a GC'd
nfsd_filefor LOCALIO is a bug.NFSv4 guarantees an OPEN to pin the
nfsd_file, and a CLOSE (or lease expiry) to release it. There's no desire for or need for garbage collection.Quoted 6 lines
NFSv3 and LOCALIO use each
nfsd_filefor the life of one I/O operation, and thatnfsd_fileis cached in the expectation that another I/O operation on the same file will happen with frequent temporarl proximity. Garbage collection is needed for both cases.No. There is a huge difference between the two. In the case of NFSv3, the server has no idea whether or not there is further need for the file (stateless), whereas in the localio case, the client is able to tell exactly when the application holds the file open or not (stateful).
Quoted 8 lines
The only reason for jumping through all these hoops in the case of localio is the 'user may want to unmount' exception.
Is there any reason why we couldn't add a notification for that, to give knfsd the opportunity to clear out any open file state? The current approach appears to be flat out optimising for the exception.
No, and after discussing it with others here at the bake-a-thon, I think that might be the best path forward here. At a high level:
Add a callback to the client that runs just before calling nfsd_file_cache_purge() in expkey_flush(). The client would be expected to return all of its nfsd_files "soon" and switch back to normal network I/O. After that point, it can try to get a new nfsd_file and start up localio at that point. With that change too you can switch to using non-GC'ed nfsd_files. The client can just hold them open and do I/O to them at will.
I think that's probably less brittle than trying to keep opaque inode pointers around, and would probably mean better performance since you won't be thrashing the filecache as much.
Signature · 1 line
Jeff Layton <[email protected]>
Quoted 45 lines · NeilBrown
Quoted 37 lines · Mike Snitzer
Rather than make
nfsd_file_do_acquire()more complex (by training it to conditionally skip bothfh_verify()andnfsd_fileallocation and contruction) introducenfsd_file_acquire_gc_cached()-- which duplicates the minimalist subset ofnfsd_file_do_acquire()needed to achievenfsd_filelookup using an opaque @inode_key.
nfsd_file_acquire_gc_cached()only returns a cached and GC'dnfsd_fileobtained using the opaque @inode_key, established from a previous call tonfsd_file_do_acquire_local()that originally added the GC'dnfsd_fileto the filecache.Update
nfsd_open_local_fhto store @inode_key in @nfs_fh so later calls can check if it maps to an open GC'dnfsd_filein the filecache usingnfsd_file_acquire_gc_cached(). Itsnfsd_file_lookup_locked()call will only find a match if @cred matches the nfsd_file'snf_cred.And care is taken to clear the
inode_keyinnfsd_file_free()if thenfsd_filehas a non-NULLnf_inodep(which is a pointer to the address of the opaqueinode_keystored in thenfs_fh). This avoids any risk of re-using the oldinode_keyfor a differentnfsd_file.This commit's cached
nfsd_filelookup dramatically speeds up LOCALIO performance, especially for small 4KO_DIRECTIO, e.g.:before: read: IOPS=376k, BW=1469MiB/s (1541MB/s)(28.7GiB/20001msec) after: read: IOPS=1037k, BW=4050MiB/s (4247MB/s)(79.1GiB/20002msec)Note that LOCALIO calls
nfsd_open_local_fh()for every IO it issues to the underlying filesystem using the returnednfsd_file. This is why caching the opaque 'inode_key' in 'structnfs_fh' is so helpful for LOCALIO to quickly return the cachednfsd_file. Whereas regular NFS avoids fh_verify()'s costly duplicate lookups of the underlying filesystem's dentry by caching it in 'fh_dentry' of 'structsvc_fh'. LOCALIO cannot take the same approach, of storing the dentry, without creating object lifetime issues associated with dentry references holding server mounts open and preventing unmounts.Quoted 1 line
Signed-off-by Mike Snitzer <[email protected]>I think this is a good idea. If we are to avoid a complete lookup for every IO we need some back-pointer from the nfsd filecache to something in nfs so that a cached lookup can be invalidated. Various other schemes have been suggested before. This one seems particularly simple.
Quoted 5 lines
I'm wondering about the request for a garbage-collected
nfsd_filethough. For NFSv3 that makes sense. For NFSv4 we would expect the file to already be open as a non-garbage-collectednfsd_fileand opening it again seems wasteful. That doesn't need to be fixed for this patch and maybe doesn't need to be fixed at all, but it seemed worth highlighting.
Maybe you're referring to the nfsd_file_acquire_gc_cached() kernel doc text? The code doesn't impose the nfsd_file must be a GC'd nfsd_file (nor do I ever create an nfsd_file, if it isn't in the filecache then it returns NULL).
GC'd just buys us a more likely chance of finding the nfsd_file in the cache so it pairs nicely with GC having been requested/used when the nfsd_file originally created and added to the cache.
Anyway, what follows in my reply is all moot given this thread has teased out the desire to utilize a callback mechanism to allow the NFS client to hold a longterm reference on the open nfsd_file.
BUT I will be folding in all the things covered below so I can at least put nfsd_file_acquire_cached() out to pasture in more fully formed state (should there ever be a need to revisit it)...
Quoted 47 lines
More below
Quoted 45 lines
--- fs/nfs/inode.c | 3 ++ fs/nfs_common/nfslocalio.c | 2 +- fs/nfsd/filecache.c | 78 ++++++++++++++++++++++++++++++++++++++ fs/nfsd/filecache.h | 7 ++++ fs/nfsd/localio.c | 46 +++++++++++++++++++--- include/linux/nfs.h | 4 ++ include/linux/nfslocalio.h | 6 +-- 7 files changed, 136 insertions(+), 10 deletions(-)fs/nfs/inode.c +3
-0@@ -2413,6 +2413,9 @@ struct inode *nfs_alloc_inode(struct super_block *sb)#endif /* CONFIG_NFS_V4 */ #ifdef CONFIG_NFS_V4_2 nfsi->xattr_cache =NULL ; +#endif +#if IS_ENABLED (CONFIG_NFS_LOCALIO ) + nfsi->fh.inode_key =NULL ;#endif nfs_netfs_inode_init (nfsi);fs/nfs_common/nfslocalio.c +1
-1@@ -130,7 +130,7 @@ EXPORT_SYMBOL_GPL(nfs_uuid_invalidate_one_client);struct nfsd_file *nfs_open_local_fh (nfs_uuid_t *uuid,struct rpc_clnt *rpc_clnt,const struct cred *cred, -const struct nfs_fh *nfs_fh,const fmode_t fmode) +struct nfs_fh *nfs_fh,const fmode_t fmode) {struct net *net;struct nfsd_file *localio;fs/nfsd/filecache.c +0
-0@@ -221,6 +221,9 @@ nfsd_file_alloc(struct net *net, struct inode *inode, unsigned char need,INIT_LIST_HEAD (&nf->nf_gc); nf->nf_birthtime =ktime_get (); nf->nf_file =NULL ;
Quoted 8 lines
fs/nfsd/filecache.c nfsd_file_alloc(struct net *net, struct inode *inode, unsigned char need, · line 226
Quoted 3 lines
fs/nfsd/filecache.c +3
-0+#if IS_ENABLED (CONFIG_NFS_LOCALIO ) + nf->nf_inodep =NULL ; +#endif All these "#if
IS_ENABLED" are ugly. I wonder if we could get rid of them.
Using__GFP_ZEROfor the alloc would work here, but might be an unwanted cost. Maybenf_inodepcould be ignored ifnf_fileis NULL.
I did originally nest the nf_inodep backpointer reset within nfsd_file_free()'s nf->nf_file check, will go back to that.
Then nf_inodep is otherwise ignored everywhere.
Quoted 21 lines
fs/nfsd/filecache.c nfsd_file_free(struct nfsd_file *nf) · line 296
Quoted 13 lines
fs/nfsd/filecache.c +6
-0nf->nf_cred =get_current_cred (); nf->nf_net = net; nf->nf_flags = want_gc ?@@ -285,6 +288,12 @@ nfsd_file_free(struct nfsd_file *nf)nfsd_file_check_write_error (nf);nfsd_filp_close (nf->nf_file); } +#if IS_ENABLED (CONFIG_NFS_LOCALIO ) +if (nf->nf_inodep) { + *(nf->nf_inodep) =NULL ; + nf->nf_inodep =NULL ; + } +#endif This one is harder to hide. We don't really need the final assignment though so maybe we could
#define
NF_INODEP(nf)(nf->nf_inodep)
or
#defineNF_INODEP(nf)(NULL)
Quoted 6 lines
in a header (where #if are more acceptable), then make this code:
if (
NF_INODEP(nf))*NF_INODEP(nf) = NULL;Is that better or worse I wonder.
Not opposed to this, will do.
Quoted 50 lines
Quoted 50 lines
fs/nfsd/filecache.c +43
-0/* * If this item is still linked via nf_lru, that's a bug. @@ -1255,6 +1264,75 @@ nfsd_file_acquire_local(struct net *net, struct svc_cred *cred,return beres; } +/** +* nfsd_file_acquire_cached - Get cached GC'd open file using inode +* @net: The network namespace in which to perform a lookup +* @cred: the user credential with which to validate access +* @inode_key: inode to use as opaque lookup key +* @may_flags: NFSD_MAY_ settings for the file +* @pnf: OUT: found cached GC'd "struct nfsd_file" object +* +* Rather than make nfsd_file_do_acquire more complex (by training +* it to conditionally skip fh_verify(), nfsd_file allocation and +* contruction) duplicate the minimalist subset of it that is +* needed to achieve nfsd_file lookup using the opaque @inode_key. +* +* The nfsd_file object returned by this API is reference-counted +* and garbage-collected. The object is retained for a few +* seconds after the final nfsd_file_put() in case the caller +* wants to re-use it. +* +* Return values: +* %nfs_ok - @pnf points to an nfsd_file with its reference +* count boosted. +* +* On error, an nfsstat value in network byte order is returned. +*/ +__be32 +nfsd_file_acquire_cached (struct net *net,const struct cred *cred, +void *inode_key,unsigned int may_flags, +struct nfsd_file **pnf) +{ +unsigned char need = may_flags &NFSD_FILE_MAY_MASK ; +struct nfsd_file *nf; +__be32 status; + +rcu_read_lock (); + nf =nfsd_file_lookup_locked (net, cred, inode_key, need, true); +rcu_read_unlock (); + +if (unlikely (!nf)) +return nfserr_noent; + +/* +* If the nf is on the LRU then it holds an extra reference +* that must be put if it's removed. It had better not be
Quoted 8 lines
fs/nfsd/filecache.c nfsd_file_acquire_local(struct net *net, struct svc_cred *cred, · line 1313
Quoted 4 lines
fs/nfsd/filecache.c +4
-0+* the last one however, since we should hold another. +*/ +if (nfsd_file_lru_remove (nf)) +WARN_ON_ONCE (refcount_dec_and_test (&nf->nf_ref));Just use
refcount_dec(&nf->nf_ref). It will provide a warning if the count reaches zero. In general you should never need warnings when using refcount as it generates the needed warnings itself.
OK, I lifted the code from nfsd_file_do_acquire() as a starting point; so it should probably be cleaned up there (independent of this patch, not it).
Quoted 11 lines
fs/nfsd/filecache.c nfsd_file_acquire_local(struct net *net, struct svc_cred *cred, · line 1319
Quoted 6 lines
fs/nfsd/filecache.c +6
-0+ +if (WARN_ON_ONCE (test_bit (NFSD_FILE_PENDING , &nf->nf_flags) || + !test_bit (NFSD_FILE_HASHED , &nf->nf_flags))) { + status = nfserr_inval; + goto error; + }Do we really want the above? I guess you were following the pattern in
nfsd_file_do_acquire()which waits forFILE_PENDINGand then re-testsFILE_HASHED(nfsd_file_lookup_locked()already tested it).
I guess it doesn't hurt, but I'm not sure it helps.
Right, was just trying to maintain status-quo. I think it doesn't hurt, and may actually help given spin_lock isn't held around nfsd_file_lookup_locked? But the WARN_ON_ONCE should be removed.
Quoted 31 lines
Quoted 31 lines
fs/nfsd/localio.c +2
-6@@ -58,33 +58,67 @@ void nfsd_localio_ops_init(void)struct nfsd_file *nfsd_open_local_fh (struct net *net,struct auth_domain *dom,struct rpc_clnt *rpc_clnt,const struct cred *cred, -const struct nfs_fh *nfs_fh,const fmode_t fmode) +struct nfs_fh *nfs_fh,const fmode_t fmode) {int mayflags =NFSD_MAY_LOCALIO ;struct svc_cred rq_cred;struct svc_fh fh;struct nfsd_file *localio; +void *nf_inode_key;__be32 beres;if (nfs_fh->size >NFS4_FHSIZE )return ERR_PTR (-EINVAL ); -/* nfs_fh -> svc_fh */ -fh_init (&fh,NFS4_FHSIZE ); - fh.fh_handle.fh_size = nfs_fh->size; -memcpy (fh.fh_handle.fh_raw, nfs_fh->data, nfs_fh->size); -if (fmode &FMODE_READ ) mayflags |=NFSD_MAY_READ ;if (fmode &FMODE_WRITE ) mayflags |=NFSD_MAY_WRITE ;
Quoted 16 lines
fs/nfsd/localio.c void nfsd_localio_ops_init(void) · line 87
Quoted 10 lines
fs/nfsd/localio.c +10
-0+/* +* Check if 'inode_key' stored in @nfs_fh maps to an +* open nfsd_file in the filecache (from a previous +* nfsd_open_local_fh). +* +* The 'inode_key' may have become stale (due to nfsd_file +* being free'd by filecache GC) so the lookup will fail +* gracefully by falling back to using nfsd_file_acquire_local(). +*/ + nf_inode_key =READ_ONCE (nfs_fh->inode_key);I think you want the above in an rcu-locked region with
nfsd_file_acquire_cached(). Else the inode could be freed and reallocated after theREAD_ONCEand before the lookup.
Maybe pass the address ofinode_keyand havenfsd_file_acquire_cached()deref after gettingrcu_read_lock().
Good point, will do.
Quoted 22 lines
Quoted 22 lines
fs/nfsd/localio.c +12
-0+if (nf_inode_key) { + beres =nfsd_file_acquire_cached (net, cred, + nf_inode_key, + mayflags, &localio); +if (beres == nfs_ok) +return localio; +/* +* reset stale nfs_fh->inode_key and fallthru +* to use normal nfsd_file_acquire_local(). +*/ + nfs_fh->inode_key =NULL ; + }include/linux/nfs.h +1
-0@@ -29,6 +29,10 @@struct nfs_fh {unsigned short size;unsigned char data[NFS_MAXFHSIZE ]; +#if IS_ENABLED (CONFIG_NFS_LOCALIO )
Quoted 8 lines
include/linux/nfs.h line 35
Quoted 3 lines
include/linux/nfs.h +3
-0+/* 'inode_key' is an opaque key used for nfsd_file hash lookups */ +void * inode_key; +#endif I wonder if this is really the right place to store the
inode_key. 'structnfs_fh' appears in lots of places and the only place where you want theinode_keyis inside thenfs_inode. Maybe store an augmentednfs_fhin there...
Yeah, I went with nfs_fh because it served my needs (and nfs_fh is passed to nfs_open_local_fh). But very much open to suggestions. While I'm not yet sure about your nfs_inode suggestion, I'll certainly take a closer look after addressing your other feedback.
Thanks for your review!
Mike
Quoted 47 lines · Jeff Layton
Quoted 34 lines · Trond Myklebust
Quoted 20 lines · Chuck Lever III
Quoted 13 lines · NeilBrown
I'm wondering about the request for a garbage-collected
nfsd_filethough. For NFSv3 that makes sense. For NFSv4 we would expect the file
to already be open as a non-garbage-collectednfsd_fileand opening it
again seems wasteful. That doesn't need to be fixed for this patch and
maybe doesn't need to be fixed at all, but it seemed worth highlighting.I don't think using a GC'd
nfsd_filefor LOCALIO is a bug.NFSv4 guarantees an OPEN to pin the
nfsd_file, and a CLOSE (or lease expiry) to release it. There's no desire for or need for garbage collection.Quoted 6 lines
NFSv3 and LOCALIO use each
nfsd_filefor the life of one I/O operation, and thatnfsd_fileis cached in the expectation that another I/O operation on the same file will happen with frequent temporarl proximity. Garbage collection is needed for both cases.No. There is a huge difference between the two. In the case of NFSv3, the server has no idea whether or not there is further need for the file (stateless), whereas in the localio case, the client is able to tell exactly when the application holds the file open or not (stateful).
Quoted 8 lines
The only reason for jumping through all these hoops in the case of localio is the 'user may want to unmount' exception.
Is there any reason why we couldn't add a notification for that, to give knfsd the opportunity to clear out any open file state? The current approach appears to be flat out optimising for the exception.
No, and after discussing it with others here at the bake-a-thon, I think that might be the best path forward here. At a high level:
Quoted 7 lines
Add a callback to the client that runs just before calling
nfsd_file_cache_purge()inexpkey_flush(). The client would be expected to return all of itsnfsd_files"soon" and switch back to normal network I/O. After that point, it can try to get a newnfsd_fileand start up localio at that point. With that change too you can switch to using non-GC'ednfsd_files. The client can just hold them open and do I/O to them at will.
I don't think a "callback" is the best approach - certainly not in the sense of a function pointer that nfs gives to nfsd. Rather we want some protocol, mediated by nfs-local, which allows nfsd to invalidate a thing and nfs to know when it has been invalidated and to signal its release.
I suspect the "thing" is an nfsd_file. I think a suitable protocol involves SRCU. There would be one SRCU state for all of nfsd.
nfs gets a reference to an nfsd_file and holds it as long as it likes. Before accessing nf_file (or nf_inode) it gets srcu_read_lock() on nfsd and checks that the nfsd_file is still valid - probably if it is still hashed. If not it drops the reference. If it is valid it can safely use nf_file for an IO, and then srcu_read_unlock() to release it.
nfsd_file_cache_purge() add a synchronize_srcu() call just before calling nfsd_file_dispose_list()
We would need two different refcounts - on for localio to use which must be augmented with srcu and which doesn't prevent nfsd_file_cond_queue() from queuing for deletion, and the current one which does prevent queuing.
The new refcount would be a kref and when when it reaches zero the nfsd_file is freed. So nfsd_file_slab_free() wouldn't free the nfsd_file but would kref_put() it.
NeilBrown
Quoted 8 lines
I think that's probably less brittle than trying to keep opaque inode pointers around, and would probably mean better performance since you won't be thrashing the filecache as much.
Signature · 1 line
Jeff Layton <[email protected]>