From owner-dev-commits-src-all@freebsd.org Sun Jan 17 05:10:45 2021 Return-Path: Delivered-To: dev-commits-src-all@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id C41474DBDB9; Sun, 17 Jan 2021 05:10:45 +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 4DJNKc6yLLz3wLH; Sun, 17 Jan 2021 05:10:43 +0000 (UTC) (envelope-from git@FreeBSD.org) 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 499571BFE7; Sun, 17 Jan 2021 05:10:43 +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 10H5AhPv046178; Sun, 17 Jan 2021 05:10:43 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 10H5AhBB046177; Sun, 17 Jan 2021 05:10:43 GMT (envelope-from git) Date: Sun, 17 Jan 2021 05:10:43 GMT Message-Id: <202101170510.10H5AhBB046177@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: 71dfe98e34ac - stable/12 - tty_wait_background: improve locking. 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/12 X-Git-Reftype: branch X-Git-Commit: 71dfe98e34acdcb8b0f90880de28611e66662e91 Auto-Submitted: auto-generated X-BeenThere: dev-commits-src-all@freebsd.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Commit messages for all branches of the src repository List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 17 Jan 2021 05:10:46 -0000 The branch stable/12 has been updated by kib: URL: https://cgit.FreeBSD.org/src/commit/?id=71dfe98e34acdcb8b0f90880de28611e66662e91 commit 71dfe98e34acdcb8b0f90880de28611e66662e91 Author: Konstantin Belousov AuthorDate: 2020-12-31 13:45:06 +0000 Commit: Konstantin Belousov CommitDate: 2021-01-17 04:45:47 +0000 tty_wait_background: improve locking. (cherry picked from commit a008bdeda3b8278fe600cf83ecf44acd1ccb30b6) --- sys/kern/tty.c | 33 ++++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/sys/kern/tty.c b/sys/kern/tty.c index 63324ac3e881..693032908b3a 100644 --- a/sys/kern/tty.c +++ b/sys/kern/tty.c @@ -413,7 +413,7 @@ tty_is_ctty(struct tty *tp, struct proc *p) int tty_wait_background(struct tty *tp, struct thread *td, int sig) { - struct proc *p = td->td_proc; + struct proc *p; struct pgrp *pg; ksiginfo_t ksi; int error; @@ -421,8 +421,22 @@ tty_wait_background(struct tty *tp, struct thread *td, int sig) MPASS(sig == SIGTTIN || sig == SIGTTOU); tty_assert_locked(tp); + p = td->td_proc; for (;;) { + pg = p->p_pgrp; + PGRP_LOCK(pg); PROC_LOCK(p); + + /* + * pg may no longer be our process group. + * Re-check after locking. + */ + if (p->p_pgrp != pg) { + PROC_UNLOCK(p); + PGRP_UNLOCK(pg); + continue; + } + /* * The process should only sleep, when: * - This terminal is the controlling terminal @@ -435,6 +449,7 @@ tty_wait_background(struct tty *tp, struct thread *td, int sig) if (!tty_is_ctty(tp, p) || p->p_pgrp == tp->t_pgrp) { /* Allow the action to happen. */ PROC_UNLOCK(p); + PGRP_UNLOCK(pg); return (0); } @@ -442,13 +457,14 @@ tty_wait_background(struct tty *tp, struct thread *td, int sig) SIGISMEMBER(td->td_sigmask, sig)) { /* Only allow them in write()/ioctl(). */ PROC_UNLOCK(p); + PGRP_UNLOCK(pg); return (sig == SIGTTOU ? 0 : EIO); } - pg = p->p_pgrp; if ((p->p_flag & P_PPWAIT) != 0 || pg->pg_jobc == 0) { /* Don't allow the action to happen. */ PROC_UNLOCK(p); + PGRP_UNLOCK(pg); return (EIO); } PROC_UNLOCK(p); @@ -463,20 +479,7 @@ tty_wait_background(struct tty *tp, struct thread *td, int sig) ksi.ksi_signo = sig; sig = 0; } - PGRP_LOCK(pg); - - /* - * pg may no longer be our process group. - * Re-check after locking process group. - */ - PROC_LOCK(p); - if (p->p_pgrp != pg) { - PROC_UNLOCK(p); - PGRP_UNLOCK(pg); - continue; - } - PROC_UNLOCK(p); pgsignal(pg, ksi.ksi_signo, 1, &ksi); PGRP_UNLOCK(pg);