Skip to content

Commit a031fe8

Browse files
committed
Pull rust updates from Miguel Ojeda: "In terms of lines, most changes this time are on the pinned-init API and infrastructure. While we have a Rust version upgrade, and thus a bunch of changes from the vendored 'alloc' crate as usual, this time those do not account for many lines. Toolchain and infrastructure: - Upgrade to Rust 1.71.1. This is the second such upgrade, which is a smaller jump compared to the last time. This version allows us to remove the '__rust_*' allocator functions -- the compiler now generates them as expected, thus now our 'KernelAllocator' is used. It also introduces the 'offset_of!' macro in the standard library (as an unstable feature) which we will need soon. So far, we were using a declarative macro as a prerequisite in some not-yet-landed patch series, which did not support sub-fields (i.e. nested structs): #[repr(C)] struct S { a: u16, b: (u8, u8), } assert_eq!(offset_of!(S, b.1), 3); - Upgrade to bindgen 0.65.1. This is the first time we upgrade its version. Given it is a fairly big jump, it comes with a fair number of improvements/changes that affect us, such as a fix needed to support LLVM 16 as well as proper support for '__noreturn' C functions, which are now mapped to return the '!' type in Rust: void __noreturn f(void); // C pub fn f() -> !; // Rust - 'scripts/rust_is_available.sh' improvements and fixes. This series takes care of all the issues known so far and adds a few new checks to cover for even more cases, plus adds some more help texts. All this together will hopefully make problematic setups easier to identify and to be solved by users building the kernel. In addition, it adds a test suite which covers all branches of the shell script, as well as tests for the issues found so far. - Support rust-analyzer for out-of-tree modules too. - Give 'cfg's to rust-analyzer for the 'core' and 'alloc' crates. - Drop 'scripts/is_rust_module.sh' since it is not needed anymore. Macros crate: - New 'paste!' proc macro. This macro is a more flexible version of 'concat_idents!': it allows the resulting identifier to be used to declare new items and it allows to transform the identifiers before concatenating them, e.g. let x_1 = 42; paste!(let [<x _2>] = [<x _1>];); assert!(x_1 == x_2); The macro is then used for several of the pinned-init API changes in this pull. Pinned-init API: - Make '#[pin_data]' compatible with conditional compilation of fields, allowing to write code like: #[pin_data] pub struct Foo { #[cfg(CONFIG_BAR)] a: Bar, #[cfg(not(CONFIG_BAR))] a: Baz, } - New '#[derive(Zeroable)]' proc macro for the 'Zeroable' trait, which allows 'unsafe' implementations for structs where every field implements the 'Zeroable' trait, e.g.: #[derive(Zeroable)] pub struct DriverData { id: i64, buf_ptr: *mut u8, len: usize, } - Add '..Zeroable::zeroed()' syntax to the 'pin_init!' macro for zeroing all other fields, e.g.: pin_init!(Buf { buf: [1; 64], ..Zeroable::zeroed() }); - New '{,pin_}init_array_from_fn()' functions to create array initializers given a generator function, e.g.: let b: Box<[usize; 1_000]> = Box::init::<Error>( init_array_from_fn(|i| i) ).unwrap(); assert_eq!(b.len(), 1_000); assert_eq!(b[123], 123); - New '{,pin_}chain' methods for '{,Pin}Init<T, E>' that allow to execute a closure on the value directly after initialization, e.g.: let foo = init!(Foo { buf <- init::zeroed() }).chain(|foo| { foo.setup(); Ok(()) }); - Support arbitrary paths in init macros, instead of just identifiers and generic types. - Implement the 'Zeroable' trait for the 'UnsafeCell<T>' and 'Opaque<T>' types. - Make initializer values inaccessible after initialization. - Make guards in the init macros hygienic. 'allocator' module: - Use 'krealloc_aligned()' in 'KernelAllocator::alloc' preventing misaligned allocations when the Rust 1.71.1 upgrade is applied later in this pull. The equivalent fix for the previous compiler version (where 'KernelAllocator' is not yet used) was merged into 6.5 already, which added the 'krealloc_aligned()' function used here. - Implement 'KernelAllocator::{realloc, alloc_zeroed}' for performance, using 'krealloc_aligned()' too, which forwards the call to the C API. 'types' module: - Make 'Opaque' be '!Unpin', removing the need to add a 'PhantomPinned' field to Rust structs that contain C structs which must not be moved. - Make 'Opaque' use 'UnsafeCell' as the outer type, rather than inner. Documentation: - Suggest obtaining the source code of the Rust's 'core' library using the tarball instead of the repository. MAINTAINERS: - Andreas and Alice, from Samsung and Google respectively, are joining as reviewers of the "RUST" entry. As well as a few other minor changes and cleanups" * tag 'rust-6.6' of https://github.com/Rust-for-Linux/linux: (42 commits) rust: init: update expanded macro explanation rust: init: add `{pin_}chain` functions to `{Pin}Init<T, E>` rust: init: make `PinInit<T, E>` a supertrait of `Init<T, E>` rust: init: implement `Zeroable` for `UnsafeCell<T>` and `Opaque<T>` rust: init: add support for arbitrary paths in init macros rust: init: add functions to create array initializers rust: init: add `..Zeroable::zeroed()` syntax for zeroing all missing fields rust: init: make initializer values inaccessible after initializing rust: init: wrap type checking struct initializers in a closure rust: init: make guards in the init macros hygienic rust: add derive macro for `Zeroable` rust: init: make `#[pin_data]` compatible with conditional compilation of fields rust: init: consolidate init macros docs: rust: clarify what 'rustup override' does docs: rust: update instructions for obtaining 'core' source docs: rust: add command line to rust-analyzer section scripts: generate_rust_analyzer: provide `cfg`s for `core` and `alloc` rust: bindgen: upgrade to 0.65.1 rust: enable `no_mangle_with_rust_abi` Clippy lint rust: upgrade to Rust 1.71.1 ...
2 parents f2586d9 + 4af84c6 commit a031fe8

