Skip to content

Commit 7608b76

Browse files
committed
Merge branch 'bits/190-rust' into asahi-wip
2 parents 6d04d60 + b150ae9 commit 7608b76

126 files changed

Lines changed: 11178 additions & 850 deletions

Some content is hidden

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

.clippy.toml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# SPDX-License-Identifier: GPL-2.0
2+
3+
check-private-items = true
4+
5+
disallowed-macros = [
6+
# The `clippy::dbg_macro` lint only works with `std::dbg!`, thus we simulate
7+
# it here, see: https://github.com/rust-lang/rust-clippy/issues/11303.
8+
{ path = "kernel::dbg", reason = "the `dbg!` macro is intended as a debugging tool" },
9+
]

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ modules.order
103103
# We don't want to ignore the following even if they are dot-files
104104
#
105105
!.clang-format
106+
!.clippy.toml
106107
!.cocciconfig
107108
!.editorconfig
108109
!.get_maintainer.ignore

Documentation/rust/coding-guidelines.rst

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,3 +227,151 @@ The equivalent in Rust may look like (ignoring documentation):
227227
That is, the equivalent of ``GPIO_LINE_DIRECTION_IN`` would be referred to as
228228
``gpio::LineDirection::In``. In particular, it should not be named
229229
``gpio::gpio_line_direction::GPIO_LINE_DIRECTION_IN``.
230+
231+
232+
Lints
233+
-----
234+
235+
In Rust, it is possible to ``allow`` particular warnings (diagnostics, lints)
236+
locally, making the compiler ignore instances of a given warning within a given
237+
function, module, block, etc.
238+
239+
It is similar to ``#pragma GCC diagnostic push`` + ``ignored`` + ``pop`` in C
240+
[#]_:
241+
242+
.. code-block:: c
243+
244+
#pragma GCC diagnostic push
245+
#pragma GCC diagnostic ignored "-Wunused-function"
246+
static void f(void) {}
247+
#pragma GCC diagnostic pop
248+
249+
.. [#] In this particular case, the kernel's ``__{always,maybe}_unused``
250+
attributes (C23's ``[[maybe_unused]]``) may be used; however, the example
251+
is meant to reflect the equivalent lint in Rust discussed afterwards.
252+
253+
But way less verbose:
254+
255+
.. code-block:: rust
256+
257+
#[allow(dead_code)]
258+
fn f() {}
259+
260+
By that virtue, it makes it possible to comfortably enable more diagnostics by
261+
default (i.e. outside ``W=`` levels). In particular, those that may have some
262+
false positives but that are otherwise quite useful to keep enabled to catch
263+
potential mistakes.
264+
265+
On top of that, Rust provides the ``expect`` attribute which takes this further.
266+
It makes the compiler warn if the warning was not produced. For instance, the
267+
following will ensure that, when ``f()`` is called somewhere, we will have to
268+
remove the attribute:
269+
270+
.. code-block:: rust
271+
272+
#[expect(dead_code)]
273+
fn f() {}
274+
275+
If we do not, we get a warning from the compiler::
276+
277+
warning: this lint expectation is unfulfilled
278+
--> x.rs:3:10
279+
|
280+
3 | #[expect(dead_code)]
281+
| ^^^^^^^^^
282+
|
283+
= note: `#[warn(unfulfilled_lint_expectations)]` on by default
284+
285+
This means that ``expect``\ s do not get forgotten when they are not needed, which
286+
may happen in several situations, e.g.:
287+
288+
- Temporary attributes added while developing.
289+
290+
- Improvements in lints in the compiler, Clippy or custom tools which may
291+
remove a false positive.
292+
293+
- When the lint is not needed anymore because it was expected that it would be
294+
removed at some point, such as the ``dead_code`` example above.
295+
296+
It also increases the visibility of the remaining ``allow``\ s and reduces the
297+
chance of misapplying one.
298+
299+
Thus prefer ``except`` over ``allow`` unless:
300+
301+
- The lint attribute is intended to be temporary, e.g. while developing.
302+
303+
- Conditional compilation triggers the warning in some cases but not others.
304+
305+
If there are only a few cases where the warning triggers (or does not
306+
trigger) compared to the total number of cases, then one may consider using
307+
a conditional ``expect`` (i.e. ``cfg_attr(..., expect(...))``). Otherwise,
308+
it is likely simpler to just use ``allow``.
309+
310+
- Inside macros, when the different invocations may create expanded code that
311+
triggers the warning in some cases but not in others.
312+
313+
- When code may trigger a warning for some architectures but not others, such
314+
as an ``as`` cast to a C FFI type.
315+
316+
As a more developed example, consider for instance this program:
317+
318+
.. code-block:: rust
319+
320+
fn g() {}
321+
322+
fn main() {
323+
#[cfg(CONFIG_X)]
324+
g();
325+
}
326+
327+
Here, function ``g()`` is dead code if ``CONFIG_X`` is not set. Can we use
328+
``expect`` here?
329+
330+
.. code-block:: rust
331+
332+
#[expect(dead_code)]
333+
fn g() {}
334+
335+
fn main() {
336+
#[cfg(CONFIG_X)]
337+
g();
338+
}
339+
340+
This would emit a lint if ``CONFIG_X`` is set, since it is not dead code in that
341+
configuration. Therefore, in cases like this, we cannot use ``expect`` as-is.
342+
343+
A simple possibility is using ``allow``:
344+
345+
.. code-block:: rust
346+
347+
#[allow(dead_code)]
348+
fn g() {}
349+
350+
fn main() {
351+
#[cfg(CONFIG_X)]
352+
g();
353+
}
354+
355+
An alternative would be using a conditional ``expect``:
356+
357+
.. code-block:: rust
358+
359+
#[cfg_attr(not(CONFIG_X), expect(dead_code))]
360+
fn g() {}
361+
362+
fn main() {
363+
#[cfg(CONFIG_X)]
364+
g();
365+
}
366+
367+
This would ensure that, if someone introduces another call to ``g()`` somewhere
368+
(e.g. unconditionally), then it would be spotted that it is not dead code
369+
anymore. However, the ``cfg_attr`` is more complex than a simple ``allow``.
370+
371+
Therefore, it is likely that it is not worth using conditional ``expect``\ s when
372+
more than one or two configurations are involved or when the lint may be
373+
triggered due to non-local changes (such as ``dead_code``).
374+
375+
For more information about diagnostics in Rust, please see:
376+
377+
https://doc.rust-lang.org/stable/reference/attributes/diagnostics.html

Documentation/rust/quick-start.rst

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,23 @@ they should generally work out of the box, e.g.::
8787
zypper install rust rust1.79-src rust-bindgen clang
8888

8989

90+
Ubuntu
91+
******
92+
93+
Ubuntu LTS and non-LTS (interim) releases provide recent Rust releases and thus
94+
they should generally work out of the box, e.g.::
95+
96+
apt install rustc-1.80 rust-1.80-src bindgen-0.65 rustfmt-1.80 rust-1.80-clippy
97+
98+
``RUST_LIB_SRC`` needs to be set when using the versioned packages, e.g.::
99+
100+
RUST_LIB_SRC=/usr/src/rustc-$(rustc-1.80 --version | cut -d' ' -f2)/library
101+
102+
In addition, ``bindgen-0.65`` is available in newer releases (24.04 LTS and
103+
24.10), but it may not be available in older ones (20.04 LTS and 22.04 LTS),
104+
thus ``bindgen`` may need to be built manually (please see below).
105+
106+
90107
Requirements: Building
91108
----------------------
92109

MAINTAINERS

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6978,6 +6978,11 @@ F: include/linux/kobj*
69786978
F: include/linux/property.h
69796979
F: lib/kobj*
69806980
F: rust/kernel/device.rs
6981+
F: rust/kernel/device_id.rs
6982+
F: rust/kernel/devres.rs
6983+
F: rust/kernel/driver.rs
6984+
F: rust/kernel/platform.rs
6985+
F: samples/rust/rust_driver_platform.rs
69816986

69826987
DRIVERS FOR OMAP ADAPTIVE VOLTAGE SCALING (AVS)
69836988
M: Nishanth Menon <nm@ti.com>
@@ -17304,6 +17309,7 @@ T: git git://git.kernel.org/pub/scm/linux/kernel/git/robh/linux.git
1730417309
F: Documentation/ABI/testing/sysfs-firmware-ofw
1730517310
F: drivers/of/
1730617311
F: include/linux/of*.h
17312+
F: rust/kernel/of.rs
1730717313
F: scripts/dtc/
1730817314
F: tools/testing/selftests/dt/
1730917315
K: of_overlay_notifier_
@@ -17903,6 +17909,8 @@ F: include/asm-generic/pci*
1790317909
F: include/linux/of_pci.h
1790417910
F: include/linux/pci*
1790517911
F: include/uapi/linux/pci*
17912+
F: rust/kernel/pci.rs
17913+
F: samples/rust/rust_driver_pci.rs
1790617914

1790717915
PCIE DRIVER FOR AMAZON ANNAPURNA LABS
1790817916
M: Jonathan Chocron <jonnyc@amazon.com>
@@ -20191,13 +20199,21 @@ B: https://github.com/Rust-for-Linux/linux/issues
2019120199
C: zulip://rust-for-linux.zulipchat.com
2019220200
P: https://rust-for-linux.com/contributing
2019320201
T: git https://github.com/Rust-for-Linux/linux.git rust-next
20202+
F: .clippy.toml
2019420203
F: Documentation/rust/
2019520204
F: rust/
2019620205
F: samples/rust/
2019720206
F: scripts/*rust*
2019820207
F: tools/testing/selftests/rust/
2019920208
K: \b(?i:rust)\b
2020020209

20210+
RUST [ALLOC]
20211+
M: Danilo Krummrich <dakr@kernel.org>
20212+
L: rust-for-linux@vger.kernel.org
20213+
S: Maintained
20214+
F: rust/kernel/alloc.rs
20215+
F: rust/kernel/alloc/
20216+
2020120217
RXRPC SOCKETS (AF_RXRPC)
2020220218
M: David Howells <dhowells@redhat.com>
2020320219
M: Marc Dionne <marc.dionne@auristor.com>

Makefile

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -446,18 +446,22 @@ KBUILD_USERLDFLAGS := $(USERLDFLAGS)
446446
export rust_common_flags := --edition=2021 \
447447
-Zbinary_dep_depinfo=y \
448448
-Astable_features \
449-
-Dunsafe_op_in_unsafe_fn \
450449
-Dnon_ascii_idents \
450+
-Dunsafe_op_in_unsafe_fn \
451+
-Wmissing_docs \
451452
-Wrust_2018_idioms \
452453
-Wunreachable_pub \
453-
-Wmissing_docs \
454-
-Wrustdoc::missing_crate_level_docs \
455454
-Wclippy::all \
455+
-Wclippy::ignored_unit_patterns \
456456
-Wclippy::mut_mut \
457457
-Wclippy::needless_bitwise_bool \
458458
-Wclippy::needless_continue \
459459
-Wclippy::no_mangle_with_rust_abi \
460-
-Wclippy::dbg_macro
460+
-Wclippy::undocumented_unsafe_blocks \
461+
-Wclippy::unnecessary_safety_comment \
462+
-Wclippy::unnecessary_safety_doc \
463+
-Wrustdoc::missing_crate_level_docs \
464+
-Wrustdoc::unescaped_backticks
461465

462466
KBUILD_HOSTCFLAGS := $(KBUILD_USERHOSTCFLAGS) $(HOST_LFS_CFLAGS) \
463467
$(HOSTCFLAGS) -I $(srctree)/scripts/include
@@ -582,6 +586,9 @@ endif
582586
# Allows the usage of unstable features in stable compilers.
583587
export RUSTC_BOOTSTRAP := 1
584588

589+
# Allows finding `.clippy.toml` in out-of-srctree builds.
590+
export CLIPPY_CONF_DIR := $(srctree)
591+
585592
export ARCH SRCARCH CONFIG_SHELL BASH HOSTCC KBUILD_HOSTCFLAGS CROSS_COMPILE LD CC HOSTPKG_CONFIG
586593
export RUSTC RUSTDOC RUSTFMT RUSTC_OR_CLIPPY_QUIET RUSTC_OR_CLIPPY BINDGEN
587594
export HOSTRUSTC KBUILD_HOSTRUSTFLAGS

drivers/block/rnull.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use kernel::{
2020
error::Result,
2121
new_mutex, pr_info,
2222
prelude::*,
23+
str::CStr,
2324
sync::{Arc, Mutex},
2425
types::ARef,
2526
};
@@ -32,11 +33,11 @@ module! {
3233
}
3334

3435
struct NullBlkModule {
35-
_disk: Pin<Box<Mutex<GenDisk<NullBlkDevice>>>>,
36+
_disk: Pin<KBox<Mutex<GenDisk<NullBlkDevice>>>>,
3637
}
3738

3839
impl kernel::Module for NullBlkModule {
39-
fn init(_module: &'static ThisModule) -> Result<Self> {
40+
fn init(_name: &'static CStr, _module: &'static ThisModule) -> Result<Self> {
4041
pr_info!("Rust null_blk loaded\n");
4142
let tagset = Arc::pin_init(TagSet::new(1, 256, 1), flags::GFP_KERNEL)?;
4243

@@ -47,7 +48,7 @@ impl kernel::Module for NullBlkModule {
4748
.rotational(false)
4849
.build(format_args!("rnullb{}", 0), tagset)?;
4950

50-
let disk = Box::pin_init(new_mutex!(disk, "nullb:disk"), flags::GFP_KERNEL)?;
51+
let disk = KBox::pin_init(new_mutex!(disk, "nullb:disk"), flags::GFP_KERNEL)?;
5152

5253
Ok(Self { _disk: disk })
5354
}

drivers/gpu/drm/drm_panic_qr.rs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -209,12 +209,9 @@ const FORMAT_INFOS_QR_L: [u16; 8] = [
209209
impl Version {
210210
/// Returns the smallest QR version than can hold these segments.
211211
fn from_segments(segments: &[&Segment<'_>]) -> Option<Version> {
212-
for v in (1..=40).map(|k| Version(k)) {
213-
if v.max_data() * 8 >= segments.iter().map(|s| s.total_size_bits(v)).sum() {
214-
return Some(v);
215-
}
216-
}
217-
None
212+
(1..=40)
213+
.map(Version)
214+
.find(|&v| v.max_data() * 8 >= segments.iter().map(|s| s.total_size_bits(v)).sum())
218215
}
219216

220217
fn width(&self) -> u8 {
@@ -242,7 +239,7 @@ impl Version {
242239
}
243240

244241
fn alignment_pattern(&self) -> &'static [u8] {
245-
&ALIGNMENT_PATTERNS[self.0 - 1]
242+
ALIGNMENT_PATTERNS[self.0 - 1]
246243
}
247244

248245
fn poly(&self) -> &'static [u8] {
@@ -479,7 +476,7 @@ struct EncodedMsg<'a> {
479476
/// Data to be put in the QR code, with correct segment encoding, padding, and
480477
/// Error Code Correction.
481478
impl EncodedMsg<'_> {
482-
fn new<'a, 'b>(segments: &[&Segment<'b>], data: &'a mut [u8]) -> Option<EncodedMsg<'a>> {
479+
fn new<'a>(segments: &[&Segment<'_>], data: &'a mut [u8]) -> Option<EncodedMsg<'a>> {
483480
let version = Version::from_segments(segments)?;
484481
let ec_size = version.ec_size();
485482
let g1_blocks = version.g1_blocks();
@@ -492,7 +489,7 @@ impl EncodedMsg<'_> {
492489
data.fill(0);
493490

494491
let mut em = EncodedMsg {
495-
data: data,
492+
data,
496493
ec_size,
497494
g1_blocks,
498495
g2_blocks,
@@ -722,7 +719,10 @@ impl QrImage<'_> {
722719

723720
fn is_finder(&self, x: u8, y: u8) -> bool {
724721
let end = self.width - 8;
725-
(x < 8 && y < 8) || (x < 8 && y >= end) || (x >= end && y < 8)
722+
#[expect(clippy::nonminimal_bool)]
723+
{
724+
(x < 8 && y < 8) || (x < 8 && y >= end) || (x >= end && y < 8)
725+
}
726726
}
727727

728728
// Alignment pattern: 5x5 squares in a grid.
@@ -979,10 +979,11 @@ pub unsafe extern "C" fn drm_panic_qr_generate(
979979
/// * `url_len`: Length of the URL.
980980
///
981981
/// * If `url_len` > 0, remove the 2 segments header/length and also count the
982-
/// conversion to numeric segments.
982+
/// conversion to numeric segments.
983983
/// * If `url_len` = 0, only removes 3 bytes for 1 binary segment.
984984
#[no_mangle]
985985
pub extern "C" fn drm_panic_qr_max_data_size(version: u8, url_len: usize) -> usize {
986+
#[expect(clippy::manual_range_contains)]
986987
if version < 1 || version > 40 {
987988
return 0;
988989
}

drivers/soc/apple/Kconfig

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,11 @@ config APPLE_SART
7474

7575
Say 'y' here if you have an Apple SoC.
7676

77+
config RUST_APPLE_RTKIT
78+
bool
79+
depends on RUST
80+
depends on APPLE_RTKIT
81+
7782
endmenu
7883

7984
endif

0 commit comments

Comments
 (0)