From: Zhao Liu <zhao1.liu@intel.com>
To: Paolo Bonzini <pbonzini@redhat.com>
Cc: qemu-devel@nongnu.org, junjie.mao@hotmail.com
Subject: Re: [PATCH 01/12] rust: qom: add ParentField
Date: Wed, 25 Dec 2024 16:54:32 +0800 [thread overview]
Message-ID: <Z2vISD28uK11AevU@intel.com> (raw)
In-Reply-To: <20241220142955.652636-2-pbonzini@redhat.com>
On Fri, Dec 20, 2024 at 03:29:43PM +0100, Paolo Bonzini wrote:
> Date: Fri, 20 Dec 2024 15:29:43 +0100
> From: Paolo Bonzini <pbonzini@redhat.com>
> Subject: [PATCH 01/12] rust: qom: add ParentField
> X-Mailer: git-send-email 2.47.1
>
> Add a type that, together with the C function object_deinit, ensures the
> correct drop order for QOM objects relative to their superclasses.
>
> Right now it is not possible to implement the Drop trait for QOM classes
> that are defined in Rust, as the drop() function would not be called when
> the object goes away; instead what is called is ObjectImpl::INSTANCE_FINALIZE.
> It would be nice for INSTANCE_FINALIZE to just drop the object, but this has
> a problem: suppose you have
>
> pub struct MySuperclass {
> parent: DeviceState,
> field: Box<MyData>,
> ...
> }
>
> impl Drop for MySuperclass {
> ...
> }
>
> pub struct MySubclass {
> parent: MySuperclass,
> ...
> }
>
> and an instance_finalize implementation that is like
>
> unsafe extern "C" fn drop_object<T: ObjectImpl>(obj: *mut Object) {
> unsafe { std::ptr::drop_in_place(obj.cast::<T>()) }
> }
>
> When instance_finalize is called for MySubclass, it will walk the struct's
> list of fields and call the drop method for MySuperclass. Then, object_deinit
> recurses to the superclass and calls the same drop method again. This
> will cause double-freeing of the Box<Data>.
>
> What's happening here is that QOM wants to control the drop order of
> MySuperclass and MySubclass's fields. To do so, the parent field must
> be marked ManuallyDrop<>, which is quite ugly. Instead, add a wrapper
> type ParentField<> that is specific to QOM. This hides the implementation
> detail of *what* is special about the ParentField, and will also be easy
> to check in the #[derive(Object)] macro.
>
> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
> ---
> rust/hw/char/pl011/src/device.rs | 6 ++--
> rust/qemu-api/src/qom.rs | 56 +++++++++++++++++++++++++++++---
> rust/qemu-api/tests/tests.rs | 4 +--
> 3 files changed, 57 insertions(+), 9 deletions(-)
...
> unsafe extern "C" fn rust_instance_init<T: ObjectImpl>(obj: *mut Object) {
> // SAFETY: obj is an instance of T, since rust_instance_init<T>
> // is called from QOM core as the instance_init function
> @@ -151,8 +198,9 @@ fn as_ref(&self) -> &$parent {
> ///
> /// - the struct must be `#[repr(C)]`;
> ///
> -/// - the first field of the struct must be of the instance struct corresponding
> -/// to the superclass, which is `ObjectImpl::ParentType`
> +/// - the first field of the struct must be of type
> +/// [`ParentField<T>`](ParentField), where `T` is the parent type
> +/// [`ObjectImpl::ParentType`]
> ///
> /// - likewise, the first field of the `Class` must be of the class struct
> /// corresponding to the superclass, which is `ObjectImpl::ParentType::Class`.
I think the "likewise" word should be deleted as well, since Class'
parent field doesn't need any wrapper because Class also doesn't have
finalize method. The remaining description is clear enough.
Others look good to me!
Reviewed-by: Zhao Liu <zhao1.liu@intel.com>
next prev parent reply other threads:[~2024-12-25 8:36 UTC|newest]
Thread overview: 26+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-12-20 14:29 [PATCH 00/12] Next round of qemu_api patches Paolo Bonzini
2024-12-20 14:29 ` [PATCH 01/12] rust: qom: add ParentField Paolo Bonzini
2024-12-25 8:54 ` Zhao Liu [this message]
2024-12-20 14:29 ` [PATCH 02/12] rust: add a utility module for compile-time type checks Paolo Bonzini
2024-12-25 16:24 ` Zhao Liu
2024-12-27 4:26 ` Paolo Bonzini
2024-12-20 14:29 ` [PATCH 03/12] rust: macros: check that the first field of a #[derive(Object)] struct is a ParentField Paolo Bonzini
2024-12-25 16:41 ` Zhao Liu
2024-12-20 14:29 ` [PATCH 04/12] rust: macros: check that #[derive(Object)] requires #[repr(C)] Paolo Bonzini
2024-12-25 16:46 ` Zhao Liu
2024-12-20 14:29 ` [PATCH 05/12] rust: qom: automatically use Drop trait to implement instance_finalize Paolo Bonzini
2024-12-20 14:29 ` [PATCH 06/12] rust: qom: move device_id to PL011 class side Paolo Bonzini
2024-12-26 3:07 ` Zhao Liu
2024-12-20 14:29 ` [PATCH 07/12] rust: pl011: only leave embedded object initialization in instance_init Paolo Bonzini
2024-12-26 3:20 ` Zhao Liu
2024-12-20 14:29 ` [PATCH 08/12] rust: qom: make INSTANCE_POST_INIT take a shared reference Paolo Bonzini
2024-12-26 3:29 ` Zhao Liu
2024-12-20 14:29 ` [PATCH 09/12] rust: qdev: expose inherited methods to subclasses of SysBusDevice Paolo Bonzini
2024-12-26 4:47 ` Zhao Liu
2024-12-20 14:29 ` [PATCH 10/12] rust: qemu-api-macros: extend error reporting facility to parse errors Paolo Bonzini
2024-12-26 4:50 ` Zhao Liu
2024-12-20 14:29 ` [PATCH 11/12] rust: qemu-api-macros: add automatic TryFrom/TryInto derivation Paolo Bonzini
2024-12-26 7:14 ` Zhao Liu
2024-12-20 14:29 ` [PATCH 12/12] rust: hide warnings for subprojects Paolo Bonzini
2024-12-26 7:21 ` Zhao Liu
2024-12-23 6:40 ` [PATCH 00/12] Next round of qemu_api patches Zhao Liu
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=Z2vISD28uK11AevU@intel.com \
--to=zhao1.liu@intel.com \
--cc=junjie.mao@hotmail.com \
--cc=pbonzini@redhat.com \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.