Skip to content

Commit 0c55932

Browse files
committed
Merge tag 'rust-fixes-6.12' of https://github.com/Rust-for-Linux/linux
Pull Rust fixes from Miguel Ojeda: "Toolchain and infrastructure: - Fix/improve a couple 'depends on' on the newly added CFI/KASAN suppport to avoid build errors/warnings - Fix ARCH_SLAB_MINALIGN multiple definition error for RISC-V under !CONFIG_MMU - Clean upcoming (Rust 1.83.0) Clippy warnings 'kernel' crate: - 'sync' module: fix soundness issue by requiring 'T: Sync' for 'LockedBy::access'; and fix helpers build error under PREEMPT_RT - Fix trivial sorting issue ('rustfmtcheck') on the v6.12 Rust merge" * tag 'rust-fixes-6.12' of https://github.com/Rust-for-Linux/linux: rust: kunit: use C-string literals to clean warning cfi: encode cfi normalized integers + kasan/gcov bug in Kconfig rust: KASAN+RETHUNK requires rustc 1.83.0 rust: cfi: fix `patchable-function-entry` starting version rust: mutex: fix __mutex_init() usage in case of PREEMPT_RT rust: fix `ARCH_SLAB_MINALIGN` multiple definition error rust: sync: require `T: Sync` for `LockedBy::access` rust: kernel: sort Rust modules
2 parents 263a25d + 05cef2c commit 0c55932

File tree

7 files changed

+48
-10
lines changed

7 files changed

+48
-10
lines changed

arch/Kconfig

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -838,7 +838,7 @@ config CFI_CLANG
838838
config CFI_ICALL_NORMALIZE_INTEGERS
839839
bool "Normalize CFI tags for integers"
840840
depends on CFI_CLANG
841-
depends on $(cc-option,-fsanitize=kcfi -fsanitize-cfi-icall-experimental-normalize-integers)
841+
depends on HAVE_CFI_ICALL_NORMALIZE_INTEGERS
842842
help
843843
This option normalizes the CFI tags for integer types so that all
844844
integer types of the same size and signedness receive the same CFI
@@ -851,6 +851,22 @@ config CFI_ICALL_NORMALIZE_INTEGERS
851851

852852
This option is necessary for using CFI with Rust. If unsure, say N.
853853

854+
config HAVE_CFI_ICALL_NORMALIZE_INTEGERS
855+
def_bool !GCOV_KERNEL && !KASAN
856+
depends on CFI_CLANG
857+
depends on $(cc-option,-fsanitize=kcfi -fsanitize-cfi-icall-experimental-normalize-integers)
858+
help
859+
Is CFI_ICALL_NORMALIZE_INTEGERS supported with the set of compilers
860+
currently in use?
861+
862+
This option defaults to false if GCOV or KASAN is enabled, as there is
863+
an LLVM bug that makes normalized integers tags incompatible with
864+
KASAN and GCOV. Kconfig currently does not have the infrastructure to
865+
detect whether your rustc compiler contains the fix for this bug, so
866+
it is assumed that it doesn't. If your compiler has the fix, you can
867+
explicitly enable this option in your config file. The Kconfig logic
868+
needed to detect this will be added in a future kernel release.
869+
854870
config CFI_PERMISSIVE
855871
bool "Use CFI in permissive mode"
856872
depends on CFI_CLANG

init/Kconfig

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1946,10 +1946,11 @@ config RUST
19461946
depends on !GCC_PLUGIN_RANDSTRUCT
19471947
depends on !RANDSTRUCT
19481948
depends on !DEBUG_INFO_BTF || PAHOLE_HAS_LANG_EXCLUDE
1949-
depends on !CFI_CLANG || RUSTC_VERSION >= 107900 && $(cc-option,-fsanitize=kcfi -fsanitize-cfi-icall-experimental-normalize-integers)
1949+
depends on !CFI_CLANG || RUSTC_VERSION >= 107900 && HAVE_CFI_ICALL_NORMALIZE_INTEGERS
19501950
select CFI_ICALL_NORMALIZE_INTEGERS if CFI_CLANG
1951-
depends on !CALL_PADDING || RUSTC_VERSION >= 108000
1951+
depends on !CALL_PADDING || RUSTC_VERSION >= 108100
19521952
depends on !KASAN_SW_TAGS
1953+
depends on !(MITIGATION_RETHUNK && KASAN) || RUSTC_VERSION >= 108300
19531954
help
19541955
Enables Rust support in the kernel.
19551956

rust/bindgen_parameters

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,8 @@
2424
# These functions use the `__preserve_most` calling convention, which neither bindgen
2525
# nor Rust currently understand, and which Clang currently declares to be unstable.
2626
--blocklist-function __list_.*_report
27+
28+
# These constants are sometimes not recognized by bindgen depending on config.
29+
# We use const helpers to aid bindgen, to avoid conflicts when constants are
30+
# recognized, block generation of the non-helper constants.
31+
--blocklist-item ARCH_SLAB_MINALIGN

rust/helpers/mutex.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,9 @@ void rust_helper_mutex_lock(struct mutex *lock)
77
{
88
mutex_lock(lock);
99
}
10+
11+
void rust_helper___mutex_init(struct mutex *mutex, const char *name,
12+
struct lock_class_key *key)
13+
{
14+
__mutex_init(mutex, name, key);
15+
}

rust/kernel/kunit.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ pub fn err(args: fmt::Arguments<'_>) {
1818
#[cfg(CONFIG_PRINTK)]
1919
unsafe {
2020
bindings::_printk(
21-
b"\x013%pA\0".as_ptr() as _,
21+
c"\x013%pA".as_ptr() as _,
2222
&args as *const _ as *const c_void,
2323
);
2424
}
@@ -34,7 +34,7 @@ pub fn info(args: fmt::Arguments<'_>) {
3434
#[cfg(CONFIG_PRINTK)]
3535
unsafe {
3636
bindings::_printk(
37-
b"\x016%pA\0".as_ptr() as _,
37+
c"\x016%pA".as_ptr() as _,
3838
&args as *const _ as *const c_void,
3939
);
4040
}

rust/kernel/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ pub mod net;
4444
pub mod page;
4545
pub mod prelude;
4646
pub mod print;
47-
pub mod sizes;
4847
pub mod rbtree;
48+
pub mod sizes;
4949
mod static_assert;
5050
#[doc(hidden)]
5151
pub mod std_vendor;

rust/kernel/sync/locked_by.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,12 @@ pub struct LockedBy<T: ?Sized, U: ?Sized> {
8383
// SAFETY: `LockedBy` can be transferred across thread boundaries iff the data it protects can.
8484
unsafe impl<T: ?Sized + Send, U: ?Sized> Send for LockedBy<T, U> {}
8585

86-
// SAFETY: `LockedBy` serialises the interior mutability it provides, so it is `Sync` as long as the
87-
// data it protects is `Send`.
86+
// SAFETY: If `T` is not `Sync`, then parallel shared access to this `LockedBy` allows you to use
87+
// `access_mut` to hand out `&mut T` on one thread at the time. The requirement that `T: Send` is
88+
// sufficient to allow that.
89+
//
90+
// If `T` is `Sync`, then the `access` method also becomes available, which allows you to obtain
91+
// several `&T` from several threads at once. However, this is okay as `T` is `Sync`.
8892
unsafe impl<T: ?Sized + Send, U: ?Sized> Sync for LockedBy<T, U> {}
8993

9094
impl<T, U> LockedBy<T, U> {
@@ -118,7 +122,10 @@ impl<T: ?Sized, U> LockedBy<T, U> {
118122
///
119123
/// Panics if `owner` is different from the data protected by the lock used in
120124
/// [`new`](LockedBy::new).
121-
pub fn access<'a>(&'a self, owner: &'a U) -> &'a T {
125+
pub fn access<'a>(&'a self, owner: &'a U) -> &'a T
126+
where
127+
T: Sync,
128+
{
122129
build_assert!(
123130
size_of::<U>() > 0,
124131
"`U` cannot be a ZST because `owner` wouldn't be unique"
@@ -127,7 +134,10 @@ impl<T: ?Sized, U> LockedBy<T, U> {
127134
panic!("mismatched owners");
128135
}
129136

130-
// SAFETY: `owner` is evidence that the owner is locked.
137+
// SAFETY: `owner` is evidence that there are only shared references to the owner for the
138+
// duration of 'a, so it's not possible to use `Self::access_mut` to obtain a mutable
139+
// reference to the inner value that aliases with this shared reference. The type is `Sync`
140+
// so there are no other requirements.
131141
unsafe { &*self.data.get() }
132142
}
133143

0 commit comments

Comments
 (0)