dri-devel.lists.freedesktop.org archive mirror
 help / color / mirror / Atom feed
From: Daniel Vetter <daniel.vetter@ffwll.ch>
To: DRI Development <dri-devel@lists.freedesktop.org>
Cc: Daniel Vetter <daniel.vetter@ffwll.ch>,
	Intel Graphics Development <intel-gfx@lists.freedesktop.org>,
	Sam Ravnborg <sam@ravnborg.org>,
	Daniel Vetter <daniel.vetter@intel.com>
Subject: [PATCH 24/51] drm: Manage drm_vblank_cleanup with drmm_
Date: Mon, 23 Mar 2020 15:49:23 +0100	[thread overview]
Message-ID: <20200323144950.3018436-25-daniel.vetter@ffwll.ch> (raw)
In-Reply-To: <20200323144950.3018436-1-daniel.vetter@ffwll.ch>

Nothing special here, except that this is the first time that we
automatically clean up something that's initialized with an explicit
driver call. But the cleanup was done at the very end of the release
sequence for all drivers, and that's still the case. At least without
more uses of drmm_ through explicit driver calls.

Also for this one we need drmm_kcalloc, so lets add those.

The motivation here is to allow us to remove the explicit calls to
drm_dev_fini() from all drivers.

v2: Sort includes (Laurent)

v3: Motivate the change in the commit message better (Sam)

Acked-by: Sam Ravnborg <sam@ravnborg.org>
Signed-off-by: Daniel Vetter <daniel.vetter@intel.com>
---
 drivers/gpu/drm/drm_drv.c      |  1 -
 drivers/gpu/drm/drm_internal.h |  1 -
 drivers/gpu/drm/drm_vblank.c   | 31 ++++++++++++-------------------
 include/drm/drm_managed.h      | 16 ++++++++++++++++
 4 files changed, 28 insertions(+), 21 deletions(-)

diff --git a/drivers/gpu/drm/drm_drv.c b/drivers/gpu/drm/drm_drv.c
index 18e7f7389897..3516b16cd33d 100644
--- a/drivers/gpu/drm/drm_drv.c
+++ b/drivers/gpu/drm/drm_drv.c
@@ -750,7 +750,6 @@ EXPORT_SYMBOL(devm_drm_dev_init);
  */
 void drm_dev_fini(struct drm_device *dev)
 {
-	drm_vblank_cleanup(dev);
 }
 EXPORT_SYMBOL(drm_dev_fini);
 
diff --git a/drivers/gpu/drm/drm_internal.h b/drivers/gpu/drm/drm_internal.h
index a40a0222419e..2470a352730b 100644
--- a/drivers/gpu/drm/drm_internal.h
+++ b/drivers/gpu/drm/drm_internal.h
@@ -94,7 +94,6 @@ void drm_managed_release(struct drm_device *dev);
 
 /* drm_vblank.c */
 void drm_vblank_disable_and_save(struct drm_device *dev, unsigned int pipe);
-void drm_vblank_cleanup(struct drm_device *dev);
 
 /* IOCTLS */
 int drm_wait_vblank_ioctl(struct drm_device *dev, void *data,
diff --git a/drivers/gpu/drm/drm_vblank.c b/drivers/gpu/drm/drm_vblank.c
index da7b0b0c1090..bcf346b3e486 100644
--- a/drivers/gpu/drm/drm_vblank.c
+++ b/drivers/gpu/drm/drm_vblank.c
@@ -30,6 +30,7 @@
 #include <drm/drm_crtc.h>
 #include <drm/drm_drv.h>
 #include <drm/drm_framebuffer.h>
+#include <drm/drm_managed.h>
 #include <drm/drm_modeset_helper_vtables.h>
 #include <drm/drm_print.h>
 #include <drm/drm_vblank.h>
@@ -425,14 +426,10 @@ static void vblank_disable_fn(struct timer_list *t)
 	spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
 }
 
