From owner-freebsd-usb@FreeBSD.ORG Thu Jul 5 03:00:10 2007 Return-Path: X-Original-To: freebsd-usb@hub.freebsd.org Delivered-To: freebsd-usb@hub.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [69.147.83.52]) by hub.freebsd.org (Postfix) with ESMTP id C2C4D16A46B for ; Thu, 5 Jul 2007 03:00:10 +0000 (UTC) (envelope-from gnats@FreeBSD.org) Received: from freefall.freebsd.org (freefall.freebsd.org [69.147.83.40]) by mx1.freebsd.org (Postfix) with ESMTP id A3AEF13C45B for ; Thu, 5 Jul 2007 03:00:10 +0000 (UTC) (envelope-from gnats@FreeBSD.org) Received: from freefall.freebsd.org (gnats@localhost [127.0.0.1]) by freefall.freebsd.org (8.13.4/8.13.4) with ESMTP id l6530AdL025081 for ; Thu, 5 Jul 2007 03:00:10 GMT (envelope-from gnats@freefall.freebsd.org) Received: (from gnats@localhost) by freefall.freebsd.org (8.13.4/8.13.4/Submit) id l6530Ao8025080; Thu, 5 Jul 2007 03:00:10 GMT (envelope-from gnats) Resent-Date: Thu, 5 Jul 2007 03:00:10 GMT Resent-Message-Id: <200707050300.l6530Ao8025080@freefall.freebsd.org> Resent-From: FreeBSD-gnats-submit@FreeBSD.org (GNATS Filer) Resent-To: freebsd-usb@FreeBSD.org Resent-Reply-To: FreeBSD-gnats-submit@FreeBSD.org, Victor Liu Received: from mx1.freebsd.org (mx1.freebsd.org [69.147.83.52]) by hub.freebsd.org (Postfix) with ESMTP id 85E1216A421 for ; Thu, 5 Jul 2007 02:55:53 +0000 (UTC) (envelope-from nobody@FreeBSD.org) Received: from www.freebsd.org (www.freebsd.org [69.147.83.33]) by mx1.freebsd.org (Postfix) with ESMTP id 5BE9E13C455 for ; Thu, 5 Jul 2007 02:55:53 +0000 (UTC) (envelope-from nobody@FreeBSD.org) Received: from www.freebsd.org (localhost [127.0.0.1]) by www.freebsd.org (8.13.1/8.13.1) with ESMTP id l652tr5k013837 for ; Thu, 5 Jul 2007 02:55:53 GMT (envelope-from nobody@www.freebsd.org) Received: (from nobody@localhost) by www.freebsd.org (8.13.1/8.13.1/Submit) id l652trbP013836; Thu, 5 Jul 2007 02:55:53 GMT (envelope-from nobody) Message-Id: <200707050255.l652trbP013836@www.freebsd.org> Date: Thu, 5 Jul 2007 02:55:53 GMT From: Victor Liu To: freebsd-gnats-submit@FreeBSD.org X-Send-Pr-Version: www-3.0 Cc: Subject: usb/114310: USB hub attachment panics kernel during libusb device scan X-BeenThere: freebsd-usb@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: FreeBSD support for USB List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 05 Jul 2007 03:00:10 -0000 >Number: 114310 >Category: usb >Synopsis: USB hub attachment panics kernel during libusb device scan >Confidential: no >Severity: critical >Priority: medium >Responsible: freebsd-usb >State: open >Quarter: >Keywords: >Date-Required: >Class: sw-bug >Submitter-Id: current-users >Arrival-Date: Thu Jul 05 03:00:09 GMT 2007 >Closed-Date: >Last-Modified: >Originator: Victor Liu >Release: 5.4-RELEASE >Organization: Fastsoft >Environment: FreeBSD xxxxxxxxxxxxxxxxxxxxxxxxx 5.4-RELEASE FreeBSD 5.4-RELEASE #44: Mon Jul 2 18:25:07 PDT 2007 root@xxxxxxxxxxxxxxxxxxxxxxxxx:/usr/src/sys/i386/compile/xxxxxxxxxxxxx i386 >Description: When attaching a USB hub (anything from a generic DLink to a keyboard with integrated hub) while a userspace libusb query is going on, the kernel panics (trap 12, fault virtual address = 0xdeadc0e6) at usbd_device_fillinfo:1350; p->device is 0xdeadc0de. I have experimented several times, I think the cause is during hub attachment, there is a tsleep when waiting for power to settle (uhub.c:288). In this time, libusb's usb_find_devices happens to request an ioctl for a device exploration. At this point, the port structures of the hub are not yet initialized. I have a temporary fix that just initializes p->device to NULL before the sleep, but this doesn't solve a similar problem exists during hub detachment (which I haven't been able to narrow it down much further). >How-To-Repeat: Run a program that continuously polls for USB devices using libusb's usb_find_devices(), while attaching a USB hub. This won't cause it to crash everytime, but it is likely that out of 20 attachments, there will be at least one panic. A piece of code along the lines of while(1){ DPRINTF(("before usb_init\n")); usb_init(); DPRINTF(("before usb_find_busses\n")); usb_find_busses(); DPRINTF(("before usb_find_devices\n")); usb_find_devices(); } should do the trick of producing something like the log. >Fix: Proposed patch (only a hackish fix for the attachment problem): @@ -284,6 +284,15 @@ goto bad; } + // Fixes crash on hub attachment + // Need to init device to NULL before delay sleep; + // otherwise exploration could hit an uninit'd port + for (p = 0; p < nports; p++) { + struct usbd_port *up = &hub->ports[p]; + up->device = NULL; + } + // end changes + /* Wait with power off for a while. */ usbd_delay_ms(dev, USB_POWER_DOWN_TIME); >Release-Note: >Audit-Trail: >Unformatted: