From owner-freebsd-hackers@FreeBSD.ORG Tue Apr 26 10:11:39 2011 Return-Path: Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 1718E106566C for ; Tue, 26 Apr 2011 10:11:39 +0000 (UTC) (envelope-from freebsd@chillt.de) Received: from dd16434.kasserver.com (dd16434.kasserver.com [85.13.137.111]) by mx1.freebsd.org (Postfix) with ESMTP id D3AAC8FC08 for ; Tue, 26 Apr 2011 10:11:38 +0000 (UTC) Received: from taiko.lan (ppp-197-43.21-151.libero.it [151.21.43.197]) by dd16434.kasserver.com (Postfix) with ESMTPSA id 07440188606E for ; Tue, 26 Apr 2011 11:53:16 +0200 (CEST) Message-ID: <4DB695DB.1080505@chillt.de> Date: Tue, 26 Apr 2011 11:52:27 +0200 From: Bartosz Fabianowski User-Agent: Mozilla/5.0 (X11; U; FreeBSD amd64; en-US; rv:1.9.2.15) Gecko/20110309 Thunderbird/3.1.9 MIME-Version: 1.0 To: freebsd-hackers@freebsd.org Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Subject: Is there some implicit locking of device methods? X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 26 Apr 2011 10:11:39 -0000 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