Skip to contentSkip to conversation

drm/panic: Split into core and helpers

PATCH Superseded Thomas Zimmermann 3 participants 20 messages Aug 24

Subsystems (27)

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Thomas Zimmermann

author maintainer
#0 lore raw

v2 cover drm/panic: Split into core and helpers

Split DRM's current panic handling into a core and a helper module. The core is still part of DRM's core library. The panic helpers are part of DRM's KMS helper module (for now).

The motivation behind this series is to get panic handling's drawing code out of the DRM core. At some point it should share a single module with DRM's other drawing and format conversion.

With the series applied, panic handling follows DRM's established design pattern of having a DRM core interface with a dedicated implementation in each driver. Drivers share common code in helper modules. For panic handling, the DRM core still contains the panic entry points, the parameters and the debugfs interface. All drawing is now located in the panic helpers.

Patches 1 to 4 improve several issues in the current panic code. It makes sense to fix them before moving the core around.

Patches 5 to 8 put handling of all panic state and locking into a single place, drm_panic_display_panic_screen(). It is maintained by panic handling's DRM core code. This also enables the test cases to work more independently from the kernel's configuration.

Patches 9 to 12 split the panic-handling code into the core and panic helpers. The former maintain all state, locking and public interfaces. The latter handles drawing and details of the involved framebuffers.

DRM devices that offer panic handling provide a single callback, struct drm_plane_funcs.display_panic_screen. A new initializer macro makes it point to the existing panic-drawing implementation. Hence there's no change in features or functionality. If panic handling is configured off, it all clears out to NULL.

Patch 13 reworks the test cases slighly, so that they can be build as a separate module. Do 'modprobe drm_panic_helper_test' to run them.

Tested under bochs, i915, amdgpu with debugfs and test cases.

Future directions:

- Panic handling shares drawing code with format conversion and clients. There's even more drawing code in vkms. Later series' can attempt to unify and harmonize all these implementations in a single draw module.

- The drawing helper drm_plane_helper_display_panic_screen() still implements various access schemes (map, set_pixel, memcpy) in a single function. It could be split up into specialized helpers.

- If we ever need a driver-specific helper, it is now possible to implement a custom version of display_panic_screen.

v2:
- resolve the various Kconfig issues (Randy)
- remove changes to qrbuf allocation (Jocelyn, Geert)
- fix possible URL format-string attack
- make test cases work again uder certain edge cases
- add better error reporting from failed draw_screen
- filter out non-primary planes from panic handling

Thomas Zimmermann (13):
drm/panic: Do not use un-escaped URL as format string drm/panic: Test address from kmap op for NULL drm/panic: Return -EINVAL if font is not available drm/panic: Return errno codes if panic output fails drm/panic: Pass colors to draw_panic_dispatch() drm/panic: Pass global module parameters to drm_panic_dispatch() drm/panic: Retry in dispatch function if panic output fails drm/panic: Split draw_panic_plane()
drm/panic: Restrict to primary planes; unconditionally unregister drm/panic: Display panic screen via per-plane callback drm/panic: Internalize panic locking in DRM core and helpers drm/panic: Move panic display code into helper library drm/panic: Compile KUnit tests as module

 Documentation/gpu/drm-kms-helpers.rst         |  11 +-
 Documentation/gpu/drm-kms.rst                 |   3 +
 MAINTAINERS                                   |  11 +-
 drivers/gpu/drm/Kconfig                       |   9 +-
 drivers/gpu/drm/Kconfig.debug                 |   1 +
 drivers/gpu/drm/Makefile                      |   3 +-
 .../amd/display/amdgpu_dm/amdgpu_dm_plane.c   |   2 +
 drivers/gpu/drm/ast/ast_mode.c                |   2 +
 drivers/gpu/drm/drm_atomic_helper.c           |   2 +-
 drivers/gpu/drm/drm_crtc_internal.h           |   2 -
 drivers/gpu/drm/drm_drv.c                     |   3 +-
 drivers/gpu/drm/drm_kms_helper_common.c       |  14 +
 drivers/gpu/drm/drm_modeset_helper.c          |   2 +
 drivers/gpu/drm/drm_panic.c                   | 919 +-----------------
 drivers/gpu/drm/drm_panic_helper.c            | 918 +++++++++++++++++
 ...drm_panic_qr.rs => drm_panic_helper_qr.rs} |   4 +-
 drivers/gpu/drm/drm_panic_internal.h          |  69 ++
 drivers/gpu/drm/hyperv/hyperv_drm_modeset.c   |   2 +
 drivers/gpu/drm/i915/display/i9xx_plane.c     |   3 +
 .../drm/i915/display/skl_universal_plane.c    |   4 +
 drivers/gpu/drm/imx/ipuv3/ipuv3-plane.c       |   2 +
 drivers/gpu/drm/mgag200/mgag200_drv.h         |   4 +-
 drivers/gpu/drm/nouveau/dispnv50/wndw.c       |   2 +
 .../gpu/drm/renesas/rcar-du/rcar_du_plane.c   |   2 +
 .../drm/renesas/shmobile/shmob_drm_plane.c    |   2 +
 drivers/gpu/drm/sysfb/drm_sysfb_helper.h      |   4 +-
 drivers/gpu/drm/sysfb/vesadrm.c               |   1 +
 drivers/gpu/drm/tests/Makefile                |   1 +
 ...m_panic_test.c => drm_panic_helper_test.c} |  67 +-
 drivers/gpu/drm/tidss/tidss_plane.c           |   2 +
 drivers/gpu/drm/tiny/bochs.c                  |   4 +-
 drivers/gpu/drm/virtio/virtgpu_plane.c        |   2 +
 include/drm/drm_mode_config.h                 |   4 +-
 include/drm/drm_modeset_helper_vtables.h      |  11 +-
 include/drm/drm_panic.h                       | 117 +--
 include/drm/drm_panic_helper.h                |  40 +
 include/drm/drm_plane.h                       |  57 ++
 rust/bindings/bindings_helper.h               |   4 +-
 38 files changed, 1273 insertions(+), 1037 deletions(-)
 create mode 100644 drivers/gpu/drm/drm_panic_helper.c
 rename drivers/gpu/drm/{drm_panic_qr.rs => drm_panic_helper_qr.rs} (99%)
 create mode 100644 drivers/gpu/drm/drm_panic_internal.h
 rename drivers/gpu/drm/tests/{drm_panic_test.c => drm_panic_helper_test.c} (79%)
 create mode 100644 include/drm/drm_panic_helper.h

base-commit: dc462ab791b686c48545c160ecc81be64f77a846
prerequisite-patch-id: c67e5d886a47b7d0266d81100837557fda34cb24
prerequisite-patch-id: a5a973e527c88a5b47053d7a72aefe0b550197cb
prerequisite-patch-id: b9adc9622920a3e70168e672c2c92795b3e3a106
prerequisite-patch-id: 5030de433a01c2e99056cadb676a8e2ba35f055a

Signature · 1 line
2.55.0
author maintainer
#1 lore raw

PATCH v2 1/13 drm/panic: Do not use un-escaped URL as format string

Replace the direct use of DRM_PANIC_SCREEN_QR_CODE_URL in snprintf() with a %s conversion specifier. Avoids a possible vulnerabilty from a crafted URL string.

As the attacker needs to control the kernel at the Kconfig level, the potential for an exploit is likely low.

Fixes cb5164ac43d0 ("drm/panic: Add a QR code panic screen")
Cc Javier Martinez Canillas <[email protected]>
Cc Jocelyn Falempe <[email protected]>
Cc <[email protected]> # v6.12+
---
 drivers/gpu/drm/drm_panic.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/gpu/drm/drm_panic.c +2 -1
index e576c4791861..ba21df4be338 100644
@@ -658,7 +658,8 @@ static int drm_panic_get_qr_code_url(u8 **qr_image)
 	char *kmsg;
 	int max_qr_data_size, url_len;
 
-	url_len = snprintf(url, sizeof(url), CONFIG_DRM_PANIC_SCREEN_QR_CODE_URL "?a=%s&v=%s&z=",
+	url_len = snprintf(url, sizeof(url), "%s?a=%s&v=%s&z=",
+			   CONFIG_DRM_PANIC_SCREEN_QR_CODE_URL,
 			   utsname()->machine, utsname()->release);
 
 	max_qr_data_size = drm_panic_qr_max_data_size(panic_qr_version, url_len);
Signature · 1 line
2.55.0
author maintainer
#2 lore raw

PATCH v2 2/13 drm/panic: Test address from kmap op for NULL

In reply to Thomas Zimmermann

Do nothing if kmap_local_page_try_from_panic() returns NULL. This is not likely to hapen, as the previous page from the same memory area has been mapped successfully.

Fixes 23437509a694 ("drm/panic: Fix 24bit pixel crossing page boundaries")
Cc Javier Martinez Canillas <[email protected]>
Cc Jocelyn Falempe <[email protected]>
Cc <[email protected]> # v6.18+
---
 drivers/gpu/drm/drm_panic.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/gpu/drm/drm_panic.c +2 -0
index ba21df4be338..d28ce8878e05 100644
@@ -178,6 +178,8 @@ static void drm_panic_write_pixel24_xpage(void *vaddr, struct page *next_page,
 	u8 *p = vaddr + offset;
 
 	vaddr2 = kmap_local_page_try_from_panic(next_page);
+	if (!vaddr2)
+		return;
 
 	*p++ = color & 0xff;
 	color >>= 8;
Signature · 1 line
2.55.0
author maintainer
#3 lore raw

PATCH v2 3/13 drm/panic: Return -EINVAL if font is not available

In reply to Thomas Zimmermann

Font lookup does not allocate memory. Hence reporting ENOMEM on failed lookups is misleading. Report EINVAL instead.

---
 drivers/gpu/drm/drm_panic.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/gpu/drm/drm_panic.c +3 -1
index d28ce8878e05..62490d32019c 100644
@@ -751,8 +751,10 @@ static int _draw_panic_screen_qr_code(struct drm_scanout_buffer *sb)
 	int qr_width, qr_canvas_width, qr_pitch, v_margin;
 	u8 *qr_image;
 
-	if (!font || !qrbuf1 || !qrbuf2 || !stream.workspace)
+	if (!qrbuf1 || !qrbuf2 || !stream.workspace)
 		return -ENOMEM;
+	if (!font)
+		return -EINVAL;
 
 	r_screen = DRM_RECT_INIT(0, 0, sb->width, sb->height);
 
Signature · 1 line
2.55.0
author maintainer
#4 lore raw

PATCH v2 4/13 drm/panic: Return errno codes if panic output fails

In reply to Thomas Zimmermann

Return errno codes from the panic output helpers to detect invalid panic handling. Avoid flushing the display if an error ocured. The unflushed display output might be helpful in debugging.

For now, test the result values in the panic test cases. A later patch will add support for retrying failed panic output.

v2:
- forward return value of draw_panic_screen_user() (Sashiko)
- forward errno code from drm_panic_get_qr_code

---
 drivers/gpu/drm/drm_panic.c            | 47 +++++++++++++++++---------
 drivers/gpu/drm/tests/drm_panic_test.c | 16 +++++----
 2 files changed, 41 insertions(+), 22 deletions(-)
drivers/gpu/drm/drm_panic.c +31 -16
index 62490d32019c..cc33a2b4bb52 100644
@@ -480,7 +480,7 @@ static void drm_panic_logo_draw(struct drm_scanout_buffer *sb, struct drm_rect *
 				   fg_color);
 }
 
-static void draw_panic_screen_user(struct drm_scanout_buffer *sb)
+static int draw_panic_screen_user(struct drm_scanout_buffer *sb)
 {
 	u32 fg_color = drm_draw_color_from_xrgb8888(CONFIG_DRM_PANIC_FOREGROUND_COLOR,
 						    sb->format->format);
@@ -491,7 +491,7 @@ static void draw_panic_screen_user(struct drm_scanout_buffer *sb)
 	unsigned int msg_width, msg_height;
 
 	if (!font)
-		return;
+		return -EINVAL;
 
 	r_screen = DRM_RECT_INIT(0, 0, sb->width, sb->height);
 	drm_panic_logo_rect(&r_logo, font);
@@ -510,6 +510,8 @@ static void draw_panic_screen_user(struct drm_scanout_buffer *sb)
 		drm_panic_logo_draw(sb, &r_logo, font, fg_color);
 
 	draw_txt_rectangle(sb, font, panic_msg, panic_msg_lines, true, &r_msg, fg_color);
+
+	return 0;
 }
 
 /*
@@ -549,7 +551,7 @@ static int draw_line_with_wrap(struct drm_scanout_buffer *sb, const struct font_
  * Draw the kmsg buffer to the screen, starting from the youngest message at the bottom,
  * and going up until reaching the top of the screen.
  */
-static void draw_panic_screen_kmsg(struct drm_scanout_buffer *sb)
+static int draw_panic_screen_kmsg(struct drm_scanout_buffer *sb)
 {
 	u32 fg_color = drm_draw_color_from_xrgb8888(CONFIG_DRM_PANIC_FOREGROUND_COLOR,
 						    sb->format->format);
@@ -564,7 +566,7 @@ static void draw_panic_screen_kmsg(struct drm_scanout_buffer *sb)
 	int yoffset;
 
 	if (!font || font->width > sb->width)
-		return;
+		return -EINVAL;
 
 	yoffset = sb->height - font->height - (sb->height % font->height) / 2;
 
@@ -591,6 +593,8 @@ static void draw_panic_screen_kmsg(struct drm_scanout_buffer *sb)
 			start--;
 		}
 	}
+
+	return 0;
 }
 
 #if defined(CONFIG_DRM_PANIC_SCREEN_QR_CODE)
@@ -767,7 +771,9 @@ static int _draw_panic_screen_qr_code(struct drm_scanout_buffer *sb)
 	max_qr_size = min(3 * sb->width / 4, 3 * sb->height / 4);
 
 	qr_width = drm_panic_get_qr_code(&qr_image);
-	if (qr_width <= 0)
+	if (qr_width < 0)
+		return qr_width;
+	else if (!qr_width)
 		return -ENOSPC;
 
 	qr_canvas_width = qr_width + QR_MARGIN * 2;
@@ -808,10 +814,11 @@ static int _draw_panic_screen_qr_code(struct drm_scanout_buffer *sb)
 	return 0;
 }
 
-static void draw_panic_screen_qr_code(struct drm_scanout_buffer *sb)
+static int draw_panic_screen_qr_code(struct drm_scanout_buffer *sb)
 {
 	if (_draw_panic_screen_qr_code(sb))
-		draw_panic_screen_user(sb);
+		return draw_panic_screen_user(sb);
+	return 0;
 }
 #else
 static void drm_panic_qr_init(void) {};
@@ -882,23 +889,25 @@ static bool drm_panic_is_format_supported(const struct drm_format_info *format)
 	return drm_draw_can_convert_from_xrgb8888(format->format);
 }
 
