NetBSD Problem Report #25010

Received: (qmail 13941 invoked by uid 605); 1 Apr 2004 14:51:24 -0000
Message-Id: <200404011451.i31EpJWv025274@lyssa.zhadum.de>
Date: Thu, 1 Apr 2004 16:51:19 +0200 (CEST)
From: Matthias Scheler <tron@colwyn.zhadum.de>
Sender: gnats-bugs-owner@NetBSD.org
Reply-To: tron@colwyn.zhadum.de
To: gnats-bugs@gnats.NetBSD.org
Subject: com(4) can get stuck while sending data
X-Send-Pr-Version: 3.95

>Number:         25010
>Category:       kern
>Synopsis:       com(4) can get stuck while sending data
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    kern-bug-people
>State:          closed
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Thu Apr 01 14:52:00 +0000 2004
>Closed-Date:    Mon Apr 05 22:55:34 +0000 2004
>Last-Modified:  Tue Apr 06 08:00:01 +0000 2004
>Originator:     
>Release:        NetBSD 2.0B / 1.6.2_STABLE
>Organization:
Matthias Scheler                                  http://scheler.de/~matthias/
>Environment:
System: NetBSD lyssa.zhadum.de 2.0B NetBSD 2.0B (LYSSA) #0: Wed Mar 31 13:13:28 CEST 2004 tron@lyssa.zhadum.de:/src/sys/compile/LYSSA i386

System: NetBSD colwyn.zhadum.de 1.6.2_STABLE NetBSD 1.6.2_STABLE (COLWYN) #0: Sat Mar 20 14:55:07 CET 2004     tron@colwyn.zhadum.de:/src/sys/compile/COLWYN i386

Architecture: i386
Machine: i386

>Description:
While investigating PR kern/18799 FTP connections on my SLIP link often
hung exactly like Tomasz Luchowski reported in the above problem report.
Surprisingly it also happened with my tun(4) based userland SLIP
implementation which I wrote and succesfully tested years ago. After
extensive debugging I began to suspect a bug in com(4) driver used
for the serial ports of both machines. So I wrote a small test program
which works like this:

1.) In sender mode it keeps sending as much data as it can in random
    block sizes. All incoming data will be read in small blocks.

2.) In receive mode (option "-r") it will read all incoming data in
    small blocks and occasionally send out small data blocks after
    it received some data.

Here are the results:

1.6_2_STABLE com(4) sending to 2.0B com(4):	hangs after a short time
1.6_2_STABLE com(4) sending to 2.0B ucom(4):	hangs after a short time
2.0B com(4) sending to 1.6.2_STABLE com(4):	hangs after a short time
2.0B ucom(4) sending to 1.6.2_STABLE com(4):	works several minutes until
						test stopped

So it looks like the com(4) driver can get stuck on sending after a while.
I've tried with and without hardware handshake but it didn't make a
difference.

>How-To-Repeat:
Compile the following program ...

#include <sys/ioctl.h>
#include <sys/param.h>
#include <sys/termios.h>
#include <sys/time.h>
#include <fcntl.h>
#include <paths.h>
#include <unistd.h>

#include <errno.h>
#include <poll.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

static void
Usage(char *Progname)
{
	(void)fprintf(stderr, 
	    "Usage: %s [-hr] [-s baudrate] [-t timeout] ttyname\n", Progname);
	exit(EXIT_FAILURE);
}

