[PATCH 1/3] drm/komeda: Consolidate struct komeda_drv allocations

Mihail Atanassov Mihail.Atanassov at arm.com
Fri Oct 4 14:34:37 UTC 2019


struct komeda_drv has two pointer members only, and both
it and its members get allocated separately. Since both
members' types are in scope, there's not a lot to gain by
keeping the indirection around.

To avoid double-free issues, provide a barebones ->release()
hook for the driver.

Signed-off-by: Mihail Atanassov <mihail.atanassov at arm.com>
---
 .../gpu/drm/arm/display/komeda/komeda_dev.c   | 21 ++++-------
 .../gpu/drm/arm/display/komeda/komeda_dev.h   |  4 +--
 .../gpu/drm/arm/display/komeda/komeda_drv.c   | 36 +++++++++----------
 .../gpu/drm/arm/display/komeda/komeda_kms.c   | 26 ++++++++------
 .../gpu/drm/arm/display/komeda/komeda_kms.h   |  4 +--
 5 files changed, 42 insertions(+), 49 deletions(-)

diff --git a/drivers/gpu/drm/arm/display/komeda/komeda_dev.c b/drivers/gpu/drm/arm/display/komeda/komeda_dev.c
index 937a6d4c4865..c84f978cacc3 100644
--- a/drivers/gpu/drm/arm/display/komeda/komeda_dev.c
+++ b/drivers/gpu/drm/arm/display/komeda/komeda_dev.c
@@ -179,28 +179,23 @@ static int komeda_parse_dt(struct device *dev, struct komeda_dev *mdev)
 	return ret;
 }
 
-struct komeda_dev *komeda_dev_create(struct device *dev)
+int komeda_dev_init(struct komeda_dev *mdev, struct device *dev)
 {
 	struct platform_device *pdev = to_platform_device(dev);
 	const struct komeda_product_data *product;
-	struct komeda_dev *mdev;
 	struct resource *io_res;
 	int err = 0;
 
 	product = of_device_get_match_data(dev);
 	if (!product)
-		return ERR_PTR(-ENODEV);
+		return -ENODEV;
 
 	io_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
 	if (!io_res) {
 		DRM_ERROR("No registers defined.\n");
-		return ERR_PTR(-ENODEV);
+		return -ENODEV;
 	}
 
-	mdev = devm_kzalloc(dev, sizeof(*mdev), GFP_KERNEL);
-	if (!mdev)
-		return ERR_PTR(-ENOMEM);
-
 	mutex_init(&mdev->lock);
 
 	mdev->dev = dev;
@@ -284,16 +279,16 @@ struct komeda_dev *komeda_dev_create(struct device *dev)
 	komeda_debugfs_init(mdev);
 #endif
 
-	return mdev;
+	return 0;
 
 disable_clk:
 	clk_disable_unprepare(mdev->aclk);
 err_cleanup:
-	komeda_dev_destroy(mdev);
-	return ERR_PTR(err);
+	komeda_dev_fini(mdev);
+	return err;
 }
 
-void komeda_dev_destroy(struct komeda_dev *mdev)
+void komeda_dev_fini(struct komeda_dev *mdev)
 {
 	struct device *dev = mdev->dev;
 	const struct komeda_dev_funcs *funcs = mdev->funcs;
@@ -335,8 +330,6 @@ void komeda_dev_destroy(struct komeda_dev *mdev)
 		devm_clk_put(dev, mdev->aclk);
 		mdev->aclk = NULL;
 	}
-
-	devm_kfree(dev, mdev);
 }
 
 int komeda_dev_resume(struct komeda_dev *mdev)
diff --git a/drivers/gpu/drm/arm/display/komeda/komeda_dev.h b/drivers/gpu/drm/arm/display/komeda/komeda_dev.h
index 414200233b64..e392b8ffc372 100644
--- a/drivers/gpu/drm/arm/display/komeda/komeda_dev.h
+++ b/drivers/gpu/drm/arm/display/komeda/komeda_dev.h
@@ -213,8 +213,8 @@ komeda_product_match(struct komeda_dev *mdev, u32 target)
 const struct komeda_dev_funcs *
 d71_identify(u32 __iomem *reg, struct komeda_chip_info *chip);
 
