Skip to content

Commit ab632d5

Browse files
committed
print.rs: Allow printing in the kernel crate
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. The attribute `#[allow(unused_unsafe)]` is also added to the unsafe blocks in the print macros to prevent warnings when printing from within an already unsafe context. Signed-off-by: Adam Bratschi-Kaye <[email protected]>
1 parent 3b8575d commit ab632d5

File tree

3 files changed

+23
-10
lines changed

3 files changed

+23
-10
lines changed

rust/kernel/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ pub use crate::types::{CStr, Mode};
6969
/// [`PAGE_SHIFT`]: ../../../include/asm-generic/page.h
7070
pub const PAGE_SIZE: usize = 1 << bindings::PAGE_SHIFT;
7171

72+
/// Prefix to appear before log messages printed from within the kernel crate.
73+
const __LOG_PREFIX: &[u8] = b"rust_kernel_crate\0";
74+
7275
/// The top level entrypoint to implementing a kernel module.
7376
///
7477
/// For any teardown or cleanup operations, your type may implement [`Drop`].

rust/kernel/print.rs

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -209,20 +209,25 @@ macro_rules! print_macro (
209209
($format_string:path, false, $fmt:expr) => (
210210
// SAFETY: This hidden macro should only be called by the documented
211211
// printing macros which ensure the format string is one of the fixed
212-
// ones. All `__MODULE_NAME`s are null-terminated as they are generated
213-
// by the `module!` proc macro.
212+
// ones. All `__LOG_PREFIX`s are null-terminated as they are generated
213+
// by the `module!` proc macro or fixed values defined in a kernel
214+
// crate.
215+
//
216+
// The `unsafe` block may be unused if the macro is called from
217+
// within an unsafe block or function.
218+
#[allow(unused_unsafe)]
214219
unsafe {
215-
kernel::print::call_printk(
220+
$crate::print::call_printk(
216221
&$format_string,
217-
crate::__MODULE_NAME,
222+
crate::__LOG_PREFIX,
218223
$fmt.as_bytes(),
219224
);
220225
}
221226
);
222227

223228
// Without extra arguments: no need to format anything (`CONT` case).
224229
($format_string:path, true, $fmt:expr) => (
225-
kernel::print::call_printk_cont(
230+
$crate::print::call_printk_cont(
226231
$fmt.as_bytes(),
227232
);
228233
);
@@ -245,12 +250,17 @@ macro_rules! print_macro (
245250
//
246251
// SAFETY: This hidden macro should only be called by the documented
247252
// printing macros which ensure the format string is one of the fixed
248-
// ones. All `__MODULE_NAME`s are null-terminated as they are generated
249-
// by the `module!` proc macro.
253+
// ones. All `__LOG_PREFIX`s are null-terminated as they are generated
254+
// by the `module!` proc macro or fixed values defined in a kernel
255+
// crate.
256+
//
257+
// The `unsafe` block may be unused if the macro is called from
258+
// within an unsafe block or function.
259+
#[allow(unused_unsafe)]
250260
unsafe {
251-
kernel::print::format_and_call::<$cont>(
261+
$crate::print::format_and_call::<$cont>(
252262
&$format_string,
253-
crate::__MODULE_NAME,
263+
crate::__LOG_PREFIX,
254264
format_args!($fmt, $($arg)*),
255265
);
256266
}

rust/module.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -587,7 +587,7 @@ pub fn module(ts: TokenStream) -> TokenStream {
587587
/// The module name.
588588
///
589589
/// Used by the printing macros, e.g. [`info!`].
590-
const __MODULE_NAME: &[u8] = b\"{name}\\0\";
590+
const __LOG_PREFIX: &[u8] = b\"{name}\\0\";
591591
592592
static mut __MOD: Option<{type_}> = None;
593593

0 commit comments

Comments
 (0)