qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [Qemu-devel] qdev_get_machine() can return something non-NULL but not TYPE_MACHINE
@ 2017-09-04 16:36 Peter Maydell
  2017-09-04 19:38 ` Igor Mammedov
  0 siblings, 1 reply; 8+ messages in thread
From: Peter Maydell @ 2017-09-04 16:36 UTC (permalink / raw)
  To: QEMU Developers; +Cc: Igor Mammedov, Paolo Bonzini

I just got bitten by qdev_get_machine()'s behaviour on the user-only
emulators, where it can return something that isn't NULL and isn't
an instance of TYPE_MACHINE either.

It looks like maybe this can happen in some cases in softmmu too,
judging by the way that qdev_get_hotplug_handler() does an
object_dynamic_cast() check that it really got back a TYPE_MACHINE.

Is this intentional? Does anything rely on qdev_get_machine()
returning something odd like this?

In the code I have which ran into this I can just make it do an
object_dynamic_cast() check like the hotplug_handler code does,
but if the current implementation is intentional we should
probably document that this is what you're supposed to do.

thanks
-- PMM

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [Qemu-devel] qdev_get_machine() can return something non-NULL but not TYPE_MACHINE
  2017-09-04 16:36 [Qemu-devel] qdev_get_machine() can return something non-NULL but not TYPE_MACHINE Peter Maydell
@ 2017-09-04 19:38 ` Igor Mammedov
  2017-09-05  9:08   ` Peter Maydell
  0 siblings, 1 reply; 8+ messages in thread
From: Igor Mammedov @ 2017-09-04 19:38 UTC (permalink / raw)
  To: Peter Maydell; +Cc: QEMU Developers, Paolo Bonzini

On Mon, 4 Sep 2017 17:36:59 +0100
Peter Maydell <peter.maydell@linaro.org> wrote:

> I just got bitten by qdev_get_machine()'s behaviour on the user-only
> emulators, where it can return something that isn't NULL and isn't
> an instance of TYPE_MACHINE either.
user-only shouldn't get to qdev_get_machine() at all,
issue probably in container_get().
I'd try to fix wrong user if possible and maybe add ifdef build failure
to qdev_get_machine() so it would not build in user mode.
 
> It looks like maybe this can happen in some cases in softmmu too,
> judging by the way that qdev_get_hotplug_handler() does an
> object_dynamic_cast() check that it really got back a TYPE_MACHINE.
As I recall only bus or machine provide hotplug_handler currently,
but it's possible to extend to other objects if we find use-case.

We could do static cast to machine instead dynamic there but
in hotplug case it will abort QEMU if error happens,
hence dynamic check to avoid be more resilient during hotplug.
(well, if qdev_get_machine() returns not machine during startup
we would be screwed anyways, but that should break much earlier)

> Is this intentional? Does anything rely on qdev_get_machine()
> returning something odd like this?
> 
> In the code I have which ran into this I can just make it do an
> object_dynamic_cast() check like the hotplug_handler code does,
> but if the current implementation is intentional we should
> probably document that this is what you're supposed to do.
> 
> thanks
> -- PMM

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [Qemu-devel] qdev_get_machine() can return something non-NULL but not TYPE_MACHINE
  2017-09-04 19:38 ` Igor Mammedov
@ 2017-09-05  9:08   ` Peter Maydell
  2017-09-11 12:10     ` Igor Mammedov
  0 siblings, 1 reply; 8+ messages in thread
From: Peter Maydell @ 2017-09-05  9:08 UTC (permalink / raw)
  To: Igor Mammedov; +Cc: QEMU Developers, Paolo Bonzini

On 4 September 2017 at 20:38, Igor Mammedov <imammedo@redhat.com> wrote:
> On Mon, 4 Sep 2017 17:36:59 +0100
> Peter Maydell <peter.maydell@linaro.org> wrote:
>
>> I just got bitten by qdev_get_machine()'s behaviour on the user-only
>> emulators, where it can return something that isn't NULL and isn't
>> an instance of TYPE_MACHINE either.
> user-only shouldn't get to qdev_get_machine() at all,
> issue probably in container_get().

I need it in cpu_common_realizefn(), for
http://patchwork.ozlabs.org/patch/797940/

