[RFC PATCH 1/2] kbuild: rust: auto generate rust helper exports

From: Gary Guo
Date: Wed May 29 2024 - 16:30:38 EST


Generate helper exports similar 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.

This removes the need for explicit exporting all symbols. This change
also makes it easier to support inlining these symbols directly to Rust
crates without them being exported at all.

Signed-off-by: Gary Guo <gary@xxxxxxxxxxx>
---
rust/Makefile | 5 ++++-
rust/exports.c | 1 +
rust/helpers.c | 20 --------------------
3 files changed, 5 insertions(+), 21 deletions(-)

diff --git a/rust/Makefile b/rust/Makefile
index f70d5e244fee..b4d63ea9209f 100644
--- a/rust/Makefile
+++ b/rust/Makefile
@@ -17,7 +17,7 @@ no-clean-files += libmacros.so
always-$(CONFIG_RUST) += bindings/bindings_generated.rs bindings/bindings_helpers_generated.rs
obj-$(CONFIG_RUST) += alloc.o bindings.o kernel.o
always-$(CONFIG_RUST) += exports_alloc_generated.h exports_bindings_generated.h \
- exports_kernel_generated.h
+ exports_kernel_generated.h exports_helpers_generated.h

always-$(CONFIG_RUST) += uapi/uapi_generated.rs
obj-$(CONFIG_RUST) += uapi.o
@@ -373,6 +373,9 @@ $(obj)/exports_bindings_generated.h: $(obj)/bindings.o FORCE
$(obj)/exports_kernel_generated.h: $(obj)/kernel.o FORCE
$(call if_changed,exports)

+$(obj)/exports_helpers_generated.h: $(obj)/helpers.o FORCE
+ $(call if_changed,exports)
+
quiet_cmd_rustc_procmacro = $(RUSTC_OR_CLIPPY_QUIET) P $@
cmd_rustc_procmacro = \
$(RUSTC_OR_CLIPPY) $(rust_common_flags) \
diff --git a/rust/exports.c b/rust/exports.c
index 3803c21d1403..aa1218b325e5 100644
--- a/rust/exports.c
+++ b/rust/exports.c
@@ -19,6 +19,7 @@
#include "exports_alloc_generated.h"
#include "exports_bindings_generated.h"
#include "exports_kernel_generated.h"
+#include "exports_helpers_generated.h"

// For modules using `rust/build_error.rs`.
#ifdef CONFIG_RUST_BUILD_ASSERT_ALLOW
diff --git a/rust/helpers.c b/rust/helpers.c
index 2c37a0f5d7a8..895f4b696962 100644
--- a/rust/helpers.c
+++ b/rust/helpers.c
@@ -37,13 +37,11 @@ __noreturn void rust_helper_BUG(void)
{
BUG();
}
-EXPORT_SYMBOL_GPL(rust_helper_BUG);

void rust_helper_mutex_lock(struct mutex *lock)
{
mutex_lock(lock);
}
-EXPORT_SYMBOL_GPL(rust_helper_mutex_lock);

void rust_helper___spin_lock_init(spinlock_t *lock, const char *name,
struct lock_class_key *key)
@@ -54,97 +52,81 @@ void rust_helper___spin_lock_init(spinlock_t *lock, const char *name,
spin_lock_init(lock);
#endif
}
-EXPORT_SYMBOL_GPL(rust_helper___spin_lock_init);

void rust_helper_spin_lock(spinlock_t *lock)
{
spin_lock(lock);
}
-EXPORT_SYMBOL_GPL(rust_helper_spin_lock);

void rust_helper_spin_unlock(spinlock_t *lock)
{
spin_unlock(lock);
}
-EXPORT_SYMBOL_GPL(rust_helper_spin_unlock);

void rust_helper_init_wait(struct wait_queue_entry *wq_entry)
{
init_wait(wq_entry);
}
-EXPORT_SYMBOL_GPL(rust_helper_init_wait);

int rust_helper_signal_pending(struct task_struct *t)
{
return signal_pending(t);
}
-EXPORT_SYMBOL_GPL(rust_helper_signal_pending);

refcount_t rust_helper_REFCOUNT_INIT(int n)
{
return (refcount_t)REFCOUNT_INIT(n);
}
-EXPORT_SYMBOL_GPL(rust_helper_REFCOUNT_INIT);

void rust_helper_refcount_inc(refcount_t *r)
{
refcount_inc(r);
}
-EXPORT_SYMBOL_GPL(rust_helper_refcount_inc);

bool rust_helper_refcount_dec_and_test(refcount_t *r)
{
return refcount_dec_and_test(r);
}
-EXPORT_SYMBOL_GPL(rust_helper_refcount_dec_and_test);

__force void *rust_helper_ERR_PTR(long err)
{
return ERR_PTR(err);
}
-EXPORT_SYMBOL_GPL(rust_helper_ERR_PTR);

bool rust_helper_IS_ERR(__force const void *ptr)
{
return IS_ERR(ptr);
}
-EXPORT_SYMBOL_GPL(rust_helper_IS_ERR);

long rust_helper_PTR_ERR(__force const void *ptr)
{
return PTR_ERR(ptr);
}
-EXPORT_SYMBOL_GPL(rust_helper_PTR_ERR);

const char *rust_helper_errname(int err)
{
return errname(err);
}
-EXPORT_SYMBOL_GPL(rust_helper_errname);

struct task_struct *rust_helper_get_current(void)
{
return current;
}
-EXPORT_SYMBOL_GPL(rust_helper_get_current);

void rust_helper_get_task_struct(struct task_struct *t)
{
get_task_struct(t);
}
-EXPORT_SYMBOL_GPL(rust_helper_get_task_struct);

void rust_helper_put_task_struct(struct task_struct *t)
{
put_task_struct(t);
}
-EXPORT_SYMBOL_GPL(rust_helper_put_task_struct);

struct kunit *rust_helper_kunit_get_current_test(void)
{
return kunit_get_current_test();
}
-EXPORT_SYMBOL_GPL(rust_helper_kunit_get_current_test);

void rust_helper_init_work_with_key(struct work_struct *work, work_func_t func,
bool onstack, const char *name,
@@ -156,14 +138,12 @@ void rust_helper_init_work_with_key(struct work_struct *work, work_func_t func,
INIT_LIST_HEAD(&work->entry);
work->func = func;
}
-EXPORT_SYMBOL_GPL(rust_helper_init_work_with_key);

void * __must_check __realloc_size(2)
rust_helper_krealloc(const void *objp, size_t new_size, gfp_t flags)
{
return krealloc(objp, new_size, flags);
}
-EXPORT_SYMBOL_GPL(rust_helper_krealloc);

/*
* `bindgen` binds the C `size_t` type as the Rust `usize` type, so we can
--
2.42.0