Skip to contentSkip to conversation

drm/ssd130x: Allocate buffer in the CRTC's .atomic_check() callback

RFC PATCH In review Javier Martinez Canillas 4 participants 18 messages Aug 30, 2023 – Sep 6, 2023

Subsystems (3)
lore
Share & tools
lore

Javier Martinez Canillas

author maintainer
#0 lore raw

RFC PATCH 1/1 drm/ssd130x: Allocate buffer in the CRTC's .atomic_check() callback

The commit 45b58669e532 ("drm/ssd130x: Allocate buffer in the plane's .atomic_check() callback") moved the allocation of the intermediate and HW buffers from the encoder's .atomic_enable callback to primary plane's .atomic_check callback.

This was suggested by Maxime Ripard because drivers aren't allowed to fail after drm_atomic_helper_swap_state() has been called, and the encoder's .atomic_enable happens after the new atomic state has been swapped.

But that change caused a performance regression in very slow platforms, since now the allocation happens for every plane's atomic state commit. For example, Geert Uytterhoeven reports that is the case on a VexRiscV softcore (RISC-V CPU implementation on an FPGA).

To prevent that, move the move the buffers' allocation and free to the CRTC's .atomic_check and .atomic_destroy_state callbacks, so that only happens on a modeset. Since the intermediate buffer is only needed when not using the controller native format (R1), doing the buffer allocation at that CRTC's .atomic_check time would be enough.

Fixes 45b58669e532 ("drm/ssd130x: Allocate buffer in the plane's .atomic_check() callback")

---
Hello,

This is a RFC because I'm not sure if there is a net benefit after this change. I find the currect code much cleaner and less error prone, even when Geert reports that performs worse on his (very slow) platform.

But I'm still posting it to see what others think. I've tested the patch on an I2C ssd1306 OLED and found no regressions.

The patch is on top on Geert's latest patch-set that adds support for the DRM_FORMAT_R1 to the ssd130x driver:

https://lore.kernel.org/dri-devel/[email protected]

Best regards,
Javier

 drivers/gpu/drm/solomon/ssd130x.c | 106 ++++++++++++++++--------------
 1 file changed, 56 insertions(+), 50 deletions(-)
drivers/gpu/drm/solomon/ssd130x.c +56 -50
index 0d2b36ba4011..60536cd0c42d 100644
@@ -650,46 +650,6 @@ static int ssd130x_fb_blit_rect(struct drm_plane_state *state,
 	return ret;
 }
 
