Skip to content

Commit fc2f191

Browse files
Darksonnojeda
authored andcommitted
panic_qr: use new #[export] macro
This validates at compile time that the signatures match what is in the header file. It highlights one annoyance with the compile-time check, which is that it can only be used with functions marked unsafe. If the function is not unsafe, then this error is emitted: error[E0308]: `if` and `else` have incompatible types --> <linux>/drivers/gpu/drm/drm_panic_qr.rs:987:19 | 986 | #[export] | --------- expected because of this 987 | pub extern "C" fn drm_panic_qr_max_data_size(version: u8, url_len: usize) -> usize { | ^^^^^^^^^^^^^^^^^^^^^^^^^^ expected unsafe fn, found safe fn | = note: expected fn item `unsafe extern "C" fn(_, _) -> _ {kernel::bindings::drm_panic_qr_max_data_size}` found fn item `extern "C" fn(_, _) -> _ {drm_panic_qr_max_data_size}` The signature declarations are moved to a header file so it can be included in the Rust bindings helper, and the extern keyword is removed as it is unnecessary. Reviewed-by: Andreas Hindborg <[email protected]> Reviewed-by: Tamir Duberstein <[email protected]> Acked-by: Simona Vetter <[email protected]> Acked-by: Greg Kroah-Hartman <[email protected]> Signed-off-by: Alice Ryhl <[email protected]> Reviewed-by: Jocelyn Falempe <[email protected]> Link: https://lore.kernel.org/r/[email protected] [ Fixed `rustfmt`. Moved on top the unsafe requirement comment to follow the usual style, and slightly reworded it for clarity. Formatted bindings helper comment. - Miguel ] Signed-off-by: Miguel Ojeda <[email protected]>
1 parent 92d2873 commit fc2f191

File tree

4 files changed

+21
-9
lines changed

4 files changed

+21
-9
lines changed

drivers/gpu/drm/drm_panic.c

-5
Original file line numberDiff line numberDiff line change
@@ -486,11 +486,6 @@ static void drm_panic_qr_exit(void)
486486
stream.workspace = NULL;
487487
}
488488

489-
extern size_t drm_panic_qr_max_data_size(u8 version, size_t url_len);
490-
491-
extern u8 drm_panic_qr_generate(const char *url, u8 *data, size_t data_len, size_t data_size,
492-
u8 *tmp, size_t tmp_size);
493-
494489
static int drm_panic_get_qr_code_url(u8 **qr_image)
495490
{
496491
struct kmsg_dump_iter iter;

drivers/gpu/drm/drm_panic_qr.rs

+9-4
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
//! * <https://github.com/bjguillot/qr>
2828
2929
use core::cmp;
30-
use kernel::str::CStr;
30+
use kernel::{prelude::*, str::CStr};
3131

3232
#[derive(Debug, Clone, Copy, PartialEq, Eq, Ord, PartialOrd)]
3333
struct Version(usize);
@@ -929,7 +929,7 @@ impl QrImage<'_> {
929929
/// * `tmp` must be valid for reading and writing for `tmp_size` bytes.
930930
///
931931
/// They must remain valid for the duration of the function call.
932-
#[no_mangle]
932+
#[export]
933933
pub unsafe extern "C" fn drm_panic_qr_generate(
934934
url: *const kernel::ffi::c_char,
935935
data: *mut u8,
@@ -980,8 +980,13 @@ pub unsafe extern "C" fn drm_panic_qr_generate(
980980
/// * If `url_len` > 0, remove the 2 segments header/length and also count the
981981
/// conversion to numeric segments.
982982
/// * If `url_len` = 0, only removes 3 bytes for 1 binary segment.
983-
#[no_mangle]
984-
pub extern "C" fn drm_panic_qr_max_data_size(version: u8, url_len: usize) -> usize {
983+
///
984+
/// # Safety
985+
///
986+
/// Always safe to call.
987+
// Required to be unsafe due to the `#[export]` annotation.
988+
#[export]
989+
pub unsafe extern "C" fn drm_panic_qr_max_data_size(version: u8, url_len: usize) -> usize {
985990
#[expect(clippy::manual_range_contains)]
986991
if version < 1 || version > 40 {
987992
return 0;

include/drm/drm_panic.h

+7
Original file line numberDiff line numberDiff line change
@@ -163,4 +163,11 @@ static inline void drm_panic_unlock(struct drm_device *dev, unsigned long flags)
163163

164164
#endif
165165

166+
#if defined(CONFIG_DRM_PANIC_SCREEN_QR_CODE)
167+
size_t drm_panic_qr_max_data_size(u8 version, size_t url_len);
168+
169+
u8 drm_panic_qr_generate(const char *url, u8 *data, size_t data_len, size_t data_size,
170+
u8 *tmp, size_t tmp_size);
171+
#endif
172+
166173
#endif /* __DRM_PANIC_H__ */

rust/bindings/bindings_helper.h

+5
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@
3737
#include <linux/workqueue.h>
3838
#include <trace/events/rust_sample.h>
3939

40+
#if defined(CONFIG_DRM_PANIC_SCREEN_QR_CODE)
41+
// Used by `#[export]` in `drivers/gpu/drm/drm_panic_qr.rs`.
42+
#include <drm/drm_panic.h>
43+
#endif
44+
4045
/* `bindgen` gets confused at certain things. */
4146
const size_t RUST_CONST_HELPER_ARCH_SLAB_MINALIGN = ARCH_SLAB_MINALIGN;
4247
const size_t RUST_CONST_HELPER_PAGE_SIZE = PAGE_SIZE;

0 commit comments

Comments
 (0)