> I'd try to fix wrong user if possible and maybe add ifdef build failure
> to qdev_get_machine() so it would not build in user mode.

Can't ifdef, that source file is built once for all targets.

My fix (which I intend to send to the list today) is to make
it do the object_dynamic_cast() check -- if that doesn't give
a TYPE_MACHINE then we're in user mode and don't need to set
ignore_memory_transaction_failures on the cpu object anyway.

>> It looks like maybe this can happen in some cases in softmmu too,
>> judging by the way that qdev_get_hotplug_handler() does an
>> object_dynamic_cast() check that it really got back a TYPE_MACHINE.
> As I recall only bus or machine provide hotplug_handler currently,
> but it's possible to extend to other objects if we find use-case.
>
> We could do static cast to machine instead dynamic there but
> in hotplug case it will abort QEMU if error happens,
> hence dynamic check to avoid be more resilient during hotplug.
> (well, if qdev_get_machine() returns not machine during startup
> we would be screwed anyways, but that should break much earlier)

If this can't ever happen then we should be aborting; that's
the idea behind the cast macros doing assertions. I'm not
sure hotplug needs to be special here if it doesn't have
a genuine reason to think it might get back something of
the wrong type.

thanks
-- PMM

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [Qemu-devel] qdev_get_machine() can return something non-NULL but not TYPE_MACHINE
  2017-09-05  9:08   ` Peter Maydell
@ 2017-09-11 12:10     ` Igor Mammedov
  2017-09-11 13:33       ` Peter Maydell
  0 siblings, 1 reply; 8+ messages in thread
From: Igor Mammedov @ 2017-09-11 12:10 UTC (permalink / raw)
  To: Peter Maydell; +Cc: QEMU Developers, Paolo Bonzini

On Tue, 5 Sep 2017 10:08:01 +0100
Peter Maydell <peter.maydell@linaro.org> wrote:

> On 4 September 2017 at 20:38, Igor Mammedov <imammedo@redhat.com> wrote:
> > On Mon, 4 Sep 2017 17:36:59 +0100
> > Peter Maydell <peter.maydell@linaro.org> wrote:
> >  
> >> I just got bitten by qdev_get_machine()'s behaviour on the user-only
> >> emulators, where it can return something that isn't NULL and isn't
> >> an instance of TYPE_MACHINE either.  
> > user-only shouldn't get to qdev_get_machine() at all,
> > issue probably in container_get().  
> 
> I need it in cpu_common_realizefn(), for
> http://patchwork.ozlabs.org/patch/797940/
Link might be broken (unable to connect to server)

Anyways I'd avoid using machine from cpu_*_realizefn(),
instead of I'd add property to CPU that has needed data
and set it from board code. Should work fine for *-user targets
and maintain clear separation of device impl. and board details. 

> 
> > I'd try to fix wrong user if possible and maybe add ifdef build failure
> > to qdev_get_machine() so it would not build in user mode.  
> 
> Can't ifdef, that source file is built once for all targets.
> 
> My fix (which I intend to send to the list today) is to make
> it do the object_dynamic_cast() check -- if that doesn't give
> a TYPE_MACHINE then we're in user mode and don't need to set
> ignore_memory_transaction_failures on the cpu object anyway.
> 
> >> It looks like maybe this can happen in some cases in softmmu too,
> >> judging by the way that qdev_get_hotplug_handler() does an
> >> object_dynamic_cast() check that it really got back a TYPE_MACHINE.  
> > As I recall only bus or machine provide hotplug_handler currently,
> > but it's possible to extend to other objects if we find use-case.
> >
> > We could do static cast to machine instead dynamic there but
> > in hotplug case it will abort QEMU if error happens,
> > hence dynamic check to avoid be more resilient during hotplug.
> > (well, if qdev_get_machine() returns not machine during startup
> > we would be screwed anyways, but that should break much earlier)  
> 
> If this can't ever happen then we should be aborting; that's
> the idea behind the cast macros doing assertions. I'm not
> sure hotplug needs to be special here if it doesn't have
> a genuine reason to think it might get back something of
> the wrong type.
Yep, we should abort.

> 
> thanks
> -- PMM

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [Qemu-devel] qdev_get_machine() can return something non-NULL but not TYPE_MACHINE
  2017-09-11 12:10     ` Igor Mammedov
@ 2017-09-11 13:33       ` Peter Maydell
  2017-09-12  7:40         ` Igor Mammedov
  0 siblings, 1 reply; 8+ messages in thread
From: Peter Maydell @ 2017-09-11 13:33 UTC (permalink / raw)
  To: Igor Mammedov; +Cc: QEMU Developers, Paolo Bonzini

On 11 September 2017 at 13:10, Igor Mammedov <imammedo@redhat.com> wrote:
> On Tue, 5 Sep 2017 10:08:01 +0100
> Peter Maydell <peter.maydell@linaro.org> wrote:
>
>> On 4 September 2017 at 20:38, Igor Mammedov <imammedo@redhat.com> wrote:
>> > On Mon, 4 Sep 2017 17:36:59 +0100
>> > Peter Maydell <peter.maydell@linaro.org> wrote:
>> >
>> >> I just got bitten by qdev_get_machine()'s behaviour on the user-only
>> >> emulators, where it can return something that isn't NULL and isn't
>> >> an instance of TYPE_MACHINE either.
>> > user-only shouldn't get to qdev_get_machine() at all,
>> > issue probably in container_get().
>>
>> I need it in cpu_common_realizefn(), for
>> http://patchwork.ozlabs.org/patch/797940/
> Link might be broken (unable to connect to server)

Works for me, but it is in master now anyway, commit
ed860129acd3fcd0b1.

> Anyways I'd avoid using machine from cpu_*_realizefn(),
> instead of I'd add property to CPU that has needed data
> and set it from board code. Should work fine for *-user targets
> and maintain clear separation of device impl. and board details.

It's not possible in all cases to set a CPU property from the
top level board code. In quite a lot of cases the CPU
object is created by an SoC object which is in turn
created by the board code, and there is no plumbing
there to pass arbitrary properties through to the CPU
object...

thanks
-- PMM

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [Qemu-devel] qdev_get_machine() can return something non-NULL but not TYPE_MACHINE
  2017-09-11 13:33       ` Peter Maydell
@ 2017-09-12  7:40         ` Igor Mammedov
  2017-09-12  9:11           ` Peter Maydell
  0 siblings, 1 reply; 8+ messages in thread
From: Igor Mammedov @ 2017-09-12  7:40 UTC (permalink / raw)
  To: Peter Maydell; +Cc: QEMU Developers, Paolo Bonzini

On Mon, 11 Sep 2017 14:33:03 +0100
Peter Maydell <peter.maydell@linaro.org> wrote:

> On 11 September 2017 at 13:10, Igor Mammedov <imammedo@redhat.com> wrote:
> > On Tue, 5 Sep 2017 10:08:01 +0100
> > Peter Maydell <peter.maydell@linaro.org> wrote:
> >  
> >> On 4 September 2017 at 20:38, Igor Mammedov <imammedo@redhat.com> wrote:  
> >> > On Mon, 4 Sep 2017 17:36:59 +0100
> >> > Peter Maydell <peter.maydell@linaro.org> wrote:
> >> >  
> >> >> I just got bitten by qdev_get_machine()'s behaviour on the user-only
> >> >> emulators, where it can return something that isn't NULL and isn't
> >> >> an instance of TYPE_MACHINE either.  
> >> > user-only shouldn't get to qdev_get_machine() at all,
> >> > issue probably in container_get().  
> >>
> >> I need it in cpu_common_realizefn(), for
> >> http://patchwork.ozlabs.org/patch/797940/  
> > Link might be broken (unable to connect to server)  
> 
> Works for me, but it is in master now anyway, commit
> ed860129acd3fcd0b1.
> 
> > Anyways I'd avoid using machine from cpu_*_realizefn(),
> > instead of I'd add property to CPU that has needed data
> > and set it from board code. Should work fine for *-user targets
> > and maintain clear separation of device impl. and board details.  
> 
> It's not possible in all cases to set a CPU property from the
> top level board code. In quite a lot of cases the CPU
> object is created by an SoC object which is in turn
> created by the board code, and there is no plumbing
> there to pass arbitrary properties through to the CPU
> object...
there is a cleaner way without cpu accessing machine,
make it property of cpu and use compat machinery that
was invented for fixing up stuff of this kind.

SET_MACHINE_COMPAT(MachineClass,
                   { .driver = "arm-cpu",
                     .property = "foo",
                     .value    = "off",
                   }
                  )

> 
> thanks
> -- PMM

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [Qemu-devel] qdev_get_machine() can return something non-NULL but not TYPE_MACHINE
  2017-09-12  7:40         ` Igor Mammedov
