From jdc@koitsu.strangled.net  Wed Oct 24 03:45:55 2012
Return-Path: <jdc@koitsu.strangled.net>
Received: from mx1.freebsd.org (mx1.freebsd.org [69.147.83.52])
	by hub.freebsd.org (Postfix) with ESMTP id 3F6773F1
	for <freebsd-gnats-submit@freebsd.org>; Wed, 24 Oct 2012 03:45:55 +0000 (UTC)
	(envelope-from jdc@koitsu.strangled.net)
Received: from qmta14.emeryville.ca.mail.comcast.net (qmta14.emeryville.ca.mail.comcast.net [IPv6:2001:558:fe2d:44:76:96:27:212])
	by mx1.freebsd.org (Postfix) with ESMTP id 232DE8FC0A
	for <freebsd-gnats-submit@freebsd.org>; Wed, 24 Oct 2012 03:45:55 +0000 (UTC)
Received: from omta13.emeryville.ca.mail.comcast.net ([76.96.30.52])
	by qmta14.emeryville.ca.mail.comcast.net with comcast
	id ErkC1k00217UAYkAErluQb; Wed, 24 Oct 2012 03:45:54 +0000
Received: from koitsu.strangled.net ([67.180.84.87])
	by omta13.emeryville.ca.mail.comcast.net with comcast
	id Erlt1k00Y1t3BNj8ZrludA; Wed, 24 Oct 2012 03:45:54 +0000
Received: by icarus.home.lan (Postfix, from userid 1000)
	id E5D8473A1A; Tue, 23 Oct 2012 20:45:53 -0700 (PDT)
Message-Id: <20121024034553.E5D8473A1A@icarus.home.lan>
Date: Tue, 23 Oct 2012 20:45:53 -0700 (PDT)
From: Jeremy Chadwick <jdc@koitsu.org>
Reply-To: Jeremy Chadwick <jdc@koitsu.org>
To: FreeBSD-gnats-submit@freebsd.org
Cc:
Subject: Backgrounded processes remain in ttyin state / SIGCONT does not remove sleeping processes from sleep queue
X-Send-Pr-Version: 3.113
X-GNATS-Notify:

>Number:         173010
>Category:       kern
>Synopsis:       Backgrounded processes remain in ttyin state / SIGCONT does not remove sleeping processes from sleep queue
>Confidential:   no
>Severity:       non-critical
>Priority:       medium
>Responsible:    freebsd-bugs
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Wed Oct 24 03:50:00 UTC 2012
>Closed-Date:    Sun Nov 11 13:37:57 CET 2012
>Last-Modified:  Sun Nov 11 13:37:57 CET 2012
>Originator:     Jeremy Chadwick
>Release:        FreeBSD 9.1-PRERELEASE amd64
>Organization:
>Environment:
System: FreeBSD icarus.home.lan 9.1-PRERELEASE FreeBSD 9.1-PRERELEASE #0: Sun Oct 21 05:24:09 PDT 2012 root@icarus.home.lan:/usr/obj/usr/src/sys/X7SBA_RELENG_9_amd64 amd64
>Description:
	The following bug/quirk in the tty layer has been discovered.  This
	issue affects processes which require tty input but are backgrounded.

	This issue has been discussed with kib@ and ed@ on freebsd-stable who
	confirmed the issue.

	Full details are in the thread titled "pty/tty or signal strangeness,
	or grep/bsdgrep bug?" here:

	http://lists.freebsd.org/pipermail/freebsd-stable/2012-October/thread.html#70204

	This affects any process that requires tty input; it's not specific
	grep/bsdgrep.
>How-To-Repeat:
	Step-by-step instructions are available:

	http://lists.freebsd.org/pipermail/freebsd-stable/2012-October/070204.html
>Fix:
	kib@ did provide a patch but mentioned it would be best to wait for
	what ed@ proposes.
>Release-Note:
>Audit-Trail:

From: Jeremy Chadwick <jdc@koitsu.org>
To: FreeBSD-gnats-submit@FreeBSD.org, freebsd-bugs@FreeBSD.org
Cc:  
Subject: Re: kern/173010: Backgrounded processes remain in ttyin state /
 SIGCONT does not remove sleeping processes from sleep queue
