Skip to content

Commit e26fa54

Browse files
nbdd0121ojeda
authored andcommitted
rust: kbuild: auto generate helper exports
This removes the need to explicitly export all symbols. Generate helper exports similarly to what's currently done for Rust crates. These helpers are exclusively called from within Rust code and therefore can be treated similar as other Rust symbols. Signed-off-by: Gary Guo <gary@garyguo.net> Reviewed-by: Boqun Feng <boqun.feng@gmail.com> Tested-by: Boqun Feng <boqun.feng@gmail.com> Link: https://lore.kernel.org/r/20240817165302.3852499-1-gary@garyguo.net [ Fixed dependency path, reworded slightly, edited comment a bit and rebased on top of the changes made when applying Andreas' patch (e.g. no `README.md` anymore, so moved the edits). - Miguel ] Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
1 parent c4d7f54 commit e26fa54

File tree

18 files changed

+15
-42
lines changed

18 files changed

+15
-42
lines changed

rust/Makefile

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ no-clean-files += libmacros.so
1616

1717
always-$(CONFIG_RUST) += bindings/bindings_generated.rs bindings/bindings_helpers_generated.rs
1818
obj-$(CONFIG_RUST) += alloc.o bindings.o kernel.o
19-
always-$(CONFIG_RUST) += exports_alloc_generated.h exports_bindings_generated.h \
20-
exports_kernel_generated.h
19+
always-$(CONFIG_RUST) += exports_alloc_generated.h exports_helpers_generated.h \
20+
exports_bindings_generated.h exports_kernel_generated.h
2121

2222
always-$(CONFIG_RUST) += uapi/uapi_generated.rs
2323
obj-$(CONFIG_RUST) += uapi.o
@@ -313,6 +313,18 @@ $(obj)/exports_core_generated.h: $(obj)/core.o FORCE
313313
$(obj)/exports_alloc_generated.h: $(obj)/alloc.o FORCE
314314
$(call if_changed,exports)
315315

316+
# Even though Rust kernel modules should never use the bindings directly,
317+
# symbols from the `bindings` crate and the C helpers need to be exported
318+
# because Rust generics and inlined functions may not get their code generated
319+
# in the crate where they are defined. Other helpers, called from non-inline
320+
# functions, may not be exported, in principle. However, in general, the Rust
321+
# compiler does not guarantee codegen will be performed for a non-inline
322+
# function either. Therefore, we export all symbols from helpers and bindings.
323+
# In the future, this may be revisited to reduce the number of exports after
324+
# the compiler is informed about the places codegen is required.
325+
$(obj)/exports_helpers_generated.h: $(obj)/helpers/helpers.o FORCE
326+
$(call if_changed,exports)
327+
316328
$(obj)/exports_bindings_generated.h: $(obj)/bindings.o FORCE
317329
$(call if_changed,exports)
318330

rust/exports.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
#include "exports_core_generated.h"
1919
#include "exports_alloc_generated.h"
20+
#include "exports_helpers_generated.h"
2021
#include "exports_bindings_generated.h"
2122
#include "exports_kernel_generated.h"
2223

rust/helpers/blk.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,8 @@ void *rust_helper_blk_mq_rq_to_pdu(struct request *rq)
77
{
88
return blk_mq_rq_to_pdu(rq);
99
}
10-
EXPORT_SYMBOL_GPL(rust_helper_blk_mq_rq_to_pdu);
1110

1211
struct request *rust_helper_blk_mq_rq_from_pdu(void *pdu)
1312
{
1413
return blk_mq_rq_from_pdu(pdu);
1514
}
16-
EXPORT_SYMBOL_GPL(rust_helper_blk_mq_rq_from_pdu);

rust/helpers/bug.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,3 @@ __noreturn void rust_helper_BUG(void)
66
{
77
BUG();
88
}
9-
EXPORT_SYMBOL_GPL(rust_helper_BUG);

rust/helpers/build_bug.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,3 @@ const char *rust_helper_errname(int err)
77
{
88
return errname(err);
99
}
10-
EXPORT_SYMBOL_GPL(rust_helper_errname);

rust/helpers/err.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,13 @@ __force void *rust_helper_ERR_PTR(long err)
77
{
88
return ERR_PTR(err);
99
}
10-
EXPORT_SYMBOL_GPL(rust_helper_ERR_PTR);
1110

1211
bool rust_helper_IS_ERR(__force const void *ptr)
1312
{
1413
return IS_ERR(ptr);
1514
}
16-
EXPORT_SYMBOL_GPL(rust_helper_IS_ERR);
1715

1816
long rust_helper_PTR_ERR(__force const void *ptr)
1917
{
2018
return PTR_ERR(ptr);
2119
}
22-
EXPORT_SYMBOL_GPL(rust_helper_PTR_ERR);

rust/helpers/helpers.c

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,6 @@
44
* cannot be called either. This file explicitly creates functions ("helpers")
55
* that wrap those so that they can be called from Rust.
66
*
7-
* Even though Rust kernel modules should never use the bindings directly, 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-
*
207
* Sorted alphabetically.
218
*/
229

rust/helpers/kunit.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,3 @@ struct kunit *rust_helper_kunit_get_current_test(void)
77
{
88
return kunit_get_current_test();
99
}
10-
EXPORT_SYMBOL_GPL(rust_helper_kunit_get_current_test);

rust/helpers/mutex.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,3 @@ void rust_helper_mutex_lock(struct mutex *lock)
77
{
88
mutex_lock(lock);
99
}
10-
EXPORT_SYMBOL_GPL(rust_helper_mutex_lock);

rust/helpers/page.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,13 @@ struct page *rust_helper_alloc_pages(gfp_t gfp_mask, unsigned int order)
77
{
88
return alloc_pages(gfp_mask, order);
99
}
10-
EXPORT_SYMBOL_GPL(rust_helper_alloc_pages);
1110

1211
void *rust_helper_kmap_local_page(struct page *page)
1312
{
1413
return kmap_local_page(page);
1514
}
16-
EXPORT_SYMBOL_GPL(rust_helper_kmap_local_page);
1715

1816
void rust_helper_kunmap_local(const void *addr)
1917
{
2018
kunmap_local(addr);
2119
}
22-
EXPORT_SYMBOL_GPL(rust_helper_kunmap_local);

0 commit comments

Comments
 (0)