rust: add support for dynamic debug
Subsystems (3)
v2 cover rust: add support for dynamic debug
This patch series adds support for dynamic debug to the rust pr_debug! and dev_dbg! macros.
I have tested it and it and it does work but, there are a few differences between this and the c version (hence why this is a rfc).
rust does not have an equivalent to the C __func__ macro. so, for the time being this hard codes the function name to be the name of the macro e.g. "pr_debug!" and "dev_dbg!".
This uses the module_path! macro to get the module name, which will not be exactly the same as the c version if a kernel module defines a rust mod. e.g. it might be "kernel_module_name::rust_module_name".
this does give a more accurate description of where the print statement is located but it is not identical to what the c version does.
This patch series also had to make a small change to the jump_label module. to add a STATIC_KEY_INIT_FALSE and updates the static_branch_unlikely macro so that it allows keys that are nested multiple structs deep within a static variable.
changes in v2:
- fix typos
- define and use STATIC_KEY_INIT_FALSE.
- use raw pointers to avoid undefined behavior
- use cleaner version of macro definition for nested arguments (thanks Alice)
link to v1: https://lore.kernel.org/rust-for-linux/[email protected]/
Andrew Ballance (4):
rust: jump label: add support for nested arguments
rust: jump label: add STATIC_KEY_INIT_FALSE
rust: print: add support for dynamic debug to pr_debug!
rust: device: add support for dynamic debug to dev_dbg!
rust/bindings/bindings_helper.h | 1 + rust/kernel/device.rs | 105 +++++++++++++++++++- rust/kernel/jump_label.rs | 15 ++- rust/kernel/print.rs | 171 +++++++++++++++++++++++++++++++- 4 files changed, 281 insertions(+), 11 deletions(-)
Signature · 1 line
2.49.0
RFC PATCH v2 1/4 rust: jump label: add support for nested arguments
allows for nested arguments to be used with the static_branch macro. e.g. outer.inner.key can now be accessed by the macro
--- rust/kernel/jump_label.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)
rust/kernel/jump_label.rs +5 -5
@@ -19,9 +19,9 @@/// The macro must be used with a real static key defined by C. #[macro_export] macro_rules! static_branch_unlikely { - ($key:path, $keytyp:ty, $field:ident) => {{ + ($key:path, $keytyp:ty, $($field:ident).+ ) => {{let _key: *const $keytyp = ::core::ptr::addr_of!($key); -let _key: *const $crate::bindings::static_key_false = ::core::ptr::addr_of!((*_key).$field); +let _key: *const $crate::bindings::static_key_false = ::core::ptr::addr_of!((*_key)$(.$field)+);let _key: *const $crate::bindings::static_key = _key.cast ();#[cfg(not( CONFIG_JUMP_LABEL ))]
@@ -30,7 +30,7 @@ macro_rules! static_branch_unlikely { }#[cfg( CONFIG_JUMP_LABEL )] - $crate::jump_label::arch_static_branch! { $key, $keytyp, $field,false } + $crate::jump_label::arch_static_branch! { $key, $keytyp, $($field).+,false } }}; }pub use static_branch_unlikely;
@@ -46,14 +46,14 @@ macro_rules! static_branch_unlikely {#[doc(hidden)] #[cfg( CONFIG_JUMP_LABEL )] macro_rules! arch_static_branch { - ($key:path, $keytyp:ty, $field:ident, $branch:expr) => {'my_label: { + ($key:path, $keytyp:ty, $($field:ident).+ , $branch:expr) => {'my_label: { $crate::asm!(include! (concat! (env! ("OBJTREE" ),"/rust/kernel/generated_arch_static_branch_asm.rs" )); l_yes =label {break 'my_labeltrue ; }, symb = sym $key, - off =const ::core::mem::offset_of!($keytyp, $field), + off =const ::core::mem::offset_of!($keytyp, $($field).+), branch =const $crate ::jump_label::bool_to_int ($branch), );
Signature · 1 line
2.49.0
RFC PATCH v2 2/4 rust: jump label: add STATIC_KEY_INIT_FALSE
In reply to Andrew Ballanceadds a const STATIC_KEY_INIT_FALSE which should be used to init a static_key_false.
--- rust/kernel/jump_label.rs | 5 +++++ 1 file changed, 5 insertions(+)
rust/kernel/jump_label.rs +5 -0
@@ -72,3 +72,8 @@ macro_rules! arch_static_branch {pub const fn bool_to_int (b:bool ) ->i32 { bas i32 } + +/// Default value for a `static_key_false`. +// SAFETY: a unlikely static key is always zeroed +#[allow(dead_code)] +pub (crate )const STATIC_KEY_INIT_FALSE : bindings::static_key_false =unsafe { core::mem::zeroed () };
Signature · 1 line
2.49.0
RFC PATCH v2 3/4 rust: print: add support for dynamic debug to pr_debug!
In reply to Andrew Ballanceadds support for dynamic debug for the pr_debug macro.
--- rust/bindings/bindings_helper.h | 1 + rust/kernel/print.rs | 171 +++++++++++++++++++++++++++++++- 2 files changed, 168 insertions(+), 4 deletions(-)
rust/bindings/bindings_helper.h +1 -0
@@ -46,6 +46,7 @@#include <linux/cred.h> #include <linux/device/faux.h> #include <linux/dma-mapping.h> +#include <linux/dynamic_debug.h> #include <linux/errname.h> #include <linux/ethtool.h> #include <linux/file.h>
rust/kernel/print.rs +167 -4
191 lines not on this page · raw message
Signature · 1 line
2.49.0
RFC PATCH v2 4/4 rust: device: add support for dynamic debug to dev_dbg!
In reply to Andrew Ballanceadds support for dynamic debug for the dev_dbg macro.
--- rust/kernel/device.rs | 105 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 103 insertions(+), 2 deletions(-)
rust/kernel/device.rs +103 -2
@@ -536,11 +536,14 @@ macro_rules! dev_info {/// /// This level should be used for debug messages. /// -/// Equivalent to the kernel's `dev_dbg` macro, except that it doesn't support dynamic debug yet. +/// Equivalent to the kernel's `dev_dbg` macro. +/// +/// This has support for [`dynamic debug`]. /// /// Mimics the interface of [`std::print!`]. More information about the syntax is available from /// [`core::fmt`] and [`std::format!`]. /// +/// [`dynamic debug`]: https://docs.kernel.org/admin-guide/dynamic-debug-howto.html /// [`std::print!`]: https://doc.rust-lang.org/std/macro.print.html /// [`std::format!`]: https://doc.rust-lang.org/std/macro.format.html ///
@@ -555,5 +558,103 @@ macro_rules! dev_info {/// ``` #[macro_export] macro_rules! dev_dbg { - ($($f:tt)*) => { $crate::dev_printk!(pr_dbg, $($f)*); } + ($($f:tt)*) => { +#[cfg(any( DYNAMIC_DEBUG_MODULE , CONFIG_DYNAMIC_DEBUG ))] + { $crate::dynamic_dev_dbg_unlikely!($($f)*); } + +#[cfg(not(any( DYNAMIC_DEBUG_MODULE , CONFIG_DYNAMIC_DEBUG )))] + { $crate::dev_printk!(pr_dbg, $($f)*); } + } +} + +/// this contains all of the code that is used for dynamic debug for dev_dbg! +/// this is public but hidden. This code should only be called +/// by the `pr_debug!` or `dev_dbg!` macros. +#[cfg( CONFIG_DYNAMIC_DEBUG_CORE )] +#[doc(hidden)] +pub mod dynamic_debug { + +use super ::Device ; +use kernel::c_str; +use kernel::print::dynamic_debug::_Ddebug; + +/// a wrapper function around the c function `__dynamic_dev_dbg`. +/// # Safety +/// - descriptor must be a valid pointer to a `static mut` _Ddebug +pub unsafe fn dynamic_dev_dbg ( + descriptor: *mut _Ddebug , + dev: &Device , + args: core::fmt::Arguments <'_>, + ) { +// SAFETY: +// - "%pA" is null terminated and is the format for rust printing +// - dev.as_raw() is a valid pointer to struct device +unsafe { + bindings::__dynamic_dev_dbg ( + &raw mut (*descriptor).inner, + dev.as_raw (), +c_str! ("%pA" ).as_char_ptr (), + (&raw const args).cast ::<ffi::c_void >(), + ) + }; + } + +/// macro for dynamic debug equivalent to the C `dev_dbg` macro +#[doc(hidden)] +#[macro_export] +macro_rules! dynamic_dev_dbg_unlikely { + ($dev:expr, $($f:tt)*) => {{ +use $crate::c_str; +use $crate::str ::CStr ; +use $crate::print::dynamic_debug::{_ddebug, _Ddebug}; + +const MOD_NAME : &CStr =c_str! (module_path! ()); +// right now rust does not have a function! macro so hard code this to be +// the name of the macro that is printing +// TODO: +// replace this once either a function! macro exists +// or core::any::type_name becomes const +const FN_NAME : &CStr =c_str! ("dev_dbg!" ); +const FILE_NAME : &CStr =c_str! (file! ()); +const MESSAGE : &CStr =c_str! (stringify! ($($f)*)); +const LINE :u32 =line! (); + +#[used] +#[ unsafe (link_section = "__dyndbg" )] +static mut DEBUG_INFO :_Ddebug = + _Ddebug::new_unlikely (MOD_NAME ,FN_NAME ,FILE_NAME ,MESSAGE ,LINE ); + +// SAFETY: +// - this is reading from a `static mut` variable +// - key.dd_key_false is a valid static key +let should_print:bool =unsafe { +#[cfg( CONFIG_JUMP_LABEL )] + { + ::kernel::jump_label::static_branch_unlikely!( +DEBUG_INFO , + _Ddebug, + inner.key.dd_key_false + ) + } +#[cfg(not( CONFIG_JUMP_LABEL ))] + { +// gets the _DPRINTK_FLAGS_PRINT bit +// use pointers to avoid undefined behavior +let ptr: *const _ddebug = &raw const DEBUG_INFO .inner; + (*ptr).flags () &1 !=0 + } + }; + +if should_print { +// SAFETY: `&mut DEBUG_INFO` is a valid pointer to a static mut _Ddebug +unsafe { + $crate::device::dynamic_debug::dynamic_dev_dbg ( + &raw mut DEBUG_INFO , + $dev, +format_args! ($($f)*) + ); + } + } + }}; + } }
Signature · 1 line
2.49.0