Skip to content

Commit 392e34b

Browse files
Danilo Krummrichojeda
Danilo Krummrich
authored andcommitted
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 <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Miguel Ojeda <[email protected]>
1 parent 8ae740c commit 392e34b

File tree

5 files changed

+16
-108
lines changed

5 files changed

+16
-108
lines changed

rust/Makefile

+10-33
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ always-$(CONFIG_RUST) += libmacros.so
1515
no-clean-files += libmacros.so
1616

1717
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 \
2020
exports_bindings_generated.h exports_kernel_generated.h
2121

2222
always-$(CONFIG_RUST) += uapi/uapi_generated.rs
@@ -53,11 +53,6 @@ endif
5353
core-cfgs = \
5454
--cfg no_fp_fmt_parse
5555

56-
alloc-cfgs = \
57-
--cfg no_global_oom_handling \
58-
--cfg no_rc \
59-
--cfg no_sync
60-
6156
quiet_cmd_rustdoc = RUSTDOC $(if $(rustdoc_host),H, ) $<
6257
cmd_rustdoc = \
6358
OBJTREE=$(abspath $(objtree)) \
@@ -81,7 +76,7 @@ quiet_cmd_rustdoc = RUSTDOC $(if $(rustdoc_host),H, ) $<
8176
# command-like flags to solve the issue. Meanwhile, we use the non-custom case
8277
# and then retouch the generated files.
8378
rustdoc: rustdoc-core rustdoc-macros rustdoc-compiler_builtins \
84-
rustdoc-alloc rustdoc-kernel
79+
rustdoc-kernel
8580
$(Q)cp $(srctree)/Documentation/images/logo.svg $(rustdoc_output)/static.files/
8681
$(Q)cp $(srctree)/Documentation/images/COPYING-logo $(rustdoc_output)/static.files/
8782
$(Q)find $(rustdoc_output) -name '*.html' -type f -print0 | xargs -0 sed -Ei \
@@ -108,20 +103,11 @@ rustdoc-core: $(RUST_LIB_SRC)/core/src/lib.rs FORCE
108103
rustdoc-compiler_builtins: $(src)/compiler_builtins.rs rustdoc-core FORCE
109104
+$(call if_changed,rustdoc)
110105

111-
# We need to allow `rustdoc::broken_intra_doc_links` because some
112-
# `no_global_oom_handling` functions refer to non-`no_global_oom_handling`
113-
# functions. Ideally `rustdoc` would have a way to distinguish broken links
114-
# due to things that are "configured out" vs. entirely non-existing ones.
115-
rustdoc-alloc: private rustc_target_flags = $(alloc-cfgs) \
116-
-Arustdoc::broken_intra_doc_links
117-
rustdoc-alloc: $(RUST_LIB_SRC)/alloc/src/lib.rs rustdoc-core rustdoc-compiler_builtins FORCE
118-
+$(call if_changed,rustdoc)
119-
120-
rustdoc-kernel: private rustc_target_flags = --extern alloc \
106+
rustdoc-kernel: private rustc_target_flags = \
121107
--extern build_error --extern macros=$(objtree)/$(obj)/libmacros.so \
122108
--extern bindings --extern uapi
123109
rustdoc-kernel: $(src)/kernel/lib.rs rustdoc-core rustdoc-macros \
124-
rustdoc-compiler_builtins rustdoc-alloc $(obj)/libmacros.so \
110+
rustdoc-compiler_builtins $(obj)/libmacros.so \
125111
$(obj)/bindings.o FORCE
126112
+$(call if_changed,rustdoc)
127113

@@ -165,7 +151,7 @@ quiet_cmd_rustdoc_test_kernel = RUSTDOC TK $<
165151
mkdir -p $(objtree)/$(obj)/test/doctests/kernel; \
166152
OBJTREE=$(abspath $(objtree)) \
167153
$(RUSTDOC) --test $(rust_flags) \
168-
-L$(objtree)/$(obj) --extern alloc --extern kernel \
154+
-L$(objtree)/$(obj) --extern kernel \
169155
--extern build_error --extern macros \
170156
--extern bindings --extern uapi \
171157
--no-run --crate-name kernel -Zunstable-options \
@@ -201,7 +187,7 @@ rusttest-macros: $(src)/macros/lib.rs FORCE
201187
+$(call if_changed,rustc_test)
202188
+$(call if_changed,rustdoc_test)
203189

