rust-for-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* Printing with overflow checks can cause modpost errors
@ 2025-09-11 21:31 Joel Fernandes
  2025-09-12  0:27 ` Andrew Ballance
  0 siblings, 1 reply; 11+ messages in thread
From: Joel Fernandes @ 2025-09-11 21:31 UTC (permalink / raw)
  To: rust-for-linux, linux-kernel
  Cc: John Hubbard, Alexandre Courbot, Timur Tabi, Alistair Popple,
	Miguel Ojeda

Hello,
Recently some of have been running into modpost errors more frequently. Ahead
of Kangrejos, I am trying to study them, the one I looked at today is truly
weird, below are more details.

I narrowed it down to the print statement and specifically the FFI call to
printk bindings. This was first reported by Timur Tabi on CC. 

With CONFIG_RUST_OVERFLOW_CHECKS=y and CONFIG_RUST_BUILD_ASSERT_ALLOW=y, the
following patch when applied to nova-core will fail to build with following
errors. The question is why does the overflow checking fail since the
arithmetic is valid, and why only during printing (and say not during the
call to write32).

  MODPOST Module.symvers
ERROR: modpost: "rust_build_error" [drivers/gpu/nova-core/nova_core.ko] undefined!
make[2]: *** [scripts/Makefile.modpost:147: Module.symvers] Error 1
make[1]: *** [/home/joelaf/repo/linux-nova-rm-call/Makefile:1961: modpost] Error 2
make: *** [Makefile:248: __sub-make] Error 2

Any comments or thoughts?

---8<-----------------------

From: Joel Fernandes <joelagnelf@nvidia.com>
Subject: [PATCH] Reproduce modpost error due to print

Signed-off-by: Joel Fernandes <joelagnelf@nvidia.com>
---
 drivers/gpu/nova-core/nova_core.rs    |  1 +
 drivers/gpu/nova-core/test_modpost.rs | 60 +++++++++++++++++++++++++++
 2 files changed, 61 insertions(+)
 create mode 100644 drivers/gpu/nova-core/test_modpost.rs

diff --git a/drivers/gpu/nova-core/nova_core.rs b/drivers/gpu/nova-core/nova_core.rs
index 4dbc7e5daae3..9ed48ddf6df9 100644
--- a/drivers/gpu/nova-core/nova_core.rs
+++ b/drivers/gpu/nova-core/nova_core.rs
@@ -15,6 +15,7 @@
 mod sbuffer;
 mod util;
 mod vbios;
+mod test_modpost;
 
 pub(crate) const MODULE_NAME: &kernel::str::CStr = <LocalModule as kernel::ModuleMetadata>::NAME;
 
diff --git a/drivers/gpu/nova-core/test_modpost.rs b/drivers/gpu/nova-core/test_modpost.rs
new file mode 100644
index 000000000000..eab82b6176a8
--- /dev/null
+++ b/drivers/gpu/nova-core/test_modpost.rs
@@ -0,0 +1,60 @@
+use kernel::prelude::*;
+use kernel::io::Io;
+use core::ops::Deref;
+use crate::driver::Bar0;
+use crate::falcon::{FalconEngine};
+use crate::falcon::gsp::Gsp;
+
+macro_rules! test_reg_array {
+    ($name:ident @ $base:ty [ $offset:literal [ $size:expr ] ]) => {
+        pub(crate) struct $name(u32);
+        
+        impl $name {
+            pub(crate) const OFFSET: usize = $offset;
+            pub(crate) const SIZE: usize = $size;
+            pub(crate) const STRIDE: usize = 4;
+            
+            #[inline(always)]
+            pub(crate) fn write_with_printk<const SIZE: usize, T, B>(
+                self,
+                io: &T,
+                _base: &B,
+                idx: usize
+            ) where
+                T: Deref<Target = Io<SIZE>>,
+                B: kernel::io::register::RegisterBase<$base>,
+            {
+                // This assert has no effect on the issue and can be commented
+                // to still reproduce the error.
+                build_assert!(idx < Self::SIZE);
+
+                let offset = <B as kernel::io::register::RegisterBase<$base>>::BASE +
+                    Self::OFFSET + (idx * Self::STRIDE);
+
+                // PRINTK BLOCK -- entire block can be replaced with pr_err("{}", offset)
+                // and it would still show the same error.
+                let args = core::format_args!("{}", offset);
+                unsafe {
+                    kernel::bindings::_printk( // ---> Causes the MODPOST error
+                        kernel::print::format_strings::ERR.as_ptr(),
+                        crate::__LOG_PREFIX.as_ptr(),
+                        core::ptr::from_ref(&args).cast::<kernel::ffi::c_void>(),
+                    );
+                }
+                // END PRINTK BLOCK
+                
+                io.write32(self.0, offset);
+            }
+            
+        }
+    };
+}
+
+test_reg_array!(TestFbifTranscfg @ crate::falcon::PFalconBase [ 0x00000600 [ 8 ] ]);
+
+#[no_mangle]
+pub(crate) extern "C" fn test_single(bar: &Bar0) {
+    TestFbifTranscfg(0x00000001).write_with_printk(bar, &Gsp::ID, 0);
+}
+
+
-- 
2.34.1


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

end of thread, other threads:[~2025-09-17 23:19 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-09-11 21:31 Printing with overflow checks can cause modpost errors Joel Fernandes
2025-09-12  0:27 ` Andrew Ballance
2025-09-12  2:53   ` Joel Fernandes
2025-09-12  4:08     ` Andrew Ballance
2025-09-12  8:27       ` Miguel Ojeda
2025-09-12  9:45         ` Alice Ryhl
2025-09-12 10:08           ` Miguel Ojeda
2025-09-16 10:32       ` Joel Fernandes
2025-09-16 10:48         ` Joel Fernandes
2025-09-16 18:52           ` Joel Fernandes
2025-09-17 23:18             ` Timur Tabi

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).