Skip to contentSkip to conversation

drm/fsl-dcu: use drm managed resources

PATCH Superseded Danilo Krummrich 1 participant 9 messages Sep 15, 2022

Subsystems (3)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover drm/fsl-dcu: use drm managed resources

Hi,

This patch series converts the driver to use drm managed resources to prevent potential use-after-free issues on driver unbind/rebind and to get rid of the usage of deprecated APIs.

Danilo Krummrich (8):
drm/fsl-dcu: use drmm_* to allocate driver structures drm/fsl-dcu: replace drm->dev_private with drm_to_fsl_dcu_drm_dev() drm/fsl-dcu: crtc: use drmm_crtc_init_with_planes() drm/fsl-dcu: plane: use drm managed resources drm/fsl-dcu: use drm_dev_unplug()
drm/fsl-dcu: plane: protect device resources after removal drm/fsl-dcu: crtc: protect device resources after removal drm/fsl-dcu: remove trailing return statements

 drivers/gpu/drm/fsl-dcu/fsl_dcu_drm_crtc.c  | 64 ++++++++++++++++-----
 drivers/gpu/drm/fsl-dcu/fsl_dcu_drm_drv.c   | 43 ++++++--------
 drivers/gpu/drm/fsl-dcu/fsl_dcu_drm_drv.h   |  4 +-
 drivers/gpu/drm/fsl-dcu/fsl_dcu_drm_kms.c   | 19 +++---
 drivers/gpu/drm/fsl-dcu/fsl_dcu_drm_plane.c | 48 ++++++++--------
 drivers/gpu/drm/fsl-dcu/fsl_dcu_drm_rgb.c   |  8 +--
 6 files changed, 108 insertions(+), 78 deletions(-)

base-commit: 961bcdf956a4645745407a5d919be8757549b062

Signature · 1 line
2.37.3
#1 lore raw

PATCH 1/8 drm/fsl-dcu: use drmm_* to allocate driver structures

Use drm managed resources to allocate driver structures and get rid of the deprecated drm_dev_alloc() call and replace it with devm_drm_dev_alloc().

This also serves as preparation to get rid of drm_device->dev_private and to fix use-after-free issues on driver unload.

---
 drivers/gpu/drm/fsl-dcu/fsl_dcu_drm_crtc.c |  7 ++---
 drivers/gpu/drm/fsl-dcu/fsl_dcu_drm_drv.c  | 30 +++++++++-------------
 drivers/gpu/drm/fsl-dcu/fsl_dcu_drm_drv.h  |  2 +-
 drivers/gpu/drm/fsl-dcu/fsl_dcu_drm_kms.c  | 19 +++++++-------
 drivers/gpu/drm/fsl-dcu/fsl_dcu_drm_rgb.c  |  8 +++---
 5 files changed, 31 insertions(+), 35 deletions(-)
