Skip to contentSkip to conversation

drm/log: Introduce a new boot logger to draw the kmsg on the screen

PATCH Superseded Jocelyn Falempe 3 participants 8 messages Sep 10, 2024 – Oct 7, 2024

Subsystems (3)

v3 of 9 Superseded Latest is v9 →

lore
Share & tools
lore

Jocelyn Falempe

author maintainer
#0 lore raw

v3 cover drm/log: Introduce a new boot logger to draw the kmsg on the screen

drm_log is a simple logger that uses the drm_client API to print the kmsg boot log on the screen. This is not a full replacement to fbcon, as it will only print the kmsg.
It will never handle user input, or a terminal because this is better done in userspace.

If you're curious on how it looks like, I've put a small demo here:
https://people.redhat.com/jfalempe/drm_log/drm_log_draft_boot_v2.mp4

Design decisions:
* It uses the drm_client API, so it should work on all drm drivers from the start.
* It doesn't scroll the message, that way it doesn't need to redraw the whole screen for each new message. It also means it doesn't have to keep drawn messages in memory, to redraw them when scrolling.
* drm_log can only be built-in (and drm must be built-in too).
The reason is that, if you build it as a module, then a userspace application will be more appropriate than this module.
* It uses the new non-blocking console API, so it should work well with PREEMPT_RT

v2:
* Use vmap_local() api, with that change, I've tested it successfully on simpledrm, virtio-gpu, amdgpu, and nouveau.
* Stop drawing when the drm_master is taken. This avoid wasting CPU cycle if the buffer is not visible.
* Use deferred probe. Only do the probe the first time there is a log to draw. With this, if you boot with quiet, drm_log won't do any modeset.
* Add color support for the timestamp prefix, like what dmesg does.
* Add build dependency on disabling the fbdev emulation, as they are both drm_client, and there is no way to choose which one gets the focus.

v3:
* Remove the work thread and circular buffer, and use the new write_thread() console API.
* Register a console for each drm driver.

Thanks and best regards,

Jocelyn Falempe (4):
drm/panic: Move drawing functions to drm_draw
drm/log: Introduce a new boot logger to draw the kmsg on the screen drm/log: Do not draw if drm_master is taken
drm/log: Color the timestamp, to improve readability

 drivers/gpu/drm/Kconfig     |  19 ++
 drivers/gpu/drm/Makefile    |   3 +
 drivers/gpu/drm/drm_draw.c  | 216 ++++++++++++++++++++
 drivers/gpu/drm/drm_draw.h  |  56 ++++++
 drivers/gpu/drm/drm_drv.c   |   2 +
 drivers/gpu/drm/drm_log.c   | 391 ++++++++++++++++++++++++++++++++++++
 drivers/gpu/drm/drm_log.h   |  11 +
 drivers/gpu/drm/drm_panic.c | 247 +++--------------------
 8 files changed, 721 insertions(+), 224 deletions(-)
 create mode 100644 drivers/gpu/drm/drm_draw.c
 create mode 100644 drivers/gpu/drm/drm_draw.h
 create mode 100644 drivers/gpu/drm/drm_log.c
 create mode 100644 drivers/gpu/drm/drm_log.h

base-commit: 9aaeb87ce1e966169a57f53a02ba05b30880ffb8

Signature · 1 line
2.46.0
author maintainer
#1 lore raw

PATCH v3 1/4 drm/panic: Move drawing functions to drm_draw

Move the color conversions, blit and fill functions to drm_draw.c, so that they can be re-used by drm_log.
drm_draw is internal to the drm subsystem, and shouldn't be used by gpu drivers.

---
 drivers/gpu/drm/Kconfig     |   5 +
 drivers/gpu/drm/Makefile    |   1 +
 drivers/gpu/drm/drm_draw.c  | 216 +++++++++++++++++++++++++++++++
 drivers/gpu/drm/drm_draw.h  |  56 ++++++++
 drivers/gpu/drm/drm_panic.c | 247 ++++--------------------------------
 5 files changed, 301 insertions(+), 224 deletions(-)
 create mode 100644 drivers/gpu/drm/drm_draw.c
 create mode 100644 drivers/gpu/drm/drm_draw.h
