From jh@sirocco.sandstorm.net  Mon May  8 23:24:43 2006
Return-Path: <jh@sirocco.sandstorm.net>
Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125])
	by hub.freebsd.org (Postfix) with ESMTP id 4AD1016A422;
	Mon,  8 May 2006 23:24:43 +0000 (UTC)
	(envelope-from jh@sirocco.sandstorm.net)
Received: from sirocco.sandstorm.net (ip-69-33-111-75.bos.megapath.net [69.33.111.75])
	by mx1.FreeBSD.org (Postfix) with ESMTP id 968F243D5C;
	Mon,  8 May 2006 23:24:37 +0000 (GMT)
	(envelope-from jh@sirocco.sandstorm.net)
Received: from sirocco.sandstorm.net (localhost [127.0.0.1])
	by sirocco.sandstorm.net (8.13.3/8.13.3) with ESMTP id k48NTOQ7011773;
	Mon, 8 May 2006 19:29:24 -0400 (EDT)
	(envelope-from jh@sirocco.sandstorm.net)
Received: (from jh@localhost)
	by sirocco.sandstorm.net (8.13.3/8.13.3/Submit) id k48NTOTI011772;
	Mon, 8 May 2006 19:29:24 -0400 (EDT)
	(envelope-from jh)
Message-Id: <20060508232924.GA10461@sandstorm.net>
Date: Mon, 8 May 2006 19:29:24 -0400
From: john hood <jh@sandstorm.net>
Reply-To: john hood <jh@sandstorm.net>, cgull@glup.org, ni@sandstorm.net
To: FreeBSD-gnats-submit@freebsd.org
Cc: phk@freebsd.org
Subject: bogus "All threads purged from ugen0.1" message

>Number:         97000
>Category:       kern
>Synopsis:       [kernel] [patch] bogus "All threads purged from ugen0.1" message
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    freebsd-bugs
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Mon May 08 23:30:23 GMT 2006
>Closed-Date:    Fri Mar 11 09:41:34 UTC 2011
>Last-Modified:  Fri Mar 11 09:41:34 UTC 2011
>Originator:     john hood
>Release:        FreeBSD 6.0-RELEASE i386
>Organization:
Sandstorm Enterprises, Inc.
>Environment:

FreeBSD 6.0-RELEASE, config file quite similar to GENERIC, minor
Sandstorm hacks to things like securelevel and bpf.

Intel S815EBM1 motherboard, ICH2 southbridge, Keylok USB security dongle.

>Description:

On some devices, when the device instance is torn down, the kernel
prints the message "All threads purged from <device>" message,
regardless of whether there were actually any threads to purge.  For
us, this happens with Keylok security dongles and their pesky binary
library to access the thing; I'm guessing their library causes the
device to reconfigure on the bus.

>How-To-Repeat:

Plug in a USB device that is claimed by the ugen driver.  Plug it in.
Unplug it, and observe kernel log messages.  Google also shows this
happening for some people with USB serial ports.

>Fix:

--- /usr/src/sys/kern/kern_conf.c	Thu Oct  6 11:17:41 2005
+++ kern/kern_conf.c	Thu May  4 19:59:30 2006
@@ -660,14 +660,15 @@
 
 	csw = dev->si_devsw;
 	dev->si_devsw = NULL;	/* already NULL for SI_ALIAS */
-	while (csw != NULL && csw->d_purge != NULL && dev->si_threadcount) {
-		printf("Purging %lu threads from %s\n",
-		    dev->si_threadcount, devtoname(dev));
-		csw->d_purge(dev);
-		msleep(csw, &devmtx, PRIBIO, "devprg", hz/10);
-	}
-	if (csw != NULL && csw->d_purge != NULL)
+	if (csw != NULL && csw->d_purge != NULL && dev->si_threadcount) {
+		while (dev->si_threadcount) {
+			printf("Purging %lu threads from %s\n",
+			       dev->si_threadcount, devtoname(dev));
+			csw->d_purge(dev);
+			msleep(csw, &devmtx, PRIBIO, "devprg", hz/10);
+		}
 		printf("All threads purged from %s\n", devtoname(dev));
+	}
 
 	dev->si_drv1 = 0;
 	dev->si_drv2 = 0;