@ 2017-09-12  9:11           ` Peter Maydell
  2017-09-12  9:53             ` Igor Mammedov
  0 siblings, 1 reply; 8+ messages in thread
From: Peter Maydell @ 2017-09-12  9:11 UTC (permalink / raw)
  To: Igor Mammedov; +Cc: QEMU Developers, Paolo Bonzini

On 12 September 2017 at 08:40, Igor Mammedov <imammedo@redhat.com> wrote:
> On Mon, 11 Sep 2017 14:33:03 +0100
> Peter Maydell <peter.maydell@linaro.org> wrote:
>> It's not possible in all cases to set a CPU property from the
>> top level board code. In quite a lot of cases the CPU
>> object is created by an SoC object which is in turn
>> created by the board code, and there is no plumbing
>> there to pass arbitrary properties through to the CPU
>> object...
> there is a cleaner way without cpu accessing machine,
> make it property of cpu and use compat machinery that
> was invented for fixing up stuff of this kind.
>
> SET_MACHINE_COMPAT(MachineClass,
>                    { .driver = "arm-cpu",
>                      .property = "foo",
>                      .value    = "off",
>                    }
>                   )

It looks like we only use that machine-compat stuff on
our versioned boards, which is pretty much the only place
where we don't need to set this particular flag...

thanks
-- PMM

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [Qemu-devel] qdev_get_machine() can return something non-NULL but not TYPE_MACHINE
  2017-09-12  9:11           ` Peter Maydell
@ 2017-09-12  9:53             ` Igor Mammedov
  0 siblings, 0 replies; 8+ messages in thread
From: Igor Mammedov @ 2017-09-12  9:53 UTC (permalink / raw)
  To: Peter Maydell; +Cc: Paolo Bonzini, QEMU Developers

On Tue, 12 Sep 2017 10:11:32 +0100
Peter Maydell <peter.maydell@linaro.org> wrote:

> On 12 September 2017 at 08:40, Igor Mammedov <imammedo@redhat.com> wrote:
> > On Mon, 11 Sep 2017 14:33:03 +0100
> > Peter Maydell <peter.maydell@linaro.org> wrote:  
> >> It's not possible in all cases to set a CPU property from the
> >> top level board code. In quite a lot of cases the CPU
> >> object is created by an SoC object which is in turn
> >> created by the board code, and there is no plumbing
> >> there to pass arbitrary properties through to the CPU
> >> object...  
> > there is a cleaner way without cpu accessing machine,
> > make it property of cpu and use compat machinery that
> > was invented for fixing up stuff of this kind.
> >
> > SET_MACHINE_COMPAT(MachineClass,
> >                    { .driver = "arm-cpu",
> >                      .property = "foo",
> >                      .value    = "off",
> >                    }
> >                   )  
> 
> It looks like we only use that machine-compat stuff on
> our versioned boards, which is pretty much the only place
> where we don't need to set this particular flag...
typically, yes it's used for versioned boards to, because
it's where we have to fixup/override defaults to keep compatibility.
But it's does not mean that it's limited to it.

in this case it allows to keep clean separation of device model
and not add an extra member to generic MachineClass which is used
by some old boards.


> thanks
> -- PMM
> 

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2017-09-12  9:53 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-09-04 16:36 [Qemu-devel] qdev_get_machine() can return something non-NULL but not TYPE_MACHINE Peter Maydell
2017-09-04 19:38 ` Igor Mammedov
2017-09-05  9:08   ` Peter Maydell
2017-09-11 12:10     ` Igor Mammedov
2017-09-11 13:33       ` Peter Maydell
2017-09-12  7:40         ` Igor Mammedov
2017-09-12  9:11           ` Peter Maydell
2017-09-12  9:53             ` Igor Mammedov

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).