-static void draw_panic_dispatch(struct drm_scanout_buffer *sb)
+static int draw_panic_dispatch(struct drm_scanout_buffer *sb)
 {
+	int ret;
+
 	switch (drm_panic_type) {
 	case DRM_PANIC_TYPE_KMSG:
-		draw_panic_screen_kmsg(sb);
+		ret = draw_panic_screen_kmsg(sb);
 		break;
-
 #if IS_ENABLED(CONFIG_DRM_PANIC_SCREEN_QR_CODE)
 	case DRM_PANIC_TYPE_QR:
-		draw_panic_screen_qr_code(sb);
+		ret = draw_panic_screen_qr_code(sb);
 		break;
 #endif
-
 	case DRM_PANIC_TYPE_USER:
 	default:
-		draw_panic_screen_user(sb);
+		ret = draw_panic_screen_user(sb);
 	}
+
+	return ret;
 }
 
 static void drm_panic_set_description(const char *description)
@@ -945,9 +954,15 @@ static void draw_panic_plane(struct drm_plane *plane, const char *description)
 
 	drm_panic_set_description(description);
 
-	draw_panic_dispatch(&sb);
-	if (plane->helper_private->panic_flush)
-		plane->helper_private->panic_flush(plane);
+	ret = draw_panic_dispatch(&sb);
+	if (!ret) {
+		/*
+		 * Only flush if we have a panic screen to display. Otherwise
+		 * it's probably better to leave the display output as-is.
+		 */
+		if (plane->helper_private->panic_flush)
+			plane->helper_private->panic_flush(plane);
+	}
 
 	drm_panic_clear_description();
 
drivers/gpu/drm/tests/drm_panic_test.c +10 -6
index ad2f3a2f93b6..fdd77b0cc54c 100644
@@ -30,7 +30,7 @@ struct drm_test_mode {
 	const int width;
 	const int height;
 	const u32 format;
-	void (*draw_screen)(struct drm_scanout_buffer *sb);
+	int (*draw_screen)(struct drm_scanout_buffer *sb);
 	const char *fname;
 };
 
@@ -87,7 +87,7 @@ static void drm_test_panic_screen_user_map(struct kunit *test)
 	const struct drm_test_mode *params = test->param_value;
 	char *fb;
 	int fb_size;
-	int i;
+	int i, ret;
 
 	sb->format = drm_format_info(params->format);
 	fb_size = params->width * params->height * sb->format->cpp[0];
@@ -102,7 +102,8 @@ static void drm_test_panic_screen_user_map(struct kunit *test)
 	sb->height = params->height;
 	sb->pitch[0] = params->width * sb->format->cpp[0];
 
-	params->draw_screen(sb);
+	ret = params->draw_screen(sb);
+	KUNIT_ASSERT_EQ(test, ret, 0);
 
 	for (i = 0; i < fb_size; i++)
 		drm_panic_check_color_byte(test, fb[i]);