int
main(int argc, char **argv)
{
	char		*progname;
	int		CFlag, Receiver, Speed, Timeout, Opt, TTY;
	int		Index, Sent, Received;
	struct termios	TIO;
	struct pollfd	pfd;
	char		OutBuffer[65536], InBuffer[256];

	progname = argv[0];
	CFlag = HUPCL|CLOCAL;
	Receiver = 0;
	Speed = 9600;
	Timeout = 10;
	while ((Opt = getopt(argc, argv, "hrs:t:")) != -1) {
		switch (Opt) {
		case 'h':
			CFlag |= CRTSCTS;
			break;
		case 'r':
			Receiver = 1;
			break;
		case 's':
			Speed = atoi(optarg);
			break;
		case 't':
			Timeout = atoi(optarg);
			break;
		default:
			Usage(progname);
		}
	}

	argc -= optind;
	argv += optind;
	if (argc != 1)
		Usage(progname);

	if (chdir(_PATH_DEV)) {
		perror(_PATH_DEV);
		return EXIT_FAILURE;
	}

	if ((TTY = open(argv[0], O_RDWR|O_NONBLOCK))<0) {
		perror(argv[0]);
		return EXIT_FAILURE;
	}
	TIO.c_cflag = CREAD|CS8|CFlag;
	TIO.c_iflag = 0;
	TIO.c_lflag = 0;
	TIO.c_oflag = 0;
	TIO.c_cc[VMIN] = 1;
	TIO.c_cc[VTIME] = 0;
	(void) cfsetspeed(&TIO, Speed);
	if (tcsetattr(TTY, TCSADRAIN, &TIO) < 0) {
		perror(argv[1]);
		return EXIT_FAILURE;
	}

	srandom(time(NULL));
	for (Index = 0; Index < sizeof (OutBuffer); Index++)
		OutBuffer[Index] = random();

	pfd.fd = TTY;
	pfd.events = POLLIN|POLLOUT;

	Sent = 0;
	Received = 0;

	for (;;) {
		int 	Num, Size;

		if (Receiver) {
			if (Received > 0 && (random() % 7) == 4)
				pfd.events |= POLLOUT;
			else
				pfd.events &= ~POLLOUT;
		}

		Num = poll(&pfd, 1, Timeout * 1000);
		if (Num < 0) {
			perror("poll");
			break;
		}
		if (Num == 0) {
			(void)printf("No traffic for %d seconds "
			    "after %d bytes received and %d bytes sent\n",
			    Timeout, Received, Sent);
			continue;
		}

		if (pfd.revents & POLLIN) {
			Size = (random() % sizeof (InBuffer)) + 1;
			Num = read(TTY, InBuffer, Size);
			if (Num < 0) {
				perror("read");
				break;
			}
			Received += Num;
		}

		if (pfd.revents & POLLOUT) {
			int	Max, Offset;

			Max = Receiver ? 64 : sizeof (OutBuffer);
			Size = (random() % Max) + 1;
			Offset = (Size == sizeof (OutBuffer)) ?
			    0 : (random() % (sizeof (OutBuffer) - Size));
			Num = write(TTY, &OutBuffer[Offset], Size);
			if (Num < 0) {
				perror("write");
				break;
			}
			Sent += Num;
		}
	}

	return EXIT_FAILURE;
}

... and it like this:

1. machine:	ttytest	-h -s 115200 /dev/tty00
2. machine:	ttytest -r -h -s 115200 /dev/tty00

The problem happens only if the 1. machine is using com(4) as serial driver.
>Fix:
None provided.

>Release-Note:
>Audit-Trail:
State-Changed-From-To: open->feedback 
State-Changed-By: enami 
State-Changed-When: Sat Apr 3 01:26:46 UTC 2004 
State-Changed-Why:  
Problem is analyzed and possible fix is sent to PR reporter and some people 
related to this (com driver and bug). 
# Sorry, but I forgot to edit Subject field when editing reply mail from 
# www output and missed kern/NNNNN, so the reply won't appended this PR 
# and I don't have copy of it. 

