Skip to content

Commit b6964fe

Browse files
committed
Pull Rust updates from Miguel Ojeda: "Another routine one in terms of features. In terms of lines, this time the 'alloc' version upgrade is less prominent, given that it was fairly small (and we did not have two upgrades) Toolchain and infrastructure: - Upgrade to Rust 1.74.1 The patch release includes a fix for an ICE that the Apple AGX GPU driver was hitting - Support 'srctree'-relative links in Rust code documentation - Automate part of the manual constants handling (i.e. the ones not recognised by 'bindgen') - Suppress searching builtin sysroot to avoid confusion with installed sysroots, needed for the to-be-merged arm64 support which uses a builtin target - Ignore '__preserve_most' functions for 'bindgen' - Reduce header inclusion bloat in exports 'kernel' crate: - Implement 'Debug' for 'CString' - Make 'CondVar::wait()' an uninterruptible wait 'macros' crate: - Update 'paste!' to accept string literals - Improve '#[vtable]' documentation Documentation: - Add testing section (KUnit and 'rusttest' target) - Remove 'CC=clang' mentions - Clarify that 'rustup override' applies to build directory" * tag 'rust-6.8' of https://github.com/Rust-for-Linux/linux: docs: rust: Clarify that 'rustup override' applies to build directory docs: rust: Add rusttest info docs: rust: remove `CC=clang` mentions rust: support `srctree`-relative links rust: sync: Makes `CondVar::wait()` an uninterruptible wait rust: upgrade to Rust 1.74.1 rust: Suppress searching builtin sysroot rust: macros: improve `#[vtable]` documentation rust: macros: update 'paste!' macro to accept string literals rust: bindings: rename const binding using sed rust: Ignore preserve-most functions rust: replace <linux/module.h> with <linux/export.h> in rust/exports.c rust: kernel: str: Implement Debug for CString
2 parents 5bad490 + 711cbfc commit b6964fe

28 files changed

+274
-72
lines changed

Documentation/process/changes.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ 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.73.0 rustc --version
34+
Rust (optional) 1.74.1 rustc --version
3535
bindgen (optional) 0.65.1 bindgen --version
3636
GNU make 3.82 make --version
3737
bash 4.2 bash --version

Documentation/rust/coding-guidelines.rst

+13
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,19 @@ please take a look at the ``rustdoc`` book at:
177177

178178
https://doc.rust-lang.org/rustdoc/how-to-write-documentation.html
179179

180+
In addition, the kernel supports creating links relative to the source tree by
181+
prefixing the link destination with ``srctree/``. For instance:
182+
183+
.. code-block:: rust
184+
185+
//! C header: [`include/linux/printk.h`](srctree/include/linux/printk.h)
186+
187+
or:
188+
189+
.. code-block:: rust
190+
191+
/// [`struct mutex`]: srctree/include/linux/mutex.h
192+
180193
181194
Naming
182195
------

Documentation/rust/general-information.rst

+24
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,27 @@ configuration:
7777
#[cfg(CONFIG_X="y")] // Enabled as a built-in (`y`)
7878
#[cfg(CONFIG_X="m")] // Enabled as a module (`m`)
7979
#[cfg(not(CONFIG_X))] // Disabled
80+
81+
82+
Testing
83+
-------
84+
85+
There are the tests that come from the examples in the Rust documentation
86+
and get transformed into KUnit tests. These can be run via KUnit. For example
87+
via ``kunit_tool`` (``kunit.py``) on the command line::
88+
89+
./tools/testing/kunit/kunit.py run --make_options LLVM=1 --arch x86_64 --kconfig_add CONFIG_RUST=y
90+
91+
Alternatively, KUnit can run them as kernel built-in at boot. Refer to
92+
Documentation/dev-tools/kunit/index.rst for the general KUnit documentation
93+
and Documentation/dev-tools/kunit/architecture.rst for the details of kernel
94+
built-in vs. command line testing.
95+
96+
Additionally, there are the ``#[test]`` tests. These can be run using
97+
the ``rusttest`` Make target::
98+
99+
make LLVM=1 rusttest
100+
101+
This requires the kernel ``.config`` and downloads external repositories.
102+
It runs the ``#[test]`` tests on the host (currently) and thus is fairly
103+
limited in what these tests can test.

Documentation/rust/quick-start.rst

+9-9
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,18 @@ A particular version of the Rust compiler is required. Newer versions may or
3333
may not work because, for the moment, the kernel depends on some unstable
3434
Rust features.
3535