@@ -119,7 +120,7 @@ static void drm_test_panic_screen_user_page(struct kunit *test)
 {
 	struct drm_scanout_buffer *sb = test->priv;
 	const struct drm_test_mode *params = test->param_value;
-	int fb_size, p, i, npages;
+	int fb_size, p, i, npages, ret;
 	struct page **pages;
 	u8 *vaddr;
 
@@ -146,7 +147,8 @@ static void drm_test_panic_screen_user_page(struct kunit *test)
 	sb->height = params->height;
 	sb->pitch[0] = params->width * sb->format->cpp[0];
 
-	params->draw_screen(sb);
+	ret = params->draw_screen(sb);
+	KUNIT_ASSERT_EQ(test, ret, 0);
 
 	for (p = 0; p < npages; p++) {
 		int bytes_in_page = (p == npages - 1) ? fb_size - p * PAGE_SIZE : PAGE_SIZE;
@@ -182,6 +184,7 @@ static void drm_test_panic_screen_user_set_pixel(struct kunit *test)
 {
 	struct drm_scanout_buffer *sb = test->priv;
 	const struct drm_test_mode *params = test->param_value;
+	int ret;
 
 	sb->format = drm_format_info(params->format);
 	sb->set_pixel = drm_test_panic_set_pixel;
@@ -189,7 +192,8 @@ static void drm_test_panic_screen_user_set_pixel(struct kunit *test)
 	sb->height = params->height;
 	sb->private = test;
 
-	params->draw_screen(sb);
+	ret = params->draw_screen(sb);
+	KUNIT_ASSERT_EQ(test, ret, 0);
 }
 
 static void drm_test_panic_desc(const struct drm_test_mode *t, char *desc)
Signature · 1 line
2.55.0
#5 lore raw

Re: [PATCH v2 04/13] drm/panic: Return errno codes if panic output fails

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:

New issues:
- [Medium] Using KUNIT_ASSERT_EQ causes kernel test memory to leak if the drawing function returns an error, bypassing explicit resource cleanup.

Pre-existing issues:
- [High] Concurrent invocation of the debugfs trigger for planes on different devices causes data races on global panic buffers, leading to memory corruption. --

--- Patch [4]: [PATCH v2 04/13] drm/panic: Return errno codes if panic output fails --- commit 6a8545466378db489975c829328cc895f57550b4
Author: Thomas Zimmermann <[email protected]>

drm/panic: Return errno codes if panic output fails

Return errno codes from the panic output helpers to detect invalid panic handling and avoid flushing the display if an error occurred. The panic test cases are updated to assert these new return values.

Quoted 4 lines
drivers/gpu/drm/drm_panic.c +0 -0
index 62490d32019cc..cc33a2b4bb52a 100644

[ ... ]

drivers/gpu/drm/drm_panic.c static void draw_panic_plane(struct drm_plane *plane, const char *description) · line 957

Quoted 8 lines
drivers/gpu/drm/drm_panic.c +1 -3
@@ -945,9 +954,15 @@ static void draw_panic_plane(struct drm_plane *plane, const char *description)
 
 	drm_panic_set_description(description);
 
-	draw_panic_dispatch(&sb);
-	if (plane->helper_private->panic_flush)
-		plane->helper_private->panic_flush(plane);
+	ret = draw_panic_dispatch(&sb);

[Severity: High]
This is a pre-existing issue, but can concurrent writes to the debugfs trigger corrupt global buffers?

If a user concurrently writes to the debugfs trigger files (drm_panic_plane_%d) for planes on different DRM devices, draw_panic_plane() executes concurrently because dev->mode_config.panic_lock is a per-device lock.

When DRM_PANIC_TYPE_QR is active, multiple threads could enter drm_panic_get_qr_code_url() and modify the global static variables qrbuf1, qrbuf2, and stream.workspace without cross-device synchronization:

drivers/gpu/drm/drm_panic.c:drm_panic_get_qr_code_url() {
    ...
    stream.next_in = kmsg;
    stream.avail_in = kmsg_len;
    stream.total_in = 0;
    stream.next_out = qrbuf2;
    stream.avail_out = QR_BUFFER2_SIZE;
    stream.total_out = 0;
    ...
}

Could this cause memory corruption during simultaneous debugfs writes?

Quoted 10 lines
drivers/gpu/drm/drm_panic.c +8 -0
+	if (!ret) {
+		/*
+		 * Only flush if we have a panic screen to display. Otherwise
+		 * it's probably better to leave the display output as-is.
+		 */
+		if (plane->helper_private->panic_flush)
+			plane->helper_private->panic_flush(plane);
+	}
 
 	drm_panic_clear_description();

[ ... ]

Quoted 4 lines
drivers/gpu/drm/tests/drm_panic_test.c +0 -0
index ad2f3a2f93b6a..fdd77b0cc54c9 100644

[ ... ]

drivers/gpu/drm/tests/drm_panic_test.c static void drm_test_panic_screen_user_map(struct kunit *test) · line 106

Quoted 7 lines
drivers/gpu/drm/tests/drm_panic_test.c +2 -1
@@ -102,7 +102,8 @@ static void drm_test_panic_screen_user_map(struct kunit *test)
 	sb->height = params->height;
 	sb->pitch[0] = params->width * sb->format->cpp[0];
 
-	params->draw_screen(sb);
+	ret = params->draw_screen(sb);
+	KUNIT_ASSERT_EQ(test, ret, 0);

[Severity: Medium]
Will this KUNIT_ASSERT_EQ() cause memory leaks if the test fails?

If params->draw_screen() returns an error, KUNIT_ASSERT_EQ() will abort the test thread immediately. This means the test will skip explicit resource cleanup, such as the vfree(fb) call at the end of drm_test_panic_screen_user_map().

Should this use KUNIT_EXPECT_EQ() instead to allow the cleanup code to run?

Quoted 3 lines
drivers/gpu/drm/tests/drm_panic_test.c +0 -0
 	for (i = 0; i < fb_size; i++)
 		drm_panic_check_color_byte(test, fb[i]);

[ ... ]

drivers/gpu/drm/tests/drm_panic_test.c static void drm_test_panic_screen_user_page(struct kunit *test) · line 151

Quoted 7 lines
drivers/gpu/drm/tests/drm_panic_test.c +2 -1
@@ -146,7 +147,8 @@ static void drm_test_panic_screen_user_page(struct kunit *test)
 	sb->height = params->height;
 	sb->pitch[0] = params->width * sb->format->cpp[0];
 
-	params->draw_screen(sb);
+	ret = params->draw_screen(sb);
+	KUNIT_ASSERT_EQ(test, ret, 0);

[Severity: Medium]
Similarly here in drm_test_panic_screen_user_page(), if KUNIT_ASSERT_EQ() aborts the test on failure, will it skip the free_pages: cleanup label, resulting in a permanent memory leak?

Quoted 3 lines
drivers/gpu/drm/tests/drm_panic_test.c +0 -0
 	for (p = 0; p < npages; p++) {
 		int bytes_in_page = (p == npages - 1) ? fb_size - p * PAGE_SIZE : PAGE_SIZE;
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
author maintainer
#6 lore raw

PATCH v2 5/13 drm/panic: Pass colors to draw_panic_dispatch()

In reply to Thomas Zimmermann

Set the panic-screen colors in draw_panic_plane() as a single point of truth. Pass the values into the individual drawing functions.

In the test cases, replace the Kconfig-defined colors with white and black. Allows for removal of the tests' limitations and dependency on the exact values. All tests now always verify the generated output.

Hardcoding the test colors will later also allow for building the test case without having DRM panic handling enabled.

---
 drivers/gpu/drm/drm_panic.c            | 54 +++++++++++++++-----------
 drivers/gpu/drm/tests/drm_panic_test.c | 26 +++++++++----
 2 files changed, 49 insertions(+), 31 deletions(-)
drivers/gpu/drm/drm_panic.c +31 -23
index cc33a2b4bb52..834653d288a9 100644
@@ -480,12 +480,8 @@ static void drm_panic_logo_draw(struct drm_scanout_buffer *sb, struct drm_rect *
 				   fg_color);
 }
 
-static int draw_panic_screen_user(struct drm_scanout_buffer *sb)
+static int draw_panic_screen_user(struct drm_scanout_buffer *sb, u32 fg_color, u32 bg_color)
 {
-	u32 fg_color = drm_draw_color_from_xrgb8888(CONFIG_DRM_PANIC_FOREGROUND_COLOR,
-						    sb->format->format);
-	u32 bg_color = drm_draw_color_from_xrgb8888(CONFIG_DRM_PANIC_BACKGROUND_COLOR,
-						    sb->format->format);
 	const struct font_desc *font = get_default_font(sb->width, sb->height, NULL, NULL);
 	struct drm_rect r_screen, r_logo, r_msg;
 	unsigned int msg_width, msg_height;
@@ -493,6 +489,9 @@ static int draw_panic_screen_user(struct drm_scanout_buffer *sb)
 	if (!font)
 		return -EINVAL;
 
+	fg_color = drm_draw_color_from_xrgb8888(fg_color, sb->format->format);
+	bg_color = drm_draw_color_from_xrgb8888(bg_color, sb->format->format);
+
 	r_screen = DRM_RECT_INIT(0, 0, sb->width, sb->height);
 	drm_panic_logo_rect(&r_logo, font);
 
@@ -551,12 +550,8 @@ static int draw_line_with_wrap(struct drm_scanout_buffer *sb, const struct font_
  * Draw the kmsg buffer to the screen, starting from the youngest message at the bottom,
  * and going up until reaching the top of the screen.
  */
-static int draw_panic_screen_kmsg(struct drm_scanout_buffer *sb)
+static int draw_panic_screen_kmsg(struct drm_scanout_buffer *sb, u32 fg_color, u32 bg_color)
 {
-	u32 fg_color = drm_draw_color_from_xrgb8888(CONFIG_DRM_PANIC_FOREGROUND_COLOR,
-						    sb->format->format);
-	u32 bg_color = drm_draw_color_from_xrgb8888(CONFIG_DRM_PANIC_BACKGROUND_COLOR,
-						    sb->format->format);
 	const struct font_desc *font = get_default_font(sb->width, sb->height, NULL, NULL);
 	struct drm_rect r_screen = DRM_RECT_INIT(0, 0, sb->width, sb->height);
 	struct kmsg_dump_iter iter;
@@ -568,6 +563,9 @@ static int draw_panic_screen_kmsg(struct drm_scanout_buffer *sb)
 	if (!font || font->width > sb->width)
 		return -EINVAL;
 
+	fg_color = drm_draw_color_from_xrgb8888(fg_color, sb->format->format);
+	bg_color = drm_draw_color_from_xrgb8888(bg_color, sb->format->format);
+
 	yoffset = sb->height - font->height - (sb->height % font->height) / 2;
 
 	/* Fill with the background color, and draw text on top */
@@ -742,12 +740,9 @@ static int drm_panic_get_qr_code(u8 **qr_image)
 /*
  * Draw the panic message at the center of the screen, with a QR Code
  */
-static int _draw_panic_screen_qr_code(struct drm_scanout_buffer *sb)
+static int _draw_panic_screen_qr_code(struct drm_scanout_buffer *sb,
+				      u32 fg_color, u32 bg_color)
 {
-	u32 fg_color = drm_draw_color_from_xrgb8888(CONFIG_DRM_PANIC_FOREGROUND_COLOR,
-						    sb->format->format);
-	u32 bg_color = drm_draw_color_from_xrgb8888(CONFIG_DRM_PANIC_BACKGROUND_COLOR,
-						    sb->format->format);
 	const struct font_desc *font = get_default_font(sb->width, sb->height, NULL, NULL);
 	struct drm_rect r_screen, r_logo, r_msg, r_qr, r_qr_canvas;
 	unsigned int max_qr_size, scale;
@@ -760,6 +755,9 @@ static int _draw_panic_screen_qr_code(struct drm_scanout_buffer *sb)
 	if (!font)
 		return -EINVAL;
 
+	fg_color = drm_draw_color_from_xrgb8888(fg_color, sb->format->format);
+	bg_color = drm_draw_color_from_xrgb8888(bg_color, sb->format->format);
+
 	r_screen = DRM_RECT_INIT(0, 0, sb->width, sb->height);
 
 	drm_panic_logo_rect(&r_logo, font);
@@ -814,10 +812,10 @@ static int _draw_panic_screen_qr_code(struct drm_scanout_buffer *sb)
 	return 0;
 }
 
-static int draw_panic_screen_qr_code(struct drm_scanout_buffer *sb)
+static int draw_panic_screen_qr_code(struct drm_scanout_buffer *sb, u32 fg_color, u32 bg_color)
 {
-	if (_draw_panic_screen_qr_code(sb))
-		return draw_panic_screen_user(sb);
+	if (_draw_panic_screen_qr_code(sb, fg_color, bg_color))
+		return draw_panic_screen_user(sb, fg_color, bg_color);
 	return 0;
 }
 #else
@@ -889,22 +887,22 @@ static bool drm_panic_is_format_supported(const struct drm_format_info *format)
 	return drm_draw_can_convert_from_xrgb8888(format->format);
 }
 
-static int draw_panic_dispatch(struct drm_scanout_buffer *sb)
+static int draw_panic_dispatch(struct drm_scanout_buffer *sb, u32 fg_color, u32 bg_color)
 {
 	int ret;
 
 	switch (drm_panic_type) {
 	case DRM_PANIC_TYPE_KMSG:
-		ret = draw_panic_screen_kmsg(sb);
+		ret = draw_panic_screen_kmsg(sb, fg_color, bg_color);
 		break;
 #if IS_ENABLED(CONFIG_DRM_PANIC_SCREEN_QR_CODE)
 	case DRM_PANIC_TYPE_QR:
-		ret = draw_panic_screen_qr_code(sb);
+		ret = draw_panic_screen_qr_code(sb, fg_color, bg_color);
 		break;
 #endif
 	case DRM_PANIC_TYPE_USER:
 	default:
-		ret = draw_panic_screen_user(sb);
+		ret = draw_panic_screen_user(sb, fg_color, bg_color);
 	}
 
 	return ret;
@@ -939,6 +937,16 @@ static void draw_panic_plane(struct drm_plane *plane, const char *description)
 	struct drm_scanout_buffer sb = { };
 	int ret;
 	unsigned long flags;
+#if defined(CONFIG_DRM_PANIC_FOREGROUND_COLOR)
+	u32 fg_color = CONFIG_DRM_PANIC_FOREGROUND_COLOR;
+#else
+	u32 fg_color = 0x00ffffff;
+#endif
+#if defined(CONFIG_DRM_PANIC_BACKGROUND_COLOR)
+	u32 bg_color = CONFIG_DRM_PANIC_BACKGROUND_COLOR;
+#else
+	u32 bg_color = 0x00000000;
+#endif
 
 	if (!drm_panic_trylock(plane->dev, flags))
 		return;
@@ -954,7 +962,7 @@ static void draw_panic_plane(struct drm_plane *plane, const char *description)
 
 	drm_panic_set_description(description);
 
-	ret = draw_panic_dispatch(&sb);
+	ret = draw_panic_dispatch(&sb, fg_color, bg_color);
 	if (!ret) {
 		/*
 		 * Only flush if we have a panic screen to display. Otherwise
drivers/gpu/drm/tests/drm_panic_test.c +18 -8
index fdd77b0cc54c..969a096ec840 100644
@@ -14,17 +14,10 @@
 #include <linux/units.h>
 #include <linux/vmalloc.h>
 
-/* Check the framebuffer color only if the panic colors are the default */
-#if (CONFIG_DRM_PANIC_BACKGROUND_COLOR == 0 && \
-	CONFIG_DRM_PANIC_FOREGROUND_COLOR == 0xffffff)
-
 static void drm_panic_check_color_byte(struct kunit *test, u8 b)
 {
 	KUNIT_EXPECT_TRUE(test, (b == 0 || b == 0xff));
 }
-#else
-static void drm_panic_check_color_byte(struct kunit *test, u8 b) {}
-#endif
 
 struct drm_test_mode {
 	const int width;
@@ -48,10 +41,27 @@ struct drm_test_mode {
 	.width = w, \
 	.height = h, \
 	.format = f, \
-	.draw_screen = draw_panic_screen_##name, \
+	.draw_screen = draw_panic_screen_ ## name ## _default, \
 	.fname = #name, \
 	}, \
 
+static int draw_panic_screen_user_default(struct drm_scanout_buffer *sb)
+{
+	return draw_panic_screen_user(sb, 0x00ffffff, 0x00000000);
+}
+
+static int draw_panic_screen_kmsg_default(struct drm_scanout_buffer *sb)
+{
+	return draw_panic_screen_kmsg(sb, 0x00ffffff, 0x00000000);
+}
+
+#if IS_ENABLED(CONFIG_DRM_PANIC_SCREEN_QR_CODE)
+static int draw_panic_screen_qr_code_default(struct drm_scanout_buffer *sb)
+{
+	return draw_panic_screen_qr_code(sb, 0x00ffffff, 0x00000000);
+}
+#endif
+
 static const struct drm_test_mode drm_test_modes_cases[] = {
 	DRM_TEST_MODE_LIST(user)
 	DRM_TEST_MODE_LIST(kmsg)
Signature · 1 line
2.55.0
#7 lore raw

Re: [PATCH v2 05/13] drm/panic: Pass colors to draw_panic_dispatch()

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] Using KUNIT_ASSERT_EQ with unmanaged resources causes kernel memory leaks on test failure. --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] Using KUNIT_ASSERT_EQ with unmanaged resources causes kernel memory leaks on test failure. --

--- Patch [5]: [PATCH v2 05/13] drm/panic: Pass colors to draw_panic_dispatch() --- commit dfb1a50d1181cba2ccd93fe314ac428958a579a7
Author: Thomas Zimmermann <[email protected]>

drm/panic: Pass colors to draw_panic_dispatch()

This commit sets the panic-screen colors in a central location and passes them to the individual drawing functions. In the test cases, it hardcodes these colors to remove test dependencies on the exact config values.

Quoted 4 lines
drivers/gpu/drm/tests/drm_panic_test.c +0 -0
index fdd77b0cc54c9..969a096ec840f 100644

[ ... ]

drivers/gpu/drm/tests/drm_panic_test.c struct drm_test_mode { · line 44

Quoted 6 lines
drivers/gpu/drm/tests/drm_panic_test.c +1 -1
@@ -48,10 +41,27 @@ struct drm_test_mode {
 	.width = w, \
 	.height = h, \
 	.format = f, \
-	.draw_screen = draw_panic_screen_##name, \
+	.draw_screen = draw_panic_screen_ ## name ## _default, \

[Severity: Medium]
When params->draw_screen(sb) fails, does this cause the test to leak fb?

In drm_test_panic_screen_user_map(), the KUNIT_ASSERT_EQ() macro aborts the test before vfree(fb) can execute:

drivers/gpu/drm/tests/drm_panic_test.c:drm_test_panic_screen_user_map() {
    ...
	fb = vmalloc(fb_size);
    ...
	ret = params->draw_screen(sb);
	KUNIT_ASSERT_EQ(test, ret, 0);
    ...
	vfree(fb);
}

Similarly, does drm_test_panic_screen_user_page() leak the allocated pages when the test fails? An abort here skips the free_pages cleanup block:

drivers/gpu/drm/tests/drm_panic_test.c:drm_test_panic_screen_user_page() {
    ...
	ret = params->draw_screen(sb);
	KUNIT_ASSERT_EQ(test, ret, 0);
    ...
free_pages:
	for (p = 0; p < npages; p++)
		__free_page(pages[p]);
	kfree(pages);
}
Quoted 2 lines
drivers/gpu/drm/tests/drm_panic_test.c +0 -0
 	.fname = #name, \
 	}, \
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
author maintainer
#8 lore raw

PATCH v2 6/13 drm/panic: Pass global module parameters to drm_panic_dispatch()

In reply to Thomas Zimmermann

Set the panic type and QR version in draw_panic_plane() as a single point of truth. Pass the values into the individual drawing functions.

In the test case, always create a QR code with version 40 (i.e., the highest supported version).

---
 drivers/gpu/drm/drm_panic.c            | 39 ++++++++++++++++----------
 drivers/gpu/drm/tests/drm_panic_test.c |  2 +-
 2 files changed, 25 insertions(+), 16 deletions(-)
drivers/gpu/drm/drm_panic.c +24 -15
index 834653d288a9..f597ef04c5bc 100644
@@ -654,7 +654,7 @@ static void drm_panic_qr_exit(void)
 	stream.workspace = NULL;
 }
 
-static int drm_panic_get_qr_code_url(u8 **qr_image)
+static int drm_panic_get_qr_code_url(u8 **qr_image, unsigned int qr_version)
 {
 	struct kmsg_dump_iter iter;
 	char url[256];
@@ -666,7 +666,7 @@ static int drm_panic_get_qr_code_url(u8 **qr_image)
 			   CONFIG_DRM_PANIC_SCREEN_QR_CODE_URL,
 			   utsname()->machine, utsname()->release);
 
-	max_qr_data_size = drm_panic_qr_max_data_size(panic_qr_version, url_len);
+	max_qr_data_size = drm_panic_qr_max_data_size(qr_version, url_len);
 	max_kmsg_size = min(MAX_ZLIB_RATIO * max_qr_data_size, QR_BUFFER1_SIZE);
 
 	/* get kmsg to buffer 1 */
@@ -712,11 +712,11 @@ static int drm_panic_get_qr_code_url(u8 **qr_image)
 				     qrbuf1, QR_BUFFER1_SIZE);
 }
 
-static int drm_panic_get_qr_code_raw(u8 **qr_image)
+static int drm_panic_get_qr_code_raw(u8 **qr_image, unsigned int qr_version)
 {
 	struct kmsg_dump_iter iter;
 	size_t kmsg_len;
-	size_t max_kmsg_size = min(drm_panic_qr_max_data_size(panic_qr_version, 0),
+	size_t max_kmsg_size = min(drm_panic_qr_max_data_size(qr_version, 0),
 				   QR_BUFFER1_SIZE);
 
 	kmsg_dump_rewind(&iter);
@@ -729,19 +729,20 @@ static int drm_panic_get_qr_code_raw(u8 **qr_image)
 				     qrbuf2, QR_BUFFER2_SIZE);
 }
 
-static int drm_panic_get_qr_code(u8 **qr_image)
+static int drm_panic_get_qr_code(u8 **qr_image, unsigned int qr_version)
 {
 	if (strlen(CONFIG_DRM_PANIC_SCREEN_QR_CODE_URL) > 0)
-		return drm_panic_get_qr_code_url(qr_image);
+		return drm_panic_get_qr_code_url(qr_image, qr_version);
 	else
-		return drm_panic_get_qr_code_raw(qr_image);
+		return drm_panic_get_qr_code_raw(qr_image, qr_version);
 }
 
 /*
  * Draw the panic message at the center of the screen, with a QR Code
  */
 static int _draw_panic_screen_qr_code(struct drm_scanout_buffer *sb,
-				      u32 fg_color, u32 bg_color)
+				      u32 fg_color, u32 bg_color,
+				      unsigned int qr_version)
 {
 	const struct font_desc *font = get_default_font(sb->width, sb->height, NULL, NULL);
 	struct drm_rect r_screen, r_logo, r_msg, r_qr, r_qr_canvas;
@@ -768,7 +769,7 @@ static int _draw_panic_screen_qr_code(struct drm_scanout_buffer *sb,
 
 	max_qr_size = min(3 * sb->width / 4, 3 * sb->height / 4);
 
-	qr_width = drm_panic_get_qr_code(&qr_image);
+	qr_width = drm_panic_get_qr_code(&qr_image, qr_version);
 	if (qr_width < 0)
 		return qr_width;
 	else if (!qr_width)
@@ -812,9 +813,11 @@ static int _draw_panic_screen_qr_code(struct drm_scanout_buffer *sb,
 	return 0;
 }
 
-static int draw_panic_screen_qr_code(struct drm_scanout_buffer *sb, u32 fg_color, u32 bg_color)
+static int draw_panic_screen_qr_code(struct drm_scanout_buffer *sb,
+				     u32 fg_color, u32 bg_color,
+				     unsigned int qr_version)
 {
-	if (_draw_panic_screen_qr_code(sb, fg_color, bg_color))
+	if (_draw_panic_screen_qr_code(sb, fg_color, bg_color, qr_version))
 		return draw_panic_screen_user(sb, fg_color, bg_color);
 	return 0;
 }
@@ -887,17 +890,18 @@ static bool drm_panic_is_format_supported(const struct drm_format_info *format)
 	return drm_draw_can_convert_from_xrgb8888(format->format);
 }
 
-static int draw_panic_dispatch(struct drm_scanout_buffer *sb, u32 fg_color, u32 bg_color)
+static int draw_panic_dispatch(struct drm_scanout_buffer *sb, enum drm_panic_type panic_type,
+			       u32 fg_color, u32 bg_color, unsigned int qr_version)
 {
 	int ret;
 
-	switch (drm_panic_type) {
+	switch (panic_type) {
 	case DRM_PANIC_TYPE_KMSG:
 		ret = draw_panic_screen_kmsg(sb, fg_color, bg_color);
 		break;
 #if IS_ENABLED(CONFIG_DRM_PANIC_SCREEN_QR_CODE)
 	case DRM_PANIC_TYPE_QR:
-		ret = draw_panic_screen_qr_code(sb, fg_color, bg_color);
+		ret = draw_panic_screen_qr_code(sb, fg_color, bg_color, qr_version);
 		break;
 #endif
 	case DRM_PANIC_TYPE_USER:
@@ -947,6 +951,11 @@ static void draw_panic_plane(struct drm_plane *plane, const char *description)
 #else
 	u32 bg_color = 0x00000000;
 #endif
+#if IS_ENABLED(CONFIG_DRM_PANIC_SCREEN_QR_CODE)
+	unsigned int qr_version = panic_qr_version;
+#else
+	unsigned int qr_version = 0;
+#endif
 
 	if (!drm_panic_trylock(plane->dev, flags))
 		return;
@@ -962,7 +971,7 @@ static void draw_panic_plane(struct drm_plane *plane, const char *description)
 
 	drm_panic_set_description(description);
 
-	ret = draw_panic_dispatch(&sb, fg_color, bg_color);
+	ret = draw_panic_dispatch(&sb, drm_panic_type, fg_color, bg_color, qr_version);
 	if (!ret) {
 		/*
 		 * Only flush if we have a panic screen to display. Otherwise
drivers/gpu/drm/tests/drm_panic_test.c +1 -1
index 969a096ec840..196496238d23 100644
@@ -58,7 +58,7 @@ static int draw_panic_screen_kmsg_default(struct drm_scanout_buffer *sb)
 #if IS_ENABLED(CONFIG_DRM_PANIC_SCREEN_QR_CODE)
 static int draw_panic_screen_qr_code_default(struct drm_scanout_buffer *sb)
 {
-	return draw_panic_screen_qr_code(sb, 0x00ffffff, 0x00000000);
+	return draw_panic_screen_qr_code(sb, 0x00ffffff, 0x00000000, 40);
 }
 #endif
 
Signature · 1 line
2.55.0
author maintainer
#9 lore raw

PATCH v2 7/13 drm/panic: Retry in dispatch function if panic output fails

In reply to Thomas Zimmermann

On failed output of QR codes and kmsg buffers, fall back to a user message. QR-code output already did this internally, but now formally support it in drm_panic_dispatch(). Streamlines the existing logic.

Also pass module parameter drm_panic_type as a parameter to drm_panic_dispatch(). This simpifies the implementation. It will later allow for moving the function into a helper library.

With the implicit fallback removed from QR-code support, the tests cases fail for thr smallest output size (300x200). This is because the QR code requires a minimal output size. Increase the size to 494x494, which is the minimum supported size for QR output.

v2:
- fix minimum test size (Sashiko)

---
 drivers/gpu/drm/drm_panic.c            | 24 ++++++++++++------------
 drivers/gpu/drm/tests/drm_panic_test.c |  2 +-
 2 files changed, 13 insertions(+), 13 deletions(-)
drivers/gpu/drm/drm_panic.c +12 -12
index f597ef04c5bc..bc7c096c4236 100644
@@ -740,9 +740,9 @@ static int drm_panic_get_qr_code(u8 **qr_image, unsigned int qr_version)
 /*
  * Draw the panic message at the center of the screen, with a QR Code
  */
-static int _draw_panic_screen_qr_code(struct drm_scanout_buffer *sb,
-				      u32 fg_color, u32 bg_color,
-				      unsigned int qr_version)
+static int draw_panic_screen_qr_code(struct drm_scanout_buffer *sb,
+				     u32 fg_color, u32 bg_color,
+				     unsigned int qr_version)
 {
 	const struct font_desc *font = get_default_font(sb->width, sb->height, NULL, NULL);
 	struct drm_rect r_screen, r_logo, r_msg, r_qr, r_qr_canvas;
@@ -812,15 +812,6 @@ static int _draw_panic_screen_qr_code(struct drm_scanout_buffer *sb,
 	drm_panic_blit(sb, &r_qr, qr_image, qr_pitch, scale, fg_color);
 	return 0;
 }
-
-static int draw_panic_screen_qr_code(struct drm_scanout_buffer *sb,
-				     u32 fg_color, u32 bg_color,
-				     unsigned int qr_version)
-{
-	if (_draw_panic_screen_qr_code(sb, fg_color, bg_color, qr_version))
-		return draw_panic_screen_user(sb, fg_color, bg_color);
-	return 0;
-}
 #else
 static void drm_panic_qr_init(void) {};
 static void drm_panic_qr_exit(void) {};
@@ -895,13 +886,22 @@ static int draw_panic_dispatch(struct drm_scanout_buffer *sb, enum drm_panic_typ
 {
 	int ret;
 
+retry:
 	switch (panic_type) {
 	case DRM_PANIC_TYPE_KMSG:
 		ret = draw_panic_screen_kmsg(sb, fg_color, bg_color);
+		if (ret) {
+			panic_type = DRM_PANIC_TYPE_USER;
+			goto retry;
+		}
 		break;
 #if IS_ENABLED(CONFIG_DRM_PANIC_SCREEN_QR_CODE)
 	case DRM_PANIC_TYPE_QR:
 		ret = draw_panic_screen_qr_code(sb, fg_color, bg_color, qr_version);
+		if (ret) {
+			panic_type = DRM_PANIC_TYPE_USER;
+			goto retry;
+		}
 		break;
 #endif
 	case DRM_PANIC_TYPE_USER:
drivers/gpu/drm/tests/drm_panic_test.c +1 -1
index 196496238d23..55d77bc203df 100644
@@ -32,7 +32,7 @@ struct drm_test_mode {
  */
 #define DRM_TEST_MODE_LIST(func) \
 	DRM_PANIC_TEST_MODE(1024, 768, DRM_FORMAT_XRGB8888, func) \
-	DRM_PANIC_TEST_MODE(300, 200, DRM_FORMAT_XRGB8888, func) \
+	DRM_PANIC_TEST_MODE(494, 494, DRM_FORMAT_XRGB8888, func) \
 	DRM_PANIC_TEST_MODE(1920, 1080, DRM_FORMAT_XRGB8888, func) \
 	DRM_PANIC_TEST_MODE(1024, 768, DRM_FORMAT_RGB565, func) \
 	DRM_PANIC_TEST_MODE(1024, 768, DRM_FORMAT_RGB888, func) \
Signature · 1 line
2.55.0
#10 lore raw

Re: [PATCH v2 07/13] drm/panic: Retry in dispatch function if panic output fails

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Medium] The commit message claims a change that is not present in the code diff. - [Medium] Fallback to…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] The commit message claims a change that is not present in the code diff.
- [Medium] Fallback to user mode silently flushes a blank screen if the screen is narrower than a single character, defeating failure preservation. --

--- Patch [7]: [PATCH v2 07/13] drm/panic: Retry in dispatch function if panic output fails --- commit 43648cf544bdeed6f6c85be5e74f3e855c778814
Author: Thomas Zimmermann <[email protected]>

drm/panic: Retry in dispatch function if panic output fails

This patch introduces explicit fallback to DRM_PANIC_TYPE_USER in drm_panic_dispatch() when QR code or kmsg outputs fail. It also increases the minimum QR code test size to 494x494.

Quoted 3 lines

Also pass module parameter drm_panic_type as a parameter to drm_panic_dispatch(). This simpifies the implementation. It will later allow for moving the function into a helper library.

[Severity: Medium]
This isn't a bug, but the commit message mentions passing drm_panic_type as a parameter to drm_panic_dispatch(), which isn't present in this patch's code changes. Was this already implemented in a preceding commit?

Quoted 4 lines
drivers/gpu/drm/drm_panic.c +0 -0
index f597ef04c5bcb..bc7c096c42363 100644

[ ... ]

drivers/gpu/drm/drm_panic.c static int draw_panic_dispatch(struct drm_scanout_buffer *sb, enum drm_panic_typ · line 896

Quoted 12 lines
drivers/gpu/drm/drm_panic.c +5 -0
@@ -895,13 +886,22 @@ static int draw_panic_dispatch(struct drm_scanout_buffer *sb, enum drm_panic_typ
 {
 	int ret;
 
+retry:
 	switch (panic_type) {
 	case DRM_PANIC_TYPE_KMSG:
 		ret = draw_panic_screen_kmsg(sb, fg_color, bg_color);
+		if (ret) {
+			panic_type = DRM_PANIC_TYPE_USER;
+			goto retry;
+		}

[Severity: Medium]
Does this fallback logic inadvertently cause the screen to be overwritten with a blank screen if the screen is narrower than a single character?

If drm_panic_type is set to KMSG and sb->width < font->width, draw_panic_screen_kmsg() returns -EINVAL. The retry logic here catches this and falls back to DRM_PANIC_TYPE_USER.

However, looking at draw_panic_screen_user(), it lacks a similar width check. It clears the screen with the background color:

drivers/gpu/drm/drm_panic.c:draw_panic_screen_user() {
    ...
    /* Fill with the background color, and draw text on top */
    drm_panic_fill(sb, &r_screen, bg_color);
    ...
}

It then returns 0 (success) without drawing any text. This success code causes draw_panic_plane() to flush the blank screen, destroying the existing visual evidence instead of preserving it.

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

PATCH v2 8/13 drm/panic: Split draw_panic_plane()

In reply to Thomas Zimmermann

Move locking and parameters from draw_panic_plane() into the new helper drm_panic_display_panic_screen(). Call draw_panic_plane() from there.

The new helper drm_panic_display_panic_screen() is now the DRM core's interface for displaying a panic screen. The code remaining in draw_panic_plane() still does all the heavy lifting. It will become a plane helper for DRM drivers.

v2:
- use panic_type argument in draw_panic_plane() (Sashiko)

---
 drivers/gpu/drm/drm_panic.c | 62 ++++++++++++++++++++-----------------
 1 file changed, 34 insertions(+), 28 deletions(-)
drivers/gpu/drm/drm_panic.c +34 -28
index bc7c096c4236..2048a3bf736f 100644
@@ -936,42 +936,25 @@ static void drm_panic_clear_description(void)
 	desc_line->txt = NULL;
 }
 
-static void draw_panic_plane(struct drm_plane *plane, const char *description)
+static void draw_panic_plane(struct drm_plane *plane, const char *description,
+			     enum drm_panic_type panic_type, u32 fg_color, u32 bg_color,
+			     unsigned int qr_version)
 {
 	struct drm_scanout_buffer sb = { };
 	int ret;
-	unsigned long flags;
-#if defined(CONFIG_DRM_PANIC_FOREGROUND_COLOR)
-	u32 fg_color = CONFIG_DRM_PANIC_FOREGROUND_COLOR;
-#else
-	u32 fg_color = 0x00ffffff;
-#endif
-#if defined(CONFIG_DRM_PANIC_BACKGROUND_COLOR)
-	u32 bg_color = CONFIG_DRM_PANIC_BACKGROUND_COLOR;
-#else
-	u32 bg_color = 0x00000000;
-#endif
-#if IS_ENABLED(CONFIG_DRM_PANIC_SCREEN_QR_CODE)
-	unsigned int qr_version = panic_qr_version;
-#else
-	unsigned int qr_version = 0;
-#endif
-
-	if (!drm_panic_trylock(plane->dev, flags))
-		return;
 
 	ret = plane->helper_private->get_scanout_buffer(plane, &sb);
 
 	if (ret || !drm_panic_is_format_supported(sb.format))
-		goto unlock;
+		return;
 
 	/* One of these should be set, or it can't draw pixels */
 	if (!sb.set_pixel && !sb.pages && iosys_map_is_null(&sb.map[0]))
-		goto unlock;
+		return;
 
 	drm_panic_set_description(description);
 
-	ret = draw_panic_dispatch(&sb, drm_panic_type, fg_color, bg_color, qr_version);
+	ret = draw_panic_dispatch(&sb, panic_type, fg_color, bg_color, qr_version);
 	if (!ret) {
 		/*
 		 * Only flush if we have a panic screen to display. Otherwise
@@ -982,9 +965,33 @@ static void draw_panic_plane(struct drm_plane *plane, const char *description)
 	}
 
 	drm_panic_clear_description();
+}
 
-unlock:
-	drm_panic_unlock(plane->dev, flags);
+static void drm_panic_display_panic_screen(struct drm_plane *plane, const char *description)
+{
+#if defined(CONFIG_DRM_PANIC_FOREGROUND_COLOR)
+	u32 fg_color = CONFIG_DRM_PANIC_FOREGROUND_COLOR;
+#else
+	u32 fg_color = 0x00ffffff;
+#endif
+#if defined(CONFIG_DRM_PANIC_BACKGROUND_COLOR)
+	u32 bg_color = CONFIG_DRM_PANIC_BACKGROUND_COLOR;
+#else
+	u32 bg_color = 0x00000000;
+#endif
+#if IS_ENABLED(CONFIG_DRM_PANIC_SCREEN_QR_CODE)
+	unsigned int qr_version = panic_qr_version;
+#else
+	unsigned int qr_version = 0;
+#endif
+	struct drm_device *dev = plane->dev;
+	unsigned long flags;
+
+	if (drm_panic_trylock(dev, flags)) {
+		draw_panic_plane(plane, description, drm_panic_type,
+				 fg_color, bg_color, qr_version);
+		drm_panic_unlock(dev, flags);
+	}
 }
 
 static struct drm_plane *to_drm_plane(struct kmsg_dumper *kd)
@@ -997,10 +1004,9 @@ static void drm_panic(struct kmsg_dumper *dumper, struct kmsg_dump_detail *detai
 	struct drm_plane *plane = to_drm_plane(dumper);
 
 	if (detail->reason == KMSG_DUMP_PANIC)
-		draw_panic_plane(plane, detail->description);
+		drm_panic_display_panic_screen(plane, detail->description);
 }
 
-
 /*
  * DEBUG FS, This is currently unsafe.
  * Create one file per plane, so it's possible to debug one plane at a time.
@@ -1017,7 +1023,7 @@ static ssize_t debugfs_trigger_write(struct file *file, const char __user *user_
 	if (kstrtobool_from_user(user_buf, count, &run) == 0 && run) {
 		struct drm_plane *plane = file->private_data;
 
-		draw_panic_plane(plane, "Test from debugfs");
+		drm_panic_display_panic_screen(plane, "Test from debugfs");
 	}
 	return count;
 }
Signature · 1 line
2.55.0
author maintainer
#12 lore raw

PATCH v2 9/13 drm/panic: Restrict to primary planes; unconditionally unregister

In reply to Thomas Zimmermann

Restrict panic handling to primary displays. Overlays and cursors should not display panic output. If there are exceptions, they can be registered by the driver itself.

For cleanup, unregister each plane unconditionally. The kmsg dumper already keeps track of the registration status so DRM doesn't have to.

---
 drivers/gpu/drm/drm_panic.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
drivers/gpu/drm/drm_panic.c +3 -4
index 2048a3bf736f..7dbbcc1c219f 100644
@@ -1079,6 +1079,8 @@ void drm_panic_register(struct drm_device *dev)
 		return;
 
 	drm_for_each_plane(plane, dev) {
+		if (plane->type != DRM_PLANE_TYPE_PRIMARY)
+			continue;
 		if (!plane->helper_private || !plane->helper_private->get_scanout_buffer)
 			continue;
 		plane->kmsg_panic.dump = drm_panic;
@@ -1105,11 +1107,8 @@ void drm_panic_unregister(struct drm_device *dev)
 	if (!dev->mode_config.num_total_plane)
 		return;
 
-	drm_for_each_plane(plane, dev) {
-		if (!plane->helper_private || !plane->helper_private->get_scanout_buffer)
-			continue;
+	drm_for_each_plane(plane, dev)
 		kmsg_dump_unregister(&plane->kmsg_panic);
-	}
 }
 
 /**
Signature · 1 line
2.55.0
#13 lore raw

Re: [PATCH v2 09/13] drm/panic: Restrict to primary planes; unconditionally unregister

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Medium] Unconditional calls to kmsg_dump_unregister() for all planes cause severe delays during device…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] Unconditional calls to kmsg_dump_unregister() for all planes cause severe delays during device teardown due to sequential synchronize_rcu() calls.
- [Medium] drm_panic_is_enabled() was not updated to enforce the primary plane restriction, leading to inconsistent panic handler state. --

--- Patch [9]: [PATCH v2 09/13] drm/panic: Restrict to primary planes; unconditionally unregister --- commit c3117dcbe59bcb2d0d31d68229c5ec24738e412e
Author: Thomas Zimmermann <[email protected]>

drm/panic: Restrict to primary planes; unconditionally unregister

Restrict panic handling to primary displays. Overlays and cursors should not display panic output. If there are exceptions, they can be registered by the driver itself.

For cleanup, unregister each plane unconditionally. The kmsg dumper already keeps track of the registration status so DRM doesn't have to.

drivers/gpu/drm/drm_panic.c void drm_panic_register(struct drm_device *dev) · line 1083

Quoted 10 lines
drivers/gpu/drm/drm_panic.c +2 -0
index 2048a3bf736f0..7dbbcc1c219ff 100644
@@ -1079,6 +1079,8 @@ void drm_panic_register(struct drm_device *dev)
 		return;
 
 	drm_for_each_plane(plane, dev) {
+		if (plane->type != DRM_PLANE_TYPE_PRIMARY)
+			continue;

[Severity: Medium]
Was drm_panic_is_enabled() updated to match this new primary plane restriction?

If a driver implements the panic callback on a non-primary plane but not on a primary plane, drm_panic_is_enabled() will return true since it lacks this type check:

drivers/gpu/drm/drm_panic.c:drm_panic_is_enabled() {
...

	drm_for_each_plane(plane, dev)
		if (plane->helper_private && plane->helper_private->get_scanout_buffer)
			return true;

...
}

Because drm_panic_register() skips the non-primary plane, it won't actually be registered. Does this false positive in drm_panic_is_enabled() introduce a regression where drm_fb_helper disables its fallback panic handling, leaving the system with no panic screen handler at all?

Quoted 3 lines
drivers/gpu/drm/drm_panic.c +0 -0
 		if (!plane->helper_private || !plane->helper_private->get_scanout_buffer)
 			continue;
 		plane->kmsg_panic.dump = drm_panic;

[ ... ]

drivers/gpu/drm/drm_panic.c void drm_panic_unregister(struct drm_device *dev) · line 1112

Quoted 11 lines
drivers/gpu/drm/drm_panic.c +1 -4
@@ -1105,11 +1107,8 @@ void drm_panic_unregister(struct drm_device *dev)
 	if (!dev->mode_config.num_total_plane)
 		return;
 
-	drm_for_each_plane(plane, dev) {
-		if (!plane->helper_private || !plane->helper_private->get_scanout_buffer)
-			continue;
+	drm_for_each_plane(plane, dev)
 		kmsg_dump_unregister(&plane->kmsg_panic);
-	}
 }

[Severity: Medium]
Does this unconditional loop cause severe delays during device teardown?

The function kmsg_dump_unregister() executes synchronize_rcu() at the end regardless of the dumper's registration status:

kernel/printk/printk.c:kmsg_dump_unregister() {
...

	spin_unlock_irqrestore(&dump_list_lock, flags);
	synchronize_rcu();

...
}

Looping sequentially over all planes without checking if they were actually registered forces the teardown path to block for multiple consecutive RCU grace periods. Can this regression potentially add hundreds of milliseconds to several seconds of delay during driver unbind or hot-unplug?

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

PATCH v2 10/13 drm/panic: Display panic screen via per-plane callback

In reply to Thomas Zimmermann

Add display_panic_screen to struct drm_plane_funcs. If set on a plane, register a panic handler for the plane from the DRM core's panic handling. Invoke this helper from the core to display a panic screen.

The new callback replaces get_scanout_buffer as signal for panic support in a plane. Using get_scanout_buffer is now merely a detail of the implementation. This will also free struct drm_scanout_buffer for use cases besides panic handling.

With display_panic_screen in place, rename draw_panic_plane() to drm_plane_helper_display_panic_screen() and make it available to DRM drivers via drm_panic_helper.h. Do not change the implementation or functionality of this code. drm_plane_helper_display_panic_screen() still supports kernel messages, user messages and QR codes on various color formats.

Then update all drivers that implement panic support. Provide the initializer macro DRM_PANIC_PLANE_FUNCS that sets display_panic_screen to drm_plane_helper_display_panic_screen(). If panic handling has been disabled, it instead clears the callback. There's again no change in functionality.

This logically splits panic handling into a set of entry points in the DRM code and a set of helper functions provided by drivers. The core invokes the driver's helper via callback, while the driver can tailor the implementation to its needs. Panic handling now works like most other interfaces in the DRM framework.

Locking and state-handling happens in the DRM core or helpers. Drivers that receive a call to display_panic_screen can assume that no concurrent modeset takes place on the plane or its CRTC.

Also update the documentation. Move information about locking and state guarantees from drm_panic_trylock() to display_panic_screen, where drivers authors can find it easily. Move information about the features of the current implementation to drm_panic_helper_display_panic_screen().

v2:
- only consider planes with get_scanout_buffer (Sashiko)
- set callback for planes create with drm_crtc_init() (Sashiko)
- return errno code from display_panic_screen

---
 MAINTAINERS                                   |  3 +-
 .../amd/display/amdgpu_dm/amdgpu_dm_plane.c   |  2 +
 drivers/gpu/drm/ast/ast_mode.c                |  2 +
 drivers/gpu/drm/drm_modeset_helper.c          |  2 +
 drivers/gpu/drm/drm_panic.c                   | 89 ++++++++++---------
 drivers/gpu/drm/hyperv/hyperv_drm_modeset.c   |  2 +
 drivers/gpu/drm/i915/display/i9xx_plane.c     |  3 +
 .../drm/i915/display/skl_universal_plane.c    |  4 +
 drivers/gpu/drm/imx/ipuv3/ipuv3-plane.c       |  2 +
 drivers/gpu/drm/mgag200/mgag200_drv.h         |  4 +-
 drivers/gpu/drm/nouveau/dispnv50/wndw.c       |  2 +
 .../gpu/drm/renesas/rcar-du/rcar_du_plane.c   |  2 +
 .../drm/renesas/shmobile/shmob_drm_plane.c    |  2 +
 drivers/gpu/drm/sysfb/drm_sysfb_helper.h      |  4 +-
 drivers/gpu/drm/sysfb/vesadrm.c               |  1 +
 drivers/gpu/drm/tidss/tidss_plane.c           |  2 +
 drivers/gpu/drm/tiny/bochs.c                  |  4 +-
 drivers/gpu/drm/virtio/virtgpu_plane.c        |  2 +
 include/drm/drm_mode_config.h                 |  4 +-
 include/drm/drm_modeset_helper_vtables.h      | 11 ++-
 include/drm/drm_panic.h                       | 49 +++-------
 include/drm/drm_panic_helper.h                | 27 ++++++
 include/drm/drm_plane.h                       | 57 ++++++++++++
 23 files changed, 191 insertions(+), 89 deletions(-)
 create mode 100644 include/drm/drm_panic_helper.h
MAINTAINERS +2 -1
index 928b3ba23a76..472c5c48729d 100644
@@ -9020,7 +9020,8 @@ F:	drivers/gpu/drm/drm_draw.c
 F:	drivers/gpu/drm/drm_draw_internal.h
 F:	drivers/gpu/drm/drm_panic*.c
 F:	drivers/gpu/drm/tests/drm_panic_test.c
-F:	include/drm/drm_panic*
+F:	include/drm/drm_panic.h
+F:	include/drm/drm_panic_helper.h
 
 DRM PANIC QR CODE
 M:	Jocelyn Falempe <[email protected]>
drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_plane.c +2 -0
index 824ef3ce5de0..eadc22a255bb 100644
@@ -28,6 +28,7 @@
 #include <drm/drm_blend.h>
 #include "drm/drm_framebuffer.h"
 #include <drm/drm_gem_atomic_helper.h>
+#include <drm/drm_panic_helper.h>
 #include <drm/drm_plane_helper.h>
 #include <drm/drm_gem_framebuffer_helper.h>
 #include <drm/drm_fourcc.h>
@@ -2177,6 +2178,7 @@ static const struct drm_plane_funcs dm_plane_funcs = {
 	.atomic_set_property = dm_atomic_plane_set_property,
 	.atomic_get_property = dm_atomic_plane_get_property,
 #endif
+	DRM_PANIC_PLANE_FUNCS,
 };
 
 int amdgpu_dm_plane_init(struct amdgpu_display_manager *dm,
drivers/gpu/drm/ast/ast_mode.c +2 -0
index d5ed8c5c7925..ceebf9421100 100644
@@ -43,6 +43,7 @@
 #include <drm/drm_gem_shmem_helper.h>
 #include <drm/drm_managed.h>
 #include <drm/drm_panic.h>
+#include <drm/drm_panic_helper.h>
 #include <drm/drm_print.h>
 #include <drm/drm_probe_helper.h>
 
@@ -652,6 +653,7 @@ static const struct drm_plane_funcs ast_primary_plane_funcs = {
 	.disable_plane = drm_atomic_helper_disable_plane,
 	.destroy = drm_plane_cleanup,
 	DRM_GEM_SHADOW_PLANE_FUNCS,
+	DRM_PANIC_PLANE_FUNCS,
 };
 
 static int ast_primary_plane_init(struct ast_device *ast)
drivers/gpu/drm/drm_modeset_helper.c +2 -0
index e54584da4c3d..cca623eb23e5 100644
@@ -27,6 +27,7 @@
 #include <drm/drm_fourcc.h>
 #include <drm/drm_framebuffer.h>
 #include <drm/drm_modeset_helper.h>
+#include <drm/drm_panic_helper.h>
 #include <drm/drm_plane_helper.h>
 #include <drm/drm_print.h>
 #include <drm/drm_probe_helper.h>
@@ -113,6 +114,7 @@ static const uint32_t safe_modeset_formats[] = {
 
 static const struct drm_plane_funcs primary_plane_funcs = {
 	DRM_PLANE_NON_ATOMIC_FUNCS,
+	DRM_PANIC_PLANE_FUNCS,
 };
 
 /**
drivers/gpu/drm/drm_panic.c +49 -40

149 lines not on this page · raw message

drivers/gpu/drm/hyperv/hyperv_drm_modeset.c +2 -0
index 1855749c1e41..3bf228ef97ff 100644
@@ -18,6 +18,7 @@
 #include <drm/drm_gem_shmem_helper.h>
 #include <drm/drm_probe_helper.h>
 #include <drm/drm_panic.h>
+#include <drm/drm_panic_helper.h>
 #include <drm/drm_plane.h>
 #include <drm/drm_print.h>
 #include <drm/drm_vblank.h>
@@ -237,6 +238,7 @@ static const struct drm_plane_funcs hv_drm_plane_funcs = {
 	.disable_plane		= drm_atomic_helper_disable_plane,
 	.destroy		= drm_plane_cleanup,
 	DRM_GEM_SHADOW_PLANE_FUNCS,
+	DRM_PANIC_PLANE_FUNCS,
 };
 
 static const struct drm_encoder_funcs hv_drm_simple_encoder_funcs_cleanup = {
drivers/gpu/drm/i915/display/i9xx_plane.c +3 -0
index df6da1c6a359..0902b17d0269 100644
@@ -8,6 +8,7 @@
 #include <drm/drm_atomic_helper.h>
 #include <drm/drm_blend.h>
 #include <drm/drm_fourcc.h>
+#include <drm/drm_panic_helper.h>
 #include <drm/drm_print.h>
 
 #include "i9xx_plane.h"
@@ -887,6 +888,7 @@ static const struct drm_plane_funcs i965_plane_funcs = {
 	.atomic_destroy_state = intel_plane_destroy_state,
 	.format_mod_supported = i965_plane_format_mod_supported,
 	.format_mod_supported_async = intel_plane_format_mod_supported_async,
+	DRM_PANIC_PLANE_FUNCS,
 };
 
 static const struct drm_plane_funcs i8xx_plane_funcs = {
@@ -897,6 +899,7 @@ static const struct drm_plane_funcs i8xx_plane_funcs = {
 	.atomic_destroy_state = intel_plane_destroy_state,
 	.format_mod_supported = i8xx_plane_format_mod_supported,
 	.format_mod_supported_async = intel_plane_format_mod_supported_async,
+	DRM_PANIC_PLANE_FUNCS,
 };
 
 static void i9xx_disable_tiling(struct intel_plane *plane)
drivers/gpu/drm/i915/display/skl_universal_plane.c +4 -0
index 07a683293352..45e8379d93d4 100644
@@ -7,6 +7,7 @@
 #include <drm/drm_blend.h>
 #include <drm/drm_damage_helper.h>
 #include <drm/drm_fourcc.h>
+#include <drm/drm_panic_helper.h>
 #include <drm/drm_print.h>
 #include <drm/intel/step.h>
 
@@ -2707,6 +2708,7 @@ static const struct drm_plane_funcs skl_plane_funcs = {
 	.atomic_destroy_state = intel_plane_destroy_state,
 	.format_mod_supported = skl_plane_format_mod_supported,
 	.format_mod_supported_async = intel_plane_format_mod_supported_async,
+	DRM_PANIC_PLANE_FUNCS,
 };
 
 static const struct drm_plane_funcs icl_plane_funcs = {
@@ -2717,6 +2719,7 @@ static const struct drm_plane_funcs icl_plane_funcs = {
 	.atomic_destroy_state = intel_plane_destroy_state,
 	.format_mod_supported = icl_plane_format_mod_supported,
 	.format_mod_supported_async = intel_plane_format_mod_supported_async,
+	DRM_PANIC_PLANE_FUNCS,
 };
 
 static const struct drm_plane_funcs tgl_plane_funcs = {
@@ -2727,6 +2730,7 @@ static const struct drm_plane_funcs tgl_plane_funcs = {
 	.atomic_destroy_state = intel_plane_destroy_state,
 	.format_mod_supported = tgl_plane_format_mod_supported,
 	.format_mod_supported_async = intel_plane_format_mod_supported_async,
+	DRM_PANIC_PLANE_FUNCS,
 };
 
 static void
drivers/gpu/drm/imx/ipuv3/ipuv3-plane.c +2 -0
index c7ec09e557c1..377db7cea726 100644
@@ -14,6 +14,7 @@
 #include <drm/drm_gem_atomic_helper.h>
 #include <drm/drm_gem_dma_helper.h>
 #include <drm/drm_managed.h>
+#include <drm/drm_panic_helper.h>
 #include <drm/drm_print.h>
 
 #include <video/imx-ipu-v3.h>
@@ -362,6 +363,7 @@ static const struct drm_plane_funcs ipu_plane_funcs = {
 	.atomic_duplicate_state	= ipu_plane_duplicate_state,
 	.atomic_destroy_state	= ipu_plane_destroy_state,
 	.format_mod_supported = ipu_plane_format_mod_supported,
+	DRM_PANIC_PLANE_FUNCS,
 };
 
 static int ipu_plane_atomic_check(struct drm_plane *plane,
drivers/gpu/drm/mgag200/mgag200_drv.h +3 -1
index 3732c62351bb..d3995ce876f4 100644
@@ -17,6 +17,7 @@
 #include <drm/drm_encoder.h>
 #include <drm/drm_gem.h>
 #include <drm/drm_gem_shmem_helper.h>
+#include <drm/drm_panic_helper.h>
 #include <drm/drm_plane.h>
 
 #include "mgag200_reg.h"
@@ -386,7 +387,8 @@ int mgag200_primary_plane_helper_get_scanout_buffer(struct drm_plane *plane,
 	.update_plane = drm_atomic_helper_update_plane, \
 	.disable_plane = drm_atomic_helper_disable_plane, \
 	.destroy = drm_plane_cleanup, \
-	DRM_GEM_SHADOW_PLANE_FUNCS
+	DRM_GEM_SHADOW_PLANE_FUNCS, \
+	DRM_PANIC_PLANE_FUNCS
 
 void mgag200_crtc_fill_gamma(struct mga_device *mdev, const struct drm_format_info *format);
 void mgag200_crtc_load_gamma(struct mga_device *mdev,
drivers/gpu/drm/nouveau/dispnv50/wndw.c +2 -0
index 15a322422f4e..fc29a291e96f 100644
@@ -39,6 +39,7 @@
 #include <drm/drm_framebuffer.h>
 #include <drm/drm_gem_atomic_helper.h>
 #include <drm/drm_panic.h>
+#include <drm/drm_panic_helper.h>
 #include <drm/ttm/ttm_bo.h>
 
 #include "nouveau_bo.h"
@@ -841,6 +842,7 @@ nv50_wndw = {
 	.atomic_duplicate_state = nv50_wndw_atomic_duplicate_state,
 	.atomic_destroy_state = nv50_wndw_atomic_destroy_state,
 	.format_mod_supported = nv50_plane_format_mod_supported,
+	DRM_PANIC_PLANE_FUNCS,
 };
 
 static const u64 nv50_cursor_format_modifiers[] = {
drivers/gpu/drm/renesas/rcar-du/rcar_du_plane.c +2 -0
index 8870766b9e54..f4b0e5b70c7d 100644
@@ -16,6 +16,7 @@
 #include <drm/drm_fourcc.h>
 #include <drm/drm_framebuffer.h>
 #include <drm/drm_gem_dma_helper.h>
+#include <drm/drm_panic_helper.h>
 
 #include "rcar_du_drv.h"
 #include "rcar_du_group.h"
@@ -773,6 +774,7 @@ static const struct drm_plane_funcs rcar_du_plane_funcs = {
 	.atomic_destroy_state = rcar_du_plane_atomic_destroy_state,
 	.atomic_set_property = rcar_du_plane_atomic_set_property,
 	.atomic_get_property = rcar_du_plane_atomic_get_property,
+	DRM_PANIC_PLANE_FUNCS,
 };
 
 static const uint32_t formats[] = {
drivers/gpu/drm/renesas/shmobile/shmob_drm_plane.c +2 -0
index 148de34af785..907f85462fa0 100644
@@ -14,6 +14,7 @@
 #include <drm/drm_fourcc.h>
 #include <drm/drm_framebuffer.h>
 #include <drm/drm_gem_dma_helper.h>
+#include <drm/drm_panic_helper.h>
 
 #include "shmob_drm_drv.h"
 #include "shmob_drm_kms.h"
@@ -286,6 +287,7 @@ static const struct drm_plane_funcs shmob_drm_plane_funcs = {
 	.reset = shmob_drm_plane_reset,
 	.atomic_duplicate_state = shmob_drm_plane_atomic_duplicate_state,
 	.atomic_destroy_state = shmob_drm_plane_atomic_destroy_state,
+	DRM_PANIC_PLANE_FUNCS,
 };
 
 static const uint32_t formats[] = {
drivers/gpu/drm/sysfb/drm_sysfb_helper.h +3 -1
index 547f2327af5e..232be339659e 100644
@@ -12,6 +12,7 @@
 #include <drm/drm_device.h>
 #include <drm/drm_gem_atomic_helper.h>
 #include <drm/drm_modes.h>
+#include <drm/drm_panic_helper.h>
 
 struct drm_format_info;
 struct drm_scanout_buffer;
@@ -144,7 +145,8 @@ void drm_sysfb_plane_atomic_destroy_state(struct drm_plane *plane,
 	.update_plane = drm_atomic_helper_update_plane, \
 	.disable_plane = drm_atomic_helper_disable_plane, \
 	.atomic_duplicate_state = drm_sysfb_plane_atomic_duplicate_state, \
-	.atomic_destroy_state = drm_sysfb_plane_atomic_destroy_state
+	.atomic_destroy_state = drm_sysfb_plane_atomic_destroy_state, \
+	DRM_PANIC_PLANE_FUNCS
 
 /*
  * CRTC
drivers/gpu/drm/sysfb/vesadrm.c +1 -0
index 6a67b2d2e451..14d2e6ab88aa 100644
@@ -24,6 +24,7 @@
 #include <drm/drm_managed.h>
 #include <drm/drm_modeset_helper.h>
 #include <drm/drm_modeset_helper_vtables.h>
+#include <drm/drm_panic_helper.h>
 #include <drm/drm_print.h>
 #include <drm/drm_probe_helper.h>
 
drivers/gpu/drm/tidss/tidss_plane.c +2 -0
index 6d82976c2db1..b0d5d6959b88 100644
@@ -12,6 +12,7 @@
 #include <drm/drm_fourcc.h>
 #include <drm/drm_framebuffer.h>
 #include <drm/drm_gem_atomic_helper.h>
+#include <drm/drm_panic_helper.h>
 
 #include "tidss_crtc.h"
 #include "tidss_dispc.h"
@@ -182,6 +183,7 @@ static const struct drm_plane_funcs tidss_plane_funcs = {
 	.atomic_create_state = drm_atomic_helper_plane_create_state,
 	.atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
 	.atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
+	DRM_PANIC_PLANE_FUNCS,
 };
 
 struct tidss_plane *tidss_plane_create(struct tidss_device *tidss,
drivers/gpu/drm/tiny/bochs.c +3 -1
index e2d957e51505..076d19b26511 100644
@@ -20,6 +20,7 @@
 #include <drm/drm_managed.h>
 #include <drm/drm_module.h>
 #include <drm/drm_panic.h>
+#include <drm/drm_panic_helper.h>
 #include <drm/drm_plane_helper.h>
 #include <drm/drm_print.h>
 #include <drm/drm_probe_helper.h>
@@ -501,7 +502,8 @@ static const struct drm_plane_funcs bochs_primary_plane_funcs = {
 	.update_plane = drm_atomic_helper_update_plane,
 	.disable_plane = drm_atomic_helper_disable_plane,
 	.destroy = drm_plane_cleanup,
-	DRM_GEM_SHADOW_PLANE_FUNCS
+	DRM_GEM_SHADOW_PLANE_FUNCS,
+	DRM_PANIC_PLANE_FUNCS,
 };
 
 static void bochs_crtc_helper_mode_set_nofb(struct drm_crtc *crtc)
drivers/gpu/drm/virtio/virtgpu_plane.c +2 -0
index 1d1b27ece62a..108b5b93f130 100644
@@ -30,6 +30,7 @@
 #include <linux/virtio_dma_buf.h>
 #include <drm/drm_managed.h>
 #include <drm/drm_panic.h>
+#include <drm/drm_panic_helper.h>
 #include <drm/drm_print.h>
 
 #include "virtgpu_drv.h"
@@ -94,6 +95,7 @@ static const struct drm_plane_funcs virtio_gpu_plane_funcs = {
 	.reset			= drm_atomic_helper_plane_reset,
 	.atomic_duplicate_state = virtio_gpu_plane_duplicate_state,
 	.atomic_destroy_state	= drm_atomic_helper_plane_destroy_state,
+	DRM_PANIC_PLANE_FUNCS,
 };
 
 static int virtio_gpu_plane_atomic_check(struct drm_plane *plane,
include/drm/drm_mode_config.h +2 -2
index d8f5b7e9673e..084517beb924 100644
@@ -495,8 +495,8 @@ struct drm_mode_config {
 	 *
 	 * Raw spinlock used to protect critical sections of code that access
 	 * the display hardware or modeset software state, which the panic
-	 * printing code must be protected against. See drm_panic_trylock(),
-	 * drm_panic_lock() and drm_panic_unlock().
+	 * printing code must be protected against. See the documentation on
+	 * DRM panic handling for more information.
 	 */
 	struct raw_spinlock panic_lock;
 
include/drm/drm_modeset_helper_vtables.h +7 -4
index ca6268945c28..614bdee0c1ed 100644
@@ -1442,17 +1442,20 @@ struct drm_plane_helper_funcs {
 	/**
 	 * @get_scanout_buffer:
 	 *
-	 * Get the current scanout buffer, to display a message with drm_panic.
+	 * Get the current scanout buffer to display a message with drm_panic.
 	 * The driver should do the minimum changes to provide a buffer,
 	 * that can be used to display the panic screen. Currently only linear
 	 * buffers are supported. Non-linear buffer support is on the TODO list.
 	 * The device &dev.mode_config.panic_lock is taken before calling this
 	 * function, so you can safely access the &plane.state
 	 * It is called from a panic callback, and must follow its restrictions.
-	 * Please look the documentation at drm_panic_trylock() for an in-depth
+	 * Please look the documentation on DRM panic handling for an in-depth
 	 * discussions of what's safe and what is not allowed.
+	 *
 	 * It's a best effort mode, so it's expected that in some complex cases
-	 * the panic screen won't be displayed.
+	 * the panic screen won't be displayed. Drivers must not make any
+	 * assumptions about the actual state of the hardware.
+	 *
 	 * The returned &drm_scanout_buffer.map must be valid if no error code is
 	 * returned.
 	 *
@@ -1472,7 +1475,7 @@ struct drm_plane_helper_funcs {
 	 * It is only called if get_scanout_buffer() returned successfully, and
 	 * the &dev.mode_config.panic_lock is held during the entire sequence.
 	 * It is called from a panic callback, and must follow its restrictions.
-	 * Please look the documentation at drm_panic_trylock() for an in-depth
+	 * Please look the documentation on DRM panic handling for an in-depth
 	 * discussions of what's safe and what is not allowed.
 	 */
 	void (*panic_flush)(struct drm_plane *plane);
include/drm/drm_panic.h +10 -39

61 lines not on this page · raw message

include/drm/drm_panic_helper.h new file +27 -0
new file mode 100644
index 000000000000..0b6ad8f61821
@@ -0,0 +1,27 @@
+/* SPDX-License-Identifier: GPL-2.0 or MIT */
+
+#ifndef __DRM_DRM_PANIC_HELPER_H_
+#define __DRM_DRM_PANIC_HELPER_H_
+
+#include <linux/types.h>
+
+enum drm_panic_type;
+
+struct drm_plane;
+
+/* drm_panic_helper.c */
+int drm_plane_helper_display_panic_screen(struct drm_plane *plane,
+					  const char *description,
+					  enum drm_panic_type panic_type,
+					  u32 fg_color, u32 bg_color,
+					  unsigned int qr_version);
+
+#if IS_ENABLED(CONFIG_DRM_PANIC)
+#define DRM_PANIC_PLANE_FUNCS \
+	.display_panic_screen = drm_plane_helper_display_panic_screen
+#else
+#define DRM_PANIC_PLANE_FUNCS \
+	.display_panic_screen = NULL
+#endif
+
+#endif
include/drm/drm_plane.h +57 -0

69 lines not on this page · raw message

Signature · 1 line
2.55.0
author maintainer
#15 lore raw

PATCH v2 11/13 drm/panic: Internalize panic locking in DRM core and helpers

In reply to Thomas Zimmermann

None of the DRM drivers handles panic locking. Declare the interfaces in an internal header file. Move the trylock required during the panic into the DRM core's sources.

---
 Documentation/gpu/drm-kms.rst        |  3 ++
 MAINTAINERS                          |  3 +-
 drivers/gpu/drm/drm_atomic_helper.c  |  2 +-
 drivers/gpu/drm/drm_drv.c            |  2 +-
 drivers/gpu/drm/drm_panic.c          |  4 ++
 drivers/gpu/drm/drm_panic_internal.h | 55 +++++++++++++++++++++++
 include/drm/drm_panic.h              | 65 ----------------------------
 7 files changed, 66 insertions(+), 68 deletions(-)
 create mode 100644 drivers/gpu/drm/drm_panic_internal.h
Documentation/gpu/drm-kms.rst +3 -0
index 0dd440a14946..8988fd64369b 100644
@@ -421,6 +421,9 @@ Plane Panic Functions Reference
 .. kernel-doc:: include/drm/drm_panic.h
    :internal:
 
+.. kernel-doc:: drivers/gpu/drm/drm_panic_internal.h
+   :internal:
+
 .. kernel-doc:: drivers/gpu/drm/drm_panic.c
    :export:
 
MAINTAINERS +2 -1
index 472c5c48729d..f3dfdd9314bd 100644
@@ -9018,7 +9018,8 @@ S:	Supported
 T:	git https://gitlab.freedesktop.org/drm/misc/kernel.git
 F:	drivers/gpu/drm/drm_draw.c
 F:	drivers/gpu/drm/drm_draw_internal.h
-F:	drivers/gpu/drm/drm_panic*.c
+F:	drivers/gpu/drm/drm_panic.c
+F:	drivers/gpu/drm/drm_panic_internal.h
 F:	drivers/gpu/drm/tests/drm_panic_test.c
 F:	include/drm/drm_panic.h
 F:	include/drm/drm_panic_helper.h
drivers/gpu/drm/drm_atomic_helper.c +1 -1
index 285aac3554df..af3934df3313 100644
@@ -41,7 +41,6 @@
 #include <drm/drm_drv.h>
 #include <drm/drm_framebuffer.h>
 #include <drm/drm_gem_atomic_helper.h>
-#include <drm/drm_panic.h>
 #include <drm/drm_print.h>
 #include <drm/drm_self_refresh_helper.h>
 #include <drm/drm_vblank.h>
@@ -49,6 +48,7 @@
 
 #include "drm_crtc_helper_internal.h"
 #include "drm_crtc_internal.h"
+#include "drm_panic_internal.h"
 
 /**
  * DOC: overview
drivers/gpu/drm/drm_drv.c +1 -1
index c808958a2188..02ff02931515 100644
@@ -50,13 +50,13 @@
 #include <drm/drm_file.h>
 #include <drm/drm_managed.h>
 #include <drm/drm_mode_object.h>
-#include <drm/drm_panic.h>
 #include <drm/drm_print.h>
 #include <drm/drm_privacy_screen_machine.h>
 #include <drm/drm_ras_genl_family.h>
 
 #include "drm_crtc_internal.h"
 #include "drm_internal.h"
+#include "drm_panic_internal.h"
 
 MODULE_AUTHOR("Gareth Hughes, Leif Delgass, José Fonseca, Jon Smirl");
 MODULE_DESCRIPTION("DRM shared core routines");
drivers/gpu/drm/drm_panic.c +4 -0
index 0fe9a0d67db3..9a6e3704bf09 100644
@@ -35,6 +35,7 @@
 
 #include "drm_crtc_internal.h"
 #include "drm_draw_internal.h"
+#include "drm_panic_internal.h"
 
 MODULE_AUTHOR("Jocelyn Falempe");
 MODULE_DESCRIPTION("DRM panic handler");
@@ -976,6 +977,9 @@ int drm_plane_helper_display_panic_screen(struct drm_plane *plane, const char *d
 }
 EXPORT_SYMBOL(drm_plane_helper_display_panic_screen);
 
+#define drm_panic_trylock(dev, flags) \
+	raw_spin_trylock_irqsave(&(dev)->mode_config.panic_lock, flags)
+
 static void drm_panic_display_panic_screen(struct drm_plane *plane, const char *description)
 {
 #if defined(CONFIG_DRM_PANIC_FOREGROUND_COLOR)
drivers/gpu/drm/drm_panic_internal.h new file +55 -0

55 lines not on this page · raw message

include/drm/drm_panic.h +0 -65

82 lines not on this page · raw message

Signature · 1 line
2.55.0
author maintainer
#16 lore raw

PATCH v2 12/13 drm/panic: Move panic display code into helper library

In reply to Thomas Zimmermann

Move drm_plane_helper_display_panic_screen() and the existing panic- display code into drm_panic_helper.c. Rename symbols accordingly. Also update the QR-related code and the bindings accordingly.

Link the rsp. object file into drm_kms_helper.ko. Eventually the code will become a DRM drawing library, together with DRM's format and draw helpers. This requires more preparation. until then, KMS helpers serve as a module. All DRM drivers link it anyway.

Add a new section about drawing helpers to the documentation of the KMS helpers. List format and panic helpers there.

v2:
- fix dependencies on other DRM modules (Randy, Sashiko)
- provide placeholders for drm_panic_helper_{init,exit}() (Sashiko)

---
 Documentation/gpu/drm-kms-helpers.rst         |  11 +-
 MAINTAINERS                                   |   5 +-
 drivers/gpu/drm/Kconfig                       |   9 +-
 drivers/gpu/drm/Makefile                      |   3 +-
 drivers/gpu/drm/drm_crtc_internal.h           |   2 -
 drivers/gpu/drm/drm_drv.c                     |   1 -
 drivers/gpu/drm/drm_kms_helper_common.c       |  14 +
 drivers/gpu/drm/drm_panic.c                   | 894 -----------------
 drivers/gpu/drm/drm_panic_helper.c            | 916 ++++++++++++++++++
 ...drm_panic_qr.rs => drm_panic_helper_qr.rs} |   4 +-
 drivers/gpu/drm/drm_panic_internal.h          |  14 +
 ...m_panic_test.c => drm_panic_helper_test.c} |  24 +-
 include/drm/drm_panic.h                       |   7 -
 include/drm/drm_panic_helper.h                |   5 +
 rust/bindings/bindings_helper.h               |   4 +-
 15 files changed, 987 insertions(+), 926 deletions(-)
 create mode 100644 drivers/gpu/drm/drm_panic_helper.c
 rename drivers/gpu/drm/{drm_panic_qr.rs => drm_panic_helper_qr.rs} (99%)
 rename drivers/gpu/drm/tests/{drm_panic_test.c => drm_panic_helper_test.c} (88%)
Documentation/gpu/drm-kms-helpers.rst +10 -1
index 94cfc26acecc..400cd0112713 100644
@@ -118,12 +118,21 @@ fbdev Helper Functions Reference
 .. kernel-doc:: drivers/gpu/drm/drm_fb_helper.c
    :export:
 
-format Helper Functions Reference
+Draw Helper Functions Reference
 =================================
 
+Format-Conversion Helper Reference
+----------------------------------
+
 .. kernel-doc:: drivers/gpu/drm/drm_format_helper.c
    :export:
 
+Panic Helper Reference
+----------------------
+
+.. kernel-doc:: drivers/gpu/drm/drm_panic_helper.c
+   :export:
+
 Framebuffer DMA Helper Functions Reference
 ==========================================
 
MAINTAINERS +3 -2
index f3dfdd9314bd..f7e7c6e2d285 100644
@@ -9019,8 +9019,9 @@ T:	git https://gitlab.freedesktop.org/drm/misc/kernel.git
 F:	drivers/gpu/drm/drm_draw.c
 F:	drivers/gpu/drm/drm_draw_internal.h
 F:	drivers/gpu/drm/drm_panic.c
+F:	drivers/gpu/drm/drm_panic_helper.c
 F:	drivers/gpu/drm/drm_panic_internal.h
-F:	drivers/gpu/drm/tests/drm_panic_test.c
+F:	drivers/gpu/drm/tests/drm_panic_helper_test.c
 F:	include/drm/drm_panic.h
 F:	include/drm/drm_panic_helper.h
 
@@ -9031,7 +9032,7 @@ L:	[email protected]
 L:	[email protected]
 S:	Supported
 T:	git https://gitlab.freedesktop.org/drm/misc/kernel.git
-F:	drivers/gpu/drm/drm_panic_qr.rs
+F:	drivers/gpu/drm/drm_panic_helper_qr.rs
 
 DRM PRIVACY-SCREEN CLASS
 M:	Hans de Goede <[email protected]>
drivers/gpu/drm/Kconfig +7 -2
index 323422861e8f..65d46dfa7266 100644
@@ -45,6 +45,7 @@ config DRM_MIPI_DSI
 config DRM_KMS_HELPER
 	tristate
 	depends on DRM
+	select DRM_PANIC_HELPER if DRM_PANIC
 	select FB_CORE if DRM_FBDEV_EMULATION
 	help
 	  CRTC helpers for KMS drivers.
@@ -56,8 +57,6 @@ config DRM_DRAW
 config DRM_PANIC
 	bool "Display a user-friendly message when a kernel panic occurs"
 	depends on DRM && PRINTK
-	select FONT_SUPPORT
-	select DRM_DRAW
 	help
 	  Enable a drm panic handler, which will display a user-friendly message
 	  when a kernel panic occurs. It's useful when using a user-space
@@ -130,6 +129,12 @@ config DRM_PANIC_SCREEN_QR_VERSION
 	  Smaller QR code are easier to read, but will contain less debugging
 	  data. Default is 40.
 
+config DRM_PANIC_HELPER
+	bool
+	depends on DRM
+	select DRM_DRAW
+	select FONT_SUPPORT
+
 config DRM_RAS
 	bool "DRM RAS support"
 	depends on DRM
drivers/gpu/drm/Makefile +2 -1
index e635fcffd379..f0c340aa2ec6 100644
@@ -92,7 +92,6 @@ drm-$(CONFIG_DRM_PRIVACY_SCREEN) += \
 drm-$(CONFIG_DRM_ACCEL) += ../../accel/drm_accel.o
 drm-$(CONFIG_DRM_PANIC) += drm_panic.o
 drm-$(CONFIG_DRM_DRAW) += drm_draw.o
-drm-$(CONFIG_DRM_PANIC_SCREEN_QR_CODE) += drm_panic_qr.o
 drm-$(CONFIG_DRM_RAS) += drm_ras.o drm_ras_nl.o drm_ras_genl_family.o
 obj-$(CONFIG_DRM)	+= drm.o
 
@@ -157,6 +156,8 @@ drm_kms_helper-y := \
 	drm_simple_kms_helper.o \
 	drm_vblank_helper.o
 drm_kms_helper-$(CONFIG_DRM_PANEL_BRIDGE) += bridge/panel.o
+drm_kms_helper-$(CONFIG_DRM_PANIC_HELPER) += drm_panic_helper.o
+drm_kms_helper-$(CONFIG_DRM_PANIC_SCREEN_QR_CODE) += drm_panic_helper_qr.o
 drm_kms_helper-$(CONFIG_DRM_FBDEV_EMULATION) += drm_fb_helper.o
 obj-$(CONFIG_DRM_KMS_HELPER) += drm_kms_helper.o
 
drivers/gpu/drm/drm_crtc_internal.h +0 -2
index 83146ffef00c..3492787c3697 100644
@@ -322,13 +322,11 @@ bool drm_panic_is_enabled(struct drm_device *dev);
 void drm_panic_register(struct drm_device *dev);
 void drm_panic_unregister(struct drm_device *dev);
 void drm_panic_init(void);
-void drm_panic_exit(void);
 #else
 static inline bool drm_panic_is_enabled(struct drm_device *dev) { return false; }
 static inline void drm_panic_register(struct drm_device *dev) {}
 static inline void drm_panic_unregister(struct drm_device *dev) {}
 static inline void drm_panic_init(void) {}
-static inline void drm_panic_exit(void) {}
 #endif
 
 #endif /* __DRM_CRTC_INTERNAL_H__ */
drivers/gpu/drm/drm_drv.c +0 -1
index 02ff02931515..1e813645b3eb 100644
@@ -1248,7 +1248,6 @@ static void drm_core_exit(void)
 {
 	drm_ras_genl_family_unregister();
 	drm_privacy_screen_lookup_exit();
-	drm_panic_exit();
 	accel_core_exit();
 	unregister_chrdev(DRM_MAJOR, "drm");
 	drm_debugfs_remove_root();
drivers/gpu/drm/drm_kms_helper_common.c +14 -0
index 0c7550c0462b..b5d5b469b444 100644
@@ -27,6 +27,20 @@
 
 #include <linux/module.h>
 
+#include "drm_panic_internal.h"
+
+static int __init drm_kms_helper_init(void)
+{
+	return drm_panic_helper_init();
+}
+module_init(drm_kms_helper_init);
+
+static void __exit drm_kms_helper_exit(void)
+{
+	drm_panic_helper_exit();
+}
+module_exit(drm_kms_helper_exit);
+
 MODULE_AUTHOR("David Airlie, Jesse Barnes");
 MODULE_DESCRIPTION("DRM KMS helper");
 MODULE_LICENSE("GPL and additional rights");
drivers/gpu/drm/drm_panic.c +0 -894

936 lines not on this page · raw message

drivers/gpu/drm/drm_panic_helper.c new file +916 -0

916 lines not on this page · raw message

drivers/gpu/drm/drm_panic_helper_qr.rs renamed from drivers/gpu/drm/drm_panic_qr.rs +2 -2
similarity index 99%
rename from drivers/gpu/drm/drm_panic_qr.rs
rename to drivers/gpu/drm/drm_panic_helper_qr.rs
index ac27e86c601c..22966956331e 100644
@@ -938,7 +938,7 @@ fn draw_all(&mut self, data: impl Iterator<Item = u8>) {
 ///
 /// They must remain valid for the duration of the function call.
 #[export]
-pub unsafe extern "C" fn drm_panic_qr_generate(
+pub unsafe extern "C" fn drm_panic_helper_qr_generate(
     url: *const kernel::ffi::c_char,
     data: *mut u8,
     data_len: usize,
@@ -994,7 +994,7 @@ fn draw_all(&mut self, data: impl Iterator<Item = u8>) {
 /// Always safe to call.
 // Required to be unsafe due to the `#[export]` annotation.
 #[export]
-pub unsafe extern "C" fn drm_panic_qr_max_data_size(version: u8, url_len: usize) -> usize {
+pub unsafe extern "C" fn drm_panic_helper_qr_max_data_size(version: u8, url_len: usize) -> usize {
     #[expect(clippy::manual_range_contains)]
     if version < 1 || version > 40 {
         return 0;
drivers/gpu/drm/drm_panic_internal.h +14 -0
index c68d64c56e15..fe6d514a0833 100644
@@ -52,4 +52,18 @@ static inline void drm_panic_lock(struct drm_device *dev, unsigned long flags) {
 static inline void drm_panic_unlock(struct drm_device *dev, unsigned long flags) {}
 #endif
 
+#if IS_ENABLED(CONFIG_DRM_PANIC_HELPER)
+/* drm_panic_helper.c */
+int __init drm_panic_helper_init(void);
+void __exit drm_panic_helper_exit(void);
+#else
+static inline int __init drm_panic_helper_init(void)
+{
+	return 0;
+}
+
+static inline void __exit drm_panic_helper_exit(void)
+{ }
+#endif
+
 #endif /* __DRM_PANIC_INTERNAL_H__ */
drivers/gpu/drm/tests/drm_panic_helper_test.c renamed from drivers/gpu/drm/tests/drm_panic_test.c +12 -12
similarity index 88%
rename from drivers/gpu/drm/tests/drm_panic_test.c
rename to drivers/gpu/drm/tests/drm_panic_helper_test.c
index 55d77bc203df..e6befe0f1c83 100644
@@ -3,7 +3,7 @@
  * Copyright (c) 2025 Red Hat.
  * Author: Jocelyn Falempe <[email protected]>
  *
- * KUNIT tests for drm panic
+ * KUNIT tests for DRM panic helpers
  */
 
 #include <drm/drm_fourcc.h>
@@ -41,24 +41,24 @@ struct drm_test_mode {
 	.width = w, \
 	.height = h, \
 	.format = f, \
-	.draw_screen = draw_panic_screen_ ## name ## _default, \
+	.draw_screen = drm_panic_helper_draw_screen_ ## name ## _default, \
 	.fname = #name, \
 	}, \
 
-static int draw_panic_screen_user_default(struct drm_scanout_buffer *sb)
+static int drm_panic_helper_draw_screen_user_default(struct drm_scanout_buffer *sb)
 {
-	return draw_panic_screen_user(sb, 0x00ffffff, 0x00000000);
+	return drm_panic_helper_draw_screen_user(sb, 0x00ffffff, 0x00000000);
 }
 
-static int draw_panic_screen_kmsg_default(struct drm_scanout_buffer *sb)
+static int drm_panic_helper_draw_screen_kmsg_default(struct drm_scanout_buffer *sb)
 {
-	return draw_panic_screen_kmsg(sb, 0x00ffffff, 0x00000000);
+	return drm_panic_helper_draw_screen_kmsg(sb, 0x00ffffff, 0x00000000);
 }
 
 #if IS_ENABLED(CONFIG_DRM_PANIC_SCREEN_QR_CODE)
-static int draw_panic_screen_qr_code_default(struct drm_scanout_buffer *sb)
+static int drm_panic_helper_draw_screen_qr_code_default(struct drm_scanout_buffer *sb)
 {
-	return draw_panic_screen_qr_code(sb, 0x00ffffff, 0x00000000, 40);
+	return drm_panic_helper_draw_screen_qr_code(sb, 0x00ffffff, 0x00000000, 40);
 }
 #endif
 
@@ -81,7 +81,7 @@ static int drm_test_panic_init(struct kunit *test)
 
 	test->priv = priv;
 
-	drm_panic_set_description("Kunit testing");
+	drm_panic_helper_set_description("Kunit testing");
 
 	return 0;
 }
@@ -226,10 +226,10 @@ static struct kunit_case drm_panic_screen_user_test[] = {
 	{ }
 };
 
-static struct kunit_suite drm_panic_suite = {
-	.name = "drm_panic",
+static struct kunit_suite drm_panic_helper_suite = {
+	.name = "drm_panic_helper",
 	.init = drm_test_panic_init,
 	.test_cases = drm_panic_screen_user_test,
 };
 
-kunit_test_suite(drm_panic_suite);
+kunit_test_suite(drm_panic_helper_suite);
include/drm/drm_panic.h +0 -7
index 99572b7eeab9..3e628cbaffca 100644
@@ -85,11 +85,4 @@ struct drm_scanout_buffer {
 	void *private;
 };
 
-#if defined(CONFIG_DRM_PANIC_SCREEN_QR_CODE)
-size_t drm_panic_qr_max_data_size(u8 version, size_t url_len);
-
-u8 drm_panic_qr_generate(const char *url, u8 *data, size_t data_len, size_t data_size,
-			 u8 *tmp, size_t tmp_size);
-#endif
-
 #endif /* __DRM_PANIC_H__ */
include/drm/drm_panic_helper.h +5 -0
index 0b6ad8f61821..6cc9a0eba077 100644
@@ -16,6 +16,11 @@ int drm_plane_helper_display_panic_screen(struct drm_plane *plane,
 					  u32 fg_color, u32 bg_color,
 					  unsigned int qr_version);
 
+/* drm_panic_helper_qr.rs */
+size_t drm_panic_helper_qr_max_data_size(u8 version, size_t url_len);
+u8 drm_panic_helper_qr_generate(const char *url, u8 *data, size_t data_len, size_t data_size,
+				u8 *tmp, size_t tmp_size);
+
 #if IS_ENABLED(CONFIG_DRM_PANIC)
 #define DRM_PANIC_PLANE_FUNCS \
 	.display_panic_screen = drm_plane_helper_display_panic_screen
rust/bindings/bindings_helper.h +2 -2
index 1124785e210b..0a11b1f5c4b3 100644
@@ -102,8 +102,8 @@
 #include <../../drivers/base/base.h>
 
 #if defined(CONFIG_DRM_PANIC_SCREEN_QR_CODE)
-// Used by `#[export]` in `drivers/gpu/drm/drm_panic_qr.rs`.
-#include <drm/drm_panic.h>
+// Used by `#[export]` in `drivers/gpu/drm/drm_panic_helper_qr.rs`.
+#include <drm/drm_panic_helper.h>
 #endif
 
 /* `bindgen` gets confused at certain things. */
Signature · 1 line
2.55.0
author maintainer
#17 lore raw

PATCH v2 13/13 drm/panic: Compile KUnit tests as module

In reply to Thomas Zimmermann

Fix dependency of the panic-helper KUnit tests on CONFIG_DRM_KUNIT_TEST=y and make them build as a module. Export the necessary interfaces from the panic helpers iff the KUnit framework has been enabled.

Select DRM_PANIC_HELPER from DRM_KUNIT_TEST to meet the dependencies of the panic-helper tests.

It is now possible to run the tests by modprobe-ing drm_panic_helper_test at runtime.

v2:
- fix parameter order in decl of drm_panic_helper_draw_screen_qr() (Sashiko)

---
 drivers/gpu/drm/Kconfig.debug                 |  1 +
 drivers/gpu/drm/drm_panic_helper.c            | 26 ++++++++++---------
 drivers/gpu/drm/tests/Makefile                |  1 +
 drivers/gpu/drm/tests/drm_panic_helper_test.c | 15 ++++++++---
 include/drm/drm_panic_helper.h                |  8 ++++++
 5 files changed, 35 insertions(+), 16 deletions(-)
drivers/gpu/drm/Kconfig.debug +1 -0
index 1f4c98cbf090..b909cd0a619f 100644
@@ -69,6 +69,7 @@ config DRM_KUNIT_TEST
 	select DRM_EXPORT_FOR_TESTS if m
 	select DRM_GEM_SHMEM_HELPER
 	select DRM_KUNIT_TEST_HELPERS
+	select DRM_PANIC_HELPER
 	select DRM_SYSFB_HELPER
 	select PRIME_NUMBERS
 	default KUNIT_ALL_TESTS
drivers/gpu/drm/drm_panic_helper.c +14 -12
index 669f09225f8e..253262b7e0d4 100644
@@ -8,6 +8,8 @@
 #include <linux/utsname.h>
 #include <linux/zlib.h>
 
+#include <kunit/visibility.h>
+
 #include <drm/drm_fourcc.h>
 #include <drm/drm_framebuffer.h>
 #include <drm/drm_modeset_helper_vtables.h>
@@ -433,8 +435,8 @@ static void drm_panic_helper_logo_draw(struct drm_scanout_buffer *sb, struct drm
 				   fg_color);
 }
 
-static int drm_panic_helper_draw_screen_user(struct drm_scanout_buffer *sb,
-					     u32 fg_color, u32 bg_color)
+VISIBLE_IF_KUNIT int drm_panic_helper_draw_screen_user(struct drm_scanout_buffer *sb,
+						       u32 fg_color, u32 bg_color)
 {
 	const struct font_desc *font = get_default_font(sb->width, sb->height, NULL, NULL);
 	struct drm_rect r_screen, r_logo, r_msg;
@@ -466,6 +468,7 @@ static int drm_panic_helper_draw_screen_user(struct drm_scanout_buffer *sb,
 
 	return 0;
 }
+EXPORT_SYMBOL_IF_KUNIT(drm_panic_helper_draw_screen_user);
 
 /*
  * Draw one line of kmsg, and handle wrapping if it won't fit in the screen width.
@@ -504,8 +507,8 @@ static int draw_line_with_wrap(struct drm_scanout_buffer *sb, const struct font_
  * Draw the kmsg buffer to the screen, starting from the youngest message at the bottom,
  * and going up until reaching the top of the screen.
  */
-static int drm_panic_helper_draw_screen_kmsg(struct drm_scanout_buffer *sb,
-					     u32 fg_color, u32 bg_color)
+VISIBLE_IF_KUNIT int drm_panic_helper_draw_screen_kmsg(struct drm_scanout_buffer *sb,
+						       u32 fg_color, u32 bg_color)
 {
 	const struct font_desc *font = get_default_font(sb->width, sb->height, NULL, NULL);
 	struct drm_rect r_screen = DRM_RECT_INIT(0, 0, sb->width, sb->height);
@@ -549,6 +552,7 @@ static int drm_panic_helper_draw_screen_kmsg(struct drm_scanout_buffer *sb,
 
 	return 0;
 }
+EXPORT_SYMBOL_IF_KUNIT(drm_panic_helper_draw_screen_kmsg);
 
 #if defined(CONFIG_DRM_PANIC_SCREEN_QR_CODE)
 /*
@@ -691,9 +695,9 @@ static int drm_panic_helper_get_qr_code(u8 **qr_image, unsigned int qr_version)
 /*
  * Draw the panic message at the center of the screen, with a QR Code
  */
-static int drm_panic_helper_draw_screen_qr_code(struct drm_scanout_buffer *sb,
-						u32 fg_color, u32 bg_color,
-						unsigned int qr_version)
+VISIBLE_IF_KUNIT int drm_panic_helper_draw_screen_qr_code(struct drm_scanout_buffer *sb,
+							  u32 fg_color, u32 bg_color,
+							  unsigned int qr_version)
 {
 	const struct font_desc *font = get_default_font(sb->width, sb->height, NULL, NULL);
 	struct drm_rect r_screen, r_logo, r_msg, r_qr, r_qr_canvas;
@@ -763,6 +767,7 @@ static int drm_panic_helper_draw_screen_qr_code(struct drm_scanout_buffer *sb,
 	drm_panic_helper_blit(sb, &r_qr, qr_image, qr_pitch, scale, fg_color);
 	return 0;
 }
+EXPORT_SYMBOL_IF_KUNIT(drm_panic_helper_draw_screen_qr_code);
 #else
 static void __init drm_panic_helper_qr_init(void) { };
 static void __exit drm_panic_helper_qr_exit(void) { };
@@ -815,7 +820,7 @@ static int draw_panic_helper_dispatch(struct drm_scanout_buffer *sb,
 	return ret;
 }
 
-static void drm_panic_helper_set_description(const char *description)
+VISIBLE_IF_KUNIT void drm_panic_helper_set_description(const char *description)
 {
 	u32 len;
 
@@ -830,6 +835,7 @@ static void drm_panic_helper_set_description(const char *description)
 		desc_line->len = len;
 	}
 }
+EXPORT_SYMBOL_IF_KUNIT(drm_panic_helper_set_description);
 
 static void drm_panic_helper_clear_description(void)
 {
@@ -910,7 +916,3 @@ void __exit drm_panic_helper_exit(void)
 {
 	drm_panic_helper_qr_exit();
 }
-
-#ifdef CONFIG_DRM_KUNIT_TEST
-#include "tests/drm_panic_helper_test.c"
-#endif
drivers/gpu/drm/tests/Makefile +1 -0
index e7193b49cf91..42207df1d1c8 100644
@@ -20,6 +20,7 @@ obj-$(CONFIG_DRM_KUNIT_TEST) += \
 	drm_managed_test.o \
 	drm_mm_test.o \
 	drm_modes_test.o \
+	drm_panic_helper_test.o \
 	drm_plane_helper_test.o \
 	drm_probe_helper_test.o \
 	drm_rect_test.o \
drivers/gpu/drm/tests/drm_panic_helper_test.c +11 -4
index e6befe0f1c83..f24f4bc2bc1f 100644
@@ -6,13 +6,17 @@
  * KUNIT tests for DRM panic helpers
  */
 
-#include <drm/drm_fourcc.h>
-#include <drm/drm_panic.h>
+#include <linux/highmem.h>
+#include <linux/units.h>
+#include <linux/vmalloc.h>
 
 #include <kunit/test.h>
 
-#include <linux/units.h>
-#include <linux/vmalloc.h>
+#include <drm/drm_fourcc.h>
+#include <drm/drm_panic.h>
+#include <drm/drm_panic_helper.h>
+
+MODULE_IMPORT_NS("EXPORTED_FOR_KUNIT_TESTING");
 
 static void drm_panic_check_color_byte(struct kunit *test, u8 b)
 {
@@ -233,3 +237,6 @@ static struct kunit_suite drm_panic_helper_suite = {
 };
 
 kunit_test_suite(drm_panic_helper_suite);
+
+MODULE_DESCRIPTION("KUnit test suite for DRM panic handling");
+MODULE_LICENSE("GPL");
include/drm/drm_panic_helper.h +8 -0
index 6cc9a0eba077..6b19607e0784 100644
@@ -8,6 +8,7 @@
 enum drm_panic_type;
 
 struct drm_plane;
+struct drm_scanout_buffer;
 
 /* drm_panic_helper.c */
 int drm_plane_helper_display_panic_screen(struct drm_plane *plane,
@@ -15,6 +16,13 @@ int drm_plane_helper_display_panic_screen(struct drm_plane *plane,
 					  enum drm_panic_type panic_type,
 					  u32 fg_color, u32 bg_color,
 					  unsigned int qr_version);
+#if IS_ENABLED(CONFIG_KUNIT)
+int drm_panic_helper_draw_screen_user(struct drm_scanout_buffer *sb, u32 fg_color, u32 bg_color);
+int drm_panic_helper_draw_screen_kmsg(struct drm_scanout_buffer *sb, u32 fg_color, u32 bg_color);
+int drm_panic_helper_draw_screen_qr_code(struct drm_scanout_buffer *sb,
+					 u32 fg_color, u32 bg_color, unsigned int qr_version);
+void drm_panic_helper_set_description(const char *description);
+#endif
 
 /* drm_panic_helper_qr.rs */
 size_t drm_panic_helper_qr_max_data_size(u8 version, size_t url_len);
Signature · 1 line
2.55.0
#18 lore raw

Re: [PATCH v2 13/13] drm/panic: Compile KUnit tests as module

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

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

Pre-existing issues:
- [High] Dangling pointer in panic_msg array causes use-after-free during kernel panic after the test module is unloaded. --

--- Patch [13]: [PATCH v2 13/13] drm/panic: Compile KUnit tests as module --- commit 9a00d4c3f17fad5dbc6e3335649c2316482a4fad
Author: Thomas Zimmermann <[email protected]>
Subject: drm/panic: Compile KUnit tests as module

Fix dependency of the panic-helper KUnit tests on CONFIG_DRM_KUNIT_TEST=y and make them build as a module. Export the necessary interfaces from the panic helpers iff the KUnit framework has been enabled.

Quoted 4 lines
drivers/gpu/drm/drm_panic_helper.c +0 -0
index 669f09225f8ed..253262b7e0d4c 100644

[ ... ]

Quoted 7 lines
drivers/gpu/drm/drm_panic_helper.c +1 -1
-static void drm_panic_helper_set_description(const char *description)
+VISIBLE_IF_KUNIT void drm_panic_helper_set_description(const char *description)
 {
 	u32 len;
 
 	if (description) {
 		struct drm_panic_line *desc_line = &panic_msg[panic_msg_lines - 1];

drivers/gpu/drm/drm_panic_helper.c

Quoted 10 lines
drivers/gpu/drm/drm_panic_helper.c +1 -0
 
 		desc_line->txt = description;
 		len = strlen(description);
 		/* ignore the last newline character */
 		if (len && description[len - 1] == '\n')
 			len -= 1;
 		desc_line->len = len;
 	}
 }
+EXPORT_SYMBOL_IF_KUNIT(drm_panic_helper_set_description);

[Severity: High]
This is a pre-existing issue, but since the test can now be built as a module, does unloading it leave a dangling pointer in the global panic_msg array?

In drivers/gpu/drm/tests/drm_panic_helper_test.c, drm_test_panic_init() calls drm_panic_helper_set_description("Kunit testing"), which stores a pointer to the test module's .rodata.

When the drm_panic_helper_test module is unloaded, this memory is freed. Since the test suite lacks an .exit callback to call drm_panic_helper_clear_description(), the dangling pointer remains in the global array.

If a real kernel panic occurs later where detail->description is NULL (such as via kmsg_dump(KMSG_DUMP_PANIC) on some architectures), this function silently ignores the NULL input and leaves the dangling pointer intact.

Will this cause a use-after-free read when the panic screen rendering dereferences desc_line->txt, potentially preventing the panic screen from displaying during an actual crash?

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

✓ i915.CI.BAT: success for drm/panic: Split into core and helpers (rev2)

In reply to Thomas Zimmermann
== Series Details ==

== Series Details ==

Series: drm/panic: Split into core and helpers (rev2)
URL : https://patchwork.freedesktop.org/series/172395/
State : success

== Summary ==

CI Bug Log - changes from CI_DRM_19038 -> Patchwork_172395v2
====================================================

Summary
-------

**SUCCESS**

No regressions found.

External URL: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_172395v2/index.html

Participating hosts (40 -> 38)
------------------------------

Missing (2): bat-dg2-13 fi-snb-2520m

Known issues
------------

Here are the changes found in Patchwork_172395v2 that come from known issues:

### IGT changes ###

#### Issues hit ####

* igt@core_debugfs@read-all-entries:
- bat-adlp-6: [PASS][1] -> [DMESG-WARN][2] ([i915#15673])
[1]: https://intel-gfx-ci.01.org/tree/drm-tip/CI_DRM_19038/bat-adlp-6/igt@[email protected]
[2]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_172395v2/bat-adlp-6/igt@[email protected]

#### Possible fixes ####

* igt@core_auth@basic-auth:
- bat-adlp-6: [DMESG-WARN][3] ([i915#15673]) -> [PASS][4]
[3]: https://intel-gfx-ci.01.org/tree/drm-tip/CI_DRM_19038/bat-adlp-6/igt@[email protected]
[4]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_172395v2/bat-adlp-6/igt@[email protected]

[i915#15673]: https://gitlab.freedesktop.org/drm/i915/kernel/-/issues/15673

Build changes
-------------

* Linux: CI_DRM_19038 -> Patchwork_172395v2

CI-20190529: 20190529
CI_DRM_19038: 00537470d4dadf8e803a2df45b36940dba096d4b @ git://anongit.freedesktop.org/gfx-ci/linux
IGT_9070: 80b6e4c9b9cb853c63a9bcaa4784e0306d1f7fca @ https://gitlab.freedesktop.org/drm/igt-gpu-tools.git
Patchwork_172395v2: 00537470d4dadf8e803a2df45b36940dba096d4b @ git://anongit.freedesktop.org/gfx-ci/linux

== Logs ==

For more details see: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_172395v2/index.html