[PATCH v7 25/26] kbuild: rust: remove the `alloc` crate and `GlobalAlloc`

Danilo Krummrich posted 26 patches 2 months ago
There is a newer version of this series
[PATCH v7 25/26] kbuild: rust: remove the `alloc` crate and `GlobalAlloc`
Posted by Danilo Krummrich 2 months ago
Now that we have our own `Allocator`, `Box` and `Vec` types we can remove
Rust's `alloc` crate and the `new_uninit` unstable feature.

Also remove `Kmalloc`'s `GlobalAlloc` implementation -- we can't remove
this in a separate patch, since the `alloc` crate requires a
`#[global_allocator]` to set, that implements `GlobalAlloc`.

Signed-off-by: Danilo Krummrich <dakr@kernel.org>
---
 rust/Makefile                     | 43 +++++----------------
 rust/exports.c                    |  1 -
 rust/kernel/alloc/allocator.rs    | 63 +------------------------------
 scripts/Makefile.build            |  7 +---
 scripts/generate_rust_analyzer.py | 11 +-----
 5 files changed, 15 insertions(+), 110 deletions(-)

diff --git a/rust/Makefile b/rust/Makefile
index 4eae318f36ff..a3ac60b48ae1 100644
--- a/rust/Makefile
+++ b/rust/Makefile
@@ -15,8 +15,8 @@ always-$(CONFIG_RUST) += libmacros.so
 no-clean-files += libmacros.so
 
 always-$(CONFIG_RUST) += bindings/bindings_generated.rs bindings/bindings_helpers_generated.rs
-obj-$(CONFIG_RUST) += alloc.o bindings.o kernel.o
-always-$(CONFIG_RUST) += exports_alloc_generated.h exports_helpers_generated.h \
+obj-$(CONFIG_RUST) += bindings.o kernel.o
+always-$(CONFIG_RUST) += exports_helpers_generated.h \
     exports_bindings_generated.h exports_kernel_generated.h
 
 always-$(CONFIG_RUST) += uapi/uapi_generated.rs
@@ -53,11 +53,6 @@ endif
 core-cfgs = \
     --cfg no_fp_fmt_parse
 
-alloc-cfgs = \
-    --cfg no_global_oom_handling \
-    --cfg no_rc \
-    --cfg no_sync
-
 quiet_cmd_rustdoc = RUSTDOC $(if $(rustdoc_host),H, ) $<
       cmd_rustdoc = \
 	OBJTREE=$(abspath $(objtree)) \
@@ -81,7 +76,7 @@ quiet_cmd_rustdoc = RUSTDOC $(if $(rustdoc_host),H, ) $<
 # command-like flags to solve the issue. Meanwhile, we use the non-custom case
 # and then retouch the generated files.
 rustdoc: rustdoc-core rustdoc-macros rustdoc-compiler_builtins \
-    rustdoc-alloc rustdoc-kernel
+    rustdoc-kernel
 	$(Q)cp $(srctree)/Documentation/images/logo.svg $(rustdoc_output)/static.files/
 	$(Q)cp $(srctree)/Documentation/images/COPYING-logo $(rustdoc_output)/static.files/
 	$(Q)find $(rustdoc_output) -name '*.html' -type f -print0 | xargs -0 sed -Ei \
@@ -105,20 +100,11 @@ rustdoc-core: $(RUST_LIB_SRC)/core/src/lib.rs FORCE
 rustdoc-compiler_builtins: $(src)/compiler_builtins.rs rustdoc-core FORCE
 	+$(call if_changed,rustdoc)
 
-# We need to allow `rustdoc::broken_intra_doc_links` because some
-# `no_global_oom_handling` functions refer to non-`no_global_oom_handling`
-# functions. Ideally `rustdoc` would have a way to distinguish broken links
-# 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: $(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 \
+rustdoc-kernel: private rustc_target_flags = \
     --extern build_error --extern macros=$(objtree)/$(obj)/libmacros.so \
     --extern bindings --extern uapi
 rustdoc-kernel: $(src)/kernel/lib.rs rustdoc-core rustdoc-macros \
