2
0
mirror of git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git synced 2025-09-04 20:19:47 +08:00
linux/rust/kernel/seq_file.rs
Kunwu Chan 0b9817caac
rust: optimize rust symbol generation for SeqFile
When build the kernel using the llvm-18.1.3-rust-1.85.0-x86_64
with ARCH=arm64, the following symbols are generated:

$nm vmlinux | grep ' _R'.*SeqFile | rustfilt
ffff8000805b78ac T <kernel::seq_file::SeqFile>::call_printf

This Rust symbol is trivial wrappers around the C functions seq_printf.
It doesn't make sense to go through a trivial wrapper for its functions,
so mark it inline.

Link: https://github.com/Rust-for-Linux/linux/issues/1145
Suggested-by: Alice Ryhl <aliceryhl@google.com>
Co-developed-by: Grace Deng <Grace.Deng006@Gmail.com>
Signed-off-by: Grace Deng <Grace.Deng006@Gmail.com>
Signed-off-by: Kunwu Chan <kunwu.chan@hotmail.com>
Link: https://lore.kernel.org/r/20250317030418.2371265-1-kunwu.chan@linux.dev
Reviewed-by: Alice Ryhl <aliceryhl@google.com>
Reviewed-by: Benno Lossin <benno.lossin@proton.me>
Signed-off-by: Christian Brauner <brauner@kernel.org>
2025-03-18 09:26:24 +01:00

54 lines
1.7 KiB
Rust

// SPDX-License-Identifier: GPL-2.0
//! Seq file bindings.
//!
//! C header: [`include/linux/seq_file.h`](srctree/include/linux/seq_file.h)
use crate::{bindings, c_str, types::NotThreadSafe, types::Opaque};
/// A utility for generating the contents of a seq file.
#[repr(transparent)]
pub struct SeqFile {
inner: Opaque<bindings::seq_file>,
_not_send: NotThreadSafe,
}
impl SeqFile {
/// Creates a new [`SeqFile`] from a raw pointer.
///
/// # Safety
///
/// The caller must ensure that for the duration of 'a the following is satisfied:
/// * The pointer points at a valid `struct seq_file`.
/// * The `struct seq_file` is not accessed from any other thread.
pub unsafe fn from_raw<'a>(ptr: *mut bindings::seq_file) -> &'a SeqFile {
// SAFETY: The caller ensures that the reference is valid for 'a. There's no way to trigger
// a data race by using the `&SeqFile` since this is the only thread accessing the seq_file.
//
// CAST: The layout of `struct seq_file` and `SeqFile` is compatible.
unsafe { &*ptr.cast() }
}
/// Used by the [`seq_print`] macro.
#[inline]
pub fn call_printf(&self, args: core::fmt::Arguments<'_>) {
// SAFETY: Passing a void pointer to `Arguments` is valid for `%pA`.
unsafe {
bindings::seq_printf(
self.inner.get(),
c_str!("%pA").as_char_ptr(),
&args as *const _ as *const crate::ffi::c_void,
);
}
}
}
/// Write to a [`SeqFile`] with the ordinary Rust formatting syntax.
#[macro_export]
macro_rules! seq_print {
($m:expr, $($arg:tt)+) => (
$m.call_printf(format_args!($($arg)+))
);
}
pub use seq_print;