mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] rust: export zerocopy symbols to modules
@ 2026-07-08 10:37 Alexandre Courbot
  2026-07-08 10:50 ` Miguel Ojeda
  2026-07-08 11:34 ` Gary Guo
  0 siblings, 2 replies; 6+ messages in thread
From: Alexandre Courbot @ 2026-07-08 10:37 UTC (permalink / raw)
  To: Miguel Ojeda, Boqun Feng, Gary Guo, Björn Roy Baron,
	Benno Lossin, Andreas Hindborg, Alice Ryhl, Trevor Gross,
	Danilo Krummrich, Daniel Almeida, Tamir Duberstein,
	Onur Özkan
  Cc: rust-for-linux, linux-kernel, Alexandre Courbot

Non-generic, non-inlined functions of the `zerocopy` crate are only
codegen'd into `zerocopy.o`, with their symbols currently not exported,
making them unavailable to loadable modules.

This can break e.g. nova-core built as a module when
`CONFIG_CC_OPTIMIZE_FOR_SIZE=y`:

    ERROR: modpost: "_RNvMNtCs5wX7wwEUCR9_8zerocopy6layoutNtB2_8SizeInfo24try_to_nonzero_elem_size" [drivers/gpu/nova-core.ko] undefined!
    ERROR: modpost: "_RNvNtCs5wX7wwEUCR9_8zerocopy4util18padding_needed_for" [drivers/gpu/nova-core.ko] undefined!

Fix this by generating and including exports for `zerocopy.o`, similarly
to what is already done for `kernel.o`. All defined symbols (~12 in
total) are exported since the compiler gives no guarantee as to which
functions are codegen'd.

Signed-off-by: Alexandre Courbot <acourbot@nvidia.com>
---
My `CONFIG_CC_OPTIMIZE_FOR_SIZE=y` config is uncovering the most
interesting bugs. :) This one was revealed after applying [1].

I am not a build system expert at all, so be aware that the patch is
mostly mimicking what I am seeing for the kernel crate and there might
be a better way.

[1] https://lore.kernel.org/20260702120324.40388-4-niyudi.honda@gmail.com
---
 rust/Makefile  | 4 ++++
 rust/exports.c | 1 +
 2 files changed, 5 insertions(+)

diff --git a/rust/Makefile b/rust/Makefile
index a870d1616c71..65170c6d31a4 100644
--- a/rust/Makefile
+++ b/rust/Makefile
@@ -7,6 +7,7 @@ obj-$(CONFIG_RUST) += core.o compiler_builtins.o ffi.o
 always-$(CONFIG_RUST) += exports_core_generated.h
 
 obj-$(CONFIG_RUST) += zerocopy.o
+always-$(CONFIG_RUST) += exports_zerocopy_generated.h
 
 ifdef CONFIG_RUST_INLINE_HELPERS
 always-$(CONFIG_RUST) += helpers/helpers.bc helpers/helpers_module.bc
@@ -566,6 +567,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_zerocopy_generated.h: $(obj)/zerocopy.o FORCE
+	$(call if_changed,exports)
+
 quiet_cmd_rustc_procmacrolibrary = $(if $(skip_clippy),RUSTC,$(RUSTC_OR_CLIPPY_QUIET)) PL $@
       cmd_rustc_procmacrolibrary = \
 	$(rustc_target_envs) \
diff --git a/rust/exports.c b/rust/exports.c
index 1b52460b0f4e..a9d6be8e8485 100644
--- a/rust/exports.c
+++ b/rust/exports.c
@@ -18,6 +18,7 @@
 #include "exports_core_generated.h"
 #include "exports_bindings_generated.h"
 #include "exports_kernel_generated.h"
+#include "exports_zerocopy_generated.h"
 
 #ifndef CONFIG_RUST_INLINE_HELPERS
 #include "exports_helpers_generated.h"

---
base-commit: dc59e4fea9d83f03bad6bddf3fa2e52491777482
change-id: 20260708-zerocopy-export-e6d29c42386c

Best regards,
--  
Alexandre Courbot <acourbot@nvidia.com>


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] rust: export zerocopy symbols to modules
  2026-07-08 10:37 [PATCH] rust: export zerocopy symbols to modules Alexandre Courbot