35 files changed

+1914
-849
lines changed

Documentation/process/changes.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ you probably needn't concern yourself with pcmciautils.
3131
====================== =============== ========================================
3232
GNU C 5.1 gcc --version
3333
Clang/LLVM (optional) 11.0.0 clang --version
34-
Rust (optional) 1.68.2 rustc --version
35-
bindgen (optional) 0.56.0 bindgen --version
34+
Rust (optional) 1.71.1 rustc --version
35+
bindgen (optional) 0.65.1 bindgen --version
3636
GNU make 3.82 make --version
3737
bash 4.2 bash --version
3838
binutils 2.25 ld -v

Documentation/rust/quick-start.rst

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ and run::
3838

3939
rustup override set $(scripts/min-tool-version.sh rustc)
4040

41-
Otherwise, fetch a standalone installer from:
41+
This will configure your working directory to use the correct version of
42+
``rustc`` without affecting your default toolchain. If you are not using
43+
``rustup``, fetch a standalone installer from:
4244

4345
https://forge.rust-lang.org/infra/other-installation-methods.html#standalone
4446

@@ -56,16 +58,17 @@ If ``rustup`` is being used, run::
5658
The components are installed per toolchain, thus upgrading the Rust compiler
5759
version later on requires re-adding the component.
5860

59-
Otherwise, if a standalone installer is used, the Rust repository may be cloned
60-
into the installation folder of the toolchain::
61+
Otherwise, if a standalone installer is used, the Rust source tree may be
62+
downloaded into the toolchain's installation folder::
6163

62-
git clone --recurse-submodules \
63-
--branch $(scripts/min-tool-version.sh rustc) \
64-
https://github.com/rust-lang/rust \
65-
$(rustc --print sysroot)/lib/rustlib/src/rust
64+
curl -L "https://static.rust-lang.org/dist/rust-src-$(scripts/min-tool-version.sh rustc).tar.gz" |
65+
tar -xzf - -C "$(rustc --print sysroot)/lib" \
66+
"rust-src-$(scripts/min-tool-version.sh rustc)/rust-src/lib/" \
67+
--strip-components=3
6668

6769
In this case, upgrading the Rust compiler version later on requires manually
68-
updating this clone.
70+
updating the source tree (this can be done by removing ``$(rustc --print
71+
sysroot)/lib/rustlib/src/rust`` then rerunning the above command).
6972

7073

7174
libclang
@@ -98,7 +101,24 @@ the ``bindgen`` tool. A particular version is required.
98101

99102
Install it via (note that this will download and build the tool from source)::
100103

101-
cargo install --locked --version $(scripts/min-tool-version.sh bindgen) bindgen
104+
cargo install --locked --version $(scripts/min-tool-version.sh bindgen) bindgen-cli
105+
106+
``bindgen`` needs to find a suitable ``libclang`` in order to work. If it is
107+
not found (or a different ``libclang`` than the one found should be used),
108+
the process can be tweaked using the environment variables understood by
109+
``clang-sys`` (the Rust bindings crate that ``bindgen`` uses to access
110+
``libclang``):
111+
112+
* ``LLVM_CONFIG_PATH`` can be pointed to an ``llvm-config`` executable.
113+
114+
* Or ``LIBCLANG_PATH`` can be pointed to a ``libclang`` shared library
115+
or to the directory containing it.
116+
117+
* Or ``CLANG_PATH`` can be pointed to a ``clang`` executable.
118+
119+
For details, please see ``clang-sys``'s documentation at:
120+
121+
https://github.com/KyleMayes/clang-sys#environment-variables
102122

