From nobody Thu Nov 23 04:01:03 2023 X-Original-To: dev-commits-src-all@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 4SbPYC5WDyz51C27; Thu, 23 Nov 2023 04:01:03 +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 "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4SbPYC4m7Wz4Nxq; Thu, 23 Nov 2023 04:01:03 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1700712063; 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=bhLuLVhWbzLx4PQG/k6WUEYPbB2WCFa2w+K3L/HLSw0=; b=RXxTrhZjT0NaS3916KUBhyfyYSBG6UDyElZ57kYZXTF5uArYSWy1zC5BOglZcuyVE1qR4b xcBrN7zbK4SxENBo3eoXUAc6+qvcil7hE96see7wAfJP08ivoVDr+d3pEp3x9JvymrnCeV 2YGp+FEecYuuiYVxj7fKwYfAVJZaS6T0ApvvDdcRqR1k1WzbIZIs+oZLldmhzN1PA66VqR hwB2nddy+75UacbfAiAuItU/GA7dfC0nBHHUR1oeiomzv/ShNWv4zv9T92UqvrPaoE7/GZ r6M5GokNVzzKXZUSiIi89RKm+iogXcakNHzQFf0u4MiJ9mavnf2YVUTMoStvIQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1700712063; 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=bhLuLVhWbzLx4PQG/k6WUEYPbB2WCFa2w+K3L/HLSw0=; b=fgmg7/1yNKcGufYxFDmJ2G+dimUkxGyx/EIEJYhXR0emeZJymZuSYKtBk1HYNZMMucIxMA O+5P/PgdfIiLsXtnn3Wl1eQkUd2IcLL7a1BStX+2xw1jr3dW7q6hoG5K2FGeWhBKlqmX/e jECEW7xrgzgJxE8D7J+NWlJhJE3TOey+sKaZOYthyx2C6VgJjvhx0ae3ROv+EU64G4rwy5 wnPnOalsilwCrlTWzILfSlV8Y4agSiZWzIqe/Q5DU18rZxF8w7Dr5PlX4i9pLIkTVIzc15 toM6YOZX1afxBQy+jU7FuKGjqqT/x8I2wBx9yRv3ds3b08reZf8VEjZYNOAokQ== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1700712063; a=rsa-sha256; cv=none; b=UlbFWEN1xrEAPJurEowRMJ3nxWyfkSKC/2+UuYBEAhLAh34FR+SBufdNaJ5ULyQPDzyRLm aq2VMc3K65Gv85umWhpG+h6E8qqjBYpLPznYwsVUq1zDjsf/Seoi7Izs8FLMJYzDG0WOHK jpx+LN9SBdFeZVF+O/9X8QLwBd/4bbW9ZF5FJNeh3zCQqr9tdyOWhaXVfdqrZXFEyHeIgY qVsP3PNVoRujX4Fc6UgF4Dlvnz15qW9HDLRyCqKDeWu28+6ynynpoP54yenLz9cs0/LULj KAYmOL/hgUsQx68jrJRCmTG0WSTfyqZ+q9bZG3eX1GVrf1r84zUa7ut7p9wCvw== 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 4SbPYC3Sx1zmfN; Thu, 23 Nov 2023 04:01:03 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.17.1/8.17.1) with ESMTP id 3AN413uJ098692; Thu, 23 Nov 2023 04:01:03 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 3AN413J0098689; Thu, 23 Nov 2023 04:01:03 GMT (envelope-from git) Date: Thu, 23 Nov 2023 04:01:03 GMT Message-Id: <202311230401.3AN413J0098689@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Konstantin Belousov Subject: git: 3114e149b020 - stable/14 - vn_lock_pair(): reasonably handle vp1 == vp2 case List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: kib X-Git-Repository: src X-Git-Refname: refs/heads/stable/14 X-Git-Reftype: branch X-Git-Commit: 3114e149b02016f7231c9f2fc774b7a61af191c4 Auto-Submitted: auto-generated The branch stable/14 has been updated by kib: URL: https://cgit.FreeBSD.org/src/commit/?id=3114e149b02016f7231c9f2fc774b7a61af191c4 commit 3114e149b02016f7231c9f2fc774b7a61af191c4 Author: Konstantin Belousov AuthorDate: 2023-11-16 22:42:25 +0000 Commit: Konstantin Belousov CommitDate: 2023-11-23 04:00:36 +0000 vn_lock_pair(): reasonably handle vp1 == vp2 case (cherry picked from commit 22bac49b09a9f975330dc0d811436599ce740890) --- sys/kern/vfs_vnops.c | 41 ++++++++++++++++++++++++++++++++++++----- 1 file changed, 36 insertions(+), 5 deletions(-) diff --git a/sys/kern/vfs_vnops.c b/sys/kern/vfs_vnops.c index 27ce5401f15f..f81516608eeb 100644 --- a/sys/kern/vfs_vnops.c +++ b/sys/kern/vfs_vnops.c @@ -4042,10 +4042,10 @@ vn_lock_pair_pause(const char *wmesg) } /* - * Lock pair of vnodes vp1, vp2, avoiding lock order reversal. - * vp1_locked indicates whether vp1 is locked; if not, vp1 must be - * unlocked. Same for vp2 and vp2_locked. One of the vnodes can be - * NULL. + * Lock pair of (possibly same) vnodes vp1, vp2, avoiding lock order + * reversal. vp1_locked indicates whether vp1 is locked; if not, vp1 + * must be unlocked. Same for vp2 and vp2_locked. One of the vnodes + * can be NULL. * * The function returns with both vnodes exclusively or shared locked, * according to corresponding lkflags, and guarantees that it does not @@ -4056,12 +4056,14 @@ vn_lock_pair_pause(const char *wmesg) * * Only one of LK_SHARED and LK_EXCLUSIVE must be specified. * LK_NODDLKTREAT can be optionally passed. + * + * If vp1 == vp2, only one, most exclusive, lock is obtained on it. */ void vn_lock_pair(struct vnode *vp1, bool vp1_locked, int lkflags1, struct vnode *vp2, bool vp2_locked, int lkflags2) { - int error; + int error, locked1; MPASS(((lkflags1 & LK_SHARED) != 0) ^ ((lkflags1 & LK_EXCLUSIVE) != 0)); MPASS((lkflags1 & ~(LK_SHARED | LK_EXCLUSIVE | LK_NODDLKTREAT)) == 0); @@ -4071,6 +4073,35 @@ vn_lock_pair(struct vnode *vp1, bool vp1_locked, int lkflags1, if (vp1 == NULL && vp2 == NULL) return; + if (vp1 == vp2) { + MPASS(vp1_locked == vp2_locked); + + /* Select the most exclusive mode for lock. */ + if ((lkflags1 & LK_TYPE_MASK) != (lkflags2 & LK_TYPE_MASK)) + lkflags1 = (lkflags1 & ~LK_SHARED) | LK_EXCLUSIVE; + + if (vp1_locked) { + ASSERT_VOP_LOCKED(vp1, "vp1"); + + /* No need to relock if any lock is exclusive. */ + if ((vp1->v_vnlock->lock_object.lo_flags & + LK_NOSHARE) != 0) + return; + + locked1 = VOP_ISLOCKED(vp1); + if (((lkflags1 & LK_SHARED) != 0 && + locked1 != LK_EXCLUSIVE) || + ((lkflags1 & LK_EXCLUSIVE) != 0 && + locked1 == LK_EXCLUSIVE)) + return; + VOP_UNLOCK(vp1); + } + + ASSERT_VOP_UNLOCKED(vp1, "vp1"); + vn_lock(vp1, lkflags1 | LK_RETRY); + return; + } + if (vp1 != NULL) { if ((lkflags1 & LK_SHARED) != 0 && (vp1->v_vnlock->lock_object.lo_flags & LK_NOSHARE) != 0)