C: zulip://rust-for-linux.zulipchat.com
P: https://rust-for-linux.com/contributing
T: git https://github.com/Rust-for-Linux/linux.git rust-next
+ F: .clippy.toml
F: Documentation/rust/
+F: include/trace/events/rust_sample.h
F: rust/
F: samples/rust/
F: scripts/*rust*
pub mod sync;
pub mod task;
pub mod time;
+pub mod tracepoint;
+ pub mod transmute;
pub mod types;
pub mod uaccess;
pub mod workqueue;
//!
//! C header: [`include/linux/sched.h`](srctree/include/linux/sched.h).
- use core::{
- cmp::{Eq, PartialEq},
- ffi::{c_int, c_long, c_uint},
- ops::Deref,
- ptr,
- };
+use crate::{
+ bindings,
+ pid_namespace::PidNamespace,
+ types::{ARef, NotThreadSafe, Opaque},
+};
-use crate::types::Opaque;
-use core::{marker::PhantomData, ops::Deref, ptr};
+ use crate::ffi::{c_int, c_long, c_uint};
++use core::{cmp::{Eq, PartialEq},ops::Deref, ptr};
/// A sentinel value used for infinite timeouts.
pub const MAX_SCHEDULE_TIMEOUT: c_long = c_long::MAX;
/// Constructs an instance of [`Either`] containing a value of type `R`.
Right(R),
}
- /// Types for which any bit pattern is valid.
- ///
- /// Not all types are valid for all values. For example, a `bool` must be either zero or one, so
- /// reading arbitrary bytes into something that contains a `bool` is not okay.
- ///
- /// It's okay for the type to have padding, as initializing those bytes has no effect.
- ///
- /// # Safety
- ///
- /// All bit-patterns must be valid for this type. This type must not have interior mutability.
- pub unsafe trait FromBytes {}
-
- // SAFETY: All bit patterns are acceptable values of the types below.
- unsafe impl FromBytes for u8 {}
- unsafe impl FromBytes for u16 {}
- unsafe impl FromBytes for u32 {}
- unsafe impl FromBytes for u64 {}
- unsafe impl FromBytes for usize {}
- unsafe impl FromBytes for i8 {}
- unsafe impl FromBytes for i16 {}
- unsafe impl FromBytes for i32 {}
- unsafe impl FromBytes for i64 {}
- unsafe impl FromBytes for isize {}
- // SAFETY: If all bit patterns are acceptable for individual values in an array, then all bit
- // patterns are also acceptable for arrays of that type.
- unsafe impl<T: FromBytes> FromBytes for [T] {}
- unsafe impl<T: FromBytes, const N: usize> FromBytes for [T; N] {}
-
- /// Types that can be viewed as an immutable slice of initialized bytes.
- ///
- /// If a struct implements this trait, then it is okay to copy it byte-for-byte to userspace. This
- /// means that it should not have any padding, as padding bytes are uninitialized. Reading
- /// uninitialized memory is not just undefined behavior, it may even lead to leaking sensitive
- /// information on the stack to userspace.
- ///
- /// The struct should also not hold kernel pointers, as kernel pointer addresses are also considered
- /// sensitive. However, leaking kernel pointers is not considered undefined behavior by Rust, so
- /// this is a correctness requirement, but not a safety requirement.
- ///
- /// # Safety
- ///
- /// Values of this type may not contain any uninitialized bytes. This type must not have interior
- /// mutability.
- pub unsafe trait AsBytes {}
-
- // SAFETY: Instances of the following types have no uninitialized portions.
- unsafe impl AsBytes for u8 {}
- unsafe impl AsBytes for u16 {}
- unsafe impl AsBytes for u32 {}
- unsafe impl AsBytes for u64 {}
- unsafe impl AsBytes for usize {}
- unsafe impl AsBytes for i8 {}
- unsafe impl AsBytes for i16 {}
- unsafe impl AsBytes for i32 {}
- unsafe impl AsBytes for i64 {}
- unsafe impl AsBytes for isize {}
- unsafe impl AsBytes for bool {}
- unsafe impl AsBytes for char {}
- unsafe impl AsBytes for str {}
- // SAFETY: If individual values in an array have no uninitialized portions, then the array itself
- // does not have any uninitialized portions either.
- unsafe impl<T: AsBytes> AsBytes for [T] {}
- unsafe impl<T: AsBytes, const N: usize> AsBytes for [T; N] {}
-
+
+/// Zero-sized type to mark types not [`Send`].
+///
+/// Add this type as a field to your struct if your type should not be sent to a different task.
+/// Since [`Send`] is an auto trait, adding a single field that is `!Send` will ensure that the
+/// whole type is `!Send`.
+///
+/// If a type is `!Send` it is impossible to give control over an instance of the type to another
+/// task. This is useful to include in types that store or reference task-local information. A file
+/// descriptor is an example of such task-local information.
+///
+/// This type also makes the type `!Sync`, which prevents immutable access to the value from
+/// several threads in parallel.
+pub type NotThreadSafe = PhantomData<*mut ()>;
+
+/// Used to construct instances of type [`NotThreadSafe`] similar to how `PhantomData` is
+/// constructed.
+///
+/// [`NotThreadSafe`]: type@NotThreadSafe
+#[allow(non_upper_case_globals)]
+pub const NotThreadSafe: NotThreadSafe = PhantomData;
--- /dev/null
+// 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;
+
++#[expect(clippy::disallowed_macros)]
+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<Self> {
+ 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) }
+ }
+}
# Compile Rust sources (.rs)
# ---------------------------------------------------------------------------
- rust_allowed_features := asm_const,asm_goto,new_uninit
-rust_allowed_features := arbitrary_self_types,lint_reasons
++rust_allowed_features := asm_const,asm_goto,arbitrary_self_types,lint_reasons
# `--out-dir` is required to avoid temporaries being created by `rustc` in the
# current working directory, which may be not accessible in the out-of-tree