-void drm_vblank_cleanup(struct drm_device *dev)
+static void drm_vblank_init_release(struct drm_device *dev, void *ptr)
 {
 	unsigned int pipe;
 
-	/* Bail if the driver didn't call drm_vblank_init() */
-	if (dev->num_crtcs == 0)
-		return;
-
 	for (pipe = 0; pipe < dev->num_crtcs; pipe++) {
 		struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
 
@@ -441,10 +438,6 @@ void drm_vblank_cleanup(struct drm_device *dev)
 
 		del_timer_sync(&vblank->disable_timer);
 	}
-
-	kfree(dev->vblank);
-
-	dev->num_crtcs = 0;
 }
 
 /**
@@ -453,25 +446,29 @@ void drm_vblank_cleanup(struct drm_device *dev)
  * @num_crtcs: number of CRTCs supported by @dev
  *
  * This function initializes vblank support for @num_crtcs display pipelines.
- * Cleanup is handled by the DRM core, or through calling drm_dev_fini() for
- * drivers with a &drm_driver.release callback.
+ * Cleanup is handled automatically through a cleanup function added with
+ * drmm_add_action().
  *
  * Returns:
  * Zero on success or a negative error code on failure.
  */
 int drm_vblank_init(struct drm_device *dev, unsigned int num_crtcs)
 {
-	int ret = -ENOMEM;
+	int ret;
 	unsigned int i;
 
 	spin_lock_init(&dev->vbl_lock);
 	spin_lock_init(&dev->vblank_time_lock);
 
+	dev->vblank = drmm_kcalloc(dev, num_crtcs, sizeof(*dev->vblank), GFP_KERNEL);
+	if (!dev->vblank)
+		return -ENOMEM;
+
 	dev->num_crtcs = num_crtcs;
 
-	dev->vblank = kcalloc(num_crtcs, sizeof(*dev->vblank), GFP_KERNEL);
-	if (!dev->vblank)
-		goto err;
+	ret = drmm_add_action(dev, drm_vblank_init_release, NULL);
+	if (ret)
+		return ret;
 
 	for (i = 0; i < num_crtcs; i++) {
 		struct drm_vblank_crtc *vblank = &dev->vblank[i];
@@ -486,10 +483,6 @@ int drm_vblank_init(struct drm_device *dev, unsigned int num_crtcs)
 	DRM_INFO("Supports vblank timestamp caching Rev 2 (21.10.2013).\n");
 
 	return 0;
-
-err:
-	dev->num_crtcs = 0;
-	return ret;
 }
 EXPORT_SYMBOL(drm_vblank_init);
 
diff --git a/include/drm/drm_managed.h b/include/drm/drm_managed.h
index 2d1e29a2200c..191d8d206ff4 100644
--- a/include/drm/drm_managed.h
+++ b/include/drm/drm_managed.h
@@ -4,6 +4,7 @@
 #define _DRM_MANAGED_H_
 
 #include <linux/gfp.h>
+#include <linux/overflow.h>
 #include <linux/types.h>
 
 struct drm_device;
@@ -31,6 +32,21 @@ static inline void *drmm_kzalloc(struct drm_device *dev, size_t size, gfp_t gfp)
 {
 	return drmm_kmalloc(dev, size, gfp | __GFP_ZERO);
 }
+static inline void *drmm_kmalloc_array(struct drm_device *dev,
+				       size_t n, size_t size, gfp_t flags)
+{
+	size_t bytes;
+
+	if (unlikely(check_mul_overflow(n, size, &bytes)))
+		return NULL;
+
+	return drmm_kmalloc(dev, bytes, flags);
+}
+static inline void *drmm_kcalloc(struct drm_device *dev,
+				 size_t n, size_t size, gfp_t flags)
+{
+	return drmm_kmalloc_array(dev, n, size, flags | __GFP_ZERO);
+}
 char *drmm_kstrdup(struct drm_device *dev, const char *s, gfp_t gfp);
 
 void drmm_kfree(struct drm_device *dev, void *data);
-- 
2.25.1

_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/dri-devel

  parent reply	other threads:[~2020-03-23 14:51 UTC|newest]

Thread overview: 77+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-03-23 14:48 [PATCH 00/51] drm_device managed resources, v5 Daniel Vetter
2020-03-23 14:49 ` [PATCH 01/51] mm/sl[uo]b: export __kmalloc_track(_node)_caller Daniel Vetter
2020-03-26 13:46   ` Daniel Vetter
2020-03-23 14:49 ` [PATCH 02/51] drm/i915: Don't clear drvdata in ->release Daniel Vetter
2020-03-25 18:20   ` [Intel-gfx] " Jani Nikula
2020-03-26 13:15     ` Jani Nikula
2020-03-23 14:49 ` [PATCH 03/51] drm: add managed resources tied to drm_device Daniel Vetter
2020-03-23 18:36   ` Sam Ravnborg
2020-03-24 12:45   ` [PATCH] " Daniel Vetter
2020-03-23 14:49 ` [PATCH 04/51] drm: Set final_kfree in drm_dev_alloc Daniel Vetter
2020-03-23 14:49 ` [PATCH 05/51] drm/mipi_dbi: Use drmm_add_final_kfree in all drivers Daniel Vetter
2020-03-23 14:49 ` [PATCH 06/51] drm/udl: Use drmm_add_final_kfree Daniel Vetter
2020-03-23 14:49 ` [PATCH 07/51] drm/qxl: " Daniel Vetter
2020-03-23 14:49 ` [PATCH 08/51] drm/i915: " Daniel Vetter
2020-03-26 13:10   ` Jani Nikula
2020-03-26 13:33     ` Daniel Vetter
2020-03-23 14:49 ` [PATCH 09/51] drm/cirrus: " Daniel Vetter
2020-03-23 14:49 ` [PATCH 10/51] drm/v3d: " Daniel Vetter
2020-03-23 14:49 ` [PATCH 11/51] drm/tidss: " Daniel Vetter
2020-03-23 14:49 ` [PATCH 12/51] drm/mcde: " Daniel Vetter
2020-03-23 14:49 ` [PATCH 13/51] drm/vgem: " Daniel Vetter
2020-03-23 14:49 ` [PATCH 14/51] drm/vkms: " Daniel Vetter
2020-03-23 14:49 ` [PATCH 15/51] drm/repaper: " Daniel Vetter
2020-03-23 14:49 ` [PATCH 16/51] drm/ingenic: " Daniel Vetter
2020-03-23 14:49 ` [PATCH 17/51] drm/gm12u320: " Daniel Vetter
2020-03-23 14:49 ` [PATCH 18/51] drm/<drivers>: " Daniel Vetter
2020-03-23 14:49 ` [PATCH 19/51] drm: Cleanups after drmm_add_final_kfree rollout Daniel Vetter
2020-04-02  0:50   ` Laurent Pinchart
2020-04-02  5:17     ` Daniel Vetter
2020-04-02  9:39       ` Laurent Pinchart
2020-04-02  9:50         ` Daniel Vetter
2020-03-23 14:49 ` [PATCH 20/51] drm: Handle dev->unique with drmm_ Daniel Vetter
2020-03-23 14:49 ` [PATCH 21/51] drm: Use drmm_ for drm_dev_init cleanup Daniel Vetter
2020-03-24 21:20   ` Sam Ravnborg
2020-03-23 14:49 ` [PATCH 22/51] drm: manage drm_minor cleanup with drmm_ Daniel Vetter
2020-03-24  8:54   ` Thomas Zimmermann
2020-03-24 20:39   ` [PATCH] " Daniel Vetter
2020-03-24 21:42     ` Sam Ravnborg
2020-03-25  9:09       ` Daniel Vetter
2020-03-24 21:36   ` [PATCH 22/51] " Sam Ravnborg
2020-03-25  9:07     ` Daniel Vetter
2020-03-23 14:49 ` [PATCH 23/51] drm: Manage drm_gem_init " Daniel Vetter
2020-03-23 14:49 ` Daniel Vetter [this message]
2020-03-23 14:49 ` [PATCH 25/51] drm: Garbage collect drm_dev_fini Daniel Vetter
2020-03-23 14:49 ` [PATCH 26/51] drm: Manage drm_mode_config_init with drmm_ Daniel Vetter
2020-03-23 14:49 ` [PATCH 27/51] drm/bochs: Remove leftover drm_atomic_helper_shutdown Daniel Vetter
2020-03-23 14:49 ` [PATCH 28/51] drm/bochs: Drop explicit drm_mode_config_cleanup Daniel Vetter
2020-03-23 14:49 ` [PATCH 29/51] drm/cirrus: Drop explicit drm_mode_config_cleanup call Daniel Vetter
2020-03-23 14:49 ` [PATCH 30/51] drm/cirrus: Fully embrace devm_ Daniel Vetter
2020-03-23 14:49 ` [PATCH 31/51] drm/ingenic: Drop explicit drm_mode_config_cleanup call Daniel Vetter
2020-03-23 14:49 ` [PATCH 32/51] drm/mcde: " Daniel Vetter
2020-03-23 14:49 ` [PATCH 33/51] drm/mcde: More devm_drm_dev_init Daniel Vetter
2020-03-23 14:49 ` [PATCH 34/51] drm/meson: Drop explicit drm_mode_config_cleanup call Daniel Vetter
2020-03-23 14:49 ` [PATCH 35/51] drm/pl111: " Daniel Vetter
2020-03-23 14:49 ` [PATCH 36/51] drm/rcar-du: " Daniel Vetter
2020-03-23 14:49 ` [PATCH 37/51] drm/rockchip: " Daniel Vetter
2020-03-23 14:49 ` [PATCH 38/51] drm/stm: " Daniel Vetter
2020-03-23 14:49 ` [PATCH 39/51] drm/shmob: " Daniel Vetter
2020-03-23 14:49 ` [PATCH 40/51] drm/mtk: " Daniel Vetter
2020-03-23 15:27   ` Chun-Kuang Hu
2020-03-23 14:49 ` [PATCH 41/51] drm/tidss: " Daniel Vetter
2020-03-23 14:49 ` [PATCH 42/51] drm/gm12u320: More drmm_ Daniel Vetter
2020-03-23 14:49 ` [PATCH 43/51] drm/gm12u320: Use devm_drm_dev_init Daniel Vetter
2020-03-23 14:49 ` [PATCH 44/51] drm/gm12u320: Use helpers for shutdown/suspend/resume Daniel Vetter
2020-03-23 14:49 ` [PATCH 45/51] drm/gm12u320: Simplify upload work Daniel Vetter
2020-03-23 14:49 ` [PATCH 46/51] drm/repaper: Drop explicit drm_mode_config_cleanup call Daniel Vetter
2020-03-23 14:49 ` [PATCH 47/51] drm/mipi-dbi: Move drm_mode_config_init into mipi library Daniel Vetter
2020-03-23 14:49 ` [PATCH 48/51] drm/mipi-dbi: Drop explicit drm_mode_config_cleanup call Daniel Vetter
2020-03-23 14:49 ` [PATCH 49/51] drm/udl: " Daniel Vetter
2020-03-24  8:56   ` Thomas Zimmermann
2020-03-23 14:49 ` [PATCH 50/51] drm/udl: drop drm_driver.release hook Daniel Vetter
2020-03-23 14:49 ` [PATCH 51/51] drm: Add docs for managed resources Daniel Vetter
2020-03-26 15:10 ` [PATCH 00/51] drm_device managed resources, v5 Daniel Vetter
  -- strict thread matches above, loose matches on Subject: below --
2020-03-02 22:25 [PATCH 00/51] drm_device managed resources, v4 Daniel Vetter
2020-03-02 22:26 ` [PATCH 24/51] drm: Manage drm_vblank_cleanup with drmm_ Daniel Vetter
2020-03-07  8:28   ` Sam Ravnborg
2020-02-27 18:14 [PATCH 00/51] drm managed resources, v3 Daniel Vetter
2020-02-27 18:14 ` [PATCH 24/51] drm: Manage drm_vblank_cleanup with drmm_ Daniel Vetter
2020-02-21 21:02 [PATCH 00/51] drm managed resources, v2 Daniel Vetter
2020-02-21 21:02 ` [PATCH 24/51] drm: Manage drm_vblank_cleanup with drmm_ Daniel Vetter

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200323144950.3018436-25-daniel.vetter@ffwll.ch \
    --to=daniel.vetter@ffwll.ch \
    --cc=daniel.vetter@intel.com \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=intel-gfx@lists.freedesktop.org \
    --cc=sam@ravnborg.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).