Skip to content

Commit 5701725

Browse files
committed
Merge tag 'rust-6.12' of https://github.com/Rust-for-Linux/linux
Pull Rust updates from Miguel Ojeda: "Toolchain and infrastructure: - Support 'MITIGATION_{RETHUNK,RETPOLINE,SLS}' (which cleans up objtool warnings), teach objtool about 'noreturn' Rust symbols and mimic '___ADDRESSABLE()' for 'module_{init,exit}'. With that, we should be objtool-warning-free, so enable it to run for all Rust object files. - KASAN (no 'SW_TAGS'), KCFI and shadow call sanitizer support. - Support 'RUSTC_VERSION', including re-config and re-build on change. - Split helpers file into several files in a folder, to avoid conflicts in it. Eventually those files will be moved to the right places with the new build system. In addition, remove the need to manually export the symbols defined there, reusing existing machinery for that. - Relax restriction on configurations with Rust + GCC plugins to just the RANDSTRUCT plugin. 'kernel' crate: - New 'list' module: doubly-linked linked list for use with reference counted values, which is heavily used by the upcoming Rust Binder. This includes 'ListArc' (a wrapper around 'Arc' that is guaranteed unique for the given ID), 'AtomicTracker' (tracks whether a 'ListArc' exists using an atomic), 'ListLinks' (the prev/next pointers for an item in a linked list), 'List' (the linked list itself), 'Iter' (an iterator over a 'List'), 'Cursor' (a cursor into a 'List' that allows to remove elements), 'ListArcField' (a field exclusively owned by a 'ListArc'), as well as support for heterogeneous lists. - New 'rbtree' module: red-black tree abstractions used by the upcoming Rust Binder. This includes 'RBTree' (the red-black tree itself), 'RBTreeNode' (a node), 'RBTreeNodeReservation' (a memory reservation for a node), 'Iter' and 'IterMut' (immutable and mutable iterators), 'Cursor' (bidirectional cursor that allows to remove elements), as well as an entry API similar to the Rust standard library one. - 'init' module: add 'write_[pin_]init' methods and the 'InPlaceWrite' trait. Add the 'assert_pinned!' macro. - 'sync' module: implement the 'InPlaceInit' trait for 'Arc' by introducing an associated type in the trait. - 'alloc' module: add 'drop_contents' method to 'BoxExt'. - 'types' module: implement the 'ForeignOwnable' trait for 'Pin<Box<T>>' and improve the trait's documentation. In addition, add the 'into_raw' method to the 'ARef' type. - 'error' module: in preparation for the upcoming Rust support for 32-bit architectures, like arm, locally allow Clippy lint for those. Documentation: - https://rust.docs.kernel.org has been announced, so link to it. - Enable rustdoc's "jump to definition" feature, making its output a bit closer to the experience in a cross-referencer. - Debian Testing now also provides recent Rust releases (outside of the freeze period), so add it to the list. MAINTAINERS: - Trevor is joining as reviewer of the "RUST" entry. And a few other small bits" * tag 'rust-6.12' of https://github.com/Rust-for-Linux/linux: (54 commits) kasan: rust: Add KASAN smoke test via UAF kbuild: rust: Enable KASAN support rust: kasan: Rust does not support KHWASAN kbuild: rust: Define probing macros for rustc kasan: simplify and clarify Makefile rust: cfi: add support for CFI_CLANG with Rust cfi: add CONFIG_CFI_ICALL_NORMALIZE_INTEGERS rust: support for shadow call stack sanitizer docs: rust: include other expressions in conditional compilation section kbuild: rust: replace proc macros dependency on `core.o` with the version text kbuild: rust: rebuild if the version text changes kbuild: rust: re-run Kconfig if the version text changes kbuild: rust: add `CONFIG_RUSTC_VERSION` rust: avoid `box_uninit_write` feature MAINTAINERS: add Trevor Gross as Rust reviewer rust: rbtree: add `RBTree::entry` rust: rbtree: add cursor rust: rbtree: add mutable iterator rust: rbtree: add iterator rust: rbtree: add red-black tree implementation backed by the C version ...
2 parents 684a64b + a2f1154 commit 5701725

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+3870
-394
lines changed

Documentation/rust/general-information.rst

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,26 @@ but not `std <https://doc.rust-lang.org/std/>`_. Crates for use in the
1515
kernel must opt into this behavior using the ``#![no_std]`` attribute.
1616

1717

18+
.. _rust_code_documentation:
19+
1820
Code documentation
1921
------------------
2022

2123
Rust kernel code is documented using ``rustdoc``, its built-in documentation
2224
generator.
2325

2426
The generated HTML docs include integrated search, linked items (e.g. types,
25-
functions, constants), source code, etc. They may be read at (TODO: link when
26-
in mainline and generated alongside the rest of the documentation):
27+
functions, constants), source code, etc. They may be read at:
28+
29+
https://rust.docs.kernel.org
30+
31+
For linux-next, please see:
32+
33+
https://rust.docs.kernel.org/next/
2734

28-
http://kernel.org/
35+
There are also tags for each main release, e.g.:
36+
37+
https://rust.docs.kernel.org/6.10/
2938

3039
The docs can also be easily generated and read locally. This is quite fast
3140
(same order as compiling the code itself) and no special tools or environment
@@ -75,7 +84,7 @@ should provide as-safe-as-possible abstractions as needed.
7584
.. code-block::
7685
7786
rust/bindings/
78-
(rust/helpers.c)
87+
(rust/helpers/)
7988
8089
include/ -----+ <-+
8190
| |
@@ -112,7 +121,7 @@ output files in the ``rust/bindings/`` directory.
112121

113122
For parts of the C header that ``bindgen`` does not auto generate, e.g. C
114123
``inline`` functions or non-trivial macros, it is acceptable to add a small
115-
wrapper function to ``rust/helpers.c`` to make it available for the Rust side as
124+
wrapper function to ``rust/helpers/`` to make it available for the Rust side as
116125
well.
117126

118127
Abstractions
@@ -142,3 +151,11 @@ configuration:
142151
#[cfg(CONFIG_X="y")] // Enabled as a built-in (`y`)
143152
#[cfg(CONFIG_X="m")] // Enabled as a module (`m`)
144153
#[cfg(not(CONFIG_X))] // Disabled
154+
155+
For other predicates that Rust's ``cfg`` does not support, e.g. expressions with
156+
numerical comparisons, one may define a new Kconfig symbol:
157+
158+
.. code-block:: kconfig
159+
160+
config RUSTC_VERSION_MIN_107900
161+
def_bool y if RUSTC_VERSION >= 107900

Documentation/rust/index.rst

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,27 @@ support is still in development/experimental, especially for certain kernel
2525
configurations.
2626

2727

28+
Code documentation
29+
------------------
30+
31+
Given a kernel configuration, the kernel may generate Rust code documentation,
32+
i.e. HTML rendered by the ``rustdoc`` tool.
33+
2834
.. only:: rustdoc and html
2935

30-
You can also browse `rustdoc documentation <rustdoc/kernel/index.html>`_.
36+
This kernel documentation was built with `Rust code documentation
37+
<rustdoc/kernel/index.html>`_.
3138

3239
.. only:: not rustdoc and html
3340

34-
This documentation does not include rustdoc generated information.
41+
This kernel documentation was not built with Rust code documentation.
42+
43+
A pregenerated version is provided at:
44+
45+
https://rust.docs.kernel.org
46+
47+
Please see the :ref:`Code documentation <rust_code_documentation>` section for
48+
more details.
3549

3650
.. toctree::
3751
:maxdepth: 1

Documentation/rust/quick-start.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ of the box, e.g.::
3939
Debian
4040
******
4141

42-
Debian Unstable (Sid), outside of the freeze period, provides recent Rust
43-
releases and thus it should generally work out of the box, e.g.::
42+
Debian Testing and Debian Unstable (Sid), outside of the freeze period, provide
43+
recent Rust releases and thus they should generally work out of the box, e.g.::
4444

4545
apt install rustc rust-src bindgen rustfmt rust-clippy
4646

MAINTAINERS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20144,6 +20144,7 @@ R: Björn Roy Baron <bjorn3_gh@protonmail.com>
2014420144
R: Benno Lossin <benno.lossin@proton.me>
2014520145
R: Andreas Hindborg <a.hindborg@kernel.org>
2014620146
R: Alice Ryhl <aliceryhl@google.com>
20147+
R: Trevor Gross <tmgross@umich.edu>
2014720148
L: rust-for-linux@vger.kernel.org
2014820149
S: Supported
2014920150
W: https://rust-for-linux.com

Makefile

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -645,9 +645,11 @@ endif
645645

646646
# The expansion should be delayed until arch/$(SRCARCH)/Makefile is included.
647647
# Some architectures define CROSS_COMPILE in arch/$(SRCARCH)/Makefile.
648-
# CC_VERSION_TEXT is referenced from Kconfig (so it needs export),
649-
# and from include/config/auto.conf.cmd to detect the compiler upgrade.
648+
# CC_VERSION_TEXT and RUSTC_VERSION_TEXT are referenced from Kconfig (so they
649+
# need export), and from include/config/auto.conf.cmd to detect the compiler
650+
# upgrade.
650651
CC_VERSION_TEXT = $(subst $(pound),,$(shell LC_ALL=C $(CC) --version 2>/dev/null | head -n 1))
652+
RUSTC_VERSION_TEXT = $(subst $(pound),,$(shell $(RUSTC) --version 2>/dev/null))
651653

652654
ifneq ($(findstring clang,$(CC_VERSION_TEXT)),)
653655
include $(srctree)/scripts/Makefile.clang
@@ -668,7 +670,7 @@ ifdef config-build
668670
# KBUILD_DEFCONFIG may point out an alternative default configuration
669671
# used for 'make defconfig'
670672
include $(srctree)/arch/$(SRCARCH)/Makefile
671-
export KBUILD_DEFCONFIG KBUILD_KCONFIG CC_VERSION_TEXT
673+
export KBUILD_DEFCONFIG KBUILD_KCONFIG CC_VERSION_TEXT RUSTC_VERSION_TEXT
672674

673675
config: outputmakefile scripts_basic FORCE
674676
$(Q)$(MAKE) $(build)=scripts/kconfig $@
@@ -924,6 +926,7 @@ ifdef CONFIG_SHADOW_CALL_STACK
924926
ifndef CONFIG_DYNAMIC_SCS
925927
CC_FLAGS_SCS := -fsanitize=shadow-call-stack
926928
KBUILD_CFLAGS += $(CC_FLAGS_SCS)
929+
KBUILD_RUSTFLAGS += -Zsanitizer=shadow-call-stack
927930
endif
928931
export CC_FLAGS_SCS
929932
endif
@@ -948,6 +951,16 @@ endif
948951

949952
ifdef CONFIG_CFI_CLANG
950953
CC_FLAGS_CFI := -fsanitize=kcfi
954+
ifdef CONFIG_CFI_ICALL_NORMALIZE_INTEGERS
955+
CC_FLAGS_CFI += -fsanitize-cfi-icall-experimental-normalize-integers
956+
endif
957+
ifdef CONFIG_RUST
958+
# Always pass -Zsanitizer-cfi-normalize-integers as CONFIG_RUST selects
959+
# CONFIG_CFI_ICALL_NORMALIZE_INTEGERS.
960+
RUSTC_FLAGS_CFI := -Zsanitizer=kcfi -Zsanitizer-cfi-normalize-integers
961+
KBUILD_RUSTFLAGS += $(RUSTC_FLAGS_CFI)
962+
export RUSTC_FLAGS_CFI
963+
endif
951964
KBUILD_CFLAGS += $(CC_FLAGS_CFI)
952965
export CC_FLAGS_CFI
953966
endif

arch/Kconfig

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -835,6 +835,22 @@ config CFI_CLANG
835835

836836
https://clang.llvm.org/docs/ControlFlowIntegrity.html
837837

838+
config CFI_ICALL_NORMALIZE_INTEGERS
839+
bool "Normalize CFI tags for integers"
840+
depends on CFI_CLANG
841+
depends on $(cc-option,-fsanitize=kcfi -fsanitize-cfi-icall-experimental-normalize-integers)
842+
help
843+
This option normalizes the CFI tags for integer types so that all
844+
integer types of the same size and signedness receive the same CFI
845+
tag.
846+
847+
The option is separate from CONFIG_RUST because it affects the ABI.
848+
When working with build systems that care about the ABI, it is
849+
convenient to be able to turn on this flag first, before Rust is
850+
turned on.
851+
852+
This option is necessary for using CFI with Rust. If unsure, say N.
853+
838854
config CFI_PERMISSIVE
839855
bool "Use CFI in permissive mode"
840856
depends on CFI_CLANG

arch/arm64/Kconfig

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ config ARM64
235235
select HAVE_FUNCTION_ARG_ACCESS_API
236236
select MMU_GATHER_RCU_TABLE_FREE
237237
select HAVE_RSEQ
238-
select HAVE_RUST if CPU_LITTLE_ENDIAN
238+
select HAVE_RUST if RUSTC_SUPPORTS_ARM64
239239
select HAVE_STACKPROTECTOR
240240
select HAVE_SYSCALL_TRACEPOINTS
241241
select HAVE_KPROBES
@@ -270,6 +270,18 @@ config ARM64
270270
help
271271
ARM 64-bit (AArch64) Linux support.
272272

273+
config RUSTC_SUPPORTS_ARM64
274+
def_bool y
275+
depends on CPU_LITTLE_ENDIAN
276+
# Shadow call stack is only supported on certain rustc versions.
277+
#
278+
# When using the UNWIND_PATCH_PAC_INTO_SCS option, rustc version 1.80+ is
279+
# required due to use of the -Zfixed-x18 flag.
280+
#
281+
# Otherwise, rustc version 1.82+ is required due to use of the
282+
# -Zsanitizer=shadow-call-stack flag.
283+
depends on !SHADOW_CALL_STACK || RUSTC_VERSION >= 108200 || RUSTC_VERSION >= 108000 && UNWIND_PATCH_PAC_INTO_SCS
284+
273285
config CLANG_SUPPORTS_DYNAMIC_FTRACE_WITH_ARGS
274286
def_bool CC_IS_CLANG
275287
# https://github.com/ClangBuiltLinux/linux/issues/1507

arch/arm64/Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,11 @@ KBUILD_AFLAGS += $(call cc-option,-mabi=lp64)
5757
ifneq ($(CONFIG_UNWIND_TABLES),y)
5858
KBUILD_CFLAGS += -fno-asynchronous-unwind-tables -fno-unwind-tables
5959
KBUILD_AFLAGS += -fno-asynchronous-unwind-tables -fno-unwind-tables
60+
KBUILD_RUSTFLAGS += -Cforce-unwind-tables=n
6061
else
6162
KBUILD_CFLAGS += -fasynchronous-unwind-tables
6263
KBUILD_AFLAGS += -fasynchronous-unwind-tables
64+
KBUILD_RUSTFLAGS += -Cforce-unwind-tables=y -Zuse-sync-unwind=n
6365
endif
6466

6567
ifeq ($(CONFIG_STACKPROTECTOR_PER_TASK),y)
@@ -114,6 +116,7 @@ endif
114116

115117
ifeq ($(CONFIG_SHADOW_CALL_STACK), y)
116118
KBUILD_CFLAGS += -ffixed-x18
119+
KBUILD_RUSTFLAGS += -Zfixed-x18
117120
endif
118121

119122
ifeq ($(CONFIG_CPU_BIG_ENDIAN), y)

