Rust for Linux List
 help / color / mirror / Atom feed
* [PATCH] rust: avoid `box_uninit_write` feature
@ 2024-09-04 14:42 Miguel Ojeda
  2024-09-04 14:51 ` Alice Ryhl
  2024-09-04 21:16 ` Miguel Ojeda
  0 siblings, 2 replies; 4+ messages in thread
From: Miguel Ojeda @ 2024-09-04 14:42 UTC (permalink / raw)
  To: Miguel Ojeda, Alex Gaynor, Wedson Almeida Filho
  Cc: Boqun Feng, Gary Guo, Björn Roy Baron, Benno Lossin,
	Andreas Hindborg, Alice Ryhl, Trevor Gross, Matt Gilbride,
	Danilo Krummrich, rust-for-linux, linux-kernel, patches

Like commit 0903b9e2a46c ("rust: alloc: eschew
`Box<MaybeUninit<T>>::write`"), but for the new `rbtree` and `alloc` code.

That is, `feature(new_uninit)` [1] got partially stabilized [2]
for Rust 1.82.0 (expected to be released on 2024-10-17), but it
did not include `Box<MaybeUninit<T>>::write`, which got split into
`feature(box_uninit_write)` [3].

To avoid relying on a new unstable feature, rewrite the `write` +
`assume_init` pair manually.

Link: https://github.com/rust-lang/rust/issues/63291 [1]
Link: https://github.com/rust-lang/rust/pull/129401 [2]
Link: https://github.com/rust-lang/rust/issues/129397 [3]
Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
---
 rust/kernel/alloc/box_ext.rs |  6 ++++--
 rust/kernel/rbtree.rs        | 17 ++++++++---------
 2 files changed, 12 insertions(+), 11 deletions(-)

diff --git a/rust/kernel/alloc/box_ext.rs b/rust/kernel/alloc/box_ext.rs
index b68ade26a42d..5b1550d620fd 100644
--- a/rust/kernel/alloc/box_ext.rs
+++ b/rust/kernel/alloc/box_ext.rs
@@ -26,9 +26,11 @@ pub trait BoxExt<T>: Sized {
     /// use kernel::alloc::{flags, box_ext::BoxExt};
     /// let value = Box::new([0; 32], flags::GFP_KERNEL)?;
     /// assert_eq!(*value, [0; 32]);
-    /// let value = Box::drop_contents(value);
+    /// let mut value = Box::drop_contents(value);
     /// // Now we can re-use `value`:
-    /// let value = Box::write(value, [1; 32]);
+    /// value.write([1; 32]);
+    /// // SAFETY: We just wrote to it.
+    /// let value = unsafe { value.assume_init() };
     /// assert_eq!(*value, [1; 32]);
     /// # Ok::<(), Error>(())
     /// ```
diff --git a/rust/kernel/rbtree.rs b/rust/kernel/rbtree.rs
index 48ceb9560bf5..25eb36fd1cdc 100644
--- a/rust/kernel/rbtree.rs
+++ b/rust/kernel/rbtree.rs
@@ -1059,15 +1059,14 @@ impl<K, V> RBTreeNodeReservation<K, V> {
     /// Initialises a node reservation.
     ///
     /// It then becomes an [`RBTreeNode`] that can be inserted into a tree.
-    pub fn into_node(self, key: K, value: V) -> RBTreeNode<K, V> {
-        let node = Box::write(
-            self.node,
-            Node {
-                key,
-                value,
-                links: bindings::rb_node::default(),
-            },
-        );
+    pub fn into_node(mut self, key: K, value: V) -> RBTreeNode<K, V> {
+        self.node.write(Node {
+            key,
+            value,
+            links: bindings::rb_node::default(),
+        });
+        // SAFETY: We just wrote to it.
+        let node = unsafe { self.node.assume_init() };
         RBTreeNode { node }
     }
 }

base-commit: 68d3b6aa08708bb3907c2c13eaf4b3ccf4805160
-- 
2.46.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] rust: avoid `box_uninit_write` feature
  2024-09-04 14:42 [PATCH] rust: avoid `box_uninit_write` feature Miguel Ojeda
@ 2024-09-04 14:51 ` Alice Ryhl
  2024-09-04 18:02   ` Matt Gilbride
  2024-09-04 21:16 ` Miguel Ojeda
  1 sibling, 1 reply; 4+ messages in thread
From: Alice Ryhl @ 2024-09-04 14:51 UTC (permalink / raw)
  To: Miguel Ojeda, Matt Gilbride
  Cc: Alex Gaynor, Wedson Almeida Filho, Boqun Feng, Gary Guo,
	Björn Roy Baron, Benno Lossin, Andreas Hindborg,
	Trevor Gross, Danilo Krummrich, rust-for-linux, linux-kernel,
	patches

On Wed, Sep 4, 2024 at 4:43 PM Miguel Ojeda <ojeda@kernel.org> wrote:
>
> Like commit 0903b9e2a46c ("rust: alloc: eschew
> `Box<MaybeUninit<T>>::write`"), but for the new `rbtree` and `alloc` code.
>
> That is, `feature(new_uninit)` [1] got partially stabilized [2]
> for Rust 1.82.0 (expected to be released on 2024-10-17), but it
> did not include `Box<MaybeUninit<T>>::write`, which got split into
> `feature(box_uninit_write)` [3].
>
> To avoid relying on a new unstable feature, rewrite the `write` +
> `assume_init` pair manually.
>
> Link: https://github.com/rust-lang/rust/issues/63291 [1]
> Link: https://github.com/rust-lang/rust/pull/129401 [2]
> Link: https://github.com/rust-lang/rust/issues/129397 [3]
> Signed-off-by: Miguel Ojeda <ojeda@kernel.org>

Good catch!

Reviewed-by: Alice Ryhl <aliceryhl@google.com>

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] rust: avoid `box_uninit_write` feature
  2024-09-04 14:51 ` Alice Ryhl
@ 2024-09-04 18:02   ` Matt Gilbride
  0 siblings, 0 replies; 4+ messages in thread
From: Matt Gilbride @ 2024-09-04 18:02 UTC (permalink / raw)
  To: Miguel Ojeda, Alex Gaynor, Wedson Almeida Filho
  Cc: Alice Ryhl, Boqun Feng, Gary Guo, Björn Roy Baron,
	Benno Lossin, Andreas Hindborg, Trevor Gross, Danilo Krummrich,
	rust-for-linux, linux-kernel, patches

On Wed, Sep 4, 2024 at 4:43 PM Miguel Ojeda <ojeda@kernel.org> wrote:
>
> Like commit 0903b9e2a46c ("rust: alloc: eschew
> `Box<MaybeUninit<T>>::write`"), but for the new `rbtree` and `alloc` code.
>
> That is, `feature(new_uninit)` [1] got partially stabilized [2]
> for Rust 1.82.0 (expected to be released on 2024-10-17), but it
> did not include `Box<MaybeUninit<T>>::write`, which got split into
> `feature(box_uninit_write)` [3].
>
> To avoid relying on a new unstable feature, rewrite the `write` +
> `assume_init` pair manually.
>
> Link: https://github.com/rust-lang/rust/issues/63291 [1]
> Link: https://github.com/rust-lang/rust/pull/129401 [2]
> Link: https://github.com/rust-lang/rust/issues/129397 [3]
> Signed-off-by: Miguel Ojeda <ojeda@kernel.org>

Reviewed-by: Matt Gilbride <mattgilbride@google.com>

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] rust: avoid `box_uninit_write` feature
  2024-09-04 14:42 [PATCH] rust: avoid `box_uninit_write` feature Miguel Ojeda
  2024-09-04 14:51 ` Alice Ryhl
@ 2024-09-04 21:16 ` Miguel Ojeda
  1 sibling, 0 replies; 4+ messages in thread
From: Miguel Ojeda @ 2024-09-04 21:16 UTC (permalink / raw)
  To: Miguel Ojeda
  Cc: Alex Gaynor, Wedson Almeida Filho, Boqun Feng, Gary Guo,
	Björn Roy Baron, Benno Lossin, Andreas Hindborg, Alice Ryhl,
	Trevor Gross, Matt Gilbride, Danilo Krummrich, rust-for-linux,
	linux-kernel, patches

On Wed, Sep 4, 2024 at 4:43 PM Miguel Ojeda <ojeda@kernel.org> wrote:
>
> Like commit 0903b9e2a46c ("rust: alloc: eschew
> `Box<MaybeUninit<T>>::write`"), but for the new `rbtree` and `alloc` code.
>
> That is, `feature(new_uninit)` [1] got partially stabilized [2]
> for Rust 1.82.0 (expected to be released on 2024-10-17), but it
> did not include `Box<MaybeUninit<T>>::write`, which got split into
> `feature(box_uninit_write)` [3].
>
> To avoid relying on a new unstable feature, rewrite the `write` +
> `assume_init` pair manually.
>
> Link: https://github.com/rust-lang/rust/issues/63291 [1]
> Link: https://github.com/rust-lang/rust/pull/129401 [2]
> Link: https://github.com/rust-lang/rust/issues/129397 [3]
> Signed-off-by: Miguel Ojeda <ojeda@kernel.org>

Applied to `rust-next` -- thanks Alice and Matt for the quick reviews!

Cheers,
Miguel

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2024-09-04 21:16 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-09-04 14:42 [PATCH] rust: avoid `box_uninit_write` feature Miguel Ojeda
2024-09-04 14:51 ` Alice Ryhl
2024-09-04 18:02   ` Matt Gilbride
2024-09-04 21:16 ` Miguel Ojeda

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox