On Thu, Aug 13, 2026 at 04:15:05PM +0200, Petr Pavlu wrote:
> On 8/7/26 3:26 AM, Aaron Tomlin wrote:
> > diff --git a/include/asm-generic/vmlinux.lds.h 
> > b/include/asm-generic/vmlinux.lds.h
> > index 5659f4b5a125..799d912dcbc0 100644
> > --- a/include/asm-generic/vmlinux.lds.h
> > +++ b/include/asm-generic/vmlinux.lds.h
> > @@ -734,7 +734,9 @@
> >     EARLYCON_TABLE()                                                \
> >     LSM_TABLE()                                                     \
> >     EARLY_LSM_TABLE()                                               \
> > -   KUNIT_INIT_TABLE()
> > +   KUNIT_INIT_TABLE()                                              \
> > +   STRUCT_ALIGN();                                                 \
> > +   BOUNDED_SECTION_BY(.initcall.modnames, _initcall_modnames)
> >  
> >  #define INIT_TEXT                                                  \
> >     *(.init.text .init.text.*)                                      \
> 
> I realized that instead of using STRUCT_ALIGN(), a cleaner and less
> wasteful approach is to prevent any potential over-alignment of
> individual .initcall.modnames sections by having
> ____define_initcall_modname() define each `struct initcall_modname` with
> `__aligned(__alignof__(struct initcall_modname))`.

Hi Petr,

Nice, I see. So, adding __aligned(__alignof__(struct initcall_modname))
forces the compiler to use the structure's natural alignment (8 bytes on
64-bit), ensuring entries are packed contiguously with zero inter-element
padding.

> > diff --git a/rust/macros/module.rs b/rust/macros/module.rs
> > index 06c18e207508..13353b43b38d 100644
> > --- a/rust/macros/module.rs
> > +++ b/rust/macros/module.rs
> > @@ -479,6 +479,7 @@ pub(crate) fn module(info: ModuleInfo) -> 
> > Result<TokenStream> {
> >      let ident_init = format_ident!("__{ident}_init");
> >      let ident_exit = format_ident!("__{ident}_exit");
> >      let ident_initcall = format_ident!("__{ident}_initcall");
> > +    let ident_modname = format_ident!("__{ident}_modname");
> >      let initcall_section = ".initcall6.init";
> >  
> >      let global_asm = format!(
> > @@ -590,6 +591,21 @@ pub extern "C" fn cleanup_module() {
> >                  #[cfg(CONFIG_HAVE_ARCH_PREL32_RELOCATIONS)]
> >                  ::core::arch::global_asm!(#global_asm);
> >  
> > +                #[cfg(not(MODULE))]
> > +                #[repr(C)]
> > +                struct InitcallModname {
> > +                    initcall_fn: extern "C" fn() -> ::kernel::ffi::c_int,
> > +                    modname: *const ::kernel::ffi::c_char,
> > +                }
> > +
> > +                #[cfg(not(MODULE))]
> > +                #[used(compiler)]
> > +                #[link_section = ".initcall.modnames"]
> > +                static #ident_modname: InitcallModname = InitcallModname {
> 
> Can Rust directly use the C definition of initcall_modname via
> ::kernel::bindings::initcall_modname?

Well, defining the local '#[repr(C)] struct InitcallModname' follows the
existing precedents and avoids expanding the bindgen surface with
<linux/init.h>. I think this is preferred, no? Gary, any thoughts?

In rust/macros/module.rs the existing built-in initcall pointers are
defined directly within the macro rather than pulling in initcall_t from
bindings, keeping module.rs self-contained.

> > +                    initcall_fn: #ident_init,
> > +                    modname: #name_cstr.as_ptr().cast(),
> 
> Can the modname string be placed in .init.rodata to match the behavior
> on the C side?

This is a very good suggestion and definitely worth adopting! Thank you.


Kind regards,
-- 
Aaron Tomlin

Reply via email to