Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

kbuild: rust: remove the `alloc` crate and `GlobalAlloc`

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>
Link: https://lore.kernel.org/r/20241004154149.93856-29-dakr@kernel.org
Signed-off-by: Miguel Ojeda <ojeda@kernel.org>

authored by

Danilo Krummrich and committed by
Miguel Ojeda
392e34b6 8ae740c3

+16 -108
+10 -33
rust/Makefile
··· 15 15 no-clean-files += libmacros.so 16 16 17 17 always-$(CONFIG_RUST) += bindings/bindings_generated.rs bindings/bindings_helpers_generated.rs 18 - obj-$(CONFIG_RUST) += alloc.o bindings.o kernel.o 19 - always-$(CONFIG_RUST) += exports_alloc_generated.h exports_helpers_generated.h \ 18 + obj-$(CONFIG_RUST) += bindings.o kernel.o 19 + always-$(CONFIG_RUST) += exports_helpers_generated.h \ 20 20 exports_bindings_generated.h exports_kernel_generated.h 21 21 22 22 always-$(CONFIG_RUST) += uapi/uapi_generated.rs ··· 53 53 core-cfgs = \ 54 54 --cfg no_fp_fmt_parse 55 55 56 - alloc-cfgs = \ 57 - --cfg no_global_oom_handling \ 58 - --cfg no_rc \ 59 - --cfg no_sync 60 - 61 56 quiet_cmd_rustdoc = RUSTDOC $(if $(rustdoc_host),H, ) $< 62 57 cmd_rustdoc = \ 63 58 OBJTREE=$(abspath $(objtree)) \ ··· 76 81 # command-like flags to solve the issue. Meanwhile, we use the non-custom case 77 82 # and then retouch the generated files. 78 83 rustdoc: rustdoc-core rustdoc-macros rustdoc-compiler_builtins \ 79 - rustdoc-alloc rustdoc-kernel 84 + rustdoc-kernel 80 85 $(Q)cp $(srctree)/Documentation/images/logo.svg $(rustdoc_output)/static.files/ 81 86 $(Q)cp $(srctree)/Documentation/images/COPYING-logo $(rustdoc_output)/static.files/ 82 87 $(Q)find $(rustdoc_output) -name '*.html' -type f -print0 | xargs -0 sed -Ei \ ··· 103 108 rustdoc-compiler_builtins: $(src)/compiler_builtins.rs rustdoc-core FORCE 104 109 +$(call if_changed,rustdoc) 105 110 106 - # We need to allow `rustdoc::broken_intra_doc_links` because some 107 - # `no_global_oom_handling` functions refer to non-`no_global_oom_handling` 108 - # functions. Ideally `rustdoc` would have a way to distinguish broken links 109 - # due to things that are "configured out" vs. entirely non-existing ones. 110 - rustdoc-alloc: private rustc_target_flags = $(alloc-cfgs) \ 111 - -Arustdoc::broken_intra_doc_links 112 - rustdoc-alloc: $(RUST_LIB_SRC)/alloc/src/lib.rs rustdoc-core rustdoc-compiler_builtins FORCE 113 - +$(call if_changed,rustdoc) 114 - 115 - rustdoc-kernel: private rustc_target_flags = --extern alloc \ 111 + rustdoc-kernel: private rustc_target_flags = \ 116 112 --extern build_error --extern macros=$(objtree)/$(obj)/libmacros.so \ 117 113 --extern bindings --extern uapi 118 114 rustdoc-kernel: $(src)/kernel/lib.rs rustdoc-core rustdoc-macros \ 119 - rustdoc-compiler_builtins rustdoc-alloc $(obj)/libmacros.so \ 115 + rustdoc-compiler_builtins $(obj)/libmacros.so \ 120 116 $(obj)/bindings.o FORCE 121 117 +$(call if_changed,rustdoc) 122 118 ··· 151 165 mkdir -p $(objtree)/$(obj)/test/doctests/kernel; \ 152 166 OBJTREE=$(abspath $(objtree)) \ 153 167 $(RUSTDOC) --test $(rust_flags) \ 154 - -L$(objtree)/$(obj) --extern alloc --extern kernel \ 168 + -L$(objtree)/$(obj) --extern kernel \ 155 169 --extern build_error --extern macros \ 156 170 --extern bindings --extern uapi \ 157 171 --no-run --crate-name kernel -Zunstable-options \ ··· 187 201 +$(call if_changed,rustc_test) 188 202 +$(call if_changed,rustdoc_test) 189 203 190 - rusttest-kernel: private rustc_target_flags = --extern alloc \ 204 + rusttest-kernel: private rustc_target_flags = \ 191 205 --extern build_error --extern macros --extern bindings --extern uapi 192 206 rusttest-kernel: $(src)/kernel/lib.rs \ 193 207 rusttestlib-build_error rusttestlib-macros rusttestlib-bindings \ ··· 300 314 $(obj)/exports_core_generated.h: $(obj)/core.o FORCE 301 315 $(call if_changed,exports) 302 316 303 - $(obj)/exports_alloc_generated.h: $(obj)/alloc.o FORCE 304 - $(call if_changed,exports) 305 - 306 317 # Even though Rust kernel modules should never use the bindings directly, 307 318 # symbols from the `bindings` crate and the C helpers need to be exported 308 319 # because Rust generics and inlined functions may not get their code generated ··· 346 363 347 364 rust-analyzer: 348 365 $(Q)$(srctree)/scripts/generate_rust_analyzer.py \ 349 - --cfgs='core=$(core-cfgs)' --cfgs='alloc=$(alloc-cfgs)' \ 366 + --cfgs='core=$(core-cfgs)' \ 350 367 $(realpath $(srctree)) $(realpath $(objtree)) \ 351 368 $(rustc_sysroot) $(RUST_LIB_SRC) $(KBUILD_EXTMOD) > \ 352 369 $(if $(KBUILD_EXTMOD),$(extmod_prefix),$(objtree))/rust-project.json ··· 384 401 $(obj)/compiler_builtins.o: $(src)/compiler_builtins.rs $(obj)/core.o FORCE 385 402 +$(call if_changed_rule,rustc_library) 386 403 387 - $(obj)/alloc.o: private skip_clippy = 1 388 - $(obj)/alloc.o: private skip_flags = -Wunreachable_pub 389 - $(obj)/alloc.o: private rustc_target_flags = $(alloc-cfgs) 390 - $(obj)/alloc.o: $(RUST_LIB_SRC)/alloc/src/lib.rs $(obj)/compiler_builtins.o FORCE 391 - +$(call if_changed_rule,rustc_library) 392 - 393 404 $(obj)/build_error.o: $(src)/build_error.rs $(obj)/compiler_builtins.o FORCE 394 405 +$(call if_changed_rule,rustc_library) 395 406 ··· 398 421 $(obj)/uapi/uapi_generated.rs FORCE 399 422 +$(call if_changed_rule,rustc_library) 400 423 401 - $(obj)/kernel.o: private rustc_target_flags = --extern alloc \ 424 + $(obj)/kernel.o: private rustc_target_flags = \ 402 425 --extern build_error --extern macros --extern bindings --extern uapi 403 - $(obj)/kernel.o: $(src)/kernel/lib.rs $(obj)/alloc.o $(obj)/build_error.o \ 426 + $(obj)/kernel.o: $(src)/kernel/lib.rs $(obj)/build_error.o \ 404 427 $(obj)/libmacros.so $(obj)/bindings.o $(obj)/uapi.o FORCE 405 428 +$(call if_changed_rule,rustc_library) 406 429
-1
rust/exports.c
··· 16 16 #define EXPORT_SYMBOL_RUST_GPL(sym) extern int sym; EXPORT_SYMBOL_GPL(sym) 17 17 18 18 #include "exports_core_generated.h" 19 - #include "exports_alloc_generated.h" 20 19 #include "exports_helpers_generated.h" 21 20 #include "exports_bindings_generated.h" 22 21 #include "exports_kernel_generated.h"
+2 -63
rust/kernel/alloc/allocator.rs
··· 8 8 //! 9 9 //! Reference: <https://docs.kernel.org/core-api/memory-allocation.html> 10 10 11 - use super::{flags::*, Flags}; 12 - use core::alloc::{GlobalAlloc, Layout}; 11 + use super::Flags; 12 + use core::alloc::Layout; 13 13 use core::ptr; 14 14 use core::ptr::NonNull; 15 15 ··· 52 52 // which together with the slab guarantees means the `krealloc` will return a properly aligned 53 53 // object (see comments in `kmalloc()` for more information). 54 54 layout.size() 55 - } 56 - 57 - /// Calls `krealloc` with a proper size to alloc a new object aligned to `new_layout`'s alignment. 58 - /// 59 - /// # Safety 60 - /// 61 - /// - `ptr` can be either null or a pointer which has been allocated by this allocator. 62 - /// - `new_layout` must have a non-zero size. 63 - pub(crate) unsafe fn krealloc_aligned(ptr: *mut u8, new_layout: Layout, flags: Flags) -> *mut u8 { 64 - let size = aligned_size(new_layout); 65 - 66 - // SAFETY: 67 - // - `ptr` is either null or a pointer returned from a previous `k{re}alloc()` by the 68 - // function safety requirement. 69 - // - `size` is greater than 0 since it's from `layout.size()` (which cannot be zero according 70 - // to the function safety requirement) 71 - unsafe { bindings::krealloc(ptr as *const core::ffi::c_void, size, flags.0) as *mut u8 } 72 55 } 73 56 74 57 /// # Invariants ··· 139 156 } 140 157 } 141 158 142 - // SAFETY: TODO. 143 - unsafe impl GlobalAlloc for Kmalloc { 144 - unsafe fn alloc(&self, layout: Layout) -> *mut u8 { 145 - // SAFETY: `ptr::null_mut()` is null and `layout` has a non-zero size by the function safety 146 - // requirement. 147 - unsafe { krealloc_aligned(ptr::null_mut(), layout, GFP_KERNEL) } 148 - } 149 - 150 - unsafe fn dealloc(&self, ptr: *mut u8, _layout: Layout) { 151 - // SAFETY: TODO. 152 - unsafe { 153 - bindings::kfree(ptr as *const core::ffi::c_void); 154 - } 155 - } 156 - 157 - unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 { 158 - // SAFETY: 159 - // - `new_size`, when rounded up to the nearest multiple of `layout.align()`, will not 160 - // overflow `isize` by the function safety requirement. 161 - // - `layout.align()` is a proper alignment (i.e. not zero and must be a power of two). 162 - let layout = unsafe { Layout::from_size_align_unchecked(new_size, layout.align()) }; 163 - 164 - // SAFETY: 165 - // - `ptr` is either null or a pointer allocated by this allocator by the function safety 166 - // requirement. 167 - // - the size of `layout` is not zero because `new_size` is not zero by the function safety 168 - // requirement. 169 - unsafe { krealloc_aligned(ptr, layout, GFP_KERNEL) } 170 - } 171 - 172 - unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 { 173 - // SAFETY: `ptr::null_mut()` is null and `layout` has a non-zero size by the function safety 174 - // requirement. 175 - unsafe { krealloc_aligned(ptr::null_mut(), layout, GFP_KERNEL | __GFP_ZERO) } 176 - } 177 - } 178 - 179 159 // SAFETY: `realloc` delegates to `ReallocFunc::call`, which guarantees that 180 160 // - memory remains valid until it is explicitly freed, 181 161 // - passing a pointer to a valid memory allocation is OK, ··· 186 240 unsafe { ReallocFunc::KVREALLOC.call(ptr, layout, old_layout, flags) } 187 241 } 188 242 } 189 - 190 - #[global_allocator] 191 - static ALLOCATOR: Kmalloc = Kmalloc; 192 - 193 - // See <https://github.com/rust-lang/rust/pull/86844>. 194 - #[no_mangle] 195 - static __rust_no_alloc_shim_is_unstable: u8 = 0;
+2 -2
scripts/Makefile.build
··· 248 248 # Compile Rust sources (.rs) 249 249 # --------------------------------------------------------------------------- 250 250 251 - rust_allowed_features := arbitrary_self_types,lint_reasons,new_uninit 251 + rust_allowed_features := arbitrary_self_types,lint_reasons 252 252 253 253 # `--out-dir` is required to avoid temporaries being created by `rustc` in the 254 254 # current working directory, which may be not accessible in the out-of-tree ··· 258 258 -Zallow-features=$(rust_allowed_features) \ 259 259 -Zcrate-attr=no_std \ 260 260 -Zcrate-attr='feature($(rust_allowed_features))' \ 261 - -Zunstable-options --extern force:alloc --extern kernel \ 261 + -Zunstable-options --extern kernel \ 262 262 --crate-type rlib -L $(objtree)/rust/ \ 263 263 --crate-name $(basename $(notdir $@)) \ 264 264 --sysroot=/dev/null \
+2 -9
scripts/generate_rust_analyzer.py
··· 65 65 ) 66 66 67 67 append_crate( 68 - "alloc", 69 - sysroot_src / "alloc" / "src" / "lib.rs", 70 - ["core", "compiler_builtins"], 71 - cfg=crates_cfgs.get("alloc", []), 72 - ) 73 - 74 - append_crate( 75 68 "macros", 76 69 srctree / "rust" / "macros" / "lib.rs", 77 70 [], ··· 89 96 append_crate( 90 97 "kernel", 91 98 srctree / "rust" / "kernel" / "lib.rs", 92 - ["core", "alloc", "macros", "build_error", "bindings"], 99 + ["core", "macros", "build_error", "bindings"], 93 100 cfg=cfg, 94 101 ) 95 102 crates[-1]["source"] = { ··· 126 133 append_crate( 127 134 name, 128 135 path, 129 - ["core", "alloc", "kernel"], 136 + ["core", "kernel"], 130 137 cfg=cfg, 131 138 ) 132 139