-    rustdoc-compiler_builtins rustdoc-alloc $(obj)/libmacros.so \
+    rustdoc-compiler_builtins $(obj)/libmacros.so \
     $(obj)/bindings.o FORCE
 	+$(call if_changed,rustdoc)
 
@@ -162,7 +148,7 @@ quiet_cmd_rustdoc_test_kernel = RUSTDOC TK $<
 	mkdir -p $(objtree)/$(obj)/test/doctests/kernel; \
 	OBJTREE=$(abspath $(objtree)) \
 	$(RUSTDOC) --test $(rust_flags) \
-		-L$(objtree)/$(obj) --extern alloc --extern kernel \
+		-L$(objtree)/$(obj) --extern kernel \
 		--extern build_error --extern macros \
 		--extern bindings --extern uapi \
 		--no-run --crate-name kernel -Zunstable-options \
@@ -198,7 +184,7 @@ rusttest-macros: $(src)/macros/lib.rs FORCE
 	+$(call if_changed,rustc_test)
 	+$(call if_changed,rustdoc_test)
 
-rusttest-kernel: private rustc_target_flags = --extern alloc \
+rusttest-kernel: private rustc_target_flags = \
     --extern build_error --extern macros --extern bindings --extern uapi
 rusttest-kernel: $(src)/kernel/lib.rs \
     rusttestlib-build_error rusttestlib-macros rusttestlib-bindings \
@@ -311,9 +297,6 @@ quiet_cmd_exports = EXPORTS $@
 $(obj)/exports_core_generated.h: $(obj)/core.o FORCE
 	$(call if_changed,exports)
 
-$(obj)/exports_alloc_generated.h: $(obj)/alloc.o FORCE
-	$(call if_changed,exports)
-
 # Even though Rust kernel modules should never use the bindings directly,
 # symbols from the `bindings` crate and the C helpers need to be exported
 # because Rust generics and inlined functions may not get their code generated
@@ -360,7 +343,7 @@ quiet_cmd_rustc_library = $(if $(skip_clippy),RUSTC,$(RUSTC_OR_CLIPPY_QUIET)) L
 
 rust-analyzer:
 	$(Q)$(srctree)/scripts/generate_rust_analyzer.py \
-		--cfgs='core=$(core-cfgs)' --cfgs='alloc=$(alloc-cfgs)' \
+		--cfgs='core=$(core-cfgs)' \
 		$(realpath $(srctree)) $(realpath $(objtree)) \
 		$(rustc_sysroot) $(RUST_LIB_SRC) $(KBUILD_EXTMOD) > \
 		$(if $(KBUILD_EXTMOD),$(extmod_prefix),$(objtree))/rust-project.json
@@ -398,12 +381,6 @@ $(obj)/compiler_builtins.o: private rustc_objcopy = -w -W '__*'
 $(obj)/compiler_builtins.o: $(src)/compiler_builtins.rs $(obj)/core.o FORCE
 	+$(call if_changed_rule,rustc_library)
 
-$(obj)/alloc.o: private skip_clippy = 1
-$(obj)/alloc.o: private skip_flags = -Wunreachable_pub
-$(obj)/alloc.o: private rustc_target_flags = $(alloc-cfgs)
-$(obj)/alloc.o: $(RUST_LIB_SRC)/alloc/src/lib.rs $(obj)/compiler_builtins.o FORCE
-	+$(call if_changed_rule,rustc_library)
-
 $(obj)/build_error.o: $(src)/build_error.rs $(obj)/compiler_builtins.o FORCE
 	+$(call if_changed_rule,rustc_library)
 
@@ -418,9 +395,9 @@ $(obj)/uapi.o: $(src)/uapi/lib.rs \
     $(obj)/uapi/uapi_generated.rs FORCE
 	+$(call if_changed_rule,rustc_library)
 
-$(obj)/kernel.o: private rustc_target_flags = --extern alloc \
+$(obj)/kernel.o: private rustc_target_flags = \
     --extern build_error --extern macros --extern bindings --extern uapi
-$(obj)/kernel.o: $(src)/kernel/lib.rs $(obj)/alloc.o $(obj)/build_error.o \
+$(obj)/kernel.o: $(src)/kernel/lib.rs $(obj)/build_error.o \
     $(obj)/libmacros.so $(obj)/bindings.o $(obj)/uapi.o FORCE
 	+$(call if_changed_rule,rustc_library)
 
diff --git a/rust/exports.c b/rust/exports.c
index e5695f3b45b7..82a037381798 100644
--- a/rust/exports.c
+++ b/rust/exports.c
@@ -16,7 +16,6 @@
 #define EXPORT_SYMBOL_RUST_GPL(sym) extern int sym; EXPORT_SYMBOL_GPL(sym)
 
 #include "exports_core_generated.h"
-#include "exports_alloc_generated.h"
 #include "exports_helpers_generated.h"
 #include "exports_bindings_generated.h"
 #include "exports_kernel_generated.h"
diff --git a/rust/kernel/alloc/allocator.rs b/rust/kernel/alloc/allocator.rs
index a5d7e66a68db..0b586c0361f4 100644
--- a/rust/kernel/alloc/allocator.rs
+++ b/rust/kernel/alloc/allocator.rs
@@ -8,8 +8,8 @@
 //!
 //! Reference: <https://docs.kernel.org/core-api/memory-allocation.html>
 
-use super::{flags::*, Flags};
-use core::alloc::{GlobalAlloc, Layout};
+use super::Flags;
+use core::alloc::Layout;
 use core::ptr;
 use core::ptr::NonNull;
 
@@ -54,23 +54,6 @@ fn aligned_size(new_layout: Layout) -> usize {
     layout.size()
 }
 
-/// Calls `krealloc` with a proper size to alloc a new object aligned to `new_layout`'s alignment.
-///
-/// # Safety
-///
-/// - `ptr` can be either null or a pointer which has been allocated by this allocator.
-/// - `new_layout` must have a non-zero size.
-pub(crate) unsafe fn krealloc_aligned(ptr: *mut u8, new_layout: Layout, flags: Flags) -> *mut u8 {
-    let size = aligned_size(new_layout);
-
-    // SAFETY:
-    // - `ptr` is either null or a pointer returned from a previous `k{re}alloc()` by the
-    //   function safety requirement.
-    // - `size` is greater than 0 since it's from `layout.size()` (which cannot be zero according
-    //   to the function safety requirement)
-    unsafe { bindings::krealloc(ptr as *const core::ffi::c_void, size, flags.0) as *mut u8 }
-}
-
 /// # Invariants
 ///
 /// One of the following `krealloc`, `vrealloc`, `kvrealloc`.
@@ -148,41 +131,6 @@ unsafe fn realloc(
     }
 }
 
-unsafe impl GlobalAlloc for Kmalloc {
-    unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
-        // SAFETY: `ptr::null_mut()` is null and `layout` has a non-zero size by the function safety
-        // requirement.
-        unsafe { krealloc_aligned(ptr::null_mut(), layout, GFP_KERNEL) }
-    }
-
-    unsafe fn dealloc(&self, ptr: *mut u8, _layout: Layout) {
-        unsafe {
-            bindings::kfree(ptr as *const core::ffi::c_void);
-        }
-    }
-
-    unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
-        // SAFETY:
-        // - `new_size`, when rounded up to the nearest multiple of `layout.align()`, will not
-        //   overflow `isize` by the function safety requirement.
-        // - `layout.align()` is a proper alignment (i.e. not zero and must be a power of two).
-        let layout = unsafe { Layout::from_size_align_unchecked(new_size, layout.align()) };
-
-        // SAFETY:
-        // - `ptr` is either null or a pointer allocated by this allocator by the function safety
-        //   requirement.
-        // - the size of `layout` is not zero because `new_size` is not zero by the function safety
-        //   requirement.
-        unsafe { krealloc_aligned(ptr, layout, GFP_KERNEL) }
-    }
-
-    unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 {
-        // SAFETY: `ptr::null_mut()` is null and `layout` has a non-zero size by the function safety
-        // requirement.
-        unsafe { krealloc_aligned(ptr::null_mut(), layout, GFP_KERNEL | __GFP_ZERO) }
-    }
-}
-
 // SAFETY: `realloc` delegates to `ReallocFunc::call`, which guarantees that
 // - memory remains valid until it is explicitly freed,
 // - passing a pointer to a valid memory allocation is OK,
@@ -228,10 +176,3 @@ unsafe fn realloc(
         unsafe { ReallocFunc::KVREALLOC.call(ptr, layout, flags) }
     }
 }
-
-#[global_allocator]
-static ALLOCATOR: Kmalloc = Kmalloc;
-
-// See <https://github.com/rust-lang/rust/pull/86844>.
-#[no_mangle]
-static __rust_no_alloc_shim_is_unstable: u8 = 0;
diff --git a/scripts/Makefile.build b/scripts/Makefile.build
index 72b1232b1f7d..529ec5972e55 100644
--- a/scripts/Makefile.build
+++ b/scripts/Makefile.build
@@ -262,18 +262,13 @@ $(obj)/%.lst: $(obj)/%.c FORCE
 
 # Compile Rust sources (.rs)
 # ---------------------------------------------------------------------------
-
-rust_allowed_features := new_uninit
-
 # `--out-dir` is required to avoid temporaries being created by `rustc` in the
 # current working directory, which may be not accessible in the out-of-tree
 # modules case.
 rust_common_cmd = \
 	RUST_MODFILE=$(modfile) $(RUSTC_OR_CLIPPY) $(rust_flags) \
-	-Zallow-features=$(rust_allowed_features) \
 	-Zcrate-attr=no_std \
-	-Zcrate-attr='feature($(rust_allowed_features))' \
-	-Zunstable-options --extern force:alloc --extern kernel \
+	-Zunstable-options --extern kernel \
 	--crate-type rlib -L $(objtree)/rust/ \
 	--crate-name $(basename $(notdir $@)) \
 	--sysroot=/dev/null \
diff --git a/scripts/generate_rust_analyzer.py b/scripts/generate_rust_analyzer.py
index d2bc63cde8c6..09e1d166d8d2 100755
--- a/scripts/generate_rust_analyzer.py
+++ b/scripts/generate_rust_analyzer.py
@@ -64,13 +64,6 @@ def generate_crates(srctree, objtree, sysroot_src, external_src, cfgs):
         [],
     )
 
-    append_crate(
-        "alloc",
-        sysroot_src / "alloc" / "src" / "lib.rs",
-        ["core", "compiler_builtins"],
-        cfg=crates_cfgs.get("alloc", []),
-    )
-
     append_crate(
         "macros",
         srctree / "rust" / "macros" / "lib.rs",
@@ -96,7 +89,7 @@ def generate_crates(srctree, objtree, sysroot_src, external_src, cfgs):
     append_crate(
         "kernel",
         srctree / "rust" / "kernel" / "lib.rs",
-        ["core", "alloc", "macros", "build_error", "bindings"],
+        ["core", "macros", "build_error", "bindings"],
         cfg=cfg,
     )
     crates[-1]["source"] = {
@@ -133,7 +126,7 @@ def generate_crates(srctree, objtree, sysroot_src, external_src, cfgs):
             append_crate(
                 name,
                 path,
-                ["core", "alloc", "kernel"],
+                ["core", "kernel"],
                 cfg=cfg,
             )
 
-- 
2.46.0
Re: [PATCH v7 25/26] kbuild: rust: remove the `alloc` crate and `GlobalAlloc`
Posted by Gary Guo 1 month, 2 weeks ago
On Thu, 12 Sep 2024 00:53:01 +0200
Danilo Krummrich <dakr@kernel.org> wrote:

> Now that we have our own `Allocator`, `Box` and `Vec` types we can remove
> Rust's `alloc` crate and the `new_uninit` unstable feature.
> 
> Also remove `Kmalloc`'s `GlobalAlloc` implementation -- we can't remove
> this in a separate patch, since the `alloc` crate requires a
> `#[global_allocator]` to set, that implements `GlobalAlloc`.
> 
> Signed-off-by: Danilo Krummrich <dakr@kernel.org>
> ---
>  rust/Makefile                     | 43 +++++----------------
>  rust/exports.c                    |  1 -
>  rust/kernel/alloc/allocator.rs    | 63 +------------------------------
>  scripts/Makefile.build            |  7 +---
>  scripts/generate_rust_analyzer.py | 11 +-----
>  5 files changed, 15 insertions(+), 110 deletions(-)
> 
> diff --git a/scripts/Makefile.build b/scripts/Makefile.build
> index 72b1232b1f7d..529ec5972e55 100644
> --- a/scripts/Makefile.build
> +++ b/scripts/Makefile.build
> @@ -262,18 +262,13 @@ $(obj)/%.lst: $(obj)/%.c FORCE
>  
>  # Compile Rust sources (.rs)
>  # ---------------------------------------------------------------------------
> -
> -rust_allowed_features := new_uninit
> -

Would it make sense to throw in a soon-to-be-stable (or
already-stable-but-beyond-min-rust-version) feature here to not have to
remove the build system support for unstable feature directly?

For example, I know that const_refs_to_static might be added here
soon-ish.

>  # `--out-dir` is required to avoid temporaries being created by `rustc` in the
>  # current working directory, which may be not accessible in the out-of-tree
>  # modules case.
>  rust_common_cmd = \
>  	RUST_MODFILE=$(modfile) $(RUSTC_OR_CLIPPY) $(rust_flags) \
> -	-Zallow-features=$(rust_allowed_features) \

I think this actually allow all features instead of allowing none.

>  	-Zcrate-attr=no_std \
> -	-Zcrate-attr='feature($(rust_allowed_features))' \
> -	-Zunstable-options --extern force:alloc --extern kernel \
> +	-Zunstable-options --extern kernel \
>  	--crate-type rlib -L $(objtree)/rust/ \
>  	--crate-name $(basename $(notdir $@)) \
>  	--sysroot=/dev/null \
> diff --git a/scripts/generate_rust_analyzer.py b/scripts/generate_rust_analyzer.py
> index d2bc63cde8c6..09e1d166d8d2 100755
> --- a/scripts/generate_rust_analyzer.py
> +++ b/scripts/generate_rust_analyzer.py
> @@ -64,13 +64,6 @@ def generate_crates(srctree, objtree, sysroot_src, external_src, cfgs):
>          [],
>      )
>  
> -    append_crate(
> -        "alloc",
> -        sysroot_src / "alloc" / "src" / "lib.rs",
> -        ["core", "compiler_builtins"],
> -        cfg=crates_cfgs.get("alloc", []),
> -    )
> -
>      append_crate(
>          "macros",
>          srctree / "rust" / "macros" / "lib.rs",
> @@ -96,7 +89,7 @@ def generate_crates(srctree, objtree, sysroot_src, external_src, cfgs):
>      append_crate(
>          "kernel",
>          srctree / "rust" / "kernel" / "lib.rs",
> -        ["core", "alloc", "macros", "build_error", "bindings"],
> +        ["core", "macros", "build_error", "bindings"],
>          cfg=cfg,
>      )
>      crates[-1]["source"] = {
> @@ -133,7 +126,7 @@ def generate_crates(srctree, objtree, sysroot_src, external_src, cfgs):
>              append_crate(
>                  name,
>                  path,
> -                ["core", "alloc", "kernel"],
> +                ["core", "kernel"],
>                  cfg=cfg,
>              )
>
Re: [PATCH v7 25/26] kbuild: rust: remove the `alloc` crate and `GlobalAlloc`
Posted by Miguel Ojeda 1 month, 1 week ago
On Mon, Sep 30, 2024 at 9:11 PM Gary Guo <gary@garyguo.net> wrote:
>
> Would it make sense to throw in a soon-to-be-stable (or
> already-stable-but-beyond-min-rust-version) feature here to not have to
> remove the build system support for unstable feature directly?

Yeah, agreed -- we are going to have `lint_reasons` and
`arbitrary_self_types` before this series, so it should be fine.

Cheers,
Miguel
Re: [PATCH v7 25/26] kbuild: rust: remove the `alloc` crate and `GlobalAlloc`
Posted by Danilo Krummrich 1 month, 1 week ago
On 10/3/24 11:41 PM, Miguel Ojeda wrote:
> On Mon, Sep 30, 2024 at 9:11 PM Gary Guo <gary@garyguo.net> wrote:
>>
>> Would it make sense to throw in a soon-to-be-stable (or
>> already-stable-but-beyond-min-rust-version) feature here to not have to
>> remove the build system support for unstable feature directly?
> 
> Yeah, agreed -- we are going to have `lint_reasons` and
> `arbitrary_self_types` before this series, so it should be fine.

So, I guess you'll just fix it up when applying the series?

> 
> Cheers,
> Miguel
> 

Re: [PATCH v7 25/26] kbuild: rust: remove the `alloc` crate and `GlobalAlloc`
Posted by Miguel Ojeda 1 month, 1 week ago
On Thu, Oct 3, 2024 at 11:54 PM Danilo Krummrich <dakr@kernel.org> wrote:
>
> So, I guess you'll just fix it up when applying the series?

Sure, if there is no need for a new version, then no worries.

Cheers,
Miguel
Re: [PATCH v7 25/26] kbuild: rust: remove the `alloc` crate and `GlobalAlloc`
Posted by Danilo Krummrich 1 month, 2 weeks ago
On Sat, Sep 28, 2024 at 08:43:57PM +0100, Gary Guo wrote:
> On Thu, 12 Sep 2024 00:53:01 +0200
> Danilo Krummrich <dakr@kernel.org> wrote:
> 
> > Now that we have our own `Allocator`, `Box` and `Vec` types we can remove
> > Rust's `alloc` crate and the `new_uninit` unstable feature.
> > 
> > Also remove `Kmalloc`'s `GlobalAlloc` implementation -- we can't remove
> > this in a separate patch, since the `alloc` crate requires a
> > `#[global_allocator]` to set, that implements `GlobalAlloc`.
> > 
> > Signed-off-by: Danilo Krummrich <dakr@kernel.org>
> > ---
> >  rust/Makefile                     | 43 +++++----------------
> >  rust/exports.c                    |  1 -
> >  rust/kernel/alloc/allocator.rs    | 63 +------------------------------
> >  scripts/Makefile.build            |  7 +---
> >  scripts/generate_rust_analyzer.py | 11 +-----
> >  5 files changed, 15 insertions(+), 110 deletions(-)
> > 
> > diff --git a/scripts/Makefile.build b/scripts/Makefile.build
> > index 72b1232b1f7d..529ec5972e55 100644
> > --- a/scripts/Makefile.build
> > +++ b/scripts/Makefile.build
> > @@ -262,18 +262,13 @@ $(obj)/%.lst: $(obj)/%.c FORCE
> >  
> >  # Compile Rust sources (.rs)
> >  # ---------------------------------------------------------------------------
> > -
> > -rust_allowed_features := new_uninit
> > -
> 
> Would it make sense to throw in a soon-to-be-stable (or
> already-stable-but-beyond-min-rust-version) feature here to not have to
> remove the build system support for unstable feature directly?

It's only about those two lines, right? I can only remove the `new_uninit` and
leave `rust_allowed_features :=` and leave...

> 
> For example, I know that const_refs_to_static might be added here
> soon-ish.
> 
> >  # `--out-dir` is required to avoid temporaries being created by `rustc` in the
> >  # current working directory, which may be not accessible in the out-of-tree
> >  # modules case.
> >  rust_common_cmd = \
> >  	RUST_MODFILE=$(modfile) $(RUSTC_OR_CLIPPY) $(rust_flags) \
> > -	-Zallow-features=$(rust_allowed_features) \

...this line, I guess?

> 
> I think this actually allow all features instead of allowing none.
> 
> >  	-Zcrate-attr=no_std \
> > -	-Zcrate-attr='feature($(rust_allowed_features))' \
> > -	-Zunstable-options --extern force:alloc --extern kernel \
> > +	-Zunstable-options --extern kernel \
> >  	--crate-type rlib -L $(objtree)/rust/ \
> >  	--crate-name $(basename $(notdir $@)) \
> >  	--sysroot=/dev/null \
> > diff --git a/scripts/generate_rust_analyzer.py b/scripts/generate_rust_analyzer.py
> > index d2bc63cde8c6..09e1d166d8d2 100755
> > --- a/scripts/generate_rust_analyzer.py
> > +++ b/scripts/generate_rust_analyzer.py
> > @@ -64,13 +64,6 @@ def generate_crates(srctree, objtree, sysroot_src, external_src, cfgs):
> >          [],
> >      )
> >  
> > -    append_crate(
> > -        "alloc",
> > -        sysroot_src / "alloc" / "src" / "lib.rs",
> > -        ["core", "compiler_builtins"],
> > -        cfg=crates_cfgs.get("alloc", []),
> > -    )
> > -
> >      append_crate(
> >          "macros",
> >          srctree / "rust" / "macros" / "lib.rs",
> > @@ -96,7 +89,7 @@ def generate_crates(srctree, objtree, sysroot_src, external_src, cfgs):
> >      append_crate(
> >          "kernel",
> >          srctree / "rust" / "kernel" / "lib.rs",
> > -        ["core", "alloc", "macros", "build_error", "bindings"],
> > +        ["core", "macros", "build_error", "bindings"],
> >          cfg=cfg,
> >      )
> >      crates[-1]["source"] = {
> > @@ -133,7 +126,7 @@ def generate_crates(srctree, objtree, sysroot_src, external_src, cfgs):
> >              append_crate(
> >                  name,
> >                  path,
> > -                ["core", "alloc", "kernel"],
> > +                ["core", "kernel"],
> >                  cfg=cfg,
> >              )
> >  
>
Re: [PATCH v7 25/26] kbuild: rust: remove the `alloc` crate and `GlobalAlloc`
Posted by Danilo Krummrich 1 month, 1 week ago
On 9/29/24 5:17 PM, Danilo Krummrich wrote:
> On Sat, Sep 28, 2024 at 08:43:57PM +0100, Gary Guo wrote:
>> On Thu, 12 Sep 2024 00:53:01 +0200
>> Danilo Krummrich <dakr@kernel.org> wrote:
>>
>>> Now that we have our own `Allocator`, `Box` and `Vec` types we can remove
>>> Rust's `alloc` crate and the `new_uninit` unstable feature.
>>>
>>> Also remove `Kmalloc`'s `GlobalAlloc` implementation -- we can't remove
>>> this in a separate patch, since the `alloc` crate requires a
>>> `#[global_allocator]` to set, that implements `GlobalAlloc`.
>>>
>>> Signed-off-by: Danilo Krummrich <dakr@kernel.org>
>>> ---
>>>   rust/Makefile                     | 43 +++++----------------
>>>   rust/exports.c                    |  1 -
>>>   rust/kernel/alloc/allocator.rs    | 63 +------------------------------
>>>   scripts/Makefile.build            |  7 +---
>>>   scripts/generate_rust_analyzer.py | 11 +-----
>>>   5 files changed, 15 insertions(+), 110 deletions(-)
>>>
>>> diff --git a/scripts/Makefile.build b/scripts/Makefile.build
>>> index 72b1232b1f7d..529ec5972e55 100644
>>> --- a/scripts/Makefile.build
>>> +++ b/scripts/Makefile.build
>>> @@ -262,18 +262,13 @@ $(obj)/%.lst: $(obj)/%.c FORCE
>>>   
>>>   # Compile Rust sources (.rs)
>>>   # ---------------------------------------------------------------------------
>>> -
>>> -rust_allowed_features := new_uninit
>>> -
>>
>> Would it make sense to throw in a soon-to-be-stable (or
>> already-stable-but-beyond-min-rust-version) feature here to not have to
>> remove the build system support for unstable feature directly?
> 
> It's only about those two lines, right? I can only remove the `new_uninit` and
> leave `rust_allowed_features :=` and leave...
> 
>>
>> For example, I know that const_refs_to_static might be added here
>> soon-ish.
>>
>>>   # `--out-dir` is required to avoid temporaries being created by `rustc` in the
>>>   # current working directory, which may be not accessible in the out-of-tree
>>>   # modules case.
>>>   rust_common_cmd = \
>>>   	RUST_MODFILE=$(modfile) $(RUSTC_OR_CLIPPY) $(rust_flags) \
>>> -	-Zallow-features=$(rust_allowed_features) \
> 
> ...this line, I guess?

Seems like I can't, it blows up with an empty feature list. I'll keep it as it 
is for now. Shouldn't be a big deal to add it again, once needed.

> 
>>
>> I think this actually allow all features instead of allowing none.
>>
>>>   	-Zcrate-attr=no_std \
>>> -	-Zcrate-attr='feature($(rust_allowed_features))' \
>>> -	-Zunstable-options --extern force:alloc --extern kernel \
>>> +	-Zunstable-options --extern kernel \
>>>   	--crate-type rlib -L $(objtree)/rust/ \
>>>   	--crate-name $(basename $(notdir $@)) \
>>>   	--sysroot=/dev/null \
>>> diff --git a/scripts/generate_rust_analyzer.py b/scripts/generate_rust_analyzer.py
>>> index d2bc63cde8c6..09e1d166d8d2 100755
>>> --- a/scripts/generate_rust_analyzer.py
>>> +++ b/scripts/generate_rust_analyzer.py
>>> @@ -64,13 +64,6 @@ def generate_crates(srctree, objtree, sysroot_src, external_src, cfgs):
>>>           [],
>>>       )
>>>   
>>> -    append_crate(
>>> -        "alloc",
>>> -        sysroot_src / "alloc" / "src" / "lib.rs",
>>> -        ["core", "compiler_builtins"],
>>> -        cfg=crates_cfgs.get("alloc", []),
>>> -    )
>>> -
>>>       append_crate(
>>>           "macros",
>>>           srctree / "rust" / "macros" / "lib.rs",
>>> @@ -96,7 +89,7 @@ def generate_crates(srctree, objtree, sysroot_src, external_src, cfgs):
>>>       append_crate(
>>>           "kernel",
>>>           srctree / "rust" / "kernel" / "lib.rs",
>>> -        ["core", "alloc", "macros", "build_error", "bindings"],
>>> +        ["core", "macros", "build_error", "bindings"],
>>>           cfg=cfg,
>>>       )
>>>       crates[-1]["source"] = {
>>> @@ -133,7 +126,7 @@ def generate_crates(srctree, objtree, sysroot_src, external_src, cfgs):
>>>               append_crate(
>>>                   name,
>>>                   path,
>>> -                ["core", "alloc", "kernel"],
>>> +                ["core", "kernel"],
>>>                   cfg=cfg,
>>>               )
>>>   
>>