NFSD: fix problems with cleanup on errors in nfsd4_copy
Subsystems (2)
Dai Ngo
reviewerPATCH v3 1/1 NFSD: fix problems with cleanup on errors in nfsd4_copy
When nfsd4_copy fails to allocate memory for async_copy->cp_src, or nfs4_init_copy_state fails, it calls cleanup_async_copy to do the cleanup for the async_copy which causes page fault since async_copy is not yet initialized.
This patche rearranges the order of initializing the fields in async_copy and adds checks in cleanup_async_copy to skip un-initialized fields.
---
v3: replace list_del with list_del_init
fs/nfsd/nfs4proc.c | 12 ++++++++---- fs/nfsd/nfs4state.c | 5 +++-- 2 files changed, 11 insertions(+), 6 deletions(-)
fs/nfsd/nfs4proc.c +8 -4
@@ -1687,9 +1687,12 @@ static void cleanup_async_copy(struct nfsd4_copy *copy) {nfs4_free_copy_state (copy);release_copy_files (copy); -spin_lock (©->cp_clp->async_lock); -list_del (©->copies); -spin_unlock (©->cp_clp->async_lock); +if (copy->cp_clp) { +spin_lock (©->cp_clp->async_lock); +if (!list_empty (©->copies)) +list_del_init (©->copies); +spin_unlock (©->cp_clp->async_lock); + }nfs4_put_copy (copy); }
@@ -1786,12 +1789,13 @@ nfsd4_copy(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, async_copy =kzalloc (sizeof (struct nfsd4_copy ),GFP_KERNEL );if (!async_copy) goto out_err; +INIT_LIST_HEAD (&async_copy->copies); +refcount_set (&async_copy->refcount,1 ); async_copy->cp_src =kmalloc (sizeof (*async_copy->cp_src),GFP_KERNEL );if (!async_copy->cp_src) goto out_err;if (!nfs4_init_copy_state (nn, copy)) goto out_err; -refcount_set (&async_copy->refcount,1 );memcpy (©->cp_res.cb_stateid, ©->cp_stateid.cs_stid,sizeof (copy->cp_res.cb_stateid));dup_copy_fields (copy, async_copy);
fs/nfsd/nfs4state.c +3 -2
@@ -975,7 +975,6 @@ static int nfs4_init_cp_state(struct nfsd_net *nn, copy_stateid_t *stid, stid->cs_stid.si_opaque.so_clid.cl_boot = (u32 )nn->boot_time; stid->cs_stid.si_opaque.so_clid.cl_id = nn->s2s_cp_cl_id; - stid->cs_type = cs_type;idr_preload (GFP_KERNEL );spin_lock (&nn->s2s_cp_lock);
@@ -986,6 +985,7 @@ static int nfs4_init_cp_state(struct nfsd_net *nn, copy_stateid_t *stid,idr_preload_end ();if (new_id <0 )return 0 ; + stid->cs_type = cs_type;return 1 ; }
@@ -1019,7 +1019,8 @@ void nfs4_free_copy_state(struct nfsd4_copy *copy) {struct nfsd_net *nn; -WARN_ON_ONCE (copy->cp_stateid.cs_type !=NFS4_COPY_STID ); +if (copy->cp_stateid.cs_type !=NFS4_COPY_STID ) +return ; nn =net_generic (copy->cp_clp->net, nfsd_net_id);spin_lock (&nn->s2s_cp_lock); idr_remove(&nn->s2s_cp_stateids,
Signature · 1 line
2.9.5
Quoted 75 lines · Dai Ngo
When
nfsd4_copyfails to allocate memory forasync_copy->cp_src, ornfs4_init_copy_statefails, it callscleanup_async_copyto do the cleanup for theasync_copywhich causes page fault sinceasync_copyis not yet initialized.This patche rearranges the order of initializing the fields in
async_copyand adds checks incleanup_async_copyto skip un-initialized fields.Fixes ce0887ac96d3 ("NFSD add nfs4 inter ssc to nfsd4_copy")Fixes 87689df69491 ("NFSD: Shrink size of struct nfsd4_copy")Signed-off-by Dai Ngo <[email protected]>---
v3: replacelist_delwithlist_del_initfs/nfsd/nfs4proc.c | 12 ++++++++---- fs/nfsd/nfs4state.c | 5 +++-- 2 files changed, 11 insertions(+), 6 deletions(-)fs/nfsd/nfs4proc.c +8
-4@@ -1687,9 +1687,12 @@ static void cleanup_async_copy(struct nfsd4_copy *copy) {nfs4_free_copy_state (copy);release_copy_files (copy); -spin_lock (©->cp_clp->async_lock); -list_del (©->copies); -spin_unlock (©->cp_clp->async_lock); +if (copy->cp_clp) { +spin_lock (©->cp_clp->async_lock); +if (!list_empty (©->copies)) +list_del_init (©->copies); +spin_unlock (©->cp_clp->async_lock); + }nfs4_put_copy (copy); }@@ -1786,12 +1789,13 @@ nfsd4_copy(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, async_copy =kzalloc (sizeof (struct nfsd4_copy ),GFP_KERNEL );if (!async_copy) goto out_err; +INIT_LIST_HEAD (&async_copy->copies); +refcount_set (&async_copy->refcount,1 ); async_copy->cp_src =kmalloc (sizeof (*async_copy->cp_src),GFP_KERNEL );if (!async_copy->cp_src) goto out_err;if (!nfs4_init_copy_state (nn, copy)) goto out_err; -refcount_set (&async_copy->refcount,1 );memcpy (©->cp_res.cb_stateid, ©->cp_stateid.cs_stid,sizeof (copy->cp_res.cb_stateid));dup_copy_fields (copy, async_copy);fs/nfsd/nfs4state.c +1
-1@@ -975,7 +975,6 @@ static int nfs4_init_cp_state(struct nfsd_net *nn, copy_stateid_t *stid, stid->cs_stid.si_opaque.so_clid.cl_boot = (u32 )nn->boot_time; stid->cs_stid.si_opaque.so_clid.cl_id = nn->s2s_cp_cl_id; - stid->cs_type = cs_type;idr_preload (GFP_KERNEL );spin_lock (&nn->s2s_cp_lock);@@ -986,6 +985,7 @@ static int nfs4_init_cp_state(struct nfsd_net *nn, copy_stateid_t *stid,idr_preload_end ();if (new_id <0 )return 0 ; + stid->cs_type = cs_type;return 1 ; }
fs/nfsd/nfs4state.c void nfs4_free_copy_state(struct nfsd4_copy *copy) · line 1026
Quoted 9 lines
fs/nfsd/nfs4state.c +2
-1@@ -1019,7 +1019,8 @@ void nfs4_free_copy_state(struct nfsd4_copy *copy) {struct nfsd_net *nn; -WARN_ON_ONCE (copy->cp_stateid.cs_type !=NFS4_COPY_STID ); +if (copy->cp_stateid.cs_type !=NFS4_COPY_STID ) +return ; nn =net_generic (copy->cp_clp->net, nfsd_net_id);spin_lock (&nn->s2s_cp_lock); idr_remove(&nn->s2s_cp_stateids,