Allocation APIs
Subsystems (6)
cover Allocation APIs
From: Wedson Almeida Filho <[email protected]>
Revamp how we use the alloc crate.
We currently have a fork of the crate with changes to Vec; other changes have been upstreamed (to the Rust project). This series removes the fork and exposes all the functionality as extension traits.
Additionally, it also introduces allocation flag parameters to all functions that may result in allocations (e.g., Box::new, Arc::new, Vec::push, etc.) without the try_ prefix -- the names are available because we build alloc with no_global_oom_handling.
Lastly, the series also removes our reliance on the allocator_api unstable feature.
Long term, we still want to make such functionality available in upstream Rust, but this allows us to make progress now and reduces our maintainance burden.
In summary:
1. Removes alloc fork
2. Removes use of allocator_api unstable feature
3. Introduces flags (e.g., GFP_KERNEL, GFP_ATOMIC) when allocating
Wedson Almeida Filho (10):
rust: kernel: move allocator module under alloc
rust: alloc: introduce the VecExt trait
kbuild: use the upstream alloc crate
rust: alloc: remove our fork of the alloc crate
rust: alloc: introduce allocation flags
rust: alloc: introduce the BoxExt trait
rust: alloc: update VecExt to take allocation flags
rust: sync: update Arc and UniqueArc to take allocation flags
rust: init: update init module to take allocation flags
rust: kernel: remove usage of allocator_api unstable feature
rust/Makefile | 16 +-
rust/alloc/README.md | 36 -
rust/alloc/alloc.rs | 452 ----
rust/alloc/boxed.rs | 2463 -----------------
rust/alloc/collections/mod.rs | 160 --
rust/alloc/lib.rs | 288 --
rust/alloc/raw_vec.rs | 611 -----
rust/alloc/slice.rs | 890 -------
rust/alloc/vec/drain.rs | 255 --
rust/alloc/vec/extract_if.rs | 115 -
rust/alloc/vec/into_iter.rs | 454 ----
rust/alloc/vec/is_zero.rs | 204 --
rust/alloc/vec/mod.rs | 3683 --------------------------
rust/alloc/vec/partial_eq.rs | 49 -
rust/alloc/vec/set_len_on_drop.rs | 35 -
rust/alloc/vec/spec_extend.rs | 119 -
rust/bindings/bindings_helper.h | 3 +
rust/kernel/alloc.rs | 68 +
rust/kernel/{ => alloc}/allocator.rs | 6 +-
rust/kernel/alloc/boxext.rs | 60 +
rust/kernel/alloc/vecext.rs | 120 +
rust/kernel/error.rs | 13 +-
rust/kernel/init.rs | 57 +-
rust/kernel/lib.rs | 5 +-
rust/kernel/prelude.rs | 2 +
rust/kernel/str.rs | 6 +-
rust/kernel/sync/arc.rs | 44 +-
rust/kernel/sync/condvar.rs | 2 +-
rust/kernel/sync/lock/mutex.rs | 2 +-
rust/kernel/sync/lock/spinlock.rs | 2 +-
rust/kernel/types.rs | 4 +-
rust/kernel/workqueue.rs | 14 +-
samples/rust/rust_minimal.rs | 6 +-
samples/rust/rust_print.rs | 4 +-
scripts/generate_rust_analyzer.py | 2 +-
35 files changed, 340 insertions(+), 9910 deletions(-)
delete mode 100644 rust/alloc/README.md
delete mode 100644 rust/alloc/alloc.rs
delete mode 100644 rust/alloc/boxed.rs
delete mode 100644 rust/alloc/collections/mod.rs
delete mode 100644 rust/alloc/lib.rs
delete mode 100644 rust/alloc/raw_vec.rs
delete mode 100644 rust/alloc/slice.rs
delete mode 100644 rust/alloc/vec/drain.rs
delete mode 100644 rust/alloc/vec/extract_if.rs
delete mode 100644 rust/alloc/vec/into_iter.rs
delete mode 100644 rust/alloc/vec/is_zero.rs
delete mode 100644 rust/alloc/vec/mod.rs
delete mode 100644 rust/alloc/vec/partial_eq.rs
delete mode 100644 rust/alloc/vec/set_len_on_drop.rs
delete mode 100644 rust/alloc/vec/spec_extend.rs
create mode 100644 rust/kernel/alloc.rs
rename rust/kernel/{ => alloc}/allocator.rs (96%)
create mode 100644 rust/kernel/alloc/boxext.rs
create mode 100644 rust/kernel/alloc/vecext.rsbase-commit: 768409cff6cc89fe1194da880537a09857b6e4db
Signature · 1 line
2.34.1
PATCH 1/10 rust: kernel: move `allocator` module under `alloc`
From: Wedson Almeida Filho <[email protected]>
We will add more to the alloc module in subsequent patches (e.g., allocation flags and extension traits).
---
rust/kernel/alloc.rs | 7 +++++++
rust/kernel/{ => alloc}/allocator.rs | 0
rust/kernel/lib.rs | 4 +---
3 files changed, 8 insertions(+), 3 deletions(-)
create mode 100644 rust/kernel/alloc.rs
rename rust/kernel/{ => alloc}/allocator.rs (100%)rust/kernel/alloc.rs new file +7 -0
@@ -0,0 +1,7 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! Allocation support. + +#[cfg(not(test))] +#[cfg(not(testlib))] +mod allocator;
rust/kernel/alloc/allocator.rs renamed from rust/kernel/allocator.rs +0 -0
rust/kernel/lib.rs +1 -3
@@ -28,9 +28,7 @@// Allow proc-macros to refer to `::kernel` inside the `kernel` crate (this crate). extern crate selfas kernel; -#[cfg(not(test))] -#[cfg(not(testlib))] -mod allocator; +pub mod alloc;mod build_assert;pub mod error;pub mod init;
Signature · 1 line
2.34.1
Re: [PATCH 01/10] rust: kernel: move `allocator` module under `alloc`
On 25.03.24 20:54, Wedson Almeida Filho wrote:> From: Wedson Almeida Filho <[email protected]>
Quoted 5 lines
We will add more to the
allocmodule in subsequent patches (e.g., allocation flags and extension traits).Signed-off-by Wedson Almeida Filho <[email protected]>
One nit below, but feel free to add:
Quoted 14 lines
--- rust/kernel/alloc.rs | 7 +++++++ rust/kernel/{ => alloc}/allocator.rs | 0 rust/kernel/lib.rs | 4 +--- 3 files changed, 8 insertions(+), 3 deletions(-)create mode 100644
rust/kernel/alloc.rsrenamerust/kernel/{=>alloc}/allocator.rs(100%)rust/kernel/alloc.rs new file +0
-0
rust/kernel/alloc.rs line 3
Quoted 3 lines
rust/kernel/alloc.rs new file +3
-0@@ -0,0 +1,7 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! Allocation support.
I think 'Extensions to the alloc crate.' fits better.
Signature · 2 lines
Cheers, Benno
Quoted 26 lines
rust/kernel/alloc.rs new file +4
-0+ +#[cfg(not(test))] +#[cfg(not(testlib))] +mod allocator;rust/kernel/alloc/allocator.rs renamed from rust/kernel/allocator.rs +0
-0rust/kernel/lib.rs +1
-4@@ -28,9 +28,7 @@// Allow proc-macros to refer to `::kernel` inside the `kernel` crate (this crate). extern crate selfas kernel; -#[cfg(not(test))] -#[cfg(not(testlib))] -mod allocator; +pub mod alloc;mod build_assert;pub mod error;pub mod init; --2.34.1
Re: [PATCH 01/10] rust: kernel: move `allocator` module under `alloc`
Quoted 20 lines · Benno Lossin
On 25.03.24 20:54, Wedson Almeida Filho wrote:> From: Wedson Almeida Filho <[email protected]>
Quoted 5 lines
We will add more to the
allocmodule in subsequent patches (e.g., allocation flags and extension traits).Signed-off-by Wedson Almeida Filho <[email protected]>One nit below, but feel free to add:
Reviewed-by Benno Lossin <[email protected]>Quoted 8 lines
--- rust/kernel/alloc.rs | 7 +++++++ rust/kernel/{ => alloc}/allocator.rs | 0 rust/kernel/lib.rs | 4 +--- 3 files changed, 8 insertions(+), 3 deletions(-)create mode 100644
rust/kernel/alloc.rsrenamerust/kernel/{=>alloc}/allocator.rs(100%)
Quoted 11 lines
rust/kernel/alloc.rs line 3
Quoted 9 lines
rust/kernel/alloc.rs new file +3
-0@@ -0,0 +1,7 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! Allocation support. I think 'Extensions to the
alloccrate.' fits better.
SGTM. I will update for v2.
Quoted 31 lines
--
Cheers,
BennoQuoted 26 lines
rust/kernel/alloc.rs new file +4
-0+ +#[cfg(not(test))] +#[cfg(not(testlib))] +mod allocator;rust/kernel/alloc/allocator.rs renamed from rust/kernel/allocator.rs +0
-0rust/kernel/lib.rs +1
-4@@ -28,9 +28,7 @@// Allow proc-macros to refer to `::kernel` inside the `kernel` crate (this crate). extern crate selfas kernel; -#[cfg(not(test))] -#[cfg(not(testlib))] -mod allocator; +pub mod alloc;mod build_assert;pub mod error;pub mod init; --2.34.1
PATCH 2/10 rust: alloc: introduce the `VecExt` trait
In reply to Wedson Almeida FilhoFrom: Wedson Almeida Filho <[email protected]>
Make try_with_capacity, try_push, and try_extend_from_slice methods available in Vec even though it doesn't implement them. It is implemented with try_reserve and push_within_capacity.
This is in preparation for switching to the upstream alloc crate.
--- rust/kernel/alloc.rs | 1 + rust/kernel/alloc/vecext.rs | 52 +++++++++++++++++++++++++++++++++++++ rust/kernel/lib.rs | 1 + rust/kernel/prelude.rs | 2 ++ 4 files changed, 56 insertions(+) create mode 100644 rust/kernel/alloc/vecext.rs
rust/kernel/alloc.rs +1 -0
@@ -5,3 +5,4 @@#[cfg(not(test))] #[cfg(not(testlib))] mod allocator; +pub mod vecext;
rust/kernel/alloc/vecext.rs new file +52 -0
52 lines not on this page · raw message
rust/kernel/lib.rs +1 -0
@@ -19,6 +19,7 @@#![feature(offset_of)] #![feature(receiver_trait)] #![feature(unsize)] +#![feature(vec_push_within_capacity)] // Ensure conditional compilation based on the kernel configuration works; // otherwise we may silently break things like initcall handling.
rust/kernel/prelude.rs +2 -0
@@ -14,6 +14,8 @@#[doc(no_inline)] pub use core::pin::Pin ; +pub use crate ::alloc::vecext::VecExt ; +#[doc(no_inline)] pub use alloc::{boxed::Box , vec::Vec };
Signature · 1 line
2.34.1
Re: [PATCH 02/10] rust: alloc: introduce the `VecExt` trait
On 25.03.24 20:54, Wedson Almeida Filho wrote:> From: Wedson Almeida Filho <[email protected]>
Quoted 41 lines
Make
try_with_capacity,try_push, andtry_extend_from_slicemethods available inVeceven though it doesn't implement them. It is implemented withtry_reserveandpush_within_capacity.This is in preparation for switching to the upstream
alloccrate.Suggested-by Gary Guo <[email protected]>Signed-off-by Wedson Almeida Filho <[email protected]>--- rust/kernel/alloc.rs | 1 + rust/kernel/alloc/vecext.rs | 52 +++++++++++++++++++++++++++++++++++++ rust/kernel/lib.rs | 1 + rust/kernel/prelude.rs | 2 ++ 4 files changed, 56 insertions(+)create mode 100644
rust/kernel/alloc/vecext.rsrust/kernel/alloc.rs +1
-0@@ -5,3 +5,4 @@#[cfg(not(test))] #[cfg(not(testlib))] mod allocator; +pub mod vecext;rust/kernel/alloc/vecext.rs new file +9
-0@@ -0,0 +1,52 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! Extensions to [`Vec`] for fallible allocations. + +use alloc::{collections::TryReserveError , vec::Vec }; +use core::result::Result ; + +/// Extensions to [`Vec`]. +pub trait VecExt <T >:Sized {
rust/kernel/alloc/vecext.rs line 19
Quoted 10 lines
rust/kernel/alloc/vecext.rs new file +10
-0+/// Creates a new [`Vec`] instance with at least the given capacity. +fn try_with_capacity (capacity:usize ) ->Result <Self ,TryReserveError >; + +/// Appends an element to the back of the [`Vec`] instance. +fn try_push (&mut self, v:T ) ->Result <(),TryReserveError >; + +/// Pushes clones of the elements of slice into the [`Vec`] instance. +fn try_extend_from_slice (&mut self, other: &[T ]) ->Result <(),TryReserveError > +where +T :Clone ;
All of the non try_ prefix functions require not(no_global_oom_handling), so we could also drop the try_ prefix here.
What do you think?
Quoted 15 lines
rust/kernel/alloc/vecext.rs new file +15
-0+} + +impl <T >VecExt <T >for Vec <T > { +fn try_with_capacity (capacity:usize ) ->Result <Self ,TryReserveError > { +let mut v =Vec ::new (); + v.try_reserve (capacity)?; +Ok (v) + } + +fn try_push (&mut self, v:T ) ->Result <(),TryReserveError > { +if let Err (retry) = self.push_within_capacity (v) { + self.try_reserve (1 )?; +let _ = self.push_within_capacity (retry); + } +Ok (())
rust/kernel/alloc/vecext.rs line 44
Quoted 10 lines
rust/kernel/alloc/vecext.rs new file +10
-0+ } + +fn try_extend_from_slice (&mut self, other: &[T ]) ->Result <(),TryReserveError > +where +T :Clone , + { +let extra_cap = self.capacity () - self.len (); +if extra_cap >0 { + self.try_reserve (extra_cap)?; + }
I am confused, why are you doing this? Did you mean to do this?:
let extra_cap = self.capacity() - self.len() - other.len();
Signature · 2 lines
Cheers, Benno
Quoted 36 lines
rust/kernel/alloc/vecext.rs new file +8
-0+ +for itemin other { + self.try_push (item.clone ())?; + } + +Ok (()) + } +}rust/kernel/lib.rs +1
-0@@ -19,6 +19,7 @@#![feature(offset_of)] #![feature(receiver_trait)] #![feature(unsize)] +#![feature(vec_push_within_capacity)] // Ensure conditional compilation based on the kernel configuration works; // otherwise we may silently break things like initcall handling. rust/kernel/prelude.rs +2
-1@@ -14,6 +14,8 @@#[doc(no_inline)] pub use core::pin::Pin ; +pub use crate ::alloc::vecext::VecExt ; +#[doc(no_inline)] pub use alloc::{boxed::Box , vec::Vec }; --2.34.1
Re: [PATCH 02/10] rust: alloc: introduce the `VecExt` trait
Quoted 50 lines · Benno Lossin
On 25.03.24 20:54, Wedson Almeida Filho wrote:> From: Wedson Almeida Filho <[email protected]>
Quoted 48 lines
Make
try_with_capacity,try_push, andtry_extend_from_slicemethods available inVeceven though it doesn't implement them. It is implemented withtry_reserveandpush_within_capacity.This is in preparation for switching to the upstream
alloccrate.Suggested-by Gary Guo <[email protected]>Signed-off-by Wedson Almeida Filho <[email protected]>--- rust/kernel/alloc.rs | 1 + rust/kernel/alloc/vecext.rs | 52 +++++++++++++++++++++++++++++++++++++ rust/kernel/lib.rs | 1 + rust/kernel/prelude.rs | 2 ++ 4 files changed, 56 insertions(+)create mode 100644
rust/kernel/alloc/vecext.rsrust/kernel/alloc.rs +1
-0@@ -5,3 +5,4 @@#[cfg(not(test))] #[cfg(not(testlib))] mod allocator; +pub mod vecext;rust/kernel/alloc/vecext.rs new file +16
-0@@ -0,0 +1,52 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! Extensions to [`Vec`] for fallible allocations. + +use alloc::{collections::TryReserveError , vec::Vec }; +use core::result::Result ; + +/// Extensions to [`Vec`]. +pub trait VecExt <T >:Sized { +/// Creates a new [`Vec`] instance with at least the given capacity. +fn try_with_capacity (capacity:usize ) ->Result <Self ,TryReserveError >; + +/// Appends an element to the back of the [`Vec`] instance. +fn try_push (&mut self, v:T ) ->Result <(),TryReserveError >; + +/// Pushes clones of the elements of slice into the [`Vec`] instance.
Quoted 8 lines
rust/kernel/alloc/vecext.rs line 19
Quoted 3 lines
rust/kernel/alloc/vecext.rs new file +3
-0+fn try_extend_from_slice (&mut self, other: &[T ]) ->Result <(),TryReserveError > +where +T :Clone ;All of the non
try_prefix functions requirenot(no_global_oom_handling), so we could also drop thetry_prefix here.
What do you think?
Yes, I do drop the try_ prefixes in patch 7.
Patches 1 through 4 remove the fork with minimal changes. Patches 5 to 10 introduce new functions with flags and drop allocator_api.
Quoted 27 lines
Quoted 15 lines
rust/kernel/alloc/vecext.rs new file +15
-0+} + +impl <T >VecExt <T >for Vec <T > { +fn try_with_capacity (capacity:usize ) ->Result <Self ,TryReserveError > { +let mut v =Vec ::new (); + v.try_reserve (capacity)?; +Ok (v) + } + +fn try_push (&mut self, v:T ) ->Result <(),TryReserveError > { +if let Err (retry) = self.push_within_capacity (v) { + self.try_reserve (1 )?; +let _ = self.push_within_capacity (retry); + } +Ok (())rust/kernel/alloc/vecext.rs line 44
Quoted 10 lines
rust/kernel/alloc/vecext.rs new file +10
-0+ } + +fn try_extend_from_slice (&mut self, other: &[T ]) ->Result <(),TryReserveError > +where +T :Clone , + { +let extra_cap = self.capacity () - self.len (); +if extra_cap >0 { + self.try_reserve (extra_cap)?; + }I am confused, why are you doing this? Did you mean to do this?:
Quoted 1 line
let extra_cap = self.capacity() - self.len() - other.len();
I originally meant extracap = other.len() - self.capacity() + self.len() because originally I thought additional meant additional capacity (so the final capacity would be original capacity plus additional). But after I looked at the code I learned that additional is really the minimum capacity.
Then I decided that we don't need this check at all in try_extend_from_slice, which I removed in patch 7, I just call reserve directly with other.len().
The code happens to work now because the try_push calls below ensure the capacity gets expanded. In any case, I'll fix this in v2.
Quoted 40 lines
--
Cheers,
BennoQuoted 36 lines
rust/kernel/alloc/vecext.rs new file +8
-0+ +for itemin other { + self.try_push (item.clone ())?; + } + +Ok (()) + } +}rust/kernel/lib.rs +1
-0@@ -19,6 +19,7 @@#![feature(offset_of)] #![feature(receiver_trait)] #![feature(unsize)] +#![feature(vec_push_within_capacity)] // Ensure conditional compilation based on the kernel configuration works; // otherwise we may silently break things like initcall handling. rust/kernel/prelude.rs +2
-1@@ -14,6 +14,8 @@#[doc(no_inline)] pub use core::pin::Pin ; +pub use crate ::alloc::vecext::VecExt ; +#[doc(no_inline)] pub use alloc::{boxed::Box , vec::Vec }; --2.34.1
PATCH 3/10 kbuild: use the upstream `alloc` crate
In reply to Wedson Almeida FilhoFrom: Wedson Almeida Filho <[email protected]>
Switch away from our fork of the alloc crate. We remove it altogether in the next commit.
--- rust/Makefile | 16 +++------------- rust/kernel/str.rs | 1 + scripts/generate_rust_analyzer.py | 2 +- 3 files changed, 5 insertions(+), 14 deletions(-)
rust/Makefile +3 -13
@@ -61,15 +61,9 @@ core-cfgs = \ --cfg no_fp_fmt_parse alloc-cfgs = \ - --cfg no_borrow \ - --cfg no_fmt \ --cfg no_global_oom_handling \ - --cfg no_macros \ --cfg no_rc \ - --cfg no_str \ - --cfg no_string \ - --cfg no_sync \ - --cfg no_thin + --cfg no_sync quiet_cmd_rustdoc = RUSTDOC $(if $(rustdoc_host),H, ) $< cmd_rustdoc = \
@@ -123,7 +117,7 @@ rustdoc-compiler_builtins: $(src)/compiler_builtins.rs rustdoc-core FORCE # due to things that are "configured out" vs. entirely non-existing ones. rustdoc-alloc: private rustc_target_flags = $(alloc-cfgs) \ -Arustdoc::broken_intra_doc_links -rustdoc-alloc: $(src)/alloc/lib.rs rustdoc-core rustdoc-compiler_builtins FORCE +rustdoc-alloc: $(RUST_LIB_SRC)/alloc/src/lib.rs rustdoc-core rustdoc-compiler_builtins FORCE +$(call if_changed,rustdoc) rustdoc-kernel: private rustc_target_flags = --extern alloc \
@@ -219,8 +213,6 @@ rusttest: rusttest-macros rusttest-kernel # - `cargo` only considers the use case of building the standard library # to use it in a given package. Thus we need to create a dummy package # and pick the generated libraries from there. -# - Since we only keep a subset of upstream `alloc` in-tree, we need -# to recreate it on the fly by putting our sources on top. # - The usual ways of modifying the dependency graph in `cargo` do not seem # to apply for the `-Zbuild-std` steps, thus we have to mislead it # by modifying the sources in the sysroot.
@@ -239,8 +231,6 @@ quiet_cmd_rustsysroot = RUSTSYSROOT rm -rf $(objtree)/$(obj)/test; \ mkdir -p $(objtree)/$(obj)/test; \ cp -a $(rustc_sysroot) $(objtree)/$(obj)/test/sysroot; \ - cp -r $(srctree)/$(src)/alloc/* \ - $(objtree)/$(obj)/test/sysroot/lib/rustlib/src/rust/library/alloc/src; \ echo '\#!/bin/sh' > $(objtree)/$(obj)/test/rustc_sysroot; \ echo "$(RUSTC) --sysroot=$(abspath $(objtree)/$(obj)/test/sysroot) \"\$$@\"" \ >> $(objtree)/$(obj)/test/rustc_sysroot; \
@@ -444,7 +434,7 @@ $(obj)/compiler_builtins.o: $(src)/compiler_builtins.rs $(obj)/core.o FORCE $(obj)/alloc.o: private skip_clippy = 1 $(obj)/alloc.o: private skip_flags = -Dunreachable_pub $(obj)/alloc.o: private rustc_target_flags = $(alloc-cfgs) -$(obj)/alloc.o: $(src)/alloc/lib.rs $(obj)/compiler_builtins.o FORCE +$(obj)/alloc.o: $(RUST_LIB_SRC)/alloc/src/lib.rs $(obj)/compiler_builtins.o FORCE +$(call if_changed_dep,rustc_library) $(obj)/build_error.o: $(src)/build_error.rs $(obj)/compiler_builtins.o FORCE
rust/kernel/str.rs +1 -0
@@ -2,6 +2,7 @@//! String representations. +use crate ::alloc::vecext::VecExt ;use alloc::alloc::AllocError ;use alloc::vec::Vec ;use core::fmt::{self,Write };
scripts/generate_rust_analyzer.py +1 -1
@@ -66,7 +66,7 @@ def generate_crates(srctree, objtree, sysroot_src, external_src, cfgs): append_crate( "alloc", - srctree / "rust" / "alloc" / "lib.rs", + sysroot_src / "alloc" / "src" / "lib.rs", ["core", "compiler_builtins"], cfg=crates_cfgs.get("alloc", []), )
Signature · 1 line
2.34.1
PATCH 4/10 rust: alloc: remove our fork of the `alloc` crate
In reply to Wedson Almeida FilhoFrom: Wedson Almeida Filho <[email protected]>
It is not used anymore as VecExt now provides the functionality we depend on.
--- rust/alloc/README.md | 36 - rust/alloc/alloc.rs | 452 ---- rust/alloc/boxed.rs | 2463 ------------------- rust/alloc/collections/mod.rs | 160 -- rust/alloc/lib.rs | 288 --- rust/alloc/raw_vec.rs | 611 ----- rust/alloc/slice.rs | 890 ------- rust/alloc/vec/drain.rs | 255 -- rust/alloc/vec/extract_if.rs | 115 - rust/alloc/vec/into_iter.rs | 454 ---- rust/alloc/vec/is_zero.rs | 204 -- rust/alloc/vec/mod.rs | 3683 ----------------------------- rust/alloc/vec/partial_eq.rs | 49 - rust/alloc/vec/set_len_on_drop.rs | 35 - rust/alloc/vec/spec_extend.rs | 119 - 15 files changed, 9814 deletions(-) delete mode 100644 rust/alloc/README.md delete mode 100644 rust/alloc/alloc.rs delete mode 100644 rust/alloc/boxed.rs delete mode 100644 rust/alloc/collections/mod.rs delete mode 100644 rust/alloc/lib.rs delete mode 100644 rust/alloc/raw_vec.rs delete mode 100644 rust/alloc/slice.rs delete mode 100644 rust/alloc/vec/drain.rs delete mode 100644 rust/alloc/vec/extract_if.rs delete mode 100644 rust/alloc/vec/into_iter.rs delete mode 100644 rust/alloc/vec/is_zero.rs delete mode 100644 rust/alloc/vec/mod.rs delete mode 100644 rust/alloc/vec/partial_eq.rs delete mode 100644 rust/alloc/vec/set_len_on_drop.rs delete mode 100644 rust/alloc/vec/spec_extend.rs
rust/alloc/README.md deleted +0 -36
@@ -1,36 +0,0 @@ -# `alloc` - -These source files come from the Rust standard library, hosted in -the <https://github.com/rust-lang/rust> repository, licensed under -"Apache-2.0 OR MIT" and adapted for kernel use. For copyright details, -see <https://github.com/rust-lang/rust/blob/master/COPYRIGHT>. - -Please note that these files should be kept as close as possible to -upstream. In general, only additions should be performed (e.g. new -methods). Eventually, changes should make it into upstream so that, -at some point, this fork can be dropped from the kernel tree. - -The Rust upstream version on top of which these files are based matches -the output of `scripts/min-tool-version.sh rustc`. - - -## Rationale - -On one hand, kernel folks wanted to keep `alloc` in-tree to have more -freedom in both workflow and actual features if actually needed -(e.g. receiver types if we ended up using them), which is reasonable. - -On the other hand, Rust folks wanted to keep `alloc` as close as -upstream as possible and avoid as much divergence as possible, which -is also reasonable. - -We agreed on a middle-ground: we would keep a subset of `alloc` -in-tree that would be as small and as close as possible to upstream. -Then, upstream can start adding the functions that we add to `alloc` -etc., until we reach a point where the kernel already knows exactly -what it needs in `alloc` and all the new methods are merged into -upstream, so that we can drop `alloc` from the kernel tree and go back -to using the upstream one. - -By doing this, the kernel can go a bit faster now, and Rust can -slowly incorporate and discuss the changes as needed.
rust/alloc/alloc.rs deleted +0 -452
452 lines not on this page · raw message
rust/alloc/boxed.rs deleted +0 -2463
2,463 lines not on this page · raw message
rust/alloc/collections/mod.rs deleted +0 -160
49 lines not on this page · raw message
rust/alloc/lib.rs deleted +0 -288
288 lines not on this page · raw message
rust/alloc/raw_vec.rs deleted +0 -611
611 lines not on this page · raw message
rust/alloc/slice.rs deleted +0 -890
890 lines not on this page · raw message
rust/alloc/vec/drain.rs deleted +0 -255
255 lines not on this page · raw message
rust/alloc/vec/extract_if.rs deleted +0 -115
115 lines not on this page · raw message
rust/alloc/vec/into_iter.rs deleted +0 -454
454 lines not on this page · raw message
rust/alloc/vec/is_zero.rs deleted +0 -204
204 lines not on this page · raw message
rust/alloc/vec/mod.rs deleted +0 -3683
3,683 lines not on this page · raw message
rust/alloc/vec/partial_eq.rs deleted +0 -49
49 lines not on this page · raw message
rust/alloc/vec/set_len_on_drop.rs deleted +0 -35
Lines of this file are not shown. View the raw message.
rust/alloc/vec/spec_extend.rs deleted +0 -119
119 lines not on this page · raw message
The rest of this diff is not shown. View the raw message.
Signature · 1 line
2.34.1
Part of the diff is not shown here. View the raw message.
Re: [PATCH 04/10] rust: alloc: remove our fork of the `alloc` crate
Quoted 15 lines · Wedson Almeida Filho
From: Wedson Almeida Filho <[email protected]>
It is not used anymore as
VecExtnow provides the functionality we depend on.Signed-off-by Wedson Almeida Filho <[email protected]>--- rust/alloc/README.md | 36 - rust/alloc/alloc.rs | 452 ---- rust/alloc/boxed.rs | 2463 ------------------- rust/alloc/collections/mod.rs | 160 -- rust/alloc/lib.rs | 288 --- rust/alloc/raw_vec.rs | 611 ----- rust/alloc/slice.rs | 890 ------- rust/alloc/vec/drain.rs | 255 --
Quoted 8 lines
rust/alloc/vec/extract_if.rs | 115 - rust/alloc/vec/into_iter.rs | 454 ---- rust/alloc/vec/is_zero.rs | 204 -- rust/alloc/vec/mod.rs | 3683 ----------------------------- rust/alloc/vec/partial_eq.rs | 49 - rust/alloc/vec/set_len_on_drop.rs | 35 - rust/alloc/vec/spec_extend.rs | 119 - 15 files changed, 9814 deletions(-)
Signature · 2 lines
Cheers, Benno
PATCH 5/10 rust: alloc: introduce allocation flags
In reply to Wedson Almeida FilhoFrom: Wedson Almeida Filho <[email protected]>
We'll use them when allocating Box, Arc, and UniqueArc instances, as well as when allocating memory for Vec elements. These changes will come in subsequent patches.
--- rust/bindings/bindings_helper.h | 3 ++ rust/kernel/alloc.rs | 56 +++++++++++++++++++++++++++++++++ rust/kernel/prelude.rs | 2 +- 3 files changed, 60 insertions(+), 1 deletion(-)
rust/bindings/bindings_helper.h +3 -0
@@ -20,5 +20,8 @@/* `bindgen` gets confused at certain things. */ const size_t RUST_CONST_HELPER_ARCH_SLAB_MINALIGN =ARCH_SLAB_MINALIGN ; +const gfp_t RUST_CONST_HELPER_GFP_ATOMIC =GFP_ATOMIC ;const gfp_t RUST_CONST_HELPER_GFP_KERNEL =GFP_KERNEL ; +const gfp_t RUST_CONST_HELPER_GFP_KERNEL_ACCOUNT =GFP_KERNEL_ACCOUNT ; +const gfp_t RUST_CONST_HELPER_GFP_NOWAIT =GFP_NOWAIT ;const gfp_t RUST_CONST_HELPER___GFP_ZERO = __GFP_ZERO;
rust/kernel/alloc.rs +56 -0
59 lines not on this page · raw message
rust/kernel/prelude.rs +1 -1
@@ -14,7 +14,7 @@#[doc(no_inline)] pub use core::pin::Pin ; -pub use crate ::alloc::vecext::VecExt ; +pub use crate ::alloc::{flags::*, vecext::VecExt };#[doc(no_inline)] pub use alloc::{boxed::Box , vec::Vec };
Signature · 1 line
2.34.1
Re: [PATCH 05/10] rust: alloc: introduce allocation flags
Quoted 7 lines · Wedson Almeida Filho
From: Wedson Almeida Filho <[email protected]>
We'll use them when allocating
Box,Arc, andUniqueArcinstances, as well as when allocating memory forVecelements. These changes will come in subsequent patches.Signed-off-by Wedson Almeida Filho <[email protected]>
Signature · 2 lines
Cheers, Benno
PATCH 6/10 rust: alloc: introduce the `BoxExt` trait
In reply to Wedson Almeida FilhoFrom: Wedson Almeida Filho <[email protected]>
Make fallible versions of new and new_uninit methods available in Box even though it doesn't implement them because we build alloc with the no_global_oom_handling config.
They also have an extra flags parameter that allows callers to pass flags to the allocator.
--- rust/kernel/alloc.rs | 1 + rust/kernel/alloc/allocator.rs | 6 +++- rust/kernel/alloc/boxext.rs | 61 ++++++++++++++++++++++++++++++++++ rust/kernel/init.rs | 13 ++++---- rust/kernel/prelude.rs | 2 +- rust/kernel/sync/arc.rs | 3 +- 6 files changed, 77 insertions(+), 9 deletions(-) create mode 100644 rust/kernel/alloc/boxext.rs
rust/kernel/alloc.rs +1 -0
@@ -5,6 +5,7 @@#[cfg(not(test))] #[cfg(not(testlib))] mod allocator; +pub mod boxext;pub mod vecext;/// Flags to be used when allocating memory.
rust/kernel/alloc/allocator.rs +5 -1
@@ -15,7 +15,11 @@/// /// - `ptr` can be either null or a pointer which has been allocated by this allocator. /// - `new_layout` must have a non-zero size. -unsafe fn krealloc_aligned (ptr: *mut u8 , new_layout:Layout , flags: bindings::gfp_t ) -> *mut u8 { +pub (crate )unsafe fn krealloc_aligned ( + ptr: *mut u8 , + new_layout:Layout , + flags: bindings::gfp_t , +) -> *mut u8 {// Customized layouts from `Layout::from_size_align()` can have size < align, so pad first. let layout = new_layout.pad_to_align ();
rust/kernel/alloc/boxext.rs new file +61 -0
61 lines not on this page · raw message
rust/kernel/init.rs +7 -6
@@ -210,6 +210,7 @@//! [`pin_init!`]: crate::pin_init! use crate ::{ + alloc::{boxext::BoxExt , flags::*}, error::{self,Error }, sync::UniqueArc , types::{Opaque ,ScopeGuard },
@@ -305,9 +306,9 @@ macro_rules! stack_pin_init {/// /// stack_try_pin_init!(let foo: Result<Pin<&mut Foo>, AllocError> = pin_init!(Foo { /// a <- new_mutex!(42), -/// b: Box::try_new(Bar { +/// b: Box::new(Bar { /// x: 64, -/// })?, +/// }, GFP_KERNEL)?, /// })); /// let foo = foo.unwrap(); /// pr_info!("a: {}", &*foo.a.lock());
@@ -331,9 +332,9 @@ macro_rules! stack_pin_init {/// /// stack_try_pin_init!(let foo: Pin<&mut Foo> =? pin_init!(Foo { /// a <- new_mutex!(42), -/// b: Box::try_new(Bar { +/// b: Box::new(Bar { /// x: 64, -/// })?, +/// }, GFP_KERNEL)?, /// })); /// pr_info!("a: {}", &*foo.a.lock()); /// # Ok::<_, AllocError>(())
@@ -1158,7 +1159,7 @@ fn try_pin_init<E>(init: impl PinInit<T, E>) -> Result<Pin<Self>, E> where E: From<AllocError>, { -let mut this =Box ::try_new_uninit ()?; +let mut this = <Box <_ >as BoxExt <_ >>::new_uninit (GFP_KERNEL )?;let slot = this.as_mut_ptr ();// SAFETY: When init errors/panics, slot will get deallocated but not dropped, // slot is valid and will not be moved, because we pin it later.
@@ -1172,7 +1173,7 @@ fn try_init<E>(init: impl Init<T, E>) -> Result<Self, E> where E: From<AllocError>, { -let mut this =Box ::try_new_uninit ()?; +let mut this = <Box <_ >as BoxExt <_ >>::new_uninit (GFP_KERNEL )?;let slot = this.as_mut_ptr ();// SAFETY: When init errors/panics, slot will get deallocated but not dropped, // slot is valid.
rust/kernel/prelude.rs +1 -1
@@ -14,7 +14,7 @@#[doc(no_inline)] pub use core::pin::Pin ; -pub use crate ::alloc::{flags::*, vecext::VecExt }; +pub use crate ::alloc::{boxext::BoxExt , flags::*, vecext::VecExt };#[doc(no_inline)] pub use alloc::{boxed::Box , vec::Vec };
rust/kernel/sync/arc.rs +2 -1
@@ -16,6 +16,7 @@//! [`Arc`]: https://doc.rust-lang.org/std/sync/struct.Arc.html use crate ::{ + alloc::{boxext::BoxExt , flags::*}, bindings, error::{self,Error }, init::{self,InPlaceInit ,Init ,PinInit },
@@ -170,7 +171,7 @@ pub fn try_new(contents: T) -> Result<Self, AllocError> { data: contents, }; -let inner =Box ::try_new (value)?; +let inner = <Box <_ >as BoxExt <_ >>::new (value,GFP_KERNEL )?;// SAFETY: We just created `inner` with a reference count of 1, which is owned by the new // `Arc` object.
Signature · 1 line
2.34.1
Re: [PATCH 06/10] rust: alloc: introduce the `BoxExt` trait
Quoted 73 lines · Wedson Almeida Filho
From: Wedson Almeida Filho <[email protected]>
Make fallible versions of
newandnew_uninitmethods available inBoxeven though it doesn't implement them because we buildallocwith theno_global_oom_handlingconfig.They also have an extra
flagsparameter that allows callers to pass flags to the allocator.Signed-off-by Wedson Almeida Filho <[email protected]>--- rust/kernel/alloc.rs | 1 + rust/kernel/alloc/allocator.rs | 6 +++- rust/kernel/alloc/boxext.rs | 61 ++++++++++++++++++++++++++++++++++ rust/kernel/init.rs | 13 ++++---- rust/kernel/prelude.rs | 2 +- rust/kernel/sync/arc.rs | 3 +- 6 files changed, 77 insertions(+), 9 deletions(-)create mode 100644
rust/kernel/alloc/boxext.rsrust/kernel/alloc.rs +1
-0@@ -5,6 +5,7 @@#[cfg(not(test))] #[cfg(not(testlib))] mod allocator; +pub mod boxext;pub mod vecext;/// Flags to be used when allocating memory. rust/kernel/alloc/allocator.rs +5
-1@@ -15,7 +15,11 @@/// /// - `ptr` can be either null or a pointer which has been allocated by this allocator. /// - `new_layout` must have a non-zero size. -unsafe fn krealloc_aligned (ptr: *mut u8 , new_layout:Layout , flags: bindings::gfp_t ) -> *mut u8 { +pub (crate )unsafe fn krealloc_aligned ( + ptr: *mut u8 , + new_layout:Layout , + flags: bindings::gfp_t , +) -> *mut u8 {// Customized layouts from `Layout::from_size_align()` can have size < align, so pad first. let layout = new_layout.pad_to_align ();rust/kernel/alloc/boxext.rs new file +18
-0@@ -0,0 +1,61 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! Extensions to [`Box`] for fallible allocations. + +use super ::Flags ; +use alloc::boxed::Box ; +use core::alloc::AllocError ; +use core::mem::MaybeUninit ; +use core::result::Result ; + +/// Extensions to [`Box`]. +pub trait BoxExt <T >:Sized { +/// Allocates a new box. +/// +/// The allocation may fail, in which case an error is returned. +fn new (x:T , flags:Flags ) ->Result <Self ,AllocError >; + +/// Allocates a new uninitialised box.
rust/kernel/alloc/boxext.rs line 28
Quoted 10 lines
rust/kernel/alloc/boxext.rs new file +10
-0+/// +/// The allocation may fail, in which case an error is returned. +fn new_uninit (flags:Flags ) ->Result <Box <MaybeUninit <T >>,AllocError >; +} + +impl <T >BoxExt <T >for Box <T > { +#[cfg(any(test, testlib))] +fn new (x:T , _flags:Flags ) ->Result <Self ,AllocError > { +Ok (Box ::new (x)) + }
When running under cfg(test), are we using the normal standard library? Or why is this needed?
rust/kernel/alloc/boxext.rs line 38
Quoted 10 lines
rust/kernel/alloc/boxext.rs new file +10
-0+ +#[cfg(not(any(test, testlib)))] +fn new (x:T , flags:Flags ) ->Result <Self ,AllocError > { +let ptr =if core::mem::size_of ::<T >() ==0 { + core::ptr::NonNull ::<T >::dangling ().as_ptr () + }else { +let layout = core::alloc::Layout ::new ::<T >(); + +// SAFETY: Memory is being allocated (first arg is null). The only other source of +// safety issues is sleeping on atomic context, which is addressed by klint.
The krealloc_aligned function states:
/// # Safety
///
/// - ptr can be either null or a pointer which has been allocated by this allocator. /// - new_layout must have a non-zero size.
So it should also mention that you checked for layout.size() > 0 above.
Quoted 11 lines
rust/kernel/alloc/boxext.rs new file +11
-0+let ptr =unsafe { +super ::allocator::krealloc_aligned (core::ptr::null_mut (), layout, flags.0 ) + }; +if ptr.is_null () { +return Err (AllocError ); + } + +let ptr = ptr.cast ::<T >(); + +// SAFETY: We just allocated the memory above, it is valid for write. +unsafe { ptr.write (x) };
rust/kernel/alloc/boxext.rs line 59
Quoted 10 lines
rust/kernel/alloc/boxext.rs new file +10
-0+ ptr + }; + +// SAFETY: For non-zero-sized types, we allocate above using the global allocator. For +// zero-sized types, we use `NonNull::dangling`. +Ok (unsafe {Box ::from_raw (ptr) }) + } + +fn new_uninit (flags:Flags ) ->Result <Box <MaybeUninit <T >>,AllocError > { + <Box <_ >as BoxExt <_ >>::new (MaybeUninit ::<T >::uninit (), flags)
Why do you use the extended syntax? I tried to use Box::new and it compiled.
Signature · 2 lines
Cheers, Benno
Re: [PATCH 06/10] rust: alloc: introduce the `BoxExt` trait
Quoted 80 lines · Benno Lossin
Quoted 78 lines · Wedson Almeida Filho
From: Wedson Almeida Filho <[email protected]>
Make fallible versions of
newandnew_uninitmethods available inBoxeven though it doesn't implement them because we buildallocwith theno_global_oom_handlingconfig.They also have an extra
flagsparameter that allows callers to pass flags to the allocator.Signed-off-by Wedson Almeida Filho <[email protected]>--- rust/kernel/alloc.rs | 1 + rust/kernel/alloc/allocator.rs | 6 +++- rust/kernel/alloc/boxext.rs | 61 ++++++++++++++++++++++++++++++++++ rust/kernel/init.rs | 13 ++++---- rust/kernel/prelude.rs | 2 +- rust/kernel/sync/arc.rs | 3 +- 6 files changed, 77 insertions(+), 9 deletions(-)create mode 100644
rust/kernel/alloc/boxext.rsrust/kernel/alloc.rs +1
-0@@ -5,6 +5,7 @@#[cfg(not(test))] #[cfg(not(testlib))] mod allocator; +pub mod boxext;pub mod vecext;/// Flags to be used when allocating memory. rust/kernel/alloc/allocator.rs +5
-1@@ -15,7 +15,11 @@/// /// - `ptr` can be either null or a pointer which has been allocated by this allocator. /// - `new_layout` must have a non-zero size. -unsafe fn krealloc_aligned (ptr: *mut u8 , new_layout:Layout , flags: bindings::gfp_t ) -> *mut u8 { +pub (crate )unsafe fn krealloc_aligned ( + ptr: *mut u8 , + new_layout:Layout , + flags: bindings::gfp_t , +) -> *mut u8 {// Customized layouts from `Layout::from_size_align()` can have size < align, so pad first. let layout = new_layout.pad_to_align ();rust/kernel/alloc/boxext.rs new file +23
-0@@ -0,0 +1,61 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! Extensions to [`Box`] for fallible allocations. + +use super ::Flags ; +use alloc::boxed::Box ; +use core::alloc::AllocError ; +use core::mem::MaybeUninit ; +use core::result::Result ; + +/// Extensions to [`Box`]. +pub trait BoxExt <T >:Sized { +/// Allocates a new box. +/// +/// The allocation may fail, in which case an error is returned. +fn new (x:T , flags:Flags ) ->Result <Self ,AllocError >; + +/// Allocates a new uninitialised box. +/// +/// The allocation may fail, in which case an error is returned. +fn new_uninit (flags:Flags ) ->Result <Box <MaybeUninit <T >>,AllocError >; +} +
Quoted 8 lines
rust/kernel/alloc/boxext.rs line 28
Quoted 5 lines
rust/kernel/alloc/boxext.rs new file +5
-0+impl <T >BoxExt <T >for Box <T > { +#[cfg(any(test, testlib))] +fn new (x:T , _flags:Flags ) ->Result <Self ,AllocError > { +Ok (Box ::new (x)) + }When running under
cfg(test), are we using the normal standard library? Or why is this needed?
Because it uses 34 other crates that rely on Box::new and friends.
I discussed this with Miguel recently and once he's done with the build system changes, he will think about what to do with tests. It may be that we abandon the current method of running standalone tests and run everything in kunit, or perhaps we'll find a way to exclude code that won't run in standalone tests anyway...
Quoted 13 lines
rust/kernel/alloc/boxext.rs line 38
Quoted 10 lines
rust/kernel/alloc/boxext.rs new file +10
-0+ +#[cfg(not(any(test, testlib)))] +fn new (x:T , flags:Flags ) ->Result <Self ,AllocError > { +let ptr =if core::mem::size_of ::<T >() ==0 { + core::ptr::NonNull ::<T >::dangling ().as_ptr () + }else { +let layout = core::alloc::Layout ::new ::<T >(); + +// SAFETY: Memory is being allocated (first arg is null). The only other source of +// safety issues is sleeping on atomic context, which is addressed by klint. The
krealloc_alignedfunction states:
Quoted 7 lines
/// # Safety
///
/// -ptrcan be either null or a pointer which has been allocated by this allocator. /// -new_layoutmust have a non-zero size.So it should also mention that you checked for
layout.size() > 0above.
Good point. I mentioned this in the VecExt version but not here. I will update this for v2.
Quoted 16 lines
Quoted 16 lines
rust/kernel/alloc/boxext.rs new file +16
-0+let ptr =unsafe { +super ::allocator::krealloc_aligned (core::ptr::null_mut (), layout, flags.0 ) + }; +if ptr.is_null () { +return Err (AllocError ); + } + +let ptr = ptr.cast ::<T >(); + +// SAFETY: We just allocated the memory above, it is valid for write. +unsafe { ptr.write (x) }; + ptr + }; + +// SAFETY: For non-zero-sized types, we allocate above using the global allocator. For +// zero-sized types, we use `NonNull::dangling`.
Quoted 8 lines
rust/kernel/alloc/boxext.rs line 59
Quoted 5 lines
rust/kernel/alloc/boxext.rs new file +5
-0+Ok (unsafe {Box ::from_raw (ptr) }) + } + +fn new_uninit (flags:Flags ) ->Result <Box <MaybeUninit <T >>,AllocError > { + <Box <_ >as BoxExt <_ >>::new (MaybeUninit ::<T >::uninit (), flags)Why do you use the extended syntax? I tried to use
Box::newand it compiled.
It works when compiling the kernel but fails when compiling for userspace with regular (no_global_oom_handling disabled) alloc when running make rusttest. In the latter case, it chooses the inherent version of Box::new which is infallible and doesn't take flags so it fails to compile.
Using the extended syntax allows it always pick the right version, regardless of how alloc is compiled.
There are 5 places in existing code that required this change and this is limited to the kernel crate (e.g., drivers, samples and documentation examples can continue to use Box::new). So we thought it was ok until Miguel figures out what we want to do with tests.
Re: [PATCH 06/10] rust: alloc: introduce the `BoxExt` trait
Quoted 23 lines · Wedson Almeida Filho
Quoted 22 lines · Benno Lossin
Quoted 20 lines · Wedson Almeida Filho
From: Wedson Almeida Filho <[email protected]>
Make fallible versions of
newandnew_uninitmethods available inBoxeven though it doesn't implement them because we buildallocwith theno_global_oom_handlingconfig.They also have an extra
flagsparameter that allows callers to pass flags to the allocator.Signed-off-by Wedson Almeida Filho <[email protected]>--- rust/kernel/alloc.rs | 1 + rust/kernel/alloc/allocator.rs | 6 +++- rust/kernel/alloc/boxext.rs | 61 ++++++++++++++++++++++++++++++++++ rust/kernel/init.rs | 13 ++++---- rust/kernel/prelude.rs | 2 +- rust/kernel/sync/arc.rs | 3 +- 6 files changed, 77 insertions(+), 9 deletions(-)create mode 100644
rust/kernel/alloc/boxext.rs
Quoted 10 lines
Quoted 10 lines
rust/kernel/alloc.rs line 9
Quoted 10 lines
rust/kernel/alloc.rs +1
-0@@ -5,6 +5,7 @@#[cfg(not(test))] #[cfg(not(testlib))] mod allocator; +pub mod boxext;pub mod vecext;
One thing I forgot to say: I think these modules should be named box_ext and vec_ext. It fits better with the usual style.
Quoted 57 lines
Quoted 48 lines
Quoted 48 lines
rust/kernel/alloc.rs +0
-0/// Flags to be used when allocating memory. rust/kernel/alloc/allocator.rs +5
-1@@ -15,7 +15,11 @@/// /// - `ptr` can be either null or a pointer which has been allocated by this allocator. /// - `new_layout` must have a non-zero size. -unsafe fn krealloc_aligned (ptr: *mut u8 , new_layout:Layout , flags: bindings::gfp_t ) -> *mut u8 { +pub (crate )unsafe fn krealloc_aligned ( + ptr: *mut u8 , + new_layout:Layout , + flags: bindings::gfp_t , +) -> *mut u8 {// Customized layouts from `Layout::from_size_align()` can have size < align, so pad first. let layout = new_layout.pad_to_align ();rust/kernel/alloc/boxext.rs new file +23
-0@@ -0,0 +1,61 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! Extensions to [`Box`] for fallible allocations. + +use super ::Flags ; +use alloc::boxed::Box ; +use core::alloc::AllocError ; +use core::mem::MaybeUninit ; +use core::result::Result ; + +/// Extensions to [`Box`]. +pub trait BoxExt <T >:Sized { +/// Allocates a new box. +/// +/// The allocation may fail, in which case an error is returned. +fn new (x:T , flags:Flags ) ->Result <Self ,AllocError >; + +/// Allocates a new uninitialised box. +/// +/// The allocation may fail, in which case an error is returned. +fn new_uninit (flags:Flags ) ->Result <Box <MaybeUninit <T >>,AllocError >; +} +Quoted 8 lines
rust/kernel/alloc/boxext.rs line 28
Quoted 5 lines
rust/kernel/alloc/boxext.rs new file +5
-0+impl <T >BoxExt <T >for Box <T > { +#[cfg(any(test, testlib))] +fn new (x:T , _flags:Flags ) ->Result <Self ,AllocError > { +Ok (Box ::new (x)) + }When running under
cfg(test), are we using the normal standard library? Or why is this needed?
Quoted 7 lines
Because it uses 34 other crates that rely on
Box::newand friends.I discussed this with Miguel recently and once he's done with the build system changes, he will think about what to do with tests. It may be that we abandon the current method of running standalone tests and run everything in kunit, or perhaps we'll find a way to exclude code that won't run in standalone tests anyway...
Ah I see, I think it would be nice to not need this. Let's see what the new build system can do here.
Quoted 56 lines
Quoted 14 lines
rust/kernel/alloc/boxext.rs line 38
Quoted 11 lines
rust/kernel/alloc/boxext.rs new file +10
-0+ +#[cfg(not(any(test, testlib)))] +fn new (x:T , flags:Flags ) ->Result <Self ,AllocError > { +let ptr =if core::mem::size_of ::<T >() ==0 { + core::ptr::NonNull ::<T >::dangling ().as_ptr () + }else { +let layout = core::alloc::Layout ::new ::<T >(); + +// SAFETY: Memory is being allocated (first arg is null). The only other source of +// safety issues is sleeping on atomic context, which is addressed by klint. The
krealloc_alignedfunction states:Quoted 7 lines
/// # Safety
///
/// -ptrcan be either null or a pointer which has been allocated by this allocator. /// -new_layoutmust have a non-zero size.So it should also mention that you checked for
layout.size() > 0above.Good point. I mentioned this in the VecExt version but not here. I will update this for v2.
Quoted 16 lines
Quoted 16 lines
rust/kernel/alloc/boxext.rs new file +16
-0+let ptr =unsafe { +super ::allocator::krealloc_aligned (core::ptr::null_mut (), layout, flags.0 ) + }; +if ptr.is_null () { +return Err (AllocError ); + } + +let ptr = ptr.cast ::<T >(); + +// SAFETY: We just allocated the memory above, it is valid for write. +unsafe { ptr.write (x) }; + ptr + }; + +// SAFETY: For non-zero-sized types, we allocate above using the global allocator. For +// zero-sized types, we use `NonNull::dangling`. Quoted 8 lines
rust/kernel/alloc/boxext.rs line 59
Quoted 5 lines
rust/kernel/alloc/boxext.rs new file +5
-0+Ok (unsafe {Box ::from_raw (ptr) }) + } + +fn new_uninit (flags:Flags ) ->Result <Box <MaybeUninit <T >>,AllocError > { + <Box <_ >as BoxExt <_ >>::new (MaybeUninit ::<T >::uninit (), flags)Why do you use the extended syntax? I tried to use
Box::newand it compiled.It works when compiling the kernel but fails when compiling for userspace with regular (
no_global_oom_handlingdisabled)allocwhen runningmake rusttest. In the latter case, it chooses the inherent version ofBox::newwhich is infallible and doesn't take flags so it fails to compile.
Quoted 7 lines
Using the extended syntax allows it always pick the right version, regardless of how
allocis compiled.There are 5 places in existing code that required this change and this is limited to the kernel crate (e.g., drivers, samples and documentation examples can continue to use
Box::new). So we thought it was ok until Miguel figures out what we want to do with tests.
Thanks for the explanation, again it would be nice to be able to just write Box::new.
Signature · 2 lines
Cheers, Benno
Re: [PATCH 06/10] rust: alloc: introduce the `BoxExt` trait
Quoted 25 lines · Benno Lossin
Quoted 23 lines · Wedson Almeida Filho
Quoted 22 lines · Benno Lossin
Quoted 20 lines · Wedson Almeida Filho
From: Wedson Almeida Filho <[email protected]>
Make fallible versions of
newandnew_uninitmethods available inBoxeven though it doesn't implement them because we buildallocwith theno_global_oom_handlingconfig.They also have an extra
flagsparameter that allows callers to pass flags to the allocator.Signed-off-by Wedson Almeida Filho <[email protected]>--- rust/kernel/alloc.rs | 1 + rust/kernel/alloc/allocator.rs | 6 +++- rust/kernel/alloc/boxext.rs | 61 ++++++++++++++++++++++++++++++++++ rust/kernel/init.rs | 13 ++++---- rust/kernel/prelude.rs | 2 +- rust/kernel/sync/arc.rs | 3 +- 6 files changed, 77 insertions(+), 9 deletions(-)create mode 100644
rust/kernel/alloc/boxext.rs
Quoted 13 lines
Quoted 10 lines
Quoted 10 lines
rust/kernel/alloc.rs line 9
Quoted 10 lines
rust/kernel/alloc.rs +1
-0@@ -5,6 +5,7 @@#[cfg(not(test))] #[cfg(not(testlib))] mod allocator; +pub mod boxext;pub mod vecext;One thing I forgot to say: I think these modules should be named
box_extandvec_ext. It fits better with the usual style.
Yeah, looking at std for precedents, I see one module called backtrace but also assert_matches and async_iter.
Snake case is the standard anyway, so I'll make the change for v2.
Quoted 140 lines
Quoted 58 lines
Quoted 49 lines
Quoted 49 lines
rust/kernel/alloc.rs +0
-0/// Flags to be used when allocating memory. rust/kernel/alloc/allocator.rs +5
-1@@ -15,7 +15,11 @@/// /// - `ptr` can be either null or a pointer which has been allocated by this allocator. /// - `new_layout` must have a non-zero size. -unsafe fn krealloc_aligned (ptr: *mut u8 , new_layout:Layout , flags: bindings::gfp_t ) -> *mut u8 { +pub (crate )unsafe fn krealloc_aligned ( + ptr: *mut u8 , + new_layout:Layout , + flags: bindings::gfp_t , +) -> *mut u8 {// Customized layouts from `Layout::from_size_align()` can have size < align, so pad first. let layout = new_layout.pad_to_align ();rust/kernel/alloc/boxext.rs new file +23
-0@@ -0,0 +1,61 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! Extensions to [`Box`] for fallible allocations. + +use super ::Flags ; +use alloc::boxed::Box ; +use core::alloc::AllocError ; +use core::mem::MaybeUninit ; +use core::result::Result ; + +/// Extensions to [`Box`]. +pub trait BoxExt <T >:Sized { +/// Allocates a new box. +/// +/// The allocation may fail, in which case an error is returned. +fn new (x:T , flags:Flags ) ->Result <Self ,AllocError >; + +/// Allocates a new uninitialised box. +/// +/// The allocation may fail, in which case an error is returned. +fn new_uninit (flags:Flags ) ->Result <Box <MaybeUninit <T >>,AllocError >; +} +Quoted 8 lines
rust/kernel/alloc/boxext.rs line 28
Quoted 5 lines
rust/kernel/alloc/boxext.rs new file +5
-0+impl <T >BoxExt <T >for Box <T > { +#[cfg(any(test, testlib))] +fn new (x:T , _flags:Flags ) ->Result <Self ,AllocError > { +Ok (Box ::new (x)) + }When running under
cfg(test), are we using the normal standard library? Or why is this needed?Quoted 7 lines
Because it uses 34 other crates that rely on
Box::newand friends.I discussed this with Miguel recently and once he's done with the build system changes, he will think about what to do with tests. It may be that we abandon the current method of running standalone tests and run everything in kunit, or perhaps we'll find a way to exclude code that won't run in standalone tests anyway...
Ah I see, I think it would be nice to not need this. Let's see what the new build system can do here.
Quoted 56 lines
Quoted 14 lines
rust/kernel/alloc/boxext.rs line 38
Quoted 11 lines
rust/kernel/alloc/boxext.rs new file +10
-0+ +#[cfg(not(any(test, testlib)))] +fn new (x:T , flags:Flags ) ->Result <Self ,AllocError > { +let ptr =if core::mem::size_of ::<T >() ==0 { + core::ptr::NonNull ::<T >::dangling ().as_ptr () + }else { +let layout = core::alloc::Layout ::new ::<T >(); + +// SAFETY: Memory is being allocated (first arg is null). The only other source of +// safety issues is sleeping on atomic context, which is addressed by klint. The
krealloc_alignedfunction states:Quoted 7 lines
/// # Safety
///
/// -ptrcan be either null or a pointer which has been allocated by this allocator. /// -new_layoutmust have a non-zero size.So it should also mention that you checked for
layout.size() > 0above.Good point. I mentioned this in the VecExt version but not here. I will update this for v2.
Quoted 16 lines
Quoted 16 lines
rust/kernel/alloc/boxext.rs new file +16
-0+let ptr =unsafe { +super ::allocator::krealloc_aligned (core::ptr::null_mut (), layout, flags.0 ) + }; +if ptr.is_null () { +return Err (AllocError ); + } + +let ptr = ptr.cast ::<T >(); + +// SAFETY: We just allocated the memory above, it is valid for write. +unsafe { ptr.write (x) }; + ptr + }; + +// SAFETY: For non-zero-sized types, we allocate above using the global allocator. For +// zero-sized types, we use `NonNull::dangling`. Quoted 8 lines
rust/kernel/alloc/boxext.rs line 59
Quoted 5 lines
rust/kernel/alloc/boxext.rs new file +5
-0+Ok (unsafe {Box ::from_raw (ptr) }) + } + +fn new_uninit (flags:Flags ) ->Result <Box <MaybeUninit <T >>,AllocError > { + <Box <_ >as BoxExt <_ >>::new (MaybeUninit ::<T >::uninit (), flags)Why do you use the extended syntax? I tried to use
Box::newand it compiled.It works when compiling the kernel but fails when compiling for userspace with regular (
no_global_oom_handlingdisabled)allocwhen runningmake rusttest. In the latter case, it chooses the inherent version ofBox::newwhich is infallible and doesn't take flags so it fails to compile.Quoted 7 lines
Using the extended syntax allows it always pick the right version, regardless of how
allocis compiled.There are 5 places in existing code that required this change and this is limited to the kernel crate (e.g., drivers, samples and documentation examples can continue to use
Box::new). So we thought it was ok until Miguel figures out what we want to do with tests.Thanks for the explanation, again it would be nice to be able to just write
Box::new.--
Cheers,
Benno
Re: [PATCH 06/10] rust: alloc: introduce the `BoxExt` trait
In reply to Benno LossinQuoted 166 lines · Benno Lossin
Quoted 23 lines · Wedson Almeida Filho
Quoted 22 lines · Benno Lossin
Quoted 20 lines · Wedson Almeida Filho
From: Wedson Almeida Filho <[email protected]>
Make fallible versions of
newandnew_uninitmethods available inBoxeven though it doesn't implement them because we buildallocwith theno_global_oom_handlingconfig.They also have an extra
flagsparameter that allows callers to pass flags to the allocator.Signed-off-by Wedson Almeida Filho <[email protected]>--- rust/kernel/alloc.rs | 1 + rust/kernel/alloc/allocator.rs | 6 +++- rust/kernel/alloc/boxext.rs | 61 ++++++++++++++++++++++++++++++++++ rust/kernel/init.rs | 13 ++++---- rust/kernel/prelude.rs | 2 +- rust/kernel/sync/arc.rs | 3 +- 6 files changed, 77 insertions(+), 9 deletions(-)create mode 100644
rust/kernel/alloc/boxext.rsQuoted 10 lines
Quoted 10 lines
rust/kernel/alloc.rs line 9
Quoted 10 lines
rust/kernel/alloc.rs +1
-0@@ -5,6 +5,7 @@#[cfg(not(test))] #[cfg(not(testlib))] mod allocator; +pub mod boxext;pub mod vecext;One thing I forgot to say: I think these modules should be named
box_extandvec_ext. It fits better with the usual style.Quoted 57 lines
Quoted 48 lines
Quoted 48 lines
rust/kernel/alloc.rs +0
-0/// Flags to be used when allocating memory. rust/kernel/alloc/allocator.rs +5
-1@@ -15,7 +15,11 @@/// /// - `ptr` can be either null or a pointer which has been allocated by this allocator. /// - `new_layout` must have a non-zero size. -unsafe fn krealloc_aligned (ptr: *mut u8 , new_layout:Layout , flags: bindings::gfp_t ) -> *mut u8 { +pub (crate )unsafe fn krealloc_aligned ( + ptr: *mut u8 , + new_layout:Layout , + flags: bindings::gfp_t , +) -> *mut u8 {// Customized layouts from `Layout::from_size_align()` can have size < align, so pad first. let layout = new_layout.pad_to_align ();rust/kernel/alloc/boxext.rs new file +23
-0@@ -0,0 +1,61 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! Extensions to [`Box`] for fallible allocations. + +use super ::Flags ; +use alloc::boxed::Box ; +use core::alloc::AllocError ; +use core::mem::MaybeUninit ; +use core::result::Result ; + +/// Extensions to [`Box`]. +pub trait BoxExt <T >:Sized { +/// Allocates a new box. +/// +/// The allocation may fail, in which case an error is returned. +fn new (x:T , flags:Flags ) ->Result <Self ,AllocError >; + +/// Allocates a new uninitialised box. +/// +/// The allocation may fail, in which case an error is returned. +fn new_uninit (flags:Flags ) ->Result <Box <MaybeUninit <T >>,AllocError >; +} +Quoted 8 lines
rust/kernel/alloc/boxext.rs line 28
Quoted 5 lines
rust/kernel/alloc/boxext.rs new file +5
-0+impl <T >BoxExt <T >for Box <T > { +#[cfg(any(test, testlib))] +fn new (x:T , _flags:Flags ) ->Result <Self ,AllocError > { +Ok (Box ::new (x)) + }When running under
cfg(test), are we using the normal standard library? Or why is this needed?Quoted 7 lines
Because it uses 34 other crates that rely on
Box::newand friends.I discussed this with Miguel recently and once he's done with the build system changes, he will think about what to do with tests. It may be that we abandon the current method of running standalone tests and run everything in kunit, or perhaps we'll find a way to exclude code that won't run in standalone tests anyway...
Ah I see, I think it would be nice to not need this. Let's see what the new build system can do here.
Quoted 59 lines
Quoted 14 lines
rust/kernel/alloc/boxext.rs line 38
Quoted 11 lines
rust/kernel/alloc/boxext.rs new file +10
-0+ +#[cfg(not(any(test, testlib)))] +fn new (x:T , flags:Flags ) ->Result <Self ,AllocError > { +let ptr =if core::mem::size_of ::<T >() ==0 { + core::ptr::NonNull ::<T >::dangling ().as_ptr () + }else { +let layout = core::alloc::Layout ::new ::<T >(); + +// SAFETY: Memory is being allocated (first arg is null). The only other source of +// safety issues is sleeping on atomic context, which is addressed by klint. The
krealloc_alignedfunction states:Quoted 7 lines
/// # Safety
///
/// -ptrcan be either null or a pointer which has been allocated by this allocator. /// -new_layoutmust have a non-zero size.So it should also mention that you checked for
layout.size() > 0above.Good point. I mentioned this in the VecExt version but not here. I will update this for v2.
Quoted 16 lines
Quoted 16 lines
rust/kernel/alloc/boxext.rs new file +16
-0+let ptr =unsafe { +super ::allocator::krealloc_aligned (core::ptr::null_mut (), layout, flags.0 ) + }; +if ptr.is_null () { +return Err (AllocError ); + } + +let ptr = ptr.cast ::<T >(); + +// SAFETY: We just allocated the memory above, it is valid for write. +unsafe { ptr.write (x) }; + ptr + }; + +// SAFETY: For non-zero-sized types, we allocate above using the global allocator. For +// zero-sized types, we use `NonNull::dangling`. Quoted 8 lines
rust/kernel/alloc/boxext.rs line 59
Quoted 5 lines
rust/kernel/alloc/boxext.rs new file +5
-0+Ok (unsafe {Box ::from_raw (ptr) }) + } + +fn new_uninit (flags:Flags ) ->Result <Box <MaybeUninit <T >>,AllocError > { + <Box <_ >as BoxExt <_ >>::new (MaybeUninit ::<T >::uninit (), flags)Why do you use the extended syntax? I tried to use
Box::newand it compiled.It works when compiling the kernel but fails when compiling for userspace with regular (
no_global_oom_handlingdisabled)allocwhen runningmake rusttest. In the latter case, it chooses the inherent version ofBox::newwhich is infallible and doesn't take flags so it fails to compile.Using the extended syntax allows it always pick the right version, regardless of how
allocis compiled.
Quoted 7 lines
Quoted 4 lines
There are 5 places in existing code that required this change and this is limited to the kernel crate (e.g., drivers, samples and documentation examples can continue to use
Box::new). So we thought it was ok until Miguel figures out what we want to do with tests.Thanks for the explanation, again it would be nice to be able to just write
Box::new.
Indeed, that's what I had originally but we don't want to break rusttest.
PATCH 7/10 rust: alloc: update `VecExt` to take allocation flags
In reply to Wedson Almeida FilhoFrom: Wedson Almeida Filho <[email protected]>
We also rename the methods by removing the try_ prefix since the names are available due to our usage of the no_global_oom_handling config when building the alloc crate.
--- rust/kernel/alloc/vecext.rs | 106 ++++++++++++++++++++++++++++------- rust/kernel/lib.rs | 1 - rust/kernel/str.rs | 6 +- rust/kernel/types.rs | 4 +- samples/rust/rust_minimal.rs | 6 +- 5 files changed, 95 insertions(+), 28 deletions(-)
rust/kernel/alloc/vecext.rs +87 -19
@@ -2,51 +2,119 @@//! Extensions to [`Vec`] for fallible allocations. -use alloc::{collections::TryReserveError , vec::Vec }; -use core::result::Result ; +use super ::Flags ; +use alloc::{alloc::AllocError , vec::Vec }; +use core::{mem::ManuallyDrop , result::Result };/// Extensions to [`Vec`]. pub trait VecExt <T >:Sized {/// Creates a new [`Vec`] instance with at least the given capacity. -fn try_with_capacity (capacity:usize ) ->Result <Self ,TryReserveError >; +fn with_capacity (capacity:usize , flags:Flags ) ->Result <Self ,AllocError >;/// Appends an element to the back of the [`Vec`] instance. -fn try_push (&mut self, v:T ) ->Result <(),TryReserveError >; +fn push (&mut self, v:T , flags:Flags ) ->Result <(),AllocError >;/// Pushes clones of the elements of slice into the [`Vec`] instance. -fn try_extend_from_slice (&mut self, other: &[T ]) ->Result <(),TryReserveError > +fn extend_from_slice (&mut self, other: &[T ], flags:Flags ) ->Result <(),AllocError >where T :Clone ; + +/// Ensures that the capacity exceeds the length by at least `additional` elements. +fn reserve (&mut self, additional:usize , flags:Flags ) ->Result <(),AllocError >; }impl <T >VecExt <T >for Vec <T > { -fn try_with_capacity (capacity:usize ) ->Result <Self ,TryReserveError > { +fn with_capacity (capacity:usize , flags:Flags ) ->Result <Self ,AllocError > {let mut v =Vec ::new (); - v.try_reserve (capacity)?; + <Self as VecExt <_ >>::reserve (&mut v, capacity, flags)?;Ok (v) } -fn try_push (&mut self, v:T ) ->Result <(),TryReserveError > { -if let Err (retry) = self.push_within_capacity (v) { - self.try_reserve (1 )?; -let _ = self.push_within_capacity (retry); - } +fn push (&mut self, v:T , flags:Flags ) ->Result <(),AllocError > { + <Self as VecExt <_ >>::reserve (self,1 , flags)?; +let (ptr, len, cap) =destructure (self); +// SAFETY: ptr is valid for `cap` elements. And `cap` is greater (by at least 1) than +// `len` because of the call to `reserve` above. So the pointer after offsetting by `len` +// elements is valid for write. +unsafe { ptr.wrapping_add (len).write (v) }; + +// SAFETY: The only difference from the values returned by `destructure` is that `length` +// is incremented by 1, which is fine because we have just initialised the element at +// offset `length`. +unsafe {rebuild (self, ptr, len +1 , cap) };Ok (()) } -fn try_extend_from_slice (&mut self, other: &[T ]) ->Result <(),TryReserveError > +fn extend_from_slice (&mut self, other: &[T ], flags:Flags ) ->Result <(),AllocError >where T :Clone , { -let extra_cap = self.capacity () - self.len (); -if extra_cap >0 { - self.try_reserve (extra_cap)?; - } - + <Self as VecExt <_ >>::reserve (self, other.len (), flags)?;for itemin other { - self.try_push (item.clone ())?; + <Self as VecExt <_ >>::push (self, item.clone (), flags)?; } +Ok (()) + } +#[cfg(any(test, testlib))] +fn reserve (&mut self, additional:usize , _flags:Flags ) ->Result <(),AllocError > { +Vec ::reserve (self, additional);Ok (()) } + +#[cfg(not(any(test, testlib)))] +fn reserve (&mut self, additional:usize , flags:Flags ) ->Result <(),AllocError > { +let len = self.len (); +let cap = self.capacity (); + +if cap - len >= additional { +return Ok (()); + } + +if core::mem::size_of ::<T >() ==0 { +// The capacity is already `usize::MAX` for SZTs, we can't go higher. +return Err (AllocError ); + } + +// We know cap is <= `isize::MAX` because `Layout::array` fails if the resulting byte size +// is greater than `isize::MAX`. So the multiplication by two won't overflow. +let new_cap = core::cmp::max (cap *2 , len.checked_add (additional).ok_or (AllocError )?); +let layout = core::alloc::Layout ::array ::<T >(new_cap).map_err (|_|AllocError )?; + +let (ptr, len, cap) =destructure (self); + +// SAFETY: `ptr` is valid because it's either NULL or comes from a previous call to +// `krealloc_aligned`. We also verified that the type is not a ZST. +let new_ptr =unsafe {super ::allocator::krealloc_aligned (ptr.cast (), layout, flags.0 ) }; +if new_ptr.is_null () { +// SAFETY: We are just rebuilding the existing `Vec` with no changes. +unsafe {rebuild (self, ptr, len, cap) }; +Err (AllocError ) + }else { +// SAFETY: `ptr` has been reallocated with the layout for `new_cap` elements. New cap +// is greater than `cap`, so it continues to be >= `len`. +unsafe {rebuild (self, new_ptr.cast ::<T >(), len, new_cap) }; +Ok (()) + } + } +} + +fn destructure <T >(v: &mut Vec <T >) -> (*mut T ,usize ,usize ) { +let mut tmp =Vec ::new (); + core::mem::swap (&mut tmp, v); +let mut tmp =ManuallyDrop ::new (tmp); +let len = tmp.len (); +let cap = tmp.capacity (); + (tmp.as_mut_ptr (), len, cap) +} + +/// Rebuilds a `Vec` from a pointer, length, and capacity. +/// +/// # Safety +/// +/// The same as [`Vec::from_raw_parts`]. +unsafe fn rebuild <T >(v: &mut Vec <T >, ptr: *mut T , len:usize , cap:usize ) { +// SAFETY: The safety requirements from this function satisfy those of `from_raw_parts`. +let mut tmp =unsafe {Vec ::from_raw_parts (ptr, len, cap) }; + core::mem::swap (&mut tmp, v); }
rust/kernel/lib.rs +0 -1
@@ -19,7 +19,6 @@#![feature(offset_of)] #![feature(receiver_trait)] #![feature(unsize)] -#![feature(vec_push_within_capacity)] // Ensure conditional compilation based on the kernel configuration works; // otherwise we may silently break things like initcall handling.
rust/kernel/str.rs +3 -3
@@ -2,7 +2,7 @@//! String representations. -use crate ::alloc::vecext::VecExt ; +use crate ::alloc::{flags::*, vecext::VecExt };use alloc::alloc::AllocError ;use alloc::vec::Vec ;use core::fmt::{self,Write };
@@ -730,7 +730,7 @@ pub fn try_from_fmt(args: fmt::Arguments<'_>) -> Result<Self, Error> {let size = f.bytes_written ();// Allocate a vector with the required number of bytes, and write to it. -let mut buf =Vec ::try_with_capacity (size)?; +let mut buf = <Vec <_ >as VecExt <_ >>::with_capacity (size,GFP_KERNEL )?;// SAFETY: The buffer stored in `buf` is at least of size `size` and is valid for writes. let mut f =unsafe {Formatter ::from_buffer (buf.as_mut_ptr (), size) }; f.write_fmt (args)?;
@@ -771,7 +771,7 @@ impl<'a> TryFrom<&'a CStr> for CString {fn try_from (cstr: &'aCStr ) ->Result <CString ,AllocError > {let mut buf =Vec ::new (); - buf.try_extend_from_slice (cstr.as_bytes_with_nul ()) + <Vec <_ >as VecExt <_ >>::extend_from_slice (&mut buf, cstr.as_bytes_with_nul (),GFP_KERNEL ) .map_err (|_|AllocError )?;// INVARIANT: The `CStr` and `CString` types have the same invariants for
rust/kernel/types.rs +2 -2
@@ -157,11 +157,11 @@ unsafe fn from_foreign(_: *const core::ffi::c_void) -> Self {}/// let mut vec = /// ScopeGuard::new_with_data(Vec::new(), |v| pr_info!("vec had {} elements\n", v.len())); /// -/// vec.try_push(10u8)?; +/// vec.push(10u8, GFP_KERNEL)?; /// if arg { /// return Ok(()); /// } -/// vec.try_push(20u8)?; +/// vec.push(20u8, GFP_KERNEL)?; /// Ok(()) /// } ///
samples/rust/rust_minimal.rs +3 -3
@@ -22,9 +22,9 @@ fn init(_module: &'static ThisModule) -> Result<Self> {pr_info! ("Am I built-in? {}\n" , !cfg! (MODULE ));let mut numbers =Vec ::new (); - numbers.try_push (72 )?; - numbers.try_push (108 )?; - numbers.try_push (200 )?; + numbers.push (72 ,GFP_KERNEL )?; + numbers.push (108 ,GFP_KERNEL )?; + numbers.push (200 ,GFP_KERNEL )?;Ok (RustMinimal { numbers }) }
Signature · 1 line
2.34.1
Re: [PATCH 07/10] rust: alloc: update `VecExt` to take allocation flags
On Mon, Mar 25, 2024 at 04:54:15PM -0300, Wedson Almeida Filho wrote:
[...]
rust/kernel/alloc/vecext.rs line 44
Quoted 12 lines
rust/kernel/alloc/vecext.rs +12
-0+fn push (&mut self, v:T , flags:Flags ) ->Result <(),AllocError > { + <Self as VecExt <_ >>::reserve (self,1 , flags)?; +let (ptr, len, cap) =destructure (self); +// SAFETY: ptr is valid for `cap` elements. And `cap` is greater (by at least 1) than +// `len` because of the call to `reserve` above. So the pointer after offsetting by `len` +// elements is valid for write. +unsafe { ptr.wrapping_add (len).write (v) }; + +// SAFETY: The only difference from the values returned by `destructure` is that `length` +// is incremented by 1, which is fine because we have just initialised the element at +// offset `length`. +unsafe {rebuild (self, ptr, len +1 , cap) };
probably use spare_capacity_mut() here to avoid destructure and rebuild?
https://doc.rust-lang.org/std/vec/struct.Vec.html#method.spare_capacity_mut // .. after reserve succeed. // there must be room for adding one more. self.spare_capacity_mut()[0].write(v); // or unsafe { self.spare_capacity_mut().as_mut_ptr().cast().write(v); } unsafe { self.set_len(self.len() + 1); }
Thoughts?
Regards,
Boqun
Quoted 3 lines
Ok(())
}[...]
Re: [PATCH 07/10] rust: alloc: update `VecExt` to take allocation flags
Quoted 16 lines · Boqun Feng
On Mon, Mar 25, 2024 at 04:54:15PM -0300, Wedson Almeida Filho wrote:
[...]rust/kernel/alloc/vecext.rs line 44
Quoted 12 lines
rust/kernel/alloc/vecext.rs +12
-0+fn push (&mut self, v:T , flags:Flags ) ->Result <(),AllocError > { + <Self as VecExt <_ >>::reserve (self,1 , flags)?; +let (ptr, len, cap) =destructure (self); +// SAFETY: ptr is valid for `cap` elements. And `cap` is greater (by at least 1) than +// `len` because of the call to `reserve` above. So the pointer after offsetting by `len` +// elements is valid for write. +unsafe { ptr.wrapping_add (len).write (v) }; + +// SAFETY: The only difference from the values returned by `destructure` is that `length` +// is incremented by 1, which is fine because we have just initialised the element at +// offset `length`. +unsafe {rebuild (self, ptr, len +1 , cap) };
Quoted 2 lines
probably use
spare_capacity_mut()here to avoiddestructureandrebuild?
Ah, yes, this sounds like a better approach. I will use this in v2.
Quoted 21 lines
https://doc.rust-lang.org/std/vec/struct.Vec.html#method.spare_capacity_mut // .. after reserve succeed. // there must be room for adding one more. self.spare_capacity_mut()[0].write(v); // or unsafe { self.spare_capacity_mut().as_mut_ptr().cast().write(v); } unsafe { self.set_len(self.len() + 1); }Thoughts?
Regards,
BoqunQuoted 3 lines
Ok(()) }[...]
Benno Lossin
Reviewed-byRe: [PATCH 07/10] rust: alloc: update `VecExt` to take allocation flags
Quoted 6 lines · Wedson Almeida Filho
From: Wedson Almeida Filho <[email protected]>
We also rename the methods by removing the
try_prefix since the names are available due to our usage of theno_global_oom_handlingconfig when building thealloccrate.
Quoted 8 lines
Signed-off-by Wedson Almeida Filho <[email protected]>--- rust/kernel/alloc/vecext.rs | 106 ++++++++++++++++++++++++++++------- rust/kernel/lib.rs | 1 - rust/kernel/str.rs | 6 +- rust/kernel/types.rs | 4 +- samples/rust/rust_minimal.rs | 6 +- 5 files changed, 95 insertions(+), 28 deletions(-)
With Boqun's change:
One thing that we might consider in the future would be to create our own Extend trait to allow extending a Vec with any iterator.
Signature · 2 lines
Cheers, Benno
PATCH 8/10 rust: sync: update `Arc` and `UniqueArc` to take allocation flags
In reply to Wedson Almeida FilhoFrom: Wedson Almeida Filho <[email protected]>
We also remove the try_ prefix to align with how Box and Vec are providing methods now.
init is temporarily updated with uses of GFP_KERNEL. These will be updated in a subsequent patch to take flags as well.
--- rust/kernel/init.rs | 4 ++-- rust/kernel/sync/arc.rs | 28 ++++++++++++++-------------- samples/rust/rust_print.rs | 4 ++-- 3 files changed, 18 insertions(+), 18 deletions(-)
rust/kernel/init.rs +2 -2
@@ -1189,7 +1189,7 @@ fn try_pin_init<E>(init: impl PinInit<T, E>) -> Result<Pin<Self>, E> where E: From<AllocError>, { -let mut this =UniqueArc ::try_new_uninit ()?; +let mut this =UniqueArc ::new_uninit (GFP_KERNEL )?;let slot = this.as_mut_ptr ();// SAFETY: When init errors/panics, slot will get deallocated but not dropped, // slot is valid and will not be moved, because we pin it later.
@@ -1203,7 +1203,7 @@ fn try_init<E>(init: impl Init<T, E>) -> Result<Self, E> where E: From<AllocError>, { -let mut this =UniqueArc ::try_new_uninit ()?; +let mut this =UniqueArc ::new_uninit (GFP_KERNEL )?;let slot = this.as_mut_ptr ();// SAFETY: When init errors/panics, slot will get deallocated but not dropped, // slot is valid.
rust/kernel/sync/arc.rs +14 -14
@@ -16,7 +16,7 @@//! [`Arc`]: https://doc.rust-lang.org/std/sync/struct.Arc.html use crate ::{ - alloc::{boxext::BoxExt , flags::*}, + alloc::{boxext::BoxExt ,Flags }, bindings, error::{self,Error }, init::{self,InPlaceInit ,Init ,PinInit },
@@ -58,7 +58,7 @@/// } /// /// // Create a refcounted instance of `Example`. -/// let obj = Arc::try_new(Example { a: 10, b: 20 })?; +/// let obj = Arc::new(Example { a: 10, b: 20 }, GFP_KERNEL)?; /// /// // Get a new pointer to `obj` and increment the refcount. /// let cloned = obj.clone();
@@ -97,7 +97,7 @@/// } /// } /// -/// let obj = Arc::try_new(Example { a: 10, b: 20 })?; +/// let obj = Arc::new(Example { a: 10, b: 20 }, GFP_KERNEL)?; /// obj.use_reference(); /// obj.take_over(); /// # Ok::<(), Error>(())
@@ -120,7 +120,7 @@/// impl MyTrait for Example {} /// /// // `obj` has type `Arc<Example>`. -/// let obj: Arc<Example> = Arc::try_new(Example)?; +/// let obj: Arc<Example> = Arc::new(Example, GFP_KERNEL)?; /// /// // `coerced` has type `Arc<dyn MyTrait>`. /// let coerced: Arc<dyn MyTrait> = obj;
@@ -163,7 +163,7 @@ unsafe impl<T: ?Sized + Sync + Send> Sync for Arc<T> {}impl <T >Arc <T > {/// Constructs a new reference counted instance of `T`. -pub fn try_new (contents:T ) ->Result <Self ,AllocError > { +pub fn new (contents:T , flags:Flags ) ->Result <Self ,AllocError > {// INVARIANT: The refcount is initialised to a non-zero value. let value = ArcInner {// SAFETY: There are no safety requirements for this FFI call.
@@ -171,7 +171,7 @@ pub fn try_new(contents: T) -> Result<Self, AllocError> { data: contents, }; -let inner = <Box <_ >as BoxExt <_ >>::new (value,GFP_KERNEL )?; +let inner = <Box <_ >as BoxExt <_ >>::new (value, flags)?;// SAFETY: We just created `inner` with a reference count of 1, which is owned by the new // `Arc` object.
@@ -388,7 +388,7 @@ fn from(item: Pin<UniqueArc<T>>) -> Self {/// e.into() /// } /// -/// let obj = Arc::try_new(Example)?; +/// let obj = Arc::new(Example, GFP_KERNEL)?; /// let cloned = do_something(obj.as_arc_borrow()); /// /// // Assert that both `obj` and `cloned` point to the same underlying object.
@@ -412,7 +412,7 @@ fn from(item: Pin<UniqueArc<T>>) -> Self {/// } /// } /// -/// let obj = Arc::try_new(Example { a: 10, b: 20 })?; +/// let obj = Arc::new(Example { a: 10, b: 20 }, GFP_KERNEL)?; /// obj.as_arc_borrow().use_reference(); /// # Ok::<(), Error>(()) /// ```
@@ -500,7 +500,7 @@ fn deref(&self) -> &Self::Target {/// } /// /// fn test() -> Result<Arc<Example>> { -/// let mut x = UniqueArc::try_new(Example { a: 10, b: 20 })?; +/// let mut x = UniqueArc::new(Example { a: 10, b: 20 }, GFP_KERNEL)?; /// x.a += 1; /// x.b += 1; /// Ok(x.into())
@@ -523,7 +523,7 @@ fn deref(&self) -> &Self::Target {/// } /// /// fn test() -> Result<Arc<Example>> { -/// let x = UniqueArc::try_new_uninit()?; +/// let x = UniqueArc::new_uninit(GFP_KERNEL)?; /// Ok(x.write(Example { a: 10, b: 20 }).into()) /// } ///
@@ -543,7 +543,7 @@ fn deref(&self) -> &Self::Target {/// } /// /// fn test() -> Result<Arc<Example>> { -/// let mut pinned = Pin::from(UniqueArc::try_new(Example { a: 10, b: 20 })?); +/// let mut pinned = Pin::from(UniqueArc::new(Example { a: 10, b: 20 }, GFP_KERNEL)?); /// // We can modify `pinned` because it is `Unpin`. /// pinned.as_mut().a += 1; /// Ok(pinned.into())
@@ -557,15 +557,15 @@ pub struct UniqueArc<T: ?Sized> { impl<T> UniqueArc<T> { /// Tries to allocate a new [`UniqueArc`] instance. - pub fn try_new(value: T) -> Result<Self, AllocError> { + pub fn new(value: T, flags: Flags) -> Result<Self, AllocError> { Ok(Self { // INVARIANT: The newly-created object has a refcount of 1. - inner: Arc::try_new(value)?, + inner: Arc::new(value, flags)?, }) } /// Tries to allocate a new [`UniqueArc`] instance whose contents are not initialised yet. - pub fn try_new_uninit() -> Result<UniqueArc<MaybeUninit<T>>, AllocError> { + pub fn new_uninit(_flags: Flags) -> Result<UniqueArc<MaybeUninit<T>>, AllocError> { // INVARIANT: The refcount is initialised to a non-zero value. let inner = Box::try_init::<AllocError>(try_init!(ArcInner { // SAFETY: There are no safety requirements for this FFI call.
samples/rust/rust_print.rs +2 -2
@@ -18,8 +18,8 @@fn arc_print () ->Result {use kernel::sync::*; -let a =Arc ::try_new (1 )?; -let b =UniqueArc ::try_new ("hello, world" )?; +let a =Arc ::new (1 ,GFP_KERNEL )?; +let b =UniqueArc ::new ("hello, world" ,GFP_KERNEL )?;// Prints the value of data in `a`. pr_info! ("{}" , a);
Signature · 1 line
2.34.1
Re: [PATCH 08/10] rust: sync: update `Arc` and `UniqueArc` to take allocation flags
Quoted 8 lines · Wedson Almeida Filho
From: Wedson Almeida Filho <[email protected]>
We also remove the
try_prefix to align with howBoxandVecare providing methods now.
initis temporarily updated with uses ofGFP_KERNEL. These will be updated in a subsequent patch to take flags as well.
Quoted 6 lines
Signed-off-by Wedson Almeida Filho <[email protected]>--- rust/kernel/init.rs | 4 ++-- rust/kernel/sync/arc.rs | 28 ++++++++++++++-------------- samples/rust/rust_print.rs | 4 ++-- 3 files changed, 18 insertions(+), 18 deletions(-)
Signature · 2 lines
Cheers, Benno
PATCH 9/10 rust: init: update `init` module to take allocation flags
In reply to Wedson Almeida FilhoFrom: Wedson Almeida Filho <[email protected]>
This is the last component in the conversion for allocators to take allocation flags as parameters.
--- rust/kernel/init.rs | 49 ++++++++++++++++--------------- rust/kernel/sync/arc.rs | 17 ++++++----- rust/kernel/sync/condvar.rs | 2 +- rust/kernel/sync/lock/mutex.rs | 2 +- rust/kernel/sync/lock/spinlock.rs | 2 +- rust/kernel/workqueue.rs | 13 +++++--- 6 files changed, 47 insertions(+), 38 deletions(-)
rust/kernel/init.rs +25 -24
@@ -68,7 +68,7 @@//! # a <- new_mutex!(42, "Foo::a"), //! # b: 24, //! # }); -//! let foo: Result<Pin<Box<Foo>>> = Box::pin_init(foo); +//! let foo: Result<Pin<Box<Foo>>> = Box::pin_init(foo, GFP_KERNEL); //! ``` //! //! For more information see the [`pin_init!`] macro.
@@ -80,7 +80,8 @@//! //! ```rust //! # use kernel::sync::{new_mutex, Arc, Mutex}; -//! let mtx: Result<Arc<Mutex<usize>>> = Arc::pin_init(new_mutex!(42, "example::mtx")); +//! let mtx: Result<Arc<Mutex<usize>>> = +//! Arc::pin_init(new_mutex!(42, "example::mtx"), GFP_KERNEL); //! ``` //! //! To declare an init macro/function you just return an [`impl PinInit<T, E>`]:
@@ -99,7 +100,7 @@//! fn new() -> impl PinInit<Self, Error> { //! try_pin_init!(Self { //! status <- new_mutex!(0, "DriverData::status"), -//! buffer: Box::init(kernel::init::zeroed())?, +//! buffer: Box::init(kernel::init::zeroed(), GFP_KERNEL)?, //! }) //! } //! }
@@ -210,7 +211,7 @@//! [`pin_init!`]: crate::pin_init! use crate ::{ - alloc::{boxext::BoxExt , flags::*}, + alloc::{boxext::BoxExt ,Flags }, error::{self,Error }, sync::UniqueArc , types::{Opaque ,ScopeGuard },
@@ -391,7 +392,7 @@ macro_rules! stack_try_pin_init {/// }, /// }); /// # initializer } -/// # Box::pin_init(demo()).unwrap(); +/// # Box::pin_init(demo(), GFP_KERNEL).unwrap(); /// ``` /// /// Arbitrary Rust expressions can be used to set the value of a variable.
@@ -461,7 +462,7 @@ macro_rules! stack_try_pin_init {/// # }) /// # } /// # } -/// let foo = Box::pin_init(Foo::new()); +/// let foo = Box::pin_init(Foo::new(), GFP_KERNEL); /// ``` /// /// They can also easily embed it into their own `struct`s:
@@ -601,7 +602,7 @@ macro_rules! pin_init {/// impl BigBuf { /// fn new() -> impl PinInit<Self, Error> { /// try_pin_init!(Self { -/// big: Box::init(init::zeroed())?, +/// big: Box::init(init::zeroed(), GFP_KERNEL)?, /// small: [0; 1024 * 1024], /// ptr: core::ptr::null_mut(), /// }? Error)
@@ -702,7 +703,7 @@ macro_rules! init {/// impl BigBuf { /// fn new() -> impl Init<Self, Error> { /// try_init!(Self { -/// big: Box::init(zeroed())?, +/// big: Box::init(zeroed(), GFP_KERNEL)?, /// small: [0; 1024 * 1024], /// }? Error) /// }
@@ -1014,7 +1015,7 @@ pub fn uninit<T, E>() -> impl Init<MaybeUninit<T>, E> {/// /// ```rust /// use kernel::{error::Error, init::init_array_from_fn}; -/// let array: Box<[usize; 1_000]> = Box::init::<Error>(init_array_from_fn(|i| i)).unwrap(); +/// let array: Box<[usize; 1_000]> = Box::init::<Error>(init_array_from_fn(|i| i), GFP_KERNEL).unwrap(); /// assert_eq!(array.len(), 1_000); /// ``` pub fn init_array_from_fn<I, const N: usize, T, E>(
@@ -1058,7 +1059,7 @@ pub fn init_array_from_fn<I, const N: usize, T, E>(/// ```rust /// use kernel::{sync::{Arc, Mutex}, init::pin_init_array_from_fn, new_mutex}; /// let array: Arc<[Mutex<usize>; 1_000]> = -/// Arc::pin_init(pin_init_array_from_fn(|i| new_mutex!(i))).unwrap(); +/// Arc::pin_init(pin_init_array_from_fn(|i| new_mutex!(i)), GFP_KERNEL).unwrap(); /// assert_eq!(array.len(), 1_000); /// ``` pub fn pin_init_array_from_fn<I, const N: usize, T, E>(
@@ -1116,7 +1117,7 @@ pub trait InPlaceInit<T>: Sized {/// type. /// /// If `T: !Unpin` it will not be able to move afterwards. -fn try_pin_init <E >(init:impl PinInit <T ,E >) ->Result <Pin <Self >,E > +fn try_pin_init <E >(init:impl PinInit <T ,E >, flags:Flags ) ->Result <Pin <Self >,E >where E :From <AllocError >;
@@ -1124,7 +1125,7 @@ fn try_pin_init<E>(init: impl PinInit<T, E>) -> Result<Pin<Self>, E>/// type. /// /// If `T: !Unpin` it will not be able to move afterwards. -fn pin_init <E >(init:impl PinInit <T ,E >) -> error::Result <Pin <Self >> +fn pin_init <E >(init:impl PinInit <T ,E >, flags:Flags ) -> error::Result <Pin <Self >>where Error :From <E >, {
@@ -1132,16 +1133,16 @@ fn pin_init<E>(init: impl PinInit<T, E>) -> error::Result<Pin<Self>>let init =unsafe {pin_init_from_closure (|slot| init.__pinned_init (slot).map_err (|e|Error ::from (e))) }; -Self ::try_pin_init (init) +Self ::try_pin_init (init, flags) }/// Use the given initializer to in-place initialize a `T`. -fn try_init <E >(init:impl Init <T ,E >) ->Result <Self ,E > +fn try_init <E >(init:impl Init <T ,E >, flags:Flags ) ->Result <Self ,E >where E :From <AllocError >;/// Use the given initializer to in-place initialize a `T`. -fn init <E >(init:impl Init <T ,E >) -> error::Result <Self > +fn init <E >(init:impl Init <T ,E >, flags:Flags ) -> error::Result <Self >where Error :From <E >, {
@@ -1149,17 +1150,17 @@ fn init<E>(init: impl Init<T, E>) -> error::Result<Self>let init =unsafe {init_from_closure (|slot| init.__pinned_init (slot).map_err (|e|Error ::from (e))) }; -Self ::try_init (init) +Self ::try_init (init, flags) } }impl <T >InPlaceInit <T >for Box <T > {#[inline] -fn try_pin_init <E >(init:impl PinInit <T ,E >) ->Result <Pin <Self >,E > +fn try_pin_init <E >(init:impl PinInit <T ,E >, flags:Flags ) ->Result <Pin <Self >,E >where E :From <AllocError >, { -let mut this = <Box <_ >as BoxExt <_ >>::new_uninit (GFP_KERNEL )?; +let mut this = <Box <_ >as BoxExt <_ >>::new_uninit (flags)?;let slot = this.as_mut_ptr ();// SAFETY: When init errors/panics, slot will get deallocated but not dropped, // slot is valid and will not be moved, because we pin it later.
@@ -1169,11 +1170,11 @@ fn try_pin_init<E>(init: impl PinInit<T, E>) -> Result<Pin<Self>, E> }#[inline] -fn try_init <E >(init:impl Init <T ,E >) ->Result <Self ,E > +fn try_init <E >(init:impl Init <T ,E >, flags:Flags ) ->Result <Self ,E >where E :From <AllocError >, { -let mut this = <Box <_ >as BoxExt <_ >>::new_uninit (GFP_KERNEL )?; +let mut this = <Box <_ >as BoxExt <_ >>::new_uninit (flags)?;let slot = this.as_mut_ptr ();// SAFETY: When init errors/panics, slot will get deallocated but not dropped, // slot is valid.
@@ -1185,11 +1186,11 @@ fn try_init<E>(init: impl Init<T, E>) -> Result<Self, E>impl <T >InPlaceInit <T >for UniqueArc <T > {#[inline] -fn try_pin_init <E >(init:impl PinInit <T ,E >) ->Result <Pin <Self >,E > +fn try_pin_init <E >(init:impl PinInit <T ,E >, flags:Flags ) ->Result <Pin <Self >,E >where E :From <AllocError >, { -let mut this =UniqueArc ::new_uninit (GFP_KERNEL )?; +let mut this =UniqueArc ::new_uninit (flags)?;let slot = this.as_mut_ptr ();// SAFETY: When init errors/panics, slot will get deallocated but not dropped, // slot is valid and will not be moved, because we pin it later.
@@ -1199,11 +1200,11 @@ fn try_pin_init<E>(init: impl PinInit<T, E>) -> Result<Pin<Self>, E> }#[inline] -fn try_init <E >(init:impl Init <T ,E >) ->Result <Self ,E > +fn try_init <E >(init:impl Init <T ,E >, flags:Flags ) ->Result <Self ,E >where E :From <AllocError >, { -let mut this =UniqueArc ::new_uninit (GFP_KERNEL )?; +let mut this =UniqueArc ::new_uninit (flags)?;let slot = this.as_mut_ptr ();// SAFETY: When init errors/panics, slot will get deallocated but not dropped, // slot is valid.
rust/kernel/sync/arc.rs +10 -7
@@ -182,22 +182,22 @@ pub fn new(contents: T, flags: Flags) -> Result<Self, AllocError> {/// /// If `T: !Unpin` it will not be able to move afterwards. #[inline] -pub fn pin_init <E >(init:impl PinInit <T ,E >) -> error::Result <Self > +pub fn pin_init <E >(init:impl PinInit <T ,E >, flags:Flags ) -> error::Result <Self >where Error :From <E >, { -UniqueArc ::pin_init (init).map (|u| u.into ()) +UniqueArc ::pin_init (init, flags).map (|u| u.into ()) }/// Use the given initializer to in-place initialize a `T`. /// /// This is equivalent to [`Arc<T>::pin_init`], since an [`Arc`] is always pinned. #[inline] -pub fn init <E >(init:impl Init <T ,E >) -> error::Result <Self > +pub fn init <E >(init:impl Init <T ,E >, flags:Flags ) -> error::Result <Self >where Error :From <E >, { -UniqueArc ::init (init).map (|u| u.into ()) +UniqueArc ::init (init, flags).map (|u| u.into ()) } }
@@ -565,13 +565,16 @@ pub fn new(value: T, flags: Flags) -> Result<Self, AllocError> { }/// Tries to allocate a new [`UniqueArc`] instance whose contents are not initialised yet. -pub fn new_uninit (_flags:Flags ) ->Result <UniqueArc <MaybeUninit <T >>,AllocError > { +pub fn new_uninit (flags:Flags ) ->Result <UniqueArc <MaybeUninit <T >>,AllocError > {// INVARIANT: The refcount is initialised to a non-zero value. -let inner =Box ::try_init ::<AllocError >(try_init! (ArcInner { +let inner =Box ::try_init ::<AllocError >( +try_init! (ArcInner {// SAFETY: There are no safety requirements for this FFI call. refcount:Opaque ::new (unsafe { bindings::REFCOUNT_INIT (1 ) }), data <- init::uninit ::<T ,AllocError >(), - }?AllocError ))?; + }?AllocError ), + flags, + )?;Ok (UniqueArc {// INVARIANT: The newly-created object has a refcount of 1. // SAFETY: The pointer from the `Box` is valid.
rust/kernel/sync/condvar.rs +1 -1
@@ -75,7 +75,7 @@ macro_rules! new_condvar {/// Box::pin_init(pin_init!(Example { /// value <- new_mutex!(0), /// value_changed <- new_condvar!(), -/// })) +/// }), GFP_KERNEL) /// } /// ``` ///
rust/kernel/sync/lock/mutex.rs +1 -1
@@ -60,7 +60,7 @@ macro_rules! new_mutex {/// } /// /// // Allocate a boxed `Example`. -/// let e = Box::pin_init(Example::new())?; +/// let e = Box::pin_init(Example::new(), GFP_KERNEL)?; /// assert_eq!(e.c, 10); /// assert_eq!(e.d.lock().a, 20); /// assert_eq!(e.d.lock().b, 30);
rust/kernel/sync/lock/spinlock.rs +1 -1
@@ -58,7 +58,7 @@ macro_rules! new_spinlock {/// } /// /// // Allocate a boxed `Example`. -/// let e = Box::pin_init(Example::new())?; +/// let e = Box::pin_init(Example::new(), GFP_KERNEL)?; /// assert_eq!(e.c, 10); /// assert_eq!(e.d.lock().a, 20); /// assert_eq!(e.d.lock().b, 30);
rust/kernel/workqueue.rs +9 -4
@@ -53,7 +53,7 @@//! Arc::pin_init(pin_init!(MyStruct { //! value, //! work <- new_work!("MyStruct::work"), -//! })) +//! }), GFP_KERNEL) //! } //! } //!
@@ -101,7 +101,7 @@//! value_2, //! work_1 <- new_work!("MyStruct::work_1"), //! work_2 <- new_work!("MyStruct::work_2"), -//! })) +//! }), GFP_KERNEL) //! } //! } //!
@@ -132,6 +132,7 @@//! //! C header: [`include/linux/workqueue.h`](srctree/include/linux/workqueue.h) +use crate ::alloc::Flags ;use crate ::{bindings, prelude::*, sync::Arc , sync::LockClassKey , types::Opaque };use alloc::alloc::AllocError ;use alloc::boxed::Box ;
@@ -206,13 +207,17 @@ pub fn enqueue<W, const ID: u64>(&self, w: W) -> W::EnqueueOutput/// Tries to spawn the given function or closure as a work item. /// /// This method can fail because it allocates memory to store the work item. -pub fn try_spawn <T : 'static +Send +FnOnce ()>(&self, func:T ) ->Result <(),AllocError > { +pub fn try_spawn <T : 'static +Send +FnOnce ()>( + &self, + flags:Flags , + func:T , + ) ->Result <(),AllocError > {let init =pin_init! (ClosureWork { work <-new_work! ("Queue::try_spawn" ), func:Some (func), }); - self.enqueue (Box ::pin_init (init).map_err (|_|AllocError )?); + self.enqueue (Box ::pin_init (init, flags).map_err (|_|AllocError )?);Ok (()) } }
Signature · 1 line
2.34.1
Re: [PATCH 09/10] rust: init: update `init` module to take allocation flags
Quoted 6 lines · Wedson Almeida Filho
From: Wedson Almeida Filho <[email protected]>
This is the last component in the conversion for allocators to take allocation flags as parameters.
Signed-off-by Wedson Almeida Filho <[email protected]>
Quoted 8 lines
--- rust/kernel/init.rs | 49 ++++++++++++++++--------------- rust/kernel/sync/arc.rs | 17 ++++++----- rust/kernel/sync/condvar.rs | 2 +- rust/kernel/sync/lock/mutex.rs | 2 +- rust/kernel/sync/lock/spinlock.rs | 2 +- rust/kernel/workqueue.rs | 13 +++++--- 6 files changed, 47 insertions(+), 38 deletions(-)
One formatting issue below, with that fixed:
Quoted 11 lines
rust/kernel/sync/arc.rs +1
-1@@ -565,13 +565,16 @@ pub fn new(value: T, flags: Flags) -> Result<Self, AllocError> { }/// Tries to allocate a new [`UniqueArc`] instance whose contents are not initialised yet. -pub fn new_uninit (_flags:Flags ) ->Result <UniqueArc <MaybeUninit <T >>,AllocError > { +pub fn new_uninit (flags:Flags ) ->Result <UniqueArc <MaybeUninit <T >>,AllocError > {// INVARIANT: The refcount is initialised to a non-zero value.
rust/kernel/sync/arc.rs pub fn new(value: T, flags: Flags) -> Result<Self, AllocError> { · line 577
Quoted 10 lines
rust/kernel/sync/arc.rs +5
-2-let inner =Box ::try_init ::<AllocError >(try_init! (ArcInner { +let inner =Box ::try_init ::<AllocError >( +try_init! (ArcInner {// SAFETY: There are no safety requirements for this FFI call. refcount:Opaque ::new (unsafe { bindings::REFCOUNT_INIT (1 ) }), data <- init::uninit ::<T ,AllocError >(), - }?AllocError ))?; + }?AllocError ), + flags, + )?;
The indentation looks wrong, rustfmt sadly cannot handle the pin-init macros. This looks better to me:
let inner = Box::try_init::<AllocError>(
try_init!(ArcInner {
// SAFETY: There are no safety requirements for this FFI call.
refcount: Opaque::new(unsafe { bindings::REFCOUNT_INIT(1) }),
data <- init::uninit::<T, AllocError>(),
}? AllocError),
flags,
)?;Signature · 2 lines
Cheers, Benno
Quoted 3 lines
rust/kernel/sync/arc.rs +0
-0Ok (UniqueArc {// INVARIANT: The newly-created object has a refcount of 1. // SAFETY: The pointer from the `Box` is valid.
Re: [PATCH 09/10] rust: init: update `init` module to take allocation flags
Quoted 46 lines · Benno Lossin
Quoted 6 lines · Wedson Almeida Filho
From: Wedson Almeida Filho <[email protected]>
This is the last component in the conversion for allocators to take allocation flags as parameters.
Signed-off-by Wedson Almeida Filho <[email protected]>Quoted 8 lines
--- rust/kernel/init.rs | 49 ++++++++++++++++--------------- rust/kernel/sync/arc.rs | 17 ++++++----- rust/kernel/sync/condvar.rs | 2 +- rust/kernel/sync/lock/mutex.rs | 2 +- rust/kernel/sync/lock/spinlock.rs | 2 +- rust/kernel/workqueue.rs | 13 +++++--- 6 files changed, 47 insertions(+), 38 deletions(-)One formatting issue below, with that fixed:
Reviewed-by Benno Lossin <[email protected]>Quoted 11 lines
rust/kernel/sync/arc.rs +1
-1@@ -565,13 +565,16 @@ pub fn new(value: T, flags: Flags) -> Result<Self, AllocError> { }/// Tries to allocate a new [`UniqueArc`] instance whose contents are not initialised yet. -pub fn new_uninit (_flags:Flags ) ->Result <UniqueArc <MaybeUninit <T >>,AllocError > { +pub fn new_uninit (flags:Flags ) ->Result <UniqueArc <MaybeUninit <T >>,AllocError > {// INVARIANT: The refcount is initialised to a non-zero value. rust/kernel/sync/arc.rs pub fn new(value: T, flags: Flags) -> Result<Self, AllocError> { · line 577
Quoted 10 lines
rust/kernel/sync/arc.rs +5
-2-let inner =Box ::try_init ::<AllocError >(try_init! (ArcInner { +let inner =Box ::try_init ::<AllocError >( +try_init! (ArcInner {// SAFETY: There are no safety requirements for this FFI call. refcount:Opaque ::new (unsafe { bindings::REFCOUNT_INIT (1 ) }), data <- init::uninit ::<T ,AllocError >(), - }?AllocError ))?; + }?AllocError ), + flags, + )?;The indentation looks wrong, rustfmt sadly cannot handle the pin-init macros. This looks better to me:
Quoted 8 lines
let inner = Box::try_init::<AllocError>(
try_init!(ArcInner {
// SAFETY: There are no safety requirements for this FFI call.
refcount: Opaque::new(unsafe { bindings::REFCOUNT_INIT(1) }),
data <- init::uninit::<T, AllocError>(),
}? AllocError),
flags,
)?;I remember trying to rearrange this and rustfmtcheck not liking it but it seems to be happy with your suggestion.
Applying it in v2.
Quoted 7 lines
--
Cheers,
BennoQuoted 3 lines
rust/kernel/sync/arc.rs +0
-0Ok (UniqueArc {// INVARIANT: The newly-created object has a refcount of 1. // SAFETY: The pointer from the `Box` is valid.
PATCH 10/10 rust: kernel: remove usage of `allocator_api` unstable feature
In reply to Wedson Almeida FilhoFrom: Wedson Almeida Filho <[email protected]>
With the adoption of BoxExt and VecExt, we don't need the functions provided by this feature (namely the methods prefixed with try_ and different allocator per collection instance).
We do need AllocError, but we define our own as it is a trivial empty struct.
--- rust/kernel/alloc.rs | 3 +++ rust/kernel/alloc/boxext.rs | 3 +-- rust/kernel/alloc/vecext.rs | 4 ++-- rust/kernel/error.rs | 13 ++----------- rust/kernel/init.rs | 3 +-- rust/kernel/lib.rs | 1 - rust/kernel/str.rs | 3 +-- rust/kernel/sync/arc.rs | 4 ++-- rust/kernel/workqueue.rs | 3 +-- 9 files changed, 13 insertions(+), 24 deletions(-)
rust/kernel/alloc.rs +3 -0
@@ -8,6 +8,9 @@pub mod boxext;pub mod vecext; +/// Indicates an allocation error. +pub struct AllocError ; +/// Flags to be used when allocating memory. /// /// They can be combined with the operators `|`, `&`, and `!`.
rust/kernel/alloc/boxext.rs +1 -2
@@ -2,9 +2,8 @@//! Extensions to [`Box`] for fallible allocations. -use super ::Flags ; +use super ::{AllocError ,Flags };use alloc::boxed::Box ; -use core::alloc::AllocError ;use core::mem::MaybeUninit ;use core::result::Result ;
rust/kernel/alloc/vecext.rs +2 -2
@@ -2,8 +2,8 @@//! Extensions to [`Vec`] for fallible allocations. -use super ::Flags ; -use alloc::{alloc::AllocError , vec::Vec }; +use super ::{AllocError ,Flags }; +use alloc::vec::Vec ;use core::{mem::ManuallyDrop , result::Result };/// Extensions to [`Vec`].
rust/kernel/error.rs +2 -11
@@ -4,12 +4,9 @@//! //! C header: [`include/uapi/asm-generic/errno-base.h`](srctree/include/uapi/asm-generic/errno-base.h) -use crate ::str::CStr ; +use crate ::{alloc::AllocError , str::CStr }; -use alloc::{ - alloc::{AllocError ,LayoutError }, - collections::TryReserveError , -}; +use alloc::alloc::LayoutError ;use core::convert::From ;use core::fmt;
@@ -192,12 +189,6 @@ fn from(_: Utf8Error) -> Error { } } -impl From <TryReserveError >for Error { -fn from (_:TryReserveError ) ->Error { - code::ENOMEM - } -} -impl From <LayoutError >for Error {fn from (_:LayoutError ) ->Error { code::ENOMEM
rust/kernel/init.rs +1 -2
@@ -211,14 +211,13 @@//! [`pin_init!`]: crate::pin_init! use crate ::{ - alloc::{boxext::BoxExt ,Flags }, + alloc::{boxext::BoxExt ,AllocError ,Flags }, error::{self,Error }, sync::UniqueArc , types::{Opaque ,ScopeGuard }, };use alloc::boxed::Box ;use core::{ - alloc::AllocError , cell::UnsafeCell , convert::Infallible , marker::PhantomData ,
rust/kernel/lib.rs +0 -1
@@ -12,7 +12,6 @@//! do so first instead of bypassing this crate. #![no_std] -#![feature(allocator_api)] #![feature(coerce_unsized)] #![feature(dispatch_from_dyn)] #![feature(new_uninit)]
rust/kernel/str.rs +1 -2
@@ -2,8 +2,7 @@//! String representations. -use crate ::alloc::{flags::*, vecext::VecExt }; -use alloc::alloc::AllocError ; +use crate ::alloc::{flags::*, vecext::VecExt ,AllocError };use alloc::vec::Vec ;use core::fmt::{self,Write };use core::ops::{self,Deref ,Index };
rust/kernel/sync/arc.rs +2 -2
@@ -16,7 +16,7 @@//! [`Arc`]: https://doc.rust-lang.org/std/sync/struct.Arc.html use crate ::{ - alloc::{boxext::BoxExt ,Flags }, + alloc::{boxext::BoxExt ,AllocError ,Flags }, bindings, error::{self,Error }, init::{self,InPlaceInit ,Init ,PinInit },
@@ -25,7 +25,7 @@ };use alloc::boxed::Box ;use core::{ - alloc::{AllocError ,Layout }, + alloc::Layout , fmt, marker::{PhantomData ,Unsize }, mem::{ManuallyDrop ,MaybeUninit },
rust/kernel/workqueue.rs +1 -2
@@ -132,9 +132,8 @@//! //! C header: [`include/linux/workqueue.h`](srctree/include/linux/workqueue.h) -use crate ::alloc::Flags ; +use crate ::alloc::{AllocError ,Flags };use crate ::{bindings, prelude::*, sync::Arc , sync::LockClassKey , types::Opaque }; -use alloc::alloc::AllocError ;use alloc::boxed::Box ;use core::marker::PhantomData ;use core::pin::Pin ;
Signature · 1 line
2.34.1
Re: [PATCH 10/10] rust: kernel: remove usage of `allocator_api` unstable feature
Quoted 27 lines · Wedson Almeida Filho
From: Wedson Almeida Filho <[email protected]>
With the adoption of
BoxExtandVecExt, we don't need the functions provided by this feature (namely the methods prefixed withtry_and different allocator per collection instance).We do need
AllocError, but we define our own as it is a trivial empty struct.Signed-off-by Wedson Almeida Filho <[email protected]>--- rust/kernel/alloc.rs | 3 +++ rust/kernel/alloc/boxext.rs | 3 +-- rust/kernel/alloc/vecext.rs | 4 ++-- rust/kernel/error.rs | 13 ++----------- rust/kernel/init.rs | 3 +-- rust/kernel/lib.rs | 1 - rust/kernel/str.rs | 3 +-- rust/kernel/sync/arc.rs | 4 ++-- rust/kernel/workqueue.rs | 3 +-- 9 files changed, 13 insertions(+), 24 deletions(-)rust/kernel/alloc.rs +0
-0
rust/kernel/alloc.rs line 12
Quoted 5 lines
rust/kernel/alloc.rs +2
-0@@ -8,6 +8,9 @@pub mod boxext;pub mod vecext; +/// Indicates an allocation error. +pub struct AllocError ;
I think it would be sensible to add this now:
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
Quoted 54 lines
rust/kernel/alloc.rs +1
-0+/// Flags to be used when allocating memory. /// /// They can be combined with the operators `|`, `&`, and `!`. rust/kernel/alloc/boxext.rs +1
-2@@ -2,9 +2,8 @@//! Extensions to [`Box`] for fallible allocations. -use super ::Flags ; +use super ::{AllocError ,Flags };use alloc::boxed::Box ; -use core::alloc::AllocError ;use core::mem::MaybeUninit ;use core::result::Result ;rust/kernel/alloc/vecext.rs +2
-2@@ -2,8 +2,8 @@//! Extensions to [`Vec`] for fallible allocations. -use super ::Flags ; -use alloc::{alloc::AllocError , vec::Vec }; +use super ::{AllocError ,Flags }; +use alloc::vec::Vec ;use core::{mem::ManuallyDrop , result::Result };/// Extensions to [`Vec`]. rust/kernel/error.rs +2
-5@@ -4,12 +4,9 @@//! //! C header: [`include/uapi/asm-generic/errno-base.h`](srctree/include/uapi/asm-generic/errno-base.h) -use crate ::str::CStr ; +use crate ::{alloc::AllocError , str::CStr }; -use alloc::{ - alloc::{AllocError ,LayoutError }, - collections::TryReserveError , -}; +use alloc::alloc::LayoutError ;use core::convert::From ;use core::fmt;
rust/kernel/error.rs fn from(_: Utf8Error) -> Error { · line 200
Quoted 9 lines
rust/kernel/error.rs +0
-6@@ -192,12 +189,6 @@ fn from(_: Utf8Error) -> Error { } } -impl From <TryReserveError >for Error { -fn from (_:TryReserveError ) ->Error { - code::ENOMEM - } -} -
Why are you removing this?
Signature · 2 lines
Cheers, Benno
Quoted 89 lines
rust/kernel/error.rs +0
-0impl From <LayoutError >for Error {fn from (_:LayoutError ) ->Error { code::ENOMEM rust/kernel/init.rs +1
-2@@ -211,14 +211,13 @@//! [`pin_init!`]: crate::pin_init! use crate ::{ - alloc::{boxext::BoxExt ,Flags }, + alloc::{boxext::BoxExt ,AllocError ,Flags }, error::{self,Error }, sync::UniqueArc , types::{Opaque ,ScopeGuard }, };use alloc::boxed::Box ;use core::{ - alloc::AllocError , cell::UnsafeCell , convert::Infallible , marker::PhantomData ,rust/kernel/lib.rs +0
-1@@ -12,7 +12,6 @@//! do so first instead of bypassing this crate. #![no_std] -#![feature(allocator_api)] #![feature(coerce_unsized)] #![feature(dispatch_from_dyn)] #![feature(new_uninit)] rust/kernel/str.rs +1
-2@@ -2,8 +2,7 @@//! String representations. -use crate ::alloc::{flags::*, vecext::VecExt }; -use alloc::alloc::AllocError ; +use crate ::alloc::{flags::*, vecext::VecExt ,AllocError };use alloc::vec::Vec ;use core::fmt::{self,Write };use core::ops::{self,Deref ,Index };rust/kernel/sync/arc.rs +2
-2@@ -16,7 +16,7 @@//! [`Arc`]: https://doc.rust-lang.org/std/sync/struct.Arc.html use crate ::{ - alloc::{boxext::BoxExt ,Flags }, + alloc::{boxext::BoxExt ,AllocError ,Flags }, bindings, error::{self,Error }, init::{self,InPlaceInit ,Init ,PinInit },@@ -25,7 +25,7 @@ };use alloc::boxed::Box ;use core::{ - alloc::{AllocError ,Layout }, + alloc::Layout , fmt, marker::{PhantomData ,Unsize }, mem::{ManuallyDrop ,MaybeUninit },rust/kernel/workqueue.rs +1
-3@@ -132,9 +132,8 @@//! //! C header: [`include/linux/workqueue.h`](srctree/include/linux/workqueue.h) -use crate ::alloc::Flags ; +use crate ::alloc::{AllocError ,Flags };use crate ::{bindings, prelude::*, sync::Arc , sync::LockClassKey , types::Opaque }; -use alloc::alloc::AllocError ;use alloc::boxed::Box ;use core::marker::PhantomData ;use core::pin::Pin ; --2.34.1
Re: [PATCH 10/10] rust: kernel: remove usage of `allocator_api` unstable feature
Quoted 32 lines · Benno Lossin
Quoted 30 lines · Wedson Almeida Filho
From: Wedson Almeida Filho <[email protected]>
With the adoption of
BoxExtandVecExt, we don't need the functions provided by this feature (namely the methods prefixed withtry_and different allocator per collection instance).We do need
AllocError, but we define our own as it is a trivial empty struct.Signed-off-by Wedson Almeida Filho <[email protected]>--- rust/kernel/alloc.rs | 3 +++ rust/kernel/alloc/boxext.rs | 3 +-- rust/kernel/alloc/vecext.rs | 4 ++-- rust/kernel/error.rs | 13 ++----------- rust/kernel/init.rs | 3 +-- rust/kernel/lib.rs | 1 - rust/kernel/str.rs | 3 +-- rust/kernel/sync/arc.rs | 4 ++-- rust/kernel/workqueue.rs | 3 +-- 9 files changed, 13 insertions(+), 24 deletions(-)rust/kernel/alloc.rs +0
-0@@ -8,6 +8,9 @@pub mod boxext;pub mod vecext;
Quoted 5 lines
rust/kernel/alloc.rs line 12
Quoted 2 lines
rust/kernel/alloc.rs +2
-0+/// Indicates an allocation error. +pub struct AllocError ;I think it would be sensible to add this now:
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
Agreed. Will add in v2.
Quoted 57 lines
Quoted 57 lines
rust/kernel/alloc.rs +1
-0+/// Flags to be used when allocating memory. /// /// They can be combined with the operators `|`, `&`, and `!`. rust/kernel/alloc/boxext.rs +1
-2@@ -2,9 +2,8 @@//! Extensions to [`Box`] for fallible allocations. -use super ::Flags ; +use super ::{AllocError ,Flags };use alloc::boxed::Box ; -use core::alloc::AllocError ;use core::mem::MaybeUninit ;use core::result::Result ;rust/kernel/alloc/vecext.rs +2
-2@@ -2,8 +2,8 @@//! Extensions to [`Vec`] for fallible allocations. -use super ::Flags ; -use alloc::{alloc::AllocError , vec::Vec }; +use super ::{AllocError ,Flags }; +use alloc::vec::Vec ;use core::{mem::ManuallyDrop , result::Result };/// Extensions to [`Vec`]. rust/kernel/error.rs +2
-5@@ -4,12 +4,9 @@//! //! C header: [`include/uapi/asm-generic/errno-base.h`](srctree/include/uapi/asm-generic/errno-base.h) -use crate ::str::CStr ; +use crate ::{alloc::AllocError , str::CStr }; -use alloc::{ - alloc::{AllocError ,LayoutError }, - collections::TryReserveError , -}; +use alloc::alloc::LayoutError ;use core::convert::From ;use core::fmt;@@ -192,12 +189,6 @@ fn from(_: Utf8Error) -> Error { } }
Quoted 8 lines
rust/kernel/error.rs fn from(_: Utf8Error) -> Error { · line 200
Quoted 6 lines
rust/kernel/error.rs +0
-6-impl From <TryReserveError >for Error { -fn from (_:TryReserveError ) ->Error { - code::ENOMEM - } -} -Why are you removing this?
We don't use TryReserveError anymore. I'll move this chunk to the patch 7 in v2, which is where we remove the use of this error.
Quoted 94 lines
--
Cheers,
BennoQuoted 89 lines
rust/kernel/error.rs +0
-0impl From <LayoutError >for Error {fn from (_:LayoutError ) ->Error { code::ENOMEM rust/kernel/init.rs +1
-2@@ -211,14 +211,13 @@//! [`pin_init!`]: crate::pin_init! use crate ::{ - alloc::{boxext::BoxExt ,Flags }, + alloc::{boxext::BoxExt ,AllocError ,Flags }, error::{self,Error }, sync::UniqueArc , types::{Opaque ,ScopeGuard }, };use alloc::boxed::Box ;use core::{ - alloc::AllocError , cell::UnsafeCell , convert::Infallible , marker::PhantomData ,rust/kernel/lib.rs +0
-1@@ -12,7 +12,6 @@//! do so first instead of bypassing this crate. #![no_std] -#![feature(allocator_api)] #![feature(coerce_unsized)] #![feature(dispatch_from_dyn)] #![feature(new_uninit)] rust/kernel/str.rs +1
-2@@ -2,8 +2,7 @@//! String representations. -use crate ::alloc::{flags::*, vecext::VecExt }; -use alloc::alloc::AllocError ; +use crate ::alloc::{flags::*, vecext::VecExt ,AllocError };use alloc::vec::Vec ;use core::fmt::{self,Write };use core::ops::{self,Deref ,Index };rust/kernel/sync/arc.rs +2
-2@@ -16,7 +16,7 @@//! [`Arc`]: https://doc.rust-lang.org/std/sync/struct.Arc.html use crate ::{ - alloc::{boxext::BoxExt ,Flags }, + alloc::{boxext::BoxExt ,AllocError ,Flags }, bindings, error::{self,Error }, init::{self,InPlaceInit ,Init ,PinInit },@@ -25,7 +25,7 @@ };use alloc::boxed::Box ;use core::{ - alloc::{AllocError ,Layout }, + alloc::Layout , fmt, marker::{PhantomData ,Unsize }, mem::{ManuallyDrop ,MaybeUninit },rust/kernel/workqueue.rs +1
-3@@ -132,9 +132,8 @@//! //! C header: [`include/linux/workqueue.h`](srctree/include/linux/workqueue.h) -use crate ::alloc::Flags ; +use crate ::alloc::{AllocError ,Flags };use crate ::{bindings, prelude::*, sync::Arc , sync::LockClassKey , types::Opaque }; -use alloc::alloc::AllocError ;use alloc::boxed::Box ;use core::marker::PhantomData ;use core::pin::Pin ; --2.34.1