From nobody Thu Oct 2 00:46:13 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E6692306B24; Wed, 24 Sep 2025 12:40:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758717648; cv=none; b=JfPwPs2f+DXFgGJLIl/N6/7Ogcsvci6lnyMJphTO6f4Y7Sr0yI88Pf9kXKO2Hpgs9AmkxLG1WtK/RAc6trTZrGH8QaehJQQE/KvQMB0cU9Tu8axflaK/qUW32rejt169jdit2PH+wGegYUPdybc9vucm2Y11seudEZTt9VfYhps= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758717648; c=relaxed/simple; bh=FC9GwCkiEibqEuCwLlQ1Q5VQbSyjrJul1CXFpg5by3k=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=O7TTrp9Q8PyPzFfvBq16TjE+rCOHeOSiGAMoQjsMXEmuO7xbUurognkIwYO7tC5gg87oFDXtuzqHEtEZk9x/cGkKEO+C3wT6pgHjza4rTjmQA4kE18aj5bsxDvWF6MGF+PCVNLcSAw9yYd8WRvuh16OXZZKecXmppGPmTjvXLGg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=L5HAFyIM; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="L5HAFyIM" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4B15EC4CEF0; Wed, 24 Sep 2025 12:40:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1758717647; bh=FC9GwCkiEibqEuCwLlQ1Q5VQbSyjrJul1CXFpg5by3k=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=L5HAFyIMnrJKI0YA3DWNYV3z9TDtIQWvHydSg6yvATTb72C4OzW4OoNMs5xY7UMMB H5JHERRPXKi4wkzi4OmpnnFiYgGGVL/wq9xiAj5OD1zlHmhC8UsBclCKtfqUDen7yr AM1XbRK0g8LaHUzTESLrXsjl9po+VxrMIyxXMLzgSESJeCqMzqHJ+YzilG1o5waRZw 2FEuF12Xd5k0BAaBGqn5KlPHSuanwtrYYdSHcZXFJeQKgrbwBNEdYxYJE0UwexPJb4 QkJJWtZPFQgcFgujuAOOWdVEZzko72d9BoJK/dbUJuAwU7E7xpGnf5zPyqYeY9j3bJ 5Y8stisbb35qQ== From: Andreas Hindborg Date: Wed, 24 Sep 2025 14:39:24 +0200 Subject: [PATCH v18 1/7] rust: sync: add `SetOnce` Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250924-module-params-v3-v18-1-bf512c35d910@kernel.org> References: <20250924-module-params-v3-v18-0-bf512c35d910@kernel.org> In-Reply-To: <20250924-module-params-v3-v18-0-bf512c35d910@kernel.org> To: Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Alice Ryhl , Masahiro Yamada , Nathan Chancellor , Luis Chamberlain , Danilo Krummrich , Benno Lossin , Daniel Gomez , Benno Lossin , Nicolas Schier Cc: Trevor Gross , Adam Bratschi-Kaye , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kbuild@vger.kernel.org, Petr Pavlu , Sami Tolvanen , Daniel Gomez , Simona Vetter , Greg KH , Fiona Behrens , Daniel Almeida , linux-modules@vger.kernel.org, Andreas Hindborg X-Mailer: b4 0.15-dev X-Developer-Signature: v=1; a=openpgp-sha256; l=5476; i=a.hindborg@kernel.org; h=from:subject:message-id; bh=FC9GwCkiEibqEuCwLlQ1Q5VQbSyjrJul1CXFpg5by3k=; b=owEBbQKS/ZANAwAIAeG4Gj55KGN3AcsmYgBo0+ag9/focWPE9Rg4DKUGQq73skp7akVHcvblT 3OdqR7vPICJAjMEAAEIAB0WIQQSwflHVr98KhXWwBLhuBo+eShjdwUCaNPmoAAKCRDhuBo+eShj dw9yD/wKMqUnk7VbroVYaods1snwU8Ypzm3zaennrSDK7pIpvEpahMwr3wh97oK0vbUhtmW5RAl nRvd1LMyF3QkR6QBuI/w/Lv/eVVGHbjQ3lz8OZ7vr4JJpUp5dvqdpwynk5IDNr5akTfsD3DjzBH jsvx5kmitro7Qxa0fLSYiWbFybmW24vwkirJAWEYl/6euVxmq3uIpJRL3HdtWu3WDFETzshSgid way1279njlazVdTE5SSKAiH7XaU/YC+P4brNbWoWeObMw+3krrJRIn5zV/vqH/COGKFWecsbPEb 2u9zuUt+kRc4MyBAIwQZOdQDyF6+qF4AElQxweMkq9nF0plhbsVgvhd1BdTudboLq4ZRKC6II5z YQvzxOJPNZvhR5QHjNvbEc0W7cFysxY1E3kNP+CJikP5/hYUkMDrd7NLDMLodyH2Ef0s5vyPpLZ QBvyRv5xAUbi7ixiHQCfyolGJQsedLgdNYD2nNUVApIm516lVuz0v6UgVIH+wsIOhkgiFztkz/C 8FtpJV/2e4ZxaF+UxSDNjSfx4k3xtTryyTGZOpLN6KaofjK9o1i6MyYyxjqi/TmypmUSw8S7Lv8 o99+uJ/Az//suf+g9EJFJaj3jpFK0xDKWMh6KUSFGcgY2IGwYfAZqF2/CsKPlCP1kBHXH6eMqoq lUM8IG5WPYBUYeA== X-Developer-Key: i=a.hindborg@kernel.org; a=openpgp; fpr=3108C10F46872E248D1FB221376EB100563EF7A7 Introduce the `SetOnce` type, a container that can only be written once. The container uses an internal atomic to synchronize writes to the internal value. Reviewed-by: Alice Ryhl Reviewed-by: Benno Lossin Signed-off-by: Andreas Hindborg --- rust/kernel/sync.rs | 2 + rust/kernel/sync/set_once.rs | 125 +++++++++++++++++++++++++++++++++++++++= ++++ 2 files changed, 127 insertions(+) diff --git a/rust/kernel/sync.rs b/rust/kernel/sync.rs index cf5b638a097d9..3f957ad3a9f0a 100644 --- a/rust/kernel/sync.rs +++ b/rust/kernel/sync.rs @@ -20,6 +20,7 @@ pub mod poll; pub mod rcu; mod refcount; +mod set_once; =20 pub use arc::{Arc, ArcBorrow, UniqueArc}; pub use completion::Completion; @@ -29,6 +30,7 @@ pub use lock::spinlock::{new_spinlock, SpinLock, SpinLockGuard}; pub use locked_by::LockedBy; pub use refcount::Refcount; +pub use set_once::SetOnce; =20 /// Represents a lockdep class. It's a wrapper around C's `lock_class_key`. #[repr(transparent)] diff --git a/rust/kernel/sync/set_once.rs b/rust/kernel/sync/set_once.rs new file mode 100644 index 0000000000000..bdba601807d8b --- /dev/null +++ b/rust/kernel/sync/set_once.rs @@ -0,0 +1,125 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! A container that can be initialized at most once. + +use super::atomic::{ + ordering::{Acquire, Relaxed, Release}, + Atomic, +}; +use core::{cell::UnsafeCell, mem::MaybeUninit}; + +/// A container that can be populated at most once. Thread safe. +/// +/// Once the a [`SetOnce`] is populated, it remains populated by the same = object for the +/// lifetime `Self`. +/// +/// # Invariants +/// +/// - `init` may only increase in value. +/// - `init` may only assume values in the range `0..=3D2`. +/// - `init =3D=3D 0` if and only if `value` is uninitialized. +/// - `init =3D=3D 1` if and only if there is exactly one thread with excl= usive +/// access to `self.value`. +/// - `init =3D=3D 2` if and only if `value` is initialized and valid for = shared +/// access. +/// +/// # Example +/// +/// ``` +/// # use kernel::sync::SetOnce; +/// let value =3D SetOnce::new(); +/// assert_eq!(None, value.as_ref()); +/// +/// let status =3D value.populate(42u8); +/// assert_eq!(true, status); +/// assert_eq!(Some(&42u8), value.as_ref()); +/// assert_eq!(Some(42u8), value.copy()); +/// +/// let status =3D value.populate(101u8); +/// assert_eq!(false, status); +/// assert_eq!(Some(&42u8), value.as_ref()); +/// assert_eq!(Some(42u8), value.copy()); +/// ``` +pub struct SetOnce { + init: Atomic, + value: UnsafeCell>, +} + +impl Default for SetOnce { + fn default() -> Self { + Self::new() + } +} + +impl SetOnce { + /// Create a new [`SetOnce`]. + /// + /// The returned instance will be empty. + pub const fn new() -> Self { + // INVARIANT: The container is empty and we initialize `init` to `= 0`. + Self { + value: UnsafeCell::new(MaybeUninit::uninit()), + init: Atomic::new(0), + } + } + + /// Get a reference to the contained object. + /// + /// Returns [`None`] if this [`SetOnce`] is empty. + pub fn as_ref(&self) -> Option<&T> { + if self.init.load(Acquire) =3D=3D 2 { + // SAFETY: By the type invariants of `Self`, `self.init =3D=3D= 2` means that `self.value` + // is initialized and valid for shared access. + Some(unsafe { &*self.value.get().cast() }) + } else { + None + } + } + + /// Populate the [`SetOnce`]. + /// + /// Returns `true` if the [`SetOnce`] was successfully populated. + pub fn populate(&self, value: T) -> bool { + // INVARIANT: If the swap succeeds: + // - We increase `init`. + // - We write the valid value `1` to `init`. + // - Only one thread can succeed in this write, so we have exclus= ive access after the + // write. + if let Ok(0) =3D self.init.cmpxchg(0, 1, Relaxed) { + // SAFETY: By the type invariants of `Self`, the fact that we = succeeded in writing `1` + // to `self.init` means we obtained exclusive access to `self.= value`. + unsafe { core::ptr::write(self.value.get().cast(), value) }; + // INVARIANT: + // - We increase `init`. + // - We write the valid value `2` to `init`. + // - We release our exclusive access to `self.value` and it i= s now valid for shared + // access. + self.init.store(2, Release); + true + } else { + false + } + } + + /// Get a copy of the contained object. + /// + /// Returns [`None`] if the [`SetOnce`] is empty. + pub fn copy(&self) -> Option + where + T: Copy, + { + self.as_ref().copied() + } +} + +impl Drop for SetOnce { + fn drop(&mut self) { + if *self.init.get_mut() =3D=3D 2 { + let value =3D self.value.get_mut(); + // SAFETY: By the type invariants of `Self`, `self.init =3D=3D= 2` means that `self.value` + // contains a valid value. We have exclusive access, as we hol= d a `mut` reference to + // `self`. + unsafe { value.assume_init_drop() }; + } + } +} --=20 2.47.2 From nobody Thu Oct 2 00:46:13 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B189230C34B; Wed, 24 Sep 2025 12:41:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758717665; cv=none; b=pSofO2q+67T7kRw/nAK54VSpCy0LT9LBxhwrRxhm7oUpOsPG8lbo34XPDMmbodZV+242DfcXdllgBY8yfCdkN32XJDxn/iJcAzr9V9gyFo0twGoUTsGebyPH2eY1UYnza0GV3eBD+fjq/DBBKr3mFAXgMtxM+eMV/UtGU7UbJuI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758717665; c=relaxed/simple; bh=441eDv2wC96HCfe5soUKHsxfOl00Pmg21RsWhzUsuHQ=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=XoRQgxTa7NJzl0OYZxk6OdexpcOPezTHy3/pBhEds/yQ2VFxAv26/zolVgzIXFcXnx6xQy9tYwf8sJb6bb2V0ycspb8AU4xsOaV9oW0pByaLuyZbeMr6fWSyGJGACQCFazTYD3gpUm1gOE3t9XcP/AqRsMHgekzglY6VbxWeeOs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=qp2ru5ia; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="qp2ru5ia" Received: by smtp.kernel.org (Postfix) with ESMTPSA id EC3E9C4CEF7; Wed, 24 Sep 2025 12:40:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1758717665; bh=441eDv2wC96HCfe5soUKHsxfOl00Pmg21RsWhzUsuHQ=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=qp2ru5iamqtpvSCierEZThu3a+SuUv7KYiuhgt5rp4+EKsa2tRYEaOXoJAdhjihCk DCET+7kmr7Qf5vqYRsYUxhEW0XwF54bUojp+Fp/BuL2h0dkOyot6SCnkLMru4Igg57 tiasAP5sJvWc4ofOAJoKCKEgBLEHDvfvq8gVLf68dYjaTAnDPQ6bErO66MIY9kDBpW QBH9RHX76U5SvB1zfFkMm+ETBgGuay6fVMaH4Z3RNaYE7D/co2685VGfF8XyWChqJg V0MSxiDiyEiowVbwaoANPLiwgiu9wg8/VKFFKElfACIh/iziQBVr2i0WsjJiZDb6EK IY0Qmc/fqeDIw== From: Andreas Hindborg Date: Wed, 24 Sep 2025 14:39:25 +0200 Subject: [PATCH v18 2/7] rust: str: add radix prefixed integer parsing functions Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250924-module-params-v3-v18-2-bf512c35d910@kernel.org> References: <20250924-module-params-v3-v18-0-bf512c35d910@kernel.org> In-Reply-To: <20250924-module-params-v3-v18-0-bf512c35d910@kernel.org> To: Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Alice Ryhl , Masahiro Yamada , Nathan Chancellor , Luis Chamberlain , Danilo Krummrich , Benno Lossin , Daniel Gomez , Benno Lossin , Nicolas Schier Cc: Trevor Gross , Adam Bratschi-Kaye , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kbuild@vger.kernel.org, Petr Pavlu , Sami Tolvanen , Daniel Gomez , Simona Vetter , Greg KH , Fiona Behrens , Daniel Almeida , linux-modules@vger.kernel.org, Andreas Hindborg X-Mailer: b4 0.15-dev X-Developer-Signature: v=1; a=openpgp-sha256; l=7204; i=a.hindborg@kernel.org; h=from:subject:message-id; bh=441eDv2wC96HCfe5soUKHsxfOl00Pmg21RsWhzUsuHQ=; b=owEBbQKS/ZANAwAIAeG4Gj55KGN3AcsmYgBo0+ahlUZdtNKeJKOXzgZN3KTmCZySuvFVa33Ma K5Y+Nsl0Z2JAjMEAAEIAB0WIQQSwflHVr98KhXWwBLhuBo+eShjdwUCaNPmoQAKCRDhuBo+eShj d0wGD/97NU4IWF4HCe2UmD61Z1FwhW2q63ctOd4+BaG8jPN1m/EB+FiUxfHerzNGSgY3IDan5mI LEVc9GxkajU8JCjSuZgg6fhwh+R9hKF3KIjYv7vAC4y2nBdBOgTpf7pW+VvWSGl3dGXYi3umLKN 0lGtNCzErLk63TgkxGULVjMahUIPdK0E8dlv46setnJaUzUPl3FW5nEb4LEw0J8xRjyxEyr1lDw R+hbazmXfERgEFqpAB+aFNmksMnycnKYjDdJpa/ofXf20nqCXg/DnCL/yR/VwlJQkLH8MrlCIF+ 1FuQBupkdigkP7Idb9qY2n5e4RxQreO6otU24EuJCigVBKVkzTKsHxRHA8fDX6gzU/jP77/ifmW pDr1JxQpABb+GLMSeodGnHtfhl2lClAvYZ+urHX7sbwC4xFIshOTRinnRYk6hOLPkkvGXFt+1BN TR4N1rBAcHdmI6WDY64MV0r1vTeTtqoVtjNoUnO/7q83aPaNX+5y7rD8U2C+++vQ298j/+f8ECu 0qj9s3dIKkmlI8KmPSjvBeaMq40AU1KshQJPjX3yN54UpgAjipRjdFboMtPcsIwf8CPABb96aKy 2CtPLjRg7FP4d+YTrWdbS8vMlk0z1mp8Vla5XdcE6fSb/eUgw4QdFymsbMKdtPmrOJos5hNjRQT FazRxki8sfeqapA== X-Developer-Key: i=a.hindborg@kernel.org; a=openpgp; fpr=3108C10F46872E248D1FB221376EB100563EF7A7 Add the trait `ParseInt` for parsing string representations of integers where the string representations are optionally prefixed by a radix specifier. Implement the trait for the primitive integer types. Suggested-by: Benno Lossin Tested-by: Daniel Gomez Reviewed-by: Greg Kroah-Hartman Reviewed-by: Benno Lossin Signed-off-by: Andreas Hindborg --- rust/kernel/str.rs | 2 + rust/kernel/str/parse_int.rs | 148 +++++++++++++++++++++++++++++++++++++++= ++++ 2 files changed, 150 insertions(+) diff --git a/rust/kernel/str.rs b/rust/kernel/str.rs index 6c892550c0ba9..23fe924070e7c 100644 --- a/rust/kernel/str.rs +++ b/rust/kernel/str.rs @@ -8,6 +8,8 @@ =20 use crate::prelude::*; =20 +pub mod parse_int; + /// Byte string without UTF-8 validity guarantee. #[repr(transparent)] pub struct BStr([u8]); diff --git a/rust/kernel/str/parse_int.rs b/rust/kernel/str/parse_int.rs new file mode 100644 index 0000000000000..48eb4c202984c --- /dev/null +++ b/rust/kernel/str/parse_int.rs @@ -0,0 +1,148 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! Integer parsing functions. +//! +//! Integer parsing functions for parsing signed and unsigned integers +//! potentially prefixed with `0x`, `0o`, or `0b`. + +use crate::prelude::*; +use crate::str::BStr; +use core::ops::Deref; + +// Make `FromStrRadix` a public type with a private name. This seals +// `ParseInt`, that is, prevents downstream users from implementing the +// trait. +mod private { + use crate::prelude::*; + use crate::str::BStr; + + /// Trait that allows parsing a [`&BStr`] to an integer with a radix. + pub trait FromStrRadix: Sized { + /// Parse `src` to [`Self`] using radix `radix`. + fn from_str_radix(src: &BStr, radix: u32) -> Result; + + /// Tries to convert `value` into [`Self`] and negates the resulti= ng value. + fn from_u64_negated(value: u64) -> Result; + } +} + +/// Extract the radix from an integer literal optionally prefixed with +/// one of `0x`, `0X`, `0o`, `0O`, `0b`, `0B`, `0`. +fn strip_radix(src: &BStr) -> (u32, &BStr) { + match src.deref() { + [b'0', b'x' | b'X', rest @ ..] =3D> (16, rest.as_ref()), + [b'0', b'o' | b'O', rest @ ..] =3D> (8, rest.as_ref()), + [b'0', b'b' | b'B', rest @ ..] =3D> (2, rest.as_ref()), + // NOTE: We are including the leading zero to be able to parse + // literal `0` here. If we removed it as a radix prefix, we would + // not be able to parse `0`. + [b'0', ..] =3D> (8, src), + _ =3D> (10, src), + } +} + +/// Trait for parsing string representations of integers. +/// +/// Strings beginning with `0x`, `0o`, or `0b` are parsed as hex, octal, or +/// binary respectively. Strings beginning with `0` otherwise are parsed as +/// octal. Anything else is parsed as decimal. A leading `+` or `-` is also +/// permitted. Any string parsed by [`kstrtol()`] or [`kstrtoul()`] will be +/// successfully parsed. +/// +/// [`kstrtol()`]: https://docs.kernel.org/core-api/kernel-api.html#c.kstr= tol +/// [`kstrtoul()`]: https://docs.kernel.org/core-api/kernel-api.html#c.kst= rtoul +/// +/// # Examples +/// +/// ``` +/// # use kernel::str::parse_int::ParseInt; +/// # use kernel::b_str; +/// +/// assert_eq!(Ok(0u8), u8::from_str(b_str!("0"))); +/// +/// assert_eq!(Ok(0xa2u8), u8::from_str(b_str!("0xa2"))); +/// assert_eq!(Ok(-0xa2i32), i32::from_str(b_str!("-0xa2"))); +/// +/// assert_eq!(Ok(-0o57i8), i8::from_str(b_str!("-0o57"))); +/// assert_eq!(Ok(0o57i8), i8::from_str(b_str!("057"))); +/// +/// assert_eq!(Ok(0b1001i16), i16::from_str(b_str!("0b1001"))); +/// assert_eq!(Ok(-0b1001i16), i16::from_str(b_str!("-0b1001"))); +/// +/// assert_eq!(Ok(127i8), i8::from_str(b_str!("127"))); +/// assert!(i8::from_str(b_str!("128")).is_err()); +/// assert_eq!(Ok(-128i8), i8::from_str(b_str!("-128"))); +/// assert!(i8::from_str(b_str!("-129")).is_err()); +/// assert_eq!(Ok(255u8), u8::from_str(b_str!("255"))); +/// assert!(u8::from_str(b_str!("256")).is_err()); +/// ``` +pub trait ParseInt: private::FromStrRadix + TryFrom { + /// Parse a string according to the description in [`Self`]. + fn from_str(src: &BStr) -> Result { + match src.deref() { + [b'-', rest @ ..] =3D> { + let (radix, digits) =3D strip_radix(rest.as_ref()); + // 2's complement values range from -2^(b-1) to 2^(b-1)-1. + // So if we want to parse negative numbers as positive and + // later multiply by -1, we have to parse into a larger + // integer. We choose `u64` as sufficiently large. + // + // NOTE: 128 bit integers are not available on all + // platforms, hence the choice of 64 bits. + let val =3D + u64::from_str_radix(core::str::from_utf8(digits).map_e= rr(|_| EINVAL)?, radix) + .map_err(|_| EINVAL)?; + Self::from_u64_negated(val) + } + _ =3D> { + let (radix, digits) =3D strip_radix(src); + Self::from_str_radix(digits, radix).map_err(|_| EINVAL) + } + } + } +} + +macro_rules! impl_parse_int { + ($($ty:ty),*) =3D> { + $( + impl private::FromStrRadix for $ty { + fn from_str_radix(src: &BStr, radix: u32) -> Result { + <$ty>::from_str_radix(core::str::from_utf8(src).map_er= r(|_| EINVAL)?, radix) + .map_err(|_| EINVAL) + } + + fn from_u64_negated(value: u64) -> Result { + const ABS_MIN: u64 =3D { + #[allow(unused_comparisons)] + if <$ty>::MIN < 0 { + 1u64 << (<$ty>::BITS - 1) + } else { + 0 + } + }; + + if value > ABS_MIN { + return Err(EINVAL); + } + + if value =3D=3D ABS_MIN { + return Ok(<$ty>::MIN); + } + + // SAFETY: The above checks guarantee that `value` fit= s into `Self`: + // - if `Self` is unsigned, then `ABS_MIN =3D=3D 0` an= d thus we have returned above + // (either `EINVAL` or `MIN`). + // - if `Self` is signed, then we have that `0 <=3D va= lue < ABS_MIN`. And since + // `ABS_MIN - 1` fits into `Self` by construction, `= value` also does. + let value: Self =3D unsafe { value.try_into().unwrap_u= nchecked() }; + + Ok((!value).wrapping_add(1)) + } + } + + impl ParseInt for $ty {} + )* + }; +} + +impl_parse_int![i8, u8, i16, u16, i32, u32, i64, u64, isize, usize]; --=20 2.47.2 From nobody Thu Oct 2 00:46:13 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3BA892417C2; Wed, 24 Sep 2025 12:40:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758717630; cv=none; b=mBv1vaGUwAuPbiDpbCp56WwvY4wBKukHPr1ht0zQYGupT503rw/317QjpI52nHrKGGRu1Tw7fhaF+f4QjjsfSoQwxPT3tecfG+ZWySkMQPebpoTlo3oAnrTtY7gpvXQCKSGF479RTuZnUS7Wu6iR7Tn8cKdO74EeaU1LFwtoq+U= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758717630; c=relaxed/simple; bh=/z6qk8wppG3LqmQtwIDcJIctq6LUeuvVFHKI96Laa8M=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=TJwa4KOr0rUT3Er2qvda9QZvsgUXKeaERRvlfW46sXDWrts+/Vgm/upDl5/4Y50V0RomFiorMeSOHRMpXpBU52SdIz6hEsHpVLzVRu1W0FOMFbFzXKcyOiAt7ZVdLpVkgA7wgRfTddXxgG5lfc8Mdpw/PRLzz0jI9gMOGrKM1kI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=LO329IXf; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="LO329IXf" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9C4A1C116B1; Wed, 24 Sep 2025 12:40:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1758717629; bh=/z6qk8wppG3LqmQtwIDcJIctq6LUeuvVFHKI96Laa8M=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=LO329IXfHcUh5Vmz/im9h/Uz4lHE3OssTeuG1N6o3gDpEHpSznfEIVUWSj/T8gXe2 QIeN6niqlQNK7Cad6o0CNKlkgbDCwajXsV/EhjEVdYdsBhWx1foA1o43LAqJmlczYb +Dxl1mQRv1dfIJF2Mmu1aNcFOVKNqPe/hEoNcecintTfleGhO5QS1u5g3kr7aUlABF BON2bBJKTUEpOcw2hspK7qTrML566r8FIrBwsQAzBrA/Dv++F6pTNgXyGxcRjYJrD7 TFDGJ1uORe9myz87BLUKpjhbbYAuJ0bTPJzjuvt362mlouE5ZcD5rzrU2e8AvYz7v1 /8yejxsdp04rA== From: Andreas Hindborg Date: Wed, 24 Sep 2025 14:39:26 +0200 Subject: [PATCH v18 3/7] rust: introduce module_param module Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250924-module-params-v3-v18-3-bf512c35d910@kernel.org> References: <20250924-module-params-v3-v18-0-bf512c35d910@kernel.org> In-Reply-To: <20250924-module-params-v3-v18-0-bf512c35d910@kernel.org> To: Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Alice Ryhl , Masahiro Yamada , Nathan Chancellor , Luis Chamberlain , Danilo Krummrich , Benno Lossin , Daniel Gomez , Benno Lossin , Nicolas Schier Cc: Trevor Gross , Adam Bratschi-Kaye , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kbuild@vger.kernel.org, Petr Pavlu , Sami Tolvanen , Daniel Gomez , Simona Vetter , Greg KH , Fiona Behrens , Daniel Almeida , linux-modules@vger.kernel.org, Andreas Hindborg X-Mailer: b4 0.15-dev X-Developer-Signature: v=1; a=openpgp-sha256; l=6947; i=a.hindborg@kernel.org; h=from:subject:message-id; bh=/z6qk8wppG3LqmQtwIDcJIctq6LUeuvVFHKI96Laa8M=; b=owEBbQKS/ZANAwAIAeG4Gj55KGN3AcsmYgBo0+aiACwI+EPgFWp40UARvh6ZjpPWgNPpjWm2z DOpX+ox73WJAjMEAAEIAB0WIQQSwflHVr98KhXWwBLhuBo+eShjdwUCaNPmogAKCRDhuBo+eShj d39GD/9obLV5ujuL+Cs35TYYlrxvlm/F/mY/4KxLUaEBcqhx3La1jMfq67qHi8N2jcS/00/65Ba h00DICLCrhzPFOaR0jz9k0hHtvuhFCRTnzlKhXE465FzS4JlA5SLP3LkTYL1zseP/RghrujEehD Q/bZTppWF7VsUAKLle9TT/TJfF4KS5WA6m6mPpR4QL325PxmtmHDUS5QT9LpfrlTbZrSwjOi7fS T2e5Lyn+ACOvFbT8vTlckuvwpfBSfb7eiU/D3UuyVFePKFJ/iM1JWVPp4dcoxtmrpgc8tgSXyGy ENEXTIIVSnuax+kgcozue+Y7c/6nn5/irescl10ZoAZezjx3gaJzQ1ZuOvw47FWofyF+Lcsc6yk tkmGe+lkzSeqdqPiNQUviw3zpiirVo8TtT0SqrVNhVSGI+U7BZKVbVHPqjsHVE+vSlDi8j5FuCE wDGgx6Dj8xgrFfCNZ+zAP827egEHRQi0wZKKDNt2gl1zVsY9V3WN3QuLPnQJrskrBw7l/FO9lhp KICA0MAkwpV2vRmckjTg+Yr9Fu9FHkUAqQRAZG7hoQeAxXh77VY5irpIuhZ82/itVB3/qWEkK1h bxJ9ATncdCZ1AQhd6aDD/1jZlMrnpfO04KEZLJRdBJDMGQCF+OyMIrBL7YUZj1iIVEvS5oNHgTd ANsNKIQEiTptWOQ== X-Developer-Key: i=a.hindborg@kernel.org; a=openpgp; fpr=3108C10F46872E248D1FB221376EB100563EF7A7 Add types and traits for interfacing the C moduleparam API. Reviewed-by: Benno Lossin Signed-off-by: Andreas Hindborg --- rust/kernel/lib.rs | 1 + rust/kernel/module_param.rs | 181 ++++++++++++++++++++++++++++++++++++++++= ++++ 2 files changed, 182 insertions(+) diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs index fef97f2a50984..e34b377d93b2e 100644 --- a/rust/kernel/lib.rs +++ b/rust/kernel/lib.rs @@ -98,6 +98,7 @@ pub mod list; pub mod miscdevice; pub mod mm; +pub mod module_param; #[cfg(CONFIG_NET)] pub mod net; pub mod of; diff --git a/rust/kernel/module_param.rs b/rust/kernel/module_param.rs new file mode 100644 index 0000000000000..e7d5c930a467d --- /dev/null +++ b/rust/kernel/module_param.rs @@ -0,0 +1,181 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! Support for module parameters. +//! +//! C header: [`include/linux/moduleparam.h`](srctree/include/linux/module= param.h) + +use crate::prelude::*; +use crate::str::BStr; +use bindings; +use kernel::sync::SetOnce; + +/// Newtype to make `bindings::kernel_param` [`Sync`]. +#[repr(transparent)] +#[doc(hidden)] +pub struct KernelParam(bindings::kernel_param); + +impl KernelParam { + #[doc(hidden)] + pub const fn new(val: bindings::kernel_param) -> Self { + Self(val) + } +} + +// SAFETY: C kernel handles serializing access to this type. We never acce= ss it +// from Rust module. +unsafe impl Sync for KernelParam {} + +/// Types that can be used for module parameters. +// NOTE: This trait is `Copy` because drop could produce unsoundness durin= g teardown. +pub trait ModuleParam: Sized + Copy { + /// Parse a parameter argument into the parameter value. + fn try_from_param_arg(arg: &BStr) -> Result; +} + +/// Set the module parameter from a string. +/// +/// Used to set the parameter value at kernel initialization, when loading +/// the module or when set through `sysfs`. +/// +/// See `struct kernel_param_ops.set`. +/// +/// # Safety +/// +/// - If `val` is non-null then it must point to a valid null-terminated s= tring that must be valid +/// for reads for the duration of the call. +/// - `param` must be a pointer to a `bindings::kernel_param` initialized = by the rust module macro. +/// The pointee must be valid for reads for the duration of the call. +/// +/// # Note +/// +/// - The safety requirements are satisfied by C API contract when this fu= nction is invoked by the +/// module subsystem C code. +/// - Currently, we only support read-only parameters that are not readabl= e from `sysfs`. Thus, this +/// function is only called at kernel initialization time, or at module = load time, and we have +/// exclusive access to the parameter for the duration of the function. +/// +/// [`module!`]: macros::module +unsafe extern "C" fn set_param(val: *const c_char, param: *const bindin= gs::kernel_param) -> c_int +where + T: ModuleParam, +{ + // NOTE: If we start supporting arguments without values, val _is_ all= owed + // to be null here. + if val.is_null() { + // TODO: Use pr_warn_once available. + crate::pr_warn!("Null pointer passed to `module_param::set_param`"= ); + return EINVAL.to_errno(); + } + + // SAFETY: By function safety requirement, val is non-null, null-termi= nated + // and valid for reads for the duration of this function. + let arg =3D unsafe { CStr::from_char_ptr(val) }; + + crate::error::from_result(|| { + let new_value =3D T::try_from_param_arg(arg)?; + + // SAFETY: By function safety requirements, this access is safe. + let container =3D unsafe { &*((*param).__bindgen_anon_1.arg.cast::= >()) }; + + container + .populate(new_value) + .then_some(0) + .ok_or(kernel::error::code::EEXIST) + }) +} + +macro_rules! impl_int_module_param { + ($ty:ident) =3D> { + impl ModuleParam for $ty { + fn try_from_param_arg(arg: &BStr) -> Result { + <$ty as crate::str::parse_int::ParseInt>::from_str(arg) + } + } + }; +} + +impl_int_module_param!(i8); +impl_int_module_param!(u8); +impl_int_module_param!(i16); +impl_int_module_param!(u16); +impl_int_module_param!(i32); +impl_int_module_param!(u32); +impl_int_module_param!(i64); +impl_int_module_param!(u64); +impl_int_module_param!(isize); +impl_int_module_param!(usize); + +/// A wrapper for kernel parameters. +/// +/// This type is instantiated by the [`module!`] macro when module paramet= ers are +/// defined. You should never need to instantiate this type directly. +/// +/// Note: This type is `pub` because it is used by module crates to access +/// parameter values. +pub struct ModuleParamAccess { + value: SetOnce, + default: T, +} + +// SAFETY: We only create shared references to the contents of this contai= ner, +// so if `T` is `Sync`, so is `ModuleParamAccess`. +unsafe impl Sync for ModuleParamAccess {} + +impl ModuleParamAccess { + #[doc(hidden)] + pub const fn new(default: T) -> Self { + Self { + value: SetOnce::new(), + default, + } + } + + /// Get a shared reference to the parameter value. + // Note: When sysfs access to parameters are enabled, we have to pass = in a + // held lock guard here. + pub fn value(&self) -> &T { + self.value.as_ref().unwrap_or(&self.default) + } + + /// Get a mutable pointer to `self`. + /// + /// NOTE: In most cases it is not safe deref the returned pointer. + pub const fn as_void_ptr(&self) -> *mut c_void { + core::ptr::from_ref(self).cast_mut().cast() + } +} + +#[doc(hidden)] +/// Generate a static [`kernel_param_ops`](srctree/include/linux/modulepar= am.h) struct. +/// +/// # Examples +/// +/// ```ignore +/// make_param_ops!( +/// /// Documentation for new param ops. +/// PARAM_OPS_MYTYPE, // Name for the static. +/// MyType // A type which implements [`ModuleParam`]. +/// ); +/// ``` +macro_rules! make_param_ops { + ($ops:ident, $ty:ty) =3D> { + #[doc(hidden)] + pub static $ops: $crate::bindings::kernel_param_ops =3D $crate::bi= ndings::kernel_param_ops { + flags: 0, + set: Some(set_param::<$ty>), + get: None, + free: None, + }; + }; +} + +make_param_ops!(PARAM_OPS_I8, i8); +make_param_ops!(PARAM_OPS_U8, u8); +make_param_ops!(PARAM_OPS_I16, i16); +make_param_ops!(PARAM_OPS_U16, u16); +make_param_ops!(PARAM_OPS_I32, i32); +make_param_ops!(PARAM_OPS_U32, u32); +make_param_ops!(PARAM_OPS_I64, i64); +make_param_ops!(PARAM_OPS_U64, u64); +make_param_ops!(PARAM_OPS_ISIZE, isize); +make_param_ops!(PARAM_OPS_USIZE, usize); --=20 2.47.2 From nobody Thu Oct 2 00:46:13 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D4647304BC5; Wed, 24 Sep 2025 12:40:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758717656; cv=none; b=ky1i3k8FmqalmXcm5xYqZyf1MjYsD0ZHiCwLlQq1thlhMIPL+m4D0aTq+q+2gINYDjXJU/N1SLQHoc1m6F6d/318vtVjTm+ty/JuKyFZ7jl4NGgflhtosMM4+rUiv57WrlMJxs8PjKkOZcjNl6Q8+sGDEx80vOg1UdDe86Nm8ks= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758717656; c=relaxed/simple; bh=Pb8ap8QGaO6wH5qrd6PDiOf6/mTWyw1xHoJQ58CimIo=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=W2Liuv7IkXmYn37zlsj3d7khDmdc0I2fmPBgWDDzinc/CAm5/vg6KyDEYosdeOBK0IwrchRgmdOhsUnds9QGZkDb3iHNnqQTygzccv/J8VA/tMePxTs19ZGvtoeeBfIZ0ZrCtByRnnGoJbaWwtOz3H21mwLTmpGX+K7tJ3c0Nyw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=PNXfbk7X; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="PNXfbk7X" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 176D4C4CEF7; Wed, 24 Sep 2025 12:40:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1758717653; bh=Pb8ap8QGaO6wH5qrd6PDiOf6/mTWyw1xHoJQ58CimIo=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=PNXfbk7XxgWHtthB5Go8gJcgX7hdHZQzqrvsrBvw/BkKNR1NTPj8cPhm+r23Ojz7g 5PShhw5ZNKEP7wXdeUPeUR+jzjJ1qNhZUkGcT+gKgj7hBqQRiEIWFEoDKtgF8gJSQa sF8aebmYKrm9i+WiZOef+OSXHfLXy29bLQTn6YedS+lKW5JcKP6Z5YHdk561wJ6goD quZtLicnur0PaaOlQ9oV5KHXk/WXYp7dbkjYfhVc14qL4frjZR0UqbX9zNz5i5ycTk 6ThWDu213xwDs4OfzN/kvus8k3EJAiQlOZJglgB6T2WuAIGJSVslCPjZ4ETaOm8mVe LAMsO6p5E6qzQ== From: Andreas Hindborg Date: Wed, 24 Sep 2025 14:39:27 +0200 Subject: [PATCH v18 4/7] rust: module: use a reference in macros::module::module Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250924-module-params-v3-v18-4-bf512c35d910@kernel.org> References: <20250924-module-params-v3-v18-0-bf512c35d910@kernel.org> In-Reply-To: <20250924-module-params-v3-v18-0-bf512c35d910@kernel.org> To: Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Alice Ryhl , Masahiro Yamada , Nathan Chancellor , Luis Chamberlain , Danilo Krummrich , Benno Lossin , Daniel Gomez , Benno Lossin , Nicolas Schier Cc: Trevor Gross , Adam Bratschi-Kaye , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kbuild@vger.kernel.org, Petr Pavlu , Sami Tolvanen , Daniel Gomez , Simona Vetter , Greg KH , Fiona Behrens , Daniel Almeida , linux-modules@vger.kernel.org, Andreas Hindborg X-Mailer: b4 0.15-dev X-Developer-Signature: v=1; a=openpgp-sha256; l=1881; i=a.hindborg@kernel.org; h=from:subject:message-id; bh=Pb8ap8QGaO6wH5qrd6PDiOf6/mTWyw1xHoJQ58CimIo=; b=owEBbQKS/ZANAwAIAeG4Gj55KGN3AcsmYgBo0+ajvkInFRzIac1I/YET1HASfp2sVjEOtKmcs 4gQI0Q1KfuJAjMEAAEIAB0WIQQSwflHVr98KhXWwBLhuBo+eShjdwUCaNPmowAKCRDhuBo+eShj dy5VEACv/PPyPlCuSJhbz4c+w4awb+FCGt2u72DDNduf9av+v/r3Mk3gsMZ59jEzZbCPzSylKPY 5GJSMFn1DCfeKAv1W1hGF0yfX05mezZxCSpNSTnARjs1R3t5zcXevsz5J88KttI+TyZkL9j8Qw7 gFT/7VQeKGvJji7bnEGqKdv8x6KEktp/DtBeU/pxncIWaGh9gPc/KN+K9ax3AkE93UxcUBCEKNN gkAvvxf1TWXtnCSADaaIYRsSfO9fMRhXiO4OSJ5qs9xQP7UEOPoE6VhOVfZxjVf011TdlkA3++1 QKHpsE5yQXz2eIhvJ/N3uXP/gIYoQPoyNqYhuCVA4DmRRWU2jHF0Le1h+kxc4fuKS9fKxUs1QSE cOp7G9qyC3ivEF3+HHLB4ME0CJsrAYijdthm5guNyhRrJowM2FI5Xv6LTuMcoaqLAjbG8ijclsS u79wBUXOS2amCR6PL+pYpqWsn8l60dP9xUOI1fujWk9Ygnie5i2tSd8z19izoj5QheSWUWHsnVg omzd/0FUFR5Wm1LuXvACLo4DN8nWmduGD5knXaMXvfcnZKOWn0CRyu2q36DDCA8qFl0DG2oPhwp ogfa83V9r7LVmcA79jbFayJEKCoNEMye8C++vxE7fG3A5phxprEfFyZfvDPNasygpO4xrrRNcdB U+w94dMd38uaLqw== X-Developer-Key: i=a.hindborg@kernel.org; a=openpgp; fpr=3108C10F46872E248D1FB221376EB100563EF7A7 When we add parameter support to the module macro, we want to be able to pass a reference to `ModuleInfo` to a helper function. That is not possible when we move out of the local `modinfo`. So change the function to access the local via reference rather than value. Reviewed-by: Benno Lossin Signed-off-by: Andreas Hindborg --- rust/macros/module.rs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/rust/macros/module.rs b/rust/macros/module.rs index 5ee54a00c0b65..cbf3ac0a8f7ba 100644 --- a/rust/macros/module.rs +++ b/rust/macros/module.rs @@ -176,23 +176,23 @@ pub(crate) fn module(ts: TokenStream) -> TokenStream { // Rust does not allow hyphens in identifiers, use underscore instead. let ident =3D info.name.replace('-', "_"); let mut modinfo =3D ModInfoBuilder::new(ident.as_ref()); - if let Some(authors) =3D info.authors { + if let Some(authors) =3D &info.authors { for author in authors { - modinfo.emit("author", &author); + modinfo.emit("author", author); } } - if let Some(description) =3D info.description { - modinfo.emit("description", &description); + if let Some(description) =3D &info.description { + modinfo.emit("description", description); } modinfo.emit("license", &info.license); - if let Some(aliases) =3D info.alias { + if let Some(aliases) =3D &info.alias { for alias in aliases { - modinfo.emit("alias", &alias); + modinfo.emit("alias", alias); } } - if let Some(firmware) =3D info.firmware { + if let Some(firmware) =3D &info.firmware { for fw in firmware { - modinfo.emit("firmware", &fw); + modinfo.emit("firmware", fw); } } =20 --=20 2.47.2 From nobody Thu Oct 2 00:46:13 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 417A8303CAA; Wed, 24 Sep 2025 12:40:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758717636; cv=none; b=MXHNMrukjY0vPpYNj/IQk4PfwL3o9IfT78wsuPuv9MP1ez/PlJ/ZqOAdNcV8HMP4SAcJYfuqHwNTQr2fLpxbWL92b3SsxJ4gYjSKS5QJWp32E6C6Ay7h6+GI3J6VbyjeUBXEHensHZSFRAj9qrAJLlVRcRWa9Itvp8brfFDtF9g= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758717636; c=relaxed/simple; bh=f0aezQP+lTTBaLYyzhKAQG36mNqIE1d7LdhlwAQjbjo=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=mCQrvVgIUwK7dv6TCiSaL7OXnXjtGYMU29jfLuMmfLde/OJAKqKXlrQLzoNHPrDCodpWiV/4yOKnWDfQXc/TYciJiorC42q4tEsOseFNWpdefeYoub4qyoP5OE2b+Ri6OJ2Yq2jyAhz4e7X4bd+zMRF0rmwrlKriEbEij7mcDzw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Ole11jZn; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="Ole11jZn" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 74244C4CEE7; Wed, 24 Sep 2025 12:40:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1758717635; bh=f0aezQP+lTTBaLYyzhKAQG36mNqIE1d7LdhlwAQjbjo=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=Ole11jZnZlaiL1G20R6P5t5vIg4Jt2gCoYgOHMIoAuEuQ2+/t0US0KRmyqsoHS32X D0rqtm7N5ymemF286CZzCKMCmJIdcxgHKonPXMepFXDZT8APL1bNqb7H/mpbe3nNWR c6UVE1x2herlRaxFmZjN34axKJCQi9yngXYA7JG2gikXAk2P9vhetUELcTPJbZQ+/B XeKS2TRE/MgJYulHM95kDREUj4HVt0UZUNCJEH6/1F+mMi9sTHsIALJ2N879WBqCGn q71odUd9zhylTXruZuAFad3uu5l02YBkrNvH+UB9eD6IJ81tzUumnzCXAIzZ5gJdjC Njt4mMpkbUuYw== From: Andreas Hindborg Date: Wed, 24 Sep 2025 14:39:28 +0200 Subject: [PATCH v18 5/7] rust: module: update the module macro with module parameter support Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250924-module-params-v3-v18-5-bf512c35d910@kernel.org> References: <20250924-module-params-v3-v18-0-bf512c35d910@kernel.org> In-Reply-To: <20250924-module-params-v3-v18-0-bf512c35d910@kernel.org> To: Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Alice Ryhl , Masahiro Yamada , Nathan Chancellor , Luis Chamberlain , Danilo Krummrich , Benno Lossin , Daniel Gomez , Benno Lossin , Nicolas Schier Cc: Trevor Gross , Adam Bratschi-Kaye , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kbuild@vger.kernel.org, Petr Pavlu , Sami Tolvanen , Daniel Gomez , Simona Vetter , Greg KH , Fiona Behrens , Daniel Almeida , linux-modules@vger.kernel.org, Andreas Hindborg X-Mailer: b4 0.15-dev X-Developer-Signature: v=1; a=openpgp-sha256; l=13520; i=a.hindborg@kernel.org; h=from:subject:message-id; bh=f0aezQP+lTTBaLYyzhKAQG36mNqIE1d7LdhlwAQjbjo=; b=owEBbQKS/ZANAwAIAeG4Gj55KGN3AcsmYgBo0+akj/GBJit09OPJq5euH1Xeio+bC4AC+37D0 d2TqZZsZ9mJAjMEAAEIAB0WIQQSwflHVr98KhXWwBLhuBo+eShjdwUCaNPmpAAKCRDhuBo+eShj d0r/D/9E1ASOq4KkX8os3CYf0a0KwkmwYF96boJzKBXuWQU1UMhXD6JvYwu8PpQbL68nNUiTOTo m9IA3WOuUBPrSloHjVQwNdzLmiZb/8ROOCcUW/5bm7zNT5XgOIsjVT8uQHp9+oGnsRLGJV/u7Xu juxzOOevcA1RX6vMjhtMKg8xs0K92qyaXLYnZVH96cEqiXwG8ZBL64ewyoZIYgmtsa3/HZzgFio p4nOFUiaodKeR5Pm+RdHIKidIC82vZVLxqzqKaWgEzE/vonoNkixXHtj1dQwk5y+J4XOuzlyORK /4JHGS44VTnkkwh7uwIZ6yzrUmNyVrytZtNtYffMb4xk2iRmv5ehUeM4Mm2Es+KLil4oMnAJAis mQ/4Jmdxfu3YUdCBhh60mdKMicJQUidaDEmf8nwGhYRr4pcCGllNjkSpXw2rcTzkuRdnAIh/NbC TgZMfySh265+7rkKm/rkq5CP2Wt3RM7D2Zh6NsZ4ydvSUl2VuA/iGHJuA3gVfKP5KtZdF7KVapj lYKPgrt8LY8EaEbWAczmUKXlQa2mX+tr0B6exRB9bHnbZApQtUbLx2RK7UJiphC80qyGt0elKpe LW3J82WP2I4qSH+t6owaUQ3YQwuLiPzBD44p28JQdgOuP2PX1tURP1KaewYXNtUavi/GKXH4IXT qMzL46WWr7fGV3A== X-Developer-Key: i=a.hindborg@kernel.org; a=openpgp; fpr=3108C10F46872E248D1FB221376EB100563EF7A7 Allow module parameters to be declared in the rust `module!` macro. Reviewed-by: Benno Lossin Signed-off-by: Andreas Hindborg --- rust/macros/helpers.rs | 25 +++++++ rust/macros/lib.rs | 31 +++++++++ rust/macros/module.rs | 178 +++++++++++++++++++++++++++++++++++++++++++++= +--- 3 files changed, 224 insertions(+), 10 deletions(-) diff --git a/rust/macros/helpers.rs b/rust/macros/helpers.rs index e2602be402c10..365d7eb499c08 100644 --- a/rust/macros/helpers.rs +++ b/rust/macros/helpers.rs @@ -10,6 +10,17 @@ pub(crate) fn try_ident(it: &mut token_stream::IntoIter)= -> Option { } } =20 +pub(crate) fn try_sign(it: &mut token_stream::IntoIter) -> Option { + let peek =3D it.clone().next(); + match peek { + Some(TokenTree::Punct(punct)) if punct.as_char() =3D=3D '-' =3D> { + let _ =3D it.next(); + Some(punct.as_char()) + } + _ =3D> None, + } +} + pub(crate) fn try_literal(it: &mut token_stream::IntoIter) -> Option { if let Some(TokenTree::Literal(literal)) =3D it.next() { Some(literal.to_string()) @@ -103,3 +114,17 @@ pub(crate) fn file() -> String { proc_macro::Span::call_site().file() } } + +/// Parse a token stream of the form `expected_name: "value",` and return = the +/// string in the position of "value". +/// +/// # Panics +/// +/// - On parse error. +pub(crate) fn expect_string_field(it: &mut token_stream::IntoIter, expecte= d_name: &str) -> String { + assert_eq!(expect_ident(it), expected_name); + assert_eq!(expect_punct(it), ':'); + let string =3D expect_string(it); + assert_eq!(expect_punct(it), ','); + string +} diff --git a/rust/macros/lib.rs b/rust/macros/lib.rs index fa847cf3a9b5f..2fb520dc930af 100644 --- a/rust/macros/lib.rs +++ b/rust/macros/lib.rs @@ -28,6 +28,30 @@ /// The `type` argument should be a type which implements the [`Module`] /// trait. Also accepts various forms of kernel metadata. /// +/// The `params` field describe module parameters. Each entry has the form +/// +/// ```ignore +/// parameter_name: type { +/// default: default_value, +/// description: "Description", +/// } +/// ``` +/// +/// `type` may be one of +/// +/// - [`i8`] +/// - [`u8`] +/// - [`i8`] +/// - [`u8`] +/// - [`i16`] +/// - [`u16`] +/// - [`i32`] +/// - [`u32`] +/// - [`i64`] +/// - [`u64`] +/// - [`isize`] +/// - [`usize`] +/// /// C header: [`include/linux/moduleparam.h`](srctree/include/linux/module= param.h) /// /// [`Module`]: ../kernel/trait.Module.html @@ -44,6 +68,12 @@ /// description: "My very own kernel module!", /// license: "GPL", /// alias: ["alternate_module_name"], +/// params: { +/// my_parameter: i64 { +/// default: 1, +/// description: "This parameter has a default of 1", +/// }, +/// }, /// } /// /// struct MyModule(i32); @@ -52,6 +82,7 @@ /// fn init(_module: &'static ThisModule) -> Result { /// let foo: i32 =3D 42; /// pr_info!("I contain: {}\n", foo); +/// pr_info!("i32 param is: {}\n", module_parameters::my_paramete= r.read()); /// Ok(Self(foo)) /// } /// } diff --git a/rust/macros/module.rs b/rust/macros/module.rs index cbf3ac0a8f7ba..d62e9c1e2a898 100644 --- a/rust/macros/module.rs +++ b/rust/macros/module.rs @@ -26,6 +26,7 @@ struct ModInfoBuilder<'a> { module: &'a str, counter: usize, buffer: String, + param_buffer: String, } =20 impl<'a> ModInfoBuilder<'a> { @@ -34,10 +35,11 @@ fn new(module: &'a str) -> Self { module, counter: 0, buffer: String::new(), + param_buffer: String::new(), } } =20 - fn emit_base(&mut self, field: &str, content: &str, builtin: bool) { + fn emit_base(&mut self, field: &str, content: &str, builtin: bool, par= am: bool) { let string =3D if builtin { // Built-in modules prefix their modinfo strings by `module.`. format!( @@ -51,8 +53,14 @@ fn emit_base(&mut self, field: &str, content: &str, buil= tin: bool) { format!("{field}=3D{content}\0") }; =20 + let buffer =3D if param { + &mut self.param_buffer + } else { + &mut self.buffer + }; + write!( - &mut self.buffer, + buffer, " {cfg} #[doc(hidden)] @@ -75,20 +83,119 @@ fn emit_base(&mut self, field: &str, content: &str, bu= iltin: bool) { self.counter +=3D 1; } =20 - fn emit_only_builtin(&mut self, field: &str, content: &str) { - self.emit_base(field, content, true) + fn emit_only_builtin(&mut self, field: &str, content: &str, param: boo= l) { + self.emit_base(field, content, true, param) } =20 - fn emit_only_loadable(&mut self, field: &str, content: &str) { - self.emit_base(field, content, false) + fn emit_only_loadable(&mut self, field: &str, content: &str, param: bo= ol) { + self.emit_base(field, content, false, param) } =20 fn emit(&mut self, field: &str, content: &str) { - self.emit_only_builtin(field, content); - self.emit_only_loadable(field, content); + self.emit_internal(field, content, false); + } + + fn emit_internal(&mut self, field: &str, content: &str, param: bool) { + self.emit_only_builtin(field, content, param); + self.emit_only_loadable(field, content, param); + } + + fn emit_param(&mut self, field: &str, param: &str, content: &str) { + let content =3D format!("{param}:{content}", param =3D param, cont= ent =3D content); + self.emit_internal(field, &content, true); + } + + fn emit_params(&mut self, info: &ModuleInfo) { + let Some(params) =3D &info.params else { + return; + }; + + for param in params { + let ops =3D param_ops_path(¶m.ptype); + + // Note: The spelling of these fields is dictated by the user = space + // tool `modinfo`. + self.emit_param("parmtype", ¶m.name, ¶m.ptype); + self.emit_param("parm", ¶m.name, ¶m.description); + + write!( + self.param_buffer, + " + pub(crate) static {param_name}: + ::kernel::module_param::ModuleParamAccess<{param_type}= > =3D + ::kernel::module_param::ModuleParamAccess::new({pa= ram_default}); + + const _: () =3D {{ + #[link_section =3D \"__param\"] + #[used] + static __{module_name}_{param_name}_struct: + ::kernel::module_param::KernelParam =3D + ::kernel::module_param::KernelParam::new( + ::kernel::bindings::kernel_param {{ + name: if ::core::cfg!(MODULE) {{ + ::kernel::c_str!(\"{param_name}\").as_= bytes_with_nul() + }} else {{ + ::kernel::c_str!(\"{module_name}.{para= m_name}\") + .as_bytes_with_nul() + }}.as_ptr(), + // SAFETY: `__this_module` is constructed = by the kernel at load + // time and will not be freed until the mo= dule is unloaded. + #[cfg(MODULE)] + mod_: unsafe {{ + core::ptr::from_ref(&::kernel::binding= s::__this_module) + .cast_mut() + }}, + #[cfg(not(MODULE))] + mod_: ::core::ptr::null_mut(), + ops: core::ptr::from_ref(&{ops}), + perm: 0, // Will not appear in sysfs + level: -1, + flags: 0, + __bindgen_anon_1: ::kernel::bindings::kern= el_param__bindgen_ty_1 {{ + arg: {param_name}.as_void_ptr() + }}, + }} + ); + }}; + ", + module_name =3D info.name, + param_type =3D param.ptype, + param_default =3D param.default, + param_name =3D param.name, + ops =3D ops, + ) + .unwrap(); + } + } +} + +fn param_ops_path(param_type: &str) -> &'static str { + match param_type { + "i8" =3D> "::kernel::module_param::PARAM_OPS_I8", + "u8" =3D> "::kernel::module_param::PARAM_OPS_U8", + "i16" =3D> "::kernel::module_param::PARAM_OPS_I16", + "u16" =3D> "::kernel::module_param::PARAM_OPS_U16", + "i32" =3D> "::kernel::module_param::PARAM_OPS_I32", + "u32" =3D> "::kernel::module_param::PARAM_OPS_U32", + "i64" =3D> "::kernel::module_param::PARAM_OPS_I64", + "u64" =3D> "::kernel::module_param::PARAM_OPS_U64", + "isize" =3D> "::kernel::module_param::PARAM_OPS_ISIZE", + "usize" =3D> "::kernel::module_param::PARAM_OPS_USIZE", + t =3D> panic!("Unsupported parameter type {}", t), } } =20 +fn expect_param_default(param_it: &mut token_stream::IntoIter) -> String { + assert_eq!(expect_ident(param_it), "default"); + assert_eq!(expect_punct(param_it), ':'); + let sign =3D try_sign(param_it); + let default =3D try_literal(param_it).expect("Expected default param v= alue"); + assert_eq!(expect_punct(param_it), ','); + let mut value =3D sign.map(String::from).unwrap_or_default(); + value.push_str(&default); + value +} + #[derive(Debug, Default)] struct ModuleInfo { type_: String, @@ -98,6 +205,50 @@ struct ModuleInfo { description: Option, alias: Option>, firmware: Option>, + params: Option>, +} + +#[derive(Debug)] +struct Parameter { + name: String, + ptype: String, + default: String, + description: String, +} + +fn expect_params(it: &mut token_stream::IntoIter) -> Vec { + let params =3D expect_group(it); + assert_eq!(params.delimiter(), Delimiter::Brace); + let mut it =3D params.stream().into_iter(); + let mut parsed =3D Vec::new(); + + loop { + let param_name =3D match it.next() { + Some(TokenTree::Ident(ident)) =3D> ident.to_string(), + Some(_) =3D> panic!("Expected Ident or end"), + None =3D> break, + }; + + assert_eq!(expect_punct(&mut it), ':'); + let param_type =3D expect_ident(&mut it); + let group =3D expect_group(&mut it); + assert_eq!(group.delimiter(), Delimiter::Brace); + assert_eq!(expect_punct(&mut it), ','); + + let mut param_it =3D group.stream().into_iter(); + let param_default =3D expect_param_default(&mut param_it); + let param_description =3D expect_string_field(&mut param_it, "desc= ription"); + expect_end(&mut param_it); + + parsed.push(Parameter { + name: param_name, + ptype: param_type, + default: param_default, + description: param_description, + }) + } + + parsed } =20 impl ModuleInfo { @@ -112,6 +263,7 @@ fn parse(it: &mut token_stream::IntoIter) -> Self { "license", "alias", "firmware", + "params", ]; const REQUIRED_KEYS: &[&str] =3D &["type", "name", "license"]; let mut seen_keys =3D Vec::new(); @@ -137,6 +289,7 @@ fn parse(it: &mut token_stream::IntoIter) -> Self { "license" =3D> info.license =3D expect_string_ascii(it), "alias" =3D> info.alias =3D Some(expect_string_array(it)), "firmware" =3D> info.firmware =3D Some(expect_string_array= (it)), + "params" =3D> info.params =3D Some(expect_params(it)), _ =3D> panic!("Unknown key \"{key}\". Valid keys are: {EXP= ECTED_KEYS:?}."), } =20 @@ -199,7 +352,9 @@ pub(crate) fn module(ts: TokenStream) -> TokenStream { // Built-in modules also export the `file` modinfo string. let file =3D std::env::var("RUST_MODFILE").expect("Unable to fetch RUST_MODFILE= environmental variable"); - modinfo.emit_only_builtin("file", &file); + modinfo.emit_only_builtin("file", &file, false); + + modinfo.emit_params(&info); =20 format!( " @@ -363,15 +518,18 @@ unsafe fn __exit() {{ __MOD.assume_init_drop(); }} }} - {modinfo} }} }} + mod module_parameters {{ + {params} + }} ", type_ =3D info.type_, name =3D info.name, ident =3D ident, modinfo =3D modinfo.buffer, + params =3D modinfo.param_buffer, initcall_section =3D ".initcall6.init" ) .parse() --=20 2.47.2 From nobody Thu Oct 2 00:46:13 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 79A2930AD14; Wed, 24 Sep 2025 12:40:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758717659; cv=none; b=P0ZtCI1+/91KKzqAzwPScupMcC+Ik5uT3ytxx+7K8OBf68qgxBpnylItd+cMVxDBUuq2RpykP5872PDpUonvgX9ZGKs6RGo9GNY387k4NegwYP8x2QqjDdvqYbawMzcZYqaTb6NAEqwUW9WDa4QHP6pTjnwAWeBHkPrHZSB+D98= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758717659; c=relaxed/simple; bh=4Rj9VzFcMjV2b2FKGGXgH+pSxDEnRIoZbc9nYVS6Nl0=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=Mfch3YoFVZzU1dFLA15Dlb4wZSH7k1qtiiKmbLvztLsyfR7tneBknon1URNpi4Rny7h4g+JiTSh6+m6n9kkTmad829S1ps1f/KEp0SPJgfspjZs786g0q+5aEo9Jpbp93tUA//J7b3RiLG6M24xM1c1woREqSnhxkS0k43GJ+CE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Wc7KqvJw; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="Wc7KqvJw" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 09B1FC116B1; Wed, 24 Sep 2025 12:40:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1758717659; bh=4Rj9VzFcMjV2b2FKGGXgH+pSxDEnRIoZbc9nYVS6Nl0=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=Wc7KqvJw3OV4yZ17T1MeouXrudZYg3zb2g1XG0HLPsWuCACupIp1gTe32Lbx/YG/l qayJM991xfQ3Wn1RcMNIZjYHiQ4Djhm0VkXLohTnOgdM6c0E90dUBpeeyK919YccEn lfr3oTlZsN3psTrPnWNRztB8RAVtV5N/lBzYytFRpBkuQCYlVIA8R9JQyJGcCaLZfB yGL1TfobWQvz3pv/3B5aEbxVCQEbcxPCl4UdHBCYfRSBl77hXHxDyH5DE9yZeBQt9D 6/wmeSBKx0XVtIuOe7kI4IjxeXZLLBffQhWZPOuTmuYkKbzPOPov7s8Xg04wv1oWvP lXyXoEaSIsIrw== From: Andreas Hindborg Date: Wed, 24 Sep 2025 14:39:29 +0200 Subject: [PATCH v18 6/7] rust: samples: add a module parameter to the rust_minimal sample Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250924-module-params-v3-v18-6-bf512c35d910@kernel.org> References: <20250924-module-params-v3-v18-0-bf512c35d910@kernel.org> In-Reply-To: <20250924-module-params-v3-v18-0-bf512c35d910@kernel.org> To: Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Alice Ryhl , Masahiro Yamada , Nathan Chancellor , Luis Chamberlain , Danilo Krummrich , Benno Lossin , Daniel Gomez , Benno Lossin , Nicolas Schier Cc: Trevor Gross , Adam Bratschi-Kaye , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kbuild@vger.kernel.org, Petr Pavlu , Sami Tolvanen , Daniel Gomez , Simona Vetter , Greg KH , Fiona Behrens , Daniel Almeida , linux-modules@vger.kernel.org, Andreas Hindborg X-Mailer: b4 0.15-dev X-Developer-Signature: v=1; a=openpgp-sha256; l=1268; i=a.hindborg@kernel.org; h=from:subject:message-id; bh=4Rj9VzFcMjV2b2FKGGXgH+pSxDEnRIoZbc9nYVS6Nl0=; b=kA0DAAgB4bgaPnkoY3cByyZiAGjT5qWieA70bPcE9KwDstNTPzEDSqzNcW/KKwEfNGGe5zFzV okCMwQAAQgAHRYhBBLB+UdWv3wqFdbAEuG4Gj55KGN3BQJo0+alAAoJEOG4Gj55KGN3du8P/0Pz 1Kg4YJN2UBkSyiK13tR68CAr4cpX+mwTzvmnuAGDUnngm/RrQ/54iq6RFCTbBh0PpW6YN0zpdA2 31RzISgK9Z6DIwtJUzCI7MLFApHjtUXlDtRJuDKWjxTkdjYTpNDOfBfSJwxy1vmSoSZVf/vgzqa qh5d8pkUcN999Qqb29QusALvf+hMpdSlPmTaOFtK0CEiDxGbfI4OzBRpxpf6c4OnUYj/ltpzpH6 sMHi1T1PrIH/M50+gpDEAWWsFbkmLg/ES3UrRB+Mjtio1eBwAa0lw4wYkps9D+1ykJQe70Effcg KluCHFvRXsSsmGcltm0dbP52SpQQqiQRvUV2AJI3y7AMADVcYNqZ2cL5ZnBpbrsNtfa/EaIUsLM uIOTmkI4BBvdEfUugmClc3I9gmD4vPE7X3vpPgfaFTlI6DsmYQ4BLYOf3LKz6FDZNz/0vvS2M5h l7mmUPO/8uXD/+QLNb7avJDq8KZ4/M8tNvMmL8mTmXxUVpJ9tg1PCmyOrD//QxBJ9P5gLwGlMv3 cqp12y9kG80eNfYDabzhORc1ZCv6DzeRaZ1PkA/szPVRHsXO+T9BHZs9z90IVn0CqagstoLQcKN golZ/To4KZwWowNGAsQRLqT+F3q5DmWkUV8dYD8Q5XlUipvKzfjRA+9u5lwQr+0XhlD8vbb1QBa e/dR1 X-Developer-Key: i=a.hindborg@kernel.org; a=openpgp; fpr=3108C10F46872E248D1FB221376EB100563EF7A7 Showcase the rust module parameter support by adding a module parameter to the `rust_minimal` sample. Reviewed-by: Benno Lossin Signed-off-by: Andreas Hindborg --- samples/rust/rust_minimal.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/samples/rust/rust_minimal.rs b/samples/rust/rust_minimal.rs index 1fc7a1be6b6d7..8eb9583571d72 100644 --- a/samples/rust/rust_minimal.rs +++ b/samples/rust/rust_minimal.rs @@ -10,6 +10,12 @@ authors: ["Rust for Linux Contributors"], description: "Rust minimal sample", license: "GPL", + params: { + test_parameter: i64 { + default: 1, + description: "This parameter has a default of 1", + }, + }, } =20 struct RustMinimal { @@ -20,6 +26,10 @@ impl kernel::Module for RustMinimal { fn init(_module: &'static ThisModule) -> Result { pr_info!("Rust minimal sample (init)\n"); pr_info!("Am I built-in? {}\n", !cfg!(MODULE)); + pr_info!( + "test_parameter: {}\n", + *module_parameters::test_parameter.value() + ); =20 let mut numbers =3D KVec::new(); numbers.push(72, GFP_KERNEL)?; --=20 2.47.2 From nobody Thu Oct 2 00:46:13 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1817A301028; Wed, 24 Sep 2025 12:40:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758717642; cv=none; b=FamZuoKTQM0IucMnhIHEhSZv9f4jR4oF7uNUXES7lamq0SIrufJQu88EgQ9bwV8Wv0uyb7ZkbxfzYf8iCvAX9n1TNyQwsoaYFSmWXfDXZ3T3KKmbt/YamBAgvptl+k1kP3zA30NEbjGca408DuGg7uD7AZSnOMRgbKLSwPgIUIo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758717642; c=relaxed/simple; bh=o2I34H38DvlXWFr+9zB77lMahqj/GxSAXo8mhBl2Brg=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=olxYvsK5WFLnB4u3CAesRZaTaou4oBkOOmm2M/a9DZlbMDG7njV3IM39xrEymv+DTvSQ/992PHFqHguf26L+1v65LOk3ob+ZhXKDUmjSaFEctr8hYE0oHsRze6jlb49X9/JHTrGJV18XZ+PPIEvm/a36ImJ83RJK4OK2uqE6GoM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=BF1b1osd; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="BF1b1osd" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 57D39C4CEF0; Wed, 24 Sep 2025 12:40:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1758717641; bh=o2I34H38DvlXWFr+9zB77lMahqj/GxSAXo8mhBl2Brg=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=BF1b1osdRPiUsqBwRxEO3KoBEeqAXV1JFbexgu+zSSNDjiyGXPJfinjpCznTyOQnI ZReCszz0QEw/2S4QMxWXmt3Vptqlxnk8OfC8q/BjA0scLQIsFQjsW8LRb5DaQoRxs8 G7Q8nk4VWiyVrLRAr7M/t+fI690yYltLUAP8sIzyLI+eAcOZU0L6IbVmy2UMlJ2J2B /hSZedkijxiLHF21qBhdcdlMwbBnFzd1WpcOVSXYBNWlzu//a+DPIlbgwTFtkQSUqh U59H9IwYk1er12rWivtamgJR8YOT8o8wiw747ZJEkZfaW7XHbJKHVyzRcSg/Pd9r8M 0HpQzjBdBco8A== From: Andreas Hindborg Date: Wed, 24 Sep 2025 14:39:30 +0200 Subject: [PATCH v18 7/7] modules: add rust modules files to MAINTAINERS Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250924-module-params-v3-v18-7-bf512c35d910@kernel.org> References: <20250924-module-params-v3-v18-0-bf512c35d910@kernel.org> In-Reply-To: <20250924-module-params-v3-v18-0-bf512c35d910@kernel.org> To: Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Alice Ryhl , Masahiro Yamada , Nathan Chancellor , Luis Chamberlain , Danilo Krummrich , Benno Lossin , Daniel Gomez , Benno Lossin , Nicolas Schier Cc: Trevor Gross , Adam Bratschi-Kaye , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kbuild@vger.kernel.org, Petr Pavlu , Sami Tolvanen , Daniel Gomez , Simona Vetter , Greg KH , Fiona Behrens , Daniel Almeida , linux-modules@vger.kernel.org, Andreas Hindborg X-Mailer: b4 0.15-dev X-Developer-Signature: v=1; a=openpgp-sha256; l=810; i=a.hindborg@kernel.org; h=from:subject:message-id; bh=o2I34H38DvlXWFr+9zB77lMahqj/GxSAXo8mhBl2Brg=; b=owEBbQKS/ZANAwAIAeG4Gj55KGN3AcsmYgBo0+amMWJlaXP5TF9ysmuYwFRe5s53TWYoH3I7u uHaZmEgIHeJAjMEAAEIAB0WIQQSwflHVr98KhXWwBLhuBo+eShjdwUCaNPmpgAKCRDhuBo+eShj d7aQD/4wntLmxvwiX4j9H+THDkxEQ1KMFImB5iRHF3hDU+NfeGcAptgSSc2PzJ1IYrL2EnIPId6 mDOsZRXd+0Avw+WJ8ZCVfpsv4KndrJOyl5CBFGrHNOurC46Tk8MKxhqsEbgerEBzgfcSGjCvwW2 p8+lPmPFZlPBjopUsZPcKD7Pa0/QmPNJP3ib34T5UK364WzXHW2oqjWc21TlxVmAE1LrxZauhzn Bv2E0kZ8XqBzUOux4iu49rX16/VhKbwAhyCVB6jNIbnW2Mij3OFljERkBi7sJcZc8HvCebL5qpJ +saOgp3rIsxUfD1dSNZIZSY0Rowfo0H5qZpElk6Jqlt9fSpgLzSShM/F11FuNLsIEVuxLqeP8Uq aIMZcA7qIAkxKA0ZRGU3b5r6sfMUukn74Jcnjh+q177OjPP0Dq8TpuQHrJ+NojDD7D/WC93zxjF pCvWFahA8v+7k9b58vTfSmVkVZSoDPIYLq+tq6+u7KMSh0sgZqsmUb+GPq+az0e43jijjPT84zl /8LFJJ9VZ/UBt2hHUb8GQujcx+c2oxlrkc/2diSdRuKpbVhck89x34m/yOUU9gf8+ONVMNUjCjy HOdkoYh3l6nReiF86Tct7OGx5n9swLM5ZRiHIwT3pUhuJsP4ViPNT0cXQjw7xaBfGdYXUKzB0Nr kAkQmsY9Hei6iTA== X-Developer-Key: i=a.hindborg@kernel.org; a=openpgp; fpr=3108C10F46872E248D1FB221376EB100563EF7A7 The module subsystem people agreed to maintain rust support for modules [1]. Thus, add entries for relevant files to modules entry in MAINTAINERS. Link: https://lore.kernel.org/all/0d9e596a-5316-4e00-862b-fd77552ae4b5@suse= .com/ [1] Acked-by: Daniel Gomez Signed-off-by: Andreas Hindborg --- MAINTAINERS | 2 ++ 1 file changed, 2 insertions(+) diff --git a/MAINTAINERS b/MAINTAINERS index d69021b88aef0..55e3bf16ea0a8 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -17136,6 +17136,8 @@ F: include/linux/module*.h F: kernel/module/ F: lib/test_kmod.c F: lib/tests/module/ +F: rust/kernel/module_param.rs +F: rust/macros/module.rs F: scripts/module* F: tools/testing/selftests/kmod/ F: tools/testing/selftests/module/ --=20 2.47.2