From patchwork Sat Jan 21 10:48:15 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chris Wilson X-Patchwork-Id: 9530205 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 723C660431 for ; Sat, 21 Jan 2017 10:48:29 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 6AD632841B for ; Sat, 21 Jan 2017 10:48:29 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 5BF3628613; Sat, 21 Jan 2017 10:48:29 +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 E8DFF2841B for ; Sat, 21 Jan 2017 10:48:28 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id DE9B26E255; Sat, 21 Jan 2017 10:48:26 +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 7489C6E255; Sat, 21 Jan 2017 10:48:25 +0000 (UTC) Received: by mail-wm0-x241.google.com with SMTP id d140so12830874wmd.2; Sat, 21 Jan 2017 02:48:25 -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; bh=hIH5zhVRYYW9trFUirwBaFUGiDMcH3oGIfOkOToMdCU=; b=Bet/a9geJk6SVxNHev03mf7T9A7vPstOaRoXNPhMQDTvlWCn1I8/Ln53ISimi9hhbc 6Ev43a1P5bkIsEGIkxe2DVUBxejT5pCGj5QWtW9TOm3m7bDaYwffQJvys5C8LQ2n5ZAr pqph/cUX11zX0IJIrDmO9WxsxptMuoivdThcrUwKiKCDpFBJl73PTGcv1s58DTqv2asw 9BYFKXCftZtpOMYt353a8fPItarmiHUBaxbeMlNw166N4sOq9xCj+oxjjRUpfwB0XLv0 6krwYnB9RRkMs2HvrxZ8RmNAyLQLx4neqB5ZqKsff1yp4Opv14awgiwxFA1vWO/M2sE1 NKQQ== 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; bh=hIH5zhVRYYW9trFUirwBaFUGiDMcH3oGIfOkOToMdCU=; b=IamY6NqlfW6/FirGYLV5AuNMOEB9GbhDbVrIt2jstR3E5HpZ5Jnsj3iBHnO9hSxHps XY0ZZq7Oh2UyLE/zSdxh+6iu3JwN9ez/Ml5d9RHxZpcx4guah4y12PlXylzQYjnEKo31 ftCrBrkpNTWUQ2XnB4Mpb4hxGM/W7MxvjZ7KjKhqIGQ+3ttyox0IJTF/xrIjFiPnmv0c riw4d3B0FpW0C/MB/ByAMsmAgtAMSHlJ+Gswue4qdaTv5aLwsm0fWMkNOUh7RRgMOald aDISicJUYSIDnq/u21bjGu1/fSlaFGfCCKhNDNixOiO4JlEqvvUbiJKLpfN7I9Vu7iki kWpw== X-Gm-Message-State: AIkVDXKK52sIefWTyjc1rxZbio/RHqDr9w53sD6+luBayB8KY/1EYed+Hfq4Z/WrA6zX6w== X-Received: by 10.28.55.68 with SMTP id e65mr7096635wma.62.1484995703783; Sat, 21 Jan 2017 02:48:23 -0800 (PST) Received: from haswell.alporthouse.com ([78.156.65.138]) by smtp.gmail.com with ESMTPSA id u41sm3869211wrc.1.2017.01.21.02.48.21 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 21 Jan 2017 02:48:22 -0800 (PST) From: Chris Wilson To: dri-devel@lists.freedesktop.org Subject: [PATCH v2] drm: Provide a driver hook for drm_dev_release() Date: Sat, 21 Jan 2017 10:48:15 +0000 Message-Id: <20170121104815.2042-1-chris@chris-wilson.co.uk> X-Mailer: git-send-email 2.11.0 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.) Signed-off-by: Chris Wilson Cc: Laurent Pinchart Cc: Daniel Vetter --- drivers/gpu/drm/drm_drv.c | 56 +++++++++++++++++++++++++++++++++-------------- include/drm/drm_drv.h | 9 ++++++++ 2 files changed, 48 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..dfddd8c15b62 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);