From owner-freebsd-drivers@FreeBSD.ORG Mon Jul 2 12:29:29 2007 Return-Path: X-Original-To: freebsd-drivers@freebsd.org Delivered-To: freebsd-drivers@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [69.147.83.52]) by hub.freebsd.org (Postfix) with ESMTP id DCAFF16A46C for ; Mon, 2 Jul 2007 12:29:29 +0000 (UTC) (envelope-from daniel@roe.ch) Received: from calvin.roe.ch (calvin.roe.ch [213.144.141.26]) by mx1.freebsd.org (Postfix) with ESMTP id 9A8E513C469 for ; Mon, 2 Jul 2007 12:29:29 +0000 (UTC) (envelope-from daniel@roe.ch) Received: from d212-53-102-185.cust.tele2.ch ([212.53.102.185] helo=dragon.roe.ch) by calvin.roe.ch (envelope-from ) with ESMTPS (TLSv1:AES256-SHA:256) id 1I5KXc-0000o5-Hv for freebsd-drivers@freebsd.org; Mon, 02 Jul 2007 13:58:04 +0200 Received: from roe by dragon.roe.ch (envelope-from ) with LOCAL id 1I5Kax-0002Gq-00 for freebsd-drivers@freebsd.org; Mon, 02 Jul 2007 14:01:31 +0200 Date: Mon, 2 Jul 2007 14:01:31 +0200 From: Daniel Roethlisberger To: freebsd-drivers@freebsd.org Message-ID: <20070702120131.GB5161@dragon.roe.ch> Mail-Followup-To: freebsd-drivers@freebsd.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.5.4i DomainKey-Status: (calvin.roe.ch) DomainKey-Result: (calvin.roe.ch) Subject: cdev destroy_dev() and detach races X-BeenThere: freebsd-drivers@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Writing device drivers for FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 02 Jul 2007 12:29:29 -0000 I have been doing some finishing work on my cmx driver [1] and have run into some difficulties involving detach races. This is a pccard driver with a cdev interface, the device is a PCMCIA based smartcard reader. Originally, I thought destroy_dev() would block until all threads have left the cdevsw handlers. Then I found out that it would only do so if d_purge is set in cdevsw. However, there aren't an awful lot of drivers using this. My attempts to use a d_purge handler resulted in crashes within kern/kern_conf.c when having an endless cat loop running while detaching the device. The solution I am using now is that I sleep in detach() until cdev->si_threadcount is 0. This seems to have done the trick, but I still have to do more testing to be sure. How do other driver solve this problem? I haven't found much documentation on this, and I didn't find any suitable driver to use as a guide (ideally a pccard driver with cdev interface, sleeps within read/write handlers, running without Giant). I'd be grateful if somebody could share any insight on this, or point me to the appropriate documentation. (Or any other comments on the cmx driver code, for that matter.) Thanks -Dan [1] http://dragon.roe.ch/~roe/cmx/ -- Daniel Roethlisberger