]> www.infradead.org Git - users/jedix/linux-maple.git/commitdiff
rust: allow printing in the kernel crate
authorAdam Bratschi-Kaye <ark.email@gmail.com>
Sat, 24 Apr 2021 11:28:08 +0000 (13:28 +0200)
committerMiguel Ojeda <ojeda@kernel.org>
Tue, 27 Apr 2021 03:06:13 +0000 (05:06 +0200)
The `pr_*` print macros previously didn't compile when used from the
`kernel` crate because they refer to the path `kernel` instead of
using the `$crate` variable and require a `const __MODULE_NAME` to be
defined in any crate in which they are used.

This fixes both issues and changes the `__MODULE_NAME` to `__LOG_PREFIX`
to reflect the fact that the print macro may be called from a module or
a kernel library.

Signed-off-by: Adam Bratschi-Kaye <ark.email@gmail.com>
[normalized title]
Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
rust/kernel/lib.rs
rust/kernel/print.rs
rust/module.rs

index 8f5a637f41672dad93e88d7b5554cbdd771055a6..2ad22dc1e6dece79497f626bca6041db91bd9b86 100644 (file)
@@ -70,6 +70,9 @@ pub use crate::types::{CStr, Mode};
 /// [`PAGE_SHIFT`]: ../../../include/asm-generic/page.h
 pub const PAGE_SIZE: usize = 1 << bindings::PAGE_SHIFT;
 
+/// Prefix to appear before log messages printed from within the kernel crate.
+const __LOG_PREFIX: &[u8] = b"rust_kernel\0";
+
 /// The top level entrypoint to implementing a kernel module.
 ///
 /// For any teardown or cleanup operations, your type may implement [`Drop`].
index 71a5ebe500d72782e9761d4696e94f88c980c4e3..ff9d1269cdae6b244ee5b756509a4eab6df94ed8 100644 (file)
@@ -209,12 +209,13 @@ macro_rules! print_macro (
     ($format_string:path, false, $fmt:expr) => (
         // SAFETY: This hidden macro should only be called by the documented
         // printing macros which ensure the format string is one of the fixed
-        // ones. All `__MODULE_NAME`s are null-terminated as they are generated
-        // by the `module!` proc macro.
+        // ones. All `__LOG_PREFIX`s are null-terminated as they are generated
+        // by the `module!` proc macro or fixed values defined in a kernel
+        // crate.
         unsafe {
-            kernel::print::call_printk(
+            $crate::print::call_printk(
                 &$format_string,
-                crate::__MODULE_NAME,
+                crate::__LOG_PREFIX,
                 $fmt.as_bytes(),
             );
         }
@@ -222,7 +223,7 @@ macro_rules! print_macro (
 
     // Without extra arguments: no need to format anything (`CONT` case).
     ($format_string:path, true, $fmt:expr) => (
-        kernel::print::call_printk_cont(
+        $crate::print::call_printk_cont(
             $fmt.as_bytes(),
         );
     );
@@ -245,12 +246,13 @@ macro_rules! print_macro (
         //
         // SAFETY: This hidden macro should only be called by the documented
         // printing macros which ensure the format string is one of the fixed
-        // ones. All `__MODULE_NAME`s are null-terminated as they are generated
-        // by the `module!` proc macro.
+        // ones. All `__LOG_PREFIX`s are null-terminated as they are generated
+        // by the `module!` proc macro or fixed values defined in a kernel
+        // crate.
         unsafe {
-            kernel::print::format_and_call::<$cont>(
+            $crate::print::format_and_call::<$cont>(
                 &$format_string,
-                crate::__MODULE_NAME,
+                crate::__LOG_PREFIX,
                 format_args!($fmt, $($arg)*),
             );
         }
index 5daa1aee3b598bbf3cfd3a1a287ff2ccb35d1174..bddb90d4822d42024e4cb4c2c330ccc8be7ee94a 100644 (file)
@@ -587,7 +587,7 @@ pub fn module(ts: TokenStream) -> TokenStream {
             /// The module name.
             ///
             /// Used by the printing macros, e.g. [`info!`].
-            const __MODULE_NAME: &[u8] = b\"{name}\\0\";
+            const __LOG_PREFIX: &[u8] = b\"{name}\\0\";
 
             static mut __MOD: Option<{type_}> = None;