Skip to content

Commit beb841f

Browse files
committed
apply coding style
Taken from Documentation/process/coding-style.rst Signed-off-by: Finn Behrens <[email protected]>
1 parent c17d290 commit beb841f

File tree

18 files changed

+1387
-1066
lines changed

18 files changed

+1387
-1066
lines changed

drivers/char/rust_example/src/lib.rs

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
use kernel::prelude::*;
77

8-
module!{
8+
module! {
99
type: RustExample,
1010
name: b"rust_example",
1111
author: b"Rust for Linux Contributors",
@@ -25,27 +25,31 @@ module!{
2525
},
2626
}
2727

28-
struct RustExample {
29-
message: String,
28+
struct RustExample
29+
{
30+
message: String,
3031
}
3132

32-
impl KernelModule for RustExample {
33-
fn init() -> KernelResult<Self> {
34-
println!("Rust Example (init)");
35-
println!("Am I built-in? {}", !cfg!(MODULE));
36-
println!("Parameters:");
37-
println!(" my_bool: {}", my_bool.read());
38-
println!(" my_i32: {}", my_i32.read());
39-
Ok(RustExample {
40-
message: "on the heap!".to_owned(),
41-
})
42-
}
33+
impl KernelModule for RustExample
34+
{
35+
fn init() -> KernelResult<Self>
36+
{
37+
println!("Rust Example (init)");
38+
println!("Am I built-in? {}", !cfg!(MODULE));
39+
println!("Parameters:");
40+
println!(" my_bool: {}", my_bool.read());
41+
println!(" my_i32: {}", my_i32.read());
42+
Ok(RustExample {
43+
message: "on the heap!".to_owned(),
44+
})
45+
}
4346
}
4447

45-
impl Drop for RustExample {
46-
fn drop(&mut self) {
47-
println!("My message is {}", self.message);
48-
println!("Rust Example (exit)");
49-
}
48+
impl Drop for RustExample
49+
{
50+
fn drop(&mut self)
51+
{
52+
println!("My message is {}", self.message);
53+
println!("Rust Example (exit)");
54+
}
5055
}
51-

rust/kernel/build.rs

Lines changed: 118 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -1,135 +1,144 @@
11
// SPDX-License-Identifier: GPL-2.0
22

3-
use std::path::PathBuf;
43
use std::env;
4+
use std::path::PathBuf;
55

6-
const INCLUDED_TYPES: &[&str] = &["file_system_type", "mode_t", "umode_t", "ctl_table"];
6+
const INCLUDED_TYPES: &[&str] =
7+
&["file_system_type", "mode_t", "umode_t", "ctl_table"];
78
const INCLUDED_FUNCTIONS: &[&str] = &[
8-
"cdev_add",
9-
"cdev_init",
10-
"cdev_del",
11-
"register_filesystem",
12-
"unregister_filesystem",
13-
"krealloc",
14-
"kfree",
15-
"mount_nodev",
16-
"kill_litter_super",
17-
"register_sysctl",
18-
"unregister_sysctl_table",
19-
"access_ok",
20-
"_copy_to_user",
21-
"_copy_from_user",
22-
"alloc_chrdev_region",
23-
"unregister_chrdev_region",
24-
"wait_for_random_bytes",
25-
"get_random_bytes",
26-
"rng_is_initialized",
27-
"printk",
28-
"add_device_randomness",
9+
"cdev_add",
10+
"cdev_init",
11+
"cdev_del",
12+
"register_filesystem",
13+
"unregister_filesystem",
14+
"krealloc",
15+
"kfree",
16+
"mount_nodev",
17+
"kill_litter_super",
18+
"register_sysctl",
19+
"unregister_sysctl_table",
20+
"access_ok",
21+
"_copy_to_user",
22+
"_copy_from_user",
23+
"alloc_chrdev_region",
24+
"unregister_chrdev_region",
25+
"wait_for_random_bytes",
26+
"get_random_bytes",
27+
"rng_is_initialized",
28+
"printk",
29+
"add_device_randomness",
2930
];
3031
const INCLUDED_VARS: &[&str] = &[
31-
"EINVAL",
32-
"ENOMEM",
33-
"ESPIPE",
34-
"EFAULT",
35-
"EAGAIN",
36-
"__this_module",
37-
"FS_REQUIRES_DEV",
38-
"FS_BINARY_MOUNTDATA",
39-
"FS_HAS_SUBTYPE",
40-
"FS_USERNS_MOUNT",
41-
"FS_RENAME_DOES_D_MOVE",
42-
"BINDINGS_GFP_KERNEL",
43-
"KERN_INFO",
44-
"VERIFY_WRITE",
45-
"LINUX_VERSION_CODE",
46-
"SEEK_SET",
47-
"SEEK_CUR",
48-
"SEEK_END",
49-
"O_NONBLOCK",
50-
"param_ops_bool",
51-
"param_ops_int",
32+
"EINVAL",
33+
"ENOMEM",
34+
"ESPIPE",
35+
"EFAULT",
36+
"EAGAIN",
37+
"__this_module",
38+
"FS_REQUIRES_DEV",
39+
"FS_BINARY_MOUNTDATA",
40+
"FS_HAS_SUBTYPE",
41+
"FS_USERNS_MOUNT",
42+
"FS_RENAME_DOES_D_MOVE",
43+
"BINDINGS_GFP_KERNEL",
44+
"KERN_INFO",
45+
"VERIFY_WRITE",
46+
"LINUX_VERSION_CODE",
47+
"SEEK_SET",
48+
"SEEK_CUR",
49+
"SEEK_END",
50+
"O_NONBLOCK",
51+
"param_ops_bool",
52+
"param_ops_int",
5253
];
5354
const OPAQUE_TYPES: &[&str] = &[
54-
// These need to be opaque because they're both packed and aligned, which rustc
55-
// doesn't support yet. See https://github.com/rust-lang/rust/issues/59154
56-
// and https://github.com/rust-lang/rust-bindgen/issues/1538
57-
"desc_struct",
58-
"xregs_state",
55+
// These need to be opaque because they're both packed and aligned, which rustc
56+
// doesn't support yet. See https://github.com/rust-lang/rust/issues/59154
57+
// and https://github.com/rust-lang/rust-bindgen/issues/1538
58+
"desc_struct",
59+
"xregs_state",
5960
];
6061

6162
// Takes the CFLAGS from the kernel Makefile and changes all the include paths to be absolute
6263
// instead of relative.
63-
fn prepare_cflags(cflags: &str, kernel_dir: &str) -> Vec<String> {
64-
let cflag_parts = shlex::split(&cflags).unwrap();
65-
let mut cflag_iter = cflag_parts.iter();
66-
let mut kernel_args = vec![];
67-
while let Some(arg) = cflag_iter.next() {
68-
// TODO: bindgen complains
69-
if arg.starts_with("-Wp,-MMD") {
70-
continue;
71-
}
64+
fn prepare_cflags(cflags: &str, kernel_dir: &str) -> Vec<String>
65+
{
66+
let cflag_parts = shlex::split(&cflags).unwrap();
67+
let mut cflag_iter = cflag_parts.iter();
68+
let mut kernel_args = vec![];
69+
while let Some(arg) = cflag_iter.next() {
70+
// TODO: bindgen complains
71+
if arg.starts_with("-Wp,-MMD") {
72+
continue;
73+
}
7274

73-
if arg.starts_with("-I") && !arg.starts_with("-I/") {
74-
kernel_args.push(format!("-I{}/{}", kernel_dir, &arg[2..]));
75-
} else if arg == "-include" {
76-
kernel_args.push(arg.to_string());
77-
let include_path = cflag_iter.next().unwrap();
78-
if include_path.starts_with('/') {
79-
kernel_args.push(include_path.to_string());
80-
} else {
81-
kernel_args.push(format!("{}/{}", kernel_dir, include_path));
82-
}
83-
} else {
84-
kernel_args.push(arg.to_string());
75+
if arg.starts_with("-I") && !arg.starts_with("-I/") {
76+
kernel_args.push(format!(
77+
"-I{}/{}",
78+
kernel_dir,
79+
&arg[2..]
80+
));
81+
} else if arg == "-include" {
82+
kernel_args.push(arg.to_string());
83+
let include_path = cflag_iter.next().unwrap();
84+
if include_path.starts_with('/') {
85+
kernel_args.push(include_path.to_string());
86+
} else {
87+
kernel_args.push(format!(
88+
"{}/{}",
89+
kernel_dir, include_path
90+
));
91+
}
92+
} else {
93+
kernel_args.push(arg.to_string());
94+
}
8595
}
86-
}
87-
kernel_args
96+
kernel_args
8897
}
8998

90-
fn main() {
91-
println!("cargo:rerun-if-env-changed=CC");
92-
println!("cargo:rerun-if-env-changed=RUST_BINDGEN_CFLAGS");
99+
fn main()
100+
{
101+
println!("cargo:rerun-if-env-changed=CC");
102+
println!("cargo:rerun-if-env-changed=RUST_BINDGEN_CFLAGS");
93103

94-
let kernel_dir = "../../";
95-
let cflags = env::var("RUST_BINDGEN_CFLAGS")
96-
.expect("Must be invoked from kernel makefile");
104+
let kernel_dir = "../../";
105+
let cflags = env::var("RUST_BINDGEN_CFLAGS")
106+
.expect("Must be invoked from kernel makefile");
97107

98-
let kernel_args = prepare_cflags(&cflags, &kernel_dir);
108+
let kernel_args = prepare_cflags(&cflags, &kernel_dir);
99109

100-
let target = env::var("TARGET").unwrap();
110+
let target = env::var("TARGET").unwrap();
101111

102-
let mut builder = bindgen::Builder::default()
103-
.use_core()
104-
.ctypes_prefix("c_types")
105-
.derive_default(true)
106-
.size_t_is_usize(true)
107-
.rustfmt_bindings(true);
112+
let mut builder = bindgen::Builder::default()
113+
.use_core()
114+
.ctypes_prefix("c_types")
115+
.derive_default(true)
116+
.size_t_is_usize(true)
117+
.rustfmt_bindings(true);
108118

109-
builder = builder.clang_arg(format!("--target={}", target));
110-
for arg in kernel_args.iter() {
111-
builder = builder.clang_arg(arg.clone());
112-
}
119+
builder = builder.clang_arg(format!("--target={}", target));
120+
for arg in kernel_args.iter() {
121+
builder = builder.clang_arg(arg.clone());
122+
}
113123

114-
println!("cargo:rerun-if-changed=src/bindings_helper.h");
115-
builder = builder.header("src/bindings_helper.h");
124+
println!("cargo:rerun-if-changed=src/bindings_helper.h");
125+
builder = builder.header("src/bindings_helper.h");
116126

117-
for t in INCLUDED_TYPES {
118-
builder = builder.whitelist_type(t);
119-
}
120-
for f in INCLUDED_FUNCTIONS {
121-
builder = builder.whitelist_function(f);
122-
}
123-
for v in INCLUDED_VARS {
124-
builder = builder.whitelist_var(v);
125-
}
126-
for t in OPAQUE_TYPES {
127-
builder = builder.opaque_type(t);
128-
}
129-
let bindings = builder.generate().expect("Unable to generate bindings");
127+
for t in INCLUDED_TYPES {
128+
builder = builder.whitelist_type(t);
129+
}
130+
for f in INCLUDED_FUNCTIONS {
131+
builder = builder.whitelist_function(f);
132+
}
133+
for v in INCLUDED_VARS {
134+
builder = builder.whitelist_var(v);
135+
}
136+
for t in OPAQUE_TYPES {
137+
builder = builder.opaque_type(t);
138+
}
139+
let bindings = builder.generate().expect("Unable to generate bindings");
130140

131-
let out_path = PathBuf::from("src/bindings_gen.rs");
132-
bindings
133-
.write_to_file(out_path)
134-
.expect("Couldn't write bindings!");
141+
let out_path = PathBuf::from("src/bindings_gen.rs");
142+
bindings.write_to_file(out_path)
143+
.expect("Couldn't write bindings!");
135144
}

rust/kernel/src/allocator.rs

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,27 @@ use crate::c_types;
88

99
pub struct KernelAllocator;
1010

11-
unsafe impl GlobalAlloc for KernelAllocator {
12-
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
13-
// krealloc is used instead of kmalloc because kmalloc is an inline function and can't be
14-
// bound to as a result
15-
bindings::krealloc(ptr::null(), layout.size(), bindings::GFP_KERNEL) as *mut u8
16-
}
11+
unsafe impl GlobalAlloc for KernelAllocator
12+
{
13+
unsafe fn alloc(&self, layout: Layout) -> *mut u8
14+
{
15+
// krealloc is used instead of kmalloc because kmalloc is an inline function and can't be
16+
// bound to as a result
17+
bindings::krealloc(
18+
ptr::null(),
19+
layout.size(),
20+
bindings::GFP_KERNEL,
21+
) as *mut u8
22+
}
1723

18-
unsafe fn dealloc(&self, ptr: *mut u8, _layout: Layout) {
19-
bindings::kfree(ptr as *const c_types::c_void);
20-
}
24+
unsafe fn dealloc(&self, ptr: *mut u8, _layout: Layout)
25+
{
26+
bindings::kfree(ptr as *const c_types::c_void);
27+
}
2128
}
2229

2330
#[alloc_error_handler]
24-
fn oom(_layout: Layout) -> ! {
25-
panic!("Out of memory!");
31+
fn oom(_layout: Layout) -> !
32+
{
33+
panic!("Out of memory!");
2634
}

rust/kernel/src/bindings.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
// SPDX-License-Identifier: GPL-2.0
22

33
#[allow(
4-
clippy::all,
5-
non_camel_case_types,
6-
non_upper_case_globals,
7-
non_snake_case,
8-
improper_ctypes
4+
clippy::all,
5+
non_camel_case_types,
6+
non_upper_case_globals,
7+
non_snake_case,
8+
improper_ctypes
99
)]
10-
mod bindings_raw {
11-
use crate::c_types;
12-
include!("bindings_gen.rs");
10+
mod bindings_raw
11+
{
12+
use crate::c_types;
13+
include!("bindings_gen.rs");
1314
}
1415
pub use bindings_raw::*;
1516

0 commit comments

Comments
 (0)