qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Like Xu <like.xu@linux.intel.com>
To: Markus Armbruster <armbru@redhat.com>,
	Igor Mammedov <imammedo@redhat.com>
Cc: Peter Maydell <peter.maydell@linaro.org>,
	Thomas Huth <thuth@redhat.com>,
	Eduardo Habkost <ehabkost@redhat.com>,
	qemu-trivial@nongnu.org, qemu-devel@nongnu.org,
	Paolo Bonzini <pbonzini@redhat.com>
Subject: Re: [Qemu-devel] [PATCH v2] vl.c: make current_machine as non-global variable
Date: Thu, 4 Apr 2019 15:41:40 +0800	[thread overview]
Message-ID: <7e76d718-54fe-72b9-27b5-68f239859868@linux.intel.com> (raw)
In-Reply-To: <871s2k8jxo.fsf@dusky.pond.sub.org>

On 2019/4/3 0:13, Markus Armbruster wrote:
> Igor Mammedov <imammedo@redhat.com> writes:
> 
>> On Tue, 2 Apr 2019 21:09:39 +0800
>> Like Xu <like.xu@linux.intel.com> wrote:
>>
>>> On 2019/4/2 19:27, Markus Armbruster wrote:
>>>> Like Xu <like.xu@linux.intel.com> writes:
>>>>    
>>>>> This patch makes the remaining dozen or so uses of the global
>>>>> current_machine outside vl.c use qdev_get_machine() instead,
>>>>> and then make current_machine local to vl.c instead of global.
>>>>>
>>>>> Signed-off-by: Like Xu <like.xu@linux.intel.com>
>>>>
>>>> You effectively replace
>>>>
>>>>       current_machine
>>>>
>>>> by
>>>>
>>>>       MACHINE(qdev_get_machine())
>>>>
>>>> qdev_get_machine() uses container_get(), which has a side effect: any
>>>> path component that doesn't exist already gets created as "container"
>>>> object.  In case of qdev_get_machine(), that's just "/machine".
>>>>
>>>> Creating "/machine" as "container" is of course wrong.  You therefore
>>>> must not use qdev_get_machine() before main() creates "/machine".  It
>>>> does like this:
>>>>
>>>>       object_property_add_child(object_get_root(), "machine",
>>>>                                 OBJECT(current_machine), &error_abort);
>>>>
>>>> I recently had several cases of code rearrangements explode because the
>>>> reordered code called qdev_get_machine() too early.  Makes me rather
>>>> skeptical about this patch.  To be frank, I consider qdev_get_machine()
>>>> a trap for the unwary.  container_get(), too.
>>>>
>>>> If we decide using it to make current_machine static a good idea anyway,
>>>> we need to check the new uses carefully to make sure they can't run
>>>> before main() creates "/machine".
>>
>> maybe we can assert in qdev_get_machine() if machine hasn't been created yet?
>> with this at least it will be hard to misuse function or catch invalid users.
>> (but it still might miss some use cases/CLI options which are not tested)
> 
> Good idea.  When my code created "/machine" as a container, debugging
> the resulting crash took me a bit of time.  The assertion you propose
> would've saved me some.
> 
> 

Good idea and please help review if this assertion in qdev_get_machine() 
could help for your code rearrangement:

       if (dev == NULL) {
           dev = container_get(object_get_root(), "/machine");
       }

+     assert(object_dynamic_cast(dev, TYPE_MACHINE) != NULL);
       return dev;

      parent reply	other threads:[~2019-04-04  7:41 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-04-02  8:48 [Qemu-devel] [PATCH v2] vl.c: make current_machine as non-global variable Like Xu
2019-04-02 11:27 ` Markus Armbruster
2019-04-02 13:09   ` Like Xu
2019-04-02 15:47     ` Igor Mammedov
2019-04-02 16:13       ` Markus Armbruster
2019-04-02 16:23         ` Peter Maydell
2019-04-02 19:16           ` Eduardo Habkost
2019-04-03  0:03             ` Peter Maydell
2019-04-04 10:05           ` Igor Mammedov
2019-04-04 10:41             ` Peter Maydell
2019-04-04  7:41         ` Like Xu [this message]

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=7e76d718-54fe-72b9-27b5-68f239859868@linux.intel.com \
    --to=like.xu@linux.intel.com \
    --cc=armbru@redhat.com \
    --cc=ehabkost@redhat.com \
    --cc=imammedo@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=peter.maydell@linaro.org \
    --cc=qemu-devel@nongnu.org \
    --cc=qemu-trivial@nongnu.org \
    --cc=thuth@redhat.com \
    /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).