drivers/gpu/drm/Kconfig +5 -0
index 1cb5a4f19293..fafb3884106a 100644
@@ -105,10 +105,15 @@ config DRM_KMS_HELPER
 	help
 	  CRTC helpers for KMS drivers.
 
+config DRM_DRAW
+	bool
+	depends on DRM
+
 config DRM_PANIC
 	bool "Display a user-friendly message when a kernel panic occurs"
 	depends on DRM
 	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
drivers/gpu/drm/Makefile +1 -0
index 784229d4504d..62e03aa6a66e 100644
@@ -89,6 +89,7 @@ drm-$(CONFIG_DRM_PRIVACY_SCREEN) += \
 	drm_privacy_screen_x86.o
 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
 obj-$(CONFIG_DRM)	+= drm.o
 
drivers/gpu/drm/drm_draw.c new file +216 -0

216 lines not on this page · raw message

drivers/gpu/drm/drm_draw.h new file +56 -0

56 lines not on this page · raw message

drivers/gpu/drm/drm_panic.c +23 -224

332 lines not on this page · raw message

Signature · 1 line
2.46.0
author maintainer
#2 lore raw

PATCH v3 2/4 drm/log: Introduce a new boot logger to draw the kmsg on the screen

In reply to Jocelyn Falempe

drm_log is a simple logger that uses the drm_client API to print the kmsg boot log on the screen. This is not a full replacement to fbcon, as it will only print the kmsg. It will never handle user input, or a terminal because this is better done in userspace.

Design decisions:
* It uses the drm_client API, so it should work on all drm drivers from the start.
* It doesn't scroll the message, that way it doesn't need to redraw the whole screen for each new message.
It also means it doesn't have to keep drawn messages in memory, to redraw them when scrolling.
* It uses the new non-blocking console API, so it should work well with PREEMPT_RT.
* drm_log can only be built-in (and drm must be built-in too). The reason is that, if you build it as a module, then a userspace application will be more appropriate than this module.

---

v2:
* Use vmap_local() api, with that change, I've tested it successfully on simpledrm, virtio-gpu, amdgpu, and nouveau.
* Stop drawing when the drm_master is taken. This avoid wasting CPU cycle if the buffer is not visible.
* Use deferred probe. Only do the probe the first time there is a log to draw. With this, if you boot with quiet, drm_log won't do any modeset.
* Add color support for the timestamp prefix, like what dmesg does.
* Add build dependency on disabling the fbdev emulation, as they are both drm_client, and there is no way to choose which one gets the focus.

v3:
* Remove the work thread and circular buffer, and use the new write_thread() console API.
* Register a console for each drm driver.

 drivers/gpu/drm/Kconfig   |  14 ++
 drivers/gpu/drm/Makefile  |   2 +
 drivers/gpu/drm/drm_drv.c |   2 +
 drivers/gpu/drm/drm_log.c | 370 ++++++++++++++++++++++++++++++++++++++
 drivers/gpu/drm/drm_log.h |  11 ++
 5 files changed, 399 insertions(+)
 create mode 100644 drivers/gpu/drm/drm_log.c
 create mode 100644 drivers/gpu/drm/drm_log.h
drivers/gpu/drm/Kconfig +14 -0
index fafb3884106a..cbebafc5bbf5 100644
@@ -185,6 +185,20 @@ config DRM_PANIC_SCREEN_QR_VERSION
 	  Smaller QR code are easier to read, but will contain less debugging
 	  data. Default is 40.
 
