From tbyte@otel.net  Tue Oct 24 21:15:59 2006
Return-Path: <tbyte@otel.net>
Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125])
	by hub.freebsd.org (Postfix) with ESMTP id AA21716A5B0
	for <FreeBSD-gnats-submit@freebsd.org>; Tue, 24 Oct 2006 21:15:59 +0000 (UTC)
	(envelope-from tbyte@otel.net)
Received: from mail.otel.net (gw3.OTEL.net [212.36.8.151])
	by mx1.FreeBSD.org (Postfix) with ESMTP id 3401A43D5D
	for <FreeBSD-gnats-submit@freebsd.org>; Tue, 24 Oct 2006 21:15:58 +0000 (GMT)
	(envelope-from tbyte@otel.net)
Received: from warhead.otel.net ([212.36.8.210])
	by mail.otel.net with esmtp (Exim 4.63 (FreeBSD))
	(envelope-from <tbyte@otel.net>)
	id 1GcTcw-000Be1-Ig
	for FreeBSD-gnats-submit@freebsd.org; Wed, 25 Oct 2006 00:16:03 +0300
Message-Id: <1161724556.33505@WarHeaD.OTEL.net>
Date: Wed, 25 Oct 2006 00:15:56 +0300
From: "Iasen Kostoff" <tbyte@otel.net>
To: "FreeBSD gnats submit" <FreeBSD-gnats-submit@freebsd.org>
Subject: Driver not unset properly after kldunload
X-Send-Pr-Version: gtk-send-pr 0.4.7 
X-GNATS-Notify:

>Number:         104777
>Category:       kern
>Synopsis:       [kernel] [patch] Driver not unset properly after kldunload
>Confidential:   no
>Severity:       non-critical
>Priority:       medium
>Responsible:    jhb
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Tue Oct 24 21:20:23 GMT 2006
>Closed-Date:    Wed Jan 23 18:05:57 UTC 2008
>Last-Modified:  Wed Jan 23 18:05:57 UTC 2008
>Originator:     Iasen Kostoff
>Release:        FreeBSD 6.2-PRERELEASE i386
>Organization:
OTEL.net 
>Environment:


System: FreeBSD 6.2-PRERELEASE #0: Tue Sep 19 21:43:51 EEST 2006
    root@WarHeaD.OTEL.net:/usr/obj/usr/src6/sys/WARHEAD



>Description:



It seems that after I load and then unload a driver its name stays
linked to the device e.g:

nfe0@pci0:10:0: class=0x068000 card=0x81411043 chip=0x005710de rev=0xa3
hdr=0x00

but ofcourse if_nfe is neither compiled in kernel nor is loaded as
module anymore.
  I digged around in kernel and saw this in device_detach():

        if (!(dev->flags & DF_FIXEDCLASS))
                devclass_delete_device(dev->devclass, dev);

        dev->state = DS_NOTPRESENT;
        device_set_driver(dev, NULL);
        device_set_desc(dev, NULL);
        device_sysctl_fini(dev);

I've put some device_printf()s around and then looked at
devclass_delete_device(). It destroys (frees) a lot of the info about
the device and so the device_printf() prints device name as
"unknown" (NULL). That seems to be a problem for atleast
device_set_driver(dev, NULL) - it dosn't unset the driver. I'm not so
sure about the other 2 but I gues it same there. So when I changed the
order of this funcs everything worked fine (atleast it looks like it
worked fine :) I'm not absolutely sure that this won't broke something
else).


>How-To-Repeat:


Load and unload some driver.



>Fix:


--- subr_bus.c.diff begins here ---
--- sys/kern/subr_bus.c.bak	Sun Oct 22 18:42:11 2006
+++ sys/kern/subr_bus.c	Mon Oct 23 00:27:22 2006
@@ -2417,13 +2417,14 @@
 	if (dev->parent)
 		BUS_CHILD_DETACHED(dev->parent, dev);
 
+	device_set_driver(dev, NULL);
+	device_set_desc(dev, NULL);
+	device_sysctl_fini(dev);
+	
 	if (!(dev->flags & DF_FIXEDCLASS))
 		devclass_delete_device(dev->devclass, dev);
 
 	dev->state = DS_NOTPRESENT;
-	device_set_driver(dev, NULL);
-	device_set_desc(dev, NULL);
-	device_sysctl_fini(dev);
 
 	return (0);
 }
--- subr_bus.c.diff ends here ---



>Release-Note:
>Audit-Trail:

From: Iasen Kostov <ikostov@otel.net>
To: bug-followup@FreeBSD.org, tbyte@otel.net
Cc:  
Subject: Re: kern/104777: [kernel] [patch] Driver not unset properly after
	kldunload
Date: Mon, 11 Dec 2006 15:07:19 +0200

 --=-1Fy3i/CbiP2cB5LqrocQ
 Content-Type: text/plain
 Content-Transfer-Encoding: 7bit
 
         Forgive me for being so lazy :). I've analized the problem as I
 should
 have done the first time and I hope I found the solution. First time I
 was missleaded by sys/dev/pci/pci_user.c and the way it sets pd_name[]
 in pci_devinfo struct (You actualy need to use pciconf atleast once
 after loading the driver) ... It's only set when you use PCIOCGETCONF
 ioctl on /dev/pci and it only sets it once (which is not very clever)
 and never unset it. But one can unload the current driver and load
 another (as in if_nfe, if_nve case) or just unload the driver and
 pd_name[] will always show the first driver attached on that device.
         So I hope this new patch is better one.
 
 
 
 --=-1Fy3i/CbiP2cB5LqrocQ
 Content-Disposition: attachment; filename=pci_user.c.diff
 Content-Type: text/x-patch; name=pci_user.c.diff; charset=UTF-8
 Content-Transfer-Encoding: 7bit
 
 --- sys/dev/pci/pci_user.c.old	Thu Dec  7 02:26:12 2006
 +++ sys/dev/pci/pci_user.c	Thu Dec  7 02:52:23 2006
 @@ -303,14 +303,18 @@
  
  			/* Populate pd_name and pd_unit */
  			name = NULL;
 -			if (dinfo->cfg.dev && dinfo->conf.pd_name[0] == '\0')
 +			if (dinfo->cfg.dev) {
  				name = device_get_name(dinfo->cfg.dev);
 -			if (name) {
 -				strncpy(dinfo->conf.pd_name, name,
 -					sizeof(dinfo->conf.pd_name));
 -				dinfo->conf.pd_name[PCI_MAXNAMELEN] = 0;
 -				dinfo->conf.pd_unit =
 -					device_get_unit(dinfo->cfg.dev);
 +				if (name) {
 +					strncpy(dinfo->conf.pd_name, name,
 +						sizeof(dinfo->conf.pd_name));
 +					dinfo->conf.pd_name[PCI_MAXNAMELEN] = 0;
 +					dinfo->conf.pd_unit =
 +						device_get_unit(dinfo->cfg.dev);
 +				} else {
 +					dinfo->conf.pd_name[0] = '\0';
 +					dinfo->conf.pd_unit = 0;
 +				}
  			}
  
  			if ((pattern_buf == NULL) ||
 
 --=-1Fy3i/CbiP2cB5LqrocQ--
 
State-Changed-From-To: open->patched 
State-Changed-By: jhb 
State-Changed-When: Tue Jan 15 21:41:14 UTC 2008 
State-Changed-Why:  
Simplied fix committed to HEAD. 


Responsible-Changed-From-To: freebsd-bugs->jhb 
Responsible-Changed-By: jhb 
Responsible-Changed-When: Tue Jan 15 21:41:14 UTC 2008 
Responsible-Changed-Why:  
Simplied fix committed to HEAD. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=104777 

From: dfilter@FreeBSD.ORG (dfilter service)
To: bug-followup@FreeBSD.org
Cc:  
Subject: Re: kern/104777: commit references a PR
Date: Tue, 15 Jan 2008 21:40:53 +0000 (UTC)

 jhb         2008-01-15 21:40:47 UTC
 
   FreeBSD src repository
 
   Modified files:
     sys/dev/pci          pci_user.c 
   Log:
   Don't cache the new-bus name of a PCI device in the PCI conf structure,
   but reread it from the device_t every time the device list is fetched.
   Previously the device name in pciconf -l would not be updated when a driver
   was unloaded or if a device was detached and attached to a different
   driver.
   
   MFC after:      1 week
   PR:             kern/104777
   Submitted by:   "Iasen Kostoff"  tbyte | otel net
   
   Revision  Changes    Path
   1.26      +4 -1      src/sys/dev/pci/pci_user.c
 _______________________________________________
 cvs-all@freebsd.org mailing list
 http://lists.freebsd.org/mailman/listinfo/cvs-all
 To unsubscribe, send any mail to "cvs-all-unsubscribe@freebsd.org"
 
State-Changed-From-To: patched->closed 
State-Changed-By: jhb 
State-Changed-When: Wed Jan 23 18:05:41 UTC 2008 
State-Changed-Why:  
Fix merged to RELENG_[67]. 

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