Skip to content

Commit c779a9c

Browse files
metaspaceojeda
authored andcommitted
rust: kbuild: split up helpers.c
This patch splits up the rust helpers C file. When rebasing patch sets on upstream linux, merge conflicts in helpers.c is common and time consuming [1]. Thus, split the file so that each kernel component can live in a separate file. This patch lists helper files explicitly and thus conflicts in the file list is still likely. However, they should be more simple to resolve than the conflicts usually seen in helpers.c. [ Removed `README.md` and undeleted the original comment since now, in v3 of the series, we have a `helpers.c` again; which also allows us to keep the "Sorted alphabetically" line and makes the diff easier. In addition, update the Documentation/ mentions of the file, reworded title and removed blank lines at the end of `page.c`. - Miguel ] Link: https://rust-for-linux.zulipchat.com/#narrow/stream/288089-General/topic/Splitting.20up.20helpers.2Ec/near/426694012 [1] Signed-off-by: Andreas Hindborg <[email protected]> Reviewed-by: Gary Guo <[email protected]> Acked-by: Dirk Behme <[email protected]> Reviewed-by: Alice Ryhl <[email protected]> Reviewed-by: Benno Lossin <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Miguel Ojeda <[email protected]>
1 parent f1385dc commit c779a9c

File tree

20 files changed

+301
-244
lines changed

20 files changed

+301
-244
lines changed

Documentation/rust/general-information.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ should provide as-safe-as-possible abstractions as needed.
7575
.. code-block::
7676
7777
rust/bindings/
78-
(rust/helpers.c)
78+
(rust/helpers/)
7979
8080
include/ -----+ <-+
8181
| |
@@ -112,7 +112,7 @@ output files in the ``rust/bindings/`` directory.
112112

113113
For parts of the C header that ``bindgen`` does not auto generate, e.g. C
114114
``inline`` functions or non-trivial macros, it is acceptable to add a small
115-
wrapper function to ``rust/helpers.c`` to make it available for the Rust side as
115+
wrapper function to ``rust/helpers/`` to make it available for the Rust side as
116116
well.
117117

118118
Abstractions

rust/Makefile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ always-$(CONFIG_RUST) += exports_core_generated.h
88

99
# Missing prototypes are expected in the helpers since these are exported
1010
# for Rust only, thus there is no header nor prototypes.
11-
obj-$(CONFIG_RUST) += helpers.o
12-
CFLAGS_REMOVE_helpers.o = -Wmissing-prototypes -Wmissing-declarations
11+
obj-$(CONFIG_RUST) += helpers/helpers.o
12+
CFLAGS_REMOVE_helpers/helpers.o = -Wmissing-prototypes -Wmissing-declarations
1313

1414
always-$(CONFIG_RUST) += libmacros.so
1515
no-clean-files += libmacros.so
@@ -299,7 +299,7 @@ $(obj)/bindings/bindings_helpers_generated.rs: private bindgen_target_cflags = \
299299
-I$(objtree)/$(obj) -Wno-missing-prototypes -Wno-missing-declarations
300300
$(obj)/bindings/bindings_helpers_generated.rs: private bindgen_target_extra = ; \
301301
sed -Ei 's/pub fn rust_helper_([a-zA-Z0-9_]*)/#[link_name="rust_helper_\1"]\n pub fn \1/g' $@
302-
$(obj)/bindings/bindings_helpers_generated.rs: $(src)/helpers.c FORCE
302+
$(obj)/bindings/bindings_helpers_generated.rs: $(src)/helpers/helpers.c FORCE
303303
$(call if_changed_dep,bindgen)
304304

305305
quiet_cmd_exports = EXPORTS $@

rust/helpers.c

Lines changed: 0 additions & 239 deletions
This file was deleted.

rust/helpers/blk.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
#include <linux/blk-mq.h>
4+
#include <linux/blkdev.h>
5+
6+
void *rust_helper_blk_mq_rq_to_pdu(struct request *rq)
7+
{
8+
return blk_mq_rq_to_pdu(rq);
9+
}
10+
EXPORT_SYMBOL_GPL(rust_helper_blk_mq_rq_to_pdu);
11+
12+
struct request *rust_helper_blk_mq_rq_from_pdu(void *pdu)
13+
{
14+
return blk_mq_rq_from_pdu(pdu);
15+
}
16+
EXPORT_SYMBOL_GPL(rust_helper_blk_mq_rq_from_pdu);

rust/helpers/bug.c

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+
#include <linux/bug.h>
4+
5+
__noreturn void rust_helper_BUG(void)
6+
{
7+
BUG();
8+
}
9+
EXPORT_SYMBOL_GPL(rust_helper_BUG);

rust/helpers/build_assert.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
#include <linux/build_bug.h>
4+
5+
/*
6+
* `bindgen` binds the C `size_t` type as the Rust `usize` type, so we can
7+
* use it in contexts where Rust expects a `usize` like slice (array) indices.
8+
* `usize` is defined to be the same as C's `uintptr_t` type (can hold any
9+
* pointer) but not necessarily the same as `size_t` (can hold the size of any
10+
* single object). Most modern platforms use the same concrete integer type for
11+
* both of them, but in case we find ourselves on a platform where
12+
* that's not true, fail early instead of risking ABI or
13+
* integer-overflow issues.
14+
*
15+
* If your platform fails this assertion, it means that you are in
16+
* danger of integer-overflow bugs (even if you attempt to add
17+
* `--no-size_t-is-usize`). It may be easiest to change the kernel ABI on
18+
* your platform such that `size_t` matches `uintptr_t` (i.e., to increase
19+
* `size_t`, because `uintptr_t` has to be at least as big as `size_t`).
20+
*/
21+
static_assert(
22+
sizeof(size_t) == sizeof(uintptr_t) &&
23+
__alignof__(size_t) == __alignof__(uintptr_t),
24+
"Rust code expects C `size_t` to match Rust `usize`"
25+
);

rust/helpers/build_bug.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
#include <linux/export.h>
4+
#include <linux/errname.h>
5+
6+
const char *rust_helper_errname(int err)
7+
{
8+
return errname(err);
9+
}
10+
EXPORT_SYMBOL_GPL(rust_helper_errname);

rust/helpers/err.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
#include <linux/err.h>
4+
#include <linux/export.h>
5+
6+
__force void *rust_helper_ERR_PTR(long err)
7+
{
8+
return ERR_PTR(err);
9+
}
10+
EXPORT_SYMBOL_GPL(rust_helper_ERR_PTR);
11+
12+
bool rust_helper_IS_ERR(__force const void *ptr)
13+
{
14+
return IS_ERR(ptr);
15+
}
16+
EXPORT_SYMBOL_GPL(rust_helper_IS_ERR);
17+
18+
long rust_helper_PTR_ERR(__force const void *ptr)
19+
{
20+
return PTR_ERR(ptr);
21+
}
22+
EXPORT_SYMBOL_GPL(rust_helper_PTR_ERR);

0 commit comments

Comments
 (0)