204-
rusttest-kernel: private rustc_target_flags = --extern alloc \
190+
rusttest-kernel: private rustc_target_flags = \
205191
--extern build_error --extern macros --extern bindings --extern uapi
206192
rusttest-kernel: $(src)/kernel/lib.rs \
207193
rusttestlib-build_error rusttestlib-macros rusttestlib-bindings \
@@ -314,9 +300,6 @@ quiet_cmd_exports = EXPORTS $@
314300
$(obj)/exports_core_generated.h: $(obj)/core.o FORCE
315301
$(call if_changed,exports)
316302

317-
$(obj)/exports_alloc_generated.h: $(obj)/alloc.o FORCE
318-
$(call if_changed,exports)
319-
320303
# Even though Rust kernel modules should never use the bindings directly,
321304
# symbols from the `bindings` crate and the C helpers need to be exported
322305
# because Rust generics and inlined functions may not get their code generated
@@ -363,7 +346,7 @@ quiet_cmd_rustc_library = $(if $(skip_clippy),RUSTC,$(RUSTC_OR_CLIPPY_QUIET)) L
363346

364347
rust-analyzer:
365348
$(Q)$(srctree)/scripts/generate_rust_analyzer.py \
366-
--cfgs='core=$(core-cfgs)' --cfgs='alloc=$(alloc-cfgs)' \
349+
--cfgs='core=$(core-cfgs)' \
367350
$(realpath $(srctree)) $(realpath $(objtree)) \
368351
$(rustc_sysroot) $(RUST_LIB_SRC) $(KBUILD_EXTMOD) > \
369352
$(if $(KBUILD_EXTMOD),$(extmod_prefix),$(objtree))/rust-project.json
@@ -401,12 +384,6 @@ $(obj)/compiler_builtins.o: private rustc_objcopy = -w -W '__*'
401384
$(obj)/compiler_builtins.o: $(src)/compiler_builtins.rs $(obj)/core.o FORCE
402385
+$(call if_changed_rule,rustc_library)
403386

404-
$(obj)/alloc.o: private skip_clippy = 1
405-
$(obj)/alloc.o: private skip_flags = -Wunreachable_pub
406-
$(obj)/alloc.o: private rustc_target_flags = $(alloc-cfgs)
407-
$(obj)/alloc.o: $(RUST_LIB_SRC)/alloc/src/lib.rs $(obj)/compiler_builtins.o FORCE
408-
+$(call if_changed_rule,rustc_library)
409-
410387
$(obj)/build_error.o: $(src)/build_error.rs $(obj)/compiler_builtins.o FORCE
411388
+$(call if_changed_rule,rustc_library)
412389

@@ -421,9 +398,9 @@ $(obj)/uapi.o: $(src)/uapi/lib.rs \
421398
$(obj)/uapi/uapi_generated.rs FORCE
422399
+$(call if_changed_rule,rustc_library)
423400

424-
$(obj)/kernel.o: private rustc_target_flags = --extern alloc \
401+
$(obj)/kernel.o: private rustc_target_flags = \
425402
--extern build_error --extern macros --extern bindings --extern uapi
426-
$(obj)/kernel.o: $(src)/kernel/lib.rs $(obj)/alloc.o $(obj)/build_error.o \
403+
$(obj)/kernel.o: $(src)/kernel/lib.rs $(obj)/build_error.o \
427404
$(obj)/libmacros.so $(obj)/bindings.o $(obj)/uapi.o FORCE
428405
+$(call if_changed_rule,rustc_library)
429406

rust/exports.c

-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
#define EXPORT_SYMBOL_RUST_GPL(sym) extern int sym; EXPORT_SYMBOL_GPL(sym)
1717

1818
#include "exports_core_generated.h"
19-
#include "exports_alloc_generated.h"
2019
#include "exports_helpers_generated.h"
2120
#include "exports_bindings_generated.h"
2221
#include "exports_kernel_generated.h"

rust/kernel/alloc/allocator.rs

+2-63
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
//!
99
//! Reference: <https://docs.kernel.org/core-api/memory-allocation.html>
1010
11-
use super::{flags::*, Flags};
12-
use core::alloc::{GlobalAlloc, Layout};
11+
use super::Flags;
12+
use core::alloc::Layout;
1313
use core::ptr;
1414
use core::ptr::NonNull;
1515

@@ -54,23 +54,6 @@ fn aligned_size(new_layout: Layout) -> usize {
5454
layout.size()
5555
}
5656

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-
}
73-
7457
/// # Invariants
7558
///
7659
/// One of the following: `krealloc`, `vrealloc`, `kvrealloc`.
@@ -156,43 +139,6 @@ unsafe impl Allocator for Kmalloc {
156139
}
157140
}
158141

159-
// SAFETY: TODO.
160-
unsafe impl GlobalAlloc for Kmalloc {
161-
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
162-
// SAFETY: `ptr::null_mut()` is null and `layout` has a non-zero size by the function safety
163-
// requirement.
164-
unsafe { krealloc_aligned(ptr::null_mut(), layout, GFP_KERNEL) }
165-
}
166-
167-
unsafe fn dealloc(&self, ptr: *mut u8, _layout: Layout) {
168-
// SAFETY: TODO.
169-
unsafe {
170-
bindings::kfree(ptr as *const core::ffi::c_void);
171-
}
172-
}
173-
174-
unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
175-
// SAFETY:
176-
// - `new_size`, when rounded up to the nearest multiple of `layout.align()`, will not
177-
// overflow `isize` by the function safety requirement.
178-
// - `layout.align()` is a proper alignment (i.e. not zero and must be a power of two).
179-
let layout = unsafe { Layout::from_size_align_unchecked(new_size, layout.align()) };
180-
181-
// SAFETY:
182-
// - `ptr` is either null or a pointer allocated by this allocator by the function safety
183-
// requirement.
184-
// - the size of `layout` is not zero because `new_size` is not zero by the function safety
185-
// requirement.
186-
unsafe { krealloc_aligned(ptr, layout, GFP_KERNEL) }
187-
}
188-
189-
unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 {
190-
// SAFETY: `ptr::null_mut()` is null and `layout` has a non-zero size by the function safety
191-
// requirement.
192-
unsafe { krealloc_aligned(ptr::null_mut(), layout, GFP_KERNEL | __GFP_ZERO) }
193-
}
194-
}
195-
196142
// SAFETY: `realloc` delegates to `ReallocFunc::call`, which guarantees that
197143
// - memory remains valid until it is explicitly freed,
198144
// - passing a pointer to a valid memory allocation is OK,
@@ -240,10 +186,3 @@ unsafe impl Allocator for KVmalloc {
240186
unsafe { ReallocFunc::KVREALLOC.call(ptr, layout, old_layout, flags) }
241187
}
242188
}
243-
244-
#[global_allocator]
245-
static ALLOCATOR: Kmalloc = Kmalloc;
246-
247-
// See <https://github.com/rust-lang/rust/pull/86844>.
248-
#[no_mangle]
249-
static __rust_no_alloc_shim_is_unstable: u8 = 0;

scripts/Makefile.build

+2-2
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ $(obj)/%.lst: $(obj)/%.c FORCE
248248
# Compile Rust sources (.rs)
249249
# ---------------------------------------------------------------------------
250250

251-
rust_allowed_features := arbitrary_self_types,lint_reasons,new_uninit
251+
rust_allowed_features := arbitrary_self_types,lint_reasons
252252

253253
# `--out-dir` is required to avoid temporaries being created by `rustc` in the
254254
# current working directory, which may be not accessible in the out-of-tree
@@ -258,7 +258,7 @@ rust_common_cmd = \
258258
-Zallow-features=$(rust_allowed_features) \
259259
-Zcrate-attr=no_std \
260260
-Zcrate-attr='feature($(rust_allowed_features))' \
261-
-Zunstable-options --extern force:alloc --extern kernel \
261+
-Zunstable-options --extern kernel \
262262
--crate-type rlib -L $(objtree)/rust/ \
263263
--crate-name $(basename $(notdir $@)) \
264264
--sysroot=/dev/null \

scripts/generate_rust_analyzer.py

+2-9
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,6 @@ def append_crate(display_name, root_module, deps, cfg=[], is_workspace_member=Tr
6464
[],
6565
)
6666

67-
append_crate(
68-
"alloc",
69-
sysroot_src / "alloc" / "src" / "lib.rs",
70-
["core", "compiler_builtins"],
71-
cfg=crates_cfgs.get("alloc", []),
72-
)
73-
7467
append_crate(
7568
"macros",
7669
srctree / "rust" / "macros" / "lib.rs",
@@ -96,7 +89,7 @@ def append_crate(display_name, root_module, deps, cfg=[], is_workspace_member=Tr
9689
append_crate(
9790
"kernel",
9891
srctree / "rust" / "kernel" / "lib.rs",
99-
["core", "alloc", "macros", "build_error", "bindings"],
92+
["core", "macros", "build_error", "bindings"],
10093
cfg=cfg,
10194
)
10295
crates[-1]["source"] = {
@@ -133,7 +126,7 @@ def is_root_crate(build_file, target):
133126
append_crate(
134127
name,
135128
path,
136-
["core", "alloc", "kernel"],
129+
["core", "kernel"],
137130
cfg=cfg,
138131
)
139132

0 commit comments

Comments
 (0)