>Release-Note:
>Audit-Trail:
Adding to audit trail from misfiled PR kern/97000:

Date: Wed, 17 May 2006 08:09:12 +0200
From: "Poul-Henning Kamp" <phk@phk.freebsd.dk>

 In message <20060508232924.GA10461@sandstorm.net>, john hood writes:
 
 >On some devices, when the device instance is torn down, the kernel
 >prints the message "All threads purged from <device>" message,
 >regardless of whether there were actually any threads to purge.  For
 >us, this happens with Keylok security dongles and their pesky binary
 >library to access the thing; I'm guessing their library causes the
 >device to reconfigure on the bus.
 
 Your patch is not correct, csw->d_purge() needs to be called in all
 cases, but I agree that the message has outlived its purpose in
 the zero threads case.
 
 Will fix.
 
 -- 
 Poul-Henning Kamp       | UNIX since Zilog Zeus 3.20
 phk@FreeBSD.ORG         | TCP/IP since RFC 956
 FreeBSD committer       | BSD since 4.3-tahoe    

Adding to audit trail from misfiled PR kern/97000:

Date: Wed, 17 May 2006 15:11:39 -0400
From: john hood <jh@sandstorm.net>

 > Will fix.
 
 Um.  I think you mean something like "d_purge() needs to be called at
 least once, if set, after all threads have exited".  Is that right?
 But your code (and mine) doesn't do that.  If dev->si_threadcount ==
 0, d_purge() doesn't get called at all.
 
 What I'm getting in syslog:
 
 Apr 13 12:15:51 releng kernel: ugen0: BPT Ent Dongle?Dong, rev 1.01/0.00, addr 2
 [...]
 Apr 13 13:40:46 releng kernel: All threads purged from ugen0.1
 Apr 13 13:40:48 releng last message repeated 23 times
 Apr 13 13:50:19 releng kernel: All threads purged from ugen0.1
 Apr 13 13:50:21 releng last message repeated 23 times
 
 I never get "Purging %lu threads from %s".  
 
 Google "all threads purged" and you'll see other people getting this,
 and people getting similar behavior on USB serial devices.
 
 So this may be an actual bug leaking USB pipes and tty structs.
 
 Also, the code calls d_purge(), sleeps, and then tests
 dev->si_threadcount.
 
   --jh

Adding to audit trail from misfiled PR kern/97000:

Date: Wed, 17 May 2006 21:19:59 +0200
From: "Poul-Henning Kamp" <phk@phk.freebsd.dk>

 >Um.  I think you mean something like "d_purge() needs to be called at
 >least once, if set, after all threads have exited".  Is that right?
 
 I actually have started to wonder what the correct behaviour should
 be now that I looked at it, but if nothing else, you should not get
 the printfs unless there is a bug now.
 
 -- 
 Poul-Henning Kamp       | UNIX since Zilog Zeus 3.20
 phk@FreeBSD.ORG         | TCP/IP since RFC 956
 FreeBSD committer       | BSD since 4.3-tahoe    
 Never attribute to malice what can adequately be explained by incompetence.

From: Brad Huntting <huntting@glarp.com>
To: bug-followup@freebsd.org, jh@sandstorm.net
Cc:  
Subject: Re: kern/97000: [kernel] [patch] bogus "All threads purged from ugen0.1" message
Date: Sat, 1 Jul 2006 16:43:06 -0600

 Note that one of the more nasty side effects of the "All threads purged from 
 ugen0.x" is the fact that the device nodes get remade with default owner and 
 permissions.
 
 This results in, many otherwise mild mannered applications having to be run as 
 root for no good reason.
 
 
 brad