drivers/gpu/drm/fsl-dcu/fsl_dcu_drm_crtc.c +4 -3
index 2af60d98f48f..a1b8ce70928a 100644
@@ -171,15 +171,16 @@ int fsl_dcu_drm_crtc_create(struct fsl_dcu_drm_device *fsl_dev)
 {
 	struct drm_plane *primary;
 	struct drm_crtc *crtc = &fsl_dev->crtc;
+	struct drm_device *drm = &fsl_dev->base;
 	int ret;
 
-	fsl_dcu_drm_init_planes(fsl_dev->drm);
+	fsl_dcu_drm_init_planes(drm);
 
-	primary = fsl_dcu_drm_primary_create_plane(fsl_dev->drm);
+	primary = fsl_dcu_drm_primary_create_plane(drm);
 	if (!primary)
 		return -ENOMEM;
 
-	ret = drm_crtc_init_with_planes(fsl_dev->drm, crtc, primary, NULL,
+	ret = drm_crtc_init_with_planes(drm, crtc, primary, NULL,
 					&fsl_dcu_drm_crtc_funcs, NULL);
 	if (ret) {
 		primary->funcs->destroy(primary);
drivers/gpu/drm/fsl-dcu/fsl_dcu_drm_drv.c +12 -18
index b4acc3422ba4..a47b72995fcf 100644
@@ -168,6 +168,7 @@ static const struct drm_driver fsl_dcu_drm_driver = {
 static int fsl_dcu_drm_pm_suspend(struct device *dev)
 {
 	struct fsl_dcu_drm_device *fsl_dev = dev_get_drvdata(dev);
+	struct drm_device *drm = &fsl_dev->base;
 	int ret;
 
 	if (!fsl_dev)
@@ -175,7 +176,7 @@ static int fsl_dcu_drm_pm_suspend(struct device *dev)
 
 	disable_irq(fsl_dev->irq);
 
-	ret = drm_mode_config_helper_suspend(fsl_dev->drm);
+	ret = drm_mode_config_helper_suspend(drm);
 	if (ret) {
 		enable_irq(fsl_dev->irq);
 		return ret;
@@ -189,6 +190,7 @@ static int fsl_dcu_drm_pm_suspend(struct device *dev)
 static int fsl_dcu_drm_pm_resume(struct device *dev)
 {
 	struct fsl_dcu_drm_device *fsl_dev = dev_get_drvdata(dev);
+	struct drm_device *drm = &fsl_dev->base;
 	int ret;
 
 	if (!fsl_dev)
@@ -202,10 +204,10 @@ static int fsl_dcu_drm_pm_resume(struct device *dev)
 
 	if (fsl_dev->tcon)
 		fsl_tcon_bypass_enable(fsl_dev->tcon);
-	fsl_dcu_drm_init_planes(fsl_dev->drm);
+	fsl_dcu_drm_init_planes(drm);
 	enable_irq(fsl_dev->irq);
 
-	drm_mode_config_helper_resume(fsl_dev->drm);
+	drm_mode_config_helper_resume(drm);
 
 	return 0;
 }
@@ -255,9 +257,10 @@ static int fsl_dcu_drm_probe(struct platform_device *pdev)
 	int ret;
 	u8 div_ratio_shift = 0;
 
-	fsl_dev = devm_kzalloc(dev, sizeof(*fsl_dev), GFP_KERNEL);
-	if (!fsl_dev)
-		return -ENOMEM;
+	fsl_dev = devm_drm_dev_alloc(dev, &fsl_dcu_drm_driver,
+				     typeof(*fsl_dev), base);
+	if (IS_ERR(fsl_dev))
+		return PTR_ERR(fsl_dev);
 
 	id = of_match_node(fsl_dcu_of_match, pdev->dev.of_node);
 	if (!id)
@@ -317,28 +320,19 @@ static int fsl_dcu_drm_probe(struct platform_device *pdev)
 
 	fsl_dev->tcon = fsl_tcon_init(dev);
 
-	drm = drm_dev_alloc(&fsl_dcu_drm_driver, dev);
-	if (IS_ERR(drm)) {
-		ret = PTR_ERR(drm);
-		goto unregister_pix_clk;
-	}
-
 	fsl_dev->dev = dev;
-	fsl_dev->drm = drm;
 	fsl_dev->np = dev->of_node;
 	drm->dev_private = fsl_dev;
 	dev_set_drvdata(dev, fsl_dev);
 
 	ret = drm_dev_register(drm, 0);
 	if (ret < 0)
-		goto put;
+		goto unregister_pix_clk;
 
 	drm_fbdev_generic_setup(drm, legacyfb_depth);
 
 	return 0;
 
-put:
-	drm_dev_put(drm);
 unregister_pix_clk:
 	clk_unregister(fsl_dev->pix_clk);
 disable_clk:
@@ -349,9 +343,9 @@ static int fsl_dcu_drm_probe(struct platform_device *pdev)
 static int fsl_dcu_drm_remove(struct platform_device *pdev)
 {
 	struct fsl_dcu_drm_device *fsl_dev = platform_get_drvdata(pdev);
+	struct drm_device *drm = &fsl_dev->base;
 
-	drm_dev_unregister(fsl_dev->drm);
-	drm_dev_put(fsl_dev->drm);
+	drm_dev_unregister(drm);
 	clk_disable_unprepare(fsl_dev->clk);
 	clk_unregister(fsl_dev->pix_clk);
 
drivers/gpu/drm/fsl-dcu/fsl_dcu_drm_drv.h +1 -1
index e2049a0e8a92..20ca13ff618a 100644
@@ -177,6 +177,7 @@ struct fsl_dcu_soc_data {
 };
 
 struct fsl_dcu_drm_device {
+	struct drm_device base;
 	struct device *dev;
 	struct device_node *np;
 	struct regmap *regmap;
@@ -186,7 +187,6 @@ struct fsl_dcu_drm_device {
 	struct fsl_tcon *tcon;
 	/*protects hardware register*/
 	spinlock_t irq_lock;
-	struct drm_device *drm;
 	struct drm_crtc crtc;
 	struct drm_encoder encoder;
 	struct fsl_dcu_drm_connector connector;
drivers/gpu/drm/fsl-dcu/fsl_dcu_drm_kms.c +10 -9
index 5b47000738e4..c3d55c0aca9f 100644
@@ -20,15 +20,16 @@ static const struct drm_mode_config_funcs fsl_dcu_drm_mode_config_funcs = {
 
 int fsl_dcu_drm_modeset_init(struct fsl_dcu_drm_device *fsl_dev)
 {
+	struct drm_device *drm = &fsl_dev->base;
 	int ret;
 
-	drm_mode_config_init(fsl_dev->drm);
+	drm_mode_config_init(drm);
 
-	fsl_dev->drm->mode_config.min_width = 0;
-	fsl_dev->drm->mode_config.min_height = 0;
-	fsl_dev->drm->mode_config.max_width = 2031;
-	fsl_dev->drm->mode_config.max_height = 2047;
-	fsl_dev->drm->mode_config.funcs = &fsl_dcu_drm_mode_config_funcs;
+	drm->mode_config.min_width = 0;
+	drm->mode_config.min_height = 0;
+	drm->mode_config.max_width = 2031;
+	drm->mode_config.max_height = 2047;
+	drm->mode_config.funcs = &fsl_dcu_drm_mode_config_funcs;
 
 	ret = fsl_dcu_drm_crtc_create(fsl_dev);
 	if (ret)
@@ -42,12 +43,12 @@ int fsl_dcu_drm_modeset_init(struct fsl_dcu_drm_device *fsl_dev)
 	if (ret)
 		goto err;
 
-	drm_mode_config_reset(fsl_dev->drm);
-	drm_kms_helper_poll_init(fsl_dev->drm);
+	drm_mode_config_reset(drm);
+	drm_kms_helper_poll_init(drm);
 
 	return 0;
 
 err:
-	drm_mode_config_cleanup(fsl_dev->drm);
+	drm_mode_config_cleanup(drm);
 	return ret;
 }
drivers/gpu/drm/fsl-dcu/fsl_dcu_drm_rgb.c +4 -4
index 4d4a715b429d..b9b02f617cd2 100644
@@ -22,6 +22,7 @@ int fsl_dcu_drm_encoder_create(struct fsl_dcu_drm_device *fsl_dev,
 			       struct drm_crtc *crtc)
 {
 	struct drm_encoder *encoder = &fsl_dev->encoder;
+	struct drm_device *drm = &fsl_dev->base;
 	int ret;
 
 	encoder->possible_crtcs = 1;
@@ -30,8 +31,7 @@ int fsl_dcu_drm_encoder_create(struct fsl_dcu_drm_device *fsl_dev,
 	if (fsl_dev->tcon)
 		fsl_tcon_bypass_enable(fsl_dev->tcon);
 
-	ret = drm_simple_encoder_init(fsl_dev->drm, encoder,
-				      DRM_MODE_ENCODER_LVDS);
+	ret = drm_simple_encoder_init(drm, encoder, DRM_MODE_ENCODER_LVDS);
 	if (ret < 0)
 		return ret;
 
@@ -79,12 +79,12 @@ static int fsl_dcu_attach_panel(struct fsl_dcu_drm_device *fsl_dev,
 {
 	struct drm_encoder *encoder = &fsl_dev->encoder;
 	struct drm_connector *connector = &fsl_dev->connector.base;
+	struct drm_device *drm = &fsl_dev->base;
 	int ret;
 
 	fsl_dev->connector.encoder = encoder;
 
-	ret = drm_connector_init(fsl_dev->drm, connector,
-				 &fsl_dcu_drm_connector_funcs,
+	ret = drm_connector_init(drm, connector, &fsl_dcu_drm_connector_funcs,
 				 DRM_MODE_CONNECTOR_LVDS);
 	if (ret < 0)
 		return ret;
Signature · 1 line
2.37.3
#2 lore raw

PATCH 2/8 drm/fsl-dcu: replace drm->dev_private with drm_to_fsl_dcu_drm_dev()

In reply to Danilo Krummrich

Using drm_device->dev_private is deprecated. Since we've switched to devm_drm_dev_alloc(), struct drm_device is now embedded in struct malidp_drm, hence we can use container_of() to get the struct drm_device instance instead.

---
 drivers/gpu/drm/fsl-dcu/fsl_dcu_drm_crtc.c  | 12 ++++++------
 drivers/gpu/drm/fsl-dcu/fsl_dcu_drm_drv.c   | 13 ++++---------
 drivers/gpu/drm/fsl-dcu/fsl_dcu_drm_drv.h   |  2 ++
 drivers/gpu/drm/fsl-dcu/fsl_dcu_drm_plane.c |  8 ++++----
 4 files changed, 16 insertions(+), 19 deletions(-)
drivers/gpu/drm/fsl-dcu/fsl_dcu_drm_crtc.c +6 -6
index a1b8ce70928a..e05311e2b0e0 100644
@@ -24,7 +24,7 @@ static void fsl_dcu_drm_crtc_atomic_flush(struct drm_crtc *crtc,
 					  struct drm_atomic_state *state)
 {
 	struct drm_device *dev = crtc->dev;
-	struct fsl_dcu_drm_device *fsl_dev = dev->dev_private;
+	struct fsl_dcu_drm_device *fsl_dev = drm_to_fsl_dcu_drm_dev(dev);
 	struct drm_pending_vblank_event *event = crtc->state->event;
 
 	regmap_write(fsl_dev->regmap,
@@ -48,7 +48,7 @@ static void fsl_dcu_drm_crtc_atomic_disable(struct drm_crtc *crtc,
 	struct drm_crtc_state *old_crtc_state = drm_atomic_get_old_crtc_state(state,
 									      crtc);
 	struct drm_device *dev = crtc->dev;
-	struct fsl_dcu_drm_device *fsl_dev = dev->dev_private;
+	struct fsl_dcu_drm_device *fsl_dev = drm_to_fsl_dcu_drm_dev(dev);
 
 	/* always disable planes on the CRTC */
 	drm_atomic_helper_disable_planes_on_crtc(old_crtc_state, true);
@@ -67,7 +67,7 @@ static void fsl_dcu_drm_crtc_atomic_enable(struct drm_crtc *crtc,
 					   struct drm_atomic_state *state)
 {
 	struct drm_device *dev = crtc->dev;
-	struct fsl_dcu_drm_device *fsl_dev = dev->dev_private;
+	struct fsl_dcu_drm_device *fsl_dev = drm_to_fsl_dcu_drm_dev(dev);
 
 	clk_prepare_enable(fsl_dev->pix_clk);
 	regmap_update_bits(fsl_dev->regmap, DCU_DCU_MODE,
@@ -82,7 +82,7 @@ static void fsl_dcu_drm_crtc_atomic_enable(struct drm_crtc *crtc,
 static void fsl_dcu_drm_crtc_mode_set_nofb(struct drm_crtc *crtc)
 {
 	struct drm_device *dev = crtc->dev;
-	struct fsl_dcu_drm_device *fsl_dev = dev->dev_private;
+	struct fsl_dcu_drm_device *fsl_dev = drm_to_fsl_dcu_drm_dev(dev);
 	struct drm_connector *con = &fsl_dev->connector.base;
 	struct drm_display_mode *mode = &crtc->state->mode;
 	unsigned int pol = 0;
@@ -135,7 +135,7 @@ static const struct drm_crtc_helper_funcs fsl_dcu_drm_crtc_helper_funcs = {
 static int fsl_dcu_drm_crtc_enable_vblank(struct drm_crtc *crtc)
 {
 	struct drm_device *dev = crtc->dev;
-	struct fsl_dcu_drm_device *fsl_dev = dev->dev_private;
+	struct fsl_dcu_drm_device *fsl_dev = drm_to_fsl_dcu_drm_dev(dev);
 	unsigned int value;
 
 	regmap_read(fsl_dev->regmap, DCU_INT_MASK, &value);
@@ -148,7 +148,7 @@ static int fsl_dcu_drm_crtc_enable_vblank(struct drm_crtc *crtc)
 static void fsl_dcu_drm_crtc_disable_vblank(struct drm_crtc *crtc)
 {
 	struct drm_device *dev = crtc->dev;
-	struct fsl_dcu_drm_device *fsl_dev = dev->dev_private;
+	struct fsl_dcu_drm_device *fsl_dev = drm_to_fsl_dcu_drm_dev(dev);
 	unsigned int value;
 
 	regmap_read(fsl_dev->regmap, DCU_INT_MASK, &value);
drivers/gpu/drm/fsl-dcu/fsl_dcu_drm_drv.c +4 -9
index a47b72995fcf..4139f674c5de 100644
@@ -52,7 +52,7 @@ static const struct regmap_config fsl_dcu_regmap_config = {
 
 static void fsl_dcu_irq_reset(struct drm_device *dev)
 {
-	struct fsl_dcu_drm_device *fsl_dev = dev->dev_private;
+	struct fsl_dcu_drm_device *fsl_dev = drm_to_fsl_dcu_drm_dev(dev);
 
 	regmap_write(fsl_dev->regmap, DCU_INT_STATUS, ~0);
 	regmap_write(fsl_dev->regmap, DCU_INT_MASK, ~0);
@@ -61,7 +61,7 @@ static void fsl_dcu_irq_reset(struct drm_device *dev)
 static irqreturn_t fsl_dcu_drm_irq(int irq, void *arg)
 {
 	struct drm_device *dev = arg;
-	struct fsl_dcu_drm_device *fsl_dev = dev->dev_private;
+	struct fsl_dcu_drm_device *fsl_dev = drm_to_fsl_dcu_drm_dev(dev);
 	unsigned int int_status;
 	int ret;
 
@@ -91,7 +91,7 @@ static int fsl_dcu_irq_install(struct drm_device *dev, unsigned int irq)
 
 static void fsl_dcu_irq_uninstall(struct drm_device *dev)
 {
-	struct fsl_dcu_drm_device *fsl_dev = dev->dev_private;
+	struct fsl_dcu_drm_device *fsl_dev = drm_to_fsl_dcu_drm_dev(dev);
 
 	fsl_dcu_irq_reset(dev);
 	free_irq(fsl_dev->irq, dev);
@@ -99,7 +99,7 @@ static void fsl_dcu_irq_uninstall(struct drm_device *dev)
 
 static int fsl_dcu_load(struct drm_device *dev, unsigned long flags)
 {
-	struct fsl_dcu_drm_device *fsl_dev = dev->dev_private;
+	struct fsl_dcu_drm_device *fsl_dev = drm_to_fsl_dcu_drm_dev(dev);
 	int ret;
 
 	ret = fsl_dcu_drm_modeset_init(fsl_dev);
@@ -133,8 +133,6 @@ static int fsl_dcu_load(struct drm_device *dev, unsigned long flags)
 
 	drm_mode_config_cleanup(dev);
 done_vblank:
-	dev->dev_private = NULL;
-
 	return ret;
 }
 
@@ -145,8 +143,6 @@ static void fsl_dcu_unload(struct drm_device *dev)
 
 	drm_mode_config_cleanup(dev);
 	fsl_dcu_irq_uninstall(dev);
-
-	dev->dev_private = NULL;
 }
 
 DEFINE_DRM_GEM_DMA_FOPS(fsl_dcu_drm_fops);
@@ -322,7 +318,6 @@ static int fsl_dcu_drm_probe(struct platform_device *pdev)
 
 	fsl_dev->dev = dev;
 	fsl_dev->np = dev->of_node;
-	drm->dev_private = fsl_dev;
 	dev_set_drvdata(dev, fsl_dev);
 
 	ret = drm_dev_register(drm, 0);
drivers/gpu/drm/fsl-dcu/fsl_dcu_drm_drv.h +2 -0
index 20ca13ff618a..5b61d443da96 100644
@@ -193,6 +193,8 @@ struct fsl_dcu_drm_device {
 	const struct fsl_dcu_soc_data *soc;
 };
 
+#define drm_to_fsl_dcu_drm_dev(x) container_of(x, struct fsl_dcu_drm_device, base)
+
 int fsl_dcu_drm_modeset_init(struct fsl_dcu_drm_device *fsl_dev);
 
 #endif /* __FSL_DCU_DRM_DRV_H__ */
drivers/gpu/drm/fsl-dcu/fsl_dcu_drm_plane.c +4 -4
index 794a87d16f88..91865956da02 100644
@@ -22,7 +22,7 @@
 
 static int fsl_dcu_drm_plane_index(struct drm_plane *plane)
 {
-	struct fsl_dcu_drm_device *fsl_dev = plane->dev->dev_private;
+	struct fsl_dcu_drm_device *fsl_dev = drm_to_fsl_dcu_drm_dev(plane->dev);
 	unsigned int total_layer = fsl_dev->soc->total_layer;
 	unsigned int index;
 
@@ -63,7 +63,7 @@ static int fsl_dcu_drm_plane_atomic_check(struct drm_plane *plane,
 static void fsl_dcu_drm_plane_atomic_disable(struct drm_plane *plane,
 					     struct drm_atomic_state *state)
 {
-	struct fsl_dcu_drm_device *fsl_dev = plane->dev->dev_private;
+	struct fsl_dcu_drm_device *fsl_dev = drm_to_fsl_dcu_drm_dev(plane->dev);
 	unsigned int value;
 	int index;
 
@@ -80,7 +80,7 @@ static void fsl_dcu_drm_plane_atomic_update(struct drm_plane *plane,
 					    struct drm_atomic_state *state)
 
 {
-	struct fsl_dcu_drm_device *fsl_dev = plane->dev->dev_private;
+	struct fsl_dcu_drm_device *fsl_dev = drm_to_fsl_dcu_drm_dev(plane->dev);
 	struct drm_plane_state *new_state = drm_atomic_get_new_plane_state(state,
 									   plane);
 	struct drm_framebuffer *fb = plane->state->fb;
@@ -194,7 +194,7 @@ static const u32 fsl_dcu_drm_plane_formats[] = {
 
 void fsl_dcu_drm_init_planes(struct drm_device *dev)
 {
-	struct fsl_dcu_drm_device *fsl_dev = dev->dev_private;
+	struct fsl_dcu_drm_device *fsl_dev = drm_to_fsl_dcu_drm_dev(dev);
 	int i, j;
 
 	for (i = 0; i < fsl_dev->soc->total_layer; i++) {
Signature · 1 line
2.37.3
#3 lore raw

PATCH 3/8 drm/fsl-dcu: crtc: use drmm_crtc_init_with_planes()

In reply to Danilo Krummrich

Use drmm_crtc_init_with_planes() instead of drm_crtc_init_with_planes() to get rid of the explicit destroy hook in struct drm_plane_funcs.

---
 drivers/gpu/drm/fsl-dcu/fsl_dcu_drm_crtc.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
drivers/gpu/drm/fsl-dcu/fsl_dcu_drm_crtc.c +2 -3
index e05311e2b0e0..0b70624260fc 100644
@@ -159,7 +159,6 @@ static void fsl_dcu_drm_crtc_disable_vblank(struct drm_crtc *crtc)
 static const struct drm_crtc_funcs fsl_dcu_drm_crtc_funcs = {
 	.atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
 	.atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
-	.destroy = drm_crtc_cleanup,
 	.page_flip = drm_atomic_helper_page_flip,
 	.reset = drm_atomic_helper_crtc_reset,
 	.set_config = drm_atomic_helper_set_config,
@@ -180,8 +179,8 @@ int fsl_dcu_drm_crtc_create(struct fsl_dcu_drm_device *fsl_dev)
 	if (!primary)
 		return -ENOMEM;
 
-	ret = drm_crtc_init_with_planes(drm, crtc, primary, NULL,
-					&fsl_dcu_drm_crtc_funcs, NULL);
+	ret = drmm_crtc_init_with_planes(drm, crtc, primary, NULL,
+					 &fsl_dcu_drm_crtc_funcs, NULL);
 	if (ret) {
 		primary->funcs->destroy(primary);
 		return ret;
Signature · 1 line
2.37.3
#4 lore raw

PATCH 4/8 drm/fsl-dcu: plane: use drm managed resources

In reply to Danilo Krummrich

Use drm managed resource allocation (drmm_universal_plane_alloc()) in order to get rid of the explicit destroy hook in struct drm_plane_funcs.

---
 drivers/gpu/drm/fsl-dcu/fsl_dcu_drm_crtc.c  |  4 ++--
 drivers/gpu/drm/fsl-dcu/fsl_dcu_drm_plane.c | 25 ++++++++-------------
 2 files changed, 11 insertions(+), 18 deletions(-)
drivers/gpu/drm/fsl-dcu/fsl_dcu_drm_crtc.c +2 -2
index 0b70624260fc..1dad90f701c8 100644
@@ -176,8 +176,8 @@ int fsl_dcu_drm_crtc_create(struct fsl_dcu_drm_device *fsl_dev)
 	fsl_dcu_drm_init_planes(drm);
 
 	primary = fsl_dcu_drm_primary_create_plane(drm);
-	if (!primary)
-		return -ENOMEM;
+	if (IS_ERR(primary))
+		return PTR_ERR(primary);
 
 	ret = drmm_crtc_init_with_planes(drm, crtc, primary, NULL,
 					 &fsl_dcu_drm_crtc_funcs, NULL);
drivers/gpu/drm/fsl-dcu/fsl_dcu_drm_plane.c +9 -16
index 91865956da02..23ff285da477 100644
@@ -174,7 +174,6 @@ static const struct drm_plane_helper_funcs fsl_dcu_drm_plane_helper_funcs = {
 static const struct drm_plane_funcs fsl_dcu_drm_plane_funcs = {
 	.atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
 	.atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
-	.destroy = drm_plane_helper_destroy,
 	.disable_plane = drm_atomic_helper_disable_plane,
 	.reset = drm_atomic_helper_plane_reset,
 	.update_plane = drm_atomic_helper_update_plane,
@@ -206,24 +205,18 @@ void fsl_dcu_drm_init_planes(struct drm_device *dev)
 struct drm_plane *fsl_dcu_drm_primary_create_plane(struct drm_device *dev)
 {
 	struct drm_plane *primary;
-	int ret;
-
-	primary = kzalloc(sizeof(*primary), GFP_KERNEL);
-	if (!primary) {
-		DRM_DEBUG_KMS("Failed to allocate primary plane\n");
-		return NULL;
-	}
 
 	/* possible_crtc's will be filled in later by crtc_init */
-	ret = drm_universal_plane_init(dev, primary, 0,
-				       &fsl_dcu_drm_plane_funcs,
-				       fsl_dcu_drm_plane_formats,
-				       ARRAY_SIZE(fsl_dcu_drm_plane_formats),
-				       NULL, DRM_PLANE_TYPE_PRIMARY, NULL);
-	if (ret) {
-		kfree(primary);
-		primary = NULL;
+	primary = drmm_universal_plane_alloc(dev, struct drm_plane, dev, 0,
+					     &fsl_dcu_drm_plane_funcs,
+					     fsl_dcu_drm_plane_formats,
+					     ARRAY_SIZE(fsl_dcu_drm_plane_formats),
+					     NULL, DRM_PLANE_TYPE_PRIMARY, NULL);
+	if (IS_ERR(primary)) {
+		DRM_DEBUG_KMS("Failed to create primary plane\n");
+		return primary;
 	}
+
 	drm_plane_helper_add(primary, &fsl_dcu_drm_plane_helper_funcs);
 
 	return primary;
Signature · 1 line
2.37.3
#5 lore raw

PATCH 5/8 drm/fsl-dcu: use drm_dev_unplug()

In reply to Danilo Krummrich

When the driver is unbound, there might still be users in userspace having an open fd and are calling into the driver.

While this is fine for drm managed resources, it is not for resources bound to the device/driver lifecycle, e.g. clocks or MMIO mappings.

To prevent use-after-free issues we need to protect those resources with drm_dev_enter() and drm_dev_exit(). This does only work if we indicate that the drm device was unplugged, hence use drm_dev_unplug() instead of drm_dev_unregister().

Protecting the particular resources with drm_dev_enter()/drm_dev_exit() is handled by subsequent patches.

---
 drivers/gpu/drm/fsl-dcu/fsl_dcu_drm_drv.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/fsl-dcu/fsl_dcu_drm_drv.c +1 -1
index 4139f674c5de..3ac57516c3fe 100644
@@ -340,7 +340,7 @@ static int fsl_dcu_drm_remove(struct platform_device *pdev)
 	struct fsl_dcu_drm_device *fsl_dev = platform_get_drvdata(pdev);
 	struct drm_device *drm = &fsl_dev->base;
 
-	drm_dev_unregister(drm);
+	drm_dev_unplug(drm);
 	clk_disable_unprepare(fsl_dev->clk);
 	clk_unregister(fsl_dev->pix_clk);
 
Signature · 1 line
2.37.3
#6 lore raw

PATCH 6/8 drm/fsl-dcu: plane: protect device resources after removal

In reply to Danilo Krummrich

(Hardware) resources which are bound to the driver and device lifecycle must not be accessed after the device and driver are unbound.

However, the DRM device isn't freed as long as the last user didn't close it, hence userspace can still call into the driver.

Therefore protect the critical sections which are accessing those resources with drm_dev_enter() and drm_dev_exit().

---
 drivers/gpu/drm/fsl-dcu/fsl_dcu_drm_plane.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)
drivers/gpu/drm/fsl-dcu/fsl_dcu_drm_plane.c +12 -2
index 23ff285da477..1be3062a95df 100644
@@ -10,6 +10,7 @@
 #include <drm/drm_atomic.h>
 #include <drm/drm_atomic_helper.h>
 #include <drm/drm_crtc.h>
+#include <drm/drm_drv.h>
 #include <drm/drm_fb_dma_helper.h>
 #include <drm/drm_fourcc.h>
 #include <drm/drm_framebuffer.h>
@@ -65,7 +66,10 @@ static void fsl_dcu_drm_plane_atomic_disable(struct drm_plane *plane,
 {
 	struct fsl_dcu_drm_device *fsl_dev = drm_to_fsl_dcu_drm_dev(plane->dev);
 	unsigned int value;
-	int index;
+	int index, idx;
+
+	if (!drm_dev_enter(plane->dev, &idx))
+		return;
 
 	index = fsl_dcu_drm_plane_index(plane);
 	if (index < 0)
@@ -74,6 +78,8 @@ static void fsl_dcu_drm_plane_atomic_disable(struct drm_plane *plane,
 	regmap_read(fsl_dev->regmap, DCU_CTRLDESCLN(index, 4), &value);
 	value &= ~DCU_LAYER_EN;
 	regmap_write(fsl_dev->regmap, DCU_CTRLDESCLN(index, 4), value);
+
+	drm_dev_exit(idx);
 }
 
 static void fsl_dcu_drm_plane_atomic_update(struct drm_plane *plane,
@@ -86,7 +92,10 @@ static void fsl_dcu_drm_plane_atomic_update(struct drm_plane *plane,
 	struct drm_framebuffer *fb = plane->state->fb;
 	struct drm_gem_dma_object *gem;
 	unsigned int alpha = DCU_LAYER_AB_NONE, bpp;
-	int index;
+	int index, idx;
+
+	if (!drm_dev_enter(plane->dev, &idx))
+		return;
 
 	if (!fb)
 		return;
@@ -162,6 +171,7 @@ static void fsl_dcu_drm_plane_atomic_update(struct drm_plane *plane,
 			     DCU_LAYER_PRE_SKIP(0));
 	}
 
+	drm_dev_exit(idx);
 	return;
 }
 
Signature · 1 line
2.37.3
#7 lore raw

PATCH 7/8 drm/fsl-dcu: crtc: protect device resources after removal

In reply to Danilo Krummrich

(Hardware) resources which are bound to the driver and device lifecycle must not be accessed after the device and driver are unbound.

However, the DRM device isn't freed as long as the last user didn't close it, hence userspace can still call into the driver.

Therefore protect the critical sections which are accessing those resources with drm_dev_enter() and drm_dev_exit().

---
 drivers/gpu/drm/fsl-dcu/fsl_dcu_drm_crtc.c | 37 ++++++++++++++++++++++
 1 file changed, 37 insertions(+)
drivers/gpu/drm/fsl-dcu/fsl_dcu_drm_crtc.c +37 -0
index 1dad90f701c8..c77df9b7893f 100644
@@ -13,6 +13,7 @@
 #include <drm/drm_atomic.h>
 #include <drm/drm_atomic_helper.h>
 #include <drm/drm_crtc.h>
+#include <drm/drm_drv.h>
 #include <drm/drm_probe_helper.h>
 #include <drm/drm_vblank.h>
 
@@ -26,6 +27,10 @@ static void fsl_dcu_drm_crtc_atomic_flush(struct drm_crtc *crtc,
 	struct drm_device *dev = crtc->dev;
 	struct fsl_dcu_drm_device *fsl_dev = drm_to_fsl_dcu_drm_dev(dev);
 	struct drm_pending_vblank_event *event = crtc->state->event;
+	int idx;
+
+	if (!drm_dev_enter(dev, &idx))
+		return;
 
 	regmap_write(fsl_dev->regmap,
 		     DCU_UPDATE_MODE, DCU_UPDATE_MODE_READREG);
@@ -40,6 +45,8 @@ static void fsl_dcu_drm_crtc_atomic_flush(struct drm_crtc *crtc,
 			drm_crtc_send_vblank_event(crtc, event);
 		spin_unlock_irq(&crtc->dev->event_lock);
 	}
+
+	drm_dev_exit(idx);
 }
 
 static void fsl_dcu_drm_crtc_atomic_disable(struct drm_crtc *crtc,
@@ -49,6 +56,10 @@ static void fsl_dcu_drm_crtc_atomic_disable(struct drm_crtc *crtc,
 									      crtc);
 	struct drm_device *dev = crtc->dev;
 	struct fsl_dcu_drm_device *fsl_dev = drm_to_fsl_dcu_drm_dev(dev);
+	int idx;
+
+	if (!drm_dev_enter(dev, &idx))
+		return;
 
 	/* always disable planes on the CRTC */
 	drm_atomic_helper_disable_planes_on_crtc(old_crtc_state, true);
@@ -61,6 +72,8 @@ static void fsl_dcu_drm_crtc_atomic_disable(struct drm_crtc *crtc,
 	regmap_write(fsl_dev->regmap, DCU_UPDATE_MODE,
 		     DCU_UPDATE_MODE_READREG);
 	clk_disable_unprepare(fsl_dev->pix_clk);
+
+	drm_dev_exit(idx);
 }
 
 static void fsl_dcu_drm_crtc_atomic_enable(struct drm_crtc *crtc,
@@ -68,6 +81,10 @@ static void fsl_dcu_drm_crtc_atomic_enable(struct drm_crtc *crtc,
 {
 	struct drm_device *dev = crtc->dev;
 	struct fsl_dcu_drm_device *fsl_dev = drm_to_fsl_dcu_drm_dev(dev);
+	int idx;
+
+	if (!drm_dev_enter(dev, &idx))
+		return;
 
 	clk_prepare_enable(fsl_dev->pix_clk);
 	regmap_update_bits(fsl_dev->regmap, DCU_DCU_MODE,
@@ -77,6 +94,8 @@ static void fsl_dcu_drm_crtc_atomic_enable(struct drm_crtc *crtc,
 		     DCU_UPDATE_MODE_READREG);
 
 	drm_crtc_vblank_on(crtc);
+
+	drm_dev_exit(idx);
 }
 
 static void fsl_dcu_drm_crtc_mode_set_nofb(struct drm_crtc *crtc)
@@ -87,6 +106,10 @@ static void fsl_dcu_drm_crtc_mode_set_nofb(struct drm_crtc *crtc)
 	struct drm_display_mode *mode = &crtc->state->mode;
 	unsigned int pol = 0;
 	struct videomode vm;
+	int idx;
+
+	if (!drm_dev_enter(dev, &idx))
+		return;
 
 	clk_set_rate(fsl_dev->pix_clk, mode->clock * 1000);
 
@@ -122,6 +145,8 @@ static void fsl_dcu_drm_crtc_mode_set_nofb(struct drm_crtc *crtc)
 		     DCU_THRESHOLD_LS_BF_VS(BF_VS_VAL) |
 		     DCU_THRESHOLD_OUT_BUF_HIGH(BUF_MAX_VAL) |
 		     DCU_THRESHOLD_OUT_BUF_LOW(BUF_MIN_VAL));
+
+	drm_dev_exit(idx);
 	return;
 }
 
@@ -137,11 +162,17 @@ static int fsl_dcu_drm_crtc_enable_vblank(struct drm_crtc *crtc)
 	struct drm_device *dev = crtc->dev;
 	struct fsl_dcu_drm_device *fsl_dev = drm_to_fsl_dcu_drm_dev(dev);
 	unsigned int value;
+	int idx;
+
+	if (!drm_dev_enter(dev, &idx))
+		return -ENODEV;
 
 	regmap_read(fsl_dev->regmap, DCU_INT_MASK, &value);
 	value &= ~DCU_INT_MASK_VBLANK;
 	regmap_write(fsl_dev->regmap, DCU_INT_MASK, value);
 
+	drm_dev_exit(idx);
+
 	return 0;
 }
 
@@ -150,10 +181,16 @@ static void fsl_dcu_drm_crtc_disable_vblank(struct drm_crtc *crtc)
 	struct drm_device *dev = crtc->dev;
 	struct fsl_dcu_drm_device *fsl_dev = drm_to_fsl_dcu_drm_dev(dev);
 	unsigned int value;
+	int idx;
+
+	if (!drm_dev_enter(dev, &idx))
+		return;
 
 	regmap_read(fsl_dev->regmap, DCU_INT_MASK, &value);
 	value |= DCU_INT_MASK_VBLANK;
 	regmap_write(fsl_dev->regmap, DCU_INT_MASK, value);
+
+	drm_dev_exit(idx);
 }
 
 static const struct drm_crtc_funcs fsl_dcu_drm_crtc_funcs = {
Signature · 1 line
2.37.3
#8 lore raw

PATCH 8/8 drm/fsl-dcu: remove trailing return statements

In reply to Danilo Krummrich

Remove the trailing return statements at the end of void functions.

---
 drivers/gpu/drm/fsl-dcu/fsl_dcu_drm_crtc.c  | 1 -
 drivers/gpu/drm/fsl-dcu/fsl_dcu_drm_plane.c | 1 -
 2 files changed, 2 deletions(-)
drivers/gpu/drm/fsl-dcu/fsl_dcu_drm_crtc.c +0 -1
index c77df9b7893f..23687551c831 100644
@@ -147,7 +147,6 @@ static void fsl_dcu_drm_crtc_mode_set_nofb(struct drm_crtc *crtc)
 		     DCU_THRESHOLD_OUT_BUF_LOW(BUF_MIN_VAL));
 
 	drm_dev_exit(idx);
-	return;
 }
 
 static const struct drm_crtc_helper_funcs fsl_dcu_drm_crtc_helper_funcs = {
drivers/gpu/drm/fsl-dcu/fsl_dcu_drm_plane.c +0 -1
index 1be3062a95df..d0a14b5b506e 100644
@@ -172,7 +172,6 @@ static void fsl_dcu_drm_plane_atomic_update(struct drm_plane *plane,
 	}
 
 	drm_dev_exit(idx);
-	return;
 }
 
 static const struct drm_plane_helper_funcs fsl_dcu_drm_plane_helper_funcs = {
Signature · 1 line
2.37.3