[PATCH] rust: acpi: replace `core::mem::zeroed` with `pin_init::zeroed`

Siyuan Huang posted 1 patch 3 months, 2 weeks ago
rust/kernel/acpi.rs | 4 +---
1 file changed, 1 insertion(+), 3 deletions(-)
[PATCH] rust: acpi: replace `core::mem::zeroed` with `pin_init::zeroed`
Posted by Siyuan Huang 3 months, 2 weeks ago
All types in `bindings` implement `Zeroable` if they can, so use
`pin_init::zeroed` instead of relying on `unsafe` code.

If this ends up not compiling in the future, something in bindgen or on
the C side changed and is most likely incorrect.

Link: https://github.com/Rust-for-Linux/linux/issues/1189
Suggested-by: Benno Lossin <lossin@kernel.org>
Signed-off-by: Siyuan Huang <huangsiyuan@kylinos.cn>
---
 rust/kernel/acpi.rs | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)

diff --git a/rust/kernel/acpi.rs b/rust/kernel/acpi.rs
index 7ae317368b00..f9488be9249c 100644
--- a/rust/kernel/acpi.rs
+++ b/rust/kernel/acpi.rs
@@ -42,9 +42,7 @@ pub const fn new(id: &'static CStr) -> Self {
             "ID exceeds 16 bytes"
         );
         let src = id.as_bytes_with_nul();
-        // Replace with `bindings::acpi_device_id::default()` once stabilized for `const`.
-        // SAFETY: FFI type is valid to be zero-initialized.
-        let mut acpi: bindings::acpi_device_id = unsafe { core::mem::zeroed() };
+        let mut acpi: bindings::acpi_device_id = pin_init::zeroed();
         let mut i = 0;
         while i < src.len() {
             acpi.id[i] = src[i];
-- 
2.25.1
Re: [PATCH] rust: acpi: replace `core::mem::zeroed` with `pin_init::zeroed`
Posted by Kunwu Chan 3 months, 2 weeks ago
On 10/20/25 11:12, Siyuan Huang wrote:
> All types in `bindings` implement `Zeroable` if they can, so use
> `pin_init::zeroed` instead of relying on `unsafe` code.
>
> If this ends up not compiling in the future, something in bindgen or on
> the C side changed and is most likely incorrect.
>
> Link: https://github.com/Rust-for-Linux/linux/issues/1189
> Suggested-by: Benno Lossin <lossin@kernel.org>
> Signed-off-by: Siyuan Huang <huangsiyuan@kylinos.cn>
> ---
>   rust/kernel/acpi.rs | 4 +---
>   1 file changed, 1 insertion(+), 3 deletions(-)
>
> diff --git a/rust/kernel/acpi.rs b/rust/kernel/acpi.rs
> index 7ae317368b00..f9488be9249c 100644
> --- a/rust/kernel/acpi.rs
> +++ b/rust/kernel/acpi.rs
> @@ -42,9 +42,7 @@ pub const fn new(id: &'static CStr) -> Self {
>               "ID exceeds 16 bytes"
>           );
>           let src = id.as_bytes_with_nul();
> -        // Replace with `bindings::acpi_device_id::default()` once stabilized for `const`.
> -        // SAFETY: FFI type is valid to be zero-initialized.
> -        let mut acpi: bindings::acpi_device_id = unsafe { core::mem::zeroed() };
> +        let mut acpi: bindings::acpi_device_id = pin_init::zeroed();
>           let mut i = 0;
>           while i < src.len() {
>               acpi.id[i] = src[i];


Reviewed-by: Kunwu Chan <chentao@kylinos.cn>

-- 
Thanks,
        Kunwu Chan.
Re: [PATCH] rust: acpi: replace `core::mem::zeroed` with `pin_init::zeroed`
Posted by Danilo Krummrich 3 months, 2 weeks ago
On 10/20/25 5:12 AM, Siyuan Huang wrote:
> All types in `bindings` implement `Zeroable` if they can, so use
> `pin_init::zeroed` instead of relying on `unsafe` code.
> 
> If this ends up not compiling in the future, something in bindgen or on
> the C side changed and is most likely incorrect.
> 
> Link: https://github.com/Rust-for-Linux/linux/issues/1189
> Suggested-by: Benno Lossin <lossin@kernel.org>
> Signed-off-by: Siyuan Huang <huangsiyuan@kylinos.cn>

Acked-by: Danilo Krummrich <dakr@kernel.org>

Siyuan, there are more such cases, e.g. in OF, debugfs and a few others in case
you're interested in fixing up those as well. :)
Re: [PATCH] rust: acpi: replace `core::mem::zeroed` with `pin_init::zeroed`
Posted by Benno Lossin 3 months, 2 weeks ago
On Mon Oct 20, 2025 at 5:12 AM CEST, Siyuan Huang wrote:
> All types in `bindings` implement `Zeroable` if they can, so use
> `pin_init::zeroed` instead of relying on `unsafe` code.
>
> If this ends up not compiling in the future, something in bindgen or on
> the C side changed and is most likely incorrect.
>
> Link: https://github.com/Rust-for-Linux/linux/issues/1189
> Suggested-by: Benno Lossin <lossin@kernel.org>
> Signed-off-by: Siyuan Huang <huangsiyuan@kylinos.cn>

Thanks, great to see this.

Reviewed-by: Benno Lossin <lossin@kernel.org>

Cheers,
Benno

> ---
>  rust/kernel/acpi.rs | 4 +---
>  1 file changed, 1 insertion(+), 3 deletions(-)
Re: [PATCH] rust: acpi: replace `core::mem::zeroed` with `pin_init::zeroed`
Posted by Miguel Ojeda 3 months, 2 weeks ago
On Mon, Oct 20, 2025 at 5:12 AM Siyuan Huang <huangsiyuan@kylinos.cn> wrote:
>
> All types in `bindings` implement `Zeroable` if they can, so use
> `pin_init::zeroed` instead of relying on `unsafe` code.
>
> If this ends up not compiling in the future, something in bindgen or on
> the C side changed and is most likely incorrect.
>
> Link: https://github.com/Rust-for-Linux/linux/issues/1189
> Suggested-by: Benno Lossin <lossin@kernel.org>
> Signed-off-by: Siyuan Huang <huangsiyuan@kylinos.cn>

Rafael: I guess you will take this; otherwise, please let me know -- thanks!

Cheers,
Miguel
Re: [PATCH] rust: acpi: replace `core::mem::zeroed` with `pin_init::zeroed`
Posted by Rafael J. Wysocki 3 months, 1 week ago
On Mon, Oct 20, 2025 at 1:26 PM Miguel Ojeda
<miguel.ojeda.sandonis@gmail.com> wrote:
>
> On Mon, Oct 20, 2025 at 5:12 AM Siyuan Huang <huangsiyuan@kylinos.cn> wrote:
> >
> > All types in `bindings` implement `Zeroable` if they can, so use
> > `pin_init::zeroed` instead of relying on `unsafe` code.
> >
> > If this ends up not compiling in the future, something in bindgen or on
> > the C side changed and is most likely incorrect.
> >
> > Link: https://github.com/Rust-for-Linux/linux/issues/1189
> > Suggested-by: Benno Lossin <lossin@kernel.org>
> > Signed-off-by: Siyuan Huang <huangsiyuan@kylinos.cn>
>
> Rafael: I guess you will take this; otherwise, please let me know -- thanks!

Applied as 6.19 material, thanks!
Re: [PATCH] rust: acpi: replace `core::mem::zeroed` with `pin_init::zeroed`
Posted by Alice Ryhl 3 months, 2 weeks ago
On Mon, Oct 20, 2025 at 11:12:04AM +0800, Siyuan Huang wrote:
> All types in `bindings` implement `Zeroable` if they can, so use
> `pin_init::zeroed` instead of relying on `unsafe` code.
> 
> If this ends up not compiling in the future, something in bindgen or on
> the C side changed and is most likely incorrect.
> 
> Link: https://github.com/Rust-for-Linux/linux/issues/1189
> Suggested-by: Benno Lossin <lossin@kernel.org>
> Signed-off-by: Siyuan Huang <huangsiyuan@kylinos.cn>

We should make this method accessible under kernel::ffi:: since that's
IMO a better path for it for cases like this. It doesn't really have
anything to do with pin_init in this use-case.

Regardless:
Reviewed-by: Alice Ryhl <aliceryhl@google.com>
Re: [PATCH] rust: acpi: replace `core::mem::zeroed` with `pin_init::zeroed`
Posted by Benno Lossin 3 months, 2 weeks ago
On Mon Oct 20, 2025 at 10:18 AM CEST, Alice Ryhl wrote:
> On Mon, Oct 20, 2025 at 11:12:04AM +0800, Siyuan Huang wrote:
>> All types in `bindings` implement `Zeroable` if they can, so use
>> `pin_init::zeroed` instead of relying on `unsafe` code.
>> 
>> If this ends up not compiling in the future, something in bindgen or on
>> the C side changed and is most likely incorrect.
>> 
>> Link: https://github.com/Rust-for-Linux/linux/issues/1189
>> Suggested-by: Benno Lossin <lossin@kernel.org>
>> Signed-off-by: Siyuan Huang <huangsiyuan@kylinos.cn>
>
> We should make this method accessible under kernel::ffi:: since that's
> IMO a better path for it for cases like this. It doesn't really have
> anything to do with pin_init in this use-case.

Yeah, we should do that. I don't have time to do it, so if anyone wants
to go ahead, please do :)

Cheers,
Benno