From nobody Fri Jun 24 16:10:34 2022 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 7688686574C; Fri, 24 Jun 2022 16:10:35 +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 4LV2CZ4Pjkz3JGJ; Fri, 24 Jun 2022 16:10:34 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1656087035; 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=WwpdctxLr2KU6/e7JzFjwLCyrjYBeGR+X52D7PD5BEY=; b=IqbWfG4776n43MzB7zSO+fYhqbojcqP72j4ZcHjfDUZep1KNRiWEK2c5jCP75LWoEkansE IR8B4W6/optnLpLyJHZE3wX3F3hc4I2MpIWert4obafnkWQ+WVrvZeEOCic1KNr5PFCmgX vdusQoDnejv6mE2a3Xu2sMOpDKmy0mnkj2d4c8DddV+HUh5jKbLJ3FyLMiPcxD6eT1HRy9 sJR6ZxMkZfAo/4hfbtt/2+vbr/ypJ7KtckGRUX9dAoCaeVcnLyQwk52/++mKW15MRxMldp TLO9u4L2qsS4objvhKcw5n/fU2rXpQE/yGQuesXkkkjX+zh9E0MO6VVkUPtXyw== 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 5C11112C75; Fri, 24 Jun 2022 16:10:34 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 25OGAYFi006192; Fri, 24 Jun 2022 16:10:34 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 25OGAYHP006191; Fri, 24 Jun 2022 16:10:34 GMT (envelope-from git) Date: Fri, 24 Jun 2022 16:10:34 GMT Message-Id: <202206241610.25OGAYHP006191@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Gleb Smirnoff Subject: git: 315167c0de1a - main - unix: provide an option to return locked from unp_connectat() 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: Sender: owner-dev-commits-src-main@freebsd.org X-BeenThere: dev-commits-src-main@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: glebius X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 315167c0de1a066614a936aa2c8954acb1a909eb Auto-Submitted: auto-generated ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1656087035; 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=WwpdctxLr2KU6/e7JzFjwLCyrjYBeGR+X52D7PD5BEY=; b=oVxVmU7pRAKO0hETWA80lkp7oD5oa5G6K4USnxdXCtQTsKGjbVZRNc3QcB70eVUlX+EMRs U+K9Mu8oJphjNDhmvukVkGUKhk8v5e/Qh6kh4glWF7Yurlw0h41MRFdfQcN1HzKWJFI4xx 2X0n4xWnrw11fRkXg9xwDR/rkauSSxgs6vGAAcWXOjBoagP0HcMKPh0aREoDkmq9Gfqsgp qWLWb7RK722by0mAf2ASmIh9cKPuAw9kL5oFK/HfHVontVdXPzs8ZpW0/IHibK9P/ideez oHCSM+iW6xd+70WcM/D8ypTin720kTj/Xat4BvhYTaJplGJ5r7oDu2/yOt5Yhw== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1656087035; a=rsa-sha256; cv=none; b=EAamT24SgPzA7oHhuLCwd9yTRHne58RrMJOmj6SKEs6kjIgUX1d2/GNh4qCCj6vcdusM72 gyipgE8rNsvsSXXm4p8tT/rDs7b08XwPssBmzlO2ygazpQ8T/DRQfFYqhKCl1qBAjSZHos auWYGag46pAIG1y2ju696AqBy2LENgY2d9eFHFEKS81JQkK13KsKgAZcGv3Fun59fLeM8D TldiPNb/J0vwjraNFEkr9OdWor5KTAPMjaOmk88RpqKkjRnIQIQBCHQTyxechUK/JfSMl9 /njJu4gC2WU1n9mGleqHq+g5JOyUD1SUJIWD1aGkckxv+JcDI+nt/D+rteiBkw== ARC-Authentication-Results: i=1; mx1.freebsd.org; none X-ThisMailContainsUnwantedMimeParts: N The branch main has been updated by glebius: URL: https://cgit.FreeBSD.org/src/commit/?id=315167c0de1a066614a936aa2c8954acb1a909eb commit 315167c0de1a066614a936aa2c8954acb1a909eb Author: Gleb Smirnoff AuthorDate: 2022-06-24 16:09:10 +0000 Commit: Gleb Smirnoff CommitDate: 2022-06-24 16:09:10 +0000 unix: provide an option to return locked from unp_connectat() Use this new version in unix/dgram socket when sending to a target address. This removes extra lock release/acquisition and possible counter-intuitive ENOTCONN. Reviewed by: markj Differential revision: https://reviews.freebsd.org/D35298 --- sys/kern/uipc_usrreq.c | 48 +++++++++++++++++++++++++++--------------------- 1 file changed, 27 insertions(+), 21 deletions(-) diff --git a/sys/kern/uipc_usrreq.c b/sys/kern/uipc_usrreq.c index 2872e28aef3a..594cc35af57d 100644 --- a/sys/kern/uipc_usrreq.c +++ b/sys/kern/uipc_usrreq.c @@ -292,7 +292,7 @@ static int uipc_ctloutput(struct socket *, struct sockopt *); static int unp_connect(struct socket *, struct sockaddr *, struct thread *); static int unp_connectat(int, struct socket *, struct sockaddr *, - struct thread *); + struct thread *, bool); static void unp_connect2(struct socket *so, struct socket *so2, int); static void unp_disconnect(struct unpcb *unp, struct unpcb *unp2); static void unp_dispose(struct socket *so); @@ -721,7 +721,7 @@ uipc_connectat(int fd, struct socket *so, struct sockaddr *nam, int error; KASSERT(td == curthread, ("uipc_connectat: td != curthread")); - error = unp_connectat(fd, so, nam, td); + error = unp_connectat(fd, so, nam, td, false); return (error); } @@ -1212,22 +1212,20 @@ uipc_sosend_dgram(struct socket *so, struct sockaddr *addr, struct uio *uio, } SOCKBUF_UNLOCK(&so->so_snd); - if (addr != NULL && (error = unp_connect(so, addr, td))) - goto out3; - - UNP_PCB_LOCK(unp); - /* - * Because connect() and send() are non-atomic in a sendto() with a - * target address, it's possible that the socket will have disconnected - * before the send() can run. In that case return the slightly - * counter-intuitive but otherwise correct error that the socket is not - * connected. - */ - unp2 = unp_pcb_lock_peer(unp); - if (unp2 == NULL) { - UNP_PCB_UNLOCK(unp); - error = ENOTCONN; - goto out3; + if (addr != NULL) { + if ((error = unp_connectat(AT_FDCWD, so, addr, td, true))) + goto out3; + UNP_PCB_LOCK_ASSERT(unp); + unp2 = unp->unp_conn; + UNP_PCB_LOCK_ASSERT(unp2); + } else { + UNP_PCB_LOCK(unp); + unp2 = unp_pcb_lock_peer(unp); + if (unp2 == NULL) { + UNP_PCB_UNLOCK(unp); + error = ENOTCONN; + goto out3; + } } if (unp2->unp_flags & UNP_WANTCRED_MASK) @@ -1828,12 +1826,12 @@ static int unp_connect(struct socket *so, struct sockaddr *nam, struct thread *td) { - return (unp_connectat(AT_FDCWD, so, nam, td)); + return (unp_connectat(AT_FDCWD, so, nam, td, false)); } static int unp_connectat(int fd, struct socket *so, struct sockaddr *nam, - struct thread *td) + struct thread *td, bool return_locked) { struct mtx *vplock; struct sockaddr_un *soun; @@ -1983,11 +1981,19 @@ unp_connectat(int fd, struct socket *so, struct sockaddr *nam, KASSERT((unp->unp_flags & UNP_CONNECTING) != 0, ("%s: unp %p has UNP_CONNECTING clear", __func__, unp)); unp->unp_flags &= ~UNP_CONNECTING; - unp_pcb_unlock_pair(unp, unp2); + if (!return_locked) + unp_pcb_unlock_pair(unp, unp2); bad2: mtx_unlock(vplock); bad: if (vp != NULL) { + /* + * If we are returning locked (called via uipc_sosend_dgram()), + * we need to be sure that vput() won't sleep. This is + * guaranteed by VOP_UNP_CONNECT() call above and unp2 lock. + * SOCK_STREAM/SEQPACKET can't request return_locked (yet). + */ + MPASS(!(return_locked && connreq)); vput(vp); } free(sa, M_SONAME);