-struct komeda_dev *komeda_dev_create(struct device *dev);
-void komeda_dev_destroy(struct komeda_dev *mdev);
+int komeda_dev_init(struct komeda_dev *mdev, struct device *dev);
+void komeda_dev_fini(struct komeda_dev *mdev);
 
 struct komeda_dev *dev_to_mdev(struct device *dev);
 
diff --git a/drivers/gpu/drm/arm/display/komeda/komeda_drv.c b/drivers/gpu/drm/arm/display/komeda/komeda_drv.c
index d6c2222c5d33..660181bdc008 100644
--- a/drivers/gpu/drm/arm/display/komeda/komeda_drv.c
+++ b/drivers/gpu/drm/arm/display/komeda/komeda_drv.c
@@ -14,15 +14,15 @@
 #include "komeda_kms.h"
 
 struct komeda_drv {
-	struct komeda_dev *mdev;
-	struct komeda_kms_dev *kms;
+	struct komeda_dev mdev;
+	struct komeda_kms_dev kms;
 };
 
 struct komeda_dev *dev_to_mdev(struct device *dev)
 {
 	struct komeda_drv *mdrv = dev_get_drvdata(dev);
 
-	return mdrv ? mdrv->mdev : NULL;
+	return mdrv ? &mdrv->mdev : NULL;
 }
 
 static void komeda_unbind(struct device *dev)
@@ -32,8 +32,8 @@ static void komeda_unbind(struct device *dev)
 	if (!mdrv)
 		return;
 
-	komeda_kms_detach(mdrv->kms);
-	komeda_dev_destroy(mdrv->mdev);
+	komeda_kms_fini(mdrv->kms);
+	komeda_dev_fini(mdrv->mdev);
 
 	dev_set_drvdata(dev, NULL);
 	devm_kfree(dev, mdrv);
@@ -48,24 +48,20 @@ static int komeda_bind(struct device *dev)
 	if (!mdrv)
 		return -ENOMEM;
 
-	mdrv->mdev = komeda_dev_create(dev);
-	if (IS_ERR(mdrv->mdev)) {
-		err = PTR_ERR(mdrv->mdev);
+	err = komeda_dev_init(&mdrv->mdev, dev);
+	if (err)
 		goto free_mdrv;
-	}
 
-	mdrv->kms = komeda_kms_attach(mdrv->mdev);
-	if (IS_ERR(mdrv->kms)) {
-		err = PTR_ERR(mdrv->kms);
-		goto destroy_mdev;
-	}
+	err = komeda_kms_init(&mdrv->kms, &mdrv->mdev);
+	if (err)
+		goto fini_mdev;
 
 	dev_set_drvdata(dev, mdrv);
 
 	return 0;
 
-destroy_mdev:
-	komeda_dev_destroy(mdrv->mdev);
+fini_mdev:
+	komeda_dev_fini(&mdrv->mdev);
 
 free_mdrv:
 	devm_kfree(dev, mdrv);
@@ -140,12 +136,12 @@ MODULE_DEVICE_TABLE(of, komeda_of_match);
 static int __maybe_unused komeda_pm_suspend(struct device *dev)
 {
 	struct komeda_drv *mdrv = dev_get_drvdata(dev);
-	struct drm_device *drm = &mdrv->kms->base;
+	struct drm_device *drm = &mdrv->kms.base;
 	int res;
 
 	res = drm_mode_config_helper_suspend(drm);
 
-	komeda_dev_suspend(mdrv->mdev);
+	komeda_dev_suspend(&mdrv->mdev);
 
 	return res;
 }
@@ -153,9 +149,9 @@ static int __maybe_unused komeda_pm_suspend(struct device *dev)
 static int __maybe_unused komeda_pm_resume(struct device *dev)
 {
 	struct komeda_drv *mdrv = dev_get_drvdata(dev);
-	struct drm_device *drm = &mdrv->kms->base;
+	struct drm_device *drm = &mdrv->kms.base;
 
-	komeda_dev_resume(mdrv->mdev);
+	komeda_dev_resume(&mdrv->mdev);
 
 	return drm_mode_config_helper_resume(drm);
 }
