From nobody Wed Oct 8 04:10:22 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 B5DDD284689; Wed, 2 Jul 2025 13:19:18 +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=1751462358; cv=none; b=GUseZo4T+HVrGtggFj+SYg2Ori+MjhGN/apEMqhdMj73+2RcAkquvKy5C2cFZYSTOPGa17DgKRFuax4m5I3OpxeXkZw4nj9KWAynlOcGODjqW86OIildCp6agsg9cv3svodhXROxk/eC3hGqg1hEXhSR6Vw0gGbFx6HWGiXXt9c= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1751462358; c=relaxed/simple; bh=9S3TEgnqLYbIT6Io+nOlVEeblcZ/nrr2prk36tq+oH8=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=IrvARwfVlzuIj6WDBA2tWscW42XQD1NnZ0eRJZScXWaQpCfXfM2hzE0NRAVTiH5F9CmWXttcZgjK7fxQe/IWkahe5/sriDAN/6skYPmq+w+KYM9gnJELBlF+1RUX3eOUoqUQsGkSFZKvat+LxCnACCoqUI0mqTr7SHVusnJPqJw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=DBmmWh8/; 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="DBmmWh8/" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4C6FFC4CEED; Wed, 2 Jul 2025 13:19:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1751462358; bh=9S3TEgnqLYbIT6Io+nOlVEeblcZ/nrr2prk36tq+oH8=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=DBmmWh8/GJlFKbvHf+i6gGkybyQTg3g7OJ3ys8wpGBve2Y/hNXZswhx9DQu6bGtXV LYoC/1o3lwwANpTYC1EfIcAv8NRdSGAJN6tOcs+UyUuTfRnXUoNRfFX/iSvxh4Fc6b 3RpD0RiCMbwT2rUwPrAaUgkkPLTgJxORHhdD2qdsWi6+KpRKOfNQLsp1tW6MA13aiS XkO7wYdg7ALI2V/EXNih1P3So30XIMYe3Vyb5YBn7UB9+nPDNctgbDGOYV8SByfkSR CkyXnR9BZifVmKlteaOS0zsan8nBk5y5h36slo4BrIiU8N8yhc6jMQtLYZlSGgoS3I ZvL/zxwMqGJAw== From: Andreas Hindborg Date: Wed, 02 Jul 2025 15:18:33 +0200 Subject: [PATCH v14 1/7] rust: sync: add `OnceLock` 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: <20250702-module-params-v3-v14-1-5b1cc32311af@kernel.org> References: <20250702-module-params-v3-v14-0-5b1cc32311af@kernel.org> In-Reply-To: <20250702-module-params-v3-v14-0-5b1cc32311af@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 , 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=4168; i=a.hindborg@kernel.org; h=from:subject:message-id; bh=9S3TEgnqLYbIT6Io+nOlVEeblcZ/nrr2prk36tq+oH8=; b=owEBbQKS/ZANAwAIAeG4Gj55KGN3AcsmYgBoZTGzBIyPCDW6CbV9USY9a6bw3bRnhSdW4qSxa icxywq0esCJAjMEAAEIAB0WIQQSwflHVr98KhXWwBLhuBo+eShjdwUCaGUxswAKCRDhuBo+eShj d6XhEADFrV+1h2hwOY+GnAAVhukDmoaKdiIjl3+EsCzGbUGDd+KNxnvTas7TVnClCcwjBh9pB1f OSfGloxPrcyx2i8KoRaOuzX4CdV6kLtHW4sOM3UAt8PAc4ijjD/kbjuaSKrh5xfiC9KG6HiQa7F VeVNeOq/8vI6yYEyvoL4CjEe2TEDr9jQekFTJVe86s8VXrNax1vJ7VlRUu8hDxXzH602c3bHuEJ ModCbWl4TdYwtF6t2uKcnL1CkSAeCLCVVXUr853xP5yF4irPYcnwgZTohqpSk/CIEVIByynzSKO MMTln11PjDqQaLd9HTLEZwC0UIX6qI2dsjksLmK6NyAwN4z66z3pKyh6NcisN8VaMe06XlgDPMB zNcC8mWocCwdO+RhLfKSt2HFpn08egmytCFsISkc8Lq6EMGRacgrFVP8zqFGkPkm86Do97snDyb PUPQv+nEbC+deVu/Ih6gdnOgshXevaVFFNLbhwo7ILvdrAb5w0FQqk+GZMr0ixIKl7uDNsKIAk7 fKt1blLyOHrgtVgxFqcXPvOK+OmPeu49fCoSu9ic1DcaUVWm8rv0JQ1t7ApvBI4DtGr9SGJ38iO UHUb3AkstIk4hyNuz9fbo/B5pN4RGDH1Vn59gnZHwk1ubxys63BFZKS8F3DFh+o/NT37naOYGns pvjwnmK5Aws25EA== X-Developer-Key: i=a.hindborg@kernel.org; a=openpgp; fpr=3108C10F46872E248D1FB221376EB100563EF7A7 Introduce the `OnceLock` type, a container that can only be written once. The container uses an internal atomic to synchronize writes to the internal value. Signed-off-by: Andreas Hindborg --- rust/kernel/sync.rs | 1 + rust/kernel/sync/once_lock.rs | 104 ++++++++++++++++++++++++++++++++++++++= ++++ 2 files changed, 105 insertions(+) diff --git a/rust/kernel/sync.rs b/rust/kernel/sync.rs index c7c0e552bafe..f2ee07315091 100644 --- a/rust/kernel/sync.rs +++ b/rust/kernel/sync.rs @@ -15,6 +15,7 @@ mod condvar; pub mod lock; mod locked_by; +pub mod once_lock; pub mod poll; pub mod rcu; =20 diff --git a/rust/kernel/sync/once_lock.rs b/rust/kernel/sync/once_lock.rs new file mode 100644 index 000000000000..cd311bea3919 --- /dev/null +++ b/rust/kernel/sync/once_lock.rs @@ -0,0 +1,104 @@ +//! A container that can be initialized at most once. + +use super::atomic::ordering::Acquire; +use super::atomic::ordering::Release; +use super::atomic::Atomic; +use kernel::types::Opaque; + +/// A container that can be populated at most once. Thread safe. +/// +/// Once the a [`OnceLock`] is populated, it remains populated by the same= object for the +/// lifetime `Self`. +/// +/// # Invariants +/// +/// `init` tracks the state of the container: +/// +/// - If the container is empty, `init` is `0`. +/// - If the container is mutably accessed, `init` is `1`. +/// - If the container is populated and ready for shared access, `init` is= `2`. +/// +/// # Example +/// +/// ``` +/// # use kernel::sync::once_lock::OnceLock; +/// let value =3D OnceLock::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 OnceLock { + init: Atomic, + value: Opaque, +} + +impl Default for OnceLock { + fn default() -> Self { + Self::new() + } +} + +impl OnceLock { + /// Create a new [`OnceLock`]. + /// + /// The returned instance will be empty. + pub const fn new() -> Self { + // INVARIANT: The container is empty and we set `init` to `0`. + Self { + value: Opaque::uninit(), + init: Atomic::new(0), + } + } + + /// Get a reference to the contained object. + /// + /// Returns [`None`] if this [`OnceLock`] is empty. + pub fn as_ref(&self) -> Option<&T> { + if self.init.load(Acquire) =3D=3D 2 { + // SAFETY: As determined by the load above, the object is read= y for shared access. + Some(unsafe { &*self.value.get() }) + } else { + None + } + } + + /// Populate the [`OnceLock`]. + /// + /// Returns `true` if the [`OnceLock`] was successfully populated. + pub fn populate(&self, value: T) -> bool { + // INVARIANT: We obtain exclusive access to the contained allocati= on and write 1 to + // `init`. + if let Ok(0) =3D self.init.cmpxchg(0, 1, Acquire) { + // SAFETY: We obtained exclusive access to the contained objec= t. + unsafe { core::ptr::write(self.value.get(), value) }; + // INVARIANT: We release our exclusive access and transition t= he object to shared + // access. + self.init.store(2, Release); + true + } else { + false + } + } +} + +impl OnceLock { + /// Get a copy of the contained object. + /// + /// Returns [`None`] if the [`OnceLock`] is empty. + pub fn copy(&self) -> Option { + if self.init.load(Acquire) =3D=3D 2 { + // SAFETY: As determined by the load above, the object is read= y for shared access. + Some(unsafe { *self.value.get() }) + } else { + None + } + } +} --=20 2.47.2 From nobody Wed Oct 8 04:10:22 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 CFE502877DF; Wed, 2 Jul 2025 13:19:46 +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=1751462386; cv=none; b=BHaj3a6AhHdEEK8VKDbWutRjZGCDprKgq55PJ7QCSfKv6LBCkugP2IFjbY/KMmFO+Uz+2qSbtfWOp/dQgv7/68nacrNSJUpCxL1A+71I33ADeXvzg8RI9x61vVNXJzsHkjjrch3DQtasDVRSx6LB1Zb/LHWBdxsV3G8GouGhxRk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1751462386; c=relaxed/simple; bh=aO+G2LBOUth+c97kiCVNHXHQOAnd4TaRXt0Dw/VnL7U=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=aSvrVEvf2LKpSeGUYruju+236SHh1aIXQh2jHLzzOh0Tjo4fLFkeV9ObXs5tvwbPfPYJRL/MXfv6xkwUXkrFqvHaPjmQ+dcxQyPoJupdThwu7UE1uyudTqgOnwjTTMw7jwsUGIfw3ZG1cp4GI6rPrzJe86Y+jThVWHS7thzpDu0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=gBBwjg9Y; 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="gBBwjg9Y" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 95B0EC4CEED; Wed, 2 Jul 2025 13:19:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1751462386; bh=aO+G2LBOUth+c97kiCVNHXHQOAnd4TaRXt0Dw/VnL7U=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=gBBwjg9Y24fXcgDP1qQZsVeITJKRdng7K0amoWMngzr3h5EdtT/8d8jdqwaLPXdRj xVIAjH5gU0+HS3NKSga711FW6FmHmEZhZiUJJ5LSAQmAt5QyoS2UIsIrstMkMwIWPk Hl0/BcPpjV/IkwGlrGQGsLj9GeMN5HjOJ8+CnK8meE9OJo5HOez2dXzmcGJSUrjQV+ hALDcU0q/ZSKqKnddVb0TwHTe4oSaOBWJRZ2ll5vtWo7wSyFa5mIydMDQg+9vqzmT8 j38bpA8g36snCx3hcOrg9meQITDhkWvo1H0+ne+VevrgLBONpOGdivcXYZC1j67cz6 wJCzw5jqmXsuQ== From: Andreas Hindborg Date: Wed, 02 Jul 2025 15:18:34 +0200 Subject: [PATCH v14 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: <20250702-module-params-v3-v14-2-5b1cc32311af@kernel.org> References: <20250702-module-params-v3-v14-0-5b1cc32311af@kernel.org> In-Reply-To: <20250702-module-params-v3-v14-0-5b1cc32311af@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 , 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=7153; i=a.hindborg@kernel.org; h=from:subject:message-id; bh=aO+G2LBOUth+c97kiCVNHXHQOAnd4TaRXt0Dw/VnL7U=; b=owEBbQKS/ZANAwAIAeG4Gj55KGN3AcsmYgBoZTGzZw5aqY3V0bF0bZEjwP/QabmpTL/nU1aU+ HhcjC9gvAuJAjMEAAEIAB0WIQQSwflHVr98KhXWwBLhuBo+eShjdwUCaGUxswAKCRDhuBo+eShj d4yRD/kBUhvi3BOLZNdcXMPl5fQKqHnSsOKNxTeYALdlB0RuW4N019o42seCGIbG6sln5/il7AV MpNroZQ6Qf5+PjoVcbn5ALrkDSqDABQRWduiXU9XMuhw9I0lQLOa5VZnGS5Bs356cgtMZqEqrOg ACPhxXfE4SFn8JTuQyPfvVDH9p1jitHHCmsOwJUbM4Oob1Ug/3BRqJMv0HXQ0hg5vcsFoMCKKw9 xjGJNBAq0eJdncrP63RWTQ34mhk1LZ7xLfsCB8Wp8XC/B2OXXBcC1wBo4p6NGzYezFGMKH2o9yr nk719wNv2XuO3byTbRJcKMG8ZLYh8rSUKQlocQU+gyoohwGr3GQzn742A016FTcbaPjGje4sui1 vYfQmUa/4vLTUZhYWGJh+vx0z9eFKRsjl1FJ7GLrx5uIfzbkPJj7DHeiGvjGwDDn271yad7Led3 N+J3wNqD6z/ojJwx4kx7uJLkHTva3ULok4XZ9zpwkTFeWSWuFYMqJtfcYB6RDydHWwb5fHDy+r3 Mx42onGB7r8Kgb2i9C48hWnaCEdn41sQKaAfz9kUQybOOA+GOSZ8geSjEZFPQ7CDUwxaoFyFiGf gJ9rLckj936RtXhlcpteUCMQl7hsY5nlvtMlQO2BOiinBHdC8GWlJrip9UH9BUqSQ87E0Is6ghl O+DkJeWYiXu2JzQ== 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. Tested-by: Daniel Gomez Reviewed-by: Greg Kroah-Hartman Suggested-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 a927db8e079c..2b6c8b4a0ae4 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 000000000000..48eb4c202984 --- /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 Wed Oct 8 04:10:22 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 4051D27A13A; Wed, 2 Jul 2025 13:19:24 +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=1751462364; cv=none; b=sJswNbQSNx8yBZ9GgzN2eC2t59X/H1Hl2pFDtxlY4jQD6bV+E6Op8jHvutjPoUCENw80dBrdGcZTsFmCEEuLgfGTZO3pzXhD8hStXcCPuuDBTNow2r3tgLPfj09OF3rn+iAb5BRVciNehbPibYwfQtLT6AwJSr1mNdM7IAku8xM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1751462364; c=relaxed/simple; bh=GINSHYwROTV6WI3RSAoMCRmq0RKzfKvRWX79JeAlh8Y=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=m6WdQXb++fluejyTDoL07WeWReisxQrSDxiRW/D+TGOiN743qnl5KM4Ra2QktQrEyssWX/TuFLG1jPfHvaat1pq2apkXyeYErbfLbhouYRLlOjkhWbOeUWqL0fVQWebjJmB4JZsdyXVDFSAbH0x7tr3d5fzf53hHtTj3ZExcRc4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=bHKJeoJn; 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="bHKJeoJn" Received: by smtp.kernel.org (Postfix) with ESMTPSA id DDD0EC4CEEF; Wed, 2 Jul 2025 13:19:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1751462363; bh=GINSHYwROTV6WI3RSAoMCRmq0RKzfKvRWX79JeAlh8Y=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=bHKJeoJnTEzbknH914OXRflq+RbJpZ+iW7v7bTgSHZf9jqekqYFRQ8SpsUnTyZvxq ev/PyaqvGGT8tWMuX5k7BH28fMN9t/lRZKW3CkP+7A1pGKukkpodGldhGHi9FAibrx bpcWeYd4Nv5yEpjHrkCN6Ox5qwG139W3DF3qV355r43SBNHM2NF7eYoei1uW5gS0Gg no2HJXh86mcvN2Yty6UyYW6ZqsonzAPV3Luk2hOEu5d5p/eXAGMcZzCcZzdPIdwgdz j4kKFBvWm36bTzMLLvV1cdTXtakBZ3kmJAo7nrYaiwliLcrsDh5J1YN7gYY23VvfYs kth86SZ80CO0w== From: Andreas Hindborg Date: Wed, 02 Jul 2025 15:18:35 +0200 Subject: [PATCH v14 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: <20250702-module-params-v3-v14-3-5b1cc32311af@kernel.org> References: <20250702-module-params-v3-v14-0-5b1cc32311af@kernel.org> In-Reply-To: <20250702-module-params-v3-v14-0-5b1cc32311af@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 , 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=7330; i=a.hindborg@kernel.org; h=from:subject:message-id; bh=GINSHYwROTV6WI3RSAoMCRmq0RKzfKvRWX79JeAlh8Y=; b=owEBbQKS/ZANAwAIAeG4Gj55KGN3AcsmYgBoZTG0VAFw3SWRtVJIdAn1HRC955GhaE4foFVRg DGl8dp6xYWJAjMEAAEIAB0WIQQSwflHVr98KhXWwBLhuBo+eShjdwUCaGUxtAAKCRDhuBo+eShj d2OfEAC2Ns/toQDjzh0HLTj3WAAEGJcR+4EOHmUNJuw6Wr8ri1cHPmsBccPgKw9vq73OPpdlfbd F9gwfrBVy7XcwB2ernkxz0wBgkab2WkaZNeWwki/XqCNsPYVQJ84C+i/XfHG2fCt0Mg17FihPnV IL8yQSA9FLf61mdERtJ6HwC560GNckvGb0v5QysxfAc+aH7omhUkFFgbIFsM/XNuFGOdyg0bPVQ u86pMsFJMxuTjrrms1MADrnnQV7xKttbtshRR6hNumA4P8SMEEytg6n++EFG/WKLkQ/kvetO7oT K1jBrtLgoqpt4mqIEjBfALiPix6ZaFivCjzf8VQsLdDuBLXgFc4ijV/TTGUOvg7VOvFtqxu6mfB 3Tc7j/reD8phAA04uF9iPESSnnFc4QcXNY8BwA+ziUCL9ul5ERuXJtoQNe0ZFsKtT5AN0N+PTHV 3u6glZ25s1adrSCIHHt8fBFEZuLJEPi//kdhm4ScHzDE2M43mOTEqS1Df84IeThxYdZAXJI6g44 3Rgfb1GcG5MtgRKOrzCU14elE323EEMtaj3Y1H87LlmY5h+gKPFJ87sh7CHi9D62e3hQsY1fmFD kZqo/gMSTKBOqAjH5rLLTZTrfCxCJTuH1zIPskQwxfYL5yhyLlfKyx10x4CVdLYkkIVG0nb3lYv K/hg2B8W7tRsuTg== X-Developer-Key: i=a.hindborg@kernel.org; a=openpgp; fpr=3108C10F46872E248D1FB221376EB100563EF7A7 Add types and traits for interfacing the C moduleparam API. Signed-off-by: Andreas Hindborg Reviewed-by: Benno Lossin --- rust/kernel/lib.rs | 1 + rust/kernel/module_param.rs | 191 ++++++++++++++++++++++++++++++++++++++++= ++++ 2 files changed, 192 insertions(+) diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs index 6b4774b2b1c3..2b439ea06185 100644 --- a/rust/kernel/lib.rs +++ b/rust/kernel/lib.rs @@ -87,6 +87,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 000000000000..ca4be7e45ff7 --- /dev/null +++ b/rust/kernel/module_param.rs @@ -0,0 +1,191 @@ +// 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::once_lock::OnceLock; + +/// Newtype to make `bindings::kernel_param` [`Sync`]. +#[repr(transparent)] +#[doc(hidden)] +pub struct RacyKernelParam(bindings::kernel_param); + +impl RacyKernelParam { + #[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 RacyKernelParam {} + +/// 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 { + /// The [`ModuleParam`] will be used by the kernel module through this= type. + /// + /// This may differ from `Self` if, for example, `Self` needs to track + /// ownership without exposing it or allocate extra space for other po= ssible + /// parameter values. + // This is required to support string parameters in the future. + type Value: ?Sized; + + /// 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 as *mu= t OnceLock) }; + + 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 { + type Value =3D $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: OnceLock, + 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: OnceLock::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 get(&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 { + (self as *const 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 Wed Oct 8 04:10:22 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 429FB27F736; Wed, 2 Jul 2025 13:19:12 +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=1751462353; cv=none; b=ljiG6qQmThM63PpJXpQ4/W8h5nkWJBFn7DBbSlwmJ1/ZoEFQIjWhlAzjN2bFCxImCLh0YZVEfoXeDF5+BAYFmnaokBFMJrX8XaPcG5yeiKWZc8Hv43rDx9Hr7iLiXRzcjyj2k/Umb/foH0E4j2AZlHyynygMOnzDANn36ffp+I4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1751462353; c=relaxed/simple; bh=6Qq3JEPyfCyEUyFmISVDX2MQupJGcEbRE4c1O1T+mok=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=VZZDcI6it+QMaJZL2bLnAs8C/Nskey8ypk7k87jiLxX4keB/9abJuLEya+a8yzHhKHJ+rPo9Q9oTKWcC3KYo3YYt0VUV2CCV5QBpbC1q9SJKByk3qD9XQrJ/lpGWYxvms+dLD3wbrOllDeXFsfveZv9rzo92rHgXPBY7tdZphFI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=EsDXQ60/; 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="EsDXQ60/" Received: by smtp.kernel.org (Postfix) with ESMTPSA id B4527C4CEF1; Wed, 2 Jul 2025 13:19:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1751462352; bh=6Qq3JEPyfCyEUyFmISVDX2MQupJGcEbRE4c1O1T+mok=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=EsDXQ60/qp406117PPHHmCxHqj+D+cVO4P+hdBG1wp96pFPSwvIjEYKUMbsYXeI9c a7e4RQNMYGf56rIltX9YXR1qiXrzNB2baeMMBbaUVkZG81f0PjrY+EQz78E7qNzACP ouRga63SgTjdLlOMNgkUfUxggDAPOPP/kyvXBlxwNTZuMOs3DQG+x/6CEqXccYGmHR XbIFVrH7qNIb6MFufAknP4u2HJdbKI0L9Fm3IIj10/r6+NMdUu5f71/sPemN6YzQIJ lOzlQZOpSSdcl97zwuvT/YBkwRfiWNkP9enrWfmDfhWMDo1uw/jJC7uoHbZb3iSL2b uvJzeu9d1czeg== From: Andreas Hindborg Date: Wed, 02 Jul 2025 15:18:36 +0200 Subject: [PATCH v14 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: <20250702-module-params-v3-v14-4-5b1cc32311af@kernel.org> References: <20250702-module-params-v3-v14-0-5b1cc32311af@kernel.org> In-Reply-To: <20250702-module-params-v3-v14-0-5b1cc32311af@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 , 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=2063; i=a.hindborg@kernel.org; h=from:subject:message-id; bh=6Qq3JEPyfCyEUyFmISVDX2MQupJGcEbRE4c1O1T+mok=; b=owEBbQKS/ZANAwAIAeG4Gj55KGN3AcsmYgBoZTG1q7BWJyAZsiU8AbktVyBccUl9DhqyCQrwA st3PLRMCqaJAjMEAAEIAB0WIQQSwflHVr98KhXWwBLhuBo+eShjdwUCaGUxtQAKCRDhuBo+eShj d8ZaEAC93JQofnKZzaTP3AHtWLUIkIXhJ2c216bYIl/vCIhs9QXEv6pnYfc11CNpjudmWFmQwIb 0vvpPsTeoGyt7pmGXLZhGez1Y8nfZMeDjU/yGQq5iIgxayKNQXPtYaYktoz+m9WEq8dLY5+f31R yIKJK4FRwENvQ6bqeJ3/SdpKV64FZbvEQtXTvXdXuBV2fQhPZ05UeATLUp6Ws683WwBZ2A0JKz9 wPwdllGt83lJwTTuPC0S5Zs8Z/aK8x3ogKjDcHAHCXFE889kO0R8YgY885ltKB2w4EFksWvZLrk ML+wpnZFvmv4IHrdwZ2lDsPEeIbsZIcnUdu7BlauQAJgvM8x1zZ8pGTYrc9a4nFDtY9FoWxDGz4 2GBMhZF+r9OYw1TGZPmp9B6Lr0/Axd8pC4T8YPBFrkpXHOr5KJEQ6xPdn8Qm2R+Mc2F/jAhwiSj FlW4ZMfZ8kRIv6tI8rxW6ogiyk8wfLjf3/UOKQsMT2FP8Ie5GaijdQoSJq3RRkTvw7/h66s2mzD Wx9P0o3SJ6z+xv0rofQRL0WUOe1exH9LoYiKtP30xzkBxJVHDlVmrSwwRywaJ81qg8SkbEtgy2F XzvXiq+Kmlgbcoy21q+Po6qJ0/qn9JNNHu3F6yK159oerKiJdmb3bcBLEFRyW5y2b0u1mD+AZd2 pQmYBO8zQZ3KnlQ== 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 | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/rust/macros/module.rs b/rust/macros/module.rs index 2ddd2eeb2852..1a867a1e787e 100644 --- a/rust/macros/module.rs +++ b/rust/macros/module.rs @@ -179,26 +179,26 @@ 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(author) =3D info.author { - modinfo.emit("author", &author); + if let Some(author) =3D &info.author { + modinfo.emit("author", author); } - 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 Wed Oct 8 04:10:22 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 C28312798E1; Wed, 2 Jul 2025 13:19:52 +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=1751462392; cv=none; b=fkJKdyTKhtCPQPKLyD6xrtEOLKoX8aGjzE/4IQSM748mjjyn+CVGKcYWjr5vediqD3cmnBifHpleTQF2yOBtOzPLHxzIhw78q4MX37ve7Ykp17sMLDgqRZ6E/JzEx7fe+dksQGrFNiy9aUker8GVE9ab52zDo5bGamsbZTyamtI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1751462392; c=relaxed/simple; bh=S/JJdI/wj8kpTOdAlnf1mLcjQ66qFgptedQaa2V5SzM=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=SXQBRbzWEHifpFrBdKZ6i4Ra190W7nSlr/7H4+ZK3i2P5bqe3HTBYswVThhTBITygdMBOh4gCC1GwY+xpImVo0uuW613rghPVDL9JGLmdImmthTp5NBxjs6ZDSHvhVHMTBr5x+f909SuOIAjVDVuUeWxPZbtDot8f0MaZQ5E2gU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=IZgzWGGi; 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="IZgzWGGi" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4F5A1C4CEED; Wed, 2 Jul 2025 13:19:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1751462392; bh=S/JJdI/wj8kpTOdAlnf1mLcjQ66qFgptedQaa2V5SzM=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=IZgzWGGi2dxQSMqZJhmpmwZsmnpLY/k1RRKrY7Yw+mn9F2OFC73B9T7s8x3Y4Lrj0 FfA5ImoQMFkUaP9LyqjX8YS3N/zvjipCZRjroL65Riju+EmAbKIKii/lpRqhaiNTzO YQVAxNXvJ3dxT2JbQIkVbT0lZNceSUHd8wvi5GEVLbZj5ZCtO+UPpjfLPyfQVrEets Y67AA9+RBTEneU4j57aIJAZau3I0cCiTyLFIqlMrjGTHLu/1MXhrqtWfypz2BNGaZ4 lQBLuoo53RYdidm9j9VdYjZ5mUDaF3pwj1kRv2FDZGzzc75QRgdPFTpOVWkFKjVJWO RQyzRAww9ubRA== From: Andreas Hindborg Date: Wed, 02 Jul 2025 15:18:37 +0200 Subject: [PATCH v14 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: <20250702-module-params-v3-v14-5-5b1cc32311af@kernel.org> References: <20250702-module-params-v3-v14-0-5b1cc32311af@kernel.org> In-Reply-To: <20250702-module-params-v3-v14-0-5b1cc32311af@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 , 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=13415; i=a.hindborg@kernel.org; h=from:subject:message-id; bh=S/JJdI/wj8kpTOdAlnf1mLcjQ66qFgptedQaa2V5SzM=; b=owEBbQKS/ZANAwAIAeG4Gj55KGN3AcsmYgBoZTG2SGk38fuu6/kZ1wrzPLTn9Hzk+W+EqeBcX IUcOyXuX8WJAjMEAAEIAB0WIQQSwflHVr98KhXWwBLhuBo+eShjdwUCaGUxtgAKCRDhuBo+eShj d+ObD/9DBhxQ+JDEcSTSfS2koQXQgStBAvO4Nib2Qt4CtkBatLKs4NnDXlx2ng6FRLEcvuIyZLq aDKBrGN5rs/UmytDYqXazVcaTOJl5YYrAV4HdEarTHuSFAkpHlZPXhfmvh0e4niNItj6Xc4DZcu i/NdtzU+nKyzI7xXHB5k1lKJhhYKrN5hbghNBVgAX1JyZke/nPWESL4wzwCL1dgqNgPAmipIIZu yCaXDls7fA/xGXpolwDnsmBXTSNRh7SJcOXFnBKcqrjwmzQcyy3n5PUfuorxPMFXA9Ex3/zWZpB n5P83BLXcPXa1+zKwtVLbV1fr2E50H63hvATSSoJa0vY+6w1i23+var5cTdFqR+qcfovbzbOW2r rLx0SqYniOI/zCmXWg5qPpxJ+B8Qq0mf2BL6rITmLkxi5R3tS6d0zs7IRjDaKGFeavOojrBeATt /tqpjyPOwJ8r75JrFxl9tkVgNr7g3Vopp4YXpRQA8SXdSHtnO/Fn2Tkz7HbQgh7OJaclfiG4+34 w8wAeYvXesofyW9VFD5DFvw9L7uoxFNmb0ucDlVJp9duXcGACyPLtvsdTmhEW/XVb9fIopm47vA T09HoWQ/60bVvqdcKrxWV3K7ohQb07uEsgZMHRgO1/Whzjf2q49v/00/FFEVRo9YLMZWbxVnSNs ffV82UudqCgyxjw== X-Developer-Key: i=a.hindborg@kernel.org; a=openpgp; fpr=3108C10F46872E248D1FB221376EB100563EF7A7 Allow module parameters to be declared in the rust `module!` macro. Signed-off-by: Andreas Hindborg Reviewed-by: Benno Lossin --- rust/macros/helpers.rs | 25 +++++++ rust/macros/lib.rs | 31 +++++++++ rust/macros/module.rs | 177 +++++++++++++++++++++++++++++++++++++++++++++= +--- 3 files changed, 223 insertions(+), 10 deletions(-) diff --git a/rust/macros/helpers.rs b/rust/macros/helpers.rs index e2602be402c1..365d7eb499c0 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 fa847cf3a9b5..2fb520dc930a 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 1a867a1e787e..2b8123fe9fb0 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,118 @@ 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_t= ype}> =3D + ::kernel::module_param::ModuleParamAccess::new= ({param_default}); + + #[link_section =3D \"__param\"] + #[used] + static __{module_name}_{param_name}_struct: + ::kernel::module_param::RacyKernelParam =3D + ::kernel::module_param::RacyKernelParam::new( + ::kernel::bindings::kernel_param {{ + name: if cfg!(MODULE) {{ + ::kernel::c_str!(\"{param_name}\").as_byte= s_with_nul() + }} else {{ + ::kernel::c_str!(\"{module_name}.{param_na= me}\").as_bytes_with_nul() + }}.as_ptr(), + // SAFETY: `__this_module` is constructed by t= he kernel at load time + // and will not be freed until the module is u= nloaded. + #[cfg(MODULE)] + mod_: unsafe {{ + (&::kernel::bindings::__this_module + as *const ::kernel::bindings::module) + .cast_mut() + }}, + #[cfg(not(MODULE))] + mod_: ::core::ptr::null_mut(), + ops: &{ops} as *const ::kernel::bindings::kern= el_param_ops, + perm: 0, // Will not appear in sysfs + level: -1, + flags: 0, + __bindgen_anon_1: + ::kernel::bindings::kernel_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, @@ -99,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 { @@ -114,6 +264,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(); @@ -140,6 +291,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 @@ -205,7 +357,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!( " @@ -369,15 +523,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 Wed Oct 8 04:10:22 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 F1F8028726B; Wed, 2 Jul 2025 13:19: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=1751462376; cv=none; b=UssaUzVGDp+eFt5cMH1y8kEzF/d6zIFlh/RsANOGYFAOffL2ymown9VzEslA8o+bzEBCCH4ffLDwZp7y8C1MYLGeejj89wWDvYgV7UBNO7fMQdSAqlJ08rrsOIYMJ4duG/NE5oOS4TquijvPHyvHqnFqCqF8DL+1I/vHbsF7IMk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1751462376; c=relaxed/simple; bh=L9AZcSPjf/9WKfgudNM7sFHRKU96doSlEH7lJ7L7ndQ=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=cL5wmrHXPWMQzeKTdwf8kjUi8XdhS4d4OxLnKCUEdKRZyNsog0w4HllzitPLKi7/y4bk5h0ZiLtK7XRhVDx/QO/101ZFZjIamhrN6r0XsombqNpYgohyRXxTlnSXt/yjbc+XLlxOqYm90crAGVZedmX0eJozcxf30qhIyANjvoc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=M3eM0DKw; 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="M3eM0DKw" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 43823C4CEEF; Wed, 2 Jul 2025 13:19:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1751462375; bh=L9AZcSPjf/9WKfgudNM7sFHRKU96doSlEH7lJ7L7ndQ=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=M3eM0DKwReMND1peJD0QYy5eWyAsc1j6g28SFhEUYPh9S80G34ZLC5F592mS+Q9P9 /TGuMMCHNJBv9mV++Eji6g+LjpONNWomUdwBUkU/Sfz2/dx2avYKnRUNvhherxMleI a5TCyB2+5/oKWji/O9TIIs2TF/CHxY0WqaPnxrdFYHmzNNVMKkAT03Rad6NSMfAott nALP/oYCsATeUus80bAUZ242FDzhTEi+lwtUF3jGusWlvmClHXlcVSDFggzR58jKig Eb/mLkSn/ZWqLsEi8G+PeNVZ1JU1dqu8BsZEBlQf3gsvK2K5t8P6H4p23eqtcD3xk7 LBCCVrgVDbYRQ== From: Andreas Hindborg Date: Wed, 02 Jul 2025 15:18:38 +0200 Subject: [PATCH v14 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: <20250702-module-params-v3-v14-6-5b1cc32311af@kernel.org> References: <20250702-module-params-v3-v14-0-5b1cc32311af@kernel.org> In-Reply-To: <20250702-module-params-v3-v14-0-5b1cc32311af@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 , 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=1264; i=a.hindborg@kernel.org; h=from:subject:message-id; bh=L9AZcSPjf/9WKfgudNM7sFHRKU96doSlEH7lJ7L7ndQ=; b=owEBbQKS/ZANAwAIAeG4Gj55KGN3AcsmYgBoZTG3tzq3Zf/Qc1bFX4sK+CqJSzeKu+qFoLGRJ fSppSY5ZDuJAjMEAAEIAB0WIQQSwflHVr98KhXWwBLhuBo+eShjdwUCaGUxtwAKCRDhuBo+eShj d/EmD/4owlmnQxt/hHK/xZytuUMQikRv2PLNJ3zq5fePmu5FkXxxJ64LNEZLwLq0VyGnxwzUMq8 6tNsEv4+QnbwCEGzuu5eC4XNpKwOj8P4LUf8ddag/CImDyEfiLb2eZa0/t7pPP3Dn2ISf8peGJy ePkWX/6Tv93odd39uxy8bFPKU+dIfJgCZGYwdBq5ZIdteidovptrSnfUm6Y/WQa1Y6+k/evhbcT 7bxg4Y4Q/Awm+JIZ3xFCxoBnH7lfj/CeG+Ob5RtxvEJw4gv6zTuvH9JmCIc0cfGe9hMaSum2REK AvVmZVS1FIEoFthN10Jy2ZgnCRJgMObI6l1wBF+o46EW1AcuplP0KFFBTb7GRyu3NsXYmvFDIoC 3QiGAlwRmHt+MI01eT2uqDOK5PJjFEQVtPo8Igyd8QLERXhlvVGeXgM6BrgeCCxvJV8hewo4lyY zJv3IMXJ09W+1lw/MDP6Rye1+XgpQBaGWslq0DJJaPBG/tBAjLuVMqQ2Dl5Ly5QWRHUvNxcxobq OAWtFDA+GfSbEaJVhpCxI62uNPtefNVoiS1D3jOyht20rT/8RRw8pXxStSUY1R4iczh9a11yhSF qGJDeAHTK/9VU9c+SFrZr/SUzLEjp0Re0dJ0NcIy5A0aC9KgvVdYGqa4BJ3jhjf0y2SHrIc0jO9 HAhTUHT2iaKB+jA== 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 1fc7a1be6b6d..c04cc07b3249 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.get() + ); =20 let mut numbers =3D KVec::new(); numbers.push(72, GFP_KERNEL)?; --=20 2.47.2 From nobody Wed Oct 8 04:10:22 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 796DC2798FE; Wed, 2 Jul 2025 13:19: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=1751462381; cv=none; b=bDtk7Dhbsj3FxdLyQt7qbV54iN4vOMeYr7N8B911ymEzTc6BGmJn8y9ehbqJdiKOLohLlae+SJb8oVX0TYl0RpWLObdyY1YtXxFSXYpjPubo2YoPCqje02/nXWNCJ0MCZpBUCmVIWkEU4PNcUtJL4sSpQB9nAcfE6HlXQrGOzZI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1751462381; c=relaxed/simple; bh=QKv1eaNx8HXCqOejDO0r39rwgHrEEYHNlSfukU0dcXk=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=f6/5I6FosoIwysUVb4ot375HK7ly6TKW6yZq7Qq3JBWPaRrEZjAB2F7pbWPXzk4FnzjJFZ0ZROxMw4JXIKp1wjyvnVxBvkLqq7LTBYRqT5HDsiWu8rY1JjHVNR1R9r6Gxx/G8XB7poLT2PIAYL0kEBxvQKsaSCakKYPowtNeALw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=FbF3K1F0; 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="FbF3K1F0" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 0B025C4CEF1; Wed, 2 Jul 2025 13:19:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1751462381; bh=QKv1eaNx8HXCqOejDO0r39rwgHrEEYHNlSfukU0dcXk=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=FbF3K1F0xoMCvW2mCumeLkEI7lUkTga14EsFKlxYJXhpm/ctiwGe4N74gIPu3vIHx x6Ku4BYZEphGAar9XBVMNrE7Xs368n2HDMiUu4l2y/A6rTMK+aM8F+BFir5mMAOkpa WkuOh68i8M5i5+XMLuuERMgu5QBgEEeto/QfW6aFEWtwGj0sw9Gj62VDTCR6SvgHYK bCKPDj/FBNnB69N6OKwfdxbkH/bvAbiXuLAIND56oONr0ABCaUNXcV+yaFmRZzVBp4 VVTNYbE1UY20D2io2LkOgeMeLmdQFpLn/qCMxr7mLnrIARczPBIrF4/I7gPB5KC4RL Eu6mf0qH8KZoA== From: Andreas Hindborg Date: Wed, 02 Jul 2025 15:18:39 +0200 Subject: [PATCH v14 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: <20250702-module-params-v3-v14-7-5b1cc32311af@kernel.org> References: <20250702-module-params-v3-v14-0-5b1cc32311af@kernel.org> In-Reply-To: <20250702-module-params-v3-v14-0-5b1cc32311af@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 , 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=808; i=a.hindborg@kernel.org; h=from:subject:message-id; bh=QKv1eaNx8HXCqOejDO0r39rwgHrEEYHNlSfukU0dcXk=; b=owEBbQKS/ZANAwAIAeG4Gj55KGN3AcsmYgBoZTG4L057HjJ5TR9cS+a56ULFDPMPW9mzocfjJ Aei5uwPLiqJAjMEAAEIAB0WIQQSwflHVr98KhXWwBLhuBo+eShjdwUCaGUxuAAKCRDhuBo+eShj d5j2D/9sF/xwBInFVeLxWuLHtYlx4FfdgeB9B20QNmNCqnu9ZEGGRCv4E7ETYVzdCSYIgAAwQ1f EvT/iH9cmn5m+G9mcAQojIE8ZqrlcisATzKw4D1LKbDJ8TmV5FyWva2Lvo11kn+8lObcVcEmt7a WBAsGmE1VAiKskFe5Wt7zk+kNGKh7FjZcWvX1L9EvvrBF7u1P61BwxrJkAAhwDTq8F8H5DxghtI MMkAVLHZP+pNDsCKTrVI7FlRGHsnPoOldn0521AI3bawMC4y/0OtzSr958u8+fGBByAlDPqKHtG rJnMINI3VvgwyOKHhYEyhKQTirCNkNZZ6WaRl7gpqIWfddTq5u1fT4sft18Q/mDyfz6zu6P+phk dC6T8CU48RNwUsUDcD1aiKV4SUGbF5aewaAFqdCFLjt0woMIhMiSK8yGL0l+bOXPainnXn2Lt0R 6ll3QNuxZZYeZgqHeFPK5YR1fIpjxiEzl7E1QCKqgUmwjiVYeeXgc+6G9yO1BMohZeN4uJbd7lO VsRCejzqVqoZPwVNx5ayx5+ARamTXjt8QGLBEY3GqlwHBxVIT7iyaA+9QJNwUf07JhIMGkvYcBU Xqgk9TJqp1y4mL4/BkvTrFKnjfz3uFwBUxsUfmUc7U/hdk8UPy5+kUi9ZXvMZ2UFG4R57+j6xTk 6BXyhbtu0NRRgJA== 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 fe0cf0a2e6e5..aa5618350723 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -16796,6 +16796,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