From patchwork Fri Feb 21 21:02:54 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Daniel Vetter X-Patchwork-Id: 11397439 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 1DB4C17EF for ; Fri, 21 Feb 2020 21:04:44 +0000 (UTC) Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id F02D7207FD for ; Fri, 21 Feb 2020 21:04:43 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (1024-bit key) header.d=ffwll.ch header.i=@ffwll.ch header.b="V9Ly+kIE" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org F02D7207FD Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=ffwll.ch Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=dri-devel-bounces@lists.freedesktop.org Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 201136F513; Fri, 21 Feb 2020 21:04:23 +0000 (UTC) X-Original-To: dri-devel@lists.freedesktop.org Delivered-To: dri-devel@lists.freedesktop.org Received: from mail-wr1-x443.google.com (mail-wr1-x443.google.com [IPv6:2a00:1450:4864:20::443]) by gabe.freedesktop.org (Postfix) with ESMTPS id 3EACF6F538 for ; Fri, 21 Feb 2020 21:04:04 +0000 (UTC) Received: by mail-wr1-x443.google.com with SMTP id t3so3518810wru.7 for ; Fri, 21 Feb 2020 13:04:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ffwll.ch; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=nmbhQI3Blr+oj9m5rWSJiAkpTlxKRUfOgQLXYm7KJgA=; b=V9Ly+kIE+lp/PcH1yThxTzN9Ixp8P/x5FHzIn28p0wSAwK5sOVmzvbnRrr1ni5WfOW TgVqmY0cZV6IiSL/jrj6czYZoxGB6XExzOv49oAoJPiDP6UlX3j8oZzTH0NhQg2sufvY /G751TlaHqyZ+tt85OUOBSviyfYbt9VW9UMkI= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=nmbhQI3Blr+oj9m5rWSJiAkpTlxKRUfOgQLXYm7KJgA=; b=XQP6IeHLJ6cQgw/Ayb5OOhv/IQArnRZ+pG1dYKxcZV37/pXRluaeaOAqYO78Q6zkDU AblT44L3Wjkc0j+SnlfO4Wd68oyO+TKJPOdg1rY1pj0hWcmheZ8jvZHT48p4H6FaNaMt AM6kZCW4Ezd0qTKAus10OhfwMTXFIXKOCxuCq4dptkiqScna60/qXh3oBCaAnrTIg35v pmPvYQX7g7GZn8ZvnkWR3IdMpbKz9jNKrp8zlj5D5UsFi1E6zXx9xipZxPH/UwxeJ+r9 lgCicPQx8b/MdJzKp5ogg/U4b3YLjXpe+skCI84Dh+KHjWKdPRdKk80W/R3NQj1kfNVu ZjzA== X-Gm-Message-State: APjAAAVigenb5gj1RpSWAycYc8O3z5VxtL2yGUtTeSlv0qQTlBjowvB2 NCfRardADrJOG744hb8RwJZb8o9V7lk= X-Google-Smtp-Source: APXvYqwklmSLrHz2JIEGX9uUs/ZmE9Kmf68Zq8B05IRTuAI7wS7/OOGfXl374bfGj3IUaKDrn6aPuQ== X-Received: by 2002:adf:f310:: with SMTP id i16mr51954943wro.326.1582319042290; Fri, 21 Feb 2020 13:04:02 -0800 (PST) Received: from phenom.ffwll.local ([2a02:168:57f4:0:efd0:b9e5:5ae6:c2fa]) by smtp.gmail.com with ESMTPSA id z6sm5483930wrw.36.2020.02.21.13.04.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 21 Feb 2020 13:04:01 -0800 (PST) From: Daniel Vetter To: DRI Development Subject: [PATCH 26/51] drm: Manage drm_mode_config_init with drmm_ Date: Fri, 21 Feb 2020 22:02:54 +0100 Message-Id: <20200221210319.2245170-27-daniel.vetter@ffwll.ch> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20200221210319.2245170-1-daniel.vetter@ffwll.ch> References: <20200221210319.2245170-1-daniel.vetter@ffwll.ch> MIME-Version: 1.0 X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Daniel Vetter , Intel Graphics Development , Laurent Pinchart , Thomas Zimmermann , Daniel Vetter , Sam Ravnborg Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" drm_mode_config_cleanup is idempotent, so no harm in calling this twice. This allows us to gradually switch drivers over by removing explicit drm_mode_config_cleanup calls. With this step it's not also possible that (at least for simple drivers) automatic resource cleanup can be done correctly without a drm_driver->release hook. Therefore allow this now in devm_drm_dev_init(). Also with drmm_ explicit drm_driver->release hooks are kinda not the best option, so deprecate that hook to discourage future users. v2: Fixup the example in the kerneldoc too. v3: - For paranoia, double check that minor->dev == dev in the release hook, because I botched the pointer math in the drmm library. - Call drm_mode_config_cleanup when drmm_add_action fails, we'd be missing some mutex_destroy and ida_cleanup otherwise (Laurent) v4: Add a drmm_add_action_or_reset (like devm_ has) to encapsulate this pattern (Noralf). Cc: Laurent Pinchart Cc: "Noralf Trønnes" Cc: Sam Ravnborg Cc: Thomas Zimmermann Signed-off-by: Daniel Vetter Acked-by: Noralf Trønnes --- drivers/gpu/drm/drm_drv.c | 23 +++++++---------------- drivers/gpu/drm/drm_managed.c | 14 ++++++++++++++ drivers/gpu/drm/drm_mode_config.c | 13 ++++++++++++- include/drm/drm_managed.h | 7 +++++++ include/drm/drm_mode_config.h | 2 +- 5 files changed, 41 insertions(+), 18 deletions(-) diff --git a/drivers/gpu/drm/drm_drv.c b/drivers/gpu/drm/drm_drv.c index 3cf40864d4a6..bb326b9bcde0 100644 --- a/drivers/gpu/drm/drm_drv.c +++ b/drivers/gpu/drm/drm_drv.c @@ -98,6 +98,8 @@ static void drm_minor_alloc_release(struct drm_device *dev, void *data) struct drm_minor *minor = data; unsigned long flags; + WARN_ON(dev != minor->dev); + put_device(minor->kdev); spin_lock_irqsave(&drm_minor_lock, flags); @@ -267,8 +269,7 @@ void drm_minor_release(struct drm_minor *minor) * * The following example shows a typical structure of a DRM display driver. * The example focus on the probe() function and the other functions that is - * almost always present and serves as a demonstration of devm_drm_dev_init() - * usage with its accompanying drm_driver->release callback. + * almost always present and serves as a demonstration of devm_drm_dev_init(). * * .. code-block:: c * @@ -278,16 +279,8 @@ void drm_minor_release(struct drm_minor *minor) * struct clk *pclk; * }; * - * static void driver_drm_release(struct drm_device *drm) - * { - * struct driver_device *priv = container_of(...); - * - * drm_mode_config_cleanup(drm); - * } - * * static struct drm_driver driver_drm_driver = { * [...] - * .release = driver_drm_release, * }; * * static int driver_probe(struct platform_device *pdev) @@ -312,7 +305,9 @@ void drm_minor_release(struct drm_minor *minor) * } * drmm_add_final_kfree(drm, priv); * - * drm_mode_config_init(drm); + * ret = drm_mode_config_init(drm); + * if (ret) + * return ret; * * priv->userspace_facing = drmm_kzalloc(..., GFP_KERNEL); * if (!priv->userspace_facing) @@ -710,8 +705,7 @@ static void devm_drm_dev_init_release(void *data) * @driver: DRM driver * * Managed drm_dev_init(). The DRM device initialized with this function is - * automatically put on driver detach using drm_dev_put(). You must supply a - * &drm_driver.release callback to control the finalization explicitly. + * automatically put on driver detach using drm_dev_put(). * * RETURNS: * 0 on success, or error code on failure. @@ -722,9 +716,6 @@ int devm_drm_dev_init(struct device *parent, { int ret; - if (WARN_ON(!driver->release)) - return -EINVAL; - ret = drm_dev_init(dev, driver, parent); if (ret) return ret; diff --git a/drivers/gpu/drm/drm_managed.c b/drivers/gpu/drm/drm_managed.c index 626656369f0b..6376be01bbc8 100644 --- a/drivers/gpu/drm/drm_managed.c +++ b/drivers/gpu/drm/drm_managed.c @@ -134,6 +134,20 @@ int __drmm_add_action(struct drm_device *dev, } EXPORT_SYMBOL(__drmm_add_action); +int __drmm_add_action_or_reset(struct drm_device *dev, + drmres_release_t action, + void *data, const char *name) +{ + int ret; + + ret = __drmm_add_action(dev, action, data, name); + if (ret) + action(dev, data); + + return ret; +} +EXPORT_SYMBOL(__drmm_add_action_or_reset); + void drmm_remove_action(struct drm_device *dev, drmres_release_t action, void *data) diff --git a/drivers/gpu/drm/drm_mode_config.c b/drivers/gpu/drm/drm_mode_config.c index 08e6eff6a179..6f7005bc597f 100644 --- a/drivers/gpu/drm/drm_mode_config.c +++ b/drivers/gpu/drm/drm_mode_config.c @@ -25,6 +25,7 @@ #include #include #include +#include #include #include #include @@ -373,6 +374,11 @@ static int drm_mode_create_standard_properties(struct drm_device *dev) return 0; } +static void drm_mode_config_init_release(struct drm_device *dev, void *ptr) +{ + drm_mode_config_cleanup(dev); +} + /** * drm_mode_config_init - initialize DRM mode_configuration structure * @dev: DRM device @@ -384,8 +390,10 @@ static int drm_mode_create_standard_properties(struct drm_device *dev) * problem, since this should happen single threaded at init time. It is the * driver's problem to ensure this guarantee. * + * Cleanup is automatically handled through registering drm_mode_config_cleanup + * with drmm_add_action(). */ -void drm_mode_config_init(struct drm_device *dev) +int drm_mode_config_init(struct drm_device *dev) { mutex_init(&dev->mode_config.mutex); drm_modeset_lock_init(&dev->mode_config.connection_mutex); @@ -443,6 +451,9 @@ void drm_mode_config_init(struct drm_device *dev) drm_modeset_acquire_fini(&modeset_ctx); dma_resv_fini(&resv); } + + return drmm_add_action_or_reset(dev, drm_mode_config_init_release, + NULL); } EXPORT_SYMBOL(drm_mode_config_init); diff --git a/include/drm/drm_managed.h b/include/drm/drm_managed.h index 2b1ba2ad5582..684f884b6cea 100644 --- a/include/drm/drm_managed.h +++ b/include/drm/drm_managed.h @@ -18,6 +18,13 @@ int __must_check __drmm_add_action(struct drm_device *dev, drmres_release_t action, void *data, const char *name); +#define drmm_add_action_or_reset(dev, action, data) \ + __drmm_add_action(dev, action, data, #action) + +int __must_check __drmm_add_action_or_reset(struct drm_device *dev, + drmres_release_t action, + void *data, const char *name); + void drmm_remove_action(struct drm_device *dev, drmres_release_t action, void *data); diff --git a/include/drm/drm_mode_config.h b/include/drm/drm_mode_config.h index 3bcbe30339f0..160a3e4b51c3 100644 --- a/include/drm/drm_mode_config.h +++ b/include/drm/drm_mode_config.h @@ -929,7 +929,7 @@ struct drm_mode_config { const struct drm_mode_config_helper_funcs *helper_private; }; -void drm_mode_config_init(struct drm_device *dev); +int drm_mode_config_init(struct drm_device *dev); void drm_mode_config_reset(struct drm_device *dev); void drm_mode_config_cleanup(struct drm_device *dev);