103123

104124
Requirements: Developing
@@ -179,7 +199,9 @@ be used with many editors to enable syntax highlighting, completion, go to
179199
definition, and other features.
180200

181201
``rust-analyzer`` needs a configuration file, ``rust-project.json``, which
182-
can be generated by the ``rust-analyzer`` Make target.
202+
can be generated by the ``rust-analyzer`` Make target::
203+
204+
make LLVM=1 rust-analyzer
183205

184206

185207
Configuration

MAINTAINERS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18577,6 +18577,8 @@ R: Boqun Feng <[email protected]>
1857718577
R: Gary Guo <[email protected]>
1857818578
R: Björn Roy Baron <[email protected]>
1857918579
R: Benno Lossin <[email protected]>
18580+
R: Andreas Hindborg <[email protected]>
18581+
R: Alice Ryhl <[email protected]>
1858018582
1858118583
S: Supported
1858218584
W: https://github.com/Rust-for-Linux/linux

Makefile

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,7 @@ export rust_common_flags := --edition=2021 \
467467
-Dclippy::let_unit_value -Dclippy::mut_mut \
468468
-Dclippy::needless_bitwise_bool \
469469
-Dclippy::needless_continue \
470+
-Dclippy::no_mangle_with_rust_abi \
470471
-Wclippy::dbg_macro
471472

472473
KBUILD_HOSTCFLAGS := $(KBUILD_USERHOSTCFLAGS) $(HOST_LFS_CFLAGS) $(HOSTCFLAGS)
@@ -1289,7 +1290,7 @@ prepare0: archprepare
12891290
# All the preparing..
12901291
prepare: prepare0
12911292
ifdef CONFIG_RUST
1292-
$(Q)$(CONFIG_SHELL) $(srctree)/scripts/rust_is_available.sh -v
1293+
$(Q)$(CONFIG_SHELL) $(srctree)/scripts/rust_is_available.sh
12931294
$(Q)$(MAKE) $(build)=rust
12941295
endif
12951296

@@ -1825,7 +1826,7 @@ $(DOC_TARGETS):
18251826
# "Is Rust available?" target
18261827
PHONY += rustavailable
18271828
rustavailable:
1828-
$(Q)$(CONFIG_SHELL) $(srctree)/scripts/rust_is_available.sh -v && echo "Rust is available!"
1829+
$(Q)$(CONFIG_SHELL) $(srctree)/scripts/rust_is_available.sh && echo "Rust is available!"
18291830

18301831
# Documentation target
18311832
#
@@ -1859,11 +1860,6 @@ rustfmt:
18591860
rustfmtcheck: rustfmt_flags = --check
18601861
rustfmtcheck: rustfmt
18611862

1862-
# IDE support targets
1863-
PHONY += rust-analyzer
1864-
rust-analyzer:
1865-
$(Q)$(MAKE) $(build)=rust $@
1866-
18671863
# Misc
18681864
# ---------------------------------------------------------------------------
18691865

@@ -1924,6 +1920,7 @@ help:
19241920
@echo ' modules - default target, build the module(s)'
19251921
@echo ' modules_install - install the module'
19261922
@echo ' clean - remove generated files in module directory only'
1923+
@echo ' rust-analyzer - generate rust-project.json rust-analyzer support file'
19271924
@echo ''
19281925

19291926
__external_modules_error:
@@ -2065,6 +2062,11 @@ quiet_cmd_tags = GEN $@
20652062
tags TAGS cscope gtags: FORCE
20662063
$(call cmd,tags)
20672064

2065+
# IDE support targets
2066+
PHONY += rust-analyzer
2067+
rust-analyzer:
2068+
$(Q)$(MAKE) $(build)=rust $@
2069+
20682070
# Script to generate missing namespace dependencies
20692071
# ---------------------------------------------------------------------------
20702072

rust/Makefile

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ quiet_cmd_bindgen = BINDGEN $@
329329
$(BINDGEN) $< $(bindgen_target_flags) \
330330
--use-core --with-derive-default --ctypes-prefix core::ffi --no-layout-tests \
331331
--no-debug '.*' \
332-
--size_t-is-usize -o $@ -- $(bindgen_c_flags_final) -DMODULE \
332+
-o $@ -- $(bindgen_c_flags_final) -DMODULE \
333333
$(bindgen_target_cflags) $(bindgen_target_extra)
334334

