Skip to content

Commit 6c1a3fa

Browse files
authored
Merge pull request raspberrypi#713 from ojeda/c-comment-style
rust: C files improvements & cleanups
2 parents 486c2cd + 11d4bd3 commit 6c1a3fa

File tree

3 files changed

+60
-51
lines changed

3 files changed

+60
-51
lines changed

rust/exports.c

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
// SPDX-License-Identifier: GPL-2.0
2-
//
3-
// A hack to export Rust symbols for loadable modules without having to redo
4-
// the entire `include/linux/export.h` logic in Rust.
5-
//
6-
// This requires the Rust's new/future `v0` mangling scheme because the default
7-
// one ("legacy") uses invalid characters for C identifiers (thus we cannot use
8-
// the `EXPORT_SYMBOL_*` macros).
9-
//
10-
// All symbols are exported as GPL-only to guarantee no GPL-only feature is
11-
// accidentally exposed.
2+
/*
3+
* A hack to export Rust symbols for loadable modules without having to redo
4+
* the entire `include/linux/export.h` logic in Rust.
5+
*
6+
* This requires the Rust's new/future `v0` mangling scheme because the default
7+
* one ("legacy") uses invalid characters for C identifiers (thus we cannot use
8+
* the `EXPORT_SYMBOL_*` macros).
9+
*
10+
* All symbols are exported as GPL-only to guarantee no GPL-only feature is
11+
* accidentally exposed.
12+
*/
1213

1314
#include <linux/module.h>
1415

rust/helpers.c

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
11
// SPDX-License-Identifier: GPL-2.0
2-
//
3-
// Non-trivial C macros cannot be used in Rust. Similarly, inlined C functions
4-
// cannot be called either. This file explicitly creates functions ("helpers")
5-
// that wrap those so that they can be called from Rust.
6-
//
7-
// Even though Rust kernel modules should never use directly the bindings, some
8-
// of these helpers need to be exported because Rust generics and inlined
9-
// functions may not get their code generated in the crate where they are
10-
// defined. Other helpers, called from non-inline functions, may not be
11-
// exported, in principle. However, in general, the Rust compiler does not
12-
// guarantee codegen will be performed for a non-inline function either.
13-
// Therefore, this file exports all the helpers. In the future, this may be
14-
// revisited to reduce the number of exports after the compiler is informed
15-
// about the places codegen is required.
16-
//
17-
// All symbols are exported as GPL-only to guarantee no GPL-only feature is
18-
// accidentally exposed.
2+
/*
3+
* Non-trivial C macros cannot be used in Rust. Similarly, inlined C functions
4+
* cannot be called either. This file explicitly creates functions ("helpers")
5+
* that wrap those so that they can be called from Rust.
6+
*
7+
* Even though Rust kernel modules should never use directly the bindings, some
8+
* of these helpers need to be exported because Rust generics and inlined
9+
* functions may not get their code generated in the crate where they are
10+
* defined. Other helpers, called from non-inline functions, may not be
11+
* exported, in principle. However, in general, the Rust compiler does not
12+
* guarantee codegen will be performed for a non-inline function either.
13+
* Therefore, this file exports all the helpers. In the future, this may be
14+
* revisited to reduce the number of exports after the compiler is informed
15+
* about the places codegen is required.
16+
*
17+
* All symbols are exported as GPL-only to guarantee no GPL-only feature is
18+
* accidentally exposed.
19+
*/
1920

2021
#include <linux/bug.h>
2122
#include <linux/build_bug.h>
@@ -506,24 +507,25 @@ const struct of_device_id *rust_helper_of_match_device(
506507
}
507508
EXPORT_SYMBOL_GPL(rust_helper_of_match_device);
508509

509-
/* We use bindgen's --size_t-is-usize option to bind the C size_t type
510-
* as the Rust usize type, so we can use it in contexts where Rust
511-
* expects a usize like slice (array) indices. usize is defined to be
512-
* the same as C's uintptr_t type (can hold any pointer) but not
513-
* necessarily the same as size_t (can hold the size of any single
510+
/*
511+
* We use `bindgen`'s `--size_t-is-usize` option to bind the C `size_t` type
512+
* as the Rust `usize` type, so we can use it in contexts where Rust
513+
* expects a `usize` like slice (array) indices. `usize` is defined to be
514+
* the same as C's `uintptr_t` type (can hold any pointer) but not
515+
* necessarily the same as `size_t` (can hold the size of any single
514516
* object). Most modern platforms use the same concrete integer type for
515517
* both of them, but in case we find ourselves on a platform where
516518
* that's not true, fail early instead of risking ABI or
517519
* integer-overflow issues.
518520
*
519521
* If your platform fails this assertion, it means that you are in
520522
* danger of integer-overflow bugs (even if you attempt to remove
521-
* --size_t-is-usize). It may be easiest to change the kernel ABI on
522-
* your platform such that size_t matches uintptr_t (i.e., to increase
523-
* size_t, because uintptr_t has to be at least as big as size_t).
524-
*/
523+
* `--size_t-is-usize`). It may be easiest to change the kernel ABI on
524+
* your platform such that `size_t` matches `uintptr_t` (i.e., to increase
525+
* `size_t`, because `uintptr_t` has to be at least as big as `size_t`).
526+
*/
525527
static_assert(
526528
sizeof(size_t) == sizeof(uintptr_t) &&
527529
__alignof__(size_t) == __alignof__(uintptr_t),
528-
"Rust code expects C size_t to match Rust usize"
530+
"Rust code expects C `size_t` to match Rust `usize`"
529531
);

rust/kernel/bindings_helper.h

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,37 @@
11
/* SPDX-License-Identifier: GPL-2.0 */
2+
/*
3+
* Header that contains the code (mostly headers) for which Rust bindings
4+
* will be automatically generated by `bindgen`.
5+
*
6+
* Sorted alphabetically.
7+
*/
28

9+
#include <asm/io.h>
10+
#include <linux/amba/bus.h>
311
#include <linux/cdev.h>
412
#include <linux/clk.h>
513
#include <linux/errname.h>
14+
#include <linux/file.h>
615
#include <linux/fs.h>
16+
#include <linux/gpio/driver.h>
717
#include <linux/hw_random.h>
18+
#include <linux/interrupt.h>
19+
#include <linux/irqdomain.h>
20+
#include <linux/irq.h>
21+
#include <linux/miscdevice.h>
22+
#include <linux/mm.h>
823
#include <linux/module.h>
24+
#include <linux/of_platform.h>
25+
#include <linux/platform_device.h>
26+
#include <linux/poll.h>
927
#include <linux/random.h>
28+
#include <linux/security.h>
1029
#include <linux/slab.h>
1130
#include <linux/sysctl.h>
1231
#include <linux/uaccess.h>
1332
#include <linux/uio.h>
14-
#include <linux/miscdevice.h>
15-
#include <linux/poll.h>
16-
#include <linux/mm.h>
17-
#include <linux/file.h>
1833
#include <uapi/linux/android/binder.h>
19-
#include <linux/platform_device.h>
20-
#include <linux/of_platform.h>
21-
#include <linux/security.h>
22-
#include <asm/io.h>
23-
#include <linux/irq.h>
24-
#include <linux/interrupt.h>
25-
#include <linux/irqdomain.h>
26-
#include <linux/amba/bus.h>
27-
#include <linux/gpio/driver.h>
2834

29-
// `bindgen` gets confused at certain things
35+
/* `bindgen` gets confused at certain things. */
3036
const gfp_t BINDINGS_GFP_KERNEL = GFP_KERNEL;
3137
const gfp_t BINDINGS___GFP_ZERO = __GFP_ZERO;

0 commit comments

Comments
 (0)