From: "Daniel P. Berrange" <berrange@redhat.com>
To: Markus Armbruster <armbru@redhat.com>, Eric Blake <eblake@redhat.com>
Cc: qemu-devel@nongnu.org
Subject: Re: [Qemu-devel] [PULL 14/15] qapi: Don't box branches of flat unions
Date: Tue, 23 Feb 2016 14:50:39 +0000 [thread overview]
Message-ID: <20160223145039.GH22780@redhat.com> (raw)
In-Reply-To: <1455884286-26272-15-git-send-email-armbru@redhat.com>
On Fri, Feb 19, 2016 at 01:18:05PM +0100, Markus Armbruster wrote:
> From: Eric Blake <eblake@redhat.com>
>
> There's no reason to do two malloc's for a flat union; let's just
> inline the branch struct directly into the C union branch of the
> flat union.
>
> Surprisingly, fewer clients were actually using explicit references
> to the branch types in comparison to the number of flat unions
> thus modified.
>
> This lets us reduce the hack in qapi-types:gen_variants() added in
> the previous patch; we no longer need to distinguish between
> alternates and flat unions.
>
> The change to unboxed structs means that u.data (added in commit
> cee2dedb) is now coincident with random fields of each branch of
> the flat union, whereas beforehand it was only coincident with
> pointers (since all branches of a flat union have to be objects).
> Note that this was already the case for simple unions - but there
> we got lucky. Remember, visit_start_union() blindly returns true
> for all visitors except for the dealloc visitor, where it returns
> the value !!obj->u.data, and that this result then controls
> whether to proceed with the visit to the variant. Pre-patch,
> this meant that flat unions were testing whether the boxed pointer
> was still NULL, and thereby skipping visit_end_implicit_struct()
> and avoiding a NULL dereference if the pointer had not been
> allocated. The same was true for simple unions where the current
> branch had pointer type, except there we bypassed visit_type_FOO().
> But for simple unions where the current branch had scalar type, the
> contents of that scalar meant that the decision to call
> visit_type_FOO() was data-dependent - the reason we got lucky there
> is that visit_type_FOO() for all scalar types in the dealloc visitor
> is a no-op (only the pointer variants had anything to free), so it
> did not matter whether the dealloc visit was skipped. But with this
> patch, we would risk leaking memory if we could skip a call to
> visit_type_FOO_fields() based solely on a data-dependent decision.
>
> But notice: in the dealloc visitor, visit_type_FOO() already handles
> a NULL obj - it was only the visit_type_implicit_FOO() that was
> failing to check for NULL. And now that we have refactored things to
> have the branch be part of the parent struct, we no longer have a
> separate pointer that can be NULL in the first place. So we can just
> delete the call to visit_start_union() altogether, and blindly visit
> the branch type; there is no change in behavior except to the dealloc
> visitor, where we now unconditionally visit the branch, but where that
> visit is now always safe (for a flat union, we can no longer
> dereference NULL, and for a simple union, visit_type_FOO() was already
> safely handling NULL on pointer types).
>
> Unfortunately, simple unions are not as easy to switch to unboxed
> layout; because we are special-casing the hidden implicit type with
> a single 'data' member, we really DO need to keep calling another
> layer of visit_start_struct(), with a second malloc; although there
> are some cleanups planned for simple unions in later patches.
>
> visit_start_union() and gen_visit_implicit_struct() are now unused.
> Drop them.
>
> Note that after this patch, the only remaining use of
> visit_start_implicit_struct() is for alternate types; the next patch
> will do further cleanup based on that fact.
>
> Signed-off-by: Eric Blake <eblake@redhat.com>
> Message-Id: <1455778109-6278-14-git-send-email-eblake@redhat.com>
> [Dead code deletion squashed in, commit message updated accordingly]
> Signed-off-by: Markus Armbruster <armbru@redhat.com>
> ---
> cpus.c | 18 ++++++------------
> hmp.c | 12 ++++++------
> include/qapi/visitor-impl.h | 2 --
> include/qapi/visitor.h | 1 -
> qapi/qapi-dealloc-visitor.c | 26 --------------------------
> qapi/qapi-visit-core.c | 8 --------
> scripts/qapi-types.py | 13 +++----------
> scripts/qapi-visit.py | 36 ++----------------------------------
> tests/test-qmp-input-visitor.c | 10 +++++-----
> tests/test-qmp-output-visitor.c | 6 ++----
> 10 files changed, 24 insertions(+), 108 deletions(-)
This change has broken my pending LUKS encryption patch series[1], and
I'm not entirely sure of the best way to resolve it.
I'll simplify the schema slighty for sake of illustration here.
Consider I have the following:
{ 'struct': 'QCryptoBlockOptionsBase',
'data': { 'format': 'QCryptoBlockFormat' }}
{ 'struct': 'QCryptoBlockOptionsQCow',
'data': { '*key-secret': 'str' }}
{ 'struct': 'QCryptoBlockOptionsLUKS',
'data': { '*key-secret': 'str',
'*cipher-alg': 'QCryptoCipherAlgorithm',
'*cipher-mode': 'QCryptoCipherMode',
'*ivgen-alg': 'QCryptoIVGenAlgorithm',
'*ivgen-hash-alg': 'QCryptoHashAlgorithm',
'*hash-alg': 'QCryptoHashAlgorithm'}}
{ 'union': 'QCryptoBlockOptions',
'base': 'QCryptoBlockOptionsBase',
'discriminator': 'format',
'data': { 'qcow': 'QCryptoBlockOptionsQCow',
'luks': 'QCryptoBlockOptionsLUKS' } }
If I want to visit QCryptoBlockOptions, allocating a new struct
instance, I can still do so
QCryptoBlockOptions *opts = NULL;
visit_type_QCryptoBlockOptions(v, "block", &opts, errp)
Similarly, if I want to visit QCryptoBlockOptionsLUKS, allocating
a new struct instance, I can still do so
QCryptoBlockOptionsLUKS *opts = NULL;
visit_type_QCryptoBlockOptionsLUKS(v, "luks", &opts, errp)
My code needs todo something a little different though - it needs
to directly visit one of the union branches. Previously, I could
allocate a QCryptoBlockOptions and then visit a specific union
branch like this:
QCryptoBlockOptions *opts = NULL;
opts = g_new0(QCryptoBlockOptions, 1);
opts.format = Q_CRYPTO_BLOCK_FORMAT_LUKS
visit_type_QCryptoBlockOptionsLUKS(v, "luks", &opts.u.luks, errp)
I need todo this, because my visitor instance does not have any
'format' field to visit - we know the format upfront from the
block layer driver choice. So we need to directly visit the
appropriate inline union branch. This no longer works, because
the opts.u.luks field is now inlined instead of being boxed :-(
I could visit_type_QCryptoBlockOptionsLUKS_fields(v, opts.u.luks, errp)
but the '_fields' methods are all declared static in qapi-visit.c
preventing their use.
IMHO, now that QAPI inlines the flat unions, we should be making
the _fields() methods public.
Regards,
Daniel
[1] https://lists.gnu.org/archive/html/qemu-devel/2016-02/msg03176.html
--
|: http://berrange.com -o- http://www.flickr.com/photos/dberrange/ :|
|: http://libvirt.org -o- http://virt-manager.org :|
|: http://autobuild.org -o- http://search.cpan.org/~danberr/ :|
|: http://entangle-photo.org -o- http://live.gnome.org/gtk-vnc :|
next prev parent reply other threads:[~2016-02-23 14:50 UTC|newest]
Thread overview: 21+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-02-19 12:17 [Qemu-devel] [PULL 00/15] QAPI patches for 2016-02-19 Markus Armbruster
2016-02-19 12:17 ` [Qemu-devel] [PULL 01/15] qapi-visit: Honor prefix of discriminator enum Markus Armbruster
2016-02-19 12:17 ` [Qemu-devel] [PULL 02/15] qapi: Simplify excess input reporting in input visitors Markus Armbruster
2016-02-19 12:17 ` [Qemu-devel] [PULL 03/15] qapi: Forbid empty unions and useless alternates Markus Armbruster
2016-02-19 12:17 ` [Qemu-devel] [PULL 04/15] qapi: Forbid 'any' inside an alternate Markus Armbruster
2016-02-19 12:17 ` [Qemu-devel] [PULL 05/15] qapi: Add tests of complex objects within alternate Markus Armbruster
2016-02-19 12:17 ` [Qemu-devel] [PULL 06/15] qapi-visit: Simplify how we visit common union members Markus Armbruster
2016-02-19 12:17 ` [Qemu-devel] [PULL 07/15] qapi: Visit variants in visit_type_FOO_fields() Markus Armbruster
2016-02-19 12:17 ` [Qemu-devel] [PULL 08/15] qapi-visit: Unify struct and union visit Markus Armbruster
2016-02-19 12:18 ` [Qemu-devel] [PULL 09/15] qapi-visit: Less indirection in visit_type_Foo_fields() Markus Armbruster
2016-02-19 12:18 ` [Qemu-devel] [PULL 10/15] qapi: Adjust layout of FooList types Markus Armbruster
2016-02-19 12:18 ` [Qemu-devel] [PULL 11/15] qapi: Emit structs used as variants in topological order Markus Armbruster
2016-02-19 12:18 ` [Qemu-devel] [PULL 12/15] qapi-visit: Use common idiom in gen_visit_fields_decl() Markus Armbruster
2016-02-19 12:18 ` [Qemu-devel] [PULL 13/15] qapi: Don't box struct branch of alternate Markus Armbruster
2016-02-19 12:18 ` [Qemu-devel] [PULL 14/15] qapi: Don't box branches of flat unions Markus Armbruster
2016-02-23 14:50 ` Daniel P. Berrange [this message]
2016-02-23 15:18 ` Eric Blake
2016-02-23 15:30 ` Eric Blake
2016-02-23 15:33 ` Daniel P. Berrange
2016-02-19 12:18 ` [Qemu-devel] [PULL 15/15] qapi: Change visit_start_implicit_struct to visit_start_alternate Markus Armbruster
2016-02-19 15:19 ` [Qemu-devel] [PULL 00/15] QAPI patches for 2016-02-19 Peter Maydell
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=20160223145039.GH22780@redhat.com \
--to=berrange@redhat.com \
--cc=armbru@redhat.com \
--cc=eblake@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 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).