@ 2026-07-08 10:50 ` Miguel Ojeda
  2026-07-08 11:34 ` Gary Guo
  1 sibling, 0 replies; 6+ messages in thread
From: Miguel Ojeda @ 2026-07-08 10:50 UTC (permalink / raw)
  To: Alexandre Courbot
  Cc: Miguel Ojeda, Boqun Feng, Gary Guo, Björn Roy Baron,
	Benno Lossin, Andreas Hindborg, Alice Ryhl, Trevor Gross,
	Danilo Krummrich, Daniel Almeida, Tamir Duberstein,
	Onur Özkan, rust-for-linux, linux-kernel

On Wed, Jul 8, 2026 at 12:37 PM Alexandre Courbot <acourbot@nvidia.com> wrote:
>
> Non-generic, non-inlined functions of the `zerocopy` crate are only
> codegen'd into `zerocopy.o`, with their symbols currently not exported,
> making them unavailable to loadable modules.

Ah, in that case, yeah, we definitely need this.

> My `CONFIG_CC_OPTIMIZE_FOR_SIZE=y` config is uncovering the most
> interesting bugs. :) This one was revealed after applying [1].

Yeah, it is a bit painful. I will add it to my set.

> I am not a build system expert at all, so be aware that the patch is

No one really is... :) (Unless you are named Masahiro!)

I think I will route this one via `rust-fixes`.

Cheers,
Miguel

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] rust: export zerocopy symbols to modules
  2026-07-08 10:37 [PATCH] rust: export zerocopy symbols to modules Alexandre Courbot
  2026-07-08 10:50 ` Miguel Ojeda
@ 2026-07-08 11:34 ` Gary Guo
  2026-07-08 11:37   ` Miguel Ojeda
  2026-07-08 11:51   ` Gary Guo
  1 sibling, 2 replies; 6+ messages in thread
From: Gary Guo @ 2026-07-08 11:34 UTC (permalink / raw)
  To: Alexandre Courbot, Miguel Ojeda, Boqun Feng, Gary Guo,
	Björn Roy Baron, Benno Lossin, Andreas Hindborg, Alice Ryhl,
	Trevor Gross, Danilo Krummrich, Daniel Almeida, Tamir Duberstein,
	Onur Özkan
  Cc: rust-for-linux, linux-kernel

On Wed Jul 8, 2026 at 11:37 AM BST, Alexandre Courbot wrote:
> Non-generic, non-inlined functions of the `zerocopy` crate are only
> codegen'd into `zerocopy.o`, with their symbols currently not exported,
> making them unavailable to loadable modules.
>
> This can break e.g. nova-core built as a module when
> `CONFIG_CC_OPTIMIZE_FOR_SIZE=y`:
>
>     ERROR: modpost: "_RNvMNtCs5wX7wwEUCR9_8zerocopy6layoutNtB2_8SizeInfo24try_to_nonzero_elem_size" [drivers/gpu/nova-core.ko] undefined!
>     ERROR: modpost: "_RNvNtCs5wX7wwEUCR9_8zerocopy4util18padding_needed_for" [drivers/gpu/nova-core.ko] undefined!

It looks to me that this should be done by fixing zerocopy.

Both these functions should be marked as `#[inline]`. I would expect zerocopy.o
to be having no .text.

Instead of exporting this, I would vouch for going to another extreme --
completely leave zerocopy.o unlinked, so we catch cases where code is actually
generated.

Best,
Gary


>
> Fix this by generating and including exports for `zerocopy.o`, similarly
> to what is already done for `kernel.o`. All defined symbols (~12 in
> total) are exported since the compiler gives no guarantee as to which
> functions are codegen'd.
>
> Signed-off-by: Alexandre Courbot <acourbot@nvidia.com>
> ---
> My `CONFIG_CC_OPTIMIZE_FOR_SIZE=y` config is uncovering the most
> interesting bugs. :) This one was revealed after applying [1].
>
> I am not a build system expert at all, so be aware that the patch is
> mostly mimicking what I am seeing for the kernel crate and there might
> be a better way.
>
> [1] https://lore.kernel.org/20260702120324.40388-4-niyudi.honda@gmail.com
> ---
>  rust/Makefile  | 4 ++++
>  rust/exports.c | 1 +
>  2 files changed, 5 insertions(+)


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] rust: export zerocopy symbols to modules
  2026-07-08 11:34 ` Gary Guo
@ 2026-07-08 11:37   ` Miguel Ojeda
       [not found]     ` <CAPvrYMF3L0x5PBJk2TnSPyi0Lci2H0vGBu6QS_Rm4XFQtTV6mw@mail.gmail.com>
  2026-07-08 11:51   ` Gary Guo
  1 sibling, 1 reply; 6+ messages in thread