diff --git a/drivers/gpu/drm/arm/display/komeda/komeda_kms.c b/drivers/gpu/drm/arm/display/komeda/komeda_kms.c
index d49772de93e0..03dcf1d7688f 100644
--- a/drivers/gpu/drm/arm/display/komeda/komeda_kms.c
+++ b/drivers/gpu/drm/arm/display/komeda/komeda_kms.c
@@ -23,6 +23,15 @@
 
 DEFINE_DRM_GEM_CMA_FOPS(komeda_cma_fops);
 
+static void komeda_kms_release(struct drm_device *dev)
+{
+	drm_dev_fini(dev);
+	/*
+	 * No need to kfree(dev) since we're stored in an aggregate struct
+	 * that's managed separately.
+	 */
+}
+
 static int komeda_gem_cma_dumb_create(struct drm_file *file,
 				      struct drm_device *dev,
 				      struct drm_mode_create_dumb *args)
@@ -60,6 +69,7 @@ static irqreturn_t komeda_kms_irq_handler(int irq, void *data)
 static struct drm_driver komeda_kms_driver = {
 	.driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_ATOMIC,
 	.lastclose			= drm_fb_helper_lastclose,
+	.release			= komeda_kms_release,
 	.gem_free_object_unlocked	= drm_gem_cma_free_object,
 	.gem_vm_ops			= &drm_gem_cma_vm_ops,
 	.dumb_create			= komeda_gem_cma_dumb_create,
@@ -257,19 +267,15 @@ static void komeda_kms_mode_config_init(struct komeda_kms_dev *kms,
 	config->helper_private = &komeda_mode_config_helpers;
 }
 
-struct komeda_kms_dev *komeda_kms_attach(struct komeda_dev *mdev)
+int komeda_kms_init(struct komeda_kms_dev *kms, struct komeda_dev *mdev)
 {
-	struct komeda_kms_dev *kms = kzalloc(sizeof(*kms), GFP_KERNEL);
 	struct drm_device *drm;
 	int err;
 
-	if (!kms)
-		return ERR_PTR(-ENOMEM);
-
 	drm = &kms->base;
 	err = drm_dev_init(drm, &komeda_kms_driver, mdev->dev);
 	if (err)
-		goto free_kms;
+		return err;
 
 	drm->dev_private = mdev;
 
@@ -319,7 +325,7 @@ struct komeda_kms_dev *komeda_kms_attach(struct komeda_dev *mdev)
 	if (err)
 		goto free_interrupts;
 
-	return kms;
+	return 0;
 
 free_interrupts:
 	drm_kms_helper_poll_fini(drm);
@@ -332,12 +338,10 @@ struct komeda_kms_dev *komeda_kms_attach(struct komeda_dev *mdev)
 	komeda_kms_cleanup_private_objs(kms);
 	drm->dev_private = NULL;
 	drm_dev_put(drm);
-free_kms:
-	kfree(kms);
-	return ERR_PTR(err);
+	return err;
 }
 
-void komeda_kms_detach(struct komeda_kms_dev *kms)
+void komeda_kms_fini(struct komeda_kms_dev *kms)
 {
 	struct drm_device *drm = &kms->base;
 	struct komeda_dev *mdev = drm->dev_private;
diff --git a/drivers/gpu/drm/arm/display/komeda/komeda_kms.h b/drivers/gpu/drm/arm/display/komeda/komeda_kms.h
index 45c498e15e7a..e81ceb298034 100644
--- a/drivers/gpu/drm/arm/display/komeda/komeda_kms.h
+++ b/drivers/gpu/drm/arm/display/komeda/komeda_kms.h
@@ -181,7 +181,7 @@ void komeda_kms_cleanup_private_objs(struct komeda_kms_dev *kms);
 void komeda_crtc_handle_event(struct komeda_crtc   *kcrtc,
 			      struct komeda_events *evts);
 
-struct komeda_kms_dev *komeda_kms_attach(struct komeda_dev *mdev);
-void komeda_kms_detach(struct komeda_kms_dev *kms);
+int komeda_kms_init(struct komeda_kms_dev *kms, struct komeda_dev *mdev);
+void komeda_kms_fini(struct komeda_kms_dev *kms);
 
 #endif /*_KOMEDA_KMS_H_*/
-- 
2.23.0



More information about the dri-devel mailing list