From: Martin Kletzander <mkletzan@redhat.com>
To: Paolo Bonzini <pbonzini@redhat.com>
Cc: qemu-devel <qemu-devel@nongnu.org>,
"open list:Block layer core" <qemu-block@nongnu.org>,
Manos Pitsidianakis <manos.pitsidianakis@linaro.org>,
qemu-rust@nongnu.org, Hanna Reitz <hreitz@redhat.com>,
Kevin Wolf <kwolf@redhat.com>
Subject: Re: [PATCH 1/2] rust: Add antoher variant for impl_vmstate_struct! macro
Date: Mon, 4 Aug 2025 10:56:01 +0200 [thread overview]
Message-ID: <aJB1oeH2eTIeeJne@wheatley.k8r.cz> (raw)
In-Reply-To: <CABgObfYBVF3aCOBtX-eQcm4M-WNAHxbMcjsUKiY3rLkinGe1qA@mail.gmail.com>
[-- Attachment #1: Type: text/plain, Size: 4566 bytes --]
On Fri, Aug 01, 2025 at 11:44:03PM +0200, Paolo Bonzini wrote:
>Il ven 1 ago 2025, 17:00 Martin Kletzander <mkletzan@redhat.com> ha scritto:
>
>> From: Martin Kletzander <mkletzan@redhat.com>
>>
>> In some cases (e.g. in vmstate_tests.rs) the second argument to
>> impl_vmstate_struct! is actually an existing struct which is then
>> copied (since VMStateDescription implements Copy) when saved into the
>> static VMSD using .get(). That is not a problem because it is part of
>> the data segment and the pointers are not being free'd since they point
>> to static data. But it is a problem when tests rely on comparing the
>> VMState descriptions as pointers rather than contents. And it also
>> wastes space, more or less.
>>
>> Introduce second variant of the macro which can, instead of the
>> expression, take an identifier or what looks like a reference. This
>> second variant is added before the current variant so that it has
>> preference, and only references the existing static data from it.
>>
>> This way tests are fixed and space is saved.
>>
>> And now that the VMStateDescription checking is fixed we can also check
>> for the right value in test_vmstate_struct_varray_uint8_wrapper().
>>
>> Signed-off-by: Martin Kletzander <mkletzan@redhat.com>
>> ---
>> I'm not sure whether this is caused by different utility on my system or
>> bash
>> version or whatever, but without this patch these three tests fail for me
>> and
>> this patch fixes it.
>>
>
>I found something similar, though I wasn't sure if it was broken in master
>as well or only in the rust-next branch.
>
It is not broken on master, but it *was* broken on rust-next.
>If that works in master as well, I would remove completely the possibility
>of using &FOO, and always use .as_ref(). It's more efficient as you said,
>and there's no reason that I know to use the less efficient one.
>
That would mean that you cannot do what's done in e.g. pl011
impl_vmstate_struct!(
PL011Registers,
VMStateDescriptionBuilder::<PL011Registers>::new()
...
.build()
);
requiring you to always do:
static/const VMSTATE_PL011_REGISTERS: VMStateDescription<PL011Registers> =
VMStateDescriptionBuilder::<PL011Registers>::new()
...
.build();
impl_vmstate_struct!(PL011Registers, VMSTATE_PL011_REGISTERS);
*BUT* of course I had to rebase the patches on top of current rust-next
on Friday and there were some of your commits from Thursday which I now
see actually fix all what I tried fixing before as well. I tried
finding the previous commit on which I saw all the issues and after some
rebuilding I could not. So it is now not even broken on rust-next.
This way I completely wasted your time, but at least learned something
that's happening in the code. Sorry for that.
Martin
>Paolo
>
> rust/qemu-api/src/vmstate.rs | 11 +++++++++++
>> rust/qemu-api/tests/vmstate_tests.rs | 1 +
>> 2 files changed, 12 insertions(+)
>>
>> diff --git a/rust/qemu-api/src/vmstate.rs b/rust/qemu-api/src/vmstate.rs
>> index b5c6b764fbba..716e52afe740 100644
>> --- a/rust/qemu-api/src/vmstate.rs
>> +++ b/rust/qemu-api/src/vmstate.rs
>> @@ -449,6 +449,17 @@ macro_rules! vmstate_validate {
>> /// description of the struct.
>> #[macro_export]
>> macro_rules! impl_vmstate_struct {
>> + ($type:ty, $(&)?$vmsd:ident) => {
>> + unsafe impl $crate::vmstate::VMState for $type {
>> + const BASE: $crate::bindings::VMStateField =
>> + $crate::bindings::VMStateField {
>> + vmsd: $vmsd.as_ref(),
>> + size: ::core::mem::size_of::<$type>(),
>> + flags: $crate::bindings::VMStateFlags::VMS_STRUCT,
>> + ..$crate::zeroable::Zeroable::ZERO
>> + };
>> + }
>> + };
>> ($type:ty, $vmsd:expr) => {
>> unsafe impl $crate::vmstate::VMState for $type {
>> const BASE: $crate::bindings::VMStateField = {
>> diff --git a/rust/qemu-api/tests/vmstate_tests.rs b/rust/qemu-api/tests/
>> vmstate_tests.rs
>> index 2c0670ba0eed..7d3180e6c2ea 100644
>> --- a/rust/qemu-api/tests/vmstate_tests.rs
>> +++ b/rust/qemu-api/tests/vmstate_tests.rs
>> @@ -320,6 +320,7 @@ fn test_vmstate_struct_varray_uint8_wrapper() {
>> b"arr_a_wrap\0"
>> );
>> assert_eq!(foo_fields[5].num_offset, 228);
>> + assert_eq!(foo_fields[5].vmsd, VMSTATE_FOOA.as_ref());
>> assert!(unsafe { foo_fields[5].field_exists.unwrap()(foo_b_p, 0) });
>>
>> // The last VMStateField in VMSTATE_FOOB.
>> --
>> 2.50.1
>>
>>
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]
next prev parent reply other threads:[~2025-08-04 8:58 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-08-01 14:59 [PATCH 0/2] Few fixes I found when building QEMU with Rust Martin Kletzander
2025-08-01 14:59 ` [PATCH 1/2] rust: Add antoher variant for impl_vmstate_struct! macro Martin Kletzander
2025-08-01 21:44 ` Paolo Bonzini
2025-08-04 8:56 ` Martin Kletzander [this message]
2025-08-04 10:08 ` Paolo Bonzini
2025-08-04 12:04 ` Martin Kletzander
2025-08-04 13:06 ` Paolo Bonzini
2025-08-01 14:59 ` [PATCH 2/2] tests/qemu-iotests: Indent expected error messages Martin Kletzander
2025-08-01 15:48 ` Daniel P. Berrangé
2025-08-01 19:09 ` Fabiano Rosas
2025-08-04 8:20 ` Martin Kletzander
2025-08-04 11:33 ` Kevin Wolf
2025-08-06 6:54 ` Dr. Werner Fink
2025-08-11 9:05 ` Martin Kletzander
2025-08-12 15:35 ` Kevin Wolf
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=aJB1oeH2eTIeeJne@wheatley.k8r.cz \
--to=mkletzan@redhat.com \
--cc=hreitz@redhat.com \
--cc=kwolf@redhat.com \
--cc=manos.pitsidianakis@linaro.org \
--cc=pbonzini@redhat.com \
--cc=qemu-block@nongnu.org \
--cc=qemu-devel@nongnu.org \
--cc=qemu-rust@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).