From: enami tsugutomo <enami@but-b.or.jp>
To: Matthias Scheler <tron@colwyn.zhadum.de>
Cc: gnats-bugs@gnats.NetBSD.org, mycroft@netbsd.org, briggs@netbsd.org
Subject: Re: kern/25010
Date: Sat,  3 Apr 2004 09:47:52 +0900 (JST)

 > So it looks like the com(4) driver can get stuck on sending after a while.
 > I've tried with and without hardware handshake but it didn't make a
 > difference.

 This is due to the race condition introduced in rev. 1.189.  Before
 this change, the value of `lsr' used in the test below was up to date
 enough, but now it may be out of date if THRE interrupt occurs during
 last LSR read and IIR read.

   	if (ISSET(lsr, LSR_TXRDY)) {

 Following change works for me.

 enami.

 Index: com.c
 ===================================================================
 RCS file: /cvsroot/src/sys/dev/ic/com.c,v
 retrieving revision 1.224
 diff -c -r1.224 com.c
 *** com.c	23 Jan 2004 05:01:19 -0000	1.224
 --- com.c	3 Apr 2004 00:34:25 -0000
 ***************
 *** 2231,2242 ****

   			sc->sc_st_check = 1;
   		}
 ! 	} while (ISSET((iir = bus_space_read_1(iot, ioh, com_iir)), IIR_RXRDY)
 ! 	    || ((iir & IIR_IMASK) == 0));

   	/*
 ! 	 * Done handling any receive interrupts. See if data can be
 ! 	 * transmitted as well. Schedule tx done event if no data left
   	 * and tty was marked busy.
   	 */
   	if (ISSET(lsr, LSR_TXRDY)) {
 --- 2238,2261 ----

   			sc->sc_st_check = 1;
   		}
 ! 	} while (!ISSET((iir =
 ! 	    bus_space_read_1(iot, ioh, com_iir)), IIR_NOPEND) &&
 ! 	    /*
 ! 	     * Since some device (e.g., ST16C1550) doesn't clear IIR_TXRDY
 ! 	     * by IIR read, so we can't do this way: `process all interrupts,
 ! 	     * then do TX if possble'.
 ! 	     */
 ! 	    (iir & IIR_IMASK) != IIR_TXRDY);
 ! 
 ! 	/*
 ! 	 * Read LSR again, since there may be an interrupt between
 ! 	 * the last LSR read and IIR read above.
 ! 	 */
 ! 	lsr = bus_space_read_1(iot, ioh, com_lsr);

   	/*
 ! 	 * See if data can be transmitted as well.
 ! 	 * Schedule tx done event if no data left
   	 * and tty was marked busy.
   	 */
   	if (ISSET(lsr, LSR_TXRDY)) {

From: Allen Briggs <briggs@wasabisystems.com>
To: enami tsugutomo <enami@but-b.or.jp>
Cc: Matthias Scheler <tron@colwyn.zhadum.de>, gnats-bugs@gnats.NetBSD.org,
  mycroft@netbsd.org
Subject: Re: kern/25010
Date: Fri, 2 Apr 2004 22:58:17 -0500

 On Sat, Apr 03, 2004 at 09:47:52AM +0900, enami tsugutomo wrote:
 > This is due to the race condition introduced in rev. 1.189.  Before
 > this change, the value of `lsr' used in the test below was up to date
 > enough, but now it may be out of date if THRE interrupt occurs during
 > last LSR read and IIR read.
 > 
 >   	if (ISSET(lsr, LSR_TXRDY)) {
 > 
 > Following change works for me.

 That looks reasonable to me.  Unfortuantely, I no longer have the
 hardware for which I made the changes in revision 1.189.

 -allen

 -- 
  Allen Briggs                     briggs@wasabisystems.com
  Wasabi Systems, Inc.             http://www.wasabisystems.com/

From: Matthias Scheler <tron@zhadum.de>
To: gnats-bugs@gnats.netbsd.org
Cc: enami@netbsd.org, kern-bug-people@netbsd.org
Subject: Re: kern/25010
Date: Sat, 3 Apr 2004 08:52:47 +0200

 On Sat, Apr 03, 2004 at 01:30:29AM -0000, enami@netbsd.org wrote:
 > Synopsis: com(4) can get stuck while sending data
 > 
 > State-Changed-From-To: open->feedback
 > State-Changed-By: enami
 > State-Changed-When: Sat Apr 3 01:26:46 UTC 2004
 > State-Changed-Why: 
 > Problem is analyzed and possible fix is sent to PR reporter and some people
 > related to this (com driver and bug).
 > # Sorry, but I forgot to edit Subject field when editing reply mail from
 > # www output and missed kern/NNNNN, so the reply won't appended this PR
 > # and I don't have copy of it.

 I don't have the time to test the fix today. But I'l do so tomorrow.

 	Thanks a lot for looking into this

 -- 
 Matthias Scheler                                  http://scheler.de/~matthias/

From: Matthias Scheler <tron@zhadum.de>
To: enami tsugutomo <enami@but-b.or.jp>
Cc: gnats-bugs@gnats.NetBSD.org, mycroft@netbsd.org, briggs@netbsd.org
Subject: Re: kern/25010
Date: Sun, 4 Apr 2004 21:26:30 +0200

 On Sat, Apr 03, 2004 at 09:47:52AM +0900, enami tsugutomo wrote:
 > Following change works for me.

 The test results look very promising so far. Using new patched com(4)
 driver I could not only transfer a 30MB file over my userland SLIP
 without problems but also transfer a 12MB file with "kermit" without
 a single error.

 I'll let my stress run over night and report the result tomorrow.

 	Kind regards

 -- 
 Matthias Scheler                                  http://scheler.de/~matthias/

From: Matthias Scheler <tron@zhadum.de>
To: enami tsugutomo <enami@but-b.or.jp>
Cc: gnats-bugs@gnats.NetBSD.org, mycroft@netbsd.org, briggs@netbsd.org
Subject: Re: kern/25010
Date: Mon, 5 Apr 2004 07:56:28 +0200

 On Sun, Apr 04, 2004 at 09:26:30PM +0200, Matthias Scheler wrote:
 > I'll let my stress run over night and report the result tomorrow.

 My stress test has run for 9 hours without any problems. Please commit
 the fix, close the PR and submit pullup requests for the 1.6 and
 2.0 branch.

 	Thank you very much

 -- 
 Matthias Scheler                                  http://scheler.de/~matthias/
State-Changed-From-To: feedback->open 
State-Changed-By: tron 
State-Changed-When: Mon Apr 5 06:07:46 UTC 2004 
State-Changed-Why:  
I've confirmed that the suggested change fixes the problem. 

From: enami tsugutomo <enami@netbsd.org>
To: gnats-bugs@gnats.NetBSD.org
Cc:  
Subject: pr/25010 CVS commit: src/sys/dev/ic
Date: Mon,  5 Apr 2004 22:33:08 +0000 (UTC)

 Module Name:	src
 Committed By:	enami
 Date:		Mon Apr  5 22:33:08 UTC 2004

 Modified Files:
 	src/sys/dev/ic: com.c

 Log Message:
 Fix race condition introduced in rev 1.189; after the change, if there is
 THRE interrupt occurs between the LSR read and IIR read, we won't see the
 LSR_TXRDY bit when testing it in the variable `lsr' and we don't interrupted
 again (as the corresponding bit in the IIR is cleared by reading, except
 for some broken device).

 Tested by Matthias Scheler and me, reviewed by Allen Briggs.
 Closes PR#25010.


 To generate a diff of this commit:
 cvs rdiff -r1.224 -r1.225 src/sys/dev/ic/com.c

 Please note that diffs are not public domain; they are subject to the
 copyright notices on the relevant files.

State-Changed-From-To: open->closed 
State-Changed-By: enami 
State-Changed-When: Mon Apr 5 22:53:52 UTC 2004 
State-Changed-Why:  
fix is commited to trunk. 

From: James Chacon <jmc@netbsd.org>
To: gnats-bugs@gnats.NetBSD.org
Cc:  
Subject: pr/25010 CVS commit: [netbsd-2-0] src/sys/dev/ic
Date: Tue,  6 Apr 2004 04:01:40 +0000 (UTC)

 Module Name:	src
 Committed By:	jmc
 Date:		Tue Apr  6 04:01:40 UTC 2004

 Modified Files:
 	src/sys/dev/ic [netbsd-2-0]: com.c

 Log Message:
 Pullup rev 1.225 (requested by enami in ticket #70)

 Fix race condition introduced in rev 1.189; after the change, if there is
 THRE interrupt occurs between the LSR read and IIR read, we won't see the
 LSR_TXRDY bit when testing it in the variable 'lsr' and we don't get
 interrupted again (as the corresponding bit in the IIR is cleared by reading,
 except for some broken devices). PR#25010


 To generate a diff of this commit:
 cvs rdiff -r1.224 -r1.224.2.1 src/sys/dev/ic/com.c

 Please note that diffs are not public domain; they are subject to the
 copyright notices on the relevant files.


From: Matthias Scheler <tron@netbsd.org>
To: gnats-bugs@gnats.NetBSD.org
Cc:  
Subject: pr/25010 CVS commit: [netbsd-1-6] src/sys/dev/ic
Date: Tue,  6 Apr 2004 07:59:18 +0000 (UTC)

 Module Name:	src
 Committed By:	tron
 Date:		Tue Apr  6 07:59:18 UTC 2004

 Modified Files:
 	src/sys/dev/ic [netbsd-1-6]: com.c

 Log Message:
 Pull up revision 1.225 (requested by enami in ticket #1658):
 Fix race condition introduced in rev 1.189; after the change, if there is
 THRE interrupt occurs between the LSR read and IIR read, we won't see the
 LSR_TXRDY bit when testing it in the variable `lsr' and we don't interrupted
 again (as the corresponding bit in the IIR is cleared by reading, except
 for some broken device).
 Tested by Matthias Scheler and me, reviewed by Allen Briggs.
 Closes PR#25010.


 To generate a diff of this commit:
 cvs rdiff -r1.196 -r1.196.4.1 src/sys/dev/ic/com.c

 Please note that diffs are not public domain; they are subject to the
 copyright notices on the relevant files.

>Unformatted:

NetBSD Home
NetBSD PR Database Search

(Contact us) $NetBSD: query-full-pr,v 1.51 2026/08/10 02:28:17 riastradh Exp $
$NetBSD: gnats_config.sh,v 1.10 2026/05/13 22:00:09 riastradh Exp $
Copyright © 1994-2026 The NetBSD Foundation, Inc. ALL RIGHTS RESERVED.