mirror of
https://github.com/linux-msm/laptops-kernel.git
synced 2026-08-13 14:19:53 -07:00
Merge tag 'rust-6.16' of git://git.kernel.org/pub/scm/linux/kernel/git/ojeda/linux
Pull Rust updates from Miguel Ojeda:
"Toolchain and infrastructure:
- KUnit '#[test]'s:
- Support KUnit-mapped 'assert!' macros.
The support that landed last cycle was very basic, and the
'assert!' macros panicked since they were the standard library
ones. Now, they are mapped to the KUnit ones in a similar way to
how is done for doctests, reusing the infrastructure there.
With this, a failing test like:
#[test]
fn my_first_test() {
assert_eq!(42, 43);
}
will report:
# my_first_test: ASSERTION FAILED at rust/kernel/lib.rs:251
Expected 42 == 43 to be true, but is false
# my_first_test.speed: normal
not ok 1 my_first_test
- Support tests with checked 'Result' return types.
The return value of test functions that return a 'Result' will
be checked, thus one can now easily catch errors when e.g. using
the '?' operator in tests.
With this, a failing test like:
#[test]
fn my_test() -> Result {
f()?;
Ok(())
}
will report:
# my_test: ASSERTION FAILED at rust/kernel/lib.rs:321
Expected is_test_result_ok(my_test()) to be true, but is false
# my_test.speed: normal
not ok 1 my_test
- Add 'kunit_tests' to the prelude.
- Clarify the remaining language unstable features in use.
- Compile 'core' with edition 2024 for Rust >= 1.87.
- Workaround 'bindgen' issue with forward references to 'enum' types.
- objtool: relax slice condition to cover more 'noreturn' functions.
- Use absolute paths in macros referencing 'core' and 'kernel'
crates.
- Skip '-mno-fdpic' flag for bindgen in GCC 32-bit arm builds.
- Clean some 'doc_markdown' lint hits -- we may enable it later on.
'kernel' crate:
- 'alloc' module:
- 'Box': support for type coercion, e.g. 'Box<T>' to 'Box<dyn U>'
if 'T' implements 'U'.
- 'Vec': implement new methods (prerequisites for nova-core and
binder): 'truncate', 'resize', 'clear', 'pop',
'push_within_capacity' (with new error type 'PushError'),
'drain_all', 'retain', 'remove' (with new error type
'RemoveError'), insert_within_capacity' (with new error type
'InsertError').
In addition, simplify 'push' using 'spare_capacity_mut', split
'set_len' into 'inc_len' and 'dec_len', add type invariant 'len
<= capacity' and simplify 'truncate' using 'dec_len'.
- 'time' module:
- Morph the Rust hrtimer subsystem into the Rust timekeeping
subsystem, covering delay, sleep, timekeeping, timers. This new
subsystem has all the relevant timekeeping C maintainers listed
in the entry.
- Replace 'Ktime' with 'Delta' and 'Instant' types to represent a
duration of time and a point in time.
- Temporarily add 'Ktime' to 'hrtimer' module to allow 'hrtimer'
to delay converting to 'Instant' and 'Delta'.
- 'xarray' module:
- Add a Rust abstraction for the 'xarray' data structure. This
abstraction allows Rust code to leverage the 'xarray' to store
types that implement 'ForeignOwnable'. This support is a
dependency for memory backing feature of the Rust null block
driver, which is waiting to be merged.
- Set up an entry in 'MAINTAINERS' for the XArray Rust support.
Patches will go to the new Rust XArray tree and then via the
Rust subsystem tree for now.
- Allow 'ForeignOwnable' to carry information about the pointed-to
type. This helps asserting alignment requirements for the
pointer passed to the foreign language.
- 'container_of!': retain pointer mut-ness and add a compile-time
check of the type of the first parameter ('$field_ptr').
- Support optional message in 'static_assert!'.
- Add C FFI types (e.g. 'c_int') to the prelude.
- 'str' module: simplify KUnit tests 'format!' macro, convert
'rusttest' tests into KUnit, take advantage of the '-> Result'
support in KUnit '#[test]'s.
- 'list' module: add examples for 'List', fix path of
'assert_pinned!' (so far unused macro rule).
- 'workqueue' module: remove 'HasWork::OFFSET'.
- 'page' module: add 'inline' attribute.
'macros' crate:
- 'module' macro: place 'cleanup_module()' in '.exit.text' section.
'pin-init' crate:
- Add 'Wrapper<T>' trait for creating pin-initializers for wrapper
structs with a structurally pinned value such as 'UnsafeCell<T>' or
'MaybeUninit<T>'.
- Add 'MaybeZeroable' derive macro to try to derive 'Zeroable', but
not error if not all fields implement it. This is needed to derive
'Zeroable' for all bindgen-generated structs.
- Add 'unsafe fn cast_[pin_]init()' functions to unsafely change the
initialized type of an initializer. These are utilized by the
'Wrapper<T>' implementations.
- Add support for visibility in 'Zeroable' derive macro.
- Add support for 'union's in 'Zeroable' derive macro.
- Upstream dev news: streamline CI, fix some bugs. Add new workflows
to check if the user-space version and the one in the kernel tree
have diverged. Use the issues tab [1] to track them, which should
help folks report and diagnose issues w.r.t. 'pin-init' better.
[1] https://github.com/rust-for-linux/pin-init/issues
Documentation:
- Testing: add docs on the new KUnit '#[test]' tests.
- Coding guidelines: explain that '///' vs. '//' applies to private
items too. Add section on C FFI types.
- Quick Start guide: update Ubuntu instructions and split them into
"25.04" and "24.04 LTS and older".
And a few other cleanups and improvements"
* tag 'rust-6.16' of git://git.kernel.org/pub/scm/linux/kernel/git/ojeda/linux: (78 commits)
rust: list: Fix typo `much` in arc.rs
rust: check type of `$ptr` in `container_of!`
rust: workqueue: remove HasWork::OFFSET
rust: retain pointer mut-ness in `container_of!`
Documentation: rust: testing: add docs on the new KUnit `#[test]` tests
Documentation: rust: rename `#[test]`s to "`rusttest` host tests"
rust: str: take advantage of the `-> Result` support in KUnit `#[test]`'s
rust: str: simplify KUnit tests `format!` macro
rust: str: convert `rusttest` tests into KUnit
rust: add `kunit_tests` to the prelude
rust: kunit: support checked `-> Result`s in KUnit `#[test]`s
rust: kunit: support KUnit-mapped `assert!` macros in `#[test]`s
rust: make section names plural
rust: list: fix path of `assert_pinned!`
rust: compile libcore with edition 2024 for 1.87+
rust: dma: add missing Markdown code span
rust: task: add missing Markdown code spans and intra-doc links
rust: pci: fix docs related to missing Markdown code spans
rust: alloc: add missing Markdown code span
rust: alloc: add missing Markdown code spans
...
This commit is contained in:
@@ -135,6 +135,7 @@ Ben Widawsky <bwidawsk@kernel.org> <benjamin.widawsky@intel.com>
|
||||
Benjamin Poirier <benjamin.poirier@gmail.com> <bpoirier@suse.de>
|
||||
Benjamin Tissoires <bentiss@kernel.org> <benjamin.tissoires@gmail.com>
|
||||
Benjamin Tissoires <bentiss@kernel.org> <benjamin.tissoires@redhat.com>
|
||||
Benno Lossin <lossin@kernel.org> <benno.lossin@proton.me>
|
||||
Bingwu Zhang <xtex@aosc.io> <xtexchooser@duck.com>
|
||||
Bingwu Zhang <xtex@aosc.io> <xtex@xtexx.eu.org>
|
||||
Bjorn Andersson <andersson@kernel.org> <bjorn@kryo.se>
|
||||
|
||||
@@ -85,6 +85,18 @@ written after the documentation, e.g.:
|
||||
// ...
|
||||
}
|
||||
|
||||
This applies to both public and private items. This increases consistency with
|
||||
public items, allows changes to visibility with less changes involved and will
|
||||
allow us to potentially generate the documentation for private items as well.
|
||||
In other words, if documentation is written for a private item, then ``///``
|
||||
should still be used. For instance:
|
||||
|
||||
.. code-block:: rust
|
||||
|
||||
/// My private function.
|
||||
// TODO: ...
|
||||
fn f() {}
|
||||
|
||||
One special kind of comments are the ``// SAFETY:`` comments. These must appear
|
||||
before every ``unsafe`` block, and they explain why the code inside the block is
|
||||
correct/sound, i.e. why it cannot trigger undefined behavior in any case, e.g.:
|
||||
@@ -191,6 +203,23 @@ or:
|
||||
/// [`struct mutex`]: srctree/include/linux/mutex.h
|
||||
|
||||
|
||||
C FFI types
|
||||
-----------
|
||||
|
||||
Rust kernel code refers to C types, such as ``int``, using type aliases such as
|
||||
``c_int``, which are readily available from the ``kernel`` prelude. Please do
|
||||
not use the aliases from ``core::ffi`` -- they may not map to the correct types.
|
||||
|
||||
These aliases should generally be referred directly by their identifier, i.e.
|
||||
as a single segment path. For instance:
|
||||
|
||||
.. code-block:: rust
|
||||
|
||||
fn f(p: *const c_char) -> c_int {
|
||||
// ...
|
||||
}
|
||||
|
||||
|
||||
Naming
|
||||
------
|
||||
|
||||
|
||||
@@ -90,15 +90,53 @@ they should generally work out of the box, e.g.::
|
||||
Ubuntu
|
||||
******
|
||||
|
||||
Ubuntu LTS and non-LTS (interim) releases provide recent Rust releases and thus
|
||||
they should generally work out of the box, e.g.::
|
||||
25.04
|
||||
~~~~~
|
||||
|
||||
apt install rustc-1.80 rust-1.80-src bindgen-0.65 rustfmt-1.80 rust-1.80-clippy
|
||||
The latest Ubuntu releases provide recent Rust releases and thus they should
|
||||
generally work out of the box, e.g.::
|
||||
|
||||
apt install rustc rust-src bindgen rustfmt rust-clippy
|
||||
|
||||
In addition, ``RUST_LIB_SRC`` needs to be set, e.g.::
|
||||
|
||||
RUST_LIB_SRC=/usr/src/rustc-$(rustc --version | cut -d' ' -f2)/library
|
||||
|
||||
For convenience, ``RUST_LIB_SRC`` can be exported to the global environment.
|
||||
|
||||
|
||||
24.04 LTS and older
|
||||
~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
Though Ubuntu 24.04 LTS and older versions still provide recent Rust
|
||||
releases, they require some additional configuration to be set, using
|
||||
the versioned packages, e.g.::
|
||||
|
||||
apt install rustc-1.80 rust-1.80-src bindgen-0.65 rustfmt-1.80 \
|
||||
rust-1.80-clippy
|
||||
ln -s /usr/lib/rust-1.80/bin/rustfmt /usr/bin/rustfmt-1.80
|
||||
ln -s /usr/lib/rust-1.80/bin/clippy-driver /usr/bin/clippy-driver-1.80
|
||||
|
||||
None of these packages set their tools as defaults; therefore they should be
|
||||
specified explicitly, e.g.::
|
||||
|
||||
make LLVM=1 RUSTC=rustc-1.80 RUSTDOC=rustdoc-1.80 RUSTFMT=rustfmt-1.80 \
|
||||
CLIPPY_DRIVER=clippy-driver-1.80 BINDGEN=bindgen-0.65
|
||||
|
||||
Alternatively, modify the ``PATH`` variable to place the Rust 1.80 binaries
|
||||
first and set ``bindgen`` as the default, e.g.::
|
||||
|
||||
PATH=/usr/lib/rust-1.80/bin:$PATH
|
||||
update-alternatives --install /usr/bin/bindgen bindgen \
|
||||
/usr/bin/bindgen-0.65 100
|
||||
update-alternatives --set bindgen /usr/bin/bindgen-0.65
|
||||
|
||||
``RUST_LIB_SRC`` needs to be set when using the versioned packages, e.g.::
|
||||
|
||||
RUST_LIB_SRC=/usr/src/rustc-$(rustc-1.80 --version | cut -d' ' -f2)/library
|
||||
|
||||
For convenience, ``RUST_LIB_SRC`` can be exported to the global environment.
|
||||
|
||||
In addition, ``bindgen-0.65`` is available in newer releases (24.04 LTS and
|
||||
24.10), but it may not be available in older ones (20.04 LTS and 22.04 LTS),
|
||||
thus ``bindgen`` may need to be built manually (please see below).
|
||||
|
||||
@@ -133,13 +133,85 @@ please see:
|
||||
The ``#[test]`` tests
|
||||
---------------------
|
||||
|
||||
Additionally, there are the ``#[test]`` tests. These can be run using the
|
||||
``rusttest`` Make target::
|
||||
Additionally, there are the ``#[test]`` tests. Like for documentation tests,
|
||||
these are also fairly similar to what you would expect from userspace, and they
|
||||
are also mapped to KUnit.
|
||||
|
||||
These tests are introduced by the ``kunit_tests`` procedural macro, which takes
|
||||
the name of the test suite as an argument.
|
||||
|
||||
For instance, assume we want to test the function ``f`` from the documentation
|
||||
tests section. We could write, in the same file where we have our function:
|
||||
|
||||
.. code-block:: rust
|
||||
|
||||
#[kunit_tests(rust_kernel_mymod)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_f() {
|
||||
assert_eq!(f(10, 20), 30);
|
||||
}
|
||||
}
|
||||
|
||||
And if we run it, the kernel log would look like::
|
||||
|
||||
KTAP version 1
|
||||
# Subtest: rust_kernel_mymod
|
||||
# speed: normal
|
||||
1..1
|
||||
# test_f.speed: normal
|
||||
ok 1 test_f
|
||||
ok 1 rust_kernel_mymod
|
||||
|
||||
Like documentation tests, the ``assert!`` and ``assert_eq!`` macros are mapped
|
||||
back to KUnit and do not panic. Similarly, the
|
||||
`? <https://doc.rust-lang.org/reference/expressions/operator-expr.html#the-question-mark-operator>`_
|
||||
operator is supported, i.e. the test functions may return either nothing (i.e.
|
||||
the unit type ``()``) or ``Result`` (i.e. any ``Result<T, E>``). For instance:
|
||||
|
||||
.. code-block:: rust
|
||||
|
||||
#[kunit_tests(rust_kernel_mymod)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_g() -> Result {
|
||||
let x = g()?;
|
||||
assert_eq!(x, 30);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
If we run the test and the call to ``g`` fails, then the kernel log would show::
|
||||
|
||||
KTAP version 1
|
||||
# Subtest: rust_kernel_mymod
|
||||
# speed: normal
|
||||
1..1
|
||||
# test_g: ASSERTION FAILED at rust/kernel/lib.rs:335
|
||||
Expected is_test_result_ok(test_g()) to be true, but is false
|
||||
# test_g.speed: normal
|
||||
not ok 1 test_g
|
||||
not ok 1 rust_kernel_mymod
|
||||
|
||||
If a ``#[test]`` test could be useful as an example for the user, then please
|
||||
use a documentation test instead. Even edge cases of an API, e.g. error or
|
||||
boundary cases, can be interesting to show in examples.
|
||||
|
||||
The ``rusttest`` host tests
|
||||
---------------------------
|
||||
|
||||
These are userspace tests that can be built and run in the host (i.e. the one
|
||||
that performs the kernel build) using the ``rusttest`` Make target::
|
||||
|
||||
make LLVM=1 rusttest
|
||||
|
||||
This requires the kernel ``.config``. It runs the ``#[test]`` tests on the host
|
||||
(currently) and thus is fairly limited in what these tests can test.
|
||||
This requires the kernel ``.config``.
|
||||
|
||||
Currently, they are mostly used for testing the ``macros`` crate's examples.
|
||||
|
||||
The Kselftests
|
||||
--------------
|
||||
|
||||
+20
-6
@@ -10719,20 +10719,23 @@ F: kernel/time/timer_list.c
|
||||
F: kernel/time/timer_migration.*
|
||||
F: tools/testing/selftests/timers/
|
||||
|
||||
HIGH-RESOLUTION TIMERS [RUST]
|
||||
DELAY, SLEEP, TIMEKEEPING, TIMERS [RUST]
|
||||
M: Andreas Hindborg <a.hindborg@kernel.org>
|
||||
R: Boqun Feng <boqun.feng@gmail.com>
|
||||
R: FUJITA Tomonori <fujita.tomonori@gmail.com>
|
||||
R: Frederic Weisbecker <frederic@kernel.org>
|
||||
R: Lyude Paul <lyude@redhat.com>
|
||||
R: Thomas Gleixner <tglx@linutronix.de>
|
||||
R: Anna-Maria Behnsen <anna-maria@linutronix.de>
|
||||
R: John Stultz <jstultz@google.com>
|
||||
R: Stephen Boyd <sboyd@kernel.org>
|
||||
L: rust-for-linux@vger.kernel.org
|
||||
S: Supported
|
||||
W: https://rust-for-linux.com
|
||||
B: https://github.com/Rust-for-Linux/linux/issues
|
||||
T: git https://github.com/Rust-for-Linux/linux.git hrtimer-next
|
||||
F: rust/kernel/time/hrtimer.rs
|
||||
F: rust/kernel/time/hrtimer/
|
||||
T: git https://github.com/Rust-for-Linux/linux.git timekeeping-next
|
||||
F: rust/kernel/time.rs
|
||||
F: rust/kernel/time/
|
||||
|
||||
HIGH-SPEED SCC DRIVER FOR AX.25
|
||||
L: linux-hams@vger.kernel.org
|
||||
@@ -21588,7 +21591,7 @@ M: Alex Gaynor <alex.gaynor@gmail.com>
|
||||
R: Boqun Feng <boqun.feng@gmail.com>
|
||||
R: Gary Guo <gary@garyguo.net>
|
||||
R: Björn Roy Baron <bjorn3_gh@protonmail.com>
|
||||
R: Benno Lossin <benno.lossin@proton.me>
|
||||
R: Benno Lossin <lossin@kernel.org>
|
||||
R: Andreas Hindborg <a.hindborg@kernel.org>
|
||||
R: Alice Ryhl <aliceryhl@google.com>
|
||||
R: Trevor Gross <tmgross@umich.edu>
|
||||
@@ -21618,7 +21621,7 @@ F: rust/kernel/alloc.rs
|
||||
F: rust/kernel/alloc/
|
||||
|
||||
RUST [PIN-INIT]
|
||||
M: Benno Lossin <benno.lossin@proton.me>
|
||||
M: Benno Lossin <lossin@kernel.org>
|
||||
L: rust-for-linux@vger.kernel.org
|
||||
S: Maintained
|
||||
W: https://rust-for-linux.com/pin-init
|
||||
@@ -26829,6 +26832,17 @@ F: lib/test_xarray.c
|
||||
F: lib/xarray.c
|
||||
F: tools/testing/radix-tree
|
||||
|
||||
XARRAY API [RUST]
|
||||
M: Tamir Duberstein <tamird@gmail.com>
|
||||
M: Andreas Hindborg <a.hindborg@kernel.org>
|
||||
L: rust-for-linux@vger.kernel.org
|
||||
S: Supported
|
||||
W: https://rust-for-linux.com
|
||||
B: https://github.com/Rust-for-Linux/linux/issues
|
||||
C: https://rust-for-linux.zulipchat.com
|
||||
T: git https://github.com/Rust-for-Linux/linux.git xarray-next
|
||||
F: rust/kernel/xarray.rs
|
||||
|
||||
XBOX DVD IR REMOTE
|
||||
M: Benjamin Valentin <benpicco@googlemail.com>
|
||||
S: Maintained
|
||||
|
||||
@@ -136,6 +136,9 @@ config LD_CAN_USE_KEEP_IN_OVERLAY
|
||||
config RUSTC_HAS_COERCE_POINTEE
|
||||
def_bool RUSTC_VERSION >= 108400
|
||||
|
||||
config RUSTC_HAS_SPAN_FILE
|
||||
def_bool RUSTC_VERSION >= 108800
|
||||
|
||||
config RUSTC_HAS_UNNECESSARY_TRANSMUTES
|
||||
def_bool RUSTC_VERSION >= 108800
|
||||
|
||||
|
||||
+11
-8
@@ -60,6 +60,8 @@ endif
|
||||
core-cfgs = \
|
||||
--cfg no_fp_fmt_parse
|
||||
|
||||
core-edition := $(if $(call rustc-min-version,108700),2024,2021)
|
||||
|
||||
# `rustc` recognizes `--remap-path-prefix` since 1.26.0, but `rustdoc` only
|
||||
# since Rust 1.81.0. Moreover, `rustdoc` ICEs on out-of-tree builds since Rust
|
||||
# 1.82.0 (https://github.com/rust-lang/rust/issues/138520). Thus workaround both
|
||||
@@ -106,8 +108,8 @@ rustdoc-macros: $(src)/macros/lib.rs FORCE
|
||||
|
||||
# Starting with Rust 1.82.0, skipping `-Wrustdoc::unescaped_backticks` should
|
||||
# not be needed -- see https://github.com/rust-lang/rust/pull/128307.
|
||||
rustdoc-core: private skip_flags = -Wrustdoc::unescaped_backticks
|
||||
rustdoc-core: private rustc_target_flags = $(core-cfgs)
|
||||
rustdoc-core: private skip_flags = --edition=2021 -Wrustdoc::unescaped_backticks
|
||||
rustdoc-core: private rustc_target_flags = --edition=$(core-edition) $(core-cfgs)
|
||||
rustdoc-core: $(RUST_LIB_SRC)/core/src/lib.rs FORCE
|
||||
+$(call if_changed,rustdoc)
|
||||
|
||||
@@ -273,7 +275,7 @@ bindgen_skip_c_flags := -mno-fp-ret-in-387 -mpreferred-stack-boundary=% \
|
||||
-fzero-call-used-regs=% -fno-stack-clash-protection \
|
||||
-fno-inline-functions-called-once -fsanitize=bounds-strict \
|
||||
-fstrict-flex-arrays=% -fmin-function-alignment=% \
|
||||
-fzero-init-padding-bits=% \
|
||||
-fzero-init-padding-bits=% -mno-fdpic \
|
||||
--param=% --param asan-%
|
||||
|
||||
# Derived from `scripts/Makefile.clang`.
|
||||
@@ -402,7 +404,8 @@ quiet_cmd_rustc_procmacro = $(RUSTC_OR_CLIPPY_QUIET) P $@
|
||||
-Clink-args='$(call escsq,$(KBUILD_PROCMACROLDFLAGS))' \
|
||||
--emit=dep-info=$(depfile) --emit=link=$@ --extern proc_macro \
|
||||
--crate-type proc-macro \
|
||||
--crate-name $(patsubst lib%.$(libmacros_extension),%,$(notdir $@)) $<
|
||||
--crate-name $(patsubst lib%.$(libmacros_extension),%,$(notdir $@)) \
|
||||
@$(objtree)/include/generated/rustc_cfg $<
|
||||
|
||||
# Procedural macros can only be used with the `rustc` that compiled it.
|
||||
$(obj)/$(libmacros_name): $(src)/macros/lib.rs FORCE
|
||||
@@ -416,7 +419,7 @@ quiet_cmd_rustc_library = $(if $(skip_clippy),RUSTC,$(RUSTC_OR_CLIPPY_QUIET)) L
|
||||
cmd_rustc_library = \
|
||||
OBJTREE=$(abspath $(objtree)) \
|
||||
$(if $(skip_clippy),$(RUSTC),$(RUSTC_OR_CLIPPY)) \
|
||||
$(filter-out $(skip_flags),$(rust_flags) $(rustc_target_flags)) \
|
||||
$(filter-out $(skip_flags),$(rust_flags)) $(rustc_target_flags) \
|
||||
--emit=dep-info=$(depfile) --emit=obj=$@ \
|
||||
--emit=metadata=$(dir $@)$(patsubst %.o,lib%.rmeta,$(notdir $@)) \
|
||||
--crate-type rlib -L$(objtree)/$(obj) \
|
||||
@@ -427,7 +430,7 @@ quiet_cmd_rustc_library = $(if $(skip_clippy),RUSTC,$(RUSTC_OR_CLIPPY_QUIET)) L
|
||||
|
||||
rust-analyzer:
|
||||
$(Q)MAKEFLAGS= $(srctree)/scripts/generate_rust_analyzer.py \
|
||||
--cfgs='core=$(core-cfgs)' \
|
||||
--cfgs='core=$(core-cfgs)' $(core-edition) \
|
||||
$(realpath $(srctree)) $(realpath $(objtree)) \
|
||||
$(rustc_sysroot) $(RUST_LIB_SRC) $(if $(KBUILD_EXTMOD),$(srcroot)) \
|
||||
> rust-project.json
|
||||
@@ -483,9 +486,9 @@ $(obj)/helpers/helpers.o: $(src)/helpers/helpers.c $(recordmcount_source) FORCE
|
||||
$(obj)/exports.o: private skip_gendwarfksyms = 1
|
||||
|
||||
$(obj)/core.o: private skip_clippy = 1
|
||||
$(obj)/core.o: private skip_flags = -Wunreachable_pub
|
||||
$(obj)/core.o: private skip_flags = --edition=2021 -Wunreachable_pub
|
||||
$(obj)/core.o: private rustc_objcopy = $(foreach sym,$(redirect-intrinsics),--redefine-sym $(sym)=__rust$(sym))
|
||||
$(obj)/core.o: private rustc_target_flags = $(core-cfgs)
|
||||
$(obj)/core.o: private rustc_target_flags = --edition=$(core-edition) $(core-cfgs)
|
||||
$(obj)/core.o: $(RUST_LIB_SRC)/core/src/lib.rs \
|
||||
$(wildcard $(objtree)/include/config/RUSTC_VERSION_TEXT) FORCE
|
||||
+$(call if_changed_rule,rustc_library)
|
||||
|
||||
@@ -6,6 +6,28 @@
|
||||
* Sorted alphabetically.
|
||||
*/
|
||||
|
||||
/*
|
||||
* First, avoid forward references to `enum` types.
|
||||
*
|
||||
* This workarounds a `bindgen` issue with them:
|
||||
* <https://github.com/rust-lang/rust-bindgen/issues/3179>.
|
||||
*
|
||||
* Without this, the generated Rust type may be the wrong one (`i32`) or
|
||||
* the proper one (typically `c_uint`) depending on how the headers are
|
||||
* included, which in turn may depend on the particular kernel configuration
|
||||
* or the architecture.
|
||||
*
|
||||
* The alternative would be to use casts and likely an
|
||||
* `#[allow(clippy::unnecessary_cast)]` in the Rust source files. Instead,
|
||||
* this approach allows us to keep the correct code in the source files and
|
||||
* simply remove this section when the issue is fixed upstream and we bump
|
||||
* the minimum `bindgen` version.
|
||||
*
|
||||
* This workaround may not be possible in some cases, depending on how the C
|
||||
* headers are set up.
|
||||
*/
|
||||
#include <linux/hrtimer_types.h>
|
||||
|
||||
#include <drm/drm_device.h>
|
||||
#include <drm/drm_drv.h>
|
||||
#include <drm/drm_file.h>
|
||||
@@ -48,6 +70,7 @@
|
||||
#include <linux/tracepoint.h>
|
||||
#include <linux/wait.h>
|
||||
#include <linux/workqueue.h>
|
||||
#include <linux/xarray.h>
|
||||
#include <trace/events/rust_sample.h>
|
||||
|
||||
#if defined(CONFIG_DRM_PANIC_SCREEN_QR_CODE)
|
||||
@@ -67,3 +90,8 @@ const gfp_t RUST_CONST_HELPER___GFP_HIGHMEM = ___GFP_HIGHMEM;
|
||||
const gfp_t RUST_CONST_HELPER___GFP_NOWARN = ___GFP_NOWARN;
|
||||
const blk_features_t RUST_CONST_HELPER_BLK_FEAT_ROTATIONAL = BLK_FEAT_ROTATIONAL;
|
||||
const fop_flags_t RUST_CONST_HELPER_FOP_UNSIGNED_OFFSET = FOP_UNSIGNED_OFFSET;
|
||||
|
||||
const xa_mark_t RUST_CONST_HELPER_XA_PRESENT = XA_PRESENT;
|
||||
|
||||
const gfp_t RUST_CONST_HELPER_XA_FLAGS_ALLOC = XA_FLAGS_ALLOC;
|
||||
const gfp_t RUST_CONST_HELPER_XA_FLAGS_ALLOC1 = XA_FLAGS_ALLOC1;
|
||||
|
||||
+1
-1
@@ -17,7 +17,7 @@ macro_rules! alias {
|
||||
|
||||
// Check size compatibility with `core`.
|
||||
const _: () = assert!(
|
||||
core::mem::size_of::<$name>() == core::mem::size_of::<core::ffi::$name>()
|
||||
::core::mem::size_of::<$name>() == ::core::mem::size_of::<::core::ffi::$name>()
|
||||
);
|
||||
)*}
|
||||
}
|
||||
|
||||
@@ -43,3 +43,4 @@
|
||||
#include "vmalloc.c"
|
||||
#include "wait.c"
|
||||
#include "workqueue.c"
|
||||
#include "xarray.c"
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
// SPDX-License-Identifier: GPL-2.0
|
||||
|
||||
#include <linux/xarray.h>
|
||||
|
||||
int rust_helper_xa_err(void *entry)
|
||||
{
|
||||
return xa_err(entry);
|
||||
}
|
||||
|
||||
void rust_helper_xa_init_flags(struct xarray *xa, gfp_t flags)
|
||||
{
|
||||
return xa_init_flags(xa, flags);
|
||||
}
|
||||
|
||||
int rust_helper_xa_trylock(struct xarray *xa)
|
||||
{
|
||||
return xa_trylock(xa);
|
||||
}
|
||||
|
||||
void rust_helper_xa_lock(struct xarray *xa)
|
||||
{
|
||||
return xa_lock(xa);
|
||||
}
|
||||
|
||||
void rust_helper_xa_unlock(struct xarray *xa)
|
||||
{
|
||||
return xa_unlock(xa);
|
||||
}
|
||||
@@ -94,10 +94,10 @@ pub mod flags {
|
||||
///
|
||||
/// A lower watermark is applied to allow access to "atomic reserves". The current
|
||||
/// implementation doesn't support NMI and few other strict non-preemptive contexts (e.g.
|
||||
/// raw_spin_lock). The same applies to [`GFP_NOWAIT`].
|
||||
/// `raw_spin_lock`). The same applies to [`GFP_NOWAIT`].
|
||||
pub const GFP_ATOMIC: Flags = Flags(bindings::GFP_ATOMIC);
|
||||
|
||||
/// Typical for kernel-internal allocations. The caller requires ZONE_NORMAL or a lower zone
|
||||
/// Typical for kernel-internal allocations. The caller requires `ZONE_NORMAL` or a lower zone
|
||||
/// for direct access but can direct reclaim.
|
||||
pub const GFP_KERNEL: Flags = Flags(bindings::GFP_KERNEL);
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
//! of those types (e.g. `CString`) use kernel allocators for instantiation.
|
||||
//!
|
||||
//! In order to allow userspace test cases to make use of such types as well, implement the
|
||||
//! `Cmalloc` allocator within the allocator_test module and type alias all kernel allocators to
|
||||
//! `Cmalloc` allocator within the `allocator_test` module and type alias all kernel allocators to
|
||||
//! `Cmalloc`. The `Cmalloc` allocator uses libc's `realloc()` function as allocator backend.
|
||||
|
||||
#![allow(missing_docs)]
|
||||
|
||||
+60
-20
@@ -57,12 +57,50 @@ use pin_init::{InPlaceWrite, Init, PinInit, ZeroableOption};
|
||||
/// assert!(KVBox::<Huge>::new_uninit(GFP_KERNEL).is_ok());
|
||||
/// ```
|
||||
///
|
||||
/// [`Box`]es can also be used to store trait objects by coercing their type:
|
||||
///
|
||||
/// ```
|
||||
/// trait FooTrait {}
|
||||
///
|
||||
/// struct FooStruct;
|
||||
/// impl FooTrait for FooStruct {}
|
||||
///
|
||||
/// let _ = KBox::new(FooStruct, GFP_KERNEL)? as KBox<dyn FooTrait>;
|
||||
/// # Ok::<(), Error>(())
|
||||
/// ```
|
||||
///
|
||||
/// # Invariants
|
||||
///
|
||||
/// `self.0` is always properly aligned and either points to memory allocated with `A` or, for
|
||||
/// zero-sized types, is a dangling, well aligned pointer.
|
||||
#[repr(transparent)]
|
||||
pub struct Box<T: ?Sized, A: Allocator>(NonNull<T>, PhantomData<A>);
|
||||
#[cfg_attr(CONFIG_RUSTC_HAS_COERCE_POINTEE, derive(core::marker::CoercePointee))]
|
||||
pub struct Box<#[cfg_attr(CONFIG_RUSTC_HAS_COERCE_POINTEE, pointee)] T: ?Sized, A: Allocator>(
|
||||
NonNull<T>,
|
||||
PhantomData<A>,
|
||||
);
|
||||
|
||||
// This is to allow coercion from `Box<T, A>` to `Box<U, A>` if `T` can be converted to the
|
||||
// dynamically-sized type (DST) `U`.
|
||||
#[cfg(not(CONFIG_RUSTC_HAS_COERCE_POINTEE))]
|
||||
impl<T, U, A> core::ops::CoerceUnsized<Box<U, A>> for Box<T, A>
|
||||
where
|
||||
T: ?Sized + core::marker::Unsize<U>,
|
||||
U: ?Sized,
|
||||
A: Allocator,
|
||||
{
|
||||
}
|
||||
|
||||
// This is to allow `Box<U, A>` to be dispatched on when `Box<T, A>` can be coerced into `Box<U,
|
||||
// A>`.
|
||||
#[cfg(not(CONFIG_RUSTC_HAS_COERCE_POINTEE))]
|
||||
impl<T, U, A> core::ops::DispatchFromDyn<Box<U, A>> for Box<T, A>
|
||||
where
|
||||
T: ?Sized + core::marker::Unsize<U>,
|
||||
U: ?Sized,
|
||||
A: Allocator,
|
||||
{
|
||||
}
|
||||
|
||||
/// Type alias for [`Box`] with a [`Kmalloc`] allocator.
|
||||
///
|
||||
@@ -101,7 +139,7 @@ pub type VBox<T> = Box<T, super::allocator::Vmalloc>;
|
||||
pub type KVBox<T> = Box<T, super::allocator::KVmalloc>;
|
||||
|
||||
// SAFETY: All zeros is equivalent to `None` (option layout optimization guarantee:
|
||||
// https://doc.rust-lang.org/stable/std/option/index.html#representation).
|
||||
// <https://doc.rust-lang.org/stable/std/option/index.html#representation>).
|
||||
unsafe impl<T, A: Allocator> ZeroableOption for Box<T, A> {}
|
||||
|
||||
// SAFETY: `Box` is `Send` if `T` is `Send` because the `Box` owns a `T`.
|
||||
@@ -360,68 +398,70 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: 'static, A> ForeignOwnable for Box<T, A>
|
||||
// SAFETY: The `into_foreign` function returns a pointer that is well-aligned.
|
||||
unsafe impl<T: 'static, A> ForeignOwnable for Box<T, A>
|
||||
where
|
||||
A: Allocator,
|
||||
{
|
||||
type PointedTo = T;
|
||||
type Borrowed<'a> = &'a T;
|
||||
type BorrowedMut<'a> = &'a mut T;
|
||||
|
||||
fn into_foreign(self) -> *mut crate::ffi::c_void {
|
||||
Box::into_raw(self).cast()
|
||||
fn into_foreign(self) -> *mut Self::PointedTo {
|
||||
Box::into_raw(self)
|
||||
}
|
||||
|
||||
unsafe fn from_foreign(ptr: *mut crate::ffi::c_void) -> Self {
|
||||
unsafe fn from_foreign(ptr: *mut Self::PointedTo) -> Self {
|
||||
// SAFETY: The safety requirements of this function ensure that `ptr` comes from a previous
|
||||
// call to `Self::into_foreign`.
|
||||
unsafe { Box::from_raw(ptr.cast()) }
|
||||
unsafe { Box::from_raw(ptr) }
|
||||
}
|
||||
|
||||
unsafe fn borrow<'a>(ptr: *mut crate::ffi::c_void) -> &'a T {
|
||||
unsafe fn borrow<'a>(ptr: *mut Self::PointedTo) -> &'a T {
|
||||
// SAFETY: The safety requirements of this method ensure that the object remains alive and
|
||||
// immutable for the duration of 'a.
|
||||
unsafe { &*ptr.cast() }
|
||||
unsafe { &*ptr }
|
||||
}
|
||||
|
||||
unsafe fn borrow_mut<'a>(ptr: *mut crate::ffi::c_void) -> &'a mut T {
|
||||
let ptr = ptr.cast();
|
||||
unsafe fn borrow_mut<'a>(ptr: *mut Self::PointedTo) -> &'a mut T {
|
||||
// SAFETY: The safety requirements of this method ensure that the pointer is valid and that
|
||||
// nothing else will access the value for the duration of 'a.
|
||||
unsafe { &mut *ptr }
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: 'static, A> ForeignOwnable for Pin<Box<T, A>>
|
||||
// SAFETY: The `into_foreign` function returns a pointer that is well-aligned.
|
||||
unsafe impl<T: 'static, A> ForeignOwnable for Pin<Box<T, A>>
|
||||
where
|
||||
A: Allocator,
|
||||
{
|
||||
type PointedTo = T;
|
||||
type Borrowed<'a> = Pin<&'a T>;
|
||||
type BorrowedMut<'a> = Pin<&'a mut T>;
|
||||
|
||||
fn into_foreign(self) -> *mut crate::ffi::c_void {
|
||||
fn into_foreign(self) -> *mut Self::PointedTo {
|
||||
// SAFETY: We are still treating the box as pinned.
|
||||
Box::into_raw(unsafe { Pin::into_inner_unchecked(self) }).cast()
|
||||
Box::into_raw(unsafe { Pin::into_inner_unchecked(self) })
|
||||
}
|
||||
|
||||
unsafe fn from_foreign(ptr: *mut crate::ffi::c_void) -> Self {
|
||||
unsafe fn from_foreign(ptr: *mut Self::PointedTo) -> Self {
|
||||
// SAFETY: The safety requirements of this function ensure that `ptr` comes from a previous
|
||||
// call to `Self::into_foreign`.
|
||||
unsafe { Pin::new_unchecked(Box::from_raw(ptr.cast())) }
|
||||
unsafe { Pin::new_unchecked(Box::from_raw(ptr)) }
|
||||
}
|
||||
|
||||
unsafe fn borrow<'a>(ptr: *mut crate::ffi::c_void) -> Pin<&'a T> {
|
||||
unsafe fn borrow<'a>(ptr: *mut Self::PointedTo) -> Pin<&'a T> {
|
||||
// SAFETY: The safety requirements for this function ensure that the object is still alive,
|
||||
// so it is safe to dereference the raw pointer.
|
||||
// The safety requirements of `from_foreign` also ensure that the object remains alive for
|
||||
// the lifetime of the returned value.
|
||||
let r = unsafe { &*ptr.cast() };
|
||||
let r = unsafe { &*ptr };
|
||||
|
||||
// SAFETY: This pointer originates from a `Pin<Box<T>>`.
|
||||
unsafe { Pin::new_unchecked(r) }
|
||||
}
|
||||
|
||||
unsafe fn borrow_mut<'a>(ptr: *mut crate::ffi::c_void) -> Pin<&'a mut T> {
|
||||
let ptr = ptr.cast();
|
||||
unsafe fn borrow_mut<'a>(ptr: *mut Self::PointedTo) -> Pin<&'a mut T> {
|
||||
// SAFETY: The safety requirements for this function ensure that the object is still alive,
|
||||
// so it is safe to dereference the raw pointer.
|
||||
// The safety requirements of `from_foreign` also ensure that the object remains alive for
|
||||
|
||||
+404
-29
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,61 @@
|
||||
// SPDX-License-Identifier: GPL-2.0
|
||||
|
||||
//! Errors for the [`Vec`] type.
|
||||
|
||||
use core::fmt::{self, Debug, Formatter};
|
||||
use kernel::prelude::*;
|
||||
|
||||
/// Error type for [`Vec::push_within_capacity`].
|
||||
pub struct PushError<T>(pub T);
|
||||
|
||||
impl<T> Debug for PushError<T> {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
|
||||
write!(f, "Not enough capacity")
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> From<PushError<T>> for Error {
|
||||
fn from(_: PushError<T>) -> Error {
|
||||
// Returning ENOMEM isn't appropriate because the system is not out of memory. The vector
|
||||
// is just full and we are refusing to resize it.
|
||||
EINVAL
|
||||
}
|
||||
}
|
||||
|
||||
/// Error type for [`Vec::remove`].
|
||||
pub struct RemoveError;
|
||||
|
||||
impl Debug for RemoveError {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
|
||||
write!(f, "Index out of bounds")
|
||||
}
|
||||
}
|
||||
|
||||
impl From<RemoveError> for Error {
|
||||
fn from(_: RemoveError) -> Error {
|
||||
EINVAL
|
||||
}
|
||||
}
|
||||
|
||||
/// Error type for [`Vec::insert_within_capacity`].
|
||||
pub enum InsertError<T> {
|
||||
/// The value could not be inserted because the index is out of bounds.
|
||||
IndexOutOfBounds(T),
|
||||
/// The value could not be inserted because the vector is out of capacity.
|
||||
OutOfCapacity(T),
|
||||
}
|
||||
|
||||
impl<T> Debug for InsertError<T> {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
|
||||
match self {
|
||||
InsertError::IndexOutOfBounds(_) => write!(f, "Index out of bounds"),
|
||||
InsertError::OutOfCapacity(_) => write!(f, "Not enough capacity"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> From<InsertError<T>> for Error {
|
||||
fn from(_: InsertError<T>) -> Error {
|
||||
EINVAL
|
||||
}
|
||||
}
|
||||
@@ -73,7 +73,9 @@ impl<T: Driver + 'static> Adapter<T> {
|
||||
// Let the `struct auxiliary_device` own a reference of the driver's private data.
|
||||
// SAFETY: By the type invariant `adev.as_raw` returns a valid pointer to a
|
||||
// `struct auxiliary_device`.
|
||||
unsafe { bindings::auxiliary_set_drvdata(adev.as_raw(), data.into_foreign()) };
|
||||
unsafe {
|
||||
bindings::auxiliary_set_drvdata(adev.as_raw(), data.into_foreign().cast())
|
||||
};
|
||||
}
|
||||
Err(err) => return Error::to_errno(err),
|
||||
}
|
||||
@@ -89,7 +91,7 @@ impl<T: Driver + 'static> Adapter<T> {
|
||||
// SAFETY: `remove_callback` is only ever called after a successful call to
|
||||
// `probe_callback`, hence it's guaranteed that `ptr` points to a valid and initialized
|
||||
// `KBox<T>` pointer created through `KBox::into_foreign`.
|
||||
drop(unsafe { KBox::<T>::from_foreign(ptr) });
|
||||
drop(unsafe { KBox::<T>::from_foreign(ptr.cast()) });
|
||||
}
|
||||
}
|
||||
|
||||
@@ -234,7 +236,7 @@ impl Device {
|
||||
extern "C" fn release(dev: *mut bindings::device) {
|
||||
// SAFETY: By the type invariant `self.0.as_raw` is a pointer to the `struct device`
|
||||
// embedded in `struct auxiliary_device`.
|
||||
let adev = unsafe { container_of!(dev, bindings::auxiliary_device, dev) }.cast_mut();
|
||||
let adev = unsafe { container_of!(dev, bindings::auxiliary_device, dev) };
|
||||
|
||||
// SAFETY: `adev` points to the memory that has been allocated in `Registration::new`, via
|
||||
// `KBox::new(Opaque::<bindings::auxiliary_device>::zeroed(), GFP_KERNEL)`.
|
||||
|
||||
@@ -129,7 +129,7 @@ impl GenDiskBuilder {
|
||||
get_unique_id: None,
|
||||
// TODO: Set to THIS_MODULE. Waiting for const_refs_to_static feature to
|
||||
// be merged (unstable in rustc 1.78 which is staged for linux 6.10)
|
||||
// https://github.com/rust-lang/rust/issues/119618
|
||||
// <https://github.com/rust-lang/rust/issues/119618>
|
||||
owner: core::ptr::null_mut(),
|
||||
pr_ops: core::ptr::null_mut(),
|
||||
free_disk: None,
|
||||
|
||||
@@ -554,7 +554,7 @@ where
|
||||
let c_group: *mut bindings::config_group =
|
||||
// SAFETY: By function safety requirements, `item` is embedded in a
|
||||
// `config_group`.
|
||||
unsafe { container_of!(item, bindings::config_group, cg_item) }.cast_mut();
|
||||
unsafe { container_of!(item, bindings::config_group, cg_item) };
|
||||
|
||||
// SAFETY: The function safety requirements for this function satisfy
|
||||
// the conditions for this call.
|
||||
@@ -588,7 +588,7 @@ where
|
||||
let c_group: *mut bindings::config_group =
|
||||
// SAFETY: By function safety requirements, `item` is embedded in a
|
||||
// `config_group`.
|
||||
unsafe { container_of!(item, bindings::config_group, cg_item) }.cast_mut();
|
||||
unsafe { container_of!(item, bindings::config_group, cg_item) };
|
||||
|
||||
// SAFETY: The function safety requirements for this function satisfy
|
||||
// the conditions for this call.
|
||||
|
||||
@@ -635,7 +635,7 @@ impl Policy {
|
||||
None
|
||||
} else {
|
||||
// SAFETY: The data is earlier set from [`set_data`].
|
||||
Some(unsafe { T::borrow(self.as_ref().driver_data) })
|
||||
Some(unsafe { T::borrow(self.as_ref().driver_data.cast()) })
|
||||
}
|
||||
}
|
||||
|
||||
@@ -662,7 +662,7 @@ impl Policy {
|
||||
let data = Some(
|
||||
// SAFETY: The data is earlier set by us from [`set_data`]. It is safe to take
|
||||
// back the ownership of the data from the foreign interface.
|
||||
unsafe { <T as ForeignOwnable>::from_foreign(self.as_ref().driver_data) },
|
||||
unsafe { <T as ForeignOwnable>::from_foreign(self.as_ref().driver_data.cast()) },
|
||||
);
|
||||
self.as_mut_ref().driver_data = ptr::null_mut();
|
||||
data
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user