Date: Thu, 25 Oct 2012 01:51:45 -0700

 --ReaqsoxgOBHFXBhH
 Content-Type: text/plain; charset=us-ascii
 Content-Disposition: inline
 
 Mail thread on -stable at this point, including a patch from ed@
 
 From: Ed Schouten <ed@80386.nl>
 To: Konstantin Belousov <kostikbel@gmail.com>
 Date: Thu, 25 Oct 2012 09:45:11 +0200
 Cc: Jeremy Chadwick <jdc@koitsu.org>, freebsd-stable@freebsd.org, jhb@freebsd.org
 Subject: Re: pty/tty or signal strangeness, or grep/bsdgrep bug?
 
 Hi all,
 
 2012/10/23 Ed Schouten <ed@80386.nl>:
 > Will try to come up with a decent patch tomorrow evening.
 
 Ahem; the day after tomorrow. Jeremy, could you please try the following patch?
 
 http://80386.nl/pub/tty-bg-read.txt
 
 I decomposed the TTY read routine into four separate functions to
 improve clarity. While this was initially true, I think it's a pity
 the four functions are constantly becoming a bit more complex.
 
 The same issue is also present on the output path, but I have no idea
 how realistic/hard it is to fix this issue. Also, it might not really
 be an issue in practice. If you do a large write and become a
 non-foreground process group, you might be able to circumvent TOSTOP
 while the write() is in transit.
 
 Fixing this might be tedious, because we currently enforce that writes
 to a TTY are serialized. Blocking inside the write() might then cause
 a deadlock. But in my opinion, I would prefer the serialization over
 the enforcing of TOSTOP.
 
 Thanks again for reporting the issue!
 
 -- 
 Ed Schouten <ed@80386.nl>
 
 
 
 
 From: Jeremy Chadwick <jdc@koitsu.org>
 To: Ed Schouten <ed@80386.nl>
 Date: Thu, 25 Oct 2012 01:46:03 -0700
 Cc: Konstantin Belousov <kostikbel@gmail.com>, freebsd-stable@freebsd.org, jhb@freebsd.org
 Subject: Re: pty/tty or signal strangeness, or grep/bsdgrep bug?
 
 On Thu, Oct 25, 2012 at 09:45:11AM +0200, Ed Schouten wrote:
 > 2012/10/23 Ed Schouten <ed@80386.nl>:
 > > Will try to come up with a decent patch tomorrow evening.
 >
 > Ahem; the day after tomorrow. Jeremy, could you please try the following patch?
 >
 > http://80386.nl/pub/tty-bg-read.txt
 >
 > I decomposed the TTY read routine into four separate functions to
 > improve clarity. While this was initially true, I think it's a pity
 > the four functions are constantly becoming a bit more complex.
 >
 > The same issue is also present on the output path, but I have no idea
 > how realistic/hard it is to fix this issue. Also, it might not really
 > be an issue in practice. If you do a large write and become a
 > non-foreground process group, you might be able to circumvent TOSTOP
 > while the write() is in transit.
 >
 > Fixing this might be tedious, because we currently enforce that writes
 > to a TTY are serialized. Blocking inside the write() might then cause
 > a deadlock. But in my opinion, I would prefer the serialization over
 > the enforcing of TOSTOP.
 
 After the patch, testing with grep and cat, and checking cv/state for
 the latter case:
 
 
 (01:30:39 jdc@icarus) ~ $ csh
 % grep -r "-2011" . &
 [1] 1964
 % jobs
 [1]  + Suspended (tty input)         grep -r -2011 .
 % fg
 grep -r -2011 .
 ^C
 % jobs
 
 % grep -r "-2011" .
 ^Z
 Suspended
 % bg
 [1]    grep -r -2011 . &
 [1]  + Suspended (tty input)         grep -r -2011 .
 % fg
 grep -r -2011 .
 ^C
 
 % cat &
 [1] 2042
 % jobs
 [1]  + Suspended (tty input)         cat
 % ps -auxwU jdc | grep cat
 jdc  2042  0.0  0.0  9908 1496  1  T     1:34AM 0:00.00 cat
 jdc  2044  0.0  0.0 16268 1864  1  S+    1:34AM 0:00.00 grep cat
 % top -b -U jdc | grep cat
  2042 jdc         1  20    0  9908K  1496K STOP    0   0:00  0.00% cat
  2047 jdc         1  20    0 16268K  1864K piperd  0   0:00  0.00% grep cat
 % fg
 cat
 ^C
 % exit
 
 
 I do not get dropped characters or witness any other anomalies.  I
 tested behaviour with /bin/sh, as well as bash.  All seems good.
 
 > Thanks again for reporting the issue!
 
 No, thank *you* and others for looking + fixing it!  :-)
 
 I assume a commit to HEAD + MFC in 2 weeks is in order?
 
 I'll update the PR with this part of our mail thread.
 
 -- 
 | Jeremy Chadwick                                   jdc@koitsu.org |
 | UNIX Systems Administrator                http://jdc.koitsu.org/ |
 | Mountain View, CA, US                                            |
 | Making life hard for others since 1977.             PGP 4BD6C0CB |
 
 
 --ReaqsoxgOBHFXBhH
 Content-Type: text/plain; charset=us-ascii
 Content-Disposition: attachment; filename="tty-bg-read.txt"
 
 Index: sys/kern/tty.c
 ===================================================================
 --- sys/kern/tty.c	(Revision 241962)
 +++ sys/kern/tty.c	(Arbeitskopie)
 @@ -361,7 +361,7 @@
  	return (p->p_session == tp->t_session && p->p_flag & P_CONTROLT);
  }
  
 -static int
 +int
  tty_wait_background(struct tty *tp, struct thread *td, int sig)
  {
  	struct proc *p = td->td_proc;
 @@ -433,13 +433,6 @@
  	error = ttydev_enter(tp);
  	if (error)
  		goto done;
 -
 -	error = tty_wait_background(tp, curthread, SIGTTIN);
 -	if (error) {
 -		tty_unlock(tp);
 -		goto done;
 -	}
 -
  	error = ttydisc_read(tp, uio, ioflag);
  	tty_unlock(tp);
  
 Index: sys/kern/tty_ttydisc.c
 ===================================================================
 --- sys/kern/tty_ttydisc.c	(Revision 241962)
 +++ sys/kern/tty_ttydisc.c	(Arbeitskopie)
 @@ -126,6 +126,10 @@
  	breakc[n] = '\0';
  
  	do {
 +		error = tty_wait_background(tp, curthread, SIGTTIN);
 +		if (error)
 +			return (error);
 +
  		/*
  		 * Quite a tricky case: unlike the old TTY
  		 * implementation, this implementation copies data back
 @@ -192,6 +196,10 @@
  	 */
  
  	for (;;) {
 +		error = tty_wait_background(tp, curthread, SIGTTIN);
 +		if (error)
 +			return (error);
 +
  		error = ttyinq_read_uio(&tp->t_inq, tp, uio,
  		    uio->uio_resid, 0);
  		if (error)
 @@ -229,6 +237,10 @@
  	timevaladd(&end, &now);
  
  	for (;;) {
 +		error = tty_wait_background(tp, curthread, SIGTTIN);
 +		if (error)
 +			return (error);
 +
  		error = ttyinq_read_uio(&tp->t_inq, tp, uio,
  		    uio->uio_resid, 0);
  		if (error)
 @@ -278,6 +290,10 @@
  	 */
  
  	for (;;) {
 +		error = tty_wait_background(tp, curthread, SIGTTIN);
 +		if (error)
 +			return (error);
 +
  		error = ttyinq_read_uio(&tp->t_inq, tp, uio,
  		    uio->uio_resid, 0);
  		if (error)
 Index: sys/sys/tty.h
 ===================================================================
 --- sys/sys/tty.h	(Revision 241962)
 +++ sys/sys/tty.h	(Arbeitskopie)
 @@ -180,6 +180,7 @@
  void	tty_signal_pgrp(struct tty *tp, int signal);
  /* Waking up readers/writers. */
  int	tty_wait(struct tty *tp, struct cv *cv);
 +int	tty_wait_background(struct tty *tp, struct thread *td, int sig);
  int	tty_timedwait(struct tty *tp, struct cv *cv, int timo);
  void	tty_wakeup(struct tty *tp, int flags);
  
 
 --ReaqsoxgOBHFXBhH--

From: Jeremy Chadwick <jdc@koitsu.org>
To: FreeBSD-gnats-submit@FreeBSD.org, freebsd-bugs@FreeBSD.org
Cc:  
Subject: Re: kern/173010: Backgrounded processes remain in ttyin state /
 SIGCONT does not remove sleeping processes from sleep queue
Date: Fri, 26 Oct 2012 01:35:10 -0700

 Another follow-up in the thread -- fix has been committed to HEAD,
 pending 2 weeks for MFC.  This fix **will not** make it into
 9.1-RELEASE, sadly, but will obviously be available in RELENG_9 going
 forward.
 
 
 
 From: Ed Schouten <ed@80386.nl>
 To: Jeremy Chadwick <jdc@koitsu.org>
 Date: Thu, 25 Oct 2012 11:06:09 +0200
 Cc: Konstantin Belousov <kostikbel@gmail.com>, freebsd-stable@freebsd.org, jhb@freebsd.org
 Subject: Re: pty/tty or signal strangeness, or grep/bsdgrep bug?
 
 2012/10/25 Jeremy Chadwick <jdc@koitsu.org>:
 > I assume a commit to HEAD + MFC in 2 weeks is in order?
 
 Yes. We're far too late to get this into 9.1, so I'll MFC it after the release.
 
 Patch committed as r242078!
 
 -- 
 Ed Schouten <ed@80386.nl>
State-Changed-From-To: open->closed 
State-Changed-By: ed 
State-Changed-When: Sun Nov 11 13:37:56 CET 2012 
State-Changed-Why:  
Committed. Thanks! 

http://www.freebsd.org/cgi/query-pr.cgi?pr=173010 
>Unformatted:
