From 91d39024e1b02914cc5e2dbc137908e29b269ce4 Mon Sep 17 00:00:00 2001 From: Alice Ryhl Date: Wed, 30 Oct 2024 16:04:26 +0000 Subject: rust: samples: add tracepoint to Rust sample This updates the Rust printing sample to invoke a tracepoint. This ensures that we have a user in-tree from the get-go even though the patch is being merged before its real user. Cc: Masami Hiramatsu Cc: Mathieu Desnoyers Cc: Peter Zijlstra Cc: Josh Poimboeuf Cc: Jason Baron Cc: Ard Biesheuvel Cc: Miguel Ojeda Cc: Alex Gaynor Cc: Wedson Almeida Filho Cc: Gary Guo Cc: " =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= " Cc: Benno Lossin Cc: Andreas Hindborg Cc: Arnd Bergmann Cc: Thomas Gleixner Cc: Ingo Molnar Cc: Borislav Petkov Cc: Dave Hansen Cc: "H. Peter Anvin" Cc: Sean Christopherson Cc: Uros Bizjak Cc: Catalin Marinas Cc: Will Deacon Cc: Marc Zyngier Cc: Oliver Upton Cc: Mark Rutland Cc: Ryan Roberts Cc: Fuad Tabba Cc: Paul Walmsley Cc: Palmer Dabbelt Cc: Albert Ou Cc: Anup Patel Cc: Andrew Jones Cc: Alexandre Ghiti Cc: Conor Dooley Cc: Samuel Holland Cc: Huacai Chen Cc: WANG Xuerui Cc: Bibo Mao Cc: Tiezhu Yang Cc: Andrew Morton Cc: Tianrui Zhao Link: https://lore.kernel.org/20241030-tracepoint-v12-3-eec7f0f8ad22@google.com Reviewed-by: Boqun Feng Signed-off-by: Alice Ryhl Signed-off-by: Steven Rostedt (Google) --- samples/rust/Makefile | 3 ++- samples/rust/rust_print.rs | 18 ++++++++++++++++++ samples/rust/rust_print_events.c | 8 ++++++++ 3 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 samples/rust/rust_print_events.c (limited to 'samples') diff --git a/samples/rust/Makefile b/samples/rust/Makefile index 03086dabbea4..f29280ec4820 100644 --- a/samples/rust/Makefile +++ b/samples/rust/Makefile @@ -1,6 +1,7 @@ # SPDX-License-Identifier: GPL-2.0 +ccflags-y += -I$(src) # needed for trace events obj-$(CONFIG_SAMPLE_RUST_MINIMAL) += rust_minimal.o -obj-$(CONFIG_SAMPLE_RUST_PRINT) += rust_print.o +obj-$(CONFIG_SAMPLE_RUST_PRINT) += rust_print.o rust_print_events.o subdir-$(CONFIG_SAMPLE_RUST_HOSTPROGS) += hostprogs diff --git a/samples/rust/rust_print.rs b/samples/rust/rust_print.rs index 6eabb0d79ea3..6d14b08cac1c 100644 --- a/samples/rust/rust_print.rs +++ b/samples/rust/rust_print.rs @@ -69,6 +69,8 @@ impl kernel::Module for RustPrint { arc_print()?; + trace::trace_rust_sample_loaded(42); + Ok(RustPrint) } } @@ -78,3 +80,19 @@ impl Drop for RustPrint { pr_info!("Rust printing macros sample (exit)\n"); } } + +mod trace { + use core::ffi::c_int; + + kernel::declare_trace! { + /// # Safety + /// + /// Always safe to call. + unsafe fn rust_sample_loaded(magic: c_int); + } + + pub(crate) fn trace_rust_sample_loaded(magic: i32) { + // SAFETY: Always safe to call. + unsafe { rust_sample_loaded(magic as c_int) } + } +} diff --git a/samples/rust/rust_print_events.c b/samples/rust/rust_print_events.c new file mode 100644 index 000000000000..a9169ff0edf1 --- /dev/null +++ b/samples/rust/rust_print_events.c @@ -0,0 +1,8 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Copyright 2024 Google LLC + */ + +#define CREATE_TRACE_POINTS +#define CREATE_RUST_TRACE_POINTS +#include -- cgit v1.2.3 From 22193c586b43ee88d66954395885742a6e4a49a9 Mon Sep 17 00:00:00 2001 From: Miguel Ojeda Date: Mon, 11 Nov 2024 23:08:05 +0100 Subject: samples: rust: fix `rust_print` build making it a combined module MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The `rust_print` module, when built as a module, fails to build with: ERROR: modpost: missing MODULE_LICENSE() in samples/rust/rust_print_events.o ERROR: modpost: "__tracepoint_rust_sample_loaded" [samples/rust/rust_print.ko] undefined! ERROR: modpost: "rust_do_trace_rust_sample_loaded" [samples/rust/rust_print.ko] undefined! Fix it by building it as a combined one. Cc: Masami Hiramatsu Cc: Alex Gaynor Cc: Mark Rutland Cc: Boqun Feng Cc: Gary Guo Cc: Björn Roy Baron Cc: Benno Lossin Cc: Andreas Hindborg Cc: Alice Ryhl Cc: Trevor Gross Cc: "Linux Next Mailing List" Link: https://lore.kernel.org/20241111220805.708889-1-ojeda@kernel.org Reported-by: Stephen Rothwell Closes: https://lore.kernel.org/all/20241108152149.28459a72@canb.auug.org.au/ Fixes: 91d39024e1b0 ("rust: samples: add tracepoint to Rust sample") Signed-off-by: Miguel Ojeda Signed-off-by: Steven Rostedt (Google) --- samples/rust/Makefile | 4 +- samples/rust/rust_print.rs | 98 ----------------------------------------- samples/rust/rust_print_main.rs | 98 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 101 insertions(+), 99 deletions(-) delete mode 100644 samples/rust/rust_print.rs create mode 100644 samples/rust/rust_print_main.rs (limited to 'samples') diff --git a/samples/rust/Makefile b/samples/rust/Makefile index f29280ec4820..c1a5c1655395 100644 --- a/samples/rust/Makefile +++ b/samples/rust/Makefile @@ -2,6 +2,8 @@ ccflags-y += -I$(src) # needed for trace events obj-$(CONFIG_SAMPLE_RUST_MINIMAL) += rust_minimal.o -obj-$(CONFIG_SAMPLE_RUST_PRINT) += rust_print.o rust_print_events.o +obj-$(CONFIG_SAMPLE_RUST_PRINT) += rust_print.o + +rust_print-y := rust_print_main.o rust_print_events.o subdir-$(CONFIG_SAMPLE_RUST_HOSTPROGS) += hostprogs diff --git a/samples/rust/rust_print.rs b/samples/rust/rust_print.rs deleted file mode 100644 index 6d14b08cac1c..000000000000 --- a/samples/rust/rust_print.rs +++ /dev/null @@ -1,98 +0,0 @@ -// SPDX-License-Identifier: GPL-2.0 - -//! Rust printing macros sample. - -use kernel::pr_cont; -use kernel::prelude::*; - -module! { - type: RustPrint, - name: "rust_print", - author: "Rust for Linux Contributors", - description: "Rust printing macros sample", - license: "GPL", -} - -struct RustPrint; - -fn arc_print() -> Result { - use kernel::sync::*; - - let a = Arc::new(1, GFP_KERNEL)?; - let b = UniqueArc::new("hello, world", GFP_KERNEL)?; - - // Prints the value of data in `a`. - pr_info!("{}", a); - - // Uses ":?" to print debug fmt of `b`. - pr_info!("{:?}", b); - - let a: Arc<&str> = b.into(); - let c = a.clone(); - - // Uses `dbg` to print, will move `c` (for temporary debugging purposes). - dbg!(c); - - // Pretty-prints the debug formatting with lower-case hexadecimal integers. - pr_info!("{:#x?}", a); - - Ok(()) -} - -impl kernel::Module for RustPrint { - fn init(_module: &'static ThisModule) -> Result { - pr_info!("Rust printing macros sample (init)\n"); - - pr_emerg!("Emergency message (level 0) without args\n"); - pr_alert!("Alert message (level 1) without args\n"); - pr_crit!("Critical message (level 2) without args\n"); - pr_err!("Error message (level 3) without args\n"); - pr_warn!("Warning message (level 4) without args\n"); - pr_notice!("Notice message (level 5) without args\n"); - pr_info!("Info message (level 6) without args\n"); - - pr_info!("A line that"); - pr_cont!(" is continued"); - pr_cont!(" without args\n"); - - pr_emerg!("{} message (level {}) with args\n", "Emergency", 0); - pr_alert!("{} message (level {}) with args\n", "Alert", 1); - pr_crit!("{} message (level {}) with args\n", "Critical", 2); - pr_err!("{} message (level {}) with args\n", "Error", 3); - pr_warn!("{} message (level {}) with args\n", "Warning", 4); - pr_notice!("{} message (level {}) with args\n", "Notice", 5); - pr_info!("{} message (level {}) with args\n", "Info", 6); - - pr_info!("A {} that", "line"); - pr_cont!(" is {}", "continued"); - pr_cont!(" with {}\n", "args"); - - arc_print()?; - - trace::trace_rust_sample_loaded(42); - - Ok(RustPrint) - } -} - -impl Drop for RustPrint { - fn drop(&mut self) { - pr_info!("Rust printing macros sample (exit)\n"); - } -} - -mod trace { - use core::ffi::c_int; - - kernel::declare_trace! { - /// # Safety - /// - /// Always safe to call. - unsafe fn rust_sample_loaded(magic: c_int); - } - - pub(crate) fn trace_rust_sample_loaded(magic: i32) { - // SAFETY: Always safe to call. - unsafe { rust_sample_loaded(magic as c_int) } - } -} diff --git a/samples/rust/rust_print_main.rs b/samples/rust/rust_print_main.rs new file mode 100644 index 000000000000..6d14b08cac1c --- /dev/null +++ b/samples/rust/rust_print_main.rs @@ -0,0 +1,98 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! Rust printing macros sample. + +use kernel::pr_cont; +use kernel::prelude::*; + +module! { + type: RustPrint, + name: "rust_print", + author: "Rust for Linux Contributors", + description: "Rust printing macros sample", + license: "GPL", +} + +struct RustPrint; + +fn arc_print() -> Result { + use kernel::sync::*; + + let a = Arc::new(1, GFP_KERNEL)?; + let b = UniqueArc::new("hello, world", GFP_KERNEL)?; + + // Prints the value of data in `a`. + pr_info!("{}", a); + + // Uses ":?" to print debug fmt of `b`. + pr_info!("{:?}", b); + + let a: Arc<&str> = b.into(); + let c = a.clone(); + + // Uses `dbg` to print, will move `c` (for temporary debugging purposes). + dbg!(c); + + // Pretty-prints the debug formatting with lower-case hexadecimal integers. + pr_info!("{:#x?}", a); + + Ok(()) +} + +impl kernel::Module for RustPrint { + fn init(_module: &'static ThisModule) -> Result { + pr_info!("Rust printing macros sample (init)\n"); + + pr_emerg!("Emergency message (level 0) without args\n"); + pr_alert!("Alert message (level 1) without args\n"); + pr_crit!("Critical message (level 2) without args\n"); + pr_err!("Error message (level 3) without args\n"); + pr_warn!("Warning message (level 4) without args\n"); + pr_notice!("Notice message (level 5) without args\n"); + pr_info!("Info message (level 6) without args\n"); + + pr_info!("A line that"); + pr_cont!(" is continued"); + pr_cont!(" without args\n"); + + pr_emerg!("{} message (level {}) with args\n", "Emergency", 0); + pr_alert!("{} message (level {}) with args\n", "Alert", 1); + pr_crit!("{} message (level {}) with args\n", "Critical", 2); + pr_err!("{} message (level {}) with args\n", "Error", 3); + pr_warn!("{} message (level {}) with args\n", "Warning", 4); + pr_notice!("{} message (level {}) with args\n", "Notice", 5); + pr_info!("{} message (level {}) with args\n", "Info", 6); + + pr_info!("A {} that", "line"); + pr_cont!(" is {}", "continued"); + pr_cont!(" with {}\n", "args"); + + arc_print()?; + + trace::trace_rust_sample_loaded(42); + + Ok(RustPrint) + } +} + +impl Drop for RustPrint { + fn drop(&mut self) { + pr_info!("Rust printing macros sample (exit)\n"); + } +} + +mod trace { + use core::ffi::c_int; + + kernel::declare_trace! { + /// # Safety + /// + /// Always safe to call. + unsafe fn rust_sample_loaded(magic: c_int); + } + + pub(crate) fn trace_rust_sample_loaded(magic: i32) { + // SAFETY: Always safe to call. + unsafe { rust_sample_loaded(magic as c_int) } + } +} -- cgit v1.2.3