36-
If ``rustup`` is being used, enter the checked out source code directory
37-
and run::
36+
If ``rustup`` is being used, enter the kernel build directory (or use
37+
``--path=<build-dir>`` argument to the ``set`` sub-command) and run::
3838

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

4141
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:
42+
``rustc`` without affecting your default toolchain.
43+
44+
Note that the override applies to the current working directory (and its
45+
sub-directories).
46+
47+
If you are not using ``rustup``, fetch a standalone installer from:
4448

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

@@ -76,7 +80,7 @@ libclang
7680

7781
``libclang`` (part of LLVM) is used by ``bindgen`` to understand the C code
7882
in the kernel, which means LLVM needs to be installed; like when the kernel
79-
is compiled with ``CC=clang`` or ``LLVM=1``.
83+
is compiled with ``LLVM=1``.
8084

8185
Linux distributions are likely to have a suitable one available, so it is
8286
best to check that first.
@@ -229,10 +233,6 @@ at the moment. That is::
229233

230234
make LLVM=1
231235

232-
For architectures that do not support a full LLVM toolchain, use::
233-
234-
make CC=clang
235-
236236
Using GCC also works for some configurations, but it is very experimental at
237237
the moment.
238238

rust/Makefile

+7-1
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ quiet_cmd_rustdoc = RUSTDOC $(if $(rustdoc_host),H, ) $<
7878
$(rustc_target_flags) -L$(objtree)/$(obj) \
7979
--output $(rustdoc_output) \
8080
--crate-name $(subst rustdoc-,,$@) \
81+
$(if $(rustdoc_host),,--sysroot=/dev/null) \
8182
@$(objtree)/include/generated/rustc_cfg $<
8283

8384
# The `html_logo_url` and `html_favicon_url` forms of the `doc` attribute
@@ -98,7 +99,8 @@ rustdoc: rustdoc-core rustdoc-macros rustdoc-compiler_builtins \
9899
$(Q)find $(rustdoc_output) -name '*.html' -type f -print0 | xargs -0 sed -Ei \
99100
-e 's:rust-logo-[0-9a-f]+\.svg:logo.svg:g' \
100101
-e 's:favicon-[0-9a-f]+\.svg:logo.svg:g' \
101-
-e 's:<link rel="alternate icon" type="image/png" href="[/.]+/static\.files/favicon-(16x16|32x32)-[0-9a-f]+\.png">::g'
102+
-e 's:<link rel="alternate icon" type="image/png" href="[/.]+/static\.files/favicon-(16x16|32x32)-[0-9a-f]+\.png">::g' \
103+
-e 's:<a href="srctree/([^"]+)">:<a href="$(abs_srctree)/\1">:g'
102104
$(Q)for f in $(rustdoc_output)/static.files/rustdoc-*.css; do \
103105
echo ".logo-container > img { object-fit: contain; }" >> $$f; done
104106

@@ -178,6 +180,7 @@ quiet_cmd_rustdoc_test_kernel = RUSTDOC TK $<
178180
--extern build_error --extern macros \
179181
--extern bindings --extern uapi \
180182
--no-run --crate-name kernel -Zunstable-options \
183+
--sysroot=/dev/null \
181184
--test-builder $(objtree)/scripts/rustdoc_test_builder \
182185
$< $(rustdoc_test_kernel_quiet); \
183186
$(objtree)/scripts/rustdoc_test_gen
@@ -337,6 +340,8 @@ quiet_cmd_bindgen = BINDGEN $@
337340

338341
$(obj)/bindings/bindings_generated.rs: private bindgen_target_flags = \
339342
$(shell grep -Ev '^#|^$$' $(srctree)/$(src)/bindgen_parameters)
343+
$(obj)/bindings/bindings_generated.rs: private bindgen_target_extra = ; \
344+
sed -Ei 's/pub const RUST_CONST_HELPER_([a-zA-Z0-9_]*)/pub const \1/g' $@
340345
$(obj)/bindings/bindings_generated.rs: $(src)/bindings/bindings_helper.h \
341346
$(src)/bindgen_parameters FORCE
342347
$(call if_changed_dep,bindgen)
@@ -402,6 +407,7 @@ quiet_cmd_rustc_library = $(if $(skip_clippy),RUSTC,$(RUSTC_OR_CLIPPY_QUIET)) L
402407
--emit=metadata=$(dir $@)$(patsubst %.o,lib%.rmeta,$(notdir $@)) \
403408
--crate-type rlib -L$(objtree)/$(obj) \
404409
--crate-name $(patsubst %.o,%,$(notdir $@)) $< \
410+
--sysroot=/dev/null \
405411
$(if $(rustc_objcopy),;$(OBJCOPY) $(rustc_objcopy) $@)
406412

407413
rust-analyzer:

rust/alloc/alloc.rs

+23-9
Original file line numberDiff line numberDiff line change
@@ -345,18 +345,31 @@ extern "Rust" {
345345
fn __rust_alloc_error_handler(size: usize, align: usize) -> !;
346346
}
347347

348-
/// Abort on memory allocation error or failure.
348+
/// Signal a memory allocation error.
349349
///
350-
/// Callers of memory allocation APIs wishing to abort computation
350+
/// Callers of memory allocation APIs wishing to cease execution
351351
/// in response to an allocation error are encouraged to call this function,
352-
/// rather than directly invoking `panic!` or similar.
352+
/// rather than directly invoking [`panic!`] or similar.
353353
///
354-
/// The default behavior of this function is to print a message to standard error
355-
/// and abort the process.
356-
/// It can be replaced with [`set_alloc_error_hook`] and [`take_alloc_error_hook`].
354+
/// This function is guaranteed to diverge (not return normally with a value), but depending on
355+
/// global configuration, it may either panic (resulting in unwinding or aborting as per
356+
/// configuration for all panics), or abort the process (with no unwinding).
357+
///
358+
/// The default behavior is:
359+
///
360+
/// * If the binary links against `std` (typically the case), then
361+
/// print a message to standard error and abort the process.
362+
/// This behavior can be replaced with [`set_alloc_error_hook`] and [`take_alloc_error_hook`].
363+
/// Future versions of Rust may panic by default instead.
364+
///
365+
/// * If the binary does not link against `std` (all of its crates are marked
366+
/// [`#![no_std]`][no_std]), then call [`panic!`] with a message.
367+
/// [The panic handler] applies as to any panic.
357368
///
358369
/// [`set_alloc_error_hook`]: ../../std/alloc/fn.set_alloc_error_hook.html
359370
/// [`take_alloc_error_hook`]: ../../std/alloc/fn.take_alloc_error_hook.html
371+
/// [The panic handler]: https://doc.rust-lang.org/reference/runtime.html#the-panic_handler-attribute
372+
/// [no_std]: https://doc.rust-lang.org/reference/names/preludes.html#the-no_std-attribute
360373
#[stable(feature = "global_alloc", since = "1.28.0")]
361374
#[rustc_const_unstable(feature = "const_alloc_error", issue = "92523")]
362375
#[cfg(all(not(no_global_oom_handling), not(test)))]
@@ -397,9 +410,10 @@ pub mod __alloc_error_handler {
397410
if unsafe { __rust_alloc_error_handler_should_panic != 0 } {
398411
panic!("memory allocation of {size} bytes failed")
399412
} else {
400-
core::panicking::panic_nounwind_fmt(format_args!(
401-
"memory allocation of {size} bytes failed"
402-
))
413+
core::panicking::panic_nounwind_fmt(
414+
format_args!("memory allocation of {size} bytes failed"),
415+
/* force_no_backtrace */ false,
416+
)
403417
}
404418
}
405419
}

rust/alloc/lib.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,8 @@
9090
#![warn(missing_docs)]
9191
#![allow(explicit_outlives_requirements)]
9292
#![warn(multiple_supertrait_upcastable)]
93-
#![cfg_attr(not(bootstrap), allow(internal_features))]
94-
#![cfg_attr(not(bootstrap), allow(rustdoc::redundant_explicit_links))]
93+
#![allow(internal_features)]
94+
#![allow(rustdoc::redundant_explicit_links)]
9595
//
9696
// Library features:
9797
// tidy-alphabetical-start
@@ -122,6 +122,7 @@
122122
#![feature(const_waker)]
123123
#![feature(core_intrinsics)]
124124
#![feature(core_panic)]
125+
#![feature(deprecated_suggestion)]
125126
#![feature(dispatch_from_dyn)]
126127
#![feature(error_generic_member_access)]
127128
#![feature(error_in_core)]
@@ -145,7 +146,6 @@
145146
#![feature(ptr_metadata)]
146147
#![feature(ptr_sub_ptr)]
147148
#![feature(receiver_trait)]
148-
#![feature(saturating_int_impl)]
149149
#![feature(set_ptr_value)]
150150
#![feature(sized_type_properties)]
151151
#![feature(slice_from_ptr_range)]

rust/alloc/slice.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -594,7 +594,7 @@ impl<T> [T] {
594594
/// ```
595595
#[rustc_allow_incoherent_impl]
596596
#[stable(feature = "rust1", since = "1.0.0")]
597-
#[deprecated(since = "1.3.0", note = "renamed to join")]
597+
#[deprecated(since = "1.3.0", note = "renamed to join", suggestion = "join")]
598598
pub fn connect<Separator>(&self, sep: Separator) -> <Self as Join<Separator>>::Output
599599
where
600600
Self: Join<Separator>,

rust/alloc/vec/mod.rs

+85-2
Original file line numberDiff line numberDiff line change
@@ -1228,8 +1228,8 @@ impl<T, A: Allocator> Vec<T, A> {
12281228
/// Shortens the vector, keeping the first `len` elements and dropping
12291229
/// the rest.
12301230
///
1231-
/// If `len` is greater than the vector's current length, this has no
1232-
/// effect.
1231+
/// If `len` is greater or equal to the vector's current length, this has
1232+
/// no effect.
12331233
///
12341234
/// The [`drain`] method can emulate `truncate`, but causes the excess
12351235
/// elements to be returned instead of dropped.
@@ -1336,6 +1336,15 @@ impl<T, A: Allocator> Vec<T, A> {
13361336
/// is never written to (except inside an `UnsafeCell`) using this pointer or any pointer
13371337
/// derived from it. If you need to mutate the contents of the slice, use [`as_mut_ptr`].
13381338
///
1339+
/// This method guarantees that for the purpose of the aliasing model, this method
1340+
/// does not materialize a reference to the underlying slice, and thus the returned pointer
1341+
/// will remain valid when mixed with other calls to [`as_ptr`] and [`as_mut_ptr`].
1342+
/// Note that calling other methods that materialize mutable references to the slice,
1343+
/// or mutable references to specific elements you are planning on accessing through this pointer,
1344+
/// as well as writing to those elements, may still invalidate this pointer.
1345+
/// See the second example below for how this guarantee can be used.
1346+
///
1347+
///
13391348
/// # Examples
13401349
///
13411350
/// ```
@@ -1349,8 +1358,25 @@ impl<T, A: Allocator> Vec<T, A> {
13491358
/// }
13501359
/// ```
13511360
///
1361+
/// Due to the aliasing guarantee, the following code is legal:
1362+
///
1363+
/// ```rust
1364+
/// unsafe {
1365+
/// let mut v = vec![0, 1, 2];
1366+
/// let ptr1 = v.as_ptr();
1367+
/// let _ = ptr1.read();
1368+
/// let ptr2 = v.as_mut_ptr().offset(2);
1369+
/// ptr2.write(2);
1370+
/// // Notably, the write to `ptr2` did *not* invalidate `ptr1`
1371+
/// // because it mutated a different element:
1372+
/// let _ = ptr1.read();
1373+
/// }
1374+
/// ```
1375+
///
13521376
/// [`as_mut_ptr`]: Vec::as_mut_ptr
1377+
/// [`as_ptr`]: Vec::as_ptr
13531378
#[stable(feature = "vec_as_ptr", since = "1.37.0")]
1379+
#[cfg_attr(not(bootstrap), rustc_never_returns_null_ptr)]
13541380
#[inline]
13551381
pub fn as_ptr(&self) -> *const T {
13561382
// We shadow the slice method of the same name to avoid going through
@@ -1366,6 +1392,15 @@ impl<T, A: Allocator> Vec<T, A> {
13661392
/// Modifying the vector may cause its buffer to be reallocated,
13671393
/// which would also make any pointers to it invalid.
13681394
///
1395+
/// This method guarantees that for the purpose of the aliasing model, this method
1396+
/// does not materialize a reference to the underlying slice, and thus the returned pointer
1397+
/// will remain valid when mixed with other calls to [`as_ptr`] and [`as_mut_ptr`].
1398+
/// Note that calling other methods that materialize references to the slice,
1399+
/// or references to specific elements you are planning on accessing through this pointer,
1400+
/// may still invalidate this pointer.
1401+
/// See the second example below for how this guarantee can be used.
1402+
///
1403+
///
13691404
/// # Examples
13701405
///
13711406
/// ```
@@ -1383,7 +1418,25 @@ impl<T, A: Allocator> Vec<T, A> {
13831418
/// }
13841419
/// assert_eq!(&*x, &[0, 1, 2, 3]);
13851420
/// ```
1421+
///
1422+
/// Due to the aliasing guarantee, the following code is legal:
1423+
///
1424+
/// ```rust
1425+
/// unsafe {
1426+
/// let mut v = vec![0];
1427+
/// let ptr1 = v.as_mut_ptr();
1428+
/// ptr1.write(1);
1429+
/// let ptr2 = v.as_mut_ptr();
1430+
/// ptr2.write(2);
1431+
/// // Notably, the write to `ptr2` did *not* invalidate `ptr1`:
1432+
/// ptr1.write(3);
1433+
/// }
1434+
/// ```
1435+
///
1436+
/// [`as_mut_ptr`]: Vec::as_mut_ptr
1437+
/// [`as_ptr`]: Vec::as_ptr
13861438
#[stable(feature = "vec_as_ptr", since = "1.37.0")]
1439+
#[cfg_attr(not(bootstrap), rustc_never_returns_null_ptr)]
13871440
#[inline]
13881441
pub fn as_mut_ptr(&mut self) -> *mut T {
13891442
// We shadow the slice method of the same name to avoid going through
@@ -3403,6 +3456,36 @@ impl<T: Clone> From<&mut [T]> for Vec<T> {
34033456
}
34043457
}
34053458

3459+
#[cfg(not(no_global_oom_handling))]
3460+
#[stable(feature = "vec_from_array_ref", since = "1.74.0")]
3461+
impl<T: Clone, const N: usize> From<&[T; N]> for Vec<T> {
3462+
/// Allocate a `Vec<T>` and fill it by cloning `s`'s items.
3463+
///
3464+
/// # Examples
3465+
///
3466+
/// ```
3467+
/// assert_eq!(Vec::from(&[1, 2, 3]), vec![1, 2, 3]);
3468+
/// ```
3469+
fn from(s: &[T; N]) -> Vec<T> {
3470+
Self::from(s.as_slice())
3471+
}
3472+
}
3473+
3474+
#[cfg(not(no_global_oom_handling))]
3475+
#[stable(feature = "vec_from_array_ref", since = "1.74.0")]
3476+
impl<T: Clone, const N: usize> From<&mut [T; N]> for Vec<T> {
3477+
/// Allocate a `Vec<T>` and fill it by cloning `s`'s items.
3478+
///
3479+
/// # Examples
3480+
///
3481+
/// ```
3482+
/// assert_eq!(Vec::from(&mut [1, 2, 3]), vec![1, 2, 3]);
3483+
/// ```
3484+
fn from(s: &mut [T; N]) -> Vec<T> {
3485+
Self::from(s.as_mut_slice())
3486+
}
3487+
}
3488+
34063489
#[cfg(not(no_global_oom_handling))]
34073490
#[stable(feature = "vec_from_array", since = "1.44.0")]
34083491
impl<T, const N: usize> From<[T; N]> for Vec<T> {

rust/bindgen_parameters

+4
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,7 @@
2020

2121
# `seccomp`'s comment gets understood as a doctest
2222
--no-doc-comments
23+
24+
# These functions use the `__preserve_most` calling convention, which neither bindgen
25+
# nor Rust currently understand, and which Clang currently declares to be unstable.
26+
--blocklist-function __list_.*_report

rust/bindings/bindings_helper.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@
1818
#include <linux/workqueue.h>
1919

2020
/* `bindgen` gets confused at certain things. */
21-
const size_t BINDINGS_ARCH_SLAB_MINALIGN = ARCH_SLAB_MINALIGN;
22-
const gfp_t BINDINGS_GFP_KERNEL = GFP_KERNEL;
23-
const gfp_t BINDINGS___GFP_ZERO = __GFP_ZERO;
21+
const size_t RUST_CONST_HELPER_ARCH_SLAB_MINALIGN = ARCH_SLAB_MINALIGN;
22+
const gfp_t RUST_CONST_HELPER_GFP_KERNEL = GFP_KERNEL;
23+
const gfp_t RUST_CONST_HELPER___GFP_ZERO = __GFP_ZERO;

rust/bindings/lib.rs

-3
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,3 @@ mod bindings_helper {
4848
}
4949

5050
pub use bindings_raw::*;
51-
52-
pub const GFP_KERNEL: gfp_t = BINDINGS_GFP_KERNEL;
53-
pub const __GFP_ZERO: gfp_t = BINDINGS___GFP_ZERO;

rust/exports.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
* accidentally exposed.
1212
*/
1313

14-
#include <linux/module.h>
14+
#include <linux/export.h>
1515

1616
#define EXPORT_SYMBOL_RUST_GPL(sym) extern int sym; EXPORT_SYMBOL_GPL(sym)
1717

0 commit comments

Comments
 (0)