Commit e26fa546 authored by Gary Guo's avatar Gary Guo Committed by Miguel Ojeda

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: default avatarGary Guo <gary@garyguo.net>
Reviewed-by: default avatarBoqun Feng <boqun.feng@gmail.com>
Tested-by: default avatarBoqun 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: default avatarMiguel Ojeda <ojeda@kernel.org>
parent c4d7f546
...@@ -16,8 +16,8 @@ no-clean-files += libmacros.so ...@@ -16,8 +16,8 @@ no-clean-files += libmacros.so
always-$(CONFIG_RUST) += bindings/bindings_generated.rs bindings/bindings_helpers_generated.rs always-$(CONFIG_RUST) += bindings/bindings_generated.rs bindings/bindings_helpers_generated.rs
obj-$(CONFIG_RUST) += alloc.o bindings.o kernel.o obj-$(CONFIG_RUST) += alloc.o bindings.o kernel.o
always-$(CONFIG_RUST) += exports_alloc_generated.h exports_bindings_generated.h \ always-$(CONFIG_RUST) += exports_alloc_generated.h exports_helpers_generated.h \
exports_kernel_generated.h exports_bindings_generated.h exports_kernel_generated.h
always-$(CONFIG_RUST) += uapi/uapi_generated.rs always-$(CONFIG_RUST) += uapi/uapi_generated.rs
obj-$(CONFIG_RUST) += uapi.o obj-$(CONFIG_RUST) += uapi.o
...@@ -313,6 +313,18 @@ $(obj)/exports_core_generated.h: $(obj)/core.o FORCE ...@@ -313,6 +313,18 @@ $(obj)/exports_core_generated.h: $(obj)/core.o FORCE
$(obj)/exports_alloc_generated.h: $(obj)/alloc.o FORCE $(obj)/exports_alloc_generated.h: $(obj)/alloc.o FORCE
$(call if_changed,exports) $(call if_changed,exports)
# Even though Rust kernel modules should never use the bindings directly,
# symbols from the `bindings` crate and the C helpers need to be exported
# because Rust generics and inlined functions may not get their code generated
# in the crate where they are defined. Other helpers, called from non-inline
# functions, may not be exported, in principle. However, in general, the Rust
# compiler does not guarantee codegen will be performed for a non-inline
# function either. Therefore, we export all symbols from helpers and bindings.
# In the future, this may be revisited to reduce the number of exports after
# the compiler is informed about the places codegen is required.
$(obj)/exports_helpers_generated.h: $(obj)/helpers/helpers.o FORCE
$(call if_changed,exports)
$(obj)/exports_bindings_generated.h: $(obj)/bindings.o FORCE $(obj)/exports_bindings_generated.h: $(obj)/bindings.o FORCE
$(call if_changed,exports) $(call if_changed,exports)
......
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
#include "exports_core_generated.h" #include "exports_core_generated.h"
#include "exports_alloc_generated.h" #include "exports_alloc_generated.h"
#include "exports_helpers_generated.h"
#include "exports_bindings_generated.h" #include "exports_bindings_generated.h"
#include "exports_kernel_generated.h" #include "exports_kernel_generated.h"
......
...@@ -7,10 +7,8 @@ void *rust_helper_blk_mq_rq_to_pdu(struct request *rq) ...@@ -7,10 +7,8 @@ void *rust_helper_blk_mq_rq_to_pdu(struct request *rq)
{ {
return blk_mq_rq_to_pdu(rq); return blk_mq_rq_to_pdu(rq);
} }
EXPORT_SYMBOL_GPL(rust_helper_blk_mq_rq_to_pdu);
struct request *rust_helper_blk_mq_rq_from_pdu(void *pdu) struct request *rust_helper_blk_mq_rq_from_pdu(void *pdu)
{ {
return blk_mq_rq_from_pdu(pdu); return blk_mq_rq_from_pdu(pdu);
} }
EXPORT_SYMBOL_GPL(rust_helper_blk_mq_rq_from_pdu);
...@@ -6,4 +6,3 @@ __noreturn void rust_helper_BUG(void) ...@@ -6,4 +6,3 @@ __noreturn void rust_helper_BUG(void)
{ {
BUG(); BUG();
} }
EXPORT_SYMBOL_GPL(rust_helper_BUG);
...@@ -7,4 +7,3 @@ const char *rust_helper_errname(int err) ...@@ -7,4 +7,3 @@ const char *rust_helper_errname(int err)
{ {
return errname(err); return errname(err);
} }
EXPORT_SYMBOL_GPL(rust_helper_errname);
...@@ -7,16 +7,13 @@ __force void *rust_helper_ERR_PTR(long err) ...@@ -7,16 +7,13 @@ __force void *rust_helper_ERR_PTR(long err)
{ {
return ERR_PTR(err); return ERR_PTR(err);
} }
EXPORT_SYMBOL_GPL(rust_helper_ERR_PTR);
bool rust_helper_IS_ERR(__force const void *ptr) bool rust_helper_IS_ERR(__force const void *ptr)
{ {
return IS_ERR(ptr); return IS_ERR(ptr);
} }
EXPORT_SYMBOL_GPL(rust_helper_IS_ERR);
long rust_helper_PTR_ERR(__force const void *ptr) long rust_helper_PTR_ERR(__force const void *ptr)
{ {
return PTR_ERR(ptr); return PTR_ERR(ptr);
} }
EXPORT_SYMBOL_GPL(rust_helper_PTR_ERR);
...@@ -4,19 +4,6 @@ ...@@ -4,19 +4,6 @@
* cannot be called either. This file explicitly creates functions ("helpers") * cannot be called either. This file explicitly creates functions ("helpers")
* that wrap those so that they can be called from Rust. * that wrap those so that they can be called from Rust.
* *
* Even though Rust kernel modules should never use the bindings directly, some
* of these helpers need to be exported because Rust generics and inlined
* functions may not get their code generated in the crate where they are
* defined. Other helpers, called from non-inline functions, may not be
* exported, in principle. However, in general, the Rust compiler does not
* guarantee codegen will be performed for a non-inline function either.
* Therefore, this file exports all the helpers. In the future, this may be
* revisited to reduce the number of exports after the compiler is informed
* about the places codegen is required.
*
* All symbols are exported as GPL-only to guarantee no GPL-only feature is
* accidentally exposed.
*
* Sorted alphabetically. * Sorted alphabetically.
*/ */
......
...@@ -7,4 +7,3 @@ struct kunit *rust_helper_kunit_get_current_test(void) ...@@ -7,4 +7,3 @@ struct kunit *rust_helper_kunit_get_current_test(void)
{ {
return kunit_get_current_test(); return kunit_get_current_test();
} }
EXPORT_SYMBOL_GPL(rust_helper_kunit_get_current_test);
...@@ -7,4 +7,3 @@ void rust_helper_mutex_lock(struct mutex *lock) ...@@ -7,4 +7,3 @@ void rust_helper_mutex_lock(struct mutex *lock)
{ {
mutex_lock(lock); mutex_lock(lock);
} }
EXPORT_SYMBOL_GPL(rust_helper_mutex_lock);
...@@ -7,16 +7,13 @@ struct page *rust_helper_alloc_pages(gfp_t gfp_mask, unsigned int order) ...@@ -7,16 +7,13 @@ struct page *rust_helper_alloc_pages(gfp_t gfp_mask, unsigned int order)
{ {
return alloc_pages(gfp_mask, order); return alloc_pages(gfp_mask, order);
} }
EXPORT_SYMBOL_GPL(rust_helper_alloc_pages);
void *rust_helper_kmap_local_page(struct page *page) void *rust_helper_kmap_local_page(struct page *page)
{ {
return kmap_local_page(page); return kmap_local_page(page);
} }
EXPORT_SYMBOL_GPL(rust_helper_kmap_local_page);
void rust_helper_kunmap_local(const void *addr) void rust_helper_kunmap_local(const void *addr)
{ {
kunmap_local(addr); kunmap_local(addr);
} }
EXPORT_SYMBOL_GPL(rust_helper_kunmap_local);
...@@ -7,16 +7,13 @@ refcount_t rust_helper_REFCOUNT_INIT(int n) ...@@ -7,16 +7,13 @@ refcount_t rust_helper_REFCOUNT_INIT(int n)
{ {
return (refcount_t)REFCOUNT_INIT(n); return (refcount_t)REFCOUNT_INIT(n);
} }
EXPORT_SYMBOL_GPL(rust_helper_REFCOUNT_INIT);
void rust_helper_refcount_inc(refcount_t *r) void rust_helper_refcount_inc(refcount_t *r)
{ {
refcount_inc(r); refcount_inc(r);
} }
EXPORT_SYMBOL_GPL(rust_helper_refcount_inc);
bool rust_helper_refcount_dec_and_test(refcount_t *r) bool rust_helper_refcount_dec_and_test(refcount_t *r)
{ {
return refcount_dec_and_test(r); return refcount_dec_and_test(r);
} }
EXPORT_SYMBOL_GPL(rust_helper_refcount_dec_and_test);
...@@ -7,4 +7,3 @@ int rust_helper_signal_pending(struct task_struct *t) ...@@ -7,4 +7,3 @@ int rust_helper_signal_pending(struct task_struct *t)
{ {
return signal_pending(t); return signal_pending(t);
} }
EXPORT_SYMBOL_GPL(rust_helper_signal_pending);
...@@ -7,4 +7,3 @@ rust_helper_krealloc(const void *objp, size_t new_size, gfp_t flags) ...@@ -7,4 +7,3 @@ rust_helper_krealloc(const void *objp, size_t new_size, gfp_t flags)
{ {
return krealloc(objp, new_size, flags); return krealloc(objp, new_size, flags);
} }
EXPORT_SYMBOL_GPL(rust_helper_krealloc);
...@@ -12,16 +12,13 @@ void rust_helper___spin_lock_init(spinlock_t *lock, const char *name, ...@@ -12,16 +12,13 @@ void rust_helper___spin_lock_init(spinlock_t *lock, const char *name,
spin_lock_init(lock); spin_lock_init(lock);
#endif #endif
} }
EXPORT_SYMBOL_GPL(rust_helper___spin_lock_init);
void rust_helper_spin_lock(spinlock_t *lock) void rust_helper_spin_lock(spinlock_t *lock)
{ {
spin_lock(lock); spin_lock(lock);
} }
EXPORT_SYMBOL_GPL(rust_helper_spin_lock);
void rust_helper_spin_unlock(spinlock_t *lock) void rust_helper_spin_unlock(spinlock_t *lock)
{ {
spin_unlock(lock); spin_unlock(lock);
} }
EXPORT_SYMBOL_GPL(rust_helper_spin_unlock);
...@@ -7,16 +7,13 @@ struct task_struct *rust_helper_get_current(void) ...@@ -7,16 +7,13 @@ struct task_struct *rust_helper_get_current(void)
{ {
return current; return current;
} }
EXPORT_SYMBOL_GPL(rust_helper_get_current);
void rust_helper_get_task_struct(struct task_struct *t) void rust_helper_get_task_struct(struct task_struct *t)
{ {
get_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) void rust_helper_put_task_struct(struct task_struct *t)
{ {
put_task_struct(t); put_task_struct(t);
} }
EXPORT_SYMBOL_GPL(rust_helper_put_task_struct);
...@@ -7,11 +7,9 @@ unsigned long rust_helper_copy_from_user(void *to, const void __user *from, ...@@ -7,11 +7,9 @@ unsigned long rust_helper_copy_from_user(void *to, const void __user *from,
{ {
return copy_from_user(to, from, n); return copy_from_user(to, from, n);
} }
EXPORT_SYMBOL_GPL(rust_helper_copy_from_user);
unsigned long rust_helper_copy_to_user(void __user *to, const void *from, unsigned long rust_helper_copy_to_user(void __user *to, const void *from,
unsigned long n) unsigned long n)
{ {
return copy_to_user(to, from, n); return copy_to_user(to, from, n);
} }
EXPORT_SYMBOL_GPL(rust_helper_copy_to_user);
...@@ -7,4 +7,3 @@ void rust_helper_init_wait(struct wait_queue_entry *wq_entry) ...@@ -7,4 +7,3 @@ void rust_helper_init_wait(struct wait_queue_entry *wq_entry)
{ {
init_wait(wq_entry); init_wait(wq_entry);
} }
EXPORT_SYMBOL_GPL(rust_helper_init_wait);
...@@ -13,4 +13,3 @@ void rust_helper_init_work_with_key(struct work_struct *work, work_func_t func, ...@@ -13,4 +13,3 @@ void rust_helper_init_work_with_key(struct work_struct *work, work_func_t func,
INIT_LIST_HEAD(&work->entry); INIT_LIST_HEAD(&work->entry);
work->func = func; work->func = func;
} }
EXPORT_SYMBOL_GPL(rust_helper_init_work_with_key);
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment