Skip to content

Commit 5968a03

Browse files
Danilo Krummrichfbq
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] [boqun: Resolve conflicts with helper split and auto export]
1 parent 39b3263 commit 5968a03

File tree

5 files changed

+16
-107
lines changed

5 files changed

+16
-107
lines changed

rust/Makefile

Lines changed: 11 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ 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 \
20-
exports_bindings_generated.h exports_kernel_generated.h
18+
obj-$(CONFIG_RUST) += bindings.o kernel.o
19+
always-$(CONFIG_RUST) += exports_helpers_generated.h exports_bindings_generated.h \
20+
exports_kernel_generated.h
2121

2222
always-$(CONFIG_RUST) += uapi/uapi_generated.rs
2323
obj-$(CONFIG_RUST) += uapi.o
@@ -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 \
@@ -105,20 +100,11 @@ rustdoc-core: $(RUST_LIB_SRC)/core/src/lib.rs FORCE
105100
rustdoc-compiler_builtins: $(src)/compiler_builtins.rs rustdoc-core FORCE
106101
+$(call if_changed,rustdoc)
107102

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

@@ -162,7 +148,7 @@ quiet_cmd_rustdoc_test_kernel = RUSTDOC TK $<
162148
mkdir -p $(objtree)/$(obj)/test/doctests/kernel; \
163149
OBJTREE=$(abspath $(objtree)) \
164150
$(RUSTDOC) --test $(rust_flags) \
165-
-L$(objtree)/$(obj) --extern alloc --extern kernel \
151+
-L$(objtree)/$(obj) --extern kernel \
166152
--extern build_error --extern macros \
167153
--extern bindings --extern uapi \
168154
--no-run --crate-name kernel -Zunstable-options \
@@ -198,7 +184,7 @@ rusttest-macros: $(src)/macros/lib.rs FORCE
198184
+$(call if_changed,rustc_test)
199185
+$(call if_changed,rustdoc_test)
200186

201-
rusttest-kernel: private rustc_target_flags = --extern alloc \
187+
rusttest-kernel: private rustc_target_flags = \
202188
--extern build_error --extern macros --extern bindings --extern uapi
203189
rusttest-kernel: $(src)/kernel/lib.rs \
204190
rusttestlib-build_error rusttestlib-macros rusttestlib-bindings \
@@ -311,9 +297,6 @@ quiet_cmd_exports = EXPORTS $@
311297
$(obj)/exports_core_generated.h: $(obj)/core.o FORCE
312298
$(call if_changed,exports)
313299

314-
$(obj)/exports_alloc_generated.h: $(obj)/alloc.o FORCE
315-
$(call if_changed,exports)
316-
317300
# Even though Rust kernel modules should never use the bindings directly,
318301
# symbols from the `bindings` crate and the C helpers need to be exported
319302
# because Rust generics and inlined functions may not get their code generated
@@ -362,7 +345,7 @@ quiet_cmd_rustc_library = $(if $(skip_clippy),RUSTC,$(RUSTC_OR_CLIPPY_QUIET)) L
362345

363346
rust-analyzer:
364347
$(Q)$(srctree)/scripts/generate_rust_analyzer.py \
365-
--cfgs='core=$(core-cfgs)' --cfgs='alloc=$(alloc-cfgs)' \
348+
--cfgs='core=$(core-cfgs)' \
366349
$(realpath $(srctree)) $(realpath $(objtree)) \
367350
$(rustc_sysroot) $(RUST_LIB_SRC) $(KBUILD_EXTMOD) > \
368351
$(if $(KBUILD_EXTMOD),$(extmod_prefix),$(objtree))/rust-project.json
@@ -399,12 +382,6 @@ $(obj)/compiler_builtins.o: private rustc_objcopy = -w -W '__*'
399382
$(obj)/compiler_builtins.o: $(src)/compiler_builtins.rs $(obj)/core.o FORCE
400383
+$(call if_changed_rule,rustc_library)
401384

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

@@ -419,9 +396,9 @@ $(obj)/uapi.o: $(src)/uapi/lib.rs \
419396
$(obj)/uapi/uapi_generated.rs FORCE
420397
+$(call if_changed_rule,rustc_library)
421398

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

rust/exports.c

Lines changed: 0 additions & 1 deletion
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

Lines changed: 2 additions & 57 deletions
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`.
@@ -131,41 +114,6 @@ unsafe impl Allocator for Kmalloc {
131114
}
132115
}
133116

134-
unsafe impl GlobalAlloc for Kmalloc {
135-
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
136-
// SAFETY: `ptr::null_mut()` is null and `layout` has a non-zero size by the function safety
137-
// requirement.
138-
unsafe { krealloc_aligned(ptr::null_mut(), layout, GFP_KERNEL) }
139-
}
140-
141-
unsafe fn dealloc(&self, ptr: *mut u8, _layout: Layout) {
142-
unsafe {
143-
bindings::kfree(ptr as *const core::ffi::c_void);
144-
}
145-
}
146-
147-
unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
148-
// SAFETY:
149-
// - `new_size`, when rounded up to the nearest multiple of `layout.align()`, will not
150-
// overflow `isize` by the function safety requirement.
151-
// - `layout.align()` is a proper alignment (i.e. not zero and must be a power of two).
152-
let layout = unsafe { Layout::from_size_align_unchecked(new_size, layout.align()) };
153-
154-
// SAFETY:
155-
// - `ptr` is either null or a pointer allocated by this allocator by the function safety
156-
// requirement.
157-
// - the size of `layout` is not zero because `new_size` is not zero by the function safety
158-
// requirement.
159-
unsafe { krealloc_aligned(ptr, layout, GFP_KERNEL) }
160-
}
161-
162-
unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 {
163-
// SAFETY: `ptr::null_mut()` is null and `layout` has a non-zero size by the function safety
164-
// requirement.
165-
unsafe { krealloc_aligned(ptr::null_mut(), layout, GFP_KERNEL | __GFP_ZERO) }
166-
}
167-
}
168-
169117
unsafe impl Allocator for Vmalloc {
170118
#[inline]
171119
unsafe fn realloc(
@@ -204,9 +152,6 @@ unsafe impl Allocator for KVmalloc {
204152
}
205153
}
206154

207-
#[global_allocator]
208-
static ALLOCATOR: Kmalloc = Kmalloc;
209-
210155
// See <https://github.com/rust-lang/rust/pull/86844>.
211156
#[no_mangle]
212157
static __rust_no_alloc_shim_is_unstable: u8 = 0;

scripts/Makefile.build

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -262,18 +262,13 @@ $(obj)/%.lst: $(obj)/%.c FORCE
262262

263263
# Compile Rust sources (.rs)
264264
# ---------------------------------------------------------------------------
265-
266-
rust_allowed_features := new_uninit
267-
268265
# `--out-dir` is required to avoid temporaries being created by `rustc` in the
269266
# current working directory, which may be not accessible in the out-of-tree
270267
# modules case.
271268
rust_common_cmd = \
272269
RUST_MODFILE=$(modfile) $(RUSTC_OR_CLIPPY) $(rust_flags) \
273-
-Zallow-features=$(rust_allowed_features) \
274270
-Zcrate-attr=no_std \
275-
-Zcrate-attr='feature($(rust_allowed_features))' \
276-
-Zunstable-options --extern force:alloc --extern kernel \
271+
-Zunstable-options --extern kernel \
277272
--crate-type rlib -L $(objtree)/rust/ \
278273
--crate-name $(basename $(notdir $@)) \
279274
--sysroot=/dev/null \

scripts/generate_rust_analyzer.py

Lines changed: 2 additions & 9 deletions
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)