arch/riscv/Kconfig

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ config RISCV
177177
select HAVE_REGS_AND_STACK_ACCESS_API
178178
select HAVE_RETHOOK if !XIP_KERNEL
179179
select HAVE_RSEQ
180-
select HAVE_RUST if 64BIT
180+
select HAVE_RUST if RUSTC_SUPPORTS_RISCV
181181
select HAVE_SAMPLE_FTRACE_DIRECT
182182
select HAVE_SAMPLE_FTRACE_DIRECT_MULTI
183183
select HAVE_STACKPROTECTOR
@@ -209,6 +209,13 @@ config RISCV
209209
select USER_STACKTRACE_SUPPORT
210210
select ZONE_DMA32 if 64BIT
211211

212+
config RUSTC_SUPPORTS_RISCV
213+
def_bool y
214+
depends on 64BIT
215+
# Shadow call stack requires rustc version 1.82+ due to use of the
216+
# -Zsanitizer=shadow-call-stack flag.
217+
depends on !SHADOW_CALL_STACK || RUSTC_VERSION >= 108200
218+
212219
config CLANG_SUPPORTS_DYNAMIC_FTRACE
213220
def_bool CC_IS_CLANG
214221
# https://github.com/ClangBuiltLinux/linux/issues/1817

arch/x86/Makefile

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,15 @@ RETPOLINE_CFLAGS += $(call cc-option,-mindirect-branch-cs-prefix)
2424

2525
ifdef CONFIG_MITIGATION_RETHUNK
2626
RETHUNK_CFLAGS := -mfunction-return=thunk-extern
27+
RETHUNK_RUSTFLAGS := -Zfunction-return=thunk-extern
2728
RETPOLINE_CFLAGS += $(RETHUNK_CFLAGS)
29+
RETPOLINE_RUSTFLAGS += $(RETHUNK_RUSTFLAGS)
2830
endif
2931

3032
export RETHUNK_CFLAGS
33+
export RETHUNK_RUSTFLAGS
3134
export RETPOLINE_CFLAGS
35+
export RETPOLINE_RUSTFLAGS
3236
export RETPOLINE_VDSO_CFLAGS
3337

3438
# For gcc stack alignment is specified with -mpreferred-stack-boundary,
@@ -218,9 +222,10 @@ KBUILD_CFLAGS += -fno-asynchronous-unwind-tables
218222
# Avoid indirect branches in kernel to deal with Spectre
219223
ifdef CONFIG_MITIGATION_RETPOLINE
220224
KBUILD_CFLAGS += $(RETPOLINE_CFLAGS)
225+
KBUILD_RUSTFLAGS += $(RETPOLINE_RUSTFLAGS)
221226
# Additionally, avoid generating expensive indirect jumps which
222227
# are subject to retpolines for small number of switch cases.
223-
# clang turns off jump table generation by default when under
228+
# LLVM turns off jump table generation by default when under
224229
# retpoline builds, however, gcc does not for x86. This has
225230
# only been fixed starting from gcc stable version 8.4.0 and
226231
# onwards, but not for older ones. See gcc bug #86952.
@@ -237,6 +242,10 @@ ifdef CONFIG_CALL_PADDING
237242
PADDING_CFLAGS := -fpatchable-function-entry=$(CONFIG_FUNCTION_PADDING_BYTES),$(CONFIG_FUNCTION_PADDING_BYTES)
238243
KBUILD_CFLAGS += $(PADDING_CFLAGS)
239244
export PADDING_CFLAGS
245+
246+
PADDING_RUSTFLAGS := -Zpatchable-function-entry=$(CONFIG_FUNCTION_PADDING_BYTES),$(CONFIG_FUNCTION_PADDING_BYTES)
247+
KBUILD_RUSTFLAGS += $(PADDING_RUSTFLAGS)
248+
export PADDING_RUSTFLAGS
240249
endif
241250

242251
KBUILD_LDFLAGS += -m elf_$(UTS_MACHINE)

0 commit comments

Comments
 (0)