Skip to content

Commit 0f6279b

Browse files
metaspacematthewtgilbride
authored andcommitted
kbuild: rust: 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. Link: https://rust-for-linux.zulipchat.com/#narrow/stream/288089-General/topic/Splitting.20up.20helpers.2Ec/near/426694012 [1] Signed-off-by: Andreas Hindborg <a.hindborg@samsung.com> Reviewed-by: Benno Lossin <benno.lossin@proton.me> Acked-by: Dirk Behme <dirk.behme@de.bosch.com> Reviewed-by: Alice Ryhl <aliceryhl@google.com> Reviewed-by: Gary Guo <gary@garyguo.net>
1 parent 7c626ce commit 0f6279b

20 files changed

Lines changed: 298 additions & 242 deletions

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/README.md

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

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);

rust/helpers/helpers.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
#include "blk.c"
4+
#include "bug.c"
5+
#include "build_assert.c"
6+
#include "build_bug.c"
7+
#include "err.c"
8+
#include "kunit.c"
9+
#include "mutex.c"
10+
#include "page.c"
11+
#include "refcount.c"
12+
#include "signal.c"
13+
#include "slab.c"
14+
#include "spinlock.c"
15+
#include "task.c"
16+
#include "uaccess.c"
17+
#include "wait.c"
18+
#include "workqueue.c"

rust/helpers/kunit.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 <kunit/test-bug.h>
4+
#include <linux/export.h>
5+
6+
struct kunit *rust_helper_kunit_get_current_test(void)
7+
{
8+
return kunit_get_current_test();
9+
}
10+
EXPORT_SYMBOL_GPL(rust_helper_kunit_get_current_test);

0 commit comments

Comments
 (0)