drm/panic: Add a qr_code panic screen
Subsystems (4)
Jocelyn Falempe
maintainercover drm/panic: Add a qr_code panic screen
This series adds a new panic screen, with the kmsg data embedded in a QR-code.
The main advantage of QR-code, is that you can copy/paste the debug data to a bug report.
The QR-code encoder is written in rust, and is very specific to drm_panic.
The reason is that it is called in a panic handler, and thus can't allocate memory, or use locking. The rust code uses a few rust core API, and provides only two C entry points.
There is no particular reason to do it in rust, I just wanted to learn rust, and see if it can work in the kernel.
If you want to see what it looks like, I've put a few screenshots here:
https://github.com/kdj0c/panic_report/issues/1
Signature · 23 lines
Jocelyn Jocelyn Falempe (4): drm/panic: Add integer scaling to blit() drm/rect: add drm_rect_overlap() drm/panic: simplify logo handling drm/panic: Add a qr_code panic screen drivers/gpu/drm/Kconfig | 29 + drivers/gpu/drm/Makefile | 1 + drivers/gpu/drm/drm_drv.c | 3 + drivers/gpu/drm/drm_panic.c | 338 +++++++++-- drivers/gpu/drm/drm_panic_qr.rs | 989 ++++++++++++++++++++++++++++++++ include/drm/drm_panic.h | 4 + include/drm/drm_rect.h | 15 + 7 files changed, 1340 insertions(+), 39 deletions(-) create mode 100644 drivers/gpu/drm/drm_panic_qr.rs base-commit: 3f5ea7ed705e8effe9cfabf912e769ccb6b7d389 -- 2.45.2
Jocelyn Falempe
maintainerPATCH 1/4 drm/panic: Add integer scaling to blit()
Add a parameter to the blit function, to upscale the image. This is necessary to draw QR-code, otherwise, the pixels are usually too small to be readable by most QR-code reader.
It can also be used later for drawing fonts on high-DPI display.
--- drivers/gpu/drm/drm_panic.c | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-)
drivers/gpu/drm/drm_panic.c +19 -14
@@ -249,20 +249,20 @@ static bool drm_panic_is_pixel_fg(const u8 *sbuf8, unsigned int spitch, int x, istatic void drm_panic_blit16 (struct iosys_map *dmap,unsigned int dpitch,const u8 *sbuf8,unsigned int spitch,unsigned int height,unsigned int width, -u16 fg16) +unsigned int scale,u16 fg16) {unsigned int y, x;for (y =0 ; y < height; y++)for (x =0 ; x < width; x++) -if (drm_panic_is_pixel_fg (sbuf8, spitch, x, y)) +if (drm_panic_is_pixel_fg (sbuf8, spitch, x / scale, y / scale))iosys_map_wr (dmap, y * dpitch + x *sizeof (u16), u16, fg16); }static void drm_panic_blit24 (struct iosys_map *dmap,unsigned int dpitch,const u8 *sbuf8,unsigned int spitch,unsigned int height,unsigned int width, -u32 fg32) +unsigned int scale,u32 fg32) {unsigned int y, x;
@@ -270,7 +270,7 @@ static void drm_panic_blit24(struct iosys_map *dmap, unsigned int dpitch,for (x =0 ; x < width; x++) {u32 off = y * dpitch + x *3 ; -if (drm_panic_is_pixel_fg (sbuf8, spitch, x, y)) { +if (drm_panic_is_pixel_fg (sbuf8, spitch, x / scale, y / scale)) {/* write blue-green-red to output in little endianness */ iosys_map_wr (dmap, off, u8, (fg32 &0x000000FF ) >>0 );iosys_map_wr (dmap, off +1 , u8, (fg32 &0x0000FF00 ) >>8 );
@@ -283,24 +283,25 @@ static void drm_panic_blit24(struct iosys_map *dmap, unsigned int dpitch,static void drm_panic_blit32 (struct iosys_map *dmap,unsigned int dpitch,const u8 *sbuf8,unsigned int spitch,unsigned int height,unsigned int width, -u32 fg32) +unsigned int scale,u32 fg32) {unsigned int y, x;for (y =0 ; y < height; y++)for (x =0 ; x < width; x++) -if (drm_panic_is_pixel_fg (sbuf8, spitch, x, y)) +if (drm_panic_is_pixel_fg (sbuf8, spitch, x / scale, y / scale))iosys_map_wr (dmap, y * dpitch + x *sizeof (u32), u32, fg32); }static void drm_panic_blit_pixel (struct drm_scanout_buffer *sb,struct drm_rect *clip, -const u8 *sbuf8,unsigned int spitch,u32 fg_color) +const u8 *sbuf8,unsigned int spitch,unsigned int scale, +u32 fg_color) {unsigned int y, x;for (y =0 ; y <drm_rect_height (clip); y++)for (x =0 ; x <drm_rect_width (clip); x++) -if (drm_panic_is_pixel_fg (sbuf8, spitch, x, y)) +if (drm_panic_is_pixel_fg (sbuf8, spitch, x / scale, y / scale)) sb->set_pixel (sb, clip->x1 + x, clip->y1 + y, fg_color); }
@@ -310,18 +311,22 @@ static void drm_panic_blit_pixel(struct drm_scanout_buffer *sb, struct drm_rect* @clip: destination rectangle * @sbuf8: source buffer, in monochrome format, 8 pixels per byte. * @spitch: source pitch in bytes +* @scale: integer scale, source buffer is scale time smaller than destination +* rectangle * @fg_color: foreground color, in destination format * * This can be used to draw a font character, which is a monochrome image, to a * framebuffer in other supported format. */ static void drm_panic_blit (struct drm_scanout_buffer *sb,struct drm_rect *clip, -const u8 *sbuf8,unsigned int spitch,u32 fg_color) +const u8 *sbuf8,unsigned int spitch, +unsigned int scale,u32 fg_color) + {struct iosys_map map;if (sb->set_pixel) -return drm_panic_blit_pixel (sb, clip, sbuf8, spitch, fg_color); +return drm_panic_blit_pixel (sb, clip, sbuf8, spitch, scale, fg_color); map = sb->map[0 ];iosys_map_incr (&map, clip->y1 * sb->pitch[0 ] + clip->x1 * sb->format->cpp[0 ]);
@@ -329,15 +334,15 @@ static void drm_panic_blit(struct drm_scanout_buffer *sb, struct drm_rect *clip,switch (sb->format->cpp[0 ]) {case 2 :drm_panic_blit16 (&map, sb->pitch[0 ], sbuf8, spitch, -drm_rect_height (clip),drm_rect_width (clip), fg_color); +drm_rect_height (clip),drm_rect_width (clip), scale, fg_color);break ;case 3 :drm_panic_blit24 (&map, sb->pitch[0 ], sbuf8, spitch, -drm_rect_height (clip),drm_rect_width (clip), fg_color); +drm_rect_height (clip),drm_rect_width (clip), scale, fg_color);break ;case 4 :drm_panic_blit32 (&map, sb->pitch[0 ], sbuf8, spitch, -drm_rect_height (clip),drm_rect_width (clip), fg_color); +drm_rect_height (clip),drm_rect_width (clip), scale, fg_color);break ;default :WARN_ONCE (1 ,"Can't blit with pixel width %d\n" , sb->format->cpp[0 ]);
@@ -477,7 +482,7 @@ static void draw_txt_rectangle(struct drm_scanout_buffer *sb,for (j =0 ; j < line_len; j++) { src =get_char_bitmap (font, msg[i].txt[j], font_pitch); rec.x2 = rec.x1 + font->width; -drm_panic_blit (sb, &rec, src, font_pitch, color); +drm_panic_blit (sb, &rec, src, font_pitch,1 , color); rec.x1 += font->width; } }
Signature · 1 line
2.45.2
Jocelyn Falempe
maintainerPATCH 2/4 drm/rect: add drm_rect_overlap()
In reply to Jocelyn FalempeCheck if two rectangles overlap.
It's a bit similar to drm_rect_intersect() but this won't modify the rectangle.
Simplifies a bit drm_panic.
--- drivers/gpu/drm/drm_panic.c | 3 +-- include/drm/drm_rect.h | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-)
drivers/gpu/drm/drm_panic.c +1 -2
@@ -522,8 +522,7 @@ static void draw_panic_static_user(struct drm_scanout_buffer *sb)/* Fill with the background color, and draw text on top */ drm_panic_fill (sb, &r_screen, bg_color); -if ((r_msg.x1 >= logo_width || r_msg.y1 >= logo_height) && - logo_width <= sb->width && logo_height <= sb->height) { +if (!drm_rect_overlap (&r_logo, &r_msg)) {if (logo_mono)drm_panic_blit (sb, &r_logo, logo_mono->data,DIV_ROUND_UP (logo_width,8 ), fg_color);
include/drm/drm_rect.h +15 -0
@@ -238,6 +238,21 @@ static inline void drm_rect_fp_to_int(struct drm_rect *dst, drm_rect_height(src) >> 16); } +/** +* drm_rect_overlap - Check if two rectangles overlap +* @r1: first rectangle +* @r2: second rectangle +* +* RETURNS: +* %true if the rectangles overlap, %false otherwise. +*/ +static inline bool drm_rect_overlap (const struct drm_rect *r1, +const struct drm_rect *r2) +{ +return (r1->x2 > r2->x1 && r2->x2 > r1->x1 && + r1->y2 > r2->y1 && r2->y2 > r1->y1); +} +bool drm_rect_intersect (struct drm_rect *r,const struct drm_rect *clip);bool drm_rect_clip_scaled (struct drm_rect *src,struct drm_rect *dst,const struct drm_rect *clip);
Signature · 1 line
2.45.2
Jocelyn Falempe
maintainerPATCH 3/4 drm/panic: simplify logo handling
In reply to Jocelyn FalempeMove logo rectangle initialisation, and logo drawing in separate functions, so they can be re-used by different panic screens. It prepares the introduction of the QR-code panic screen.
--- drivers/gpu/drm/drm_panic.c | 57 +++++++++++++++++++++---------------- 1 file changed, 33 insertions(+), 24 deletions(-)
drivers/gpu/drm/drm_panic.c +33 -24
@@ -80,6 +80,7 @@ static struct drm_panic_line panic_msg[] = {PANIC_LINE ("" ), PANIC_LINE("Please reboot your computer."), }; +static const int panic_msg_lines =ARRAY_SIZE (panic_msg);static const struct drm_panic_line logo_ascii[] = { PANIC_LINE(" .--. _"),
@@ -90,6 +91,7 @@ static const struct drm_panic_line logo_ascii[] = {PANIC_LINE (" /'\\_ _/`\\ (_)" ), PANIC_LINE(" \\___)=(___/"), }; +static const int logo_ascii_lines =ARRAY_SIZE (logo_ascii);#if defined(CONFIG_LOGO ) && !defined(MODULE )static const struct linux_logo *logo_mono;
@@ -488,33 +490,45 @@ static void draw_txt_rectangle(struct drm_scanout_buffer *sb, } } +static void drm_panic_logo_rect (struct drm_rect *rect,const struct font_desc *font) +{ +if (logo_mono) +drm_rect_init (rect,0 ,0 , logo_mono->width, logo_mono->height); +else { +int logo_width =get_max_line_len (logo_ascii, logo_ascii_lines) * font->width; + +drm_rect_init (rect,0 ,0 , logo_width, logo_ascii_lines * font->height); + } +} + +static void drm_panic_logo_draw (struct drm_scanout_buffer *sb,struct drm_rect *rect, +const struct font_desc *font,u32 fg_color) +{ +if (logo_mono) +drm_panic_blit (sb, rect, logo_mono->data, +DIV_ROUND_UP (drm_rect_width (rect),8 ),1 , fg_color); +else +draw_txt_rectangle (sb, font, logo_ascii, logo_ascii_lines, false, rect, + fg_color); +} +static void draw_panic_static_user (struct drm_scanout_buffer *sb) { -size_t msg_lines =ARRAY_SIZE (panic_msg); -size_t logo_ascii_lines =ARRAY_SIZE (logo_ascii);u32 fg_color =convert_from_xrgb8888 (CONFIG_DRM_PANIC_FOREGROUND_COLOR , sb->format->format);u32 bg_color =convert_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 logo_width, logo_height; +unsigned int panic_msg_width;if (!font)return ; r_screen =DRM_RECT_INIT (0 ,0 , sb->width, sb->height); - -if (logo_mono) { - logo_width = logo_mono->width; - logo_height = logo_mono->height; - }else { - logo_width =get_max_line_len (logo_ascii, logo_ascii_lines) * font->width; - logo_height = logo_ascii_lines * font->height; - } - - r_logo =DRM_RECT_INIT (0 ,0 , logo_width, logo_height); +drm_panic_logo_rect (&r_logo, font); + panic_msg_width =get_max_line_len (panic_msg, panic_msg_lines) * font->width; r_msg =DRM_RECT_INIT (0 ,0 , -min (get_max_line_len (panic_msg, msg_lines) * font->width, sb->width), -min (msg_lines * font->height, sb->height)); +min (panic_msg_width, sb->width), +min (panic_msg_lines * font->height, sb->height));/* Center the panic message */ drm_rect_translate (&r_msg, (sb->width - r_msg.x2) /2 , (sb->height - r_msg.y2) /2 );
@@ -522,15 +536,10 @@ static void draw_panic_static_user(struct drm_scanout_buffer *sb)/* Fill with the background color, and draw text on top */ drm_panic_fill (sb, &r_screen, bg_color); -if (!drm_rect_overlap (&r_logo, &r_msg)) { -if (logo_mono) -drm_panic_blit (sb, &r_logo, logo_mono->data,DIV_ROUND_UP (logo_width,8 ), - fg_color); -else -draw_txt_rectangle (sb, font, logo_ascii, logo_ascii_lines, false, &r_logo, - fg_color); - } -draw_txt_rectangle (sb, font, panic_msg, msg_lines, true, &r_msg, fg_color); +if (!drm_rect_overlap (&r_logo, &r_msg)) +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); }/*
Signature · 1 line
2.45.2
Jocelyn Falempe
maintainerPATCH 4/4 drm/panic: Add a qr_code panic screen
In reply to Jocelyn FalempeThis patch adds a new panic screen, with a QR code and the kmsg data embedded.
If DRM_PANIC_SCREEN_QR_CODE_URL is set, then the kmsg data will be compressed with zlib and encoded as a numerical segment, and appended to the url as a url parameter. This allows to save space, and put about ~7500 bytes of kmsg data, in a V40 QR code.
Linux distributions can customize the url, and put a web frontend to directly open a bug report with the kmsg data.
Otherwise the kmsg data will be encoded as binary segment (ie raw ascii) and only a maximum of 2953 bytes of kmsg data will be available in the QR code.
You can also limit the QR code size with DRM_PANIC_SCREEN_QR_VERSION.
--- drivers/gpu/drm/Kconfig | 29 + drivers/gpu/drm/Makefile | 1 + drivers/gpu/drm/drm_drv.c | 3 + drivers/gpu/drm/drm_panic.c | 247 ++++++++ drivers/gpu/drm/drm_panic_qr.rs | 989 ++++++++++++++++++++++++++++++++ include/drm/drm_panic.h | 4 + 6 files changed, 1273 insertions(+) create mode 100644 drivers/gpu/drm/drm_panic_qr.rs
drivers/gpu/drm/Kconfig +29 -0
@@ -149,6 +149,35 @@ config DRM_PANIC_SCREEN or by writing to /sys/module/drm/parameters/panic_screen sysfs entry Default is "user" +config DRM_PANIC_SCREEN_QR_CODE + bool "Add a panic screen with a QR code" + depends on DRM_PANIC && RUST + help + This option adds a qr code generator, and a panic screen with a QR + code. The QR code will contain the last lines of kmsg and other debug + information. This should be easier for the user to report a kernel + panic, with all debug information available. + To use this panic screen, also set DRM_PANIC_SCREEN to "qr_code" + +config DRM_PANIC_SCREEN_QR_CODE_URL + string "Base url of the QR code in the panic screen" + depends on DRM_PANIC_SCREEN_QR_CODE + help + This option sets the base url to report the kernel panic. If it's set + the qr code will contain the url and the kmsg compressed with zlib as + url parameter. If it's empty, the qr code will contain the kmsg as + uncompressed text only. + +config DRM_PANIC_SCREEN_QR_VERSION + int "Maximum version (size) of the QR code." + depends on DRM_PANIC_SCREEN_QR_CODE + default 40 + help + This option limits the version (or size) of the QR code. QR code + version range from Version 1 (21x21) to Version 40 (177x177). + Smaller QR code are easier to read, but will contain less debugging + data. Default is 40. + 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 +1 -0
@@ -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_PANIC_SCREEN_QR_CODE) += drm_panic_qr.o obj-$(CONFIG_DRM) += drm.o obj-$(CONFIG_DRM_PANEL_ORIENTATION_QUIRKS) += drm_panel_orientation_quirks.o
drivers/gpu/drm/drm_drv.c +3 -0
@@ -1067,6 +1067,7 @@ static const struct file_operations drm_stub_fops = {static void drm_core_exit (void ) {drm_privacy_screen_lookup_exit (); +drm_panic_exit ();accel_core_exit ();unregister_chrdev (DRM_MAJOR ,"drm" );debugfs_remove (drm_debugfs_root);
@@ -1099,6 +1100,8 @@ static int __init drm_core_init(void)if (ret <0 ) goto error; +drm_panic_init (); +drm_privacy_screen_lookup_init (); drm_core_init_complete = true;
drivers/gpu/drm/drm_panic.c +247 -0
274 lines not on this page · raw message
drivers/gpu/drm/drm_panic_qr.rs new file +989 -0
989 lines not on this page · raw message
include/drm/drm_panic.h +4 -0
@@ -150,11 +150,15 @@ struct drm_scanout_buffer {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 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
Signature · 1 line
2.45.2
Alice Ryhl
reviewerQuoted 9 lines · Jocelyn Falempe
This patch adds a new panic screen, with a QR code and the kmsg data embedded.
IfDRM_PANIC_SCREEN_QR_CODE_URLis set, then the kmsg data will be compressed with zlib and encoded as a numerical segment, and appended to the url as a url parameter. This allows to save space, and put about ~7500 bytes of kmsg data, in a V40 QR code.
Linux distributions can customize the url, and put a web frontend to directly open a bug report with the kmsg data.
Quoted 7 lines
Otherwise the kmsg data will be encoded as binary segment (ie raw ascii) and only a maximum of 2953 bytes of kmsg data will be available in the QR code.
You can also limit the QR code size with
DRM_PANIC_SCREEN_QR_VERSION.Signed-off-by Jocelyn Falempe <[email protected]>
This is pretty cool! The Rust code looks reasonable, and it's really nice that you've isolated all of the unsafe code to a single place. That makes it much easier to review.
I have a few comments on Rust style below:
Quoted 19 lines
drivers/gpu/drm/drm_panic_qr.rs new file +13
-0@@ -0,0 +1,989 @@ +// SPDX-License-Identifier: MIT + +//! This is a simple qr encoder for DRM panic +//! Due to the Panic constraint, it doesn't allocate memory and does all the work +//! on the stack or on the provided buffers. +//! For simplification, it only supports Low error correction, and apply the +//! first mask (checkboard). It will draw the smallest QRcode that can contain +//! the string passed as parameter. +//! To get the most compact QR-code, the start of the url is encoded as binary, +//! and the compressed kmsg is encoded as numeric. +//! The binary data must be a valid url parameter, so the easiest way is to use +//! base64 encoding. But this waste 25% of data space, so the whole stack trace +//! won't fit in the QR-Code. So instead it encodes every 13bits of input into
drivers/gpu/drm/drm_panic_qr.rs line 23
Quoted 10 lines
drivers/gpu/drm/drm_panic_qr.rs new file +10
-0+//! 4 decimal digits, and then use the efficient numeric encoding, that encode 3 +//! decimal digits into 10bits. This makes 39bits of compressed data into 12 +//! decimal digits, into 40bits in the QR-Code, so wasting only 2.5%. +//! And numbers are valid url parameter, so the website can do the reverse, to +//! get the binary data. +//! +//! Inspired by this 3 projects, all under MIT license: +//! https://github.com/kennytm/qrcode-rust +//! https://github.com/erwanvivien/fast_qr +//! https://github.com/bjguillot/qr
Generally, documentation under //! or /// comments should be written using markdown. In markdown, line breaks are ignored and do not actually show up as a line break in the rendered documentation. If you want an actual line break, then you need an empty line.
I would format it like this:
//! This is a simple qr encoder for DRM panic.
//!
//! Due to the Panic constraint, it doesn't allocate memory and does all //! the work on the stack or on the provided buffers. For //! simplification, it only supports Low error correction, and apply the //! first mask (checkboard). It will draw the smallest QRcode that can //! contain the string passed as parameter. To get the most compact //! QR-code, the start of the url is encoded as binary, and the //! compressed kmsg is encoded as numeric.
//!
//! The binary data must be a valid url parameter, so the easiest way is //! to use base64 encoding. But this waste 25% of data space, so the //! whole stack trace won't fit in the QR-Code. So instead it encodes //! every 13bits of input into 4 decimal digits, and then use the //! efficient numeric encoding, that encode 3 decimal digits into //! 10bits. This makes 39bits of compressed data into 12 decimal digits, //! into 40bits in the QR-Code, so wasting only 2.5%. And numbers are //! valid url parameter, so the website can do the reverse, to get the //! binary data.
//!
//! Inspired by this 3 projects, all under MIT license:
//!
//! * https://github.com/kennytm/qrcode-rust
//! * https://github.com/erwanvivien/fast_qr
//! * https://github.com/bjguillot/qr
Quoted 7 lines
drivers/gpu/drm/drm_panic_qr.rs new file +7
-0+/// drm_panic_qr_generate() +/// +/// C entry point for the rust QR Code generator +/// +/// Write the QR code image in the data buffer, and return the qrcode size, or 0 +/// if the data doesn't fit in a QR code. +///
drivers/gpu/drm/drm_panic_qr.rs line 922
Quoted 10 lines
drivers/gpu/drm/drm_panic_qr.rs new file +10
-0+/// url: the base url of the QR code. will be encoded as Binary segment. +/// data: pointer to the binary data, to be encoded. if url is NULL, it will be +/// encoded as Binary segment. otherwise it will be encoded efficiently as +/// Numeric segment, and appendended to the url. +/// data_len: length of the data, that needs to be encoded. +/// data_size: size of data buffer, it should be at least 4071 bytes to hold a +/// V40 QR-code. it will then be overwritten with the QR-code image. +/// tmp: a temporary buffer that the QR-code encoder will use, to write the +/// segments data and ECC. +/// tmp_size: size of the tmp buffer, it must be at least 3706 bytes long for V40
The same applies here. When rendered using markdown, the above will be rendered like this:
url: the base url of the QR code. will be encoded as Binary segment.
data: pointer to the binary data, to be encoded. if url is NULL, it will be encoded as Binary segment. otherwise it will be encoded efficiently as Numeric segment, and appendended to the url. data_len: length of the data, that needs to be encoded. data_size: size of data buffer, it should be at least 4071 bytes to hold a V40 QR-code. it will then be overwritten with the QR-code image. tmp: a temporary buffer that the QR-code encoder will use, to write the segments data and ECC. tmp_size:
size of the tmp buffer, it must be at least 3706 bytes long for V40
I don't think that's what you wanted.
Quoted 27 lines
drivers/gpu/drm/drm_panic_qr.rs new file +27
-0+#[no_mangle] +pub extern "C" fn drm_panic_qr_generate ( + url: *const i8 , + data: *mut u8 , + data_len:usize , + data_size:usize , + tmp: *mut u8 , + tmp_size:usize , +) ->u8 { +if data_size <=4071 || tmp_size <=3706 { +return 0 ; + } +let data_slice: &mut [u8 ] =unsafe { core::slice::from_raw_parts_mut (data, data_size) }; +let tmp_slice: &mut [u8 ] =unsafe { core::slice::from_raw_parts_mut (tmp, tmp_size) }; +if url.is_null () { +match EncodedMsg ::new (&[&Segment ::Binary (&data_slice[0 ..data_len])], tmp_slice) { +None =>0 , +Some (em) => { +let qr_image =QrImage ::new (&em, data_slice); + qr_image.width + } + } + }else { +let url_str: &str =unsafe {CStr ::from_char_ptr (url).as_str_unchecked () }; +let segments = &[ + &Segment ::Binary (url_str.as_bytes ()), + &Segment::Numeric(&data_slice[0..data_len]),
drivers/gpu/drm/drm_panic_qr.rs line 959
Quoted 10 lines
drivers/gpu/drm/drm_panic_qr.rs new file +10
-0+ ]; +match EncodedMsg ::new (segments, tmp_slice) { +None =>0 , +Some (em) => { +let qr_image =QrImage ::new (&em, data_slice); + qr_image.width + } + } + } +}
It's very nice that you've isolated all of the unsafe code to this function. That makes it very easy to review.
A few comments:
First, all unsafe blocks must be annotated with a safety comment. For example:
// SAFETY: The caller ensures that data is valid for data_size
// bytes, and that it is valid for writing.
let data_slice: &mut [u8] = unsafe { core::slice::from_raw_parts_mut(data, data_size) };
Unsafe functions are those that can trigger memory safety problems if you call them incorrectly, and you must annotate calls with a safety comment that explains why this call cannot result in memory safety issues. In this case, you can just say that the caller promises to pass you reasonable arguments.
The next unsafe block is a bit more interesting.
drivers/gpu/drm/drm_panic_qr.rs line 946
Quoted 1 line
drivers/gpu/drm/drm_panic_qr.rs new file +1
-0+let url_str: &str =unsafe {CStr ::from_char_ptr (url).as_str_unchecked () };
Here, you call two unsafe functions:
* CStr::from_char_ptr
* CStr::as_str_unchecked
If you read the documentation, you will find these safety requirements:
/// # Safety
///
/// ptr must be a valid pointer to a NUL-terminated C string, and it must /// last at least 'a. When CStr is alive, the memory pointed by ptr /// must not be mutated.
/// # Safety
///
/// The contents must be valid UTF-8.
Your unsafe block *must* have a safety comment that explains why the above things are satisfied. The requirements of from_char_ptr are okay, but is it really the case that url is necessarily valid UTF-8?
You never actually use the fact that it's UTF-8 anywhere, so you can just not call as_str_unchecked. The CStr type also has an as_bytes method, so there's no reason to go through the &str type.
Finally, drm_panic_qr_generate should really be marked unsafe. By not marking it unsafe, you are saying that no matter what the arguments are, calling the function will not result in memory safety problems. That's not really true. If I call it with data being a null pointer, you're going to have a bad time.
You probably want something along these lines:
/// # Safety
///
/// * url must be null or point at a nul-terminated string. /// * data must be valid for reading and writing for data_size bytes. /// * tmp must be valid for reading and writing for tmp_size bytes. #[no_mangle]
pub unsafe extern "C" fn drm_panic_qr_generate(
As long as the above requirements are satisfied, calling drm_panic_qr_generate should never cause memory unsafety, so this is an appropriate list of safety requirements.
(You also require that data_len <= data_size, but if this is violated you get a kernel panic which isn't a memory safety problem, so it does not need to be listed in the safety requirements.)
Alice
Quoted 7 lines · Alice Ryhl
As long as the above requirements are satisfied, calling
drm_panic_qr_generateshould never cause memory unsafety, so this is an appropriate list of safety requirements.(You also require that
data_len <= data_size, but if this is violated you get a kernel panic which isn't a memory safety problem, so it does not need to be listed in the safety requirements.)
Panicing while trying to print out a panic message is not a good idea, so enforcing this would be nice :)
thanks,
greg k-h
Alice Ryhl
reviewerQuoted 6 lines · Greg KH
Quoted 4 lines · Alice Ryhl
As long as the above requirements are satisfied, calling
drm_panic_qr_generateshould never cause memory unsafety, so this is an appropriate list of safety requirements.
Quoted 6 lines
Quoted 3 lines
(You also require that
data_len <= data_size, but if this is violated you get a kernel panic which isn't a memory safety problem, so it does not need to be listed in the safety requirements.)Panicing while trying to print out a panic message is not a good idea, so enforcing this would be nice :)
I guess it would make sense to have an if that verifies that data_len <= data_size at the top of drm_panic_qr_generate.
Alice
Jocelyn Falempe
maintainerQuoted 50 lines · Alice Ryhl
Quoted 9 lines · Jocelyn Falempe
This patch adds a new panic screen, with a QR code and the kmsg data embedded.
IfDRM_PANIC_SCREEN_QR_CODE_URLis set, then the kmsg data will be compressed with zlib and encoded as a numerical segment, and appended to the url as a url parameter. This allows to save space, and put about ~7500 bytes of kmsg data, in a V40 QR code.
Linux distributions can customize the url, and put a web frontend to directly open a bug report with the kmsg data.Quoted 7 lines
Otherwise the kmsg data will be encoded as binary segment (ie raw ascii) and only a maximum of 2953 bytes of kmsg data will be available in the QR code.
You can also limit the QR code size with
DRM_PANIC_SCREEN_QR_VERSION.Signed-off-by Jocelyn Falempe <[email protected]>This is pretty cool! The Rust code looks reasonable, and it's really nice that you've isolated all of the unsafe code to a single place. That makes it much easier to review.
I have a few comments on Rust style below:
Quoted 26 lines
drivers/gpu/drm/drm_panic_qr.rs new file +20
-0@@ -0,0 +1,989 @@ +// SPDX-License-Identifier: MIT + +//! This is a simple qr encoder for DRM panic +//! Due to the Panic constraint, it doesn't allocate memory and does all the work +//! on the stack or on the provided buffers. +//! For simplification, it only supports Low error correction, and apply the +//! first mask (checkboard). It will draw the smallest QRcode that can contain +//! the string passed as parameter. +//! To get the most compact QR-code, the start of the url is encoded as binary, +//! and the compressed kmsg is encoded as numeric. +//! The binary data must be a valid url parameter, so the easiest way is to use +//! base64 encoding. But this waste 25% of data space, so the whole stack trace +//! won't fit in the QR-Code. So instead it encodes every 13bits of input into +//! 4 decimal digits, and then use the efficient numeric encoding, that encode 3 +//! decimal digits into 10bits. This makes 39bits of compressed data into 12 +//! decimal digits, into 40bits in the QR-Code, so wasting only 2.5%. +//! And numbers are valid url parameter, so the website can do the reverse, to +//! get the binary data. +//! +//! Inspired by this 3 projects, all under MIT license:
Quoted 8 lines
drivers/gpu/drm/drm_panic_qr.rs line 23
Quoted 3 lines
drivers/gpu/drm/drm_panic_qr.rs new file +3
-0+//! https://github.com/kennytm/qrcode-rust +//! https://github.com/erwanvivien/fast_qr +//! https://github.com/bjguillot/qr Generally, documentation under //! or /// comments should be written using markdown. In markdown, line breaks are ignored and do not actually show up as a line break in the rendered documentation. If you want an actual line break, then you need an empty line.
Thanks, I didn't know about this. I'm now playing with rustdoc, and the output is really not what I expected. I will fix the rust comments in v2
Quoted 40 lines
I would format it like this:
//! This is a simple qr encoder for DRM panic.
//!
//! Due to the Panic constraint, it doesn't allocate memory and does all //! the work on the stack or on the provided buffers. For //! simplification, it only supports Low error correction, and apply the //! first mask (checkboard). It will draw the smallest QRcode that can //! contain the string passed as parameter. To get the most compact //! QR-code, the start of the url is encoded as binary, and the //! compressed kmsg is encoded as numeric.
//!
//! The binary data must be a valid url parameter, so the easiest way is //! to use base64 encoding. But this waste 25% of data space, so the //! whole stack trace won't fit in the QR-Code. So instead it encodes //! every 13bits of input into 4 decimal digits, and then use the //! efficient numeric encoding, that encode 3 decimal digits into //! 10bits. This makes 39bits of compressed data into 12 decimal digits, //! into 40bits in the QR-Code, so wasting only 2.5%. And numbers are //! valid url parameter, so the website can do the reverse, to get the //! binary data.
//!
//! Inspired by this 3 projects, all under MIT license:
//!
//! * https://github.com/kennytm/qrcode-rust
//! * https://github.com/erwanvivien/fast_qr
//! * https://github.com/bjguillot/qrQuoted 12 lines
drivers/gpu/drm/drm_panic_qr.rs new file +12
-0+/// drm_panic_qr_generate() +/// +/// C entry point for the rust QR Code generator +/// +/// Write the QR code image in the data buffer, and return the qrcode size, or 0 +/// if the data doesn't fit in a QR code. +/// +/// url: the base url of the QR code. will be encoded as Binary segment. +/// data: pointer to the binary data, to be encoded. if url is NULL, it will be +/// encoded as Binary segment. otherwise it will be encoded efficiently as +/// Numeric segment, and appendended to the url. +/// data_len: length of the data, that needs to be encoded.
Quoted 8 lines
drivers/gpu/drm/drm_panic_qr.rs line 922
Quoted 5 lines
drivers/gpu/drm/drm_panic_qr.rs new file +5
-0+/// data_size: size of data buffer, it should be at least 4071 bytes to hold a +/// V40 QR-code. it will then be overwritten with the QR-code image. +/// tmp: a temporary buffer that the QR-code encoder will use, to write the +/// segments data and ECC. +/// tmp_size: size of the tmp buffer, it must be at least 3706 bytes long for V40 The same applies here. When rendered using markdown, the above will be rendered like this:
Yes I will fix that too.
Quoted 63 lines
url: the base url of the QR code. will be encoded as Binary segment.
data: pointer to the binary data, to be encoded. if url is NULL, it will be encoded as Binary segment. otherwise it will be encoded efficiently as Numeric segment, and appendended to the url.data_len: length of the data, that needs to be encoded.data_size: size of data buffer, it should be at least 4071 bytes to hold a V40 QR-code. it will then be overwritten with the QR-code image. tmp: a temporary buffer that the QR-code encoder will use, to write the segments data and ECC.tmp_size:
size of the tmp buffer, it must be at least 3706 bytes long for V40I don't think that's what you wanted.
Quoted 27 lines
drivers/gpu/drm/drm_panic_qr.rs new file +27
-0+#[no_mangle] +pub extern "C" fn drm_panic_qr_generate ( + url: *const i8 , + data: *mut u8 , + data_len:usize , + data_size:usize , + tmp: *mut u8 , + tmp_size:usize , +) ->u8 { +if data_size <=4071 || tmp_size <=3706 { +return 0 ; + } +let data_slice: &mut [u8 ] =unsafe { core::slice::from_raw_parts_mut (data, data_size) }; +let tmp_slice: &mut [u8 ] =unsafe { core::slice::from_raw_parts_mut (tmp, tmp_size) }; +if url.is_null () { +match EncodedMsg ::new (&[&Segment ::Binary (&data_slice[0 ..data_len])], tmp_slice) { +None =>0 , +Some (em) => { +let qr_image =QrImage ::new (&em, data_slice); + qr_image.width + } + } + }else { +let url_str: &str =unsafe {CStr ::from_char_ptr (url).as_str_unchecked () }; +let segments = &[ + &Segment ::Binary (url_str.as_bytes ()), + &Segment::Numeric(&data_slice[0..data_len]),drivers/gpu/drm/drm_panic_qr.rs line 959
Quoted 10 lines
drivers/gpu/drm/drm_panic_qr.rs new file +10
-0+ ]; +match EncodedMsg ::new (segments, tmp_slice) { +None =>0 , +Some (em) => { +let qr_image =QrImage ::new (&em, data_slice); + qr_image.width + } + } + } +}It's very nice that you've isolated all of the unsafe code to this function. That makes it very easy to review.
A few comments:
First, all unsafe blocks must be annotated with a safety comment. For example:
// SAFETY: The caller ensures that
datais valid fordata_size
// bytes, and that it is valid for writing.
letdata_slice: &mut [u8] = unsafe {core::slice::from_raw_parts_mut(data, data_size)};
Quoted 5 lines
Unsafe functions are those that can trigger memory safety problems if you call them incorrectly, and you must annotate calls with a safety comment that explains why this call cannot result in memory safety issues. In this case, you can just say that the caller promises to pass you reasonable arguments.
Yes, all these buffers come from the C side, so you need to trust the caller. I will add these safety comments.
Quoted 23 lines
The next unsafe block is a bit more interesting.
drivers/gpu/drm/drm_panic_qr.rs line 946
Quoted 1 line
drivers/gpu/drm/drm_panic_qr.rs new file +1
-0+let url_str: &str =unsafe {CStr ::from_char_ptr (url).as_str_unchecked () };Here, you call two unsafe functions:
*
CStr::from_char_ptr
*CStr::as_str_uncheckedIf you read the documentation, you will find these safety requirements:
/// # Safety
///
///ptrmust be a valid pointer to aNUL-terminated C string, and it must /// last at least'a. WhenCStris alive, the memory pointed byptr/// must not be mutated./// # Safety
///
/// The contents must be valid UTF-8.
Quoted 7 lines
Your unsafe block *must* have a safety comment that explains why the above things are satisfied. The requirements of
from_char_ptrare okay, but is it really the case thaturlis necessarily valid UTF-8?You never actually use the fact that it's UTF-8 anywhere, so you can just not call
as_str_unchecked. TheCStrtype also has anas_bytesmethod, so there's no reason to go through the&strtype.
Yes, I can use directly the CStr, so one less unsafe call :)
Quoted 6 lines
Finally,
drm_panic_qr_generateshould really be marked unsafe. By not marking it unsafe, you are saying that no matter what the arguments are, calling the function will not result in memory safety problems. That's not really true. If I call it withdatabeing a null pointer, you're going to have a bad time.
Sure, we can't guarantee that the pointers are valid, when they come from the C side. So I agree this should be unsafe.
Quoted 11 lines
You probably want something along these lines:
/// # Safety
///
/// *urlmust be null or point at a nul-terminated string. /// *datamust be valid for reading and writing fordata_sizebytes. /// *tmpmust be valid for reading and writing fortmp_sizebytes. #[no_mangle]
pub unsafe extern "C" fn drm_panic_qr_generate(
Quoted 3 lines
As long as the above requirements are satisfied, calling
drm_panic_qr_generateshould never cause memory unsafety, so this is an appropriate list of safety requirements.
Ok, I will add this in v2
Quoted 4 lines
(You also require that
data_len <= data_size, but if this is violated you get a kernel panic which isn't a memory safety problem, so it does not need to be listed in the safety requirements.)
Sure, I will add this check too.
Quoted 3 lines
Alice
Thanks a lot for this detailed review, that's really helpful.
Signature · 1 line
Jocelyn
Quoted 11 lines · Jocelyn Falempe
This series adds a new panic screen, with the kmsg data embedded in a QR-code.
The main advantage of QR-code, is that you can copy/paste the debug data to a bug report.
The QR-code encoder is written in rust, and is very specific to
drm_panic.
The reason is that it is called in a panic handler, and thus can't allocate memory, or use locking. The rust code uses a few rust core API, and provides only two C entry points.
There is no particular reason to do it in rust, I just wanted to learn rust, and see if it can work in the kernel.If you want to see what it looks like, I've put a few screenshots here:
https://github.com/kdj0c/panic_report/issues/1
Cool, this idea has been floating around for decades, here's the last attempt at this back in 2014:
https://lore.kernel.org/all/[email protected]/
Quoted 17 lines
Signature · 16 lines
-- Jocelyn Jocelyn Falempe (4): drm/panic: Add integer scaling to blit() drm/rect: add drm_rect_overlap() drm/panic: simplify logo handling drm/panic: Add a qr_code panic screen drivers/gpu/drm/Kconfig | 29 + drivers/gpu/drm/Makefile | 1 + drivers/gpu/drm/drm_drv.c | 3 + drivers/gpu/drm/drm_panic.c | 338 +++++++++-- drivers/gpu/drm/drm_panic_qr.rs | 989 ++++++++++++++++++++++++++++++++
Wait, we can put .rs files in any directory now? I didn't think that worked properly yet.
thanks,
greg k-h
Quoted 3 lines · Greg KH
Wait, we can put .rs files in any directory now? I didn't think that worked properly yet.
We can put leaves of the crate graph (e.g. in-tree we have the samples and the PHY driver), but not the rest of the graph. The former fits just fine in Kbuild (which is why it was good enough for what we did so far), the other part is what requires a rework of how things will work.
Cheers,
Miguel
Jocelyn Falempe
maintainerQuoted 10 lines · Greg KH
Quoted 9 lines · Jocelyn Falempe
This series adds a new panic screen, with the kmsg data embedded in a QR-code.
The main advantage of QR-code, is that you can copy/paste the debug data to a bug report.
The QR-code encoder is written in rust, and is very specific to
drm_panic.
The reason is that it is called in a panic handler, and thus can't allocate memory, or use locking. The rust code uses a few rust core API, and provides only two C entry points.
There is no particular reason to do it in rust, I just wanted to learn rust, and see if it can work in the kernel.
Quoted 7 lines
Quoted 2 lines
If you want to see what it looks like, I've put a few screenshots here:
https://github.com/kdj0c/panic_report/issues/1Cool, this idea has been floating around for decades, here's the last attempt at this back in 2014:
https://lore.kernel.org/all/[email protected]/
Yes, I've looked at this, but drawing a QR-code in the middle of fbcon was a bit complex. Now with drm panic, there is a proper infrastructure to display the QR code, and make sure it won't be overwritten.
Quoted 12 lines
Quoted 12 lines
Signature · 10 lines
-- Jocelyn Jocelyn Falempe (4): drm/panic: Add integer scaling to blit() drm/rect: add drm_rect_overlap() drm/panic: simplify logo handling drm/panic: Add a qr_code panic screen
Quoted 8 lines
Quoted 5 lines
drivers/gpu/drm/Kconfig | 29 + drivers/gpu/drm/Makefile | 1 + drivers/gpu/drm/drm_drv.c | 3 + drivers/gpu/drm/drm_panic.c | 338 +++++++++-- drivers/gpu/drm/drm_panic_qr.rs | 989 ++++++++++++++++++++++++++++++++Wait, we can put .rs files in any directory now? I didn't think that worked properly yet.
Yes, thanks to the work of the rust-for-linux team. When I started to work on this, I needed a few workarounds to make my rust code compile. But since 6.10-rc1, I was able to drop them all. Also for this simple case, I don't use bindgen, because there are only 2 Rust functions, that I need to call from C.
Quoted 5 lines
thanks,
greg k-h
Signature · 1 line
Jocelyn
Alice Ryhl
reviewerQuoted 8 lines · Greg KH
Quoted 6 lines · Jocelyn Falempe
Jocelyn Falempe (4):
drm/panic: Add integer scaling toblit()
drm/rect: adddrm_rect_overlap()
drm/panic: simplify logo handling
drm/panic: Add aqr_codepanic screen
Quoted 8 lines
Quoted 5 lines
drivers/gpu/drm/Kconfig | 29 + drivers/gpu/drm/Makefile | 1 + drivers/gpu/drm/drm_drv.c | 3 + drivers/gpu/drm/drm_panic.c | 338 +++++++++-- drivers/gpu/drm/drm_panic_qr.rs | 989 ++++++++++++++++++++++++++++++++Wait, we can put .rs files in any directory now? I didn't think that worked properly yet.
Yes, but Rust code outside of rust/ cannot expose a Rust API that Rust code elsewhere can use. Only C apis can be exposed.
Alice
Alice Ryhl
reviewerQuoted 35 lines · Jocelyn Falempe
This series adds a new panic screen, with the kmsg data embedded in a QR-code.
The main advantage of QR-code, is that you can copy/paste the debug data to a bug report.
The QR-code encoder is written in rust, and is very specific to
drm_panic.
The reason is that it is called in a panic handler, and thus can't allocate memory, or use locking. The rust code uses a few rust core API, and provides only two C entry points.
There is no particular reason to do it in rust, I just wanted to learn rust, and see if it can work in the kernel.If you want to see what it looks like, I've put a few screenshots here:
https://github.com/kdj0c/panic_report/issues/1--
Jocelyn
Jocelyn Falempe (4):
drm/panic: Add integer scaling toblit()
drm/rect: adddrm_rect_overlap()
drm/panic: simplify logo handling
drm/panic: Add aqr_codepanic screendrivers/gpu/drm/Kconfig | 29 + drivers/gpu/drm/Makefile | 1 + drivers/gpu/drm/drm_drv.c | 3 + drivers/gpu/drm/drm_panic.c | 338 +++++++++-- drivers/gpu/drm/drm_panic_qr.rs | 989 ++++++++++++++++++++++++++++++++ include/drm/drm_panic.h | 4 + include/drm/drm_rect.h | 15 + 7 files changed, 1340 insertions(+), 39 deletions(-) create mode 100644 drivers/gpu/drm/drm_panic_qr.rs
Quoted 1 line
base-commit:
3f5ea7ed705e8effe9cfabf912e769ccb6b7d389
I tried to apply this patch, but I can't find this commit, nor could I find any tag that it applies on.
Alice
Jocelyn Falempe
maintainerQuoted 36 lines · Alice Ryhl
Quoted 35 lines · Jocelyn Falempe
This series adds a new panic screen, with the kmsg data embedded in a QR-code.
The main advantage of QR-code, is that you can copy/paste the debug data to a bug report.
The QR-code encoder is written in rust, and is very specific to
drm_panic.
The reason is that it is called in a panic handler, and thus can't allocate memory, or use locking. The rust code uses a few rust core API, and provides only two C entry points.
There is no particular reason to do it in rust, I just wanted to learn rust, and see if it can work in the kernel.If you want to see what it looks like, I've put a few screenshots here:
https://github.com/kdj0c/panic_report/issues/1--
Jocelyn
Jocelyn Falempe (4):
drm/panic: Add integer scaling toblit()
drm/rect: adddrm_rect_overlap()
drm/panic: simplify logo handling
drm/panic: Add aqr_codepanic screendrivers/gpu/drm/Kconfig | 29 + drivers/gpu/drm/Makefile | 1 + drivers/gpu/drm/drm_drv.c | 3 + drivers/gpu/drm/drm_panic.c | 338 +++++++++-- drivers/gpu/drm/drm_panic_qr.rs | 989 ++++++++++++++++++++++++++++++++ include/drm/drm_panic.h | 4 + include/drm/drm_rect.h | 15 + 7 files changed, 1340 insertions(+), 39 deletions(-)create mode 100644
drivers/gpu/drm/drm_panic_qr.rs
Quoted 4 lines
Quoted 1 line
base-commit:
3f5ea7ed705e8effe9cfabf912e769ccb6b7d389I tried to apply this patch, but I can't find this commit, nor could I find any tag that it applies on.
Sorry, it's the drm-misc-next branch of [email protected]:drm/misc/kernel.git
Quoted 3 lines
Alice