Skip to content

Commit 33aa8fa

Browse files
authored
Merge pull request #93 from Rust-for-Linux/rust-fmt
Formatting support
2 parents 3012327 + 7ace64d commit 33aa8fa

File tree

13 files changed

+101
-26
lines changed

13 files changed

+101
-26
lines changed

.github/workflows/ci.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,3 +180,6 @@ jobs:
180180

181181
# Docs
182182
- run: make ${{ env.MAKE_ARCH }} ${{ env.MAKE_CROSS_COMPILE }} ${{ env.MAKE_TOOLCHAIN }} ${{ env.MAKE_OUTPUT }} ${{ env.MAKE_SYSROOT }} -j3 rustdoc
183+
184+
# Formatting
185+
- run: make rustfmtcheck

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ modules.order
9595
!.gitattributes
9696
!.gitignore
9797
!.mailmap
98+
!.rustfmt.toml
9899

99100
# XXX: Only for GitHub CI -- do not commit into mainline
100101
!.github

.rustfmt.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
edition = "2018"

Documentation/rust/coding.rst

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,37 @@ This document describes how to write Rust code in the kernel.
99
Coding style
1010
------------
1111

12-
For the moment, we are following the idiomatic Rust style. For instance,
13-
we use 4 spaces for indentation rather than tabs.
12+
The code is automatically formatted using the ``rustfmt`` tool. This is very
13+
good news!
14+
15+
- If you contribute from time to time to the kernel, you do not need to learn
16+
and remember one more style guide. You will also need less patch roundtrips
17+
to land a change.
18+
19+
- If you are a reviewer or a maintainer, you will not need to spend time on
20+
pointing out style issues anymore.
21+
22+
.. note:: Conventions on comments and documentation are not checked by
23+
``rustfmt``. Thus we still need to take care of those: please see
24+
:ref:`Documentation/rust/docs.rst <rust_docs>`.
25+
26+
We use the tool's default settings. This means we are following the idiomatic
27+
Rust style. For instance, we use 4 spaces for indentation rather than tabs.
28+
29+
Typically, you will want to instruct your editor/IDE to format while you type,
30+
when you save or at commit time. However, if for some reason you want
31+
to reformat the entire kernel Rust sources at some point, you may run::
32+
33+
make rustfmt
34+
35+
To check if everything is formatted (printing a diff otherwise), e.g. if you
36+
have configured a CI for a tree as a maintainer, you may run::
37+
38+
make rustfmtcheck
39+
40+
Like ``clang-format`` for the rest of the kernel, ``rustfmt`` works on
41+
individual files, and does not require a kernel configuration. Sometimes it may
42+
even work with broken code.
1443

1544

1645
Abstractions vs. bindings

Documentation/rust/quick-start.rst

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,8 @@ the kernel with ``CC=clang`` or ``LLVM=1``.
6767
rustfmt
6868
*******
6969

70-
Optionally, if you install the ``rustfmt`` tool, then the generated C bindings
71-
will be automatically formatted. It is also useful to have the tool to format
72-
your own code, too.
70+
The ``rustfmt`` tool is used to automatically format all the Rust kernel code,
71+
including the generated C bindings.
7372

7473
If you are using ``rustup``, its ``default`` profile already installs the tool,
7574
so you should be good to go. If you are using another profile, you can install

Makefile

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ no-dot-config-targets := $(clean-targets) \
264264
cscope gtags TAGS tags help% %docs check% coccicheck \
265265
$(version_h) headers headers_% archheaders archscripts \
266266
%asm-generic kernelversion %src-pkg dt_binding_check \
267-
outputmakefile
267+
outputmakefile rustfmt rustfmtcheck
268268
no-sync-config-targets := $(no-dot-config-targets) %install kernelrelease
269269
single-targets := %.a %.i %.ko %.lds %.ll %.lst %.mod %.o %.s %.symtypes %/
270270

@@ -445,6 +445,7 @@ READELF = $(CROSS_COMPILE)readelf
445445
STRIP = $(CROSS_COMPILE)strip
446446
endif
447447
RUSTC = rustc
448+
RUSTFMT = rustfmt
448449
BINDGEN = bindgen
449450
PAHOLE = pahole
450451
RESOLVE_BTFIDS = $(objtree)/tools/bpf/resolve_btfids/resolve_btfids
@@ -1651,6 +1652,13 @@ help:
16511652
@echo ' kselftest-merge - Merge all the config dependencies of'
16521653
@echo ' kselftest to existing .config.'
16531654
@echo ''
1655+
@echo 'Rust targets:'
1656+
@echo ' rustfmt - Reformat all the Rust code in the kernel.'
1657+
@echo ' rustfmtcheck - Checks if all the Rust code in the kernel'
1658+
@echo ' is formatted, printing a diff otherwise.'
1659+
@echo ' rustdoc - Generate Rust documentation'
1660+
@echo ' (requires kernel .config)'
1661+
@echo ''
16541662
@$(if $(dtstree), \
16551663
echo 'Devicetree:'; \
16561664
echo '* dtbs - Build device tree blobs for enabled boards'; \
@@ -1669,9 +1677,6 @@ help:
16691677
@echo 'Documentation targets:'
16701678
@$(MAKE) -f $(srctree)/Documentation/Makefile dochelp
16711679
@echo ''
1672-
@echo ' rustdoc - Generate Rust documentation'
1673-
@echo ' (requires kernel .config)'
1674-
@echo ''
16751680
@echo 'Architecture specific targets ($(SRCARCH)):'
16761681
@$(if $(archhelp),$(archhelp),\
16771682
echo ' No architecture specific help defined for $(SRCARCH)')
@@ -1726,14 +1731,25 @@ $(DOC_TARGETS):
17261731
$(Q)$(MAKE) $(build)=Documentation $@
17271732

17281733

1729-
# Rust documentation target
1734+
# Rust targets
17301735
# ---------------------------------------------------------------------------
17311736

1737+
# Documentation target
1738+
#
17321739
# Using the singular to avoid running afoul of `no-dot-config-targets`.
17331740
PHONY += rustdoc
17341741
rustdoc: prepare0
17351742
$(Q)$(MAKE) $(build)=rust $@
17361743

1744+
# Formatting targets
1745+
PHONY += rustfmt rustfmtcheck
1746+
1747+
rustfmt:
1748+
find -name '*.rs' | xargs $(RUSTFMT)
1749+
1750+
rustfmtcheck:
1751+
find -name '*.rs' | xargs $(RUSTFMT) --check
1752+
17371753

17381754
# Misc
17391755
# ---------------------------------------------------------------------------

drivers/char/rust_example.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ impl KernelModule for RustExample {
7373
let x: [u64; 1028] = core::hint::black_box([5; 1028]);
7474
println!("Large array has length: {}", x.len());
7575

76-
let mut chrdev_reg = chrdev::Registration::new_pinned(
77-
cstr!("rust_chrdev"), 0, &THIS_MODULE)?;
76+
let mut chrdev_reg =
77+
chrdev::Registration::new_pinned(cstr!("rust_chrdev"), 0, &THIS_MODULE)?;
7878
chrdev_reg.as_mut().register::<RustFile>()?;
7979
chrdev_reg.as_mut().register::<RustFile>()?;
8080

rust/compiler_builtins.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
2121
#![feature(compiler_builtins)]
2222
#![compiler_builtins]
23-
2423
#![no_builtins]
2524
#![no_std]
2625

rust/kernel/allocator.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,24 @@ pub fn __rust_dealloc(ptr: *mut u8, _size: usize, _align: usize) {
4040

4141
#[no_mangle]
4242
pub fn __rust_realloc(ptr: *mut u8, _old_size: usize, _align: usize, new_size: usize) -> *mut u8 {
43-
unsafe { bindings::krealloc(ptr as *const c_types::c_void, new_size, bindings::GFP_KERNEL) as *mut u8 }
43+
unsafe {
44+
bindings::krealloc(
45+
ptr as *const c_types::c_void,
46+
new_size,
47+
bindings::GFP_KERNEL,
48+
) as *mut u8
49+
}
4450
}
4551

4652
#[no_mangle]
4753
pub fn __rust_alloc_zeroed(size: usize, _align: usize) -> *mut u8 {
48-
unsafe { bindings::krealloc(core::ptr::null(), size, bindings::GFP_KERNEL | bindings::__GFP_ZERO) as *mut u8 }
54+
unsafe {
55+
bindings::krealloc(
56+
core::ptr::null(),
57+
size,
58+
bindings::GFP_KERNEL | bindings::__GFP_ZERO,
59+
) as *mut u8
60+
}
4961
}
5062

5163
#[no_mangle]

rust/kernel/chrdev.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,11 @@ impl<const N: usize> Registration<{ N }> {
4747
minors_start: u16,
4848
this_module: &'static crate::ThisModule,
4949
) -> KernelResult<Pin<Box<Self>>> {
50-
Ok(Pin::from(Box::try_new(Self::new(name, minors_start, this_module))?))
50+
Ok(Pin::from(Box::try_new(Self::new(
51+
name,
52+
minors_start,
53+
this_module,
54+
))?))
5155
}
5256
/// Register a character device with this range. Call this once per device
5357
/// type (up to `N` times).
@@ -57,7 +61,7 @@ impl<const N: usize> Registration<{ N }> {
5761
if this.inner.is_none() {
5862
let mut dev: bindings::dev_t = 0;
5963
// SAFETY: Calling unsafe function. `this.name` has 'static
60-
// lifetime
64+
// lifetime
6165
let res = unsafe {
6266
bindings::alloc_chrdev_region(
6367
&mut dev,

rust/kernel/lib.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,9 @@ impl ThisModule {
6060
// SAFETY: `kernel_param_lock` will check if the pointer is null and use the built-in mutex
6161
// in that case.
6262
#[cfg(CONFIG_SYSFS)]
63-
unsafe { bindings::kernel_param_lock(self.0) }
63+
unsafe {
64+
bindings::kernel_param_lock(self.0)
65+
}
6466

6567
KParamGuard { this_module: self }
6668
}
@@ -69,7 +71,7 @@ impl ThisModule {
6971
/// Scoped lock on the kernel parameters of `ThisModule`. Lock will be released
7072
/// when this struct is dropped.
7173
pub struct KParamGuard<'a> {
72-
this_module: &'a ThisModule
74+
this_module: &'a ThisModule,
7375
}
7476

7577
#[cfg(CONFIG_SYSFS)]

rust/kernel/user_ptr.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,17 @@ extern "C" {
1111
fn rust_helper_access_ok(addr: *const c_types::c_void, len: c_types::c_ulong)
1212
-> c_types::c_int;
1313

14-
fn rust_helper_copy_from_user(to: *mut c_types::c_void, from: *const c_types::c_void,
15-
n: c_types::c_ulong) -> c_types::c_ulong;
16-
17-
fn rust_helper_copy_to_user(to: *mut c_types::c_void, from: *const c_types::c_void,
18-
n: c_types::c_ulong) -> c_types::c_ulong;
14+
fn rust_helper_copy_from_user(
15+
to: *mut c_types::c_void,
16+
from: *const c_types::c_void,
17+
n: c_types::c_ulong,
18+
) -> c_types::c_ulong;
19+
20+
fn rust_helper_copy_to_user(
21+
to: *mut c_types::c_void,
22+
from: *const c_types::c_void,
23+
n: c_types::c_ulong,
24+
) -> c_types::c_ulong;
1925
}
2026

2127
/// A reference to an area in userspace memory, which can be either

rust/module.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ fn permissions_are_readonly(perms: &str) -> bool {
167167
};
168168
match u32::from_str_radix(digits, radix) {
169169
Ok(perms) => perms & 0o222 == 0,
170-
Err(_) => false
170+
Err(_) => false,
171171
}
172172
}
173173

@@ -306,7 +306,10 @@ pub fn module(ts: TokenStream) -> TokenStream {
306306
_ => &param_type,
307307
};
308308
let param_default = match param_type.as_ref() {
309-
"str" => format!("b\"{}\0\" as *const _ as *mut kernel::c_types::c_char", param_default),
309+
"str" => format!(
310+
"b\"{}\0\" as *const _ as *mut kernel::c_types::c_char",
311+
param_default
312+
),
310313
_ => param_default,
311314
};
312315
let read_func = match (param_type.as_ref(), permissions_are_readonly(&param_permissions)) {

0 commit comments

Comments
 (0)