From owner-freebsd-hackers@freebsd.org Sat Mar 9 21:35:55 2019 Return-Path: Delivered-To: freebsd-hackers@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id A516A1537F76 for ; Sat, 9 Mar 2019 21:35:55 +0000 (UTC) (envelope-from hps@selasky.org) Received: from mail.turbocat.net (turbocat.net [88.99.82.50]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 3718274233 for ; Sat, 9 Mar 2019 21:35:55 +0000 (UTC) (envelope-from hps@selasky.org) Received: from hps2016.home.selasky.org (unknown [176.74.212.121]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.turbocat.net (Postfix) with ESMTPSA id 4C742260377; Sat, 9 Mar 2019 22:35:52 +0100 (CET) Subject: Re: USB stack getting confused To: Konstantin Belousov , Warner Losh Cc: FreeBSD Hackers , "O'Connor, Daniel" References: <3B29D870-41F9-46AF-B9F3-03106DEC417D@dons.net.au> <20190309152613.GM2492@kib.kiev.ua> <20190309162640.GN2492@kib.kiev.ua> <20190309192330.GO2492@kib.kiev.ua> From: Hans Petter Selasky Message-ID: Date: Sat, 9 Mar 2019 22:35:28 +0100 User-Agent: Mozilla/5.0 (X11; FreeBSD amd64; rv:60.0) Gecko/20100101 Thunderbird/60.4.0 MIME-Version: 1.0 In-Reply-To: <20190309192330.GO2492@kib.kiev.ua> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit X-Rspamd-Queue-Id: 3718274233 X-Spamd-Bar: ------ Authentication-Results: mx1.freebsd.org X-Spamd-Result: default: False [-6.94 / 15.00]; NEURAL_HAM_MEDIUM(-1.00)[-1.000,0]; NEURAL_HAM_SHORT(-0.94)[-0.944,0]; REPLY(-4.00)[]; NEURAL_HAM_LONG(-1.00)[-1.000,0] X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 09 Mar 2019 21:35:55 -0000 On 3/9/19 8:23 PM, Konstantin Belousov wrote: > On Sat, Mar 09, 2019 at 11:41:31AM -0700, Warner Losh wrote: >> >> Is there a form of destroy_dev() that does a revoke on all open instances? >> Eg, this is gone, you can't use it anymore, and all further attempts to use >> the device will generate an error, but in the mean time we destroy the >> device and let the detach routine get on with life. waiting may make sense >> when you are merely unloading the driver (and getting to the detach routine >> that way), but when the device is gone, I've come around to the point of >> view that we should just destroy it w/o waiting for closes and anybody that >> touches it afterwards gets an error and has to cope with the error. But >> even in the unload case, we maybe we shouldn't get to the detach routine >> unless we're forcing and/or the detach routine just returns EBUSY since the >> only one that knows what dev_t's are associated with the device_t is the >> driver itself. > You are asking very basic questions about devfs there. > > destroy_dev(9) waits for two things: > - that all threads left the cdevsw methods for the given device; > - that all cdevpriv destructors finished running. Hi, > To facilitate waking up threads potentially sleeping inside the cdevsw > methods, drivers might implement d_purge method which must weed out sleeping > threads from inside the code in the bound time. USB will purge all callers before calling destroy_dev(). This is not the problem. > After that we return from destroy_dev(9) and guarantee that no new calls > into cdevsw is done for this device. devfs magic consumes the fo_ and > VOP_ calls and does not allow them to reach into the driver. When I designed the current USB devfs it was important to me to keep open() and close() calls balanced to avoid situations where an open call may setup some resource and then close(), which free this resource again, never gets called. destroy_dev(9) makes no such guarantee, and I think that is a failure of destroy_dev(9). That's when I started using the cdev's destructor callback function. > So what usb does there is actively defeating existing mechanism by > keeping internal refcount on opens and refusing to call destroy_dev() > until the count goes to zero The FreeBSD USB stack also is used in environments w/o DEVFS and need own refcounts. > (I did not read the usb code, but I believe > that I am not too wrong). >Would usb core just destroy_dev() when the > physical device goes away, then at worst the existing file descriptors > opened against the lost devices would become dead (not same dead as > terminals after revoke(2), but very similar). Yes, I can do that if destroy_dev() ensures that d_close is called for all open file handles once and only once before it returns. I think this is where the problem comes from. > > If the problem is due to keeping some instance data for the opened device, > then cdevpriv might be the better fit (at least the KPI was designed > to be) than blocking destroy until all users are gone. > The USB stack does not use MMAP, so this is not a problem. --HPS