[PATCH V7 16/16] DO-NOT_MERGE: cpufreq: Rename cpufreq-dt platdev

Viresh Kumar posted 16 patches 11 months ago
There is a newer version of this series
[PATCH V7 16/16] DO-NOT_MERGE: cpufreq: Rename cpufreq-dt platdev
Posted by Viresh Kumar 11 months ago
The module! implementation in the Rust code expects a module name
without a '-' symbol, else it fails to compile with following errors:

error: expected one of `:`, `;`, or `=`, found `-`
   --> drivers/cpufreq/rcpufreq_dt.rs:247:1
    |
247 | / module_platform_driver! {
248 | |     type: CPUFreqDTDriver,
249 | |     name: "cpufreq-dt",
250 | |     author: "Viresh Kumar <viresh.kumar@linaro.org>",
251 | |     description: "Generic CPUFreq DT driver",
252 | |     license: "GPL v2",
253 | | }
    | |_^ expected one of `:`, `;`, or `=`
    |
    = note: this error originates in the macro `$crate::prelude::module` which comes from the expansion of the macro `module_platform_driver` (in Nightly builds, run with -Z macro-backtrace for more info)

This must be fixed properly in the Rust code instead. Not to be merged.

Not-Signed-off-by: Viresh Kumar <viresh.kumar@linaro.org>
---
 drivers/cpufreq/cpufreq-dt-platdev.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/cpufreq/cpufreq-dt-platdev.c b/drivers/cpufreq/cpufreq-dt-platdev.c
index 9c198bd4f7e9..263e1e97538d 100644
--- a/drivers/cpufreq/cpufreq-dt-platdev.c
+++ b/drivers/cpufreq/cpufreq-dt-platdev.c
@@ -230,7 +230,7 @@ static int __init cpufreq_dt_platdev_init(void)
 	return -ENODEV;
 
 create_pdev:
-	return PTR_ERR_OR_ZERO(platform_device_register_data(NULL, "cpufreq-dt",
+	return PTR_ERR_OR_ZERO(platform_device_register_data(NULL, "cpufreq_dt",
 			       -1, data,
 			       sizeof(struct cpufreq_dt_platform_data)));
 }
-- 
2.31.1.272.g89b43f80a514
[PATCH] rust: macros: enable use of hyphens in module names
Posted by Anisse Astier 10 months, 3 weeks ago
Some modules might need naming that contains hyphens "-" to match the
auto-probing by name in the platform devices that comes from the device
tree.

But rust identifiers cannot contain hyphens, so replace the module name
by an underscore anywhere we'd use it as an identifier.

Signed-off-by: Anisse Astier <anisse@astier.eu>
---
Hello Viresh,

Does this solve you problem ?

Regards,

Anisse
---
 rust/macros/module.rs | 17 ++++++++++-------
 1 file changed, 10 insertions(+), 7 deletions(-)

diff --git a/rust/macros/module.rs b/rust/macros/module.rs
index aef3b132f32b..4cac332087b0 100644
--- a/rust/macros/module.rs
+++ b/rust/macros/module.rs
@@ -182,7 +182,9 @@ pub(crate) fn module(ts: TokenStream) -> TokenStream {
 
     let info = ModuleInfo::parse(&mut it);
 
-    let mut modinfo = ModInfoBuilder::new(info.name.as_ref());
+    /* Rust does not allow hyphens in identifiers, use underscore instead */
+    let name_identifier = info.name.replace("-", "_");
+    let mut modinfo = ModInfoBuilder::new(name_identifier.as_ref());
     if let Some(author) = info.author {
         modinfo.emit("author", &author);
     }
@@ -292,14 +294,14 @@ mod __module_init {{
                     #[doc(hidden)]
                     #[link_section = \"{initcall_section}\"]
                     #[used]
-                    pub static __{name}_initcall: extern \"C\" fn() -> core::ffi::c_int = __{name}_init;
+                    pub static __{name_identifier}_initcall: extern \"C\" fn() -> core::ffi::c_int = __{name_identifier}_init;
 
                     #[cfg(not(MODULE))]
                     #[cfg(CONFIG_HAVE_ARCH_PREL32_RELOCATIONS)]
                     core::arch::global_asm!(
                         r#\".section \"{initcall_section}\", \"a\"
-                        __{name}_initcall:
-                            .long   __{name}_init - .
+                        __{name_identifier}_initcall:
+                            .long   __{name_identifier}_init - .
                             .previous
                         \"#
                     );
@@ -307,7 +309,7 @@ mod __module_init {{
                     #[cfg(not(MODULE))]
                     #[doc(hidden)]
                     #[no_mangle]
-                    pub extern \"C\" fn __{name}_init() -> core::ffi::c_int {{
+                    pub extern \"C\" fn __{name_identifier}_init() -> core::ffi::c_int {{
                         // SAFETY: This function is inaccessible to the outside due to the double
                         // module wrapping it. It is called exactly once by the C side via its
                         // placement above in the initcall section.
@@ -317,12 +319,12 @@ mod __module_init {{
                     #[cfg(not(MODULE))]
                     #[doc(hidden)]
                     #[no_mangle]
-                    pub extern \"C\" fn __{name}_exit() {{
+                    pub extern \"C\" fn __{name_identifier}_exit() {{
                         // SAFETY:
                         // - This function is inaccessible to the outside due to the double
                         //   module wrapping it. It is called exactly once by the C side via its
                         //   unique name,
-                        // - furthermore it is only called after `__{name}_init` has returned `0`
+                        // - furthermore it is only called after `__{name_identifier}_init` has returned `0`
                         //   (which delegates to `__init`).
                         unsafe {{ __exit() }}
                     }}
@@ -369,6 +371,7 @@ unsafe fn __exit() {{
         ",
         type_ = info.type_,
         name = info.name,
+        name_identifier = name_identifier,
         modinfo = modinfo.buffer,
         initcall_section = ".initcall6.init"
     )
-- 
2.48.1
[PATCH v2] rust: macros: enable use of hyphens in module names
Posted by Anisse Astier 10 months, 3 weeks ago
Some modules might need naming that contains hyphens "-" to match the
auto-probing by name in the platform devices that comes from the device
tree.

But rust identifiers cannot contain hyphens, so replace the module name
by an underscore anywhere we'd use it as an identifier.

Signed-off-by: Anisse Astier <anisse@astier.eu>
---
Hello,

Change since v1:
 - rebase on branch rfl/staging/dev

Sorry for sending a v2 so quickly, but v1 was based on the wrong branch :-/

Kind regards,

Anisse
---
 rust/macros/module.rs | 17 ++++++++++-------
 1 file changed, 10 insertions(+), 7 deletions(-)

diff --git a/rust/macros/module.rs b/rust/macros/module.rs
index cdf94f4982df..1eff30d2ca6a 100644
--- a/rust/macros/module.rs
+++ b/rust/macros/module.rs
@@ -182,7 +182,9 @@ pub(crate) fn module(ts: TokenStream) -> TokenStream {
 
     let info = ModuleInfo::parse(&mut it);
 
-    let mut modinfo = ModInfoBuilder::new(info.name.as_ref());
+    /* Rust does not allow hyphens in identifiers, use underscore instead */
+    let name_identifier = info.name.replace("-", "_");
+    let mut modinfo = ModInfoBuilder::new(name_identifier.as_ref());
     if let Some(author) = info.author {
         modinfo.emit("author", &author);
     }
@@ -298,14 +300,14 @@ mod __module_init {{
                     #[doc(hidden)]
                     #[link_section = \"{initcall_section}\"]
                     #[used]
-                    pub static __{name}_initcall: extern \"C\" fn() -> kernel::ffi::c_int = __{name}_init;
+                    pub static __{name_identifier}_initcall: extern \"C\" fn() -> kernel::ffi::c_int = __{name_identifier}_init;
 
                     #[cfg(not(MODULE))]
                     #[cfg(CONFIG_HAVE_ARCH_PREL32_RELOCATIONS)]
                     core::arch::global_asm!(
                         r#\".section \"{initcall_section}\", \"a\"
-                        __{name}_initcall:
-                            .long   __{name}_init - .
+                        __{name_identifier}_initcall:
+                            .long   __{name_identifier}_init - .
                             .previous
                         \"#
                     );
@@ -313,7 +315,7 @@ mod __module_init {{
                     #[cfg(not(MODULE))]
                     #[doc(hidden)]
                     #[no_mangle]
-                    pub extern \"C\" fn __{name}_init() -> kernel::ffi::c_int {{
+                    pub extern \"C\" fn __{name_identifier}_init() -> kernel::ffi::c_int {{
                         // SAFETY: This function is inaccessible to the outside due to the double
                         // module wrapping it. It is called exactly once by the C side via its
                         // placement above in the initcall section.
@@ -323,12 +325,12 @@ mod __module_init {{
                     #[cfg(not(MODULE))]
                     #[doc(hidden)]
                     #[no_mangle]
-                    pub extern \"C\" fn __{name}_exit() {{
+                    pub extern \"C\" fn __{name_identifier}_exit() {{
                         // SAFETY:
                         // - This function is inaccessible to the outside due to the double
                         //   module wrapping it. It is called exactly once by the C side via its
                         //   unique name,
-                        // - furthermore it is only called after `__{name}_init` has returned `0`
+                        // - furthermore it is only called after `__{name_identifier}_init` has returned `0`
                         //   (which delegates to `__init`).
                         unsafe {{ __exit() }}
                     }}
@@ -369,6 +371,7 @@ unsafe fn __exit() {{
         ",
         type_ = info.type_,
         name = info.name,
+        name_identifier = name_identifier,
         modinfo = modinfo.buffer,
         initcall_section = ".initcall6.init"
     )
-- 
2.48.1
Re: [PATCH v2] rust: macros: enable use of hyphens in module names
Posted by Alice Ryhl 10 months, 2 weeks ago
On Wed, Jan 22, 2025 at 2:39 PM Anisse Astier <anisse@astier.eu> wrote:
>
> Some modules might need naming that contains hyphens "-" to match the
> auto-probing by name in the platform devices that comes from the device
> tree.
>
> But rust identifiers cannot contain hyphens, so replace the module name
> by an underscore anywhere we'd use it as an identifier.
>
> Signed-off-by: Anisse Astier <anisse@astier.eu>

Reviewed-by: Alice Ryhl <aliceryhl@google.com>
Re: [PATCH v2] rust: macros: enable use of hyphens in module names
Posted by Viresh Kumar 10 months, 2 weeks ago
On 22-01-25, 14:39, Anisse Astier wrote:
> +    /* Rust does not allow hyphens in identifiers, use underscore instead */
> +    let name_identifier = info.name.replace("-", "_");

With CLIPPY=1 W=1, this gives:

warning: single-character string constant used as pattern
   --> /mnt/ssd/all/work/repos/kernel/linux/rust/macros/module.rs:186:45
    |
186 |     let name_identifier = info.name.replace("-", "_");
    |                                             ^^^ help: consider using a `char`: `'-'`
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#single_char_pattern
    = note: `-W clippy::single-char-pattern` implied by `-W clippy::all`
    = help: to override `-W clippy::all` add `#[allow(clippy::single_char_pattern)]`

warning: 1 warning emitted

This fixes it:

diff --git a/rust/macros/module.rs b/rust/macros/module.rs
index 1eff30d2ca6a..2e740bbdb598 100644
--- a/rust/macros/module.rs
+++ b/rust/macros/module.rs
@@ -183,7 +183,7 @@ pub(crate) fn module(ts: TokenStream) -> TokenStream {
     let info = ModuleInfo::parse(&mut it);

     /* Rust does not allow hyphens in identifiers, use underscore instead */
-    let name_identifier = info.name.replace("-", "_");
+    let name_identifier = info.name.replace('-', "_");
     let mut modinfo = ModInfoBuilder::new(name_identifier.as_ref());
     if let Some(author) = info.author {
         modinfo.emit("author", &author);


Will include it in my V8 now, unless you have any objections to it.
Thanks.

-- 
viresh
Re: [PATCH v2] rust: macros: enable use of hyphens in module names
Posted by Anisse Astier 10 months, 2 weeks ago

Jeu 30 janv 2025, à 05:58, Viresh Kumar a écrit :
> On 22-01-25, 14:39, Anisse Astier wrote:
>> +    /* Rust does not allow hyphens in identifiers, use underscore instead */
>> +    let name_identifier = info.name.replace("-", "_");
>
> With CLIPPY=1 W=1, this gives:
>
> warning: single-character string constant used as pattern
>    --> /mnt/ssd/all/work/repos/kernel/linux/rust/macros/module.rs:186:45
>     |
> 186 |     let name_identifier = info.name.replace("-", "_");
>     |                                             ^^^ help: consider 
> using a `char`: `'-'`
>     |
>     = help: for further information visit 
> https://rust-lang.github.io/rust-clippy/master/index.html#single_char_pattern
>     = note: `-W clippy::single-char-pattern` implied by `-W clippy::all`
>     = help: to override `-W clippy::all` add 
> `#[allow(clippy::single_char_pattern)]`
>
> warning: 1 warning emitted
>
> This fixes it:
>
> diff --git a/rust/macros/module.rs b/rust/macros/module.rs
> index 1eff30d2ca6a..2e740bbdb598 100644
> --- a/rust/macros/module.rs
> +++ b/rust/macros/module.rs
> @@ -183,7 +183,7 @@ pub(crate) fn module(ts: TokenStream) -> TokenStream {
>      let info = ModuleInfo::parse(&mut it);
>
>      /* Rust does not allow hyphens in identifiers, use underscore instead */
> -    let name_identifier = info.name.replace("-", "_");
> +    let name_identifier = info.name.replace('-', "_");
>      let mut modinfo = ModInfoBuilder::new(name_identifier.as_ref());
>      if let Some(author) = info.author {
>          modinfo.emit("author", &author);
>
>
> Will include it in my V8 now, unless you have any objections to it.

No objections and nice catch!

Regards,

Anisse 
Re: [PATCH v2] rust: macros: enable use of hyphens in module names
Posted by Viresh Kumar 10 months, 3 weeks ago
On 22-01-25, 14:39, Anisse Astier wrote:
> Some modules might need naming that contains hyphens "-" to match the
> auto-probing by name in the platform devices that comes from the device
> tree.
> 
> But rust identifiers cannot contain hyphens, so replace the module name
> by an underscore anywhere we'd use it as an identifier.
> 
> Signed-off-by: Anisse Astier <anisse@astier.eu>
> ---
> Hello,
> 
> Change since v1:
>  - rebase on branch rfl/staging/dev

Tested-by: Viresh Kumar <viresh.kumar@linaro.org>

Thanks for the fix.

-- 
viresh