Subject: Re: Detaching devices
To: Lennart Augustsson <lennart@augustsson.net>
From: Bill Studenmund <wrstuden@nas.nasa.gov>
List: tech-kern
Date: 06/28/1999 12:05:45
On Mon, 28 Jun 1999, Lennart Augustsson wrote:

> Jason Thorpe wrote:
> 
> > On Sun, 27 Jun 1999 22:33:48 -0700
> >  Lennart Augustsson <lennart@augustsson.net> wrote:
> 
> That was never my plan.  Besides, I have no control over when the softc is
> deallocated.  It happens in config_detach() after the driver's detach routine
> has finished (successfully).  So at this time there had better be no references
> it.

that's what reference counting is for. :-) What you/we need to do is add
foo_addref and foo_delref routines. When all the references have been
deleted, the memory goes away.

> > The PCMCIA code does this in two steps:
> >
> >         (1) Deactivation, at interrupt time, when the card is popped out
> >             of the socket.  This is done with interrupts blocked.
> 
> It can't be done exactly like this for USB, because there is no interrupt
> for disconnect.  There is an interrupt for status change, but to find out
> what happened you need to talk to the hub that detected the change.
> And this requires interrupts...
> But I could still break it into two stages, but I'm not sure it would buy me
> anything.

You don't need an interrupt. It's just that many things, such as pcmcia,
will tell you of a removal with an interrupt. The main thing is that as
soon as you decide whatever is dead, you start the removal process.

Also, you do it in a way so that other processes relying on the device can
react somewhat gracefully.

> >         (2) Then the PCIC interrupt handler schedules the PCIC's thread
> >             to call config_detach() with the child.  config_detach()
> >             does a revoke of all open file descriptors for the device,
> >             and then the softc gets freed eventually.
> 
> Not eventually, but right after device detach returns.  For some devices
> I can imagine that it is quite hairy to to make sure there are no more refernces
> to the softc.  Take a SCSI device, for example.  How do you know exactly what
> the SCSI layer is up to when you're supposed to detach it?
> But I note that the PCMCIA aic driver doesn't do detach. :-(
> Not does the wdc driver.  Isn't this rather bad?

Yes, it's bad. To really work this right, we need reference counting on
devices. Then you'd know when you can free memory.

> Question: Once vdevgone() has been called, is the device then "isolated"?
> I.e. is it guaranteed that none of its methods (open, close, read, write, etc.)
> will be called after this point?
> Is it true for block devices as well?

I believe so. vdevgone does a vgone on all the device nodes for that
device. These vnodes will now no longer be "device" vnodes, and their
vectors will point to deadfs routines.

Take care,

Bill