Skip to contentSkip to conversation

drm/ssd130x: Set the address window in ssd132x_clear_screen()

PATCH Superseded Fabio Piparo 4 participants 8 messages Aug 17 – Sep 8

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 drm/ssd130x: Set the address window in ssd132x_clear_screen()

ssd132x_clear_screen() sends its zeroes without setting the column and row ranges first, so they land in whatever window the last ssd132x_update_rect() left programmed. A partial final update then leaves the rest of the screen uncleared, with fbcon the last damage is typically the cursor cell, so blanking keeps the console content on the panel.

Set the full-screen window first, as ssd130x_clear_screen() already does.

Fixes fdd591e00a9c ("drm/ssd130x: Add support for the SSD132x OLED controller family")
Assisted-by LLM

---
Exercised on an SPI SSD1322 under local enablement patches for that chip; compile-tested only on the SSD1325/1327. ssd133x_clear_screen() has the same problem, left alone here for lack of hardware.

 drivers/gpu/drm/solomon/ssd130x.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
drivers/gpu/drm/solomon/ssd130x.c +11 -0
index 0b0fc6fe3df..256d370e240 100644
@@ -900,9 +900,20 @@ static void ssd132x_clear_screen(struct ssd130x_device *ssd130x, u8 *data_array)
 {
 	unsigned int columns = DIV_ROUND_UP(ssd130x->width, SSD132X_SEGMENT_WIDTH);
 	unsigned int height = ssd130x->height;
+	int ret;
 
 	memset(data_array, 0, columns * height);
 
+	/* Set column start and end */
+	ret = ssd130x_write_cmd(ssd130x, 3, SSD132X_SET_COL_RANGE, 0, columns - 1);
+	if (ret < 0)
+		return;
+
+	/* Set row start and end */
+	ret = ssd130x_write_cmd(ssd130x, 3, SSD132X_SET_ROW_RANGE, 0, height - 1);
+	if (ret < 0)
+		return;
+
 	/* Write out update in one go since horizontal addressing mode is used */
 	ssd130x_write_data(ssd130x, data_array, columns * height);
 }
Signature · 1 line
2.43.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

Pre-existing issues:
- [High] 1-byte out-of-bounds read in ssd132x_update_rect() when the device width is configured to an odd number.
- [High] Pre-existing logic error in atomic_disable callbacks causes ssd132x_clear_screen() to be completely unreachable dead code, meaning the patch applies a fix to a code path that never executes. --

--- Patch [1]: [PATCH] drm/ssd130x: Set the address window in ssd132x_clear_screen() --- commit ce857c983f38da1567b74e3e8854ddf27505140a
Author: Fabio Piparo <[email protected]>

drm/ssd130x: Set the address window in ssd132x_clear_screen()

ssd132x_clear_screen() sends its zeroes without setting the column and row ranges first, so they land in whatever window the last ssd132x_update_rect() left programmed. A partial final update then leaves the rest of the screen uncleared, with fbcon the last damage is typically the cursor cell, so blanking keeps the console content on the panel.

Set the full-screen window first, as ssd130x_clear_screen() already does.

drivers/gpu/drm/solomon/ssd130x.c static void ssd132x_clear_screen(struct ssd130x_device *ssd130x, u8 *data_array) · line 900

Quoted 6 lines
drivers/gpu/drm/solomon/ssd130x.c +0 -0
index 0b0fc6fe3df23..256d370e24034 100644
@@ -900,9 +900,20 @@ static void ssd132x_clear_screen(struct ssd130x_device *ssd130x, u8 *data_array)
 {

[Severity: High]
This is a pre-existing issue, but is ssd132x_clear_screen() ever actually called during plane disable?

Looking at ssd132x_primary_plane_atomic_disable() where this is used, the function returns early if the new plane state's crtc is NULL:

drivers/gpu/drm/solomon/ssd130x.c:ssd132x_primary_plane_atomic_disable() {
    ...
	if (!plane_state->crtc)
		return;
    ...
	ssd132x_clear_screen(ssd130x, ssd130x_crtc_state->data_array);
}

Since drm_atomic_get_new_plane_state() returns a state with a NULL crtc pointer during a disable transition, doesn't this early return always trigger, preventing the clear_screen fix from executing?

Quoted 9 lines
drivers/gpu/drm/solomon/ssd130x.c +4 -0
 	unsigned int columns = DIV_ROUND_UP(ssd130x->width, SSD132X_SEGMENT_WIDTH);
 	unsigned int height = ssd130x->height;
+	int ret;
 
 	memset(data_array, 0, columns * height);
 
+	/* Set column start and end */
+	ret = ssd130x_write_cmd(ssd130x, 3, SSD132X_SET_COL_RANGE, 0, columns - 1);
+	if (ret < 0)

drivers/gpu/drm/solomon/ssd130x.c static void ssd132x_clear_screen(struct ssd130x_device *ssd130x, u8 *data_array) · line 919

Quoted 10 lines
drivers/gpu/drm/solomon/ssd130x.c +7 -0
+		return;
+
+	/* Set row start and end */
+	ret = ssd130x_write_cmd(ssd130x, 3, SSD132X_SET_ROW_RANGE, 0, height - 1);
+	if (ret < 0)
+		return;
+
 	/* Write out update in one go since horizontal addressing mode is used */
 	ssd130x_write_data(ssd130x, data_array, columns * height);
 }

