diff --git a/sys/fs/nfsclient/nfs_clstate.c.freeown b/sys/fs/nfsclient/nfs_clstate.c --- a/sys/fs/nfsclient/nfs_clstate.c.freeown +++ b/sys/fs/nfsclient/nfs_clstate.c @@ -164,7 +164,7 @@ static void nfscl_freedeleg(struct nfscldeleghead *, struct nfscldeleg *, bool); static int nfscl_errmap(struct nfsrv_descript *, u_int32_t); -static void nfscl_cleanup_common(struct nfsclclient *, u_int8_t *); +static bool nfscl_cleanup_common(struct nfsclclient *, u_int8_t *); static int nfscl_recalldeleg(struct nfsclclient *, struct nfsmount *, struct nfscldeleg *, vnode_t, struct ucred *, NFSPROC_T *, int, vnode_t *); @@ -1648,8 +1648,21 @@ static void nfscl_freeopenowner(struct nfsclowner *owp, int local) { + int owned; + /* + * Make sure the NFSCLSTATE mutex is held, to avoid races with + * calls in nfscl_renewthread() that do not hold a reference + * count on the nfsclclient and just the mutex. + * The mutex will not be held for calls done with the exclusive + * nfsclclient lock held. + */ + owned = mtx_owned(NFSCLSTATEMUTEXPTR); + if (owned == 0) + NFSLOCKCLSTATE(); LIST_REMOVE(owp, nfsow_list); + if (owned == 0) + NFSUNLOCKCLSTATE(); free(owp, M_NFSCLOWNER); if (local) nfsstatsv1.cllocalopenowners--; @@ -1664,8 +1677,21 @@ nfscl_freelockowner(struct nfscllockowner *lp, int local) { struct nfscllock *lop, *nlop; + int owned; + /* + * Make sure the NFSCLSTATE mutex is held, to avoid races with + * calls in nfscl_renewthread() that do not hold a reference + * count on the nfsclclient and just the mutex. + * The mutex will not be held for calls done with the exclusive + * nfsclclient lock held. + */ + owned = mtx_owned(NFSCLSTATEMUTEXPTR); + if (owned == 0) + NFSLOCKCLSTATE(); LIST_REMOVE(lp, nfsl_list); + if (owned == 0) + NFSUNLOCKCLSTATE(); LIST_FOREACH_SAFE(lop, &lp->nfsl_lock, nfslo_list, nlop) { nfscl_freelock(lop, local); } @@ -1851,13 +1877,15 @@ * This function must be called after the process represented by "own" has * exited. Must be called with CLSTATE lock held. */ -static void +static bool nfscl_cleanup_common(struct nfsclclient *clp, u_int8_t *own) { struct nfsclowner *owp, *nowp; struct nfscllockowner *lp, *nlp; struct nfscldeleg *dp; + bool didfree; + didfree = false; /* First, get rid of local locks on delegations. */ TAILQ_FOREACH(dp, &clp->nfsc_deleg, nfsdl_list) { LIST_FOREACH_SAFE(lp, &dp->nfsdl_lock, nfsl_list, nlp) { @@ -1865,6 +1893,7 @@ if ((lp->nfsl_rwlock.nfslock_lock & NFSV4LOCK_WANTED)) panic("nfscllckw"); nfscl_freelockowner(lp, 1); + didfree = true; } } } @@ -1879,13 +1908,15 @@ * here. For that case, let the renew thread clear * out the OpenOwner later. */ - if (LIST_EMPTY(&owp->nfsow_open)) + if (LIST_EMPTY(&owp->nfsow_open)) { nfscl_freeopenowner(owp, 0); - else + didfree = true; + } else owp->nfsow_defunct = 1; } owp = nowp; } + return (didfree); } /* @@ -1894,10 +1925,11 @@ static void nfscl_cleanupkext(struct nfsclclient *clp, struct nfscllockownerfhhead *lhp) { - struct nfsclowner *owp, *nowp; + struct nfsclowner *owp; struct nfsclopen *op; struct nfscllockowner *lp, *nlp; struct nfscldeleg *dp; + uint8_t own[NFSV4CL_LOCKNAMELEN]; /* * All the pidhash locks must be acquired, since they are sx locks @@ -1907,15 +1939,19 @@ */ pidhash_slockall(); NFSLOCKCLSTATE(); - LIST_FOREACH_SAFE(owp, &clp->nfsc_owner, nfsow_list, nowp) { +tryagain: + LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) { LIST_FOREACH(op, &owp->nfsow_open, nfso_list) { LIST_FOREACH_SAFE(lp, &op->nfso_lock, nfsl_list, nlp) { if (LIST_EMPTY(&lp->nfsl_lock)) nfscl_emptylockowner(lp, lhp); } } - if (nfscl_procdoesntexist(owp->nfsow_owner)) - nfscl_cleanup_common(clp, owp->nfsow_owner); + if (nfscl_procdoesntexist(owp->nfsow_owner)) { + memcpy(own, owp->nfsow_owner, NFSV4CL_LOCKNAMELEN); + if (nfscl_cleanup_common(clp, own)) + goto tryagain; + } } /* @@ -1926,9 +1962,14 @@ * nfscl_cleanup_common(). */ TAILQ_FOREACH(dp, &clp->nfsc_deleg, nfsdl_list) { - LIST_FOREACH_SAFE(lp, &dp->nfsdl_lock, nfsl_list, nlp) { - if (nfscl_procdoesntexist(lp->nfsl_owner)) - nfscl_cleanup_common(clp, lp->nfsl_owner); +tryagain2: + LIST_FOREACH(lp, &dp->nfsdl_lock, nfsl_list) { + if (nfscl_procdoesntexist(lp->nfsl_owner)) { + memcpy(own, lp->nfsl_owner, + NFSV4CL_LOCKNAMELEN); + if (nfscl_cleanup_common(clp, own)) + goto tryagain2; + } } } NFSUNLOCKCLSTATE();