From patchwork Sat Jan 21 10:58:25 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chris Wilson X-Patchwork-Id: 9530211 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 0141C601AE for ; Sat, 21 Jan 2017 10:58:32 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id E234B28562 for ; Sat, 21 Jan 2017 10:58:32 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id C448928613; Sat, 21 Jan 2017 10:58:32 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-3.6 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_MED,RCVD_IN_SORBS_SPAM,T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 4A16128562 for ; Sat, 21 Jan 2017 10:58:31 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 18E456E259; Sat, 21 Jan 2017 10:58:30 +0000 (UTC) X-Original-To: dri-devel@lists.freedesktop.org Delivered-To: dri-devel@lists.freedesktop.org Received: from mail-wm0-x241.google.com (mail-wm0-x241.google.com [IPv6:2a00:1450:400c:c09::241]) by gabe.freedesktop.org (Postfix) with ESMTPS id 1926E6E259; Sat, 21 Jan 2017 10:58:29 +0000 (UTC) Received: by mail-wm0-x241.google.com with SMTP id r144so12873763wme.0; Sat, 21 Jan 2017 02:58:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references; bh=jPvTQN/tgf1nKa7qflX01OA0xaTbc7MMvpFfsKdfg7M=; b=mF8P1Zh4PygnWKxUXrATHgiysovzhBb+nl/qEukoQ1NHLOTT5wuU/G4JYYJIUVN08v QS4MTct8mYSWg1hWMI5pKWXejSyLUfC2Ne0k0qzTGBPadoOG3Qnu+vSSZQ6z8KImdUWY S32wqKnn45fvBYJjnNAdCWoNxdKFGADOYdcxisCslo+luqliqprei9vxWk0dp75uvob2 kaUo4r5Xo3bcLkFTzLddKWPAn2D9QfAZp96HRd7I+DLd11+5lkU1RBqkVtv5EPIpwLT/ qNPMnfdYUqG59uZFSAeL5I8IXbgJGmBW9GO4saXm1a1MMcyZhnkW6zZms1xzNsTC8aHM u1ZA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references; bh=jPvTQN/tgf1nKa7qflX01OA0xaTbc7MMvpFfsKdfg7M=; b=ramtLa/zEiaMLaLsQGMWLrIvROVWyUcZuccGauJlQkEucgXtuc+uQIHCcMjRFKVzcE ZHyvTHKFrru9gpVF0jEZ73HNDg6hyXUwWA8RsHWFrO7xLdKBUCxktfBfFy4On491gIqC J6m/tZwWAbtkWiFdz9sNt8pbXPvnCfUomkersAoE/04yO2QqyP0I1JuqEbqzK4oyPeX9 fUgkcYl4EFYmnby6oySQR5ZB8NljOdhIq8H1/6p4wNUR83hiI5ch10SXnuaDtQhwRP7V plLyW7GFpO/GiDpMkS5ezrk0Zukmqty41yyPI4bVBZvoN3IZzF3LkO2i3XuUmOiO83/b JE2g== X-Gm-Message-State: AIkVDXKUaYyEBiXayfREEXduqfo1ccMy6ocH0B/J2A8+l+wfgiki5b9pC1YpEqVsWhdpsw== X-Received: by 10.223.148.230 with SMTP id 93mr14028582wrr.13.1484996307637; Sat, 21 Jan 2017 02:58:27 -0800 (PST) Received: from haswell.alporthouse.com ([78.156.65.138]) by smtp.gmail.com with ESMTPSA id v102sm3917555wrb.11.2017.01.21.02.58.26 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 21 Jan 2017 02:58:26 -0800 (PST) From: Chris Wilson To: dri-devel@lists.freedesktop.org Subject: [PATCH v3] drm: Provide a driver hook for drm_dev_release() Date: Sat, 21 Jan 2017 10:58:25 +0000 Message-Id: <20170121105825.27661-1-chris@chris-wilson.co.uk> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20170121104815.2042-1-chris@chris-wilson.co.uk> References: <20170121104815.2042-1-chris@chris-wilson.co.uk> Cc: Daniel Vetter , intel-gfx@lists.freedesktop.org, Laurent Pinchart X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" X-Virus-Scanned: ClamAV using ClamSMTP Some state is coupled into the device lifetime outside of the load/unload timeframe and requires teardown during final unreference from drm_dev_release(). For example, dmabufs hold both a device and module reference and may live longer than expected (i.e. the current pattern of the driver tearing down its state and then releasing a reference to the drm device) and yet touch driver private state when destroyed. v2: Export drm_dev_fini() and move the responsible for finalizing the drm_device and freeing it to the release callback. (If no callback is provided, the core will call drm_dev_fini() and kfree(dev) as before.) v3: Remember to add drm_dev_fini() to drm_drv.h Signed-off-by: Chris Wilson Cc: Laurent Pinchart Cc: Daniel Vetter --- drivers/gpu/drm/drm_drv.c | 56 +++++++++++++++++++++++++++++++++-------------- include/drm/drm_drv.h | 11 ++++++++++ 2 files changed, 50 insertions(+), 17 deletions(-) diff --git a/drivers/gpu/drm/drm_drv.c b/drivers/gpu/drm/drm_drv.c index 1b11ab628da7..517718e4f6e4 100644 --- a/drivers/gpu/drm/drm_drv.c +++ b/drivers/gpu/drm/drm_drv.c @@ -553,6 +553,39 @@ int drm_dev_init(struct drm_device *dev, EXPORT_SYMBOL(drm_dev_init); /** + * drm_dev_fini - Finalize a dead DRM device + * @dev: DRM device + * + * Finalize a dead DRM device. This is the converse to drm_dev_init() and + * frees up all state allocated by it. All driver state should be finalized + * first. Note that this function does not free the @dev, that is left to the + * caller. drm_dev_fini() should only + * + * The ref-count of @dev must be zero, and drm_dev_fini() should only be called + * from a drm_driver->release() callback. + */ +void drm_dev_fini(struct drm_device *dev) +{ + if (drm_core_check_feature(dev, DRIVER_GEM)) + drm_gem_destroy(dev); + + drm_legacy_ctxbitmap_cleanup(dev); + drm_ht_remove(&dev->map_hash); + drm_fs_inode_free(dev->anon_inode); + + drm_minor_free(dev, DRM_MINOR_PRIMARY); + drm_minor_free(dev, DRM_MINOR_RENDER); + drm_minor_free(dev, DRM_MINOR_CONTROL); + + mutex_destroy(&dev->master_mutex); + mutex_destroy(&dev->ctxlist_mutex); + mutex_destroy(&dev->filelist_mutex); + mutex_destroy(&dev->struct_mutex); + kfree(dev->unique); +} +EXPORT_SYMBOL(drm_dev_fini); + +/** * drm_dev_alloc - Allocate new DRM device * @driver: DRM driver to allocate device for * @parent: Parent device object @@ -598,23 +631,12 @@ static void drm_dev_release(struct kref *ref) { struct drm_device *dev = container_of(ref, struct drm_device, ref); - if (drm_core_check_feature(dev, DRIVER_GEM)) - drm_gem_destroy(dev); - - drm_legacy_ctxbitmap_cleanup(dev); - drm_ht_remove(&dev->map_hash); - drm_fs_inode_free(dev->anon_inode); - - drm_minor_free(dev, DRM_MINOR_PRIMARY); - drm_minor_free(dev, DRM_MINOR_RENDER); - drm_minor_free(dev, DRM_MINOR_CONTROL); - - mutex_destroy(&dev->master_mutex); - mutex_destroy(&dev->ctxlist_mutex); - mutex_destroy(&dev->filelist_mutex); - mutex_destroy(&dev->struct_mutex); - kfree(dev->unique); - kfree(dev); + if (dev->driver->release) { + dev->driver->release(dev); + } else { + drm_dev_fini(dev); + kfree(dev); + } } /** diff --git a/include/drm/drm_drv.h b/include/drm/drm_drv.h index 34ece393c639..bc5865ad0bbd 100644 --- a/include/drm/drm_drv.h +++ b/include/drm/drm_drv.h @@ -103,6 +103,15 @@ struct drm_driver { * */ void (*unload) (struct drm_device *); + + /** + * @release: + * + * Optional callback for destroying device state after the final + * reference is released, i.e. the device is being destroyed. + */ + void (*release) (struct drm_device *); + int (*dma_ioctl) (struct drm_device *dev, void *data, struct drm_file *file_priv); int (*dma_quiescent) (struct drm_device *); int (*context_dtor) (struct drm_device *dev, int context); @@ -451,6 +460,8 @@ extern unsigned int drm_debug; int drm_dev_init(struct drm_device *dev, struct drm_driver *driver, struct device *parent); +void drm_dev_fini(struct drm_device *dev); + struct drm_device *drm_dev_alloc(struct drm_driver *driver, struct device *parent); int drm_dev_register(struct drm_device *dev, unsigned long flags);