From: Wedson Almeida Filho <walmeida@microsoft.com>
This is a modified version of rust_minimal that is initialised in-place.
Signed-off-by: Wedson Almeida Filho <walmeida@microsoft.com>
---
samples/rust/Kconfig | 11 ++++++++++
samples/rust/Makefile | 1 +
samples/rust/rust_inplace.rs | 42 ++++++++++++++++++++++++++++++++++++
3 files changed, 54 insertions(+)
create mode 100644 samples/rust/rust_inplace.rs
diff --git a/samples/rust/Kconfig b/samples/rust/Kconfig
index b0f74a81c8f9..59f44a8b6958 100644
--- a/samples/rust/Kconfig
+++ b/samples/rust/Kconfig
@@ -20,6 +20,17 @@ config SAMPLE_RUST_MINIMAL
If unsure, say N.
+config SAMPLE_RUST_INPLACE
+ tristate "Minimal in-place"
+ help
+ This option builds the Rust minimal module with in-place
+ initialisation.
+
+ To compile this as a module, choose M here:
+ the module will be called rust_inplace.
+
+ If unsure, say N.
+
config SAMPLE_RUST_PRINT
tristate "Printing macros"
help
diff --git a/samples/rust/Makefile b/samples/rust/Makefile
index 03086dabbea4..791fc18180e9 100644
--- a/samples/rust/Makefile
+++ b/samples/rust/Makefile
@@ -1,6 +1,7 @@
# SPDX-License-Identifier: GPL-2.0
obj-$(CONFIG_SAMPLE_RUST_MINIMAL) += rust_minimal.o
+obj-$(CONFIG_SAMPLE_RUST_INPLACE) += rust_inplace.o
obj-$(CONFIG_SAMPLE_RUST_PRINT) += rust_print.o
subdir-$(CONFIG_SAMPLE_RUST_HOSTPROGS) += hostprogs
diff --git a/samples/rust/rust_inplace.rs b/samples/rust/rust_inplace.rs
new file mode 100644
index 000000000000..ba8d051cac56
--- /dev/null
+++ b/samples/rust/rust_inplace.rs
@@ -0,0 +1,42 @@
+// SPDX-License-Identifier: GPL-2.0
+
+//! Rust minimal in-place sample.
+
+use kernel::prelude::*;
+
+module! {
+ type: RustInPlace,
+ name: "rust_inplace",
+ author: "Rust for Linux Contributors",
+ description: "Rust minimal in-place sample",
+ license: "GPL",
+}
+
+#[pin_data(PinnedDrop)]
+struct RustInPlace {
+ numbers: Vec<i32>,
+}
+
+impl kernel::InPlaceModule for RustInPlace {
+ fn init(_module: &'static ThisModule) -> impl PinInit<Self, Error> {
+ pr_info!("Rust minimal sample (init)\n");
+ pr_info!("Am I built-in? {}\n", !cfg!(MODULE));
+ try_pin_init!(Self {
+ numbers: {
+ let mut numbers = Vec::new();
+ numbers.push(72, GFP_KERNEL)?;
+ numbers.push(108, GFP_KERNEL)?;
+ numbers.push(200, GFP_KERNEL)?;
+ numbers
+ },
+ })
+ }
+}
+
+#[pinned_drop]
+impl PinnedDrop for RustInPlace {
+ fn drop(self: Pin<&mut Self>) {
+ pr_info!("My numbers are {:?}\n", self.numbers);
+ pr_info!("Rust minimal inplace sample (exit)\n");
+ }
+}
--
2.34.1
On 27.03.24 04:23, Wedson Almeida Filho wrote:
> diff --git a/samples/rust/rust_inplace.rs b/samples/rust/rust_inplace.rs
> new file mode 100644
> index 000000000000..ba8d051cac56
> --- /dev/null
> +++ b/samples/rust/rust_inplace.rs
> @@ -0,0 +1,42 @@
> +// SPDX-License-Identifier: GPL-2.0
> +
> +//! Rust minimal in-place sample.
> +
> +use kernel::prelude::*;
> +
> +module! {
> + type: RustInPlace,
> + name: "rust_inplace",
> + author: "Rust for Linux Contributors",
> + description: "Rust minimal in-place sample",
> + license: "GPL",
> +}
> +
> +#[pin_data(PinnedDrop)]
> +struct RustInPlace {
> + numbers: Vec<i32>,
> +}
> +
> +impl kernel::InPlaceModule for RustInPlace {
> + fn init(_module: &'static ThisModule) -> impl PinInit<Self, Error> {
> + pr_info!("Rust minimal sample (init)\n");
This text needs updating.
> + pr_info!("Am I built-in? {}\n", !cfg!(MODULE));
> + try_pin_init!(Self {
> + numbers: {
> + let mut numbers = Vec::new();
> + numbers.push(72, GFP_KERNEL)?;
> + numbers.push(108, GFP_KERNEL)?;
> + numbers.push(200, GFP_KERNEL)?;
> + numbers
> + },
> + })
I think it might be useful to also have a field that needs pin-init, eg
a `Mutex` or similar. What about placing the `Vec` inside of a mutex?
--
Cheers,
Benno
> + }
> +}
> +
> +#[pinned_drop]
> +impl PinnedDrop for RustInPlace {
> + fn drop(self: Pin<&mut Self>) {
> + pr_info!("My numbers are {:?}\n", self.numbers);
> + pr_info!("Rust minimal inplace sample (exit)\n");
> + }
> +}
> --
> 2.34.1
>
On Wed, 27 Mar 2024 at 10:53, Benno Lossin <benno.lossin@proton.me> wrote:
>
> On 27.03.24 04:23, Wedson Almeida Filho wrote:
> > diff --git a/samples/rust/rust_inplace.rs b/samples/rust/rust_inplace.rs
> > new file mode 100644
> > index 000000000000..ba8d051cac56
> > --- /dev/null
> > +++ b/samples/rust/rust_inplace.rs
> > @@ -0,0 +1,42 @@
> > +// SPDX-License-Identifier: GPL-2.0
> > +
> > +//! Rust minimal in-place sample.
> > +
> > +use kernel::prelude::*;
> > +
> > +module! {
> > + type: RustInPlace,
> > + name: "rust_inplace",
> > + author: "Rust for Linux Contributors",
> > + description: "Rust minimal in-place sample",
> > + license: "GPL",
> > +}
> > +
> > +#[pin_data(PinnedDrop)]
> > +struct RustInPlace {
> > + numbers: Vec<i32>,
> > +}
> > +
> > +impl kernel::InPlaceModule for RustInPlace {
> > + fn init(_module: &'static ThisModule) -> impl PinInit<Self, Error> {
> > + pr_info!("Rust minimal sample (init)\n");
>
> This text needs updating.
Fixed in v2.
>
> > + pr_info!("Am I built-in? {}\n", !cfg!(MODULE));
> > + try_pin_init!(Self {
> > + numbers: {
> > + let mut numbers = Vec::new();
> > + numbers.push(72, GFP_KERNEL)?;
> > + numbers.push(108, GFP_KERNEL)?;
> > + numbers.push(200, GFP_KERNEL)?;
> > + numbers
> > + },
> > + })
>
> I think it might be useful to also have a field that needs pin-init, eg
> a `Mutex` or similar. What about placing the `Vec` inside of a mutex?
I'm not sure this belongs in a "minimal" example.
But I added it in v2 because we're already violating minimality with
vectors anyway. Perhaps we should later have minimal samples and
rename these to something else.
> --
> Cheers,
> Benno
>
> > + }
> > +}
> > +
> > +#[pinned_drop]
> > +impl PinnedDrop for RustInPlace {
> > + fn drop(self: Pin<&mut Self>) {
> > + pr_info!("My numbers are {:?}\n", self.numbers);
> > + pr_info!("Rust minimal inplace sample (exit)\n");
> > + }
> > +}
> > --
> > 2.34.1
> >
>
On 28.03.24 14:00, Wedson Almeida Filho wrote:
> On Wed, 27 Mar 2024 at 10:53, Benno Lossin <benno.lossin@proton.me> wrote:
>> On 27.03.24 04:23, Wedson Almeida Filho wrote:
>>> + pr_info!("Am I built-in? {}\n", !cfg!(MODULE));
>>> + try_pin_init!(Self {
>>> + numbers: {
>>> + let mut numbers = Vec::new();
>>> + numbers.push(72, GFP_KERNEL)?;
>>> + numbers.push(108, GFP_KERNEL)?;
>>> + numbers.push(200, GFP_KERNEL)?;
>>> + numbers
>>> + },
>>> + })
>>
>> I think it might be useful to also have a field that needs pin-init, eg
>> a `Mutex` or similar. What about placing the `Vec` inside of a mutex?
>
> I'm not sure this belongs in a "minimal" example.
>
> But I added it in v2 because we're already violating minimality with
> vectors anyway. Perhaps we should later have minimal samples and
> rename these to something else.
I think a fully minimal example would be less valuable as a learning
resource. We can of course have both, but I think having real usage of
pin-init in this example can help people get familiar with it.
--
Cheers,
Benno
© 2016 - 2026 Red Hat, Inc.