From patchwork Tue Jan 24 10:47:03 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chris Wilson X-Patchwork-Id: 9534865 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 1F7216042C for ; Tue, 24 Jan 2017 10:47:13 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 1067126AE3 for ; Tue, 24 Jan 2017 10:47:13 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 0454426C2F; Tue, 24 Jan 2017 10:47:13 +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=-4.1 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_MED,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 6A32226AE3 for ; Tue, 24 Jan 2017 10:47:12 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id D0FED6E76C; Tue, 24 Jan 2017 10:47:10 +0000 (UTC) X-Original-To: dri-devel@lists.freedesktop.org Delivered-To: dri-devel@lists.freedesktop.org Received: from mail-lf0-x243.google.com (mail-lf0-x243.google.com [IPv6:2a00:1450:4010:c07::243]) by gabe.freedesktop.org (Postfix) with ESMTPS id 7B8376E76C for ; Tue, 24 Jan 2017 10:47:08 +0000 (UTC) Received: by mail-lf0-x243.google.com with SMTP id h65so16562388lfi.3 for ; Tue, 24 Jan 2017 02:47:08 -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=3po/NYp9FpX/+iPk7KL1DC1BS5TqscIStoWLzgoktus=; b=AEcWO/ZvBigM+yCHwiq/4TkjCoUFENIv3ahMSJf127hSH2goD/S884liQssL6OsxsL P92z1E/8HRj2hc7I84jmSVpEcpiU0NGM+tWvTfAOZIIcbZlG05wmJ5rjTI7yugrQOxuj YoMFjnjxaZ0GN0PnNV2Ym5oMFxDk94jSApFks4Cr5GQgg0lNWgrZICBmtcYNEM/8p2Et JgNeHpfP2Tk1yyvynNrurH/SAtqfH9FtnIT0OxvpTqWl85nB0lNZ7U6H4BzSEU2wEj1x UVaFEIOLB7XdLTKK5qAmW0Az6Sy8lHHhISvMbqpsM/Y06Eme9X7Aok/nu+bCAYc7F3f+ GDpQ== 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=3po/NYp9FpX/+iPk7KL1DC1BS5TqscIStoWLzgoktus=; b=KUU7t1PzZAxQoyuKwt6b2mg68phIUB8eMOjvhlcVrLJQhjc3q4+VxsKzPEj36oj0nK mFmH54Z3kvP7UXDtQzf7CEVPq3J9EwUo+E32EY/20QjYFW7LKaK1fXNimGk4hWzF/JeH bH9NtFjVnYSthgfA1cG40VhPF/Jx0E0yh+ggtU/ZRKBHfFKvy4sUEUgJDRCl4LyE2RuK 3H/bKyPOFfwA4nVSQjPzhM2audqgvVlLRt19ZSfOqSi6sCrSslma7GGDNI3v9uQ+UKXv BoSE9IfkCI1E8PjNlR36Zbhy16h9S9nY919O2sxp09ViChfEttCu+v4KxTiOv+Go51bm pBTA== X-Gm-Message-State: AIkVDXJQq5knFD0OMB08zctvZFl8tTCNfOouzB4iS7KLPkR3aumH0N+EhIaE+TwzQ7JaSQ== X-Received: by 10.223.152.18 with SMTP id v18mr27720662wrb.78.1485254826789; Tue, 24 Jan 2017 02:47:06 -0800 (PST) Received: from haswell.alporthouse.com ([78.156.65.138]) by smtp.gmail.com with ESMTPSA id g75sm25725391wme.5.2017.01.24.02.47.05 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 24 Jan 2017 02:47:05 -0800 (PST) From: Chris Wilson To: dri-devel@lists.freedesktop.org Subject: [PATCH v5] drm: Provide a driver hook for drm_dev_release() Date: Tue, 24 Jan 2017 10:47:03 +0000 Message-Id: <20170124104703.15506-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 , 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 responsibility 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 v4: Tidy language for kerneldoc v5: Cross reference from drm_dev_init() to note that driver->release() allows for arbitrary embedding. Signed-off-by: Chris Wilson Cc: Laurent Pinchart Cc: Daniel Vetter --- drivers/gpu/drm/drm_drv.c | 67 +++++++++++++++++++++++++++++++++-------------- include/drm/drm_drv.h | 13 +++++++++ 2 files changed, 60 insertions(+), 20 deletions(-) diff --git a/drivers/gpu/drm/drm_drv.c b/drivers/gpu/drm/drm_drv.c index 72116978ec06..3db1f9003cc8 100644 --- a/drivers/gpu/drm/drm_drv.c +++ b/drivers/gpu/drm/drm_drv.c @@ -465,7 +465,10 @@ static void drm_fs_inode_free(struct inode *inode) * that do embed &struct drm_device it must be placed first in the overall * structure, and the overall structure must be allocated using kmalloc(): The * drm core's release function unconditionally calls kfree() on the @dev pointer - * when the final reference is released. + * when the final reference is released. To override this behaviour, and so + * allow embedding of the drm_device inside the driver's device struct at an + * arbitrary offset, you must supply a driver->release() callback and control + * the finalization explicitly. * * RETURNS: * 0 on success, or error code on failure. @@ -553,6 +556,43 @@ 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. + * + * 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) +{ + WARN_ON(atomic_read(&dev->ref.refcount)); + + drm_vblank_cleanup(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,25 +638,12 @@ static void drm_dev_release(struct kref *ref) { struct drm_device *dev = container_of(ref, struct drm_device, ref); - drm_vblank_cleanup(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); - 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..3562f5be547e 100644 --- a/include/drm/drm_drv.h +++ b/include/drm/drm_drv.h @@ -103,6 +103,17 @@ 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. Drivers + * using this callback are responsible for calling drm_dev_fini() + * to finalize the device and then freeing the struct themselves. + */ + 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 +462,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);