[Severity: High]
This isn't a bug introduced by this patch, but can ssd132x_update_rect() read out of bounds if the device tree configures an odd solomon,width (e.g., 127)?

In ssd132x_primary_plane_atomic_check(), the shadow buffer is allocated as exactly width * height bytes for DRM_FORMAT_R8:

drivers/gpu/drm/solomon/ssd130x.c:ssd132x_primary_plane_atomic_check() {
    ...
	ssd130x_state->buffer = kcalloc(pitch, ssd130x->height, GFP_KERNEL);
    ...
}

In ssd132x_update_rect(), the loop increments by segment_width (2):

drivers/gpu/drm/solomon/ssd130x.c:ssd132x_update_rect() {
    ...
	for (i = 0; i < height; i++) {
		for (j = 0; j < width; j += segment_width) {
			u8 n1 = buf[i * width + j];
			u8 n2 = buf[i * width + j + 1];
    ...
}

If width is 127, on the last iteration of the last row (i = height - 1, j = 126), doesn't buf[i * width + j + 1] read buf[height * 127], which is 1 byte past the end of the allocated buffer?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#2 lore raw

I can confirm that one of Sashiko's findings is correct, and it's not limited to my use case: with the early return on !plane_state->crtc, the clear in this path never runs at all.

I also have to retract the testing note. I did more testing, and the blackout I had attributed to clear_screen during bring-up was something else entirely, not the clear writing RAM. So "Exercised on an SPI SSD1322" is wrong. The asymmetry itself is still valid I think (the sibling ssd130x_clear_screen() sets the window, this one doesn't), and the patch stands on inspection and compile testing only. Happy to reword the commit message in a v2 if the cleanup is still wanted, or drop it.

The other finding, the out-of-bounds read in ssd132x_update_rect(), looks real too, but it's pre-existing and separate from this patch.

Thanks,
Fabio

#3 lore raw
In reply to Fabio Piparo

Hi Fabio,

Quoted 3 lines

ssd132x_clear_screen() sends its zeroes without setting the column and row ranges first, so they land in whatever window the last ssd132x_update_rect() left programmed.

Good catch, I noticed the same asymmetry. But as Sashiko pointed out and you confirmed, ssd132x_clear_screen() is not reachable today, so there is no user-visible bug being fixed here. I don't think this should go to stable, and I would drop both the Cc: stable and the Fixes: tag in a v2.

There is a second reason the clear cannot work in its current form. The driver does not override .atomic_commit_tail, so drm_atomic_helper_commit_tail() runs drm_atomic_helper_commit_modeset_disables() before drm_atomic_helper_commit_planes(). By the time the plane's atomic_disable runs, ssd130x_encoder_atomic_disable() has already sent DISPLAY_OFF and called ssd130x_power_off(). So even with the !plane_state->crtc guard fixed, the clear would be writing pixels into a controller that is already powered down.

None of this makes the patch wrong - I do believe in making functions do what they are supposed to do. It does mean this is a cleanup rather than a fix, though, and that it cannot be verified on hardware until the disable path itself is sorted out.

drivers/gpu/drm/solomon/ssd130x.c static void ssd132x_clear_screen(struct ssd130x_device *ssd130x, u8 *data_array) · line 915

Quoted 7 lines
drivers/gpu/drm/solomon/ssd130x.c +7 -0
+	ret = ssd130x_write_cmd(ssd130x, 3, SSD132X_SET_COL_RANGE, 0, columns - 1);
+	if (ret < 0)
+		return;
+
+	ret = ssd130x_write_cmd(ssd130x, 3, SSD132X_SET_ROW_RANGE, 0, height - 1);
+	if (ret < 0)
+		return;

This duplicates the window setup that ssd132x_update_rect() already does. I would rather see those two commands factored out into a small helper (e.g. ssd132x_set_window()) and called from both places.

Note that ssd132x_update_rect() cannot just be called from ssd132x_clear_screen() the way ssd133x_update_rect() could be from ssd133x_clear_screen(): it takes the R8 shadow buffer and packs pairs of pixels into data_array, and clear_screen() only has the data_array from the CRTC state. In the SSD135x series I have in flight, ssd135x_clear_screen() does call ssd135x_update_rect() with a full-screen rect, but only because no pixel conversion is involved there.

Signature · 2 lines
Thanks,
Amit
#4 lore raw

Agreed on all points, thanks. I'll send a v2 with the set_window helper and a reworded message when I'm
back in September.

Thanks,
Fabio

maintainer Reviewed-by on v1 1/1
#7 lore raw
In reply to Fabio Piparo

Fabio Piparo <[email protected]> writes:

Hello Fabio,

Sorry for the delay. I was on vacation for a few weeks and I'm just catching up on email.

Quoted 10 lines

ssd132x_clear_screen() sends its zeroes without setting the column and row ranges first, so they land in whatever window the last ssd132x_update_rect() left programmed. A partial final update then leaves the rest of the screen uncleared, with fbcon the last damage is typically the cursor cell, so blanking keeps the console content on the panel.

Set the full-screen window first, as ssd130x_clear_screen() already does.

All this makes a lot of sense to me.

Quoted 2 lines
Fixes fdd591e00a9c ("drm/ssd130x: Add support for the SSD132x OLED controller family")
Assisted-by LLM

I was going to mention that this isn't the convention documented in
https://docs.kernel.org/process/coding-assistants.html#attribution,
but I just noticed that there is a recent commit 816d9992d9ed ("coding-assistants: simplify attribution").

So your tag is correct and I learned today that "AGENT_NAME:MODEL_VERSION" got deprecated.

Quoted 10 lines

---
Exercised on an SPI SSD1322 under local enablement patches for that chip; compile-tested only on the SSD1325/1327. ssd133x_clear_screen() has the same problem, left alone here for lack of hardware.

 drivers/gpu/drm/solomon/ssd130x.c | 11 +++++++++++
 1 file changed, 11 insertions(+)

I'll push to drm-misc (drm-misc-fixes). Thanks for fixing this issue!

Signature · 5 lines
Best regards,

Javier Martinez Canillas
Core Platforms
Red Hat
#6 lore raw
In reply to Amit Barzilai

Amit Barzilai <[email protected]> writes:

Hello Amit,

Quoted 21 lines

Hi Fabio,

Quoted 3 lines

ssd132x_clear_screen() sends its zeroes without setting the column and row ranges first, so they land in whatever window the last ssd132x_update_rect() left programmed.

Good catch, I noticed the same asymmetry. But as Sashiko pointed out and you confirmed, ssd132x_clear_screen() is not reachable today, so there is no user-visible bug being fixed here. I don't think this should go to stable, and I would drop both the Cc: stable and the Fixes: tag in a v2.

There is a second reason the clear cannot work in its current form. The driver does not override .atomic_commit_tail, so drm_atomic_helper_commit_tail() runs drm_atomic_helper_commit_modeset_disables() before drm_atomic_helper_commit_planes(). By the time the plane's atomic_disable runs, ssd130x_encoder_atomic_disable() has already sent DISPLAY_OFF and called ssd130x_power_off(). So even with the !plane_state->crtc guard fixed, the clear would be writing pixels into a controller that is already powered down.

Quoted 5 lines

None of this makes the patch wrong - I do believe in making functions do what they are supposed to do. It does mean this is a cleanup rather than a fix, though, and that it cannot be verified on hardware until the disable path itself is sorted out.

Agreed with your points.

Quoted 12 lines

drivers/gpu/drm/solomon/ssd130x.c static void ssd132x_clear_screen(struct ssd130x_device *ssd130x, u8 *data_array) · line 915

Quoted 7 lines
drivers/gpu/drm/solomon/ssd130x.c +7 -0
+	ret = ssd130x_write_cmd(ssd130x, 3, SSD132X_SET_COL_RANGE, 0, columns - 1);
+	if (ret < 0)
+		return;
+
+	ret = ssd130x_write_cmd(ssd130x, 3, SSD132X_SET_ROW_RANGE, 0, height - 1);
+	if (ret < 0)
+		return;

This duplicates the window setup that ssd132x_update_rect() already does. I would rather see those two commands factored out into a small helper (e.g. ssd132x_set_window()) and called from both places.

Indeed. For ssd130x we have ssd130x_set_col_range() and ssd130x_set_page_range() which do some sanity check before sending the commands to the display controller.

Maybe instead of a ssd132x_set_window(), we could add ssd132x_set_col_range() and ssd132x_set_row_range() ?

Quoted 8 lines

Note that ssd132x_update_rect() cannot just be called from ssd132x_clear_screen() the way ssd133x_update_rect() could be from ssd133x_clear_screen(): it takes the R8 shadow buffer and packs pairs of pixels into data_array, and clear_screen() only has the data_array from the CRTC state. In the SSD135x series I have in flight, ssd135x_clear_screen() does call ssd135x_update_rect() with a full-screen rect, but only because no pixel conversion is involved there.

Correct.

Signature · 5 lines
Best regards,

Javier Martinez Canillas
Core Platforms
Red Hat
#5 lore raw
In reply to Fabio Piparo
Quoted 7 lines · Fabio Piparo

Agreed on all points, thanks. I'll send a v2 with the set_window helper and a reworded message when I'm
back in September.

Thanks,
Fabio

Thanks, I said that would push your patch but I'll wait for your v2 instead.

Signature · 5 lines
Best regards,

Javier Martinez Canillas
Core Platforms
Red Hat