lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20250611202952.1670168-4-andrewjballance@gmail.com>
Date: Wed, 11 Jun 2025 15:29:52 -0500
From: Andrew Ballance <andrewjballance@...il.com>
To: jbaron@...mai.com,
	jim.cromie@...il.com,
	daniel.almeida@...labora.com,
	acourbot@...dia.com,
	ojeda@...nel.org,
	alex.gaynor@...il.com,
	boqun.feng@...il.com,
	gary@...yguo.net,
	bjorn3_gh@...tonmail.com,
	lossin@...nel.org,
	a.hindborg@...nel.org,
	aliceryhl@...gle.com,
	tmgross@...ch.edu,
	dakr@...nel.org,
	gregkh@...uxfoundation.org,
	rafael@...nel.org,
	rostedt@...dmis.org,
	andrewjballance@...il.com
Cc: viresh.kumar@...aro.org,
	lina+kernel@...hilina.net,
	tamird@...il.com,
	jubalh@...oru.org,
	rust-for-linux@...r.kernel.org,
	linux-kernel@...r.kernel.org
Subject: [RFC PATCH 3/3] rust: device add support for dynamic debug to dev_dbg!

adds support for dynamic debug for the dev_dbg macro.

Signed-off-by: Andrew Ballance <andrewjballance@...il.com>
---
 rust/kernel/device.rs | 102 +++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 100 insertions(+), 2 deletions(-)

diff --git a/rust/kernel/device.rs b/rust/kernel/device.rs
index dea06b79ecb5..f59fed944876 100644
--- a/rust/kernel/device.rs
+++ b/rust/kernel/device.rs
@@ -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,100 @@ 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 reference 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 equilant 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!();
+
+            #[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
+                    DEBUG_INFO.inner.flags() & 1 != 0
+                }
+            };
+
+            if should_print {
+                // SAFETY: `&mut DEBUG_INFO` is a valid reference to a static mut _Ddebug
+                unsafe {
+                    $crate::device::dynamic_debug::dynamic_dev_dbg(
+                        &mut DEBUG_INFO,
+                        $dev,
+                        format_args!($($f)*)
+                    );
+                }
+            }
+        }};
+    }
 }
-- 
2.49.0


Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