[PATCH v2 1/6] rust: module: add static pointer to `{init,cleanup}_module()`

Miguel Ojeda posted 6 patches 1 year, 4 months ago
There is a newer version of this series
[PATCH v2 1/6] rust: module: add static pointer to `{init,cleanup}_module()`
Posted by Miguel Ojeda 1 year, 4 months ago
Add the equivalent of the `___ADDRESSABLE()` annotation in the
`module_{init,exit}` macros to the Rust `module!` macro.

Without this, `objtool` would complain if enabled for Rust, e.g.:

    samples/rust/rust_print.o: warning: objtool: cleanup_module(): not an indirect call target
    samples/rust/rust_print.o: warning: objtool: init_module(): not an indirect call target

Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
---
 rust/macros/module.rs | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/rust/macros/module.rs b/rust/macros/module.rs
index 411dc103d82e..571ffa2e189c 100644
--- a/rust/macros/module.rs
+++ b/rust/macros/module.rs
@@ -256,6 +256,12 @@ mod __module_init {{
                         unsafe {{ __init() }}
                     }}
 
+                    #[cfg(MODULE)]
+                    #[doc(hidden)]
+                    #[used]
+                    #[link_section = \".init.data\"]
+                    static __UNIQUE_ID___addressable_init_module: unsafe extern \"C\" fn() -> i32 = init_module;
+
                     #[cfg(MODULE)]
                     #[doc(hidden)]
                     #[no_mangle]
@@ -269,6 +275,12 @@ mod __module_init {{
                         unsafe {{ __exit() }}
                     }}
 
+                    #[cfg(MODULE)]
+                    #[doc(hidden)]
+                    #[used]
+                    #[link_section = \".exit.data\"]
+                    static __UNIQUE_ID___addressable_cleanup_module: extern \"C\" fn() = cleanup_module;
+
                     // Built-in modules are initialized through an initcall pointer
                     // and the identifiers need to be unique.
                     #[cfg(not(MODULE))]
-- 
2.45.2
Re: [PATCH v2 1/6] rust: module: add static pointer to `{init,cleanup}_module()`
Posted by Gary Guo 1 year, 4 months ago
On Wed, 24 Jul 2024 18:14:54 +0200
Miguel Ojeda <ojeda@kernel.org> wrote:

> Add the equivalent of the `___ADDRESSABLE()` annotation in the
> `module_{init,exit}` macros to the Rust `module!` macro.
> 
> Without this, `objtool` would complain if enabled for Rust, e.g.:
> 
>     samples/rust/rust_print.o: warning: objtool: cleanup_module(): not an indirect call target
>     samples/rust/rust_print.o: warning: objtool: init_module(): not an indirect call target
> 
> Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
> ---
>  rust/macros/module.rs | 12 ++++++++++++
>  1 file changed, 12 insertions(+)
> 
> diff --git a/rust/macros/module.rs b/rust/macros/module.rs
> index 411dc103d82e..571ffa2e189c 100644
> --- a/rust/macros/module.rs
> +++ b/rust/macros/module.rs
> @@ -256,6 +256,12 @@ mod __module_init {{
>                          unsafe {{ __init() }}
>                      }}
>  
> +                    #[cfg(MODULE)]
> +                    #[doc(hidden)]
> +                    #[used]
> +                    #[link_section = \".init.data\"]

Should this be in section `.discard.addressable` instead?

> +                    static __UNIQUE_ID___addressable_init_module: unsafe extern \"C\" fn() -> i32 = init_module;
> +
>                      #[cfg(MODULE)]
>                      #[doc(hidden)]
>                      #[no_mangle]
> @@ -269,6 +275,12 @@ mod __module_init {{
>                          unsafe {{ __exit() }}
>                      }}
>  
> +                    #[cfg(MODULE)]
> +                    #[doc(hidden)]
> +                    #[used]
> +                    #[link_section = \".exit.data\"]
> +                    static __UNIQUE_ID___addressable_cleanup_module: extern \"C\" fn() = cleanup_module;
> +
>                      // Built-in modules are initialized through an initcall pointer
>                      // and the identifiers need to be unique.
>                      #[cfg(not(MODULE))]
Re: Re: [PATCH v2 1/6] rust: module: add static pointer to `{init,cleanup}_module()`
Posted by Miguel Ojeda 1 year, 4 months ago
On Wed, 24 Jul 2024 20:46:49 +0100 Gary Guo <gary@garyguo.net> wrote:
>
> Should this be in section `.discard.addressable` instead?

