Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 26 Apr 2011 11:52:27 +0200
From:      Bartosz Fabianowski <freebsd@chillt.de>
To:        freebsd-hackers@freebsd.org
Subject:   Is there some implicit locking of device methods?
Message-ID:  <4DB695DB.1080505@chillt.de>

next in thread | raw e-mail | index | archive | help
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



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?4DB695DB.1080505>