From owner-svn-src-all@FreeBSD.ORG Fri Aug 10 15:02:50 2012 Return-Path: Delivered-To: svn-src-all@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [69.147.83.52]) by hub.freebsd.org (Postfix) with ESMTP id 90648106566B; Fri, 10 Aug 2012 15:02:50 +0000 (UTC) (envelope-from hselasky@FreeBSD.org) Received: from svn.freebsd.org (svn.freebsd.org [IPv6:2001:4f8:fff6::2c]) by mx1.freebsd.org (Postfix) with ESMTP id 721F78FC12; Fri, 10 Aug 2012 15:02:50 +0000 (UTC) Received: from svn.freebsd.org (localhost [127.0.0.1]) by svn.freebsd.org (8.14.4/8.14.4) with ESMTP id q7AF2o0F046320; Fri, 10 Aug 2012 15:02:50 GMT (envelope-from hselasky@svn.freebsd.org) Received: (from hselasky@localhost) by svn.freebsd.org (8.14.4/8.14.4/Submit) id q7AF2ofC046316; Fri, 10 Aug 2012 15:02:50 GMT (envelope-from hselasky@svn.freebsd.org) Message-Id: <201208101502.q7AF2ofC046316@svn.freebsd.org> From: Hans Petter Selasky Date: Fri, 10 Aug 2012 15:02:50 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org X-SVN-Group: head MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Cc: Subject: svn commit: r239178 - in head/sys: kern sys X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 10 Aug 2012 15:02:50 -0000 Author: hselasky Date: Fri Aug 10 15:02:49 2012 New Revision: 239178 URL: http://svn.freebsd.org/changeset/base/239178 Log: Add new device method to free the automatically allocated softc structure which is returned by device_get_softc(). This method can be used to easily implement softc refcounting. This can be desirable when the softc has memory references which are controlled by userspace handles for example. This solves the problem of blocking the caller of device_detach() for a non-deterministic time. Discussed with: kib, ed MFC after: 2 weeks Modified: head/sys/kern/device_if.m head/sys/kern/subr_bus.c head/sys/sys/bus.h Modified: head/sys/kern/device_if.m ============================================================================== --- head/sys/kern/device_if.m Fri Aug 10 14:51:41 2012 (r239177) +++ head/sys/kern/device_if.m Fri Aug 10 15:02:49 2012 (r239178) @@ -316,3 +316,14 @@ METHOD int resume { METHOD int quiesce { device_t dev; } DEFAULT null_quiesce; + +/** + * @brief Free the device softc + * + * @param _dev device pointer + * @param _softc pointer to softc + */ +METHOD void free_softc { + device_t _dev; + void *_softc; +} DEFAULT device_free_softc; Modified: head/sys/kern/subr_bus.c ============================================================================== --- head/sys/kern/subr_bus.c Fri Aug 10 14:51:41 2012 (r239177) +++ head/sys/kern/subr_bus.c Fri Aug 10 15:02:49 2012 (r239178) @@ -2406,8 +2406,8 @@ device_get_softc(device_t dev) void device_set_softc(device_t dev, void *softc) { - if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC)) - free(dev->softc, M_BUS_SC); + if (dev->softc != NULL && !(dev->flags & DF_EXTERNALSOFTC)) + DEVICE_FREE_SOFTC(dev, dev->softc); dev->softc = softc; if (dev->softc) dev->flags |= DF_EXTERNALSOFTC; @@ -2604,8 +2604,8 @@ device_set_driver(device_t dev, driver_t if (dev->driver == driver) return (0); - if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC)) { - free(dev->softc, M_BUS_SC); + if (dev->softc != NULL && !(dev->flags & DF_EXTERNALSOFTC)) { + DEVICE_FREE_SOFTC(dev, dev->softc); dev->softc = NULL; } device_set_desc(dev, NULL); @@ -4797,3 +4797,13 @@ bus_free_resource(device_t dev, int type return (0); return (bus_release_resource(dev, type, rman_get_rid(r), r)); } + +/* + * The "dev" argument passed to "device_free_softc()" is allowed to be + * NULL, if the device freeing the soft is not available. + */ +void +device_free_softc(device_t dev, void *softc) +{ + free(softc, M_BUS_SC); +} Modified: head/sys/sys/bus.h ============================================================================== --- head/sys/sys/bus.h Fri Aug 10 14:51:41 2012 (r239177) +++ head/sys/sys/bus.h Fri Aug 10 15:02:49 2012 (r239178) @@ -468,6 +468,7 @@ int device_set_unit(device_t dev, int un int device_shutdown(device_t dev); void device_unbusy(device_t dev); void device_verbose(device_t dev); +void device_free_softc(device_t dev, void *softc); /* * Access functions for devclass.