No, we need to mimic the call to `___ADDRESSABLE()` (i.e. the one with
3 underscores, not 2), which passes `__{init,exit}data`.

By the way, thanks for the reviews Gary!

Cheers,
Miguel
Re: [PATCH v2 1/6] rust: module: add static pointer to `{init,cleanup}_module()`
Posted by Gary Guo 1 year, 4 months ago
On Thu, 25 Jul 2024 19:47:13 +0200
Miguel Ojeda <ojeda@kernel.org> wrote:

> On Wed, 24 Jul 2024 20:46:49 +0100 Gary Guo <gary@garyguo.net> wrote:
> >
> > Should this be in section `.discard.addressable` instead?  
> 
> No, we need to mimic the call to `___ADDRESSABLE()` (i.e. the one with
> 3 underscores, not 2), which passes `__{init,exit}data`.
> 
> By the way, thanks for the reviews Gary!
> 
> Cheers,
> Miguel

You're right. It's a bit difficult to navigate macros :)

In this case I think this indeed matches the C implementation.

Reviewed-by: Gary Guo <gary@garyguo.net>

Thanks,
Gary
Re: Re: [PATCH v2 1/6] rust: module: add static pointer to `{init,cleanup}_module()`
Posted by Miguel Ojeda 1 year, 4 months ago
On Wed, 24 Jul 2024 20:46:49 +0100 Gary Guo <gary@garyguo.net> wrote:
>
> On Wed, 24 Jul 2024 18:14:54 +0200
> Miguel Ojeda <ojeda@kernel.org> wrote:
>
> > Add the equivalent of the `___ADDRESSABLE()` annotation in the
> > `module_{init,exit}` macros to the Rust `module!` macro.
> >
> > Without this, `objtool` would complain if enabled for Rust, e.g.:
> >
> >     samples/rust/rust_print.o: warning: objtool: cleanup_module(): not an indirect call target
> >     samples/rust/rust_print.o: warning: objtool: init_module(): not an indirect call target
> >
> > Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
> > ---
> >  rust/macros/module.rs | 12 ++++++++++++
> >  1 file changed, 12 insertions(+)
> >
> > diff --git a/rust/macros/module.rs b/rust/macros/module.rs
> > index 411dc103d82e..571ffa2e189c 100644
> > --- a/rust/macros/module.rs
> > +++ b/rust/macros/module.rs
> > @@ -256,6 +256,12 @@ mod __module_init {{
> >                          unsafe {{ __init() }}
> >                      }}
> >
> > +                    #[cfg(MODULE)]
> > +                    #[doc(hidden)]
> > +                    #[used]
> > +                    #[link_section = \".init.data\"]
>
> Should this be in section `.discard.addressable` instead?
>
> > +                    static __UNIQUE_ID___addressable_init_module: unsafe extern \"C\" fn() -> i32 = init_module;
> > +
> >                      #[cfg(MODULE)]
> >                      #[doc(hidden)]
> >                      #[no_mangle]
> > @@ -269,6 +275,12 @@ mod __module_init {{
> >                          unsafe {{ __exit() }}
> >                      }}
> >
> > +                    #[cfg(MODULE)]
> > +                    #[doc(hidden)]
> > +                    #[used]
> > +                    #[link_section = \".exit.data\"]
> > +                    static __UNIQUE_ID___addressable_cleanup_module: extern \"C\" fn() = cleanup_module;
> > +
> >                      // Built-in modules are initialized through an initcall pointer
> >                      // and the identifiers need to be unique.
> >                      #[cfg(not(MODULE))]

Boot-tested under QEMU for Rust:

Tested-by: Miguel Ojeda <ojeda@kernel.org>

Thanks!

Cheers,
Miguel
Re: Re: [PATCH v2 1/6] rust: module: add static pointer to `{init,cleanup}_module()`
Posted by Miguel Ojeda 1 year, 4 months ago
On Thu, Jul 25, 2024 at 7:45 PM Miguel Ojeda <ojeda@kernel.org> wrote:
>
> Boot-tested under QEMU for Rust:

Sorry, please ignore this message. I run a script of mine badly... :)

Cheers,
Miguel