335335
$(obj)/bindings/bindings_generated.rs: private bindgen_target_flags = \
@@ -349,8 +349,8 @@ $(obj)/uapi/uapi_generated.rs: $(src)/uapi/uapi_helper.h \
349349
# given it is `libclang`; but for consistency, future Clang changes and/or
350350
# a potential future GCC backend for `bindgen`, we disable it too.
351351
$(obj)/bindings/bindings_helpers_generated.rs: private bindgen_target_flags = \
352-
--blacklist-type '.*' --whitelist-var '' \
353-
--whitelist-function 'rust_helper_.*'
352+
--blocklist-type '.*' --allowlist-var '' \
353+
--allowlist-function 'rust_helper_.*'
354354
$(obj)/bindings/bindings_helpers_generated.rs: private bindgen_target_cflags = \
355355
-I$(objtree)/$(obj) -Wno-missing-prototypes -Wno-missing-declarations
356356
$(obj)/bindings/bindings_helpers_generated.rs: private bindgen_target_extra = ; \
@@ -402,12 +402,15 @@ quiet_cmd_rustc_library = $(if $(skip_clippy),RUSTC,$(RUSTC_OR_CLIPPY_QUIET)) L
402402
$(if $(rustc_objcopy),;$(OBJCOPY) $(rustc_objcopy) $@)
403403

404404
rust-analyzer:
405-
$(Q)$(srctree)/scripts/generate_rust_analyzer.py $(srctree) $(objtree) \
406-
$(RUST_LIB_SRC) > $(objtree)/rust-project.json
405+
$(Q)$(srctree)/scripts/generate_rust_analyzer.py \
406+
--cfgs='core=$(core-cfgs)' --cfgs='alloc=$(alloc-cfgs)' \
407+
$(abs_srctree) $(abs_objtree) \
408+
$(RUST_LIB_SRC) $(KBUILD_EXTMOD) > \
409+
$(if $(KBUILD_EXTMOD),$(extmod_prefix),$(objtree))/rust-project.json
407410

408411
redirect-intrinsics = \
409-
__eqsf2 __gesf2 __lesf2 __nesf2 __unordsf2 \
410-
__unorddf2 \
412+
__addsf3 __eqsf2 __gesf2 __lesf2 __ltsf2 __mulsf3 __nesf2 __unordsf2 \
413+
__adddf3 __ledf2 __ltdf2 __muldf3 __unorddf2 \
411414
__muloti4 __multi3 \
412415
__udivmodti4 __udivti3 __umodti3
413416

rust/alloc/alloc.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ use core::ptr::{self, NonNull};
1616
#[doc(inline)]
1717
pub use core::alloc::*;
1818

19-
use core::marker::Destruct;
20-
2119
#[cfg(test)]
2220
mod tests;
2321

@@ -41,6 +39,9 @@ extern "Rust" {
4139
#[rustc_allocator_zeroed]
4240
#[rustc_nounwind]
4341
fn __rust_alloc_zeroed(size: usize, align: usize) -> *mut u8;
42+
43+
#[cfg(not(bootstrap))]
44+
static __rust_no_alloc_shim_is_unstable: u8;
4445
}
4546

4647
/// The global memory allocator.
@@ -94,7 +95,14 @@ pub use std::alloc::Global;
9495
#[must_use = "losing the pointer will leak memory"]
9596
#[inline]
9697
pub unsafe fn alloc(layout: Layout) -> *mut u8 {
97-
unsafe { __rust_alloc(layout.size(), layout.align()) }
98+
unsafe {
99+
// Make sure we don't accidentally allow omitting the allocator shim in
100+
// stable code until it is actually stabilized.
101+
#[cfg(not(bootstrap))]
102+
core::ptr::read_volatile(&__rust_no_alloc_shim_is_unstable);
103+
104+
__rust_alloc(layout.size(), layout.align())
105+
}
98106
}
99107

100108
/// Deallocate memory with the global allocator.
@@ -333,16 +341,12 @@ unsafe fn exchange_malloc(size: usize, align: usize) -> *mut u8 {
333341

334342
#[cfg_attr(not(test), lang = "box_free")]
335343
#[inline]
336-
#[rustc_const_unstable(feature = "const_box", issue = "92521")]
337344
// This signature has to be the same as `Box`, otherwise an ICE will happen.
338345
// When an additional parameter to `Box` is added (like `A: Allocator`), this has to be added here as
339346
// well.
340347
// For example if `Box` is changed to `struct Box<T: ?Sized, A: Allocator>(Unique<T>, A)`,
341348
// this function has to be changed to `fn box_free<T: ?Sized, A: Allocator>(Unique<T>, A)` as well.
342-
pub(crate) const unsafe fn box_free<T: ?Sized, A: ~const Allocator + ~const Destruct>(
343-
ptr: Unique<T>,
344-
alloc: A,
345-
) {
349+
pub(crate) unsafe fn box_free<T: ?Sized, A: Allocator>(ptr: Unique<T>, alloc: A) {
346350
unsafe {
347351
let size = size_of_val(ptr.as_ref());
348352
let align = min_align_of_val(ptr.as_ref());

0 commit comments

Comments
 (0)