Re-export Zeroable and zeroed() from ffi module
Subsystems (4)
Alice Ryhl
reviewercover Re-export Zeroable and zeroed() from ffi module
Currently, the Zeroable trait is defined by pin-init because pin-init happens to use the trait. However, zeroed types are useful for many purposes other than pin-init. Also, we wish to implement Zeroable for types generated by bindgen. For both of these reasons, re-export Zeroable from the ffi crate, which is a already dependency of the crates with bindgen output.
---
Alice Ryhl (4):
rust: ffi: reexport Zeroable and related items
rust: cpufreq: import pin_init::zeroed() from ffi
rust: i2c: import pin_init::zeroed() from ffi
rust: acpi: import pin_init::zeroed() from ffi
rust/Makefile | 9 ++++++--- rust/ffi.rs | 7 +++++++ rust/kernel/acpi.rs | 2 +- rust/kernel/cpufreq.rs | 2 +- rust/kernel/i2c.rs | 4 ++-- 5 files changed, 17 insertions(+), 7 deletions(-)
---
base-commit: 8f0b4cce4481fb22653697cced8d0d04027cb1e8
change-id: 20251202-zeroable-ffi-2beb542376c3
Best regards,
Signature · 1 line
Alice Ryhl <[email protected]>
Alice Ryhl
reviewerPATCH 1/4 rust: ffi: reexport Zeroable and related items
To implement FromBytes/AsBytes for bindings types, we need to move these traits to the ffi crate. For consistency, this means that Zeroable should also be available from the ffi crate. Thus, add the appropriate re-exports.
--- rust/Makefile | 9 ++++++--- rust/ffi.rs | 7 +++++++ 2 files changed, 13 insertions(+), 3 deletions(-)
rust/Makefile +6 -3
@@ -207,7 +207,8 @@ rustdoc-compiler_builtins: $(src)/compiler_builtins.rs rustdoc-core FORCE +$(call if_changed,rustdoc) rustdoc-ffi: private is-kernel-object := y -rustdoc-ffi: $(src)/ffi.rs rustdoc-core FORCE +rustdoc-ffi: private rustc_target_flags = --extern pin_init +rustdoc-ffi: $(src)/ffi.rs rustdoc-pin_init rustdoc-core FORCE +$(call if_changed,rustdoc) rustdoc-pin_init_internal: private rustdoc_host = yes
@@ -249,7 +250,8 @@ quiet_cmd_rustc_test_library = $(RUSTC_OR_CLIPPY_QUIET) TL $< rusttestlib-build_error: $(src)/build_error.rs FORCE +$(call if_changed,rustc_test_library) -rusttestlib-ffi: $(src)/ffi.rs FORCE +rusttestlib-ffi: private rustc_target_flags = --extern pin_init +rusttestlib-ffi: $(src)/ffi.rs rusttestlib-pin_init FORCE +$(call if_changed,rustc_test_library) rusttestlib-proc_macro2: private rustc_target_flags = $(proc_macro2-flags)
@@ -657,7 +659,8 @@ $(obj)/build_error.o: $(src)/build_error.rs $(obj)/compiler_builtins.o FORCE +$(call if_changed_rule,rustc_library) $(obj)/ffi.o: private skip_gendwarfksyms = 1 -$(obj)/ffi.o: $(src)/ffi.rs $(obj)/compiler_builtins.o FORCE +$(obj)/ffi.o: private rustc_target_flags = --extern pin_init +$(obj)/ffi.o: $(src)/ffi.rs $(obj)/compiler_builtins.o $(obj)/pin_init.o FORCE +$(call if_changed_rule,rustc_library) $(obj)/bindings.o: private rustc_target_flags = --extern ffi --extern pin_init
rust/ffi.rs +7 -0
@@ -48,3 +48,10 @@ macro_rules! alias {pub use core::ffi::c_void;pub use core::ffi::CStr ; + +pub use pin_init::{ + zeroed, +MaybeZeroable , +Zeroable , +ZeroableOption ,// +};
Signature · 1 line
2.52.0.457.g6b5491de43-goog
Alice Ryhl
reviewerPATCH 2/4 rust: cpufreq: import pin_init::zeroed() from ffi
In reply to Alice RyhlThe zeroed() helper was re-exported from the ffi crate. As this usage of zeroed() has nothing to do with pin-init, use the new re-export.
--- rust/kernel/cpufreq.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
rust/kernel/cpufreq.rs +1 -1
@@ -1012,7 +1012,7 @@ impl<T: Driver> Registration<T> { }else {None }, - ..pin_init::zeroed () + ..ffi::zeroed () };const fn copy_name (name: &'staticCStr ) -> [c_char ;CPUFREQ_NAME_LEN ] {
Signature · 1 line
2.52.0.457.g6b5491de43-goog
Re: [PATCH 2/4] rust: cpufreq: import pin_init::zeroed() from ffi
Quoted 13 lines · Alice Ryhl
The
zeroed()helper was re-exported from the ffi crate. As this usage ofzeroed()has nothing to do with pin-init, use the new re-export.Signed-off-by Alice Ryhl <[email protected]>--- rust/kernel/cpufreq.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)rust/kernel/cpufreq.rs +0
-0
rust/kernel/cpufreq.rs impl<T: Driver> Registration<T> { · line 1018
Quoted 8 lines
rust/kernel/cpufreq.rs +1
-1@@ -1012,7 +1012,7 @@ impl<T: Driver> Registration<T> { }else {None }, - ..pin_init::zeroed () + ..ffi::zeroed () };const fn copy_name (name: &'staticCStr ) -> [c_char ;CPUFREQ_NAME_LEN ] {
Signature · 1 line
viresh
Alice Ryhl
reviewerPATCH 3/4 rust: i2c: import pin_init::zeroed() from ffi
In reply to Alice RyhlThe zeroed() helper was re-exported from the ffi crate. As this usage of zeroed() has nothing to do with pin-init, use the new re-export.
--- rust/kernel/i2c.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
rust/kernel/i2c.rs +2 -2
@@ -46,7 +46,7 @@ impl DeviceId {pub const fn new (id: &'staticCStr ) ->Self {let src = id.to_bytes_with_nul ();build_assert! (src.len () <=Self ::I2C_NAME_SIZE ,"ID exceeds 20 bytes" ); -let mut i2c: bindings::i2c_device_id = pin_init::zeroed (); +let mut i2c: bindings::i2c_device_id = ffi::zeroed ();let mut i =0 ;while i < src.len () { i2c.name[i] = src[i];
@@ -433,7 +433,7 @@ impl I2cBoardInfo {pub const fn new (type_: &'staticCStr , addr:u16 ) ->Self {let src = type_.to_bytes_with_nul ();build_assert! (src.len () <=Self ::I2C_TYPE_SIZE ,"Type exceeds 20 bytes" ); -let mut i2c_board_info: bindings::i2c_board_info = pin_init::zeroed (); +let mut i2c_board_info: bindings::i2c_board_info = ffi::zeroed ();let mut i:usize =0 ;while i < src.len () { i2c_board_info.type_[i] = src[i];
Signature · 1 line
2.52.0.457.g6b5491de43-goog
Alice Ryhl
reviewerPATCH 4/4 rust: acpi: import pin_init::zeroed() from ffi
In reply to Alice RyhlThe zeroed() helper was re-exported from the ffi crate. As this usage of zeroed() has nothing to do with pin-init, use the new re-export.
--- rust/kernel/acpi.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
rust/kernel/acpi.rs +1 -1
@@ -39,7 +39,7 @@ impl DeviceId {pub const fn new (id: &'staticCStr ) ->Self {let src = id.to_bytes_with_nul ();build_assert! (src.len () <=Self ::ACPI_ID_LEN ,"ID exceeds 16 bytes" ); -let mut acpi: bindings::acpi_device_id = pin_init::zeroed (); +let mut acpi: bindings::acpi_device_id = ffi::zeroed ();let mut i =0 ;while i < src.len () { acpi.id[i] = src[i];
Signature · 1 line
2.52.0.457.g6b5491de43-goog
Gary Guo
reviewerQuoted 6 lines · Alice Ryhl
Currently, the Zeroable trait is defined by pin-init because pin-init happens to use the trait. However, zeroed types are useful for many purposes other than pin-init. Also, we wish to implement Zeroable for types generated by bindgen. For both of these reasons, re-export Zeroable from the ffi crate, which is a already dependency of the crates with bindgen output.
I don't see a benefit of re-exporting these from the ffi crate? Especially that we re-export ffi crate from kernel crate anyway, and Zeroable is already in the kernel prelude.
We already derive Zeroable for bindgen via MaybeZeroable derive in rust/bindgen_parameters.
Best,
Gary
Quoted 20 lines
Signed-off-by Alice Ryhl <[email protected]>---
Alice Ryhl (4):
rust: ffi: reexport Zeroable and related items
rust: cpufreq: importpin_init::zeroed()from ffi
rust: i2c: importpin_init::zeroed()from ffi
rust: acpi: importpin_init::zeroed()from ffirust/Makefile | 9 ++++++--- rust/ffi.rs | 7 +++++++ rust/kernel/acpi.rs | 2 +- rust/kernel/cpufreq.rs | 2 +- rust/kernel/i2c.rs | 4 ++-- 5 files changed, 17 insertions(+), 7 deletions(-)---
base-commit:8f0b4cce4481fb22653697cced8d0d04027cb1e8
change-id: 20251202-zeroable-ffi-2beb542376c3Best regards,
Alice Ryhl
reviewerQuoted 8 lines · Gary Guo
Quoted 6 lines · Alice Ryhl
Currently, the Zeroable trait is defined by pin-init because pin-init happens to use the trait. However, zeroed types are useful for many purposes other than pin-init. Also, we wish to implement Zeroable for types generated by bindgen. For both of these reasons, re-export Zeroable from the ffi crate, which is a already dependency of the crates with bindgen output.
Quoted 6 lines
I don't see a benefit of re-exporting these from the
fficrate? Especially that we re-exportfficrate from kernel crate anyway, andZeroableis already in the kernel prelude.We already derive
Zeroablefor bindgen viaMaybeZeroablederive inrust/bindgen_parameters.
I can't find the convo now, but this change is on my list from when we discussed also implementing FromBytes / IntoBytes for the bindings types. To do that, we need to move our FromBytes / IntoBytes traits somewhere that bindings/uapi can access, and we agreed that the ffi crate was a good place for it.
And then for consistency, also reexport Zeroable from the same location.
Alice
Benno Lossin
reviewerQuoted 16 lines · Alice Ryhl
Quoted 8 lines · Gary Guo
Quoted 6 lines · Alice Ryhl
Currently, the Zeroable trait is defined by pin-init because pin-init happens to use the trait. However, zeroed types are useful for many purposes other than pin-init. Also, we wish to implement Zeroable for types generated by bindgen. For both of these reasons, re-export Zeroable from the ffi crate, which is a already dependency of the crates with bindgen output.
Quoted 6 lines
I don't see a benefit of re-exporting these from the
fficrate? Especially that we re-exportfficrate from kernel crate anyway, andZeroableis already in the kernel prelude.We already derive
Zeroablefor bindgen viaMaybeZeroablederive inrust/bindgen_parameters.
Quoted 7 lines
I can't find the convo now, but this change is on my list from when we discussed also implementing FromBytes / IntoBytes for the bindings types. To do that, we need to move our FromBytes / IntoBytes traits somewhere that bindings/uapi can access, and we agreed that the ffi crate was a good place for it.
And then for consistency, also reexport Zeroable from the same location.
I think you also mentioned at some point that using pin_init from bindings seemed strange and also using the pin_init::zeroed() function also doesn't fit, since it doesn't have to do with pinned initialization.
Cheers,
Benno
Gary Guo
reviewerQuoted 23 lines · Benno Lossin
Quoted 22 lines · Alice Ryhl
Quoted 8 lines · Gary Guo
Quoted 6 lines · Alice Ryhl
Currently, the Zeroable trait is defined by pin-init because pin-init happens to use the trait. However, zeroed types are useful for many purposes other than pin-init. Also, we wish to implement Zeroable for types generated by bindgen. For both of these reasons, re-export Zeroable from the ffi crate, which is a already dependency of the crates with bindgen output.
Quoted 6 lines
I don't see a benefit of re-exporting these from the
fficrate? Especially that we re-exportfficrate from kernel crate anyway, andZeroableis already in the kernel prelude.We already derive
Zeroablefor bindgen viaMaybeZeroablederive inrust/bindgen_parameters.I can't find the convo now, but this change is on my list from when we discussed also implementing FromBytes / IntoBytes for the bindings types. To do that, we need to move our FromBytes / IntoBytes traits somewhere that bindings/uapi can access, and we agreed that the ffi crate was a good place for it.
Quoted 6 lines
Quoted 1 line
And then for consistency, also reexport Zeroable from the same location.
I think you also mentioned at some point that using
pin_initfrombindingsseemed strange and also using thepin_init::zeroed()function also doesn't fit, since it doesn't have to do with pinned initialization.
Shouldn't it be that a crate that implements Zeroable / FromBytes / IntoBytes and then pin_init becoming an user of that crate, then?
Best,
Gary
Alice Ryhl
reviewerQuoted 27 lines · Gary Guo
Quoted 25 lines · Benno Lossin
Quoted 23 lines · Alice Ryhl
Quoted 8 lines · Gary Guo
Quoted 6 lines · Alice Ryhl
Currently, the Zeroable trait is defined by pin-init because pin-init happens to use the trait. However, zeroed types are useful for many purposes other than pin-init. Also, we wish to implement Zeroable for types generated by bindgen. For both of these reasons, re-export Zeroable from the ffi crate, which is a already dependency of the crates with bindgen output.
Quoted 6 lines
I don't see a benefit of re-exporting these from the
fficrate? Especially that we re-exportfficrate from kernel crate anyway, andZeroableis already in the kernel prelude.We already derive
Zeroablefor bindgen viaMaybeZeroablederive inrust/bindgen_parameters.I can't find the convo now, but this change is on my list from when we discussed also implementing FromBytes / IntoBytes for the bindings types. To do that, we need to move our FromBytes / IntoBytes traits somewhere that bindings/uapi can access, and we agreed that the ffi crate was a good place for it.
And then for consistency, also reexport Zeroable from the same location.
Quoted 7 lines
Quoted 4 lines
I think you also mentioned at some point that using
pin_initfrombindingsseemed strange and also using thepin_init::zeroed()function also doesn't fit, since it doesn't have to do with pinned initialization.Shouldn't it be that a crate that implements Zeroable / FromBytes / IntoBytes and then
pin_initbecoming an user of that crate, then?
The Zeroable trait has to be in pin-init because it's also outside the kernel. You *could* add yet another crate just for this and let pin-init depend on it, but just putting it in the existing ffi seems reasonable to me, and ffi is not a bad name for the owner of those traits anyway.
Though I guess if we add zerocopy, that concern goes away.
Alice
Benno Lossin
reviewerQuoted 28 lines · Alice Ryhl
Quoted 27 lines · Gary Guo
Quoted 25 lines · Benno Lossin
Quoted 23 lines · Alice Ryhl
Quoted 8 lines · Gary Guo
Quoted 6 lines · Alice Ryhl
Currently, the Zeroable trait is defined by pin-init because pin-init happens to use the trait. However, zeroed types are useful for many purposes other than pin-init. Also, we wish to implement Zeroable for types generated by bindgen. For both of these reasons, re-export Zeroable from the ffi crate, which is a already dependency of the crates with bindgen output.
Quoted 6 lines
I don't see a benefit of re-exporting these from the
fficrate? Especially that we re-exportfficrate from kernel crate anyway, andZeroableis already in the kernel prelude.We already derive
Zeroablefor bindgen viaMaybeZeroablederive inrust/bindgen_parameters.I can't find the convo now, but this change is on my list from when we discussed also implementing FromBytes / IntoBytes for the bindings types. To do that, we need to move our FromBytes / IntoBytes traits somewhere that bindings/uapi can access, and we agreed that the ffi crate was a good place for it.
And then for consistency, also reexport Zeroable from the same location.
Quoted 7 lines
Quoted 7 lines
Quoted 4 lines
I think you also mentioned at some point that using
pin_initfrombindingsseemed strange and also using thepin_init::zeroed()function also doesn't fit, since it doesn't have to do with pinned initialization.Shouldn't it be that a crate that implements Zeroable / FromBytes / IntoBytes and then
pin_initbecoming an user of that crate, then?
I think that's a little bit too much effort just for "nice naming".
Quoted 7 lines
The Zeroable trait has to be in pin-init because it's also outside the kernel. You *could* add yet another crate just for this and let pin-init depend on it, but just putting it in the existing ffi seems reasonable to me, and ffi is not a bad name for the owner of those traits anyway.
Though I guess if we add zerocopy, that concern goes away.
Indeed, I would just move pin-init to that.
Cheers,
Benno