-static int ssd130x_primary_plane_helper_atomic_check(struct drm_plane *plane,
-						     struct drm_atomic_state *state)
-{
-	struct drm_device *drm = plane->dev;
-	struct ssd130x_device *ssd130x = drm_to_ssd130x(drm);
-	struct drm_plane_state *plane_state = drm_atomic_get_new_plane_state(state, plane);
-	struct ssd130x_plane_state *ssd130x_state = to_ssd130x_plane_state(plane_state);
-	unsigned int page_height = ssd130x->device_info->page_height;
-	unsigned int pages = DIV_ROUND_UP(ssd130x->height, page_height);
-	const struct drm_format_info *fi;
-	unsigned int pitch;
-	int ret;
-
-	ret = drm_plane_helper_atomic_check(plane, state);
-	if (ret)
-		return ret;
-
-	ssd130x_state->data_array = kcalloc(ssd130x->width, pages, GFP_KERNEL);
-	if (!ssd130x_state->data_array)
-		return -ENOMEM;
-
-	if (plane_state->fb->format->format != DRM_FORMAT_R1) {
-		fi = drm_format_info(DRM_FORMAT_R1);
-		if (!fi)
-			return -EINVAL;
-
-		pitch = drm_format_info_min_pitch(fi, 0, ssd130x->width);
-
-		ssd130x_state->buffer = kcalloc(pitch, ssd130x->height, GFP_KERNEL);
-		if (!ssd130x_state->buffer) {
-			kfree(ssd130x_state->data_array);
-			/* Set to prevent a double free in .atomic_destroy_state() */
-			ssd130x_state->data_array = NULL;
-			return -ENOMEM;
-		}
-	}
-
-	return 0;
-}
-
 static void ssd130x_primary_plane_helper_atomic_update(struct drm_plane *plane,
 						       struct drm_atomic_state *state)
 {
@@ -762,10 +722,6 @@ static struct drm_plane_state *ssd130x_primary_plane_duplicate_state(struct drm_
 	if (!ssd130x_state)
 		return NULL;
 
-	/* The buffers are not duplicated and are allocated in .atomic_check */
-	ssd130x_state->buffer = NULL;
-	ssd130x_state->data_array = NULL;
-
 	new_shadow_plane_state = &ssd130x_state->base;
 
 	__drm_gem_duplicate_shadow_plane_state(plane, new_shadow_plane_state);
@@ -778,9 +734,6 @@ static void ssd130x_primary_plane_destroy_state(struct drm_plane *plane,
 {
 	struct ssd130x_plane_state *ssd130x_state = to_ssd130x_plane_state(state);
 
-	kfree(ssd130x_state->data_array);
-	kfree(ssd130x_state->buffer);
-
 	__drm_gem_destroy_shadow_plane_state(&ssd130x_state->base);
 
 	kfree(ssd130x_state);
@@ -788,7 +741,7 @@ static void ssd130x_primary_plane_destroy_state(struct drm_plane *plane,
 
 static const struct drm_plane_helper_funcs ssd130x_primary_plane_helper_funcs = {
 	DRM_GEM_SHADOW_PLANE_HELPER_FUNCS,
-	.atomic_check = ssd130x_primary_plane_helper_atomic_check,
+	.atomic_check = drm_plane_helper_atomic_check,
 	.atomic_update = ssd130x_primary_plane_helper_atomic_update,
 	.atomic_disable = ssd130x_primary_plane_helper_atomic_disable,
 };
@@ -818,6 +771,59 @@ static enum drm_mode_status ssd130x_crtc_helper_mode_valid(struct drm_crtc *crtc
 	return MODE_OK;
 }
 
+int ssd130x_crtc_helper_atomic_check(struct drm_crtc *crtc, struct drm_atomic_state *state)
+{
+	struct drm_device *drm = crtc->dev;
+	struct ssd130x_device *ssd130x = drm_to_ssd130x(drm);
+	struct drm_plane *plane = crtc->primary;
+	struct drm_plane_state *plane_state = drm_atomic_get_new_plane_state(state, plane);
+	struct ssd130x_plane_state *ssd130x_state = to_ssd130x_plane_state(plane_state);
+	unsigned int page_height = ssd130x->device_info->page_height;
+	unsigned int pages = DIV_ROUND_UP(ssd130x->height, page_height);
+	const struct drm_format_info *fi;
+	unsigned int pitch;
+	int ret;
+
+	ret = drm_crtc_helper_atomic_check(crtc, state);
+	if (ret)
+		return ret;
+
+	ssd130x_state->data_array = kcalloc(ssd130x->width, pages, GFP_KERNEL);
+	if (!ssd130x_state->data_array)
+		return -ENOMEM;
+
+	if (plane_state->fb->format->format != DRM_FORMAT_R1) {
+		fi = drm_format_info(DRM_FORMAT_R1);
+		if (!fi)
+			return -EINVAL;
+
+		pitch = drm_format_info_min_pitch(fi, 0, ssd130x->width);
+
+		ssd130x_state->buffer = kcalloc(pitch, ssd130x->height, GFP_KERNEL);
+		if (!ssd130x_state->buffer) {
+			kfree(ssd130x_state->data_array);
+			/* Set to prevent a double free in .atomic_destroy_state() */
+			ssd130x_state->data_array = NULL;
+			return -ENOMEM;
+		}
+	}
+
+	return 0;
+}
+
+static void ssd130x_crtc_destroy_state(struct drm_crtc *crtc,
+				       struct drm_crtc_state *state)
+{
+	struct drm_plane *plane = crtc->primary;
+	struct drm_plane_state *plane_state = drm_atomic_get_new_plane_state(state->state, plane);
+	struct ssd130x_plane_state *ssd130x_state = to_ssd130x_plane_state(plane_state);
+
+	drm_atomic_helper_crtc_destroy_state(crtc, state);
+
+	kfree(ssd130x_state->data_array);
+	kfree(ssd130x_state->buffer);
+}
+
 /*
  * The CRTC is always enabled. Screen updates are performed by
  * the primary plane's atomic_update function. Disabling clears
@@ -825,7 +831,7 @@ static enum drm_mode_status ssd130x_crtc_helper_mode_valid(struct drm_crtc *crtc
  */
 static const struct drm_crtc_helper_funcs ssd130x_crtc_helper_funcs = {
 	.mode_valid = ssd130x_crtc_helper_mode_valid,
-	.atomic_check = drm_crtc_helper_atomic_check,
+	.atomic_check = ssd130x_crtc_helper_atomic_check,
 };
 
 static const struct drm_crtc_funcs ssd130x_crtc_funcs = {
@@ -834,7 +840,7 @@ static const struct drm_crtc_funcs ssd130x_crtc_funcs = {
 	.set_config = drm_atomic_helper_set_config,
 	.page_flip = drm_atomic_helper_page_flip,
 	.atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
-	.atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
+	.atomic_destroy_state = ssd130x_crtc_destroy_state,
 };
 
 static void ssd130x_encoder_helper_atomic_enable(struct drm_encoder *encoder,
Signature · 1 line
2.41.0
#1 lore raw

Hi Javier

Am 30.08.23 um 08:25 schrieb Javier Martinez Canillas:

Quoted 9 lines

The commit 45b58669e532 ("drm/ssd130x: Allocate buffer in the plane's .atomic_check() callback") moved the allocation of the intermediate and HW buffers from the encoder's .atomic_enable callback to primary plane's .atomic_check callback.

This was suggested by Maxime Ripard because drivers aren't allowed to fail after drm_atomic_helper_swap_state() has been called, and the encoder's .atomic_enable happens after the new atomic state has been swapped.

Quoted 6 lines

But that change caused a performance regression in very slow platforms, since now the allocation happens for every plane's atomic state commit. For example, Geert Uytterhoeven reports that is the case on a VexRiscV softcore (RISC-V CPU implementation on an FPGA).

To prevent that, move the move the buffers' allocation and free to the

Double 'move the'

And maybe buffer's rather than buffers'

Quoted 111 lines

CRTC's .atomic_check and .atomic_destroy_state callbacks, so that only happens on a modeset. Since the intermediate buffer is only needed when not using the controller native format (R1), doing the buffer allocation at that CRTC's .atomic_check time would be enough.

Fixes 45b58669e532 ("drm/ssd130x: Allocate buffer in the plane's .atomic_check() callback")

---
Hello,

This is a RFC because I'm not sure if there is a net benefit after this change. I find the currect code much cleaner and less error prone, even when Geert reports that performs worse on his (very slow) platform.

But I'm still posting it to see what others think. I've tested the patch on an I2C ssd1306 OLED and found no regressions.

The patch is on top on Geert's latest patch-set that adds support for the DRM_FORMAT_R1 to the ssd130x driver:

https://lore.kernel.org/dri-devel/[email protected]

Best regards,
Javier

  drivers/gpu/drm/solomon/ssd130x.c | 106 ++++++++++++++++--------------
  1 file changed, 56 insertions(+), 50 deletions(-)
drivers/gpu/drm/solomon/ssd130x.c +1 -48
index 0d2b36ba4011..60536cd0c42d 100644
@@ -650,46 +650,6 @@ static int ssd130x_fb_blit_rect(struct drm_plane_state *state,
  	return ret;
  }
 
-static int ssd130x_primary_plane_helper_atomic_check(struct drm_plane *plane,
-						     struct drm_atomic_state *state)
-{
-	struct drm_device *drm = plane->dev;
-	struct ssd130x_device *ssd130x = drm_to_ssd130x(drm);
-	struct drm_plane_state *plane_state = drm_atomic_get_new_plane_state(state, plane);
-	struct ssd130x_plane_state *ssd130x_state = to_ssd130x_plane_state(plane_state);
-	unsigned int page_height = ssd130x->device_info->page_height;
-	unsigned int pages = DIV_ROUND_UP(ssd130x->height, page_height);
-	const struct drm_format_info *fi;
-	unsigned int pitch;
-	int ret;
-
-	ret = drm_plane_helper_atomic_check(plane, state);
-	if (ret)
-		return ret;
-
-	ssd130x_state->data_array = kcalloc(ssd130x->width, pages, GFP_KERNEL);
-	if (!ssd130x_state->data_array)
-		return -ENOMEM;
-
-	if (plane_state->fb->format->format != DRM_FORMAT_R1) {
-		fi = drm_format_info(DRM_FORMAT_R1);
-		if (!fi)
-			return -EINVAL;
-
-		pitch = drm_format_info_min_pitch(fi, 0, ssd130x->width);
-
-		ssd130x_state->buffer = kcalloc(pitch, ssd130x->height, GFP_KERNEL);
-		if (!ssd130x_state->buffer) {
-			kfree(ssd130x_state->data_array);
-			/* Set to prevent a double free in .atomic_destroy_state() */
-			ssd130x_state->data_array = NULL;
-			return -ENOMEM;
-		}
-	}
-
-	return 0;
-}
-
  static void ssd130x_primary_plane_helper_atomic_update(struct drm_plane *plane,
  						       struct drm_atomic_state *state)
  {
@@ -762,10 +722,6 @@ static struct drm_plane_state *ssd130x_primary_plane_duplicate_state(struct drm_
  	if (!ssd130x_state)
  		return NULL;
 
-	/* The buffers are not duplicated and are allocated in .atomic_check */
-	ssd130x_state->buffer = NULL;
-	ssd130x_state->data_array = NULL;
-
  	new_shadow_plane_state = &ssd130x_state->base;
 
  	__drm_gem_duplicate_shadow_plane_state(plane, new_shadow_plane_state);
@@ -778,9 +734,6 @@ static void ssd130x_primary_plane_destroy_state(struct drm_plane *plane,
  {
  	struct ssd130x_plane_state *ssd130x_state = to_ssd130x_plane_state(state);
 
-	kfree(ssd130x_state->data_array);
-	kfree(ssd130x_state->buffer);
-
  	__drm_gem_destroy_shadow_plane_state(&ssd130x_state->base);
 
  	kfree(ssd130x_state);
@@ -788,7 +741,7 @@ static void ssd130x_primary_plane_destroy_state(struct drm_plane *plane,
 
  static const struct drm_plane_helper_funcs ssd130x_primary_plane_helper_funcs = {
  	DRM_GEM_SHADOW_PLANE_HELPER_FUNCS,
-	.atomic_check = ssd130x_primary_plane_helper_atomic_check,
+	.atomic_check = drm_plane_helper_atomic_check,
  	.atomic_update = ssd130x_primary_plane_helper_atomic_update,
  	.atomic_disable = ssd130x_primary_plane_helper_atomic_disable,
  };

drivers/gpu/drm/solomon/ssd130x.c static enum drm_mode_status ssd130x_crtc_helper_mode_valid(struct drm_crtc *crtc · line 778

Quoted 8 lines
drivers/gpu/drm/solomon/ssd130x.c +5 -0
@@ -818,6 +771,59 @@ static enum drm_mode_status ssd130x_crtc_helper_mode_valid(struct drm_crtc *crtc
  	return MODE_OK;
  }
 
+int ssd130x_crtc_helper_atomic_check(struct drm_crtc *crtc, struct drm_atomic_state *state)
+{
+	struct drm_device *drm = crtc->dev;
+	struct ssd130x_device *ssd130x = drm_to_ssd130x(drm);
+	struct drm_plane *plane = crtc->primary;

Using 'primary' is deprecated. You can only refer from plane to crtc easily. The other direction is complicated.

drivers/gpu/drm/solomon/ssd130x.c static enum drm_mode_status ssd130x_crtc_helper_mode_valid(struct drm_crtc *crtc · line 780

Quoted 2 lines
drivers/gpu/drm/solomon/ssd130x.c +2 -0
+	struct drm_plane_state *plane_state = drm_atomic_get_new_plane_state(state, plane);
+	struct ssd130x_plane_state *ssd130x_state = to_ssd130x_plane_state(plane_state);

I'd store these pointers in the CRTC state and fetch them from within ssd130x_primary_plane_helper_atomic_update() during the display update. This guarantees that the pointer addresses are always legal.

Besides the pointers, the CRTC state can also store the primary plane format, which you update from the plane's atomic check. By doing so, you wont need to refer to the plane state from the CRTC's atomic_check. The plane's atomic_check runs before the CRTC's atomic_check. [1]

The struct ssd130x_plane_state can then be replaced by stardard DRM shadow-plane state. It also solves the problem with 'crtc->primary'.

[1] https://elixir.bootlin.com/linux/v6.5/source/drivers/gpu/drm/drm_atomic_helper.c#L974

drivers/gpu/drm/solomon/ssd130x.c static enum drm_mode_status ssd130x_crtc_helper_mode_valid(struct drm_crtc *crtc · line 791

Quoted 11 lines
drivers/gpu/drm/solomon/ssd130x.c +11 -0
+	unsigned int page_height = ssd130x->device_info->page_height;
+	unsigned int pages = DIV_ROUND_UP(ssd130x->height, page_height);
+	const struct drm_format_info *fi;
+	unsigned int pitch;
+	int ret;
+
+	ret = drm_crtc_helper_atomic_check(crtc, state);
+	if (ret)
+		return ret;
+
+	ssd130x_state->data_array = kcalloc(ssd130x->width, pages, GFP_KERNEL);

Do you really need kcalloc here? kmalloc seems good enough.

drivers/gpu/drm/solomon/ssd130x.c static enum drm_mode_status ssd130x_crtc_helper_mode_valid(struct drm_crtc *crtc · line 802

Quoted 11 lines
drivers/gpu/drm/solomon/ssd130x.c +11 -0
+	if (!ssd130x_state->data_array)
+		return -ENOMEM;
+
+	if (plane_state->fb->format->format != DRM_FORMAT_R1) {
+		fi = drm_format_info(DRM_FORMAT_R1);
+		if (!fi)
+			return -EINVAL;
+
+		pitch = drm_format_info_min_pitch(fi, 0, ssd130x->width);
+
+		ssd130x_state->buffer = kcalloc(pitch, ssd130x->height, GFP_KERNEL);

Same question about kcalloc.

Quoted 12 lines
drivers/gpu/drm/solomon/ssd130x.c +12 -0
+		if (!ssd130x_state->buffer) {
+			kfree(ssd130x_state->data_array);
+			/* Set to prevent a double free in .atomic_destroy_state() */
+			ssd130x_state->data_array = NULL;
+			return -ENOMEM;
+		}
+	}
+
+	return 0;
+}
+
+static void ssd130x_crtc_destroy_state(struct drm_crtc *crtc,

drivers/gpu/drm/solomon/ssd130x.c static enum drm_mode_status ssd130x_crtc_helper_mode_valid(struct drm_crtc *crtc · line 824

Quoted 10 lines
drivers/gpu/drm/solomon/ssd130x.c +10 -0
+				       struct drm_crtc_state *state)
+{
+	struct drm_plane *plane = crtc->primary;
+	struct drm_plane_state *plane_state = drm_atomic_get_new_plane_state(state->state, plane);
+	struct ssd130x_plane_state *ssd130x_state = to_ssd130x_plane_state(plane_state);
+
+	drm_atomic_helper_crtc_destroy_state(crtc, state);
+
+	kfree(ssd130x_state->data_array);
+	kfree(ssd130x_state->buffer);

This cross references among state-helpers has the potential to blow up. It's not really clear if there even is a plane state here.

Also see my comment on the allocation of these buffers., which would solve this problem as well.

Best regards
Thomas

Quoted 23 lines
drivers/gpu/drm/solomon/ssd130x.c +4 -2
+}
+
  /*
   * The CRTC is always enabled. Screen updates are performed by
   * the primary plane's atomic_update function. Disabling clears
@@ -825,7 +831,7 @@ static enum drm_mode_status ssd130x_crtc_helper_mode_valid(struct drm_crtc *crtc
   */
  static const struct drm_crtc_helper_funcs ssd130x_crtc_helper_funcs = {
  	.mode_valid = ssd130x_crtc_helper_mode_valid,
-	.atomic_check = drm_crtc_helper_atomic_check,
+	.atomic_check = ssd130x_crtc_helper_atomic_check,
  };
 
  static const struct drm_crtc_funcs ssd130x_crtc_funcs = {
@@ -834,7 +840,7 @@ static const struct drm_crtc_funcs ssd130x_crtc_funcs = {
  	.set_config = drm_atomic_helper_set_config,
  	.page_flip = drm_atomic_helper_page_flip,
  	.atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
-	.atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
+	.atomic_destroy_state = ssd130x_crtc_destroy_state,
  };
 
  static void ssd130x_encoder_helper_atomic_enable(struct drm_encoder *encoder,
Signature · 6 lines
Thomas Zimmermann
Graphics Driver Developer
SUSE Software Solutions Germany GmbH
Frankenstrasse 146, 90461 Nuernberg, Germany
GF: Ivo Totev, Andrew Myers, Andrew McDonald, Boudien Moerman
HRB 36809 (AG Nuernberg)
#2 lore raw

Hi Thomas,

Quoted 15 lines · Thomas Zimmermann

Am 30.08.23 um 08:25 schrieb Javier Martinez Canillas:

Quoted 14 lines

The commit 45b58669e532 ("drm/ssd130x: Allocate buffer in the plane's .atomic_check() callback") moved the allocation of the intermediate and HW buffers from the encoder's .atomic_enable callback to primary plane's .atomic_check callback.

This was suggested by Maxime Ripard because drivers aren't allowed to fail after drm_atomic_helper_swap_state() has been called, and the encoder's .atomic_enable happens after the new atomic state has been swapped.

But that change caused a performance regression in very slow platforms, since now the allocation happens for every plane's atomic state commit. For example, Geert Uytterhoeven reports that is the case on a VexRiscV softcore (RISC-V CPU implementation on an FPGA).

Quoted 14 lines
Quoted 1 line

To prevent that, move the move the buffers' allocation and free to the

Double 'move the'

And maybe buffer's rather than buffers'

Quoted 8 lines

CRTC's .atomic_check and .atomic_destroy_state callbacks, so that only happens on a modeset. Since the intermediate buffer is only needed when not using the controller native format (R1), doing the buffer allocation at that CRTC's .atomic_check time would be enough.

Fixes 45b58669e532 ("drm/ssd130x: Allocate buffer in the plane's .atomic_check() callback")

Javier: thanks for your patch!

Quoted 4 lines

Besides the pointers, the CRTC state can also store the primary plane format, which you update from the plane's atomic check. By doing so, you wont need to refer to the plane state from the CRTC's atomic_check. The plane's atomic_check runs before the CRTC's atomic_check. [1]

I haven't tested Javier's patch yet, but does this mean that his patch won't help?

The problem I saw was that these buffers were allocated and freed over and over again on each flash of the cursor of the text console on top of the emulated frame buffer device.

Gr{oetje,eeting}s,

Geert

Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
                                -- Linus Torvalds
#3 lore raw

Hi Geert

Am 30.08.23 um 09:40 schrieb Geert Uytterhoeven:

Quoted 24 lines

Hi Thomas,

Quoted 21 lines · Thomas Zimmermann

Am 30.08.23 um 08:25 schrieb Javier Martinez Canillas:

Quoted 9 lines

The commit 45b58669e532 ("drm/ssd130x: Allocate buffer in the plane's .atomic_check() callback") moved the allocation of the intermediate and HW buffers from the encoder's .atomic_enable callback to primary plane's .atomic_check callback.

This was suggested by Maxime Ripard because drivers aren't allowed to fail after drm_atomic_helper_swap_state() has been called, and the encoder's .atomic_enable happens after the new atomic state has been swapped.

Quoted 6 lines

But that change caused a performance regression in very slow platforms, since now the allocation happens for every plane's atomic state commit. For example, Geert Uytterhoeven reports that is the case on a VexRiscV softcore (RISC-V CPU implementation on an FPGA).

To prevent that, move the move the buffers' allocation and free to the

Double 'move the'

And maybe buffer's rather than buffers'

Scratch that remark.

Quoted 17 lines
Quoted 9 lines
Quoted 9 lines

CRTC's .atomic_check and .atomic_destroy_state callbacks, so that only happens on a modeset. Since the intermediate buffer is only needed when not using the controller native format (R1), doing the buffer allocation at that CRTC's .atomic_check time would be enough.

Fixes 45b58669e532 ("drm/ssd130x: Allocate buffer in the plane's .atomic_check() callback")

Javier: thanks for your patch!

Quoted 4 lines

Besides the pointers, the CRTC state can also store the primary plane format, which you update from the plane's atomic check. By doing so, you wont need to refer to the plane state from the CRTC's atomic_check. The plane's atomic_check runs before the CRTC's atomic_check. [1]

Quoted 6 lines

I haven't tested Javier's patch yet, but does this mean that his patch won't help?

The problem I saw was that these buffers were allocated and freed over and over again on each flash of the cursor of the text console on top of the emulated frame buffer device.

Javier's current patch should resolve this problem. The temporary buffers are now only allocated on display-mode/format changes, but not on each single screen update. My review concerns only the implementation.

Best regards
Thomas

Quoted 5 lines

Gr{oetje,eeting}s,

Geert

Signature · 6 lines
Thomas Zimmermann
Graphics Driver Developer
SUSE Software Solutions Germany GmbH
Frankenstrasse 146, 90461 Nuernberg, Germany
GF: Ivo Totev, Andrew Myers, Andrew McDonald, Boudien Moerman
HRB 36809 (AG Nuernberg)
#4 lore raw
In reply to Javier Martinez Canillas

Hi Javier,

another idea about this patch: why not just keep the allocation in the plane's atomic check, but store the temporary buffers in a plane struct. You'd only grow the arrays length in atomic_check and later fetch the pointers in atomic_update. It needs some locking, but nothing complicated.

Best regards
Thomas

Am 30.08.23 um 08:25 schrieb Javier Martinez Canillas:

Quoted 203 lines

The commit 45b58669e532 ("drm/ssd130x: Allocate buffer in the plane's .atomic_check() callback") moved the allocation of the intermediate and HW buffers from the encoder's .atomic_enable callback to primary plane's .atomic_check callback.

This was suggested by Maxime Ripard because drivers aren't allowed to fail after drm_atomic_helper_swap_state() has been called, and the encoder's .atomic_enable happens after the new atomic state has been swapped.

But that change caused a performance regression in very slow platforms, since now the allocation happens for every plane's atomic state commit. For example, Geert Uytterhoeven reports that is the case on a VexRiscV softcore (RISC-V CPU implementation on an FPGA).

To prevent that, move the move the buffers' allocation and free to the CRTC's .atomic_check and .atomic_destroy_state callbacks, so that only happens on a modeset. Since the intermediate buffer is only needed when not using the controller native format (R1), doing the buffer allocation at that CRTC's .atomic_check time would be enough.

Fixes 45b58669e532 ("drm/ssd130x: Allocate buffer in the plane's .atomic_check() callback")

---
Hello,

This is a RFC because I'm not sure if there is a net benefit after this change. I find the currect code much cleaner and less error prone, even when Geert reports that performs worse on his (very slow) platform.

But I'm still posting it to see what others think. I've tested the patch on an I2C ssd1306 OLED and found no regressions.

The patch is on top on Geert's latest patch-set that adds support for the DRM_FORMAT_R1 to the ssd130x driver:

https://lore.kernel.org/dri-devel/[email protected]

Best regards,
Javier

  drivers/gpu/drm/solomon/ssd130x.c | 106 ++++++++++++++++--------------
  1 file changed, 56 insertions(+), 50 deletions(-)
drivers/gpu/drm/solomon/ssd130x.c +56 -50
index 0d2b36ba4011..60536cd0c42d 100644
@@ -650,46 +650,6 @@ static int ssd130x_fb_blit_rect(struct drm_plane_state *state,
  	return ret;
  }
 
-static int ssd130x_primary_plane_helper_atomic_check(struct drm_plane *plane,
-						     struct drm_atomic_state *state)
-{
-	struct drm_device *drm = plane->dev;
-	struct ssd130x_device *ssd130x = drm_to_ssd130x(drm);
-	struct drm_plane_state *plane_state = drm_atomic_get_new_plane_state(state, plane);
-	struct ssd130x_plane_state *ssd130x_state = to_ssd130x_plane_state(plane_state);
-	unsigned int page_height = ssd130x->device_info->page_height;
-	unsigned int pages = DIV_ROUND_UP(ssd130x->height, page_height);
-	const struct drm_format_info *fi;
-	unsigned int pitch;
-	int ret;
-
-	ret = drm_plane_helper_atomic_check(plane, state);
-	if (ret)
-		return ret;
-
-	ssd130x_state->data_array = kcalloc(ssd130x->width, pages, GFP_KERNEL);
-	if (!ssd130x_state->data_array)
-		return -ENOMEM;
-
-	if (plane_state->fb->format->format != DRM_FORMAT_R1) {
-		fi = drm_format_info(DRM_FORMAT_R1);
-		if (!fi)
-			return -EINVAL;
-
-		pitch = drm_format_info_min_pitch(fi, 0, ssd130x->width);
-
-		ssd130x_state->buffer = kcalloc(pitch, ssd130x->height, GFP_KERNEL);
-		if (!ssd130x_state->buffer) {
-			kfree(ssd130x_state->data_array);
-			/* Set to prevent a double free in .atomic_destroy_state() */
-			ssd130x_state->data_array = NULL;
-			return -ENOMEM;
-		}
-	}
-
-	return 0;
-}
-
  static void ssd130x_primary_plane_helper_atomic_update(struct drm_plane *plane,
  						       struct drm_atomic_state *state)
  {
@@ -762,10 +722,6 @@ static struct drm_plane_state *ssd130x_primary_plane_duplicate_state(struct drm_
  	if (!ssd130x_state)
  		return NULL;
 
-	/* The buffers are not duplicated and are allocated in .atomic_check */
-	ssd130x_state->buffer = NULL;
-	ssd130x_state->data_array = NULL;
-
  	new_shadow_plane_state = &ssd130x_state->base;
 
  	__drm_gem_duplicate_shadow_plane_state(plane, new_shadow_plane_state);
@@ -778,9 +734,6 @@ static void ssd130x_primary_plane_destroy_state(struct drm_plane *plane,
  {
  	struct ssd130x_plane_state *ssd130x_state = to_ssd130x_plane_state(state);
 
-	kfree(ssd130x_state->data_array);
-	kfree(ssd130x_state->buffer);
-
  	__drm_gem_destroy_shadow_plane_state(&ssd130x_state->base);
 
  	kfree(ssd130x_state);
@@ -788,7 +741,7 @@ static void ssd130x_primary_plane_destroy_state(struct drm_plane *plane,
 
  static const struct drm_plane_helper_funcs ssd130x_primary_plane_helper_funcs = {
  	DRM_GEM_SHADOW_PLANE_HELPER_FUNCS,
-	.atomic_check = ssd130x_primary_plane_helper_atomic_check,
+	.atomic_check = drm_plane_helper_atomic_check,
  	.atomic_update = ssd130x_primary_plane_helper_atomic_update,
  	.atomic_disable = ssd130x_primary_plane_helper_atomic_disable,
  };
@@ -818,6 +771,59 @@ static enum drm_mode_status ssd130x_crtc_helper_mode_valid(struct drm_crtc *crtc
  	return MODE_OK;
  }
 
+int ssd130x_crtc_helper_atomic_check(struct drm_crtc *crtc, struct drm_atomic_state *state)
+{
+	struct drm_device *drm = crtc->dev;
+	struct ssd130x_device *ssd130x = drm_to_ssd130x(drm);
+	struct drm_plane *plane = crtc->primary;
+	struct drm_plane_state *plane_state = drm_atomic_get_new_plane_state(state, plane);
+	struct ssd130x_plane_state *ssd130x_state = to_ssd130x_plane_state(plane_state);
+	unsigned int page_height = ssd130x->device_info->page_height;
+	unsigned int pages = DIV_ROUND_UP(ssd130x->height, page_height);
+	const struct drm_format_info *fi;
+	unsigned int pitch;
+	int ret;
+
+	ret = drm_crtc_helper_atomic_check(crtc, state);
+	if (ret)
+		return ret;
+
+	ssd130x_state->data_array = kcalloc(ssd130x->width, pages, GFP_KERNEL);
+	if (!ssd130x_state->data_array)
+		return -ENOMEM;
+
+	if (plane_state->fb->format->format != DRM_FORMAT_R1) {
+		fi = drm_format_info(DRM_FORMAT_R1);
+		if (!fi)
+			return -EINVAL;
+
+		pitch = drm_format_info_min_pitch(fi, 0, ssd130x->width);
+
+		ssd130x_state->buffer = kcalloc(pitch, ssd130x->height, GFP_KERNEL);
+		if (!ssd130x_state->buffer) {
+			kfree(ssd130x_state->data_array);
+			/* Set to prevent a double free in .atomic_destroy_state() */
+			ssd130x_state->data_array = NULL;
+			return -ENOMEM;
+		}
+	}
+
+	return 0;
+}
+
+static void ssd130x_crtc_destroy_state(struct drm_crtc *crtc,
+				       struct drm_crtc_state *state)
+{
+	struct drm_plane *plane = crtc->primary;
+	struct drm_plane_state *plane_state = drm_atomic_get_new_plane_state(state->state, plane);
+	struct ssd130x_plane_state *ssd130x_state = to_ssd130x_plane_state(plane_state);
+
+	drm_atomic_helper_crtc_destroy_state(crtc, state);
+
+	kfree(ssd130x_state->data_array);
+	kfree(ssd130x_state->buffer);
+}
+
  /*
   * The CRTC is always enabled. Screen updates are performed by
   * the primary plane's atomic_update function. Disabling clears
@@ -825,7 +831,7 @@ static enum drm_mode_status ssd130x_crtc_helper_mode_valid(struct drm_crtc *crtc
   */
  static const struct drm_crtc_helper_funcs ssd130x_crtc_helper_funcs = {
  	.mode_valid = ssd130x_crtc_helper_mode_valid,
-	.atomic_check = drm_crtc_helper_atomic_check,
+	.atomic_check = ssd130x_crtc_helper_atomic_check,
  };
 
  static const struct drm_crtc_funcs ssd130x_crtc_funcs = {
@@ -834,7 +840,7 @@ static const struct drm_crtc_funcs ssd130x_crtc_funcs = {
  	.set_config = drm_atomic_helper_set_config,
  	.page_flip = drm_atomic_helper_page_flip,
  	.atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
-	.atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
+	.atomic_destroy_state = ssd130x_crtc_destroy_state,
  };
 
  static void ssd130x_encoder_helper_atomic_enable(struct drm_encoder *encoder,
Signature · 6 lines
Thomas Zimmermann
Graphics Driver Developer
SUSE Software Solutions Germany GmbH
Frankenstrasse 146, 90461 Nuernberg, Germany
GF: Ivo Totev, Andrew Myers, Andrew McDonald, Boudien Moerman
HRB 36809 (AG Nuernberg)
author maintainer
#5 lore raw
Quoted 7 lines · Thomas Zimmermann

Hi Javier,

another idea about this patch: why not just keep the allocation in the plane's atomic check, but store the temporary buffers in a plane struct. You'd only grow the arrays length in atomic_check and later fetch the pointers in atomic_update. It needs some locking, but nothing complicated.

Yes, that would work too. Another option is to just move the buffers to struct ssd130x_device as it was before commit 45b58669e532 ("drm/ssd130x:
Allocate buffer in the plane's .atomic_check() callback") but just make them fixed arrays with the size of the biggest format.

That will be some memory wasted but will prevent the problem of trying to allocate buffers after drm_atomic_helper_swap_state() has been called.

Quoted 4 lines

Best regards
Thomas

Am 30.08.23 um 08:25 schrieb Javier Martinez Canillas:

Signature · 5 lines
Best regards,

Javier Martinez Canillas
Core Platforms
Red Hat
maintainer
#6 lore raw
Quoted 10 lines · Javier Martinez Canillas
Quoted 7 lines · Thomas Zimmermann

Hi Javier,

another idea about this patch: why not just keep the allocation in the plane's atomic check, but store the temporary buffers in a plane struct. You'd only grow the arrays length in atomic_check and later fetch the pointers in atomic_update. It needs some locking, but nothing complicated.

Quoted 7 lines

Yes, that would work too. Another option is to just move the buffers to struct ssd130x_device as it was before commit 45b58669e532 ("drm/ssd130x:
Allocate buffer in the plane's .atomic_check() callback") but just make them fixed arrays with the size of the biggest format.

That will be some memory wasted but will prevent the problem of trying to allocate buffers after drm_atomic_helper_swap_state() has been called.

If we want to go that road, we don't even need an extra allocation, it can be part of the state or object structure itself.

Maxime

author maintainer
#7 lore raw
Quoted 16 lines · Maxime Ripard
Quoted 15 lines · Javier Martinez Canillas
Quoted 7 lines · Thomas Zimmermann

Hi Javier,

another idea about this patch: why not just keep the allocation in the plane's atomic check, but store the temporary buffers in a plane struct. You'd only grow the arrays length in atomic_check and later fetch the pointers in atomic_update. It needs some locking, but nothing complicated.

Yes, that would work too. Another option is to just move the buffers to struct ssd130x_device as it was before commit 45b58669e532 ("drm/ssd130x:
Allocate buffer in the plane's .atomic_check() callback") but just make them fixed arrays with the size of the biggest format.

Quoted 6 lines
Quoted 2 lines

That will be some memory wasted but will prevent the problem of trying to allocate buffers after drm_atomic_helper_swap_state() has been called.

If we want to go that road, we don't even need an extra allocation, it can be part of the state or object structure itself.

Yes, I meant to have it as fixed-length arrays. But still the best option seems to be to allocate them but in the CRTC's .atomic_check() and store them in a CRTC private state as Thomas suggested.

Geert will post a v2 of his R1 support patches, I'll wait for those and after that try to implement Thomas' suggestion.

Signature · 5 lines
Best regards,

Javier Martinez Canillas
Core Platforms
Red Hat
#8 lore raw
In reply to Javier Martinez Canillas

Hi

Am 01.09.23 um 09:48 schrieb Javier Martinez Canillas:

Quoted 4 lines
Quoted 2 lines · Thomas Zimmermann

Hi Javier,

Quoted 8 lines
Quoted 5 lines

another idea about this patch: why not just keep the allocation in the plane's atomic check, but store the temporary buffers in a plane struct. You'd only grow the arrays length in atomic_check and later fetch the pointers in atomic_update. It needs some locking, but nothing complicated.

Yes, that would work too. Another option is to just move the buffers to struct ssd130x_device as it was before commit 45b58669e532 ("drm/ssd130x:

Adding something like a struct ssd130x_plane that holds the temporary memory has the advantage of making a clear connection between the memory and the plane. If nothing else, to the next programmer reading the code.

Quoted 2 lines

Allocate buffer in the plane's .atomic_check() callback") but just make them fixed arrays with the size of the biggest format.

What is the size of the biggest format? I haven't read the driver code, but a shadow plane can be up to 4096 pixels wide. It's 16 KiB for XRGB888. Not too much, but not nothing either.

To reduce allocation and/or locking overhead, you could try to update the pointers in the plane struct with RCU semantics. Plane updates would use whatever pointer they saw, while the plane's atomic_check could grow the memory buffers as necessary.

Best regards
Thomas

Quoted 9 lines

That will be some memory wasted but will prevent the problem of trying to allocate buffers after drm_atomic_helper_swap_state() has been called.

Quoted 4 lines

Best regards
Thomas

Am 30.08.23 um 08:25 schrieb Javier Martinez Canillas:

Signature · 6 lines
Thomas Zimmermann
Graphics Driver Developer
SUSE Software Solutions Germany GmbH
Frankenstrasse 146, 90461 Nuernberg, Germany
GF: Ivo Totev, Andrew Myers, Andrew McDonald, Boudien Moerman
HRB 36809 (AG Nuernberg)
author maintainer
#9 lore raw

Thomas Zimmermann <[email protected]> writes:

Hello Thomas,

Quoted 13 lines

Hi

Am 01.09.23 um 09:48 schrieb Javier Martinez Canillas:

Quoted 10 lines
Quoted 7 lines · Thomas Zimmermann

Hi Javier,

another idea about this patch: why not just keep the allocation in the plane's atomic check, but store the temporary buffers in a plane struct. You'd only grow the arrays length in atomic_check and later fetch the pointers in atomic_update. It needs some locking, but nothing complicated.

Quoted 7 lines
Quoted 2 lines

Yes, that would work too. Another option is to just move the buffers to struct ssd130x_device as it was before commit 45b58669e532 ("drm/ssd130x:

Adding something like a struct ssd130x_plane that holds the temporary memory has the advantage of making a clear connection between the memory and the plane. If nothing else, to the next programmer reading the code.

Ok, I'm confused now. The current version of the driver already has a struct ssd130x_plane_state that stores the buffers:

https://git.kernel.org/pub/scm/linux/kernel/git/next/linux-next.git/tree/drivers/gpu/drm/solomon/ssd130x.c#n144

But what you are saying is that instead of setting those pointers to NULL in the ssd130x_primary_plane_duplicate_state() function, it can just be allocated once and the pointers copied when duplicating the new state?

So you want me to add some locking when accesing those since will be shared between the old and new state?

In that case another option is to just kref the buffers, I think that should be enough?

Quoted 7 lines
Quoted 2 lines

Allocate buffer in the plane's .atomic_check() callback") but just make them fixed arrays with the size of the biggest format.

What is the size of the biggest format? I haven't read the driver code, but a shadow plane can be up to 4096 pixels wide. It's 16 KiB for XRGB888. Not too much, but not nothing either.

The shadow plane yes, but I was talking about the buffers in:

struct ssd130x_plane_state {
	struct drm_shadow_plane_state base;
	/* Intermediate buffer to convert pixels from XRGB8888 to HW format */
	u8 *buffer;
	/* Buffer to store pixels in HW format and written to the panel */
	u8 *data_array;
};

These are not the size of the shadow buffer but the of the displayed area, that depends on the panel fixed resolution (defined in the Device Tree).

The biggest resolution for ssd130x panels is 132x64 (SSD1305 and SH1106), so that means the biggest buffer will be 132 * 64 * R1 pitch = 1056 bytes.

Quoted 5 lines

To reduce allocation and/or locking overhead, you could try to update the pointers in the plane struct with RCU semantics. Plane updates would use whatever pointer they saw, while the plane's atomic_check could grow the memory buffers as necessary.

I'm still unsure the added complexity is worth it. As mentioned, the fbdev driver also allocats the buffers on each display update and at least the intermediate .buffer that stores the XRGB8888 -> R1 conversion is tied to the plane, the .data_array that sends the actual data to the controller can be argued to be tied to the CRTC.

But given that a format change doesn't trigger a CRTC mode set (as Maxime pointed out to me) then at least the .buffer allocation can't be done in the CRTC .atomic_check() handler.

I could move the .data_array allocatoin there or even do it at probe time, but since the .buffer allocation would be done in the plane .atomic_check() anyways, I would rather keep as is and have both in the same function.

Quoted 3 lines

Best regards
Thomas

Signature · 5 lines
Best regards,

Javier Martinez Canillas
Core Platforms
Red Hat
maintainer
#10 lore raw
In reply to Javier Martinez Canillas

Hi,

Quoted 5 lines · Javier Martinez Canillas

The commit 45b58669e532 ("drm/ssd130x: Allocate buffer in the plane's .atomic_check() callback") moved the allocation of the intermediate and HW buffers from the encoder's .atomic_enable callback to primary plane's .atomic_check callback.

Quoted 8 lines

This was suggested by Maxime Ripard because drivers aren't allowed to fail after drm_atomic_helper_swap_state() has been called, and the encoder's .atomic_enable happens after the new atomic state has been swapped.

But that change caused a performance regression in very slow platforms, since now the allocation happens for every plane's atomic state commit. For example, Geert Uytterhoeven reports that is the case on a VexRiscV softcore (RISC-V CPU implementation on an FPGA).

I'd like to have numbers on that. It's a bit surprising to me that, given how many objects we already allocate during a commit, two small additional allocations affect performances so dramatically, even on a slow platform.

Maxime

#11 lore raw

Hi Maxime,

Quoted 15 lines · Maxime Ripard
Quoted 5 lines · Javier Martinez Canillas

The commit 45b58669e532 ("drm/ssd130x: Allocate buffer in the plane's .atomic_check() callback") moved the allocation of the intermediate and HW buffers from the encoder's .atomic_enable callback to primary plane's .atomic_check callback.

Quoted 8 lines

This was suggested by Maxime Ripard because drivers aren't allowed to fail after drm_atomic_helper_swap_state() has been called, and the encoder's .atomic_enable happens after the new atomic state has been swapped.

But that change caused a performance regression in very slow platforms, since now the allocation happens for every plane's atomic state commit. For example, Geert Uytterhoeven reports that is the case on a VexRiscV softcore (RISC-V CPU implementation on an FPGA).

Quoted 4 lines

I'd like to have numbers on that. It's a bit surprising to me that, given how many objects we already allocate during a commit, two small additional allocations affect performances so dramatically, even on a slow platform.

To be fair, I didn't benchmark that. Perhaps it's just too slow due to all these other allocations (and whatever else happens).

I just find it extremely silly to allocate a buffer over and over again, while we know that buffer is needed for each and every display update.

Gr{oetje,eeting}s,

Geert

Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
                                -- Linus Torvalds
author maintainer
#12 lore raw
Quoted 23 lines · Geert Uytterhoeven

Hi Maxime,

Quoted 15 lines · Maxime Ripard
Quoted 5 lines · Javier Martinez Canillas

The commit 45b58669e532 ("drm/ssd130x: Allocate buffer in the plane's .atomic_check() callback") moved the allocation of the intermediate and HW buffers from the encoder's .atomic_enable callback to primary plane's .atomic_check callback.

Quoted 8 lines

This was suggested by Maxime Ripard because drivers aren't allowed to fail after drm_atomic_helper_swap_state() has been called, and the encoder's .atomic_enable happens after the new atomic state has been swapped.

But that change caused a performance regression in very slow platforms, since now the allocation happens for every plane's atomic state commit. For example, Geert Uytterhoeven reports that is the case on a VexRiscV softcore (RISC-V CPU implementation on an FPGA).

Quoted 4 lines

I'd like to have numbers on that. It's a bit surprising to me that, given how many objects we already allocate during a commit, two small additional allocations affect performances so dramatically, even on a slow platform.

Quoted 6 lines

To be fair, I didn't benchmark that. Perhaps it's just too slow due to all these other allocations (and whatever else happens).

I just find it extremely silly to allocate a buffer over and over again, while we know that buffer is needed for each and every display update.

Is not efficient that's true, but on the other hand we have other objects that are destroyed and created on each atomic update.

In the ssd1307fb driver, the buffer is allocated on ssd1307fb_update_rect() (by calling the ssd1307fb_alloc_array() function), so it also happens for every display update in the fbdev driver.

Quoted 2 lines

Gr{oetje,eeting}s,

Signature · 5 lines
Best regards,

Javier Martinez Canillas
Core Platforms
Red Hat
maintainer
#13 lore raw
In reply to Geert Uytterhoeven
Quoted 23 lines · Geert Uytterhoeven

Hi Maxime,

Quoted 15 lines · Maxime Ripard
Quoted 5 lines · Javier Martinez Canillas

The commit 45b58669e532 ("drm/ssd130x: Allocate buffer in the plane's .atomic_check() callback") moved the allocation of the intermediate and HW buffers from the encoder's .atomic_enable callback to primary plane's .atomic_check callback.

Quoted 8 lines

This was suggested by Maxime Ripard because drivers aren't allowed to fail after drm_atomic_helper_swap_state() has been called, and the encoder's .atomic_enable happens after the new atomic state has been swapped.

But that change caused a performance regression in very slow platforms, since now the allocation happens for every plane's atomic state commit. For example, Geert Uytterhoeven reports that is the case on a VexRiscV softcore (RISC-V CPU implementation on an FPGA).

Quoted 4 lines

I'd like to have numbers on that. It's a bit surprising to me that, given how many objects we already allocate during a commit, two small additional allocations affect performances so dramatically, even on a slow platform.

Quoted 5 lines

To be fair, I didn't benchmark that. Perhaps it's just too slow due to all these other allocations (and whatever else happens).

I just find it extremely silly to allocate a buffer over and over again, while we know that buffer is needed for each and every display update.

Maybe it's silly, but I guess it depends on what you want to optimize for. You won't know the size of that buffer before you're in atomic_check. So it's a different trade-off than you would like, but I wouldn't call it extremely silly.

Maxime

#14 lore raw

Hi Maxime,

Quoted 25 lines · Maxime Ripard
Quoted 24 lines · Geert Uytterhoeven
Quoted 15 lines · Maxime Ripard
Quoted 5 lines · Javier Martinez Canillas

The commit 45b58669e532 ("drm/ssd130x: Allocate buffer in the plane's .atomic_check() callback") moved the allocation of the intermediate and HW buffers from the encoder's .atomic_enable callback to primary plane's .atomic_check callback.

Quoted 8 lines

This was suggested by Maxime Ripard because drivers aren't allowed to fail after drm_atomic_helper_swap_state() has been called, and the encoder's .atomic_enable happens after the new atomic state has been swapped.

But that change caused a performance regression in very slow platforms, since now the allocation happens for every plane's atomic state commit. For example, Geert Uytterhoeven reports that is the case on a VexRiscV softcore (RISC-V CPU implementation on an FPGA).

Quoted 4 lines

I'd like to have numbers on that. It's a bit surprising to me that, given how many objects we already allocate during a commit, two small additional allocations affect performances so dramatically, even on a slow platform.

To be fair, I didn't benchmark that. Perhaps it's just too slow due to all these other allocations (and whatever else happens).

Quoted 7 lines
Quoted 2 lines

I just find it extremely silly to allocate a buffer over and over again, while we know that buffer is needed for each and every display update.

Maybe it's silly, but I guess it depends on what you want to optimize for. You won't know the size of that buffer before you're in atomic_check. So it's a different trade-off than you would like, but I wouldn't call it extremely silly.

The size of ssd130x_plane_state.data_array[] is fixed, and depends on the actual display connected.
The size of ssd130x_plane_state.buffer[] is also fixed, and depends on the plane's size (which is currently fixed to the display size).

Gr{oetje,eeting}s,

Geert

Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
                                -- Linus Torvalds
author maintainer
#15 lore raw

Geert Uytterhoeven <[email protected]> writes:

Hello Geert,

Quoted 31 lines

Hi Maxime,

Quoted 28 lines · Maxime Ripard
Quoted 26 lines · Geert Uytterhoeven
Quoted 15 lines · Maxime Ripard
Quoted 5 lines · Javier Martinez Canillas

The commit 45b58669e532 ("drm/ssd130x: Allocate buffer in the plane's .atomic_check() callback") moved the allocation of the intermediate and HW buffers from the encoder's .atomic_enable callback to primary plane's .atomic_check callback.

Quoted 8 lines

This was suggested by Maxime Ripard because drivers aren't allowed to fail after drm_atomic_helper_swap_state() has been called, and the encoder's .atomic_enable happens after the new atomic state has been swapped.

But that change caused a performance regression in very slow platforms, since now the allocation happens for every plane's atomic state commit. For example, Geert Uytterhoeven reports that is the case on a VexRiscV softcore (RISC-V CPU implementation on an FPGA).

Quoted 4 lines

I'd like to have numbers on that. It's a bit surprising to me that, given how many objects we already allocate during a commit, two small additional allocations affect performances so dramatically, even on a slow platform.

To be fair, I didn't benchmark that. Perhaps it's just too slow due to all these other allocations (and whatever else happens).

I just find it extremely silly to allocate a buffer over and over again, while we know that buffer is needed for each and every display update.

Quoted 7 lines
Quoted 4 lines

Maybe it's silly, but I guess it depends on what you want to optimize for. You won't know the size of that buffer before you're in atomic_check. So it's a different trade-off than you would like, but I wouldn't call it extremely silly.

The size of ssd130x_plane_state.data_array[] is fixed, and depends on the actual display connected.

Agreed.

Quoted 3 lines

The size of ssd130x_plane_state.buffer[] is also fixed, and depends on the plane's size (which is currently fixed to the display size).

Well, one could say that also depends on the format chosen. That is, if XRGB8888 is used then its size should be the fixed display size but if R1 is used its size could be 0 (since the shadow plane will already store the pixels in R1 format).

Quoted 2 lines

Gr{oetje,eeting}s,

Signature · 5 lines
Best regards,

Javier Martinez Canillas
Core Platforms
Red Hat
maintainer
#16 lore raw
In reply to Geert Uytterhoeven
Quoted 31 lines · Geert Uytterhoeven

Hi Maxime,

Quoted 28 lines · Maxime Ripard
Quoted 26 lines · Geert Uytterhoeven
Quoted 15 lines · Maxime Ripard
Quoted 5 lines · Javier Martinez Canillas

The commit 45b58669e532 ("drm/ssd130x: Allocate buffer in the plane's .atomic_check() callback") moved the allocation of the intermediate and HW buffers from the encoder's .atomic_enable callback to primary plane's .atomic_check callback.

Quoted 8 lines

This was suggested by Maxime Ripard because drivers aren't allowed to fail after drm_atomic_helper_swap_state() has been called, and the encoder's .atomic_enable happens after the new atomic state has been swapped.

But that change caused a performance regression in very slow platforms, since now the allocation happens for every plane's atomic state commit. For example, Geert Uytterhoeven reports that is the case on a VexRiscV softcore (RISC-V CPU implementation on an FPGA).

Quoted 4 lines

I'd like to have numbers on that. It's a bit surprising to me that, given how many objects we already allocate during a commit, two small additional allocations affect performances so dramatically, even on a slow platform.

To be fair, I didn't benchmark that. Perhaps it's just too slow due to all these other allocations (and whatever else happens).

I just find it extremely silly to allocate a buffer over and over again, while we know that buffer is needed for each and every display update.

Quoted 7 lines
Quoted 4 lines

Maybe it's silly, but I guess it depends on what you want to optimize for. You won't know the size of that buffer before you're in atomic_check. So it's a different trade-off than you would like, but I wouldn't call it extremely silly.

The size of ssd130x_plane_state.data_array[] is fixed, and depends on the actual display connected.

That one can be tied to the CRTC state if needed. It would only be allocated on each modeset, so probably once for that kind of device.

Quoted 2 lines

The size of ssd130x_plane_state.buffer[] is also fixed, and depends on the plane's size (which is currently fixed to the display size).

Doesn't it depend on the format as well?

Maxime

author maintainer
#17 lore raw
Quoted 37 lines · Maxime Ripard
Quoted 36 lines · Geert Uytterhoeven

Hi Maxime,

Quoted 32 lines · Maxime Ripard
Quoted 21 lines · Geert Uytterhoeven
Quoted 15 lines · Maxime Ripard
Quoted 5 lines · Javier Martinez Canillas

The commit 45b58669e532 ("drm/ssd130x: Allocate buffer in the plane's .atomic_check() callback") moved the allocation of the intermediate and HW buffers from the encoder's .atomic_enable callback to primary plane's .atomic_check callback.

Quoted 8 lines

This was suggested by Maxime Ripard because drivers aren't allowed to fail after drm_atomic_helper_swap_state() has been called, and the encoder's .atomic_enable happens after the new atomic state has been swapped.

But that change caused a performance regression in very slow platforms, since now the allocation happens for every plane's atomic state commit. For example, Geert Uytterhoeven reports that is the case on a VexRiscV softcore (RISC-V CPU implementation on an FPGA).

Quoted 4 lines

I'd like to have numbers on that. It's a bit surprising to me that, given how many objects we already allocate during a commit, two small additional allocations affect performances so dramatically, even on a slow platform.

Quoted 5 lines

To be fair, I didn't benchmark that. Perhaps it's just too slow due to all these other allocations (and whatever else happens).

I just find it extremely silly to allocate a buffer over and over again, while we know that buffer is needed for each and every display update.

Maybe it's silly, but I guess it depends on what you want to optimize for. You won't know the size of that buffer before you're in atomic_check. So it's a different trade-off than you would like, but I wouldn't call it extremely silly.

Quoted 6 lines
Quoted 2 lines

The size of ssd130x_plane_state.data_array[] is fixed, and depends on the actual display connected.

That one can be tied to the CRTC state if needed. It would only be allocated on each modeset, so probably once for that kind of device.

Yes.

Quoted 5 lines
Quoted 2 lines

The size of ssd130x_plane_state.buffer[] is also fixed, and depends on the plane's size (which is currently fixed to the display size).

Doesn't it depend on the format as well?

Yes and no. The buffer[] size is fixed, but whether that intermediate buffer is needed or not will depend if the native format was chosen.

So one could say that is either 0 (not used) or the fixed size needed to do the format conversion from XRGB8888 to R1.

Quoted 2 lines

Maxime

Signature · 5 lines
Best regards,

Javier Martinez Canillas
Core Platforms
Red Hat