From: Miguel Ojeda @ 2026-07-08 11:37 UTC (permalink / raw)
  To: Gary Guo, Joshua Liebow-Feeser, Jack Wrenn
  Cc: Alexandre Courbot, Miguel Ojeda, Boqun Feng,
	Björn Roy Baron, Benno Lossin, Andreas Hindborg, Alice Ryhl,
	Trevor Gross, Danilo Krummrich, Daniel Almeida, Tamir Duberstein,
	Onur Özkan, rust-for-linux, linux-kernel

On Wed, Jul 8, 2026 at 1:34 PM Gary Guo <gary@garyguo.net> wrote:
>
> It looks to me that this should be done by fixing zerocopy.
>
> Both these functions should be marked as `#[inline]`. I would expect zerocopy.o
> to be having no .text.
>
> Instead of exporting this, I would vouch for going to another extreme --
> completely leave zerocopy.o unlinked, so we catch cases where code is actually
> generated.

Cc'ing Joshua and Jack -- context here:

  https://lore.kernel.org/rust-for-linux/20260708-zerocopy-export-v1-1-2bfc355853c6@nvidia.com/

Cheers,
Miguel

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] rust: export zerocopy symbols to modules
  2026-07-08 11:34 ` Gary Guo
  2026-07-08 11:37   ` Miguel Ojeda
@ 2026-07-08 11:51   ` Gary Guo
  1 sibling, 0 replies; 6+ messages in thread
From: Gary Guo @ 2026-07-08 11:51 UTC (permalink / raw)
  To: Gary Guo, Alexandre Courbot, Miguel Ojeda, Boqun Feng,
	Björn Roy Baron, Benno Lossin, Andreas Hindborg, Alice Ryhl,
	Trevor Gross, Danilo Krummrich, Daniel Almeida, Tamir Duberstein,
	Onur Özkan
  Cc: rust-for-linux, linux-kernel

On Wed Jul 8, 2026 at 12:34 PM BST, Gary Guo wrote:
> On Wed Jul 8, 2026 at 11:37 AM BST, Alexandre Courbot wrote:
>> Non-generic, non-inlined functions of the `zerocopy` crate are only
>> codegen'd into `zerocopy.o`, with their symbols currently not exported,
>> making them unavailable to loadable modules.
>>
>> This can break e.g. nova-core built as a module when
>> `CONFIG_CC_OPTIMIZE_FOR_SIZE=y`:
>>
>>     ERROR: modpost: "_RNvMNtCs5wX7wwEUCR9_8zerocopy6layoutNtB2_8SizeInfo24try_to_nonzero_elem_size" [drivers/gpu/nova-core.ko] undefined!
>>     ERROR: modpost: "_RNvNtCs5wX7wwEUCR9_8zerocopy4util18padding_needed_for" [drivers/gpu/nova-core.ko] undefined!
>
> It looks to me that this should be done by fixing zerocopy.
>
> Both these functions should be marked as `#[inline]`. I would expect zerocopy.o
> to be having no .text.
>
> Instead of exporting this, I would vouch for going to another extreme --
> completely leave zerocopy.o unlinked, so we catch cases where code is actually
> generated.
>
> Best,
> Gary
>

This should do the trick. The only other unexported symbols are f32_ext and
f64_ext, which we never use. We can mark them as `#[inline(always)]` as well,
but figuring out how to compile them out completely might be better.

diff --git a/rust/Makefile b/rust/Makefile
index a870d1616c71..98c6086342a7 100644
--- a/rust/Makefile
+++ b/rust/Makefile
@@ -6,7 +6,7 @@ rustdoc_output := $(objtree)/Documentation/output/rust/rustdoc
 obj-$(CONFIG_RUST) += core.o compiler_builtins.o ffi.o
 always-$(CONFIG_RUST) += exports_core_generated.h
 