+config DRM_LOG
+	bool "Print the kernel boot message on the screen"
+	depends on DRM=y
+	depends on !DRM_FBDEV_EMULATION
+	select DRM_DRAW
+	help
+	  This enable a drm logger, that will print the kernel messages to the
+	  screen until the userspace is ready to take over.
+	  It only makes sense to have it built-in, because otherwise it would
+	  be loaded after userspace.
+
+	  If you only need logs, but no terminal, or if you prefer userspace
+	  terminal, say "Y".
+
 config DRM_DEBUG_DP_MST_TOPOLOGY_REFS
         bool "Enable refcount backtrace history in the DP MST helpers"
 	depends on STACKTRACE_SUPPORT
drivers/gpu/drm/Makefile +2 -0
index 62e03aa6a66e..9850dcf7ed7b 100644
@@ -148,6 +148,8 @@ drm_kms_helper-$(CONFIG_DRM_PANEL_BRIDGE) += bridge/panel.o
 drm_kms_helper-$(CONFIG_DRM_FBDEV_EMULATION) += drm_fb_helper.o
 obj-$(CONFIG_DRM_KMS_HELPER) += drm_kms_helper.o
 
+obj-$(CONFIG_DRM_LOG) += drm_log.o
+
 #
 # Drivers and the rest
 #
drivers/gpu/drm/drm_drv.c +2 -0
index ac30b0ec9d93..a18cff3bcac2 100644
@@ -49,6 +49,7 @@
 #include <drm/drm_privacy_screen_machine.h>
 
 #include "drm_crtc_internal.h"
+#include "drm_log.h"
 #include "drm_internal.h"
 
 MODULE_AUTHOR("Gareth Hughes, Leif Delgass, José Fonseca, Jon Smirl");
@@ -944,6 +945,7 @@ int drm_dev_register(struct drm_device *dev, unsigned long flags)
 			goto err_unload;
 	}
 	drm_panic_register(dev);
+	drm_log_register(dev);
 
 	DRM_INFO("Initialized %s %d.%d.%d for %s on minor %d\n",
 		 driver->name, driver->major, driver->minor,
drivers/gpu/drm/drm_log.c new file +370 -0

370 lines not on this page · raw message

drivers/gpu/drm/drm_log.h new file +11 -0
new file mode 100644
index 000000000000..3a4ea8150947
@@ -0,0 +1,11 @@
+/* SPDX-License-Identifier: GPL-2.0 or MIT */
+#ifndef __DRM_LOG_H__
+#define __DRM_LOG_H__
+
+#ifdef CONFIG_DRM_LOG
+void drm_log_register(struct drm_device *dev);
+#else
+static inline void drm_log_register(struct drm_device *dev) {}
+#endif
+
+#endif
Signature · 1 line
2.46.0
Reviewed-by on v3 2/4
#3 lore raw
Quoted 18 lines · Jocelyn Falempe

drm_log is a simple logger that uses the drm_client API to print the kmsg boot log on the screen. This is not a full replacement to fbcon, as it will only print the kmsg. It will never handle user input, or a terminal because this is better done in userspace.

Design decisions:
* It uses the drm_client API, so it should work on all drm drivers from the start.
* It doesn't scroll the message, that way it doesn't need to redraw the whole screen for each new message.
It also means it doesn't have to keep drawn messages in memory, to redraw them when scrolling.
* It uses the new non-blocking console API, so it should work well with PREEMPT_RT.
* drm_log can only be built-in (and drm must be built-in too). The reason is that, if you build it as a module, then a userspace application will be more appropriate than this module.

Quoted 1 line
author maintainer
#4 lore raw

PATCH v3 3/4 drm/log: Do not draw if drm_master is taken

In reply to Jocelyn Falempe

When userspace takes drm_master, the drm_client buffer is no more visible, so drm_log shouldn't waste CPU cycle to draw on it.

---
 drivers/gpu/drm/drm_log.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
drivers/gpu/drm/drm_log.c +8 -2
index 376ee173d99d..226e206e8b6a 100644
@@ -18,6 +18,7 @@
 #include <drm/drm_print.h>
 
 #include "drm_draw.h"
+#include "drm_internal.h"
 #include "drm_log.h"
 
 MODULE_AUTHOR("Jocelyn Falempe");
@@ -308,8 +309,13 @@ static void drm_log_write_thread(struct console *con, struct nbcon_write_context
 	if (!dlog->probed)
 		drm_log_init_client(dlog);
 
-	for (i = 0; i < dlog->n_scanout; i++)
-		drm_log_draw_kmsg_record(&dlog->scanout[i], wctxt->outbuf, wctxt->len);
+	/* Check that we are still the master before drawing */
+	if (drm_master_internal_acquire(dlog->client.dev)) {
+		drm_master_internal_release(dlog->client.dev);
+
+		for (i = 0; i < dlog->n_scanout; i++)
+			drm_log_draw_kmsg_record(&dlog->scanout[i], wctxt->outbuf, wctxt->len);
+	}
 }
 
 static void drm_log_lock(struct console *con, unsigned long *flags)
Signature · 1 line
2.46.0
author maintainer
#5 lore raw

PATCH v3 4/4 drm/log: Color the timestamp, to improve readability

In reply to Jocelyn Falempe

Color the timesamp prefix, similar to dmesg.

---
 drivers/gpu/drm/drm_log.c | 27 +++++++++++++++++++++------
 1 file changed, 21 insertions(+), 6 deletions(-)
drivers/gpu/drm/drm_log.c +21 -6
index 226e206e8b6a..635dff7b37ce 100644
@@ -42,6 +42,7 @@ struct drm_log_scanout {
 	u32 format;
 	u32 px_width;
 	u32 front_color;
+	u32 prefix_color;
 };
 
 struct drm_log {
@@ -97,7 +98,7 @@ static void drm_log_clear_line(struct drm_log_scanout *scanout, u32 line)
 }
 
 static void drm_log_draw_line(struct drm_log_scanout *scanout, const char *s,
-			      unsigned int len)
+			      unsigned int len, unsigned int prefix_len)
 {
 	struct drm_framebuffer *fb = scanout->buffer->fb;
 	struct iosys_map map;
@@ -114,9 +115,10 @@ static void drm_log_draw_line(struct drm_log_scanout *scanout, const char *s,
 
 	iosys_map_incr(&map, r.y1 * fb->pitches[0]);
 	for (i = 0; i < len && i < scanout->columns; i++) {
+		u32 color = (i < prefix_len) ? scanout->prefix_color : scanout->front_color;
 		src = drm_draw_get_char_bitmap(font, s[i], font_pitch);
 		drm_log_blit(&map, fb->pitches[0], src, font_pitch, font->height, font->width,
-			     1, px_width, scanout->front_color);
+			     1, px_width, color);
 		iosys_map_incr(&map, font->width * px_width);
 	}
 
@@ -128,7 +130,7 @@ static void drm_log_draw_line(struct drm_log_scanout *scanout, const char *s,
 }
 
 static void drm_log_draw_new_line(struct drm_log_scanout *scanout,
-				  const char *s, unsigned int len)
+				  const char *s, unsigned int len, unsigned int prefix_len)
 {
 	if (scanout->line == 0) {
 		drm_log_clear_line(scanout, 0);
@@ -137,23 +139,35 @@ static void drm_log_draw_new_line(struct drm_log_scanout *scanout,
 	} else if (scanout->line + 2 < scanout->rows)
 		drm_log_clear_line(scanout, scanout->line + 2);
 
-	drm_log_draw_line(scanout, s, len);
+	drm_log_draw_line(scanout, s, len, prefix_len);
 }
 
+/*
+ * Depends on print_time() in printk.c
+ * Timestamp is written with "[%5lu.%06lu]"
+ */
+#define TS_PREFIX_LEN 13
+
 static void drm_log_draw_kmsg_record(struct drm_log_scanout *scanout,
 				     const char *s, unsigned int len)
 {
+	u32 prefix_len = 0;
+
+	if (len > TS_PREFIX_LEN && s[0] == '[' && s[6] == '.' && s[TS_PREFIX_LEN] == ']')
+		prefix_len = TS_PREFIX_LEN + 1;
+
 	/* do not print the ending \n character */
 	if (s[len - 1] == '\n')
 		len--;
 
 	while (len > scanout->columns) {
-		drm_log_draw_new_line(scanout, s, scanout->columns);
+		drm_log_draw_new_line(scanout, s, scanout->columns, prefix_len);
 		s += scanout->columns;
 		len -= scanout->columns;
+		prefix_len = 0;
 	}
 	if (len)
-		drm_log_draw_new_line(scanout, s, len);
+		drm_log_draw_new_line(scanout, s, len, prefix_len);
 }
 
 static u32 drm_log_find_usable_format(struct drm_plane *plane)
@@ -193,6 +207,7 @@ static int drm_log_setup_modeset(struct drm_client_dev *client,
 	scanout->rows = height / scanout->font->height;
 	scanout->columns = width / scanout->font->width;
 	scanout->front_color = drm_draw_color_from_xrgb8888(0xffffff, format);
+	scanout->prefix_color = drm_draw_color_from_xrgb8888(0x4e9a06, format);
 	return 0;
 }
 
Signature · 1 line
2.46.0
Tested-by on the v3 series
#6 lore raw
In reply to Jocelyn Falempe

Hi Jocelyn,

Quoted 3 lines · Jocelyn Falempe

drm_log is a simple logger that uses the drm_client API to print the kmsg boot log on the screen. This is not a full replacement to fbcon, as it will only print the kmsg.
It will never handle user input, or a terminal because this is better done in userspace.

I tried this out on the OnePlus 6 (Qualcomm SDM845/freedreno) and it looks great :D

Here's a demo for kicks:

https://people.linaro.org/~caleb.connolly/drm_log_oneplus6.mp4

Kind regards,

Quoted 47 lines

If you're curious on how it looks like, I've put a small demo here:
https://people.redhat.com/jfalempe/drm_log/drm_log_draft_boot_v2.mp4

Design decisions:
* It uses the drm_client API, so it should work on all drm drivers from the start.
* It doesn't scroll the message, that way it doesn't need to redraw the whole screen for each new message. It also means it doesn't have to keep drawn messages in memory, to redraw them when scrolling.
* drm_log can only be built-in (and drm must be built-in too).
The reason is that, if you build it as a module, then a userspace application will be more appropriate than this module.
* It uses the new non-blocking console API, so it should work well with PREEMPT_RT

v2:
* Use vmap_local() api, with that change, I've tested it successfully on simpledrm, virtio-gpu, amdgpu, and nouveau.
* Stop drawing when the drm_master is taken. This avoid wasting CPU cycle if the buffer is not visible.
* Use deferred probe. Only do the probe the first time there is a log to draw. With this, if you boot with quiet, drm_log won't do any modeset.
* Add color support for the timestamp prefix, like what dmesg does.
* Add build dependency on disabling the fbdev emulation, as they are both drm_client, and there is no way to choose which one gets the focus.

v3:
* Remove the work thread and circular buffer, and use the new write_thread() console API.
* Register a console for each drm driver.

Thanks and best regards,

Jocelyn Falempe (4):
drm/panic: Move drawing functions to drm_draw
drm/log: Introduce a new boot logger to draw the kmsg on the screen drm/log: Do not draw if drm_master is taken
drm/log: Color the timestamp, to improve readability

  drivers/gpu/drm/Kconfig     |  19 ++
  drivers/gpu/drm/Makefile    |   3 +
  drivers/gpu/drm/drm_draw.c  | 216 ++++++++++++++++++++
  drivers/gpu/drm/drm_draw.h  |  56 ++++++
  drivers/gpu/drm/drm_drv.c   |   2 +
  drivers/gpu/drm/drm_log.c   | 391 ++++++++++++++++++++++++++++++++++++
  drivers/gpu/drm/drm_log.h   |  11 +
  drivers/gpu/drm/drm_panic.c | 247 +++--------------------
  8 files changed, 721 insertions(+), 224 deletions(-)

create mode 100644 drivers/gpu/drm/drm_draw.c
create mode 100644 drivers/gpu/drm/drm_draw.h
create mode 100644 drivers/gpu/drm/drm_log.c
create mode 100644 drivers/gpu/drm/drm_log.h

base-commit: 9aaeb87ce1e966169a57f53a02ba05b30880ffb8

Signature · 1 line
// Caleb (they/them)
author maintainer
#7 lore raw
Quoted 8 lines · Caleb Connolly

Hi Jocelyn,

Quoted 3 lines · Jocelyn Falempe

drm_log is a simple logger that uses the drm_client API to print the kmsg boot log on the screen. This is not a full replacement to fbcon, as it will only print the kmsg.
It will never handle user input, or a terminal because this is better done in userspace.

Quoted 5 lines

I tried this out on the OnePlus 6 (Qualcomm SDM845/freedreno) and it looks great :D

Here's a demo for kicks:

https://people.linaro.org/~caleb.connolly/drm_log_oneplus6.mp4

Thanks, it's a really nice demo!
For high-resolution screen, I will add integer scaling soon, so that should be a bit easier to read, without requiring huge fonts.

Signature · 1 line
Jocelyn
Quoted 51 lines

Kind regards,

Quoted 46 lines

If you're curious on how it looks like, I've put a small demo here:
https://people.redhat.com/jfalempe/drm_log/drm_log_draft_boot_v2.mp4

Design decisions:
   * It uses the drm_client API, so it should work on all drm drivers from the start.
   * It doesn't scroll the message, that way it doesn't need to redraw the whole screen for each new message.
     It also means it doesn't have to keep drawn messages in memory, to redraw them when scrolling.
   * drm_log can only be built-in (and drm must be built-in too).
     The reason is that, if you build it as a module, then a userspace application will be more appropriate than this module.
   * It uses the new non-blocking console API, so it should work well with PREEMPT_RT
v2:
  * Use vmap_local() api, with that change, I've tested it successfully on simpledrm, virtio-gpu, amdgpu, and nouveau.
  * Stop drawing when the drm_master is taken. This avoid wasting CPU cycle if the buffer is not visible.
  * Use deferred probe. Only do the probe the first time there is a log to draw. With this, if you boot with quiet, drm_log won't do any modeset.
  * Add color support for the timestamp prefix, like what dmesg does.
  * Add build dependency on  disabling the fbdev emulation, as they are both drm_client, and there is no way to choose which one gets the focus.

v3:
  * Remove the work thread and circular buffer, and use the new write_thread() console API.
  * Register a console for each drm driver.

Thanks and best regards,

Jocelyn Falempe (4):
   drm/panic: Move drawing functions to drm_draw
   drm/log: Introduce a new boot logger to draw the kmsg on the screen drm/log: Do not draw if drm_master is taken
   drm/log: Color the timestamp, to improve readability

  drivers/gpu/drm/Kconfig     |  19 ++ drivers/gpu/drm/Makefile    |   3 + drivers/gpu/drm/drm_draw.c  | 216 ++++++++++++++++++++ drivers/gpu/drm/drm_draw.h  |  56 ++++++

  drivers/gpu/drm/drm_drv.c   |   2 +
  drivers/gpu/drm/drm_log.c   | 391 ++++++++++++++++++++++++++++++++++++
  drivers/gpu/drm/drm_log.h   |  11 +
  drivers/gpu/drm/drm_panic.c | 247 +++--------------------
  8 files changed, 721 insertions(+), 224 deletions(-)

  create mode 100644 drivers/gpu/drm/drm_draw.c
  create mode 100644 drivers/gpu/drm/drm_draw.h
  create mode 100644 drivers/gpu/drm/drm_log.c
  create mode 100644 drivers/gpu/drm/drm_log.h

base-commit: 9aaeb87ce1e966169a57f53a02ba05b30880ffb8