From: John Hood <jh@sandstorm.net>
To: Brad Huntting <huntting@glarp.com>
Cc: bug-followup@freebsd.org
Subject: Re: kern/97000: [kernel] [patch] bogus "All threads purged from ugen0.1" message
Date: Sun, 2 Jul 2006 20:38:20 -0400

 On Sat, Jul 01, 2006 at 04:43:06PM -0600, Brad Huntting wrote:
 > Note that one of the more nasty side effects of the "All threads purged from 
 > ugen0.x" is the fact that the device nodes get remade with default owner and 
 > permissions.
 
 Um, no.  Both of these are side effects of the devices going away and
 reappearing.
 
 > This results in, many otherwise mild mannered applications having to be run as 
 > root for no good reason.
 
 See devfs.rules.
 
   --jh

From: Andrew Muhametshin <andrew@dobrohot.org>
To: bug-followup@FreeBSD.org, jh@sandstorm.net
Cc:  
Subject: Re: kern/97000: [kernel] [patch] bogus "All threads purged from ugen0.1"
 message
Date: Fri, 30 Mar 2007 06:00:48 +0400

 I have a similar problem... Very often, scanning terminates in lag of 
 the program "sane" and a conclusion of messages:
 
 ===========[start]=====================
 ugen0: at uhub4 port 1 (addr 2) disconnected
 All threads purged from ugen0.2
 All threads purged from ugen0.1
 All threads purged from ugen0
 ugen0: detached
 ===========[stop]=====================
 
 Whether will solve this problem a patch offered by *john hood*? Or in 
 last versions FreeBSD(version 6.2-RELEASE and above) it have already 
 corrected?
 
 =========================================
 uname -ra
 FreeBSD host01 6.2-PRERELEASE FreeBSD 6.2-PRERELEASE #1: Sat Jan 13 
 05:24:24 MSK 2007
 
 $ dmesg...
 ehci0: <VIA VT6202 USB 2.0 controller> mem 0xdf040000-0xdf0400ff irq 5 
 at device 16.2 on pci0
 ehci0: [GIANT-LOCKED]
 usb4: EHCI version 0.95
 usb4: companion controllers, 2 ports each: usb2 usb3
 usb4: <VIA VT6202 USB 2.0 controller> on ehci0
 usb4: USB revision 2.0
 uhub4: VIA EHCI root hub, class 9/0, rev 2.00/1.00, addr 1
 uhub4: 4 ports with 4 removable, self powered
 ugen0: EPSON EPSON Scanner, rev 2.00/1.00, addr 2
 
 

From: John Hood <jh@sandstorm.net>
To: Andrew Muhametshin <andrew@dobrohot.org>
Cc: bug-followup@FreeBSD.org
Subject: Re: kern/97000: [kernel] [patch] bogus "All threads purged from ugen0.1" message
Date: Fri, 30 Mar 2007 13:58:28 -0400

 On Fri, Mar 30, 2007 at 06:00:48AM +0400, Andrew Muhametshin wrote:
 > I have a similar problem... Very often, scanning terminates in lag of 
 > the program "sane" and a conclusion of messages:
 > 
 > ===========[start]=====================
 > ugen0: at uhub4 port 1 (addr 2) disconnected
 > All threads purged from ugen0.2
 > All threads purged from ugen0.1
 > All threads purged from ugen0
 > ugen0: detached
 > ===========[stop]=====================
 > 
 > Whether will solve this problem a patch offered by *john hood*? Or in 
 > last versions FreeBSD(version 6.2-RELEASE and above) it have already 
 > corrected?
 
 A similar patch is already in HEAD (src/sys/kern/kern_conf.c rev
 1.198), but hasn't been merged into any of the branches.  This only
 fixes the overly noisy printf, though, and probably does not fix your
 other problems.
 
   --jh
State-Changed-From-To: open->feedback 
State-Changed-By: jh 
State-Changed-When: Mon Feb 21 15:25:08 UTC 2011 
State-Changed-Why:  
Is this still a problem for you? 

http://www.freebsd.org/cgi/query-pr.cgi?pr=97000 
State-Changed-From-To: feedback->closed 
State-Changed-By: jh 
State-Changed-When: Fri Mar 11 09:39:34 UTC 2011 
State-Changed-Why:  
On 2011-03-10, John Hood wrote: 
> The problem went away some time ago, probably with 7.x. 

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