public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH v6 0/8] drm/vkms: Switch all vkms object to DRM managed objects
@ 2024-12-30 18:37 Louis Chauvet
  2024-12-30 18:37 ` [PATCH v6 1/8] drm/vkms: Switch to managed for connector Louis Chauvet
                   ` (7 more replies)
  0 siblings, 8 replies; 16+ messages in thread
From: Louis Chauvet @ 2024-12-30 18:37 UTC (permalink / raw)
  To: Rodrigo Siqueira, Melissa Wen, Maíra Canal, Haneen Mohammed,
	Maarten Lankhorst, Maxime Ripard, Thomas Zimmermann, David Airlie,
	Simona Vetter, Simona Vetter
  Cc: dri-devel, arthurgrillo, linux-kernel, jeremie.dautheribes,
	miquel.raynal, thomas.petazzoni, seanpaul, nicolejadeyee,
	Louis Chauvet, Maíra Canal, José Expósito

To simplify the memory managment, this series replace all manual drm
object managment by drm-managed one. This way the VKMS code don't have to
manage it directly and the DRM core will handle the object destruction.

No functional changes are intended in this series.

PATCH 1/8: Migrate connector managment to drmm
PATCH 2/8: Migrate encoder managment to drmm
PATCH 3/8: Migrate connector management to drm
PATCH 4/8: Introduce cleanup function for drm_writeback_connector
PATCH 5/8: Create a helper to initialize drm_writeback_connector (common
           part between drmm and normal variants)
PATCH 6/8: Ensure the proper clean of drm_writeback_connector after a
           failure during init
PATCH 7/8: Create the drmm initialization for drm_writeback_connector
PATCH 8/8: Migrate writeback connector management to drm

For the drmm_writeback helpers, you can find some discussions here [3].

[3]:https://lore.kernel.org/all/20240906-writeback-drmm-v1-1-01ede328182c@bootlin.com/

Signed-off-by: Louis Chauvet <louis.chauvet@bootlin.com>
---
Changes in v6:
- Added R-by
- Splited the drmm_writeback_connector init in multiple commits + rework 
  how it is done. This time it should not change the behavior of existing 
  drivers
- Link to v5: https://lore.kernel.org/r/20241122-google-vkms-managed-v5-0-1ab60403e960@bootlin.com

Changes in v5:
- Rebased on drm-misc-next
- Link to v4: https://lore.kernel.org/r/20241010-google-vkms-managed-v4-0-ed04a62ad2e3@bootlin.com

Changes in v4:
- No changes for the managed part
- Add the patch to introduce drmm_writeback helpers
- Link to v3: https://lore.kernel.org/r/20240912-google-vkms-managed-v3-0-7708d6ad262d@bootlin.com

Changes in v3:
- As suggested by Maxime, split the managed and the dynamic allocation
  parts in different series
- To reduce the diff in this series, extract the "remove crtc index" part,
  see https://lore.kernel.org/all/20240906-vkms-remove-index-v1-1-3cfedd8ccb2f@bootlin.com/
- Link to v2: https://lore.kernel.org/r/20240827-google-vkms-managed-v2-0-f41104553aeb@bootlin.com

Changes in v2:
- Applied comments from José
- Extract the rename vkms_output -> vkms_crtc to avoid useless changes in
  the last commit
- Extract the rename to_vkms_crtc_state to
  drm_crtc_state_to_vkms_crtc_state to avoid useless changes in last
  commit
- Extract the drm_mode_crtc_set_gamma_size result check in its own commit
- Rebased on drm-misc/drm-misc-next
- Link to v1: https://lore.kernel.org/r/20240814-google-vkms-managed-v1-0-7ab8b8921103@bootlin.com

---
Louis Chauvet (8):
      drm/vkms: Switch to managed for connector
      drm/vkms: Switch to managed for encoder
      drm/vkms: Switch to managed for crtc
      drm: writeback: Introduce cleanup function
      drm: writeback: Create an helper for drm_writeback_connector initialization
      drm: writeback: Add missing cleanup in case of initialization failure
      drm: writeback: Create drmm variants for drm_writeback_connector initialization
      drm/vkms: Switch to managed for writeback connector

 drivers/gpu/drm/drm_writeback.c       | 208 +++++++++++++++++++++++++++++-----
 drivers/gpu/drm/vkms/vkms_crtc.c      |  14 +++
 drivers/gpu/drm/vkms/vkms_drv.c       |   9 --
 drivers/gpu/drm/vkms/vkms_output.c    |  24 ++--
 drivers/gpu/drm/vkms/vkms_writeback.c |  13 +--
 include/drm/drm_writeback.h           |   8 ++
 6 files changed, 216 insertions(+), 60 deletions(-)
---
base-commit: f8a2397baf041a5cee408b082334bb09c7e161df
change-id: 20240521-google-vkms-managed-4aec99461a77

Best regards,
-- 
Louis Chauvet <louis.chauvet@bootlin.com>


^ permalink raw reply	[flat|nested] 16+ messages in thread

* [PATCH v6 1/8] drm/vkms: Switch to managed for connector
  2024-12-30 18:37 [PATCH v6 0/8] drm/vkms: Switch all vkms object to DRM managed objects Louis Chauvet
@ 2024-12-30 18:37 ` Louis Chauvet
  2024-12-30 18:37 ` [PATCH v6 2/8] drm/vkms: Switch to managed for encoder Louis Chauvet
                   ` (6 subsequent siblings)
  7 siblings, 0 replies; 16+ messages in thread
From: Louis Chauvet @ 2024-12-30 18:37 UTC (permalink / raw)
  To: Rodrigo Siqueira, Melissa Wen, Maíra Canal, Haneen Mohammed,
	Maarten Lankhorst, Maxime Ripard, Thomas Zimmermann, David Airlie,
	Simona Vetter, Simona Vetter
  Cc: dri-devel, arthurgrillo, linux-kernel, jeremie.dautheribes,
	miquel.raynal, thomas.petazzoni, seanpaul, nicolejadeyee,
	Louis Chauvet, Maíra Canal

The current VKMS driver uses non-managed function to create connectors. It
is not an issue yet, but in order to support multiple devices easily,
convert this code to use drm and device managed helpers.

Reviewed-by: Maxime Ripard <mripard@kernel.org>
Reviewed-by: Maíra Canal <mcanal@igalia.com>
Signed-off-by: Louis Chauvet <louis.chauvet@bootlin.com>
---
 drivers/gpu/drm/vkms/vkms_output.c | 12 ++++--------
 1 file changed, 4 insertions(+), 8 deletions(-)

diff --git a/drivers/gpu/drm/vkms/vkms_output.c b/drivers/gpu/drm/vkms/vkms_output.c
index 8f4bd5aef087b459d37d0cbbf90fe0145090917a..570823ecb28f589e6323036590ec05a2f633bc9b 100644
--- a/drivers/gpu/drm/vkms/vkms_output.c
+++ b/drivers/gpu/drm/vkms/vkms_output.c
@@ -3,11 +3,11 @@
 #include "vkms_drv.h"
 #include <drm/drm_atomic_helper.h>
 #include <drm/drm_edid.h>
+#include <drm/drm_managed.h>
 #include <drm/drm_probe_helper.h>
 
 static const struct drm_connector_funcs vkms_connector_funcs = {
 	.fill_modes = drm_helper_probe_single_connector_modes,
-	.destroy = drm_connector_cleanup,
 	.reset = drm_atomic_helper_connector_reset,
 	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
@@ -75,8 +75,8 @@ int vkms_output_init(struct vkms_device *vkmsdev)
 		}
 	}
 
-	ret = drm_connector_init(dev, connector, &vkms_connector_funcs,
-				 DRM_MODE_CONNECTOR_VIRTUAL);
+	ret = drmm_connector_init(dev, connector, &vkms_connector_funcs,
+				  DRM_MODE_CONNECTOR_VIRTUAL, NULL);
 	if (ret) {
 		DRM_ERROR("Failed to init connector\n");
 		return ret;
@@ -88,7 +88,7 @@ int vkms_output_init(struct vkms_device *vkmsdev)
 			       DRM_MODE_ENCODER_VIRTUAL, NULL);
 	if (ret) {
 		DRM_ERROR("Failed to init encoder\n");
-		goto err_encoder;
+		return ret;
 	}
 	encoder->possible_crtcs = drm_crtc_mask(crtc);
 
@@ -110,9 +110,5 @@ int vkms_output_init(struct vkms_device *vkmsdev)
 
 err_attach:
 	drm_encoder_cleanup(encoder);
-
-err_encoder:
-	drm_connector_cleanup(connector);
-
 	return ret;
 }

-- 
2.47.1


^ permalink raw reply related	[flat|nested] 16+ messages in thread

* [PATCH v6 2/8] drm/vkms: Switch to managed for encoder
  2024-12-30 18:37 [PATCH v6 0/8] drm/vkms: Switch all vkms object to DRM managed objects Louis Chauvet
  2024-12-30 18:37 ` [PATCH v6 1/8] drm/vkms: Switch to managed for connector Louis Chauvet
@ 2024-12-30 18:37 ` Louis Chauvet
  2024-12-30 18:37 ` [PATCH v6 3/8] drm/vkms: Switch to managed for crtc Louis Chauvet
                   ` (5 subsequent siblings)
  7 siblings, 0 replies; 16+ messages in thread
From: Louis Chauvet @ 2024-12-30 18:37 UTC (permalink / raw)
  To: Rodrigo Siqueira, Melissa Wen, Maíra Canal, Haneen Mohammed,
	Maarten Lankhorst, Maxime Ripard, Thomas Zimmermann, David Airlie,
	Simona Vetter, Simona Vetter
  Cc: dri-devel, arthurgrillo, linux-kernel, jeremie.dautheribes,
	miquel.raynal, thomas.petazzoni, seanpaul, nicolejadeyee,
	Louis Chauvet, Maíra Canal

The current VKMS driver uses non-managed function to create encoders. It
is not an issue yet, but in order to support multiple devices easily,
convert this code to use drm and device managed helpers.

Reviewed-by: Maxime Ripard <mripard@kernel.org>
Reviewed-by: Maíra Canal <mcanal@igalia.com>
Signed-off-by: Louis Chauvet <louis.chauvet@bootlin.com>
---
 drivers/gpu/drm/vkms/vkms_output.c | 12 +++---------
 1 file changed, 3 insertions(+), 9 deletions(-)

diff --git a/drivers/gpu/drm/vkms/vkms_output.c b/drivers/gpu/drm/vkms/vkms_output.c
index 570823ecb28f589e6323036590ec05a2f633bc9b..ab9affa75b66ce9f00fe025052439405206144ec 100644
--- a/drivers/gpu/drm/vkms/vkms_output.c
+++ b/drivers/gpu/drm/vkms/vkms_output.c
@@ -13,10 +13,6 @@ static const struct drm_connector_funcs vkms_connector_funcs = {
 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
 };
 
-static const struct drm_encoder_funcs vkms_encoder_funcs = {
-	.destroy = drm_encoder_cleanup,
-};
-
 static int vkms_conn_get_modes(struct drm_connector *connector)
 {
 	int count;
@@ -84,8 +80,8 @@ int vkms_output_init(struct vkms_device *vkmsdev)
 
 	drm_connector_helper_add(connector, &vkms_conn_helper_funcs);
 
-	ret = drm_encoder_init(dev, encoder, &vkms_encoder_funcs,
-			       DRM_MODE_ENCODER_VIRTUAL, NULL);
+	ret = drmm_encoder_init(dev, encoder, NULL,
+				DRM_MODE_ENCODER_VIRTUAL, NULL);
 	if (ret) {
 		DRM_ERROR("Failed to init encoder\n");
 		return ret;
@@ -95,7 +91,7 @@ int vkms_output_init(struct vkms_device *vkmsdev)
 	ret = drm_connector_attach_encoder(connector, encoder);
 	if (ret) {
 		DRM_ERROR("Failed to attach connector to encoder\n");
-		goto err_attach;
+		return ret;
 	}
 
 	if (vkmsdev->config->writeback) {
@@ -108,7 +104,5 @@ int vkms_output_init(struct vkms_device *vkmsdev)
 
 	return 0;
 
-err_attach:
-	drm_encoder_cleanup(encoder);
 	return ret;
 }

-- 
2.47.1


^ permalink raw reply related	[flat|nested] 16+ messages in thread

* [PATCH v6 3/8] drm/vkms: Switch to managed for crtc
  2024-12-30 18:37 [PATCH v6 0/8] drm/vkms: Switch all vkms object to DRM managed objects Louis Chauvet
  2024-12-30 18:37 ` [PATCH v6 1/8] drm/vkms: Switch to managed for connector Louis Chauvet
  2024-12-30 18:37 ` [PATCH v6 2/8] drm/vkms: Switch to managed for encoder Louis Chauvet
@ 2024-12-30 18:37 ` Louis Chauvet
  2024-12-30 18:37 ` [PATCH v6 4/8] drm: writeback: Introduce cleanup function Louis Chauvet
                   ` (4 subsequent siblings)
  7 siblings, 0 replies; 16+ messages in thread
From: Louis Chauvet @ 2024-12-30 18:37 UTC (permalink / raw)
  To: Rodrigo Siqueira, Melissa Wen, Maíra Canal, Haneen Mohammed,
	Maarten Lankhorst, Maxime Ripard, Thomas Zimmermann, David Airlie,
	Simona Vetter, Simona Vetter
  Cc: dri-devel, arthurgrillo, linux-kernel, jeremie.dautheribes,
	miquel.raynal, thomas.petazzoni, seanpaul, nicolejadeyee,
	Louis Chauvet, Maíra Canal

The current VKMS driver uses managed function to create crtc, but
don't use it to properly clean the crtc workqueue. It is not an
issue yet, but in order to support multiple devices easily,
convert this code to use drm and device managed helpers.

Reviewed-by: Maxime Ripard <mripard@kernel.org>
Reviewed-by: Maíra Canal <mcanal@igalia.com>
Signed-off-by: Louis Chauvet <louis.chauvet@bootlin.com>
---
 drivers/gpu/drm/vkms/vkms_crtc.c | 14 ++++++++++++++
 drivers/gpu/drm/vkms/vkms_drv.c  |  9 ---------
 2 files changed, 14 insertions(+), 9 deletions(-)

diff --git a/drivers/gpu/drm/vkms/vkms_crtc.c b/drivers/gpu/drm/vkms/vkms_crtc.c
index 28a57ae109fcc05af3fe74f94518c462c09119e3..ace8d293f7da611110c1e117b6cf2f3c9e9b4381 100644
--- a/drivers/gpu/drm/vkms/vkms_crtc.c
+++ b/drivers/gpu/drm/vkms/vkms_crtc.c
@@ -6,6 +6,7 @@
 #include <drm/drm_atomic_helper.h>
 #include <drm/drm_probe_helper.h>
 #include <drm/drm_vblank.h>
+#include <drm/drm_managed.h>
 
 #include "vkms_drv.h"
 
@@ -270,6 +271,14 @@ static const struct drm_crtc_helper_funcs vkms_crtc_helper_funcs = {
 	.atomic_disable	= vkms_crtc_atomic_disable,
 };
 
+static void vkms_crtc_destroy_workqueue(struct drm_device *dev,
+					void *raw_vkms_out)
+{
+	struct vkms_output *vkms_out = raw_vkms_out;
+
+	destroy_workqueue(vkms_out->composer_workq);
+}
+
 int vkms_crtc_init(struct drm_device *dev, struct drm_crtc *crtc,
 		   struct drm_plane *primary, struct drm_plane *cursor)
 {
@@ -300,5 +309,10 @@ int vkms_crtc_init(struct drm_device *dev, struct drm_crtc *crtc,
 	if (!vkms_out->composer_workq)
 		return -ENOMEM;
 
+	ret = drmm_add_action_or_reset(dev, vkms_crtc_destroy_workqueue,
+				       vkms_out);
+	if (ret)
+		return ret;
+
 	return ret;
 }
diff --git a/drivers/gpu/drm/vkms/vkms_drv.c b/drivers/gpu/drm/vkms/vkms_drv.c
index e0409aba93496932b32a130ebb608ee53b1a9c59..7c142bfc3bd9de9556621db3e7f570dc0a4fab3a 100644
--- a/drivers/gpu/drm/vkms/vkms_drv.c
+++ b/drivers/gpu/drm/vkms/vkms_drv.c
@@ -53,14 +53,6 @@ MODULE_PARM_DESC(enable_overlay, "Enable/Disable overlay support");
 
 DEFINE_DRM_GEM_FOPS(vkms_driver_fops);
 
-static void vkms_release(struct drm_device *dev)
-{
-	struct vkms_device *vkms = drm_device_to_vkms_device(dev);
-
-	if (vkms->output.composer_workq)
-		destroy_workqueue(vkms->output.composer_workq);
-}
-
 static void vkms_atomic_commit_tail(struct drm_atomic_state *old_state)
 {
 	struct drm_device *dev = old_state->dev;
@@ -108,7 +100,6 @@ static const struct drm_debugfs_info vkms_config_debugfs_list[] = {
 
 static const struct drm_driver vkms_driver = {
 	.driver_features	= DRIVER_MODESET | DRIVER_ATOMIC | DRIVER_GEM,
-	.release		= vkms_release,
 	.fops			= &vkms_driver_fops,
 	DRM_GEM_SHMEM_DRIVER_OPS,
 	DRM_FBDEV_SHMEM_DRIVER_OPS,

-- 
2.47.1


^ permalink raw reply related	[flat|nested] 16+ messages in thread

* [PATCH v6 4/8] drm: writeback: Introduce cleanup function
  2024-12-30 18:37 [PATCH v6 0/8] drm/vkms: Switch all vkms object to DRM managed objects Louis Chauvet
                   ` (2 preceding siblings ...)
  2024-12-30 18:37 ` [PATCH v6 3/8] drm/vkms: Switch to managed for crtc Louis Chauvet
@ 2024-12-30 18:37 ` Louis Chauvet
  2025-01-06  9:43   ` Maxime Ripard
  2024-12-30 18:37 ` [PATCH v6 5/8] drm: writeback: Create an helper for drm_writeback_connector initialization Louis Chauvet
                   ` (3 subsequent siblings)
  7 siblings, 1 reply; 16+ messages in thread
From: Louis Chauvet @ 2024-12-30 18:37 UTC (permalink / raw)
  To: Rodrigo Siqueira, Melissa Wen, Maíra Canal, Haneen Mohammed,
	Maarten Lankhorst, Maxime Ripard, Thomas Zimmermann, David Airlie,
	Simona Vetter, Simona Vetter
  Cc: dri-devel, arthurgrillo, linux-kernel, jeremie.dautheribes,
	miquel.raynal, thomas.petazzoni, seanpaul, nicolejadeyee,
	Louis Chauvet

Currently there is no cleanup function for writeback connectors. To allows
implementation of drmm variant of writeback connector, create a cleanup
function that can be used to properly remove all the writeback-specific
properties and allocations.

This also introduce an helper to cleanup only the drm_writeback_connector
properties, so it can be used during initialization to cleanup in case of
failure.

Signed-off-by: Louis Chauvet <louis.chauvet@bootlin.com>
---
 drivers/gpu/drm/drm_writeback.c | 43 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 43 insertions(+)

diff --git a/drivers/gpu/drm/drm_writeback.c b/drivers/gpu/drm/drm_writeback.c
index 33a3c98a962d1ec49ac4b353902036cf74290ae6..c274cba257cde5f4b446df3854974e690c60bf7b 100644
--- a/drivers/gpu/drm/drm_writeback.c
+++ b/drivers/gpu/drm/drm_writeback.c
@@ -15,6 +15,7 @@
 #include <drm/drm_device.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_property.h>
 #include <drm/drm_writeback.h>
@@ -140,6 +141,22 @@ static int create_writeback_properties(struct drm_device *dev)
 	return 0;
 }
 
+static void delete_writeback_properties(struct drm_device *dev)
+{
+	if (dev->mode_config.writeback_pixel_formats_property) {
+		drm_property_destroy(dev, dev->mode_config.writeback_pixel_formats_property);
+		dev->mode_config.writeback_pixel_formats_property = NULL;
+	}
+	if (dev->mode_config.writeback_out_fence_ptr_property) {
+		drm_property_destroy(dev, dev->mode_config.writeback_out_fence_ptr_property);
+		dev->mode_config.writeback_out_fence_ptr_property = NULL;
+	}
+	if (dev->mode_config.writeback_fb_id_property) {
+		drm_property_destroy(dev, dev->mode_config.writeback_fb_id_property);
+		dev->mode_config.writeback_fb_id_property = NULL;
+	}
+}
+
 static const struct drm_encoder_funcs drm_writeback_encoder_funcs = {
 	.destroy = drm_encoder_cleanup,
 };
@@ -284,6 +301,32 @@ int drm_writeback_connector_init_with_encoder(struct drm_device *dev,
 }
 EXPORT_SYMBOL(drm_writeback_connector_init_with_encoder);
 
+/**
+ * drm_writeback_connector_cleanup - Cleanup the writeback connector
+ * @dev: DRM device
+ * @wb_connector: Pointer to the writeback connector to clean up
+ *
+ * This will decrement the reference counter of blobs and destroy properties. It
+ * will also clean the remaining jobs in this writeback connector. Caution: This helper will not
+ * clean up the attached encoder and the drm_connector.
+ */
+static void drm_writeback_connector_cleanup(struct drm_device *dev,
+					    struct drm_writeback_connector *wb_connector)
+{
+	unsigned long flags;
+	struct drm_writeback_job *pos, *n;
+
+	delete_writeback_properties(dev);
+	drm_property_blob_put(wb_connector->pixel_formats_blob_ptr);
+
+	spin_lock_irqsave(&wb_connector->job_lock, flags);
+	list_for_each_entry_safe(pos, n, &wb_connector->job_queue, list_entry) {
+		drm_writeback_cleanup_job(pos);
+		list_del(&pos->list_entry);
+	}
+	spin_unlock_irqrestore(&wb_connector->job_lock, flags);
+}
+
 int drm_writeback_set_fb(struct drm_connector_state *conn_state,
 			 struct drm_framebuffer *fb)
 {

-- 
2.47.1


^ permalink raw reply related	[flat|nested] 16+ messages in thread

* [PATCH v6 5/8] drm: writeback: Create an helper for drm_writeback_connector initialization
  2024-12-30 18:37 [PATCH v6 0/8] drm/vkms: Switch all vkms object to DRM managed objects Louis Chauvet
                   ` (3 preceding siblings ...)
  2024-12-30 18:37 ` [PATCH v6 4/8] drm: writeback: Introduce cleanup function Louis Chauvet
@ 2024-12-30 18:37 ` Louis Chauvet
  2025-01-06 12:56   ` Maxime Ripard
  2024-12-30 18:37 ` [PATCH v6 6/8] drm: writeback: Add missing cleanup in case of initialization failure Louis Chauvet
                   ` (2 subsequent siblings)
  7 siblings, 1 reply; 16+ messages in thread
From: Louis Chauvet @ 2024-12-30 18:37 UTC (permalink / raw)
  To: Rodrigo Siqueira, Melissa Wen, Maíra Canal, Haneen Mohammed,
	Maarten Lankhorst, Maxime Ripard, Thomas Zimmermann, David Airlie,
	Simona Vetter, Simona Vetter
  Cc: dri-devel, arthurgrillo, linux-kernel, jeremie.dautheribes,
	miquel.raynal, thomas.petazzoni, seanpaul, nicolejadeyee,
	Louis Chauvet

As the old drm and the new drmm variants of drm_writeback_connector
requires almost the same initialization, create an internal helper to do
most of the initialization work.

Signed-off-by: Louis Chauvet <louis.chauvet@bootlin.com>
---
 drivers/gpu/drm/drm_writeback.c | 87 +++++++++++++++++++++++++++++------------
 1 file changed, 61 insertions(+), 26 deletions(-)

diff --git a/drivers/gpu/drm/drm_writeback.c b/drivers/gpu/drm/drm_writeback.c
index c274cba257cde5f4b446df3854974e690c60bf7b..494400b09796d37ed89145da45d5f1e029632de5 100644
--- a/drivers/gpu/drm/drm_writeback.c
+++ b/drivers/gpu/drm/drm_writeback.c
@@ -219,7 +219,6 @@ EXPORT_SYMBOL(drm_writeback_connector_init);
  * @dev: DRM device
  * @wb_connector: Writeback connector to initialize
  * @enc: handle to the already initialized drm encoder
- * @con_funcs: Connector funcs vtable
  * @formats: Array of supported pixel formats for the writeback engine
  * @n_formats: Length of the formats array
  *
@@ -235,41 +234,31 @@ EXPORT_SYMBOL(drm_writeback_connector_init);
  * assigning the encoder helper functions, possible_crtcs and any other encoder
  * specific operation.
  *
- * Drivers should always use this function instead of drm_connector_init() to
- * set up writeback connectors if they want to manage themselves the lifetime of the
- * associated encoder.
- *
  * Returns: 0 on success, or a negative error code
  */
-int drm_writeback_connector_init_with_encoder(struct drm_device *dev,
-		struct drm_writeback_connector *wb_connector, struct drm_encoder *enc,
-		const struct drm_connector_funcs *con_funcs, const u32 *formats,
-		int n_formats)
+static int __drm_writeback_connector_init(struct drm_device *dev,
+					  struct drm_writeback_connector *wb_connector,
+					  struct drm_encoder *enc, const u32 *formats,
+					  int n_formats)
 {
-	struct drm_property_blob *blob;
 	struct drm_connector *connector = &wb_connector->base;
 	struct drm_mode_config *config = &dev->mode_config;
+	struct drm_property_blob *blob;
 	int ret = create_writeback_properties(dev);
 
 	if (ret != 0)
 		return ret;
 
-	blob = drm_property_create_blob(dev, n_formats * sizeof(*formats),
-					formats);
-	if (IS_ERR(blob))
-		return PTR_ERR(blob);
-
-
 	connector->interlace_allowed = 0;
 
-	ret = drm_connector_init(dev, connector, con_funcs,
-				 DRM_MODE_CONNECTOR_WRITEBACK);
-	if (ret)
-		goto connector_fail;
-
 	ret = drm_connector_attach_encoder(connector, enc);
 	if (ret)
-		goto attach_fail;
+		return ret;
+
+	blob = drm_property_create_blob(dev, n_formats * sizeof(*formats),
+					formats);
+	if (IS_ERR(blob))
+		return PTR_ERR(blob);
 
 	INIT_LIST_HEAD(&wb_connector->job_queue);
 	spin_lock_init(&wb_connector->job_lock);
@@ -292,11 +281,57 @@ int drm_writeback_connector_init_with_encoder(struct drm_device *dev,
 	wb_connector->pixel_formats_blob_ptr = blob;
 
 	return 0;
+}
+
+/**
+ * drm_writeback_connector_init_with_encoder - Initialize a writeback connector with
+ * a custom encoder
+ *
+ * @dev: DRM device
+ * @wb_connector: Writeback connector to initialize
+ * @enc: handle to the already initialized drm encoder
+ * @con_funcs: Connector funcs vtable
+ * @formats: Array of supported pixel formats for the writeback engine
+ * @n_formats: Length of the formats array
+ *
+ * This function creates the writeback-connector-specific properties if they
+ * have not been already created, initializes the connector as
+ * type DRM_MODE_CONNECTOR_WRITEBACK, and correctly initializes the property
+ * values.
+ *
+ * This function assumes that the drm_writeback_connector's encoder has already been
+ * created and initialized before invoking this function.
+ *
+ * In addition, this function also assumes that callers of this API will manage
+ * assigning the encoder helper functions, possible_crtcs and any other encoder
+ * specific operation.
+ *
+ * Drivers should always use this function instead of drm_connector_init() to
+ * set up writeback connectors if they want to manage themselves the lifetime of the
+ * associated encoder.
+ *
+ * Returns: 0 on success, or a negative error code
+ */
+int drm_writeback_connector_init_with_encoder(struct drm_device *dev,
+					      struct drm_writeback_connector *wb_connector,
+					      struct drm_encoder *enc,
+					      const struct drm_connector_funcs *con_funcs,
+					      const u32 *formats, int n_formats)
+{
+	struct drm_property_blob *blob;
+	struct drm_connector *connector = &wb_connector->base;
+	int ret;
+
+	ret = drm_connector_init(dev, connector, con_funcs,
+				 DRM_MODE_CONNECTOR_WRITEBACK);
+	if (ret)
+		return ret;
+
+	ret = __drm_writeback_connector_init(dev, wb_connector, enc, formats,
+					     n_formats);
+	if (ret)
+		drm_connector_cleanup(connector);
 
-attach_fail:
-	drm_connector_cleanup(connector);
-connector_fail:
-	drm_property_blob_put(blob);
 	return ret;
 }
 EXPORT_SYMBOL(drm_writeback_connector_init_with_encoder);

-- 
2.47.1


^ permalink raw reply related	[flat|nested] 16+ messages in thread

* [PATCH v6 6/8] drm: writeback: Add missing cleanup in case of initialization failure
  2024-12-30 18:37 [PATCH v6 0/8] drm/vkms: Switch all vkms object to DRM managed objects Louis Chauvet
                   ` (4 preceding siblings ...)
  2024-12-30 18:37 ` [PATCH v6 5/8] drm: writeback: Create an helper for drm_writeback_connector initialization Louis Chauvet
@ 2024-12-30 18:37 ` Louis Chauvet
  2025-01-06 12:58   ` Maxime Ripard
  2024-12-30 18:37 ` [PATCH v6 7/8] drm: writeback: Create drmm variants for drm_writeback_connector initialization Louis Chauvet
  2024-12-30 18:37 ` [PATCH v6 8/8] drm/vkms: Switch to managed for writeback connector Louis Chauvet
  7 siblings, 1 reply; 16+ messages in thread
From: Louis Chauvet @ 2024-12-30 18:37 UTC (permalink / raw)
  To: Rodrigo Siqueira, Melissa Wen, Maíra Canal, Haneen Mohammed,
	Maarten Lankhorst, Maxime Ripard, Thomas Zimmermann, David Airlie,
	Simona Vetter, Simona Vetter
  Cc: dri-devel, arthurgrillo, linux-kernel, jeremie.dautheribes,
	miquel.raynal, thomas.petazzoni, seanpaul, nicolejadeyee,
	Louis Chauvet

The current implementation of drm_writeback_connector initialization does
not properly clean up all resources in case of failure (allocated
properties and possible_encoders). Add this cleaning in case of failure.

Signed-off-by: Louis Chauvet <louis.chauvet@bootlin.com>
---
 drivers/gpu/drm/drm_writeback.c | 15 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)

diff --git a/drivers/gpu/drm/drm_writeback.c b/drivers/gpu/drm/drm_writeback.c
index 494400b09796d37ed89145da45d5f1e029632de5..9c69f7181e02c23dabce488405608c40d4184af5 100644
--- a/drivers/gpu/drm/drm_writeback.c
+++ b/drivers/gpu/drm/drm_writeback.c
@@ -247,18 +247,20 @@ static int __drm_writeback_connector_init(struct drm_device *dev,
 	int ret = create_writeback_properties(dev);
 
 	if (ret != 0)
-		return ret;
+		goto failed_properties;
 
 	connector->interlace_allowed = 0;
 
 	ret = drm_connector_attach_encoder(connector, enc);
 	if (ret)
-		return ret;
+		goto failed_properties;
 
 	blob = drm_property_create_blob(dev, n_formats * sizeof(*formats),
 					formats);
-	if (IS_ERR(blob))
-		return PTR_ERR(blob);
+	if (IS_ERR(blob)) {
+		ret = PTR_ERR(blob);
+		goto failed_blob;
+	}
 
 	INIT_LIST_HEAD(&wb_connector->job_queue);
 	spin_lock_init(&wb_connector->job_lock);
@@ -281,6 +283,11 @@ static int __drm_writeback_connector_init(struct drm_device *dev,
 	wb_connector->pixel_formats_blob_ptr = blob;
 
 	return 0;
+failed_blob:
+	connector->possible_encoders &= ~drm_encoder_mask(enc);
+failed_properties:
+	delete_writeback_properties(dev);
+	return ret;
 }
 
 /**

-- 
2.47.1


^ permalink raw reply related	[flat|nested] 16+ messages in thread

* [PATCH v6 7/8] drm: writeback: Create drmm variants for drm_writeback_connector initialization
  2024-12-30 18:37 [PATCH v6 0/8] drm/vkms: Switch all vkms object to DRM managed objects Louis Chauvet
                   ` (5 preceding siblings ...)
  2024-12-30 18:37 ` [PATCH v6 6/8] drm: writeback: Add missing cleanup in case of initialization failure Louis Chauvet
@ 2024-12-30 18:37 ` Louis Chauvet
  2025-01-06 13:04   ` Maxime Ripard
  2024-12-30 18:37 ` [PATCH v6 8/8] drm/vkms: Switch to managed for writeback connector Louis Chauvet
  7 siblings, 1 reply; 16+ messages in thread
From: Louis Chauvet @ 2024-12-30 18:37 UTC (permalink / raw)
  To: Rodrigo Siqueira, Melissa Wen, Maíra Canal, Haneen Mohammed,
	Maarten Lankhorst, Maxime Ripard, Thomas Zimmermann, David Airlie,
	Simona Vetter, Simona Vetter
  Cc: dri-devel, arthurgrillo, linux-kernel, jeremie.dautheribes,
	miquel.raynal, thomas.petazzoni, seanpaul, nicolejadeyee,
	Louis Chauvet

To allows driver to only use drmm objects, add helper to create
drm_writeback_connectors with automated lifetime management.

Signed-off-by: Louis Chauvet <louis.chauvet@bootlin.com>
---
 drivers/gpu/drm/drm_writeback.c | 69 +++++++++++++++++++++++++++++++++++++++++
 include/drm/drm_writeback.h     |  8 +++++
 2 files changed, 77 insertions(+)

diff --git a/drivers/gpu/drm/drm_writeback.c b/drivers/gpu/drm/drm_writeback.c
index 9c69f7181e02c23dabce488405608c40d4184af5..1251f65aae9e3b6fb5c5de9ab9280e5430342208 100644
--- a/drivers/gpu/drm/drm_writeback.c
+++ b/drivers/gpu/drm/drm_writeback.c
@@ -369,6 +369,75 @@ static void drm_writeback_connector_cleanup(struct drm_device *dev,
 	spin_unlock_irqrestore(&wb_connector->job_lock, flags);
 }
 
+/**
+ * drmm_writeback_connector_init - Initialize a writeback connector with
+ * a custom encoder
+ *
+ * @dev: DRM device
+ * @wb_connector: Writeback connector to initialize
+ * @con_funcs: Connector funcs vtable
+ * @enc: handle to the already initialized drm encoder, optional
+ * @enc_funcs: Encoder funcs vtable, optional, only used when @enc is NULL
+ * @formats: Array of supported pixel formats for the writeback engine
+ * @n_formats: Length of the formats array
+ * @possible_crtcs: if @enc is NULL, this will set the possible_crtc for the
+ *		    newly created encoder
+ *
+ * This function initialize a writeback connector and register its cleanup.
+ *
+ * This function creates the writeback-connector-specific properties if they
+ * have not been already created, initializes the connector as
+ * type DRM_MODE_CONNECTOR_WRITEBACK, and correctly initializes the property
+ * values.
+ *
+ * If @enc is NULL, this function will create a drm-managed encoder and will
+ * attach @enc_funcs on it. It will also attach the CRTC passed in
+ * @possible_crtcs
+ *
+ * Returns: 0 on success, or a negative error code
+ */
+int drmm_writeback_connector_init(struct drm_device *dev,
+				  struct drm_writeback_connector *wb_connector,
+				  const struct drm_connector_funcs *con_funcs,
+				  struct drm_encoder *enc,
+				  const struct drm_encoder_helper_funcs *enc_funcs,
+				  const u32 *formats, int n_formats,
+				  u32 possible_crtcs)
+{
+	struct drm_connector *connector = &wb_connector->base;
+	int ret;
+
+	if (!enc) {
+		ret = drmm_encoder_init(dev, &wb_connector->encoder,
+					NULL, DRM_MODE_ENCODER_VIRTUAL, NULL);
+		if (ret)
+			return ret;
+
+		enc = &wb_connector->encoder;
+		enc->possible_crtcs |= possible_crtcs;
+		if (enc_funcs)
+			drm_encoder_helper_add(enc, enc_funcs);
+	}
+
+	ret = drmm_connector_init(dev, connector, con_funcs,
+				  DRM_MODE_CONNECTOR_WRITEBACK, NULL);
+	if (ret)
+		return ret;
+
+	ret = __drm_writeback_connector_init(dev, wb_connector, enc, formats,
+					     n_formats);
+	if (ret)
+		return ret;
+
+	ret = drmm_add_action_or_reset(dev, (void *)drm_writeback_connector_cleanup,
+				       wb_connector);
+	if (ret)
+		return ret;
+
+	return 0;
+}
+EXPORT_SYMBOL(drmm_writeback_connector_init);
+
 int drm_writeback_set_fb(struct drm_connector_state *conn_state,
 			 struct drm_framebuffer *fb)
 {
diff --git a/include/drm/drm_writeback.h b/include/drm/drm_writeback.h
index 17e576c80169a820e8d5587b229b2cc2ee369a18..885373a34c000ae9a4ecff8d76125290bffca3f0 100644
--- a/include/drm/drm_writeback.h
+++ b/include/drm/drm_writeback.h
@@ -161,6 +161,14 @@ int drm_writeback_connector_init_with_encoder(struct drm_device *dev,
 				const struct drm_connector_funcs *con_funcs, const u32 *formats,
 				int n_formats);
 
+int drmm_writeback_connector_init(struct drm_device *dev,
+				  struct drm_writeback_connector *wb_connector,
+				  const struct drm_connector_funcs *con_funcs,
+				  struct drm_encoder *enc,
+				  const struct drm_encoder_helper_funcs *enc_funcs,
+				  const u32 *formats, int n_formats,
+				  u32 possible_crtcs);
+
 int drm_writeback_set_fb(struct drm_connector_state *conn_state,
 			 struct drm_framebuffer *fb);
 

-- 
2.47.1


^ permalink raw reply related	[flat|nested] 16+ messages in thread

* [PATCH v6 8/8] drm/vkms: Switch to managed for writeback connector
  2024-12-30 18:37 [PATCH v6 0/8] drm/vkms: Switch all vkms object to DRM managed objects Louis Chauvet
                   ` (6 preceding siblings ...)
  2024-12-30 18:37 ` [PATCH v6 7/8] drm: writeback: Create drmm variants for drm_writeback_connector initialization Louis Chauvet
@ 2024-12-30 18:37 ` Louis Chauvet
  7 siblings, 0 replies; 16+ messages in thread
From: Louis Chauvet @ 2024-12-30 18:37 UTC (permalink / raw)
  To: Rodrigo Siqueira, Melissa Wen, Maíra Canal, Haneen Mohammed,
	Maarten Lankhorst, Maxime Ripard, Thomas Zimmermann, David Airlie,
	Simona Vetter, Simona Vetter
  Cc: dri-devel, arthurgrillo, linux-kernel, jeremie.dautheribes,
	miquel.raynal, thomas.petazzoni, seanpaul, nicolejadeyee,
	Louis Chauvet, José Expósito

The current VKMS driver uses non-managed function to create
writeback connectors. It is not an issue yet, but in order
to support multiple devices easily, convert this code to
use drm and device managed helpers.

Reviewed-by: José Expósito <jose.exposito89@gmail.com>
Signed-off-by: Louis Chauvet <louis.chauvet@bootlin.com>
---
 drivers/gpu/drm/vkms/vkms_writeback.c | 13 ++++++-------
 1 file changed, 6 insertions(+), 7 deletions(-)

diff --git a/drivers/gpu/drm/vkms/vkms_writeback.c b/drivers/gpu/drm/vkms/vkms_writeback.c
index 79918b44fedd7ae2451d1d530fc6d5aabf2d99a3..f12417b2d24803a33e4ff56108cc89704a500faf 100644
--- a/drivers/gpu/drm/vkms/vkms_writeback.c
+++ b/drivers/gpu/drm/vkms/vkms_writeback.c
@@ -24,7 +24,6 @@ static const u32 vkms_wb_formats[] = {
 
 static const struct drm_connector_funcs vkms_wb_connector_funcs = {
 	.fill_modes = drm_helper_probe_single_connector_modes,
-	.destroy = drm_connector_cleanup,
 	.reset = drm_atomic_helper_connector_reset,
 	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
@@ -169,10 +168,10 @@ int vkms_enable_writeback_connector(struct vkms_device *vkmsdev)
 
 	drm_connector_helper_add(&wb->base, &vkms_wb_conn_helper_funcs);
 
-	return drm_writeback_connector_init(&vkmsdev->drm, wb,
-					    &vkms_wb_connector_funcs,
-					    NULL,
-					    vkms_wb_formats,
-					    ARRAY_SIZE(vkms_wb_formats),
-					    1);
+	return drmm_writeback_connector_init(&vkmsdev->drm, wb,
+					     &vkms_wb_connector_funcs,
+					     NULL, NULL,
+					     vkms_wb_formats,
+					     ARRAY_SIZE(vkms_wb_formats),
+					     1);
 }

-- 
2.47.1


^ permalink raw reply related	[flat|nested] 16+ messages in thread

* Re: [PATCH v6 4/8] drm: writeback: Introduce cleanup function
  2024-12-30 18:37 ` [PATCH v6 4/8] drm: writeback: Introduce cleanup function Louis Chauvet
@ 2025-01-06  9:43   ` Maxime Ripard
  2025-01-06 12:46     ` Louis Chauvet
  0 siblings, 1 reply; 16+ messages in thread
From: Maxime Ripard @ 2025-01-06  9:43 UTC (permalink / raw)
  To: Louis Chauvet
  Cc: Rodrigo Siqueira, Melissa Wen, Maíra Canal, Haneen Mohammed,
	Maarten Lankhorst, Thomas Zimmermann, David Airlie, Simona Vetter,
	Simona Vetter, dri-devel, arthurgrillo, linux-kernel,
	jeremie.dautheribes, miquel.raynal, thomas.petazzoni, seanpaul,
	nicolejadeyee

[-- Attachment #1: Type: text/plain, Size: 3467 bytes --]

On Mon, Dec 30, 2024 at 07:37:34PM +0100, Louis Chauvet wrote:
> Currently there is no cleanup function for writeback connectors. To allows
> implementation of drmm variant of writeback connector, create a cleanup
> function that can be used to properly remove all the writeback-specific
> properties and allocations.
> 
> This also introduce an helper to cleanup only the drm_writeback_connector
> properties, so it can be used during initialization to cleanup in case of
> failure.
> 
> Signed-off-by: Louis Chauvet <louis.chauvet@bootlin.com>
> ---
>  drivers/gpu/drm/drm_writeback.c | 43 +++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 43 insertions(+)
> 
> diff --git a/drivers/gpu/drm/drm_writeback.c b/drivers/gpu/drm/drm_writeback.c
> index 33a3c98a962d1ec49ac4b353902036cf74290ae6..c274cba257cde5f4b446df3854974e690c60bf7b 100644
> --- a/drivers/gpu/drm/drm_writeback.c
> +++ b/drivers/gpu/drm/drm_writeback.c
> @@ -15,6 +15,7 @@
>  #include <drm/drm_device.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_property.h>
>  #include <drm/drm_writeback.h>
> @@ -140,6 +141,22 @@ static int create_writeback_properties(struct drm_device *dev)
>  	return 0;
>  }
>  
> +static void delete_writeback_properties(struct drm_device *dev)
> +{
> +	if (dev->mode_config.writeback_pixel_formats_property) {
> +		drm_property_destroy(dev, dev->mode_config.writeback_pixel_formats_property);
> +		dev->mode_config.writeback_pixel_formats_property = NULL;
> +	}
> +	if (dev->mode_config.writeback_out_fence_ptr_property) {
> +		drm_property_destroy(dev, dev->mode_config.writeback_out_fence_ptr_property);
> +		dev->mode_config.writeback_out_fence_ptr_property = NULL;
> +	}
> +	if (dev->mode_config.writeback_fb_id_property) {
> +		drm_property_destroy(dev, dev->mode_config.writeback_fb_id_property);
> +		dev->mode_config.writeback_fb_id_property = NULL;
> +	}
> +}
> +
>  static const struct drm_encoder_funcs drm_writeback_encoder_funcs = {
>  	.destroy = drm_encoder_cleanup,
>  };
> @@ -284,6 +301,32 @@ int drm_writeback_connector_init_with_encoder(struct drm_device *dev,
>  }
>  EXPORT_SYMBOL(drm_writeback_connector_init_with_encoder);
>  
> +/**
> + * drm_writeback_connector_cleanup - Cleanup the writeback connector
> + * @dev: DRM device
> + * @wb_connector: Pointer to the writeback connector to clean up
> + *
> + * This will decrement the reference counter of blobs and destroy properties. It
> + * will also clean the remaining jobs in this writeback connector. Caution: This helper will not
> + * clean up the attached encoder and the drm_connector.
> + */
> +static void drm_writeback_connector_cleanup(struct drm_device *dev,
> +					    struct drm_writeback_connector *wb_connector)
> +{
> +	unsigned long flags;
> +	struct drm_writeback_job *pos, *n;
> +
> +	delete_writeback_properties(dev);
> +	drm_property_blob_put(wb_connector->pixel_formats_blob_ptr);
> +
> +	spin_lock_irqsave(&wb_connector->job_lock, flags);
> +	list_for_each_entry_safe(pos, n, &wb_connector->job_queue, list_entry) {
> +		drm_writeback_cleanup_job(pos);
> +		list_del(&pos->list_entry);
> +	}
> +	spin_unlock_irqrestore(&wb_connector->job_lock, flags);
> +}
> +

Given that this function is static now, it should be merged with the
patch using it.

Maxime

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 273 bytes --]

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH v6 4/8] drm: writeback: Introduce cleanup function
  2025-01-06  9:43   ` Maxime Ripard
@ 2025-01-06 12:46     ` Louis Chauvet
  0 siblings, 0 replies; 16+ messages in thread
From: Louis Chauvet @ 2025-01-06 12:46 UTC (permalink / raw)
  To: Maxime Ripard
  Cc: Rodrigo Siqueira, Melissa Wen, Maíra Canal, Haneen Mohammed,
	Maarten Lankhorst, Thomas Zimmermann, David Airlie, Simona Vetter,
	Simona Vetter, dri-devel, arthurgrillo, linux-kernel,
	jeremie.dautheribes, miquel.raynal, thomas.petazzoni, seanpaul,
	nicolejadeyee

On 06/01/25 - 10:43, Maxime Ripard wrote:
> On Mon, Dec 30, 2024 at 07:37:34PM +0100, Louis Chauvet wrote:
> > Currently there is no cleanup function for writeback connectors. To allows
> > implementation of drmm variant of writeback connector, create a cleanup
> > function that can be used to properly remove all the writeback-specific
> > properties and allocations.
> > 
> > This also introduce an helper to cleanup only the drm_writeback_connector
> > properties, so it can be used during initialization to cleanup in case of
> > failure.
> > 
> > Signed-off-by: Louis Chauvet <louis.chauvet@bootlin.com>
> > ---
> >  drivers/gpu/drm/drm_writeback.c | 43 +++++++++++++++++++++++++++++++++++++++++
> >  1 file changed, 43 insertions(+)
> > 
> > diff --git a/drivers/gpu/drm/drm_writeback.c b/drivers/gpu/drm/drm_writeback.c
> > index 33a3c98a962d1ec49ac4b353902036cf74290ae6..c274cba257cde5f4b446df3854974e690c60bf7b 100644
> > --- a/drivers/gpu/drm/drm_writeback.c
> > +++ b/drivers/gpu/drm/drm_writeback.c
> > @@ -15,6 +15,7 @@
> >  #include <drm/drm_device.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_property.h>
> >  #include <drm/drm_writeback.h>
> > @@ -140,6 +141,22 @@ static int create_writeback_properties(struct drm_device *dev)
> >  	return 0;
> >  }
> >  
> > +static void delete_writeback_properties(struct drm_device *dev)
> > +{
> > +	if (dev->mode_config.writeback_pixel_formats_property) {
> > +		drm_property_destroy(dev, dev->mode_config.writeback_pixel_formats_property);
> > +		dev->mode_config.writeback_pixel_formats_property = NULL;
> > +	}
> > +	if (dev->mode_config.writeback_out_fence_ptr_property) {
> > +		drm_property_destroy(dev, dev->mode_config.writeback_out_fence_ptr_property);
> > +		dev->mode_config.writeback_out_fence_ptr_property = NULL;
> > +	}
> > +	if (dev->mode_config.writeback_fb_id_property) {
> > +		drm_property_destroy(dev, dev->mode_config.writeback_fb_id_property);
> > +		dev->mode_config.writeback_fb_id_property = NULL;
> > +	}
> > +}
> > +
> >  static const struct drm_encoder_funcs drm_writeback_encoder_funcs = {
> >  	.destroy = drm_encoder_cleanup,
> >  };
> > @@ -284,6 +301,32 @@ int drm_writeback_connector_init_with_encoder(struct drm_device *dev,
> >  }
> >  EXPORT_SYMBOL(drm_writeback_connector_init_with_encoder);
> >  
> > +/**
> > + * drm_writeback_connector_cleanup - Cleanup the writeback connector
> > + * @dev: DRM device
> > + * @wb_connector: Pointer to the writeback connector to clean up
> > + *
> > + * This will decrement the reference counter of blobs and destroy properties. It
> > + * will also clean the remaining jobs in this writeback connector. Caution: This helper will not
> > + * clean up the attached encoder and the drm_connector.
> > + */
> > +static void drm_writeback_connector_cleanup(struct drm_device *dev,
> > +					    struct drm_writeback_connector *wb_connector)
> > +{
> > +	unsigned long flags;
> > +	struct drm_writeback_job *pos, *n;
> > +
> > +	delete_writeback_properties(dev);
> > +	drm_property_blob_put(wb_connector->pixel_formats_blob_ptr);
> > +
> > +	spin_lock_irqsave(&wb_connector->job_lock, flags);
> > +	list_for_each_entry_safe(pos, n, &wb_connector->job_queue, list_entry) {
> > +		drm_writeback_cleanup_job(pos);
> > +		list_del(&pos->list_entry);
> > +	}
> > +	spin_unlock_irqrestore(&wb_connector->job_lock, flags);
> > +}
> > +
> 
> Given that this function is static now, it should be merged with the
> patch using it.
> 
> Maxime

Hi Maxime,

Thanks for your review. I appreciate your feedback and will take it into 
account for the next iteration.

Besides this comment, is there anything else you noticed that might need 
attention before adding your Acked-by/Reviewed-by to the rest of the 
series?

Thanks a lot,
Louis Chauvet

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH v6 5/8] drm: writeback: Create an helper for drm_writeback_connector initialization
  2024-12-30 18:37 ` [PATCH v6 5/8] drm: writeback: Create an helper for drm_writeback_connector initialization Louis Chauvet
@ 2025-01-06 12:56   ` Maxime Ripard
  0 siblings, 0 replies; 16+ messages in thread
From: Maxime Ripard @ 2025-01-06 12:56 UTC (permalink / raw)
  To: Louis Chauvet
  Cc: arthurgrillo, dri-devel, jeremie.dautheribes, linux-kernel,
	miquel.raynal, nicolejadeyee, seanpaul, thomas.petazzoni,
	David Airlie, Haneen Mohammed, Maarten Lankhorst, Maxime Ripard,
	Maíra Canal, Melissa Wen, Rodrigo Siqueira, Simona Vetter,
	Simona Vetter, Thomas Zimmermann

On Mon, 30 Dec 2024 19:37:35 +0100, Louis Chauvet wrote:
> As the old drm and the new drmm variants of drm_writeback_connector
> requires almost the same initialization, create an internal helper to do
> most of the initialization work.
> 
> Signed-off-by: Louis Chauvet <louis.chauvet@bootlin.com>
> 
> [ ... ]

Reviewed-by: Maxime Ripard <mripard@kernel.org>

Thanks!
Maxime

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH v6 6/8] drm: writeback: Add missing cleanup in case of initialization failure
  2024-12-30 18:37 ` [PATCH v6 6/8] drm: writeback: Add missing cleanup in case of initialization failure Louis Chauvet
@ 2025-01-06 12:58   ` Maxime Ripard
  0 siblings, 0 replies; 16+ messages in thread
From: Maxime Ripard @ 2025-01-06 12:58 UTC (permalink / raw)
  To: Louis Chauvet
  Cc: Rodrigo Siqueira, Melissa Wen, Maíra Canal, Haneen Mohammed,
	Maarten Lankhorst, Thomas Zimmermann, David Airlie, Simona Vetter,
	Simona Vetter, dri-devel, arthurgrillo, linux-kernel,
	jeremie.dautheribes, miquel.raynal, thomas.petazzoni, seanpaul,
	nicolejadeyee

[-- Attachment #1: Type: text/plain, Size: 1857 bytes --]

On Mon, Dec 30, 2024 at 07:37:36PM +0100, Louis Chauvet wrote:
> The current implementation of drm_writeback_connector initialization does
> not properly clean up all resources in case of failure (allocated
> properties and possible_encoders). Add this cleaning in case of failure.
> 
> Signed-off-by: Louis Chauvet <louis.chauvet@bootlin.com>
> ---
>  drivers/gpu/drm/drm_writeback.c | 15 +++++++++++----
>  1 file changed, 11 insertions(+), 4 deletions(-)
> 
> diff --git a/drivers/gpu/drm/drm_writeback.c b/drivers/gpu/drm/drm_writeback.c
> index 494400b09796d37ed89145da45d5f1e029632de5..9c69f7181e02c23dabce488405608c40d4184af5 100644
> --- a/drivers/gpu/drm/drm_writeback.c
> +++ b/drivers/gpu/drm/drm_writeback.c
> @@ -247,18 +247,20 @@ static int __drm_writeback_connector_init(struct drm_device *dev,
>  	int ret = create_writeback_properties(dev);
>  
>  	if (ret != 0)
> -		return ret;
> +		goto failed_properties;
>  
>  	connector->interlace_allowed = 0;
>  
>  	ret = drm_connector_attach_encoder(connector, enc);
>  	if (ret)
> -		return ret;
> +		goto failed_properties;
>  
>  	blob = drm_property_create_blob(dev, n_formats * sizeof(*formats),
>  					formats);
> -	if (IS_ERR(blob))
> -		return PTR_ERR(blob);
> +	if (IS_ERR(blob)) {
> +		ret = PTR_ERR(blob);
> +		goto failed_blob;
> +	}
>  
>  	INIT_LIST_HEAD(&wb_connector->job_queue);
>  	spin_lock_init(&wb_connector->job_lock);
> @@ -281,6 +283,11 @@ static int __drm_writeback_connector_init(struct drm_device *dev,
>  	wb_connector->pixel_formats_blob_ptr = blob;
>  
>  	return 0;
> +failed_blob:
> +	connector->possible_encoders &= ~drm_encoder_mask(enc);

I don't think it's worth it to uninitialize that particular field. All
the structure fields are going to be in an undefined state, and
shouldn't be used anymore.

Maxime

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 273 bytes --]

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH v6 7/8] drm: writeback: Create drmm variants for drm_writeback_connector initialization
  2024-12-30 18:37 ` [PATCH v6 7/8] drm: writeback: Create drmm variants for drm_writeback_connector initialization Louis Chauvet
@ 2025-01-06 13:04   ` Maxime Ripard
  2025-01-06 16:19     ` Louis Chauvet
  0 siblings, 1 reply; 16+ messages in thread
From: Maxime Ripard @ 2025-01-06 13:04 UTC (permalink / raw)
  To: Louis Chauvet
  Cc: Rodrigo Siqueira, Melissa Wen, Maíra Canal, Haneen Mohammed,
	Maarten Lankhorst, Thomas Zimmermann, David Airlie, Simona Vetter,
	Simona Vetter, dri-devel, arthurgrillo, linux-kernel,
	jeremie.dautheribes, miquel.raynal, thomas.petazzoni, seanpaul,
	nicolejadeyee

[-- Attachment #1: Type: text/plain, Size: 2961 bytes --]

On Mon, Dec 30, 2024 at 07:37:37PM +0100, Louis Chauvet wrote:
> To allows driver to only use drmm objects, add helper to create
> drm_writeback_connectors with automated lifetime management.
> 
> Signed-off-by: Louis Chauvet <louis.chauvet@bootlin.com>
> ---
>  drivers/gpu/drm/drm_writeback.c | 69 +++++++++++++++++++++++++++++++++++++++++
>  include/drm/drm_writeback.h     |  8 +++++
>  2 files changed, 77 insertions(+)
> 
> diff --git a/drivers/gpu/drm/drm_writeback.c b/drivers/gpu/drm/drm_writeback.c
> index 9c69f7181e02c23dabce488405608c40d4184af5..1251f65aae9e3b6fb5c5de9ab9280e5430342208 100644
> --- a/drivers/gpu/drm/drm_writeback.c
> +++ b/drivers/gpu/drm/drm_writeback.c
> @@ -369,6 +369,75 @@ static void drm_writeback_connector_cleanup(struct drm_device *dev,
>  	spin_unlock_irqrestore(&wb_connector->job_lock, flags);
>  }
>  
> +/**
> + * drmm_writeback_connector_init - Initialize a writeback connector with
> + * a custom encoder
> + *
> + * @dev: DRM device
> + * @wb_connector: Writeback connector to initialize
> + * @con_funcs: Connector funcs vtable
> + * @enc: handle to the already initialized drm encoder, optional
> + * @enc_funcs: Encoder funcs vtable, optional, only used when @enc is NULL
> + * @formats: Array of supported pixel formats for the writeback engine
> + * @n_formats: Length of the formats array
> + * @possible_crtcs: if @enc is NULL, this will set the possible_crtc for the
> + *		    newly created encoder
> + *
> + * This function initialize a writeback connector and register its cleanup.
> + *
> + * This function creates the writeback-connector-specific properties if they
> + * have not been already created, initializes the connector as
> + * type DRM_MODE_CONNECTOR_WRITEBACK, and correctly initializes the property
> + * values.
> + *
> + * If @enc is NULL, this function will create a drm-managed encoder and will
> + * attach @enc_funcs on it. It will also attach the CRTC passed in
> + * @possible_crtcs
> + *
> + * Returns: 0 on success, or a negative error code
> + */
> +int drmm_writeback_connector_init(struct drm_device *dev,
> +				  struct drm_writeback_connector *wb_connector,
> +				  const struct drm_connector_funcs *con_funcs,
> +				  struct drm_encoder *enc,
> +				  const struct drm_encoder_helper_funcs *enc_funcs,
> +				  const u32 *formats, int n_formats,
> +				  u32 possible_crtcs)

The name isn't really consistent with the other functions. We already
have a drm_writeback_connector_init that doesn't take the encoder point
but will just read it from wb_connector->encoder, and we have
drm_writeback_connector_with_encoder that assumes the encoder has
already been created.

We should the name or behavior on either one of them. Why do we need an
optional encoder pointer? If enc is not NULL, then enc_funcs shouldn't
be necessary, if it's NULL, then drm_writeback_connector_init will be
sufficient?

Maxime

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 273 bytes --]

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH v6 7/8] drm: writeback: Create drmm variants for drm_writeback_connector initialization
  2025-01-06 13:04   ` Maxime Ripard
@ 2025-01-06 16:19     ` Louis Chauvet
  2025-01-07 15:36       ` Maxime Ripard
  0 siblings, 1 reply; 16+ messages in thread
From: Louis Chauvet @ 2025-01-06 16:19 UTC (permalink / raw)
  To: Maxime Ripard
  Cc: Rodrigo Siqueira, Melissa Wen, Maíra Canal, Haneen Mohammed,
	Maarten Lankhorst, Thomas Zimmermann, David Airlie, Simona Vetter,
	Simona Vetter, dri-devel, arthurgrillo, linux-kernel,
	jeremie.dautheribes, miquel.raynal, thomas.petazzoni, seanpaul,
	nicolejadeyee

On 06/01/25 - 14:04, Maxime Ripard wrote:
> On Mon, Dec 30, 2024 at 07:37:37PM +0100, Louis Chauvet wrote:
> > To allows driver to only use drmm objects, add helper to create
> > drm_writeback_connectors with automated lifetime management.
> > 
> > Signed-off-by: Louis Chauvet <louis.chauvet@bootlin.com>
> > ---
> >  drivers/gpu/drm/drm_writeback.c | 69 +++++++++++++++++++++++++++++++++++++++++
> >  include/drm/drm_writeback.h     |  8 +++++
> >  2 files changed, 77 insertions(+)
> > 
> > diff --git a/drivers/gpu/drm/drm_writeback.c b/drivers/gpu/drm/drm_writeback.c
> > index 9c69f7181e02c23dabce488405608c40d4184af5..1251f65aae9e3b6fb5c5de9ab9280e5430342208 100644
> > --- a/drivers/gpu/drm/drm_writeback.c
> > +++ b/drivers/gpu/drm/drm_writeback.c
> > @@ -369,6 +369,75 @@ static void drm_writeback_connector_cleanup(struct drm_device *dev,
> >  	spin_unlock_irqrestore(&wb_connector->job_lock, flags);
> >  }
> >  
> > +/**
> > + * drmm_writeback_connector_init - Initialize a writeback connector with
> > + * a custom encoder
> > + *
> > + * @dev: DRM device
> > + * @wb_connector: Writeback connector to initialize
> > + * @con_funcs: Connector funcs vtable
> > + * @enc: handle to the already initialized drm encoder, optional
> > + * @enc_funcs: Encoder funcs vtable, optional, only used when @enc is NULL
> > + * @formats: Array of supported pixel formats for the writeback engine
> > + * @n_formats: Length of the formats array
> > + * @possible_crtcs: if @enc is NULL, this will set the possible_crtc for the
> > + *		    newly created encoder
> > + *
> > + * This function initialize a writeback connector and register its cleanup.
> > + *
> > + * This function creates the writeback-connector-specific properties if they
> > + * have not been already created, initializes the connector as
> > + * type DRM_MODE_CONNECTOR_WRITEBACK, and correctly initializes the property
> > + * values.
> > + *
> > + * If @enc is NULL, this function will create a drm-managed encoder and will
> > + * attach @enc_funcs on it. It will also attach the CRTC passed in
> > + * @possible_crtcs
> > + *
> > + * Returns: 0 on success, or a negative error code
> > + */
> > +int drmm_writeback_connector_init(struct drm_device *dev,
> > +				  struct drm_writeback_connector *wb_connector,
> > +				  const struct drm_connector_funcs *con_funcs,
> > +				  struct drm_encoder *enc,
> > +				  const struct drm_encoder_helper_funcs *enc_funcs,
> > +				  const u32 *formats, int n_formats,
> > +				  u32 possible_crtcs)
> 
> The name isn't really consistent with the other functions. We already
> have a drm_writeback_connector_init that doesn't take the encoder point
> but will just read it from wb_connector->encoder, and we have
> drm_writeback_connector_with_encoder that assumes the encoder has
> already been created.
>
> We should the name or behavior on either one of them. Why do we need an
> optional encoder pointer? If enc is not NULL, then enc_funcs shouldn't
> be necessary, if it's NULL, then drm_writeback_connector_init will be
> sufficient?

This was requested by Jani in [1]. If you prefer I can create two variants 
for the next iteration.

[1]:https://lore.kernel.org/all/87a5gxyrhc.fsf@intel.com/

> 
> Maxime



^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH v6 7/8] drm: writeback: Create drmm variants for drm_writeback_connector initialization
  2025-01-06 16:19     ` Louis Chauvet
@ 2025-01-07 15:36       ` Maxime Ripard
  0 siblings, 0 replies; 16+ messages in thread
From: Maxime Ripard @ 2025-01-07 15:36 UTC (permalink / raw)
  To: Rodrigo Siqueira, Melissa Wen, Maíra Canal, Haneen Mohammed,
	Maarten Lankhorst, Thomas Zimmermann, David Airlie, Simona Vetter,
	Simona Vetter, dri-devel, arthurgrillo, linux-kernel,
	jeremie.dautheribes, miquel.raynal, thomas.petazzoni, seanpaul,
	nicolejadeyee

[-- Attachment #1: Type: text/plain, Size: 3975 bytes --]

On Mon, Jan 06, 2025 at 05:19:42PM +0100, Louis Chauvet wrote:
> On 06/01/25 - 14:04, Maxime Ripard wrote:
> > On Mon, Dec 30, 2024 at 07:37:37PM +0100, Louis Chauvet wrote:
> > > To allows driver to only use drmm objects, add helper to create
> > > drm_writeback_connectors with automated lifetime management.
> > > 
> > > Signed-off-by: Louis Chauvet <louis.chauvet@bootlin.com>
> > > ---
> > >  drivers/gpu/drm/drm_writeback.c | 69 +++++++++++++++++++++++++++++++++++++++++
> > >  include/drm/drm_writeback.h     |  8 +++++
> > >  2 files changed, 77 insertions(+)
> > > 
> > > diff --git a/drivers/gpu/drm/drm_writeback.c b/drivers/gpu/drm/drm_writeback.c
> > > index 9c69f7181e02c23dabce488405608c40d4184af5..1251f65aae9e3b6fb5c5de9ab9280e5430342208 100644
> > > --- a/drivers/gpu/drm/drm_writeback.c
> > > +++ b/drivers/gpu/drm/drm_writeback.c
> > > @@ -369,6 +369,75 @@ static void drm_writeback_connector_cleanup(struct drm_device *dev,
> > >  	spin_unlock_irqrestore(&wb_connector->job_lock, flags);
> > >  }
> > >  
> > > +/**
> > > + * drmm_writeback_connector_init - Initialize a writeback connector with
> > > + * a custom encoder
> > > + *
> > > + * @dev: DRM device
> > > + * @wb_connector: Writeback connector to initialize
> > > + * @con_funcs: Connector funcs vtable
> > > + * @enc: handle to the already initialized drm encoder, optional
> > > + * @enc_funcs: Encoder funcs vtable, optional, only used when @enc is NULL
> > > + * @formats: Array of supported pixel formats for the writeback engine
> > > + * @n_formats: Length of the formats array
> > > + * @possible_crtcs: if @enc is NULL, this will set the possible_crtc for the
> > > + *		    newly created encoder
> > > + *
> > > + * This function initialize a writeback connector and register its cleanup.
> > > + *
> > > + * This function creates the writeback-connector-specific properties if they
> > > + * have not been already created, initializes the connector as
> > > + * type DRM_MODE_CONNECTOR_WRITEBACK, and correctly initializes the property
> > > + * values.
> > > + *
> > > + * If @enc is NULL, this function will create a drm-managed encoder and will
> > > + * attach @enc_funcs on it. It will also attach the CRTC passed in
> > > + * @possible_crtcs
> > > + *
> > > + * Returns: 0 on success, or a negative error code
> > > + */
> > > +int drmm_writeback_connector_init(struct drm_device *dev,
> > > +				  struct drm_writeback_connector *wb_connector,
> > > +				  const struct drm_connector_funcs *con_funcs,
> > > +				  struct drm_encoder *enc,
> > > +				  const struct drm_encoder_helper_funcs *enc_funcs,
> > > +				  const u32 *formats, int n_formats,
> > > +				  u32 possible_crtcs)
> > 
> > The name isn't really consistent with the other functions. We already
> > have a drm_writeback_connector_init that doesn't take the encoder point
> > but will just read it from wb_connector->encoder, and we have
> > drm_writeback_connector_with_encoder that assumes the encoder has
> > already been created.
> >
> > We should the name or behavior on either one of them. Why do we need an
> > optional encoder pointer? If enc is not NULL, then enc_funcs shouldn't
> > be necessary, if it's NULL, then drm_writeback_connector_init will be
> > sufficient?
> 
> This was requested by Jani in [1]. If you prefer I can create two variants 
> for the next iteration.
> 
> [1]:https://lore.kernel.org/all/87a5gxyrhc.fsf@intel.com/

There was another suggestion in that review though ;)

I agree with Jani's second statement here: most of the weirdness of that
API stems from the fact that it deviates from the other APIs, and fixing
that should remove that weirdness.

Ultimately, allocating the encoder in the first place is weird. I don't
think we have any other example of an init function for one entity
allocating its own entity or another.

Why should we allocate that encoder in the helper?

Maxime

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 273 bytes --]

^ permalink raw reply	[flat|nested] 16+ messages in thread

end of thread, other threads:[~2025-01-07 15:36 UTC | newest]

Thread overview: 16+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-12-30 18:37 [PATCH v6 0/8] drm/vkms: Switch all vkms object to DRM managed objects Louis Chauvet
2024-12-30 18:37 ` [PATCH v6 1/8] drm/vkms: Switch to managed for connector Louis Chauvet
2024-12-30 18:37 ` [PATCH v6 2/8] drm/vkms: Switch to managed for encoder Louis Chauvet
2024-12-30 18:37 ` [PATCH v6 3/8] drm/vkms: Switch to managed for crtc Louis Chauvet
2024-12-30 18:37 ` [PATCH v6 4/8] drm: writeback: Introduce cleanup function Louis Chauvet
2025-01-06  9:43   ` Maxime Ripard
2025-01-06 12:46     ` Louis Chauvet
2024-12-30 18:37 ` [PATCH v6 5/8] drm: writeback: Create an helper for drm_writeback_connector initialization Louis Chauvet
2025-01-06 12:56   ` Maxime Ripard
2024-12-30 18:37 ` [PATCH v6 6/8] drm: writeback: Add missing cleanup in case of initialization failure Louis Chauvet
2025-01-06 12:58   ` Maxime Ripard
2024-12-30 18:37 ` [PATCH v6 7/8] drm: writeback: Create drmm variants for drm_writeback_connector initialization Louis Chauvet
2025-01-06 13:04   ` Maxime Ripard
2025-01-06 16:19     ` Louis Chauvet
2025-01-07 15:36       ` Maxime Ripard
2024-12-30 18:37 ` [PATCH v6 8/8] drm/vkms: Switch to managed for writeback connector Louis Chauvet

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox