From nobody Mon Sep 30 22:51:43 2024 X-Original-To: dev-commits-src-main@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4XHbsq24x2z5Xp0t; Mon, 30 Sep 2024 22:51:43 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R11" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4XHbsq1fHpz4Zmp; Mon, 30 Sep 2024 22:51:43 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1727736703; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=XKs/9MxWRMfWkEl7WWUMx7+0s8DThSTQFSuEbzXwr58=; b=xSZacP05zagP+tOYlYNKJOvGrBCHlXfgKgDZLkZ8BNuPK7Gi+T89sqTAdXEqOzdB5Qd7TK Umc+mr9klhujoZJXkiwjStxuu5hIy2EZdbi53lYRgNMKzf5TbmF07+fpiazNsMf9KTmSVa swBdZVSNmtWQDobx2/H+0l4BnVSgfRNFK/3pATRAyhBTycmnhg+HXamp+q5PoKWmGWp51d GA8/ohYI99LFYAc3YdziVcZh3BDj4MBQ5BUIxpJXtqzDbfwwky2Oj+XtD6ehIrFQftoPdB xFGh9VYUXSobiUIc0uoHkrCTQNhret165AShNMUTQbc7rD3JeWfGrmGWLPXMLQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1727736703; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=XKs/9MxWRMfWkEl7WWUMx7+0s8DThSTQFSuEbzXwr58=; b=GsKxhxntlq/3G5gczN6K8IwVXAuj/amJJD+gL0Rj1DE4S0nSpZRGNwUVRJti/AxL6Q39cK Vc3z9iqJ01VwW7ITOZPQwt0qIK0ldA/zIqMPgeO4Ley3DjRc18e3AjW4FDZ55q9Q3wmxph EcGYrDi6M4FQOB2bqC5NsTrR9XXUt7bcqLXAt3Xzjhch+Ts0doBrUORpwevnbFZyRDtPUU J/M5PLr3PJSQfFgurPXaS+vg+dEkladX2Oe62M4BuMjq8pG7CYBHJk/8KpQ2gc3TOiAB54 BM1aSk7sMuNprRMQnt88mBJe/QcBKuNoWfN8v9b+FNuqz7VD2/kNfv14oxeetQ== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1727736703; a=rsa-sha256; cv=none; b=PakGzSitbIQpeoaBpChuuSbifi7DazJX62geBFIZ9fqw82pzY9ZO9/o8iCGg9XbuuuHBY+ rDcB7AjvfIOKieMMrKYVXPbvAU4ca7K3QEN0TBB83sd10TYktnIKu9QpguLy/n955D+AbN 6s+mrZplxo2k6Zy4UjEbIv7DGUagLsEGyPILcKyU6R7+sOF3iQUgVpXvFoE4kTGsvdTwxS vc4yzlbb+4de0606fMlTUlvMB3TyNc3SMhkm+7pjyhU32VY49ry8ZRhRGSr7qPMz9kGVL5 akgnj8HyGtKqUHeXVq7lhFjvM4rnkNbCsWygoqLV0TYGSPDpt7DIwXR0zYZMHQ== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4XHbsq1Fm7z1C5W; Mon, 30 Sep 2024 22:51:43 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.18.1/8.18.1) with ESMTP id 48UMphLV076701; Mon, 30 Sep 2024 22:51:43 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 48UMphjP076698; Mon, 30 Sep 2024 22:51:43 GMT (envelope-from git) Date: Mon, 30 Sep 2024 22:51:43 GMT Message-Id: <202409302251.48UMphjP076698@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Rick Macklem Subject: git: eb345e05ac66 - main - nfsd: Fix close handling when vfs.nfsd.enable_locallocks=1 List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-main@freebsd.org Sender: owner-dev-commits-src-main@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: rmacklem X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: eb345e05ac6602eeef0c33fce9025bbc8ec44d0f Auto-Submitted: auto-generated The branch main has been updated by rmacklem: URL: https://cgit.FreeBSD.org/src/commit/?id=eb345e05ac6602eeef0c33fce9025bbc8ec44d0f commit eb345e05ac6602eeef0c33fce9025bbc8ec44d0f Author: Rick Macklem AuthorDate: 2024-09-30 22:49:57 +0000 Commit: Rick Macklem CommitDate: 2024-09-30 22:49:57 +0000 nfsd: Fix close handling when vfs.nfsd.enable_locallocks=1 nfsrv_freeopen() was being called after the mutex lock was released, making it possible for other kernel threads to change the lists while nfsrv_freeopen() took the nfsstateid out of the lists. This patch moves the code around "if (nfsrv_freeopen(stp, vp, 1 p) == 0) {" into nfsrv_freeopen(), so that it can remove the nfsstateid structure from all lists before unlocking the mutex. This should avoid any race between CLOSE and other nfsd threads updating the NFSv4 state. The patch does not affect semantics when vfs.nfsd.enable_locallocks=0. PR: 280978 Tested by: Matthew L. Dailey MFC after: 1 week --- sys/fs/nfsserver/nfs_nfsdstate.c | 62 +++++++++++++++++++--------------------- 1 file changed, 30 insertions(+), 32 deletions(-) diff --git a/sys/fs/nfsserver/nfs_nfsdstate.c b/sys/fs/nfsserver/nfs_nfsdstate.c index 6b40e0f64141..6cd8c1c861ec 100644 --- a/sys/fs/nfsserver/nfs_nfsdstate.c +++ b/sys/fs/nfsserver/nfs_nfsdstate.c @@ -139,7 +139,7 @@ static void nfsrv_dumpaclient(struct nfsclient *clp, struct nfsd_dumpclients *dumpp); static void nfsrv_freeopenowner(struct nfsstate *stp, int cansleep, NFSPROC_T *p); -static int nfsrv_freeopen(struct nfsstate *stp, vnode_t vp, int cansleep, +static void nfsrv_freeopen(struct nfsstate *stp, vnode_t vp, int cansleep, NFSPROC_T *p); static void nfsrv_freelockowner(struct nfsstate *stp, vnode_t vp, int cansleep, NFSPROC_T *p); @@ -1566,7 +1566,7 @@ nfsrv_freeopenowner(struct nfsstate *stp, int cansleep, NFSPROC_T *p) while (nstp != LIST_END(&stp->ls_open)) { tstp = nstp; nstp = LIST_NEXT(nstp, ls_list); - (void) nfsrv_freeopen(tstp, NULL, cansleep, p); + nfsrv_freeopen(tstp, NULL, cansleep, p); } if (stp->ls_op) nfsrvd_derefcache(stp->ls_op); @@ -1581,12 +1581,11 @@ nfsrv_freeopenowner(struct nfsstate *stp, int cansleep, NFSPROC_T *p) * are no other opens on the file. * Returns 1 if it free'd the nfslockfile, 0 otherwise. */ -static int +static void nfsrv_freeopen(struct nfsstate *stp, vnode_t vp, int cansleep, NFSPROC_T *p) { struct nfsstate *nstp, *tstp; struct nfslockfile *lfp; - int ret; LIST_REMOVE(stp, ls_hash); LIST_REMOVE(stp, ls_list); @@ -1595,35 +1594,46 @@ nfsrv_freeopen(struct nfsstate *stp, vnode_t vp, int cansleep, NFSPROC_T *p) lfp = stp->ls_lfp; /* * Now, free all lockowners associated with this open. + * Note that, if vp != NULL, nfsrv_freelockowner() will + * not call nfsrv_freeallnfslocks(), so it needs to be called, below. */ LIST_FOREACH_SAFE(tstp, &stp->ls_open, ls_list, nstp) nfsrv_freelockowner(tstp, vp, cansleep, p); + if (vp != NULL) { + KASSERT(cansleep != 0, ("nfsrv_freeopen: cansleep == 0")); + mtx_assert(NFSSTATEMUTEXPTR, MA_OWNED); + /* + * Only called with vp != NULL for Close when + * vfs.nfsd.enable_locallocks != 0. + * Lock the lfp so that it will not go away and do the + * nfsrv_freeallnfslocks() call that was not done by + * nfsrv_freelockowner(). + */ + nfsrv_locklf(lfp); + NFSUNLOCKSTATE(); + NFSVOPUNLOCK(vp); + nfsrv_freeallnfslocks(stp, vp, cansleep, p); + NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY); + NFSLOCKSTATE(); + nfsrv_unlocklf(lfp); + } + /* * The nfslockfile is freed here if there are no locks * associated with the open. * If there are locks associated with the open, the * nfslockfile structure can be freed via nfsrv_freelockowner(). - * Acquire the state mutex to avoid races with calls to - * nfsrv_getlockfile(). */ - if (cansleep != 0) - NFSLOCKSTATE(); if (lfp != NULL && LIST_EMPTY(&lfp->lf_open) && LIST_EMPTY(&lfp->lf_deleg) && LIST_EMPTY(&lfp->lf_lock) && LIST_EMPTY(&lfp->lf_locallock) && LIST_EMPTY(&lfp->lf_rollback) && lfp->lf_usecount == 0 && - (cansleep != 0 || nfsv4_testlock(&lfp->lf_locallock_lck) == 0)) { + nfsv4_testlock(&lfp->lf_locallock_lck) == 0) nfsrv_freenfslockfile(lfp); - ret = 1; - } else - ret = 0; - if (cansleep != 0) - NFSUNLOCKSTATE(); free(stp, M_NFSDSTATE); NFSD_VNET(nfsstatsv1_p)->srvopens--; nfsrv_openpluslock--; - return (ret); } /* @@ -1636,7 +1646,8 @@ nfsrv_freelockowner(struct nfsstate *stp, vnode_t vp, int cansleep, LIST_REMOVE(stp, ls_hash); LIST_REMOVE(stp, ls_list); - nfsrv_freeallnfslocks(stp, vp, cansleep, p); + if (vp == NULL) + nfsrv_freeallnfslocks(stp, vp, cansleep, p); if (stp->ls_op) nfsrvd_derefcache(stp->ls_op); free(stp, M_NFSDSTATE); @@ -3431,7 +3442,6 @@ nfsrv_openupdate(vnode_t vp, struct nfsstate *new_stp, nfsquad_t clientid, { struct nfsstate *stp; struct nfsclient *clp; - struct nfslockfile *lfp; u_int32_t bits; int error = 0, gotstate = 0, len = 0; u_char *clidp = NULL; @@ -3526,9 +3536,7 @@ nfsrv_openupdate(vnode_t vp, struct nfsstate *new_stp, nfsquad_t clientid, NFSBCOPY(clp->lc_id, clidp, len); gotstate = 1; } - NFSUNLOCKSTATE(); } else if (new_stp->ls_flags & NFSLCK_CLOSE) { - lfp = stp->ls_lfp; if (retwriteaccessp != NULL) { if ((stp->ls_flags & NFSLCK_WRITEACCESS) != 0) *retwriteaccessp = 1; @@ -3536,20 +3544,10 @@ nfsrv_openupdate(vnode_t vp, struct nfsstate *new_stp, nfsquad_t clientid, *retwriteaccessp = 0; } if (nfsrv_dolocallocks != 0 && !LIST_EMPTY(&stp->ls_open)) { - /* Get the lf lock */ - nfsrv_locklf(lfp); - NFSUNLOCKSTATE(); ASSERT_VOP_ELOCKED(vp, "nfsrv_openupdate"); - NFSVOPUNLOCK(vp); - if (nfsrv_freeopen(stp, vp, 1, p) == 0) { - NFSLOCKSTATE(); - nfsrv_unlocklf(lfp); - NFSUNLOCKSTATE(); - } - NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY); + nfsrv_freeopen(stp, vp, 1, p); } else { - (void) nfsrv_freeopen(stp, NULL, 0, p); - NFSUNLOCKSTATE(); + nfsrv_freeopen(stp, NULL, 0, p); } } else { /* @@ -3567,8 +3565,8 @@ nfsrv_openupdate(vnode_t vp, struct nfsstate *new_stp, nfsquad_t clientid, if ((nd->nd_flag & ND_NFSV41) != 0 && stp->ls_stateid.seqid == 0) stp->ls_stateid.seqid = 1; - NFSUNLOCKSTATE(); } + NFSUNLOCKSTATE(); /* * If the client just confirmed its first open, write a timestamp