Is there some implicit locking of device methods?
Bartosz Fabianowski
freebsd at chillt.de
Tue Apr 26 10:11:39 UTC 2011
Hi list
I am trying to move a device driver out from under Giant on 8-STABLE.
The driver has the usual probe/attach/detach and
open/close/read/ioctl/poll/purge methods. So far, all were protected by
each other by Giant. With that disabled, I am wondering whether I need
to guard against scenarios like the following:
1. attach() is running and executes make_dev(). Before attach() has
finished, someone calls open() on the newly created device node and
tries to read from a device that is not fully instantiated.
2. read() is running when the device is suddenly pulled (this is a USB
device) so that detach() gets run. Thus, detach() starts tearing down
data structures that read() may still be accessing.
3. attach() is running when the device is pulled again, triggering
detach(). Now, attach() and detach() are running concurrently, the first
one initializing data structures and the second one tearing them down again.
Obviously, I can avoid races under these conditions by protecting each
of the above functions with a mutex. What puzzles is me is that no other
device seems to be doing this. There never is a mutex involved in any
attach(), detach(), open() methods... Is there some kind of implicit
locking going on that I am not aware of? Are DEVMETHODs automatically
protected from each other and the world? Are methods referenced by a
struct cdevsw similarly protected from each other somehow?
- Bartosz
More information about the freebsd-hackers
mailing list