From owner-svn-src-head@freebsd.org Mon Jul 8 20:26:58 2019 Return-Path: Delivered-To: svn-src-head@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 8355315E8F24; Mon, 8 Jul 2019 20:26:58 +0000 (UTC) (envelope-from ian@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) server-signature RSA-PSS (4096 bits) client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id D17618AA65; Mon, 8 Jul 2019 20:26:57 +0000 (UTC) (envelope-from ian@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 8F01A1D73A; Mon, 8 Jul 2019 20:26:57 +0000 (UTC) (envelope-from ian@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id x68KQvaY005336; Mon, 8 Jul 2019 20:26:57 GMT (envelope-from ian@FreeBSD.org) Received: (from ian@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id x68KQvAL005334; Mon, 8 Jul 2019 20:26:57 GMT (envelope-from ian@FreeBSD.org) Message-Id: <201907082026.x68KQvAL005334@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: ian set sender to ian@FreeBSD.org using -f From: Ian Lepore Date: Mon, 8 Jul 2019 20:26:57 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r349850 - head/sys/dev/iicbus X-SVN-Group: head X-SVN-Commit-Author: ian X-SVN-Commit-Paths: head/sys/dev/iicbus X-SVN-Commit-Revision: 349850 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Rspamd-Queue-Id: D17618AA65 X-Spamd-Bar: -- Authentication-Results: mx1.freebsd.org X-Spamd-Result: default: False [-2.96 / 15.00]; local_wl_from(0.00)[FreeBSD.org]; NEURAL_HAM_MEDIUM(-1.00)[-1.000,0]; NEURAL_HAM_SHORT(-0.96)[-0.959,0]; NEURAL_HAM_LONG(-1.00)[-1.000,0]; ASN(0.00)[asn:11403, ipnet:2610:1c1:1::/48, country:US] X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: SVN commit messages for the src tree for head/-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 08 Jul 2019 20:26:58 -0000 Author: ian Date: Mon Jul 8 20:26:56 2019 New Revision: 349850 URL: https://svnweb.freebsd.org/changeset/base/349850 Log: Restore the ability for i2c slave devices to do IO from their probe method. r348164 added code to iicbus_request_bus/iicbus_release_bus to automatically call device_busy()/device_unbusy() as part of aquiring exclusive use of the bus (so modules can't be unloaded while the bus is exclusively owned and/or IO is in progress). That broke the ability to do i2c IO from a slave device probe method, because the slave isn't attached yet, so calling device_busy() triggers a sanity-check panic for trying to busy a non-attached device. Now we check whether the device status is < DS_ATTACHING, and if so we busy the iicbus rather than the slave device. I think this leaves a small window where a module could be unloaded while probing is in progress. But I think that's true of all devices, and probably should be fixed by introducing a DS_PROBING state for devices, and handling that at various points in the newbus code. Modified: head/sys/dev/iicbus/iicbus.h head/sys/dev/iicbus/iiconf.c Modified: head/sys/dev/iicbus/iicbus.h ============================================================================== --- head/sys/dev/iicbus/iicbus.h Mon Jul 8 20:20:01 2019 (r349849) +++ head/sys/dev/iicbus/iicbus.h Mon Jul 8 20:26:56 2019 (r349850) @@ -41,6 +41,7 @@ struct iicbus_softc { device_t dev; /* Myself */ device_t owner; /* iicbus owner device structure */ + device_t busydev; /* iicbus_release_bus calls unbusy on this */ u_int owncount; /* iicbus ownership nesting count */ u_char started; /* address of the 'started' slave * 0 if no start condition succeeded */ Modified: head/sys/dev/iicbus/iiconf.c ============================================================================== --- head/sys/dev/iicbus/iiconf.c Mon Jul 8 20:20:01 2019 (r349849) +++ head/sys/dev/iicbus/iiconf.c Mon Jul 8 20:26:56 2019 (r349850) @@ -131,9 +131,15 @@ iicbus_request_bus(device_t bus, device_t dev, int how /* * Mark the device busy while it owns the bus, to * prevent detaching the device, bus, or hardware - * controller, until ownership is relinquished. + * controller, until ownership is relinquished. If the + * device is doing IO from its probe method before + * attaching, it cannot be busied; mark the bus busy. */ - device_busy(dev); + if (device_get_state(dev) < DS_ATTACHING) + sc->busydev = bus; + else + sc->busydev = dev; + device_busy(sc->busydev); /* * Drop the lock around the call to the bus driver, it * should be allowed to sleep in the IIC_WAIT case. @@ -150,7 +156,7 @@ iicbus_request_bus(device_t bus, device_t dev, int how sc->owner = NULL; sc->owncount = 0; wakeup_one(sc); - device_unbusy(dev); + device_unbusy(sc->busydev); } } } @@ -184,7 +190,7 @@ iicbus_release_bus(device_t bus, device_t dev) IICBUS_LOCK(sc); sc->owner = NULL; wakeup_one(sc); - device_unbusy(dev); + device_unbusy(sc->busydev); } IICBUS_UNLOCK(sc); return (0);