[PATCH 09/10] rust: bindings: add Sync markers to types referred to by MemoryRegionOps

Paolo Bonzini posted 10 patches 2 weeks, 4 days ago
[PATCH 09/10] rust: bindings: add Sync markers to types referred to by MemoryRegionOps
Posted by Paolo Bonzini 2 weeks, 4 days ago
This is needed for the MemoryRegionOps<T> to be declared as static;
Rust requires static elements to be Sync.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 rust/qemu-api/src/bindings.rs | 19 ++++++++++++++++++-
 rust/qemu-api/src/irq.rs      |  3 +++
 2 files changed, 21 insertions(+), 1 deletion(-)

diff --git a/rust/qemu-api/src/bindings.rs b/rust/qemu-api/src/bindings.rs
index 8a9b821bb91..3f61264ab80 100644
--- a/rust/qemu-api/src/bindings.rs
+++ b/rust/qemu-api/src/bindings.rs
@@ -21,7 +21,24 @@
 #[cfg(not(MESON))]
 include!(concat!(env!("OUT_DIR"), "/bindings.inc.rs"));
 
-unsafe impl Send for Property {}
+// SAFETY: these are implemented in C; the bindings need to assert that the
+// BQL is taken, either directly or via `BqlCell` and `BqlRefCell`.
+unsafe impl Sync for BusState {}
+unsafe impl Sync for CharBackend {}
+unsafe impl Sync for Chardev {}
+unsafe impl Sync for Clock {}
+unsafe impl Sync for DeviceState {}
+unsafe impl Sync for MemoryRegion {}
+unsafe impl Sync for ObjectClass {}
+unsafe impl Sync for Object {}
+unsafe impl Sync for SysBusDevice {}
+
+// SAFETY: this is a pure data struct
+unsafe impl Sync for CoalescedMemoryRange {}
+
+// SAFETY: these are constants and vtables; the Sync requirements are deferred
+// to the unsafe callbacks that they contain
+unsafe impl Sync for MemoryRegionOps {}
 unsafe impl Sync for Property {}
 unsafe impl Sync for TypeInfo {}
 unsafe impl Sync for VMStateDescription {}
diff --git a/rust/qemu-api/src/irq.rs b/rust/qemu-api/src/irq.rs
index 378e5202951..638545c3a64 100644
--- a/rust/qemu-api/src/irq.rs
+++ b/rust/qemu-api/src/irq.rs
@@ -43,6 +43,9 @@ pub struct InterruptSource<T = bool>
     _marker: PhantomData<T>,
 }
 
+// SAFETY: the implementation asserts via `BqlCell` that the BQL is taken
+unsafe impl<T> Sync for InterruptSource<T> where c_int: From<T> {}
+
 impl InterruptSource<bool> {
     /// Send a low (`false`) value to the interrupt sink.
     pub fn lower(&self) {
-- 
2.47.1
Re: [PATCH 09/10] rust: bindings: add Sync markers to types referred to by MemoryRegionOps
Posted by Zhao Liu 1 week, 2 days ago
On Fri, Jan 17, 2025 at 08:40:02PM +0100, Paolo Bonzini wrote:
> Date: Fri, 17 Jan 2025 20:40:02 +0100
> From: Paolo Bonzini <pbonzini@redhat.com>
> Subject: [PATCH 09/10] rust: bindings: add Sync markers to types referred
>  to by MemoryRegionOps
> X-Mailer: git-send-email 2.47.1
> 
> This is needed for the MemoryRegionOps<T> to be declared as static;
> Rust requires static elements to be Sync.
> 
> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
> ---
>  rust/qemu-api/src/bindings.rs | 19 ++++++++++++++++++-
>  rust/qemu-api/src/irq.rs      |  3 +++
>  2 files changed, 21 insertions(+), 1 deletion(-)

Reviewed-by: Zhao Liu <zhao1.liu@intel.com>

> diff --git a/rust/qemu-api/src/bindings.rs b/rust/qemu-api/src/bindings.rs
> index 8a9b821bb91..3f61264ab80 100644
> --- a/rust/qemu-api/src/bindings.rs
> +++ b/rust/qemu-api/src/bindings.rs
> @@ -21,7 +21,24 @@
>  #[cfg(not(MESON))]
>  include!(concat!(env!("OUT_DIR"), "/bindings.inc.rs"));
>  
> -unsafe impl Send for Property {}

I guess this is a rebase mistake since latest rust-next drops this
change :-).

> +// SAFETY: these are implemented in C; the bindings need to assert that the
> +// BQL is taken, either directly or via `BqlCell` and `BqlRefCell`.
> +unsafe impl Sync for BusState {}

We haven't used BusState but it's fine, as the basic support, and after
all bindings file has this type.