-obj-$(CONFIG_RUST) += zerocopy.o
+always-$(CONFIG_RUST) += zerocopy.o
 
 ifdef CONFIG_RUST_INLINE_HELPERS
 always-$(CONFIG_RUST) += helpers/helpers.bc helpers/helpers_module.bc
diff --git a/rust/zerocopy/src/layout.rs b/rust/zerocopy/src/layout.rs
index 6015d0f2de52..1870363adf10 100644
--- a/rust/zerocopy/src/layout.rs
+++ b/rust/zerocopy/src/layout.rs
@@ -71,6 +71,7 @@ impl SizeInfo {
     /// Attempts to create a `SizeInfo` from `Self` in which `elem_size` is a
     /// `NonZeroUsize`. If `elem_size` is 0, returns `None`.
     #[allow(unused)]
+    #[inline(always)]
     const fn try_to_nonzero_elem_size(&self) -> Option<SizeInfo<NonZeroUsize>> {
         Some(match *self {
             SizeInfo::Sized { size } => SizeInfo::Sized { size },
diff --git a/rust/zerocopy/src/util/mod.rs b/rust/zerocopy/src/util/mod.rs
index d6d4c6c2fcd9..ceffe8b5510d 100644
--- a/rust/zerocopy/src/util/mod.rs
+++ b/rust/zerocopy/src/util/mod.rs
@@ -150,6 +150,7 @@ pub(crate) fn validate_aligned_to<T: AsAddress, U>(t: T) -> Result<(), Alignment
     // Ensures that we add the minimum required padding.
     kani::ensures(|&p| p < align.get()),
 )]
+#[inline(always)]
 pub(crate) const fn padding_needed_for(len: usize, align: NonZeroUsize) -> usize {
     #[cfg(kani)]
     #[kani::proof_for_contract(padding_needed_for)]
@@ -251,6 +252,7 @@ fn proof() {
     n & mask
 }
 
+#[inline(always)]
 pub(crate) const fn max(a: NonZeroUsize, b: NonZeroUsize) -> NonZeroUsize {
     if a.get() < b.get() {
         b
@@ -259,6 +261,7 @@ pub(crate) const fn max(a: NonZeroUsize, b: NonZeroUsize) -> NonZeroUsize {
     }
 }
 
+#[inline(always)]
 pub(crate) const fn min(a: NonZeroUsize, b: NonZeroUsize) -> NonZeroUsize {
     if a.get() > b.get() {
         b


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] rust: export zerocopy symbols to modules
       [not found]     ` <CAPvrYMF3L0x5PBJk2TnSPyi0Lci2H0vGBu6QS_Rm4XFQtTV6mw@mail.gmail.com>
@ 2026-07-08 16:23       ` Miguel Ojeda
  0 siblings, 0 replies; 6+ messages in thread
From: Miguel Ojeda @ 2026-07-08 16:23 UTC (permalink / raw)
  To: Joshua Liebow-Feeser
  Cc: Gary Guo, Jack Wrenn, Alexandre Courbot, Miguel Ojeda,
	Boqun Feng, Björn Roy Baron, Benno Lossin, Andreas Hindborg,
	Alice Ryhl, Trevor Gross, Danilo Krummrich, Daniel Almeida,
	Tamir Duberstein, Onur Özkan, rust-for-linux, linux-kernel

On Wed, Jul 8, 2026 at 5:35 PM Joshua Liebow-Feeser <joshlf@google.com> wrote:
>
> What's the list of symbols that you'd like to be marked `#[inline]`? It'd be easy for us to add that upstream.

Thanks for the reply! Gary sent a reply here:

  https://lore.kernel.org/rust-for-linux/DJT6235B3DOV.222XR5O6VHG4M@garyguo.net/

Cheers,
Miguel

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2026-07-08 16:23 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-07-08 10:37 [PATCH] rust: export zerocopy symbols to modules Alexandre Courbot
2026-07-08 10:50 ` Miguel Ojeda
2026-07-08 11:34 ` Gary Guo
2026-07-08 11:37   ` Miguel Ojeda
     [not found]     ` <CAPvrYMF3L0x5PBJk2TnSPyi0Lci2H0vGBu6QS_Rm4XFQtTV6mw@mail.gmail.com>
2026-07-08 16:23       ` Miguel Ojeda
2026-07-08 11:51   ` Gary Guo

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

Powered by JetHome