qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Steven Sistare <steven.sistare@oracle.com>
To: Peter Xu <peterx@redhat.com>
Cc: qemu-devel@nongnu.org, Fabiano Rosas <farosas@suse.de>,
	David Hildenbrand <david@redhat.com>,
	Marcel Apfelbaum <marcel.apfelbaum@gmail.com>,
	Eduardo Habkost <eduardo@habkost.net>,
	Philippe Mathieu-Daude <philmd@linaro.org>,
	Paolo Bonzini <pbonzini@redhat.com>,
	"Daniel P. Berrange" <berrange@redhat.com>,
	Markus Armbruster <armbru@redhat.com>
Subject: Re: [RFC V1 02/14] accel: accel preinit function
Date: Wed, 23 Oct 2024 12:13:18 -0400	[thread overview]
Message-ID: <2512f1ce-e191-4ee4-8c37-43801fd5e2f7@oracle.com> (raw)
In-Reply-To: <ZxZrJjGx0tifZf0B@x1n>

On 10/21/2024 10:54 AM, Peter Xu wrote:
> On Thu, Oct 17, 2024 at 08:14:03AM -0700, Steve Sistare wrote:
>> Extract the first part of the AccelState init_machine function into
>> a new function preinit, which can be called without knowing any
>> machine properties.  For now call preinit and init_machine at the
>> same place, so no functional change.
>>
>> Signed-off-by: Steve Sistare <steven.sistare@oracle.com>
>> ---
>>   accel/accel-system.c        |  6 +++++
>>   accel/kvm/kvm-all.c         | 58 +++++++++++++++++++++++++++++----------------
>>   accel/xen/xen-all.c         | 11 ++++++---
>>   include/qemu/accel.h        |  1 +
>>   target/i386/nvmm/nvmm-all.c | 10 +++++++-
>>   target/i386/whpx/whpx-all.c | 14 +++++++----
>>   6 files changed, 70 insertions(+), 30 deletions(-)
>>
>> diff --git a/accel/accel-system.c b/accel/accel-system.c
>> index f6c947d..fef6625 100644
>> --- a/accel/accel-system.c
>> +++ b/accel/accel-system.c
>> @@ -36,8 +36,14 @@ int accel_init_machine(AccelState *accel, MachineState *ms)
>>       int ret;
>>       ms->accelerator = accel;
>>       *(acc->allowed) = true;
>> +    ret = acc->preinit(accel);
>> +    if (ret < 0) {
>> +        goto fail;
>> +    }
>> +
>>       ret = acc->init_machine(ms);
>>       if (ret < 0) {
>> +fail:
> 
> Jump into another failure path's if clause might be error prone in the
> future.

I agree this is ugly, but the diff is small and easy to understand, and patch 3
makes it pretty again:

@@ -36,14 +36,8 @@ int accel_init_machine(AccelState *accel, MachineState *ms)
      int ret;
      ms->accelerator = accel;
      *(acc->allowed) = true;
-    ret = acc->preinit(accel);
-    if (ret < 0) {
-        goto fail;
-    }
-
      ret = acc->init_machine(ms);
      if (ret < 0) {
-fail:
          ms->accelerator = NULL;

- Steve

> Maybe move the error handling out while at it, e.g.:
> 
>      ret = acc->init_machine();
>      if (ret < 0) {
>         goto fail;
>      }
>      object_set_accelerator_compat_props(acc->compat_props);
>      return 0;
> 
> fail:
>      ms->accelerator = NULL;
>      *(acc->allowed) = false;
>      object_unref(OBJECT(accel));
>      return ret;
> 
>>           ms->accelerator = NULL;
>>           *(acc->allowed) = false;
>>           object_unref(OBJECT(accel));
>> diff --git a/accel/kvm/kvm-all.c b/accel/kvm/kvm-all.c
>> index 905fb84..c7c6001 100644
>> --- a/accel/kvm/kvm-all.c
>> +++ b/accel/kvm/kvm-all.c
>> @@ -2484,6 +2484,42 @@ static int kvm_setup_dirty_ring(KVMState *s)
>>       return 0;
>>   }
>>   
>> +static int kvm_preinit(AccelState *accel)
>> +{
>> +    int ret;
>> +    KVMState *s = KVM_STATE(accel);
>> +
>> +    s->fd = qemu_open_old(s->device ?: "/dev/kvm", O_RDWR);
>> +    if (s->fd == -1) {
>> +        error_report("Could not access KVM kernel module: %m");
>> +        ret = -errno;
>> +        goto err;
>> +    }
>> +
>> +    ret = kvm_ioctl(s, KVM_GET_API_VERSION, 0);
>> +    if (ret < KVM_API_VERSION) {
>> +        if (ret >= 0) {
>> +            ret = -EINVAL;
>> +        }
>> +        fprintf(stderr, "kvm version too old\n");
>> +        goto err;
>> +    }
>> +
>> +    if (ret > KVM_API_VERSION) {
>> +        ret = -EINVAL;
>> +        error_report("kvm version not supported");
>> +        goto err;
>> +    }
>> +    return 0;
>> +
>> +err:
>> +    assert(ret < 0);
>> +    if (s->fd != -1) {
>> +        close(s->fd);
>> +    }
>> +    return ret;
>> +}
>> +
>>   static int kvm_init(MachineState *ms)
>>   {
>>       MachineClass *mc = MACHINE_GET_CLASS(ms);
>> @@ -2523,27 +2559,6 @@ static int kvm_init(MachineState *ms)
>>       QTAILQ_INIT(&s->kvm_sw_breakpoints);
>>   #endif
>>       QLIST_INIT(&s->kvm_parked_vcpus);
>> -    s->fd = qemu_open_old(s->device ?: "/dev/kvm", O_RDWR);
>> -    if (s->fd == -1) {
>> -        error_report("Could not access KVM kernel module: %m");
>> -        ret = -errno;
>> -        goto err;
>> -    }
>> -
>> -    ret = kvm_ioctl(s, KVM_GET_API_VERSION, 0);
>> -    if (ret < KVM_API_VERSION) {
>> -        if (ret >= 0) {
>> -            ret = -EINVAL;
>> -        }
>> -        error_report("kvm version too old");
>> -        goto err;
>> -    }
>> -
>> -    if (ret > KVM_API_VERSION) {
>> -        ret = -EINVAL;
>> -        error_report("kvm version not supported");
>> -        goto err;
>> -    }
>>   
>>       kvm_supported_memory_attributes = kvm_check_extension(s, KVM_CAP_MEMORY_ATTRIBUTES);
>>       kvm_guest_memfd_supported =
>> @@ -3891,6 +3906,7 @@ static void kvm_accel_class_init(ObjectClass *oc, void *data)
>>   {
>>       AccelClass *ac = ACCEL_CLASS(oc);
>>       ac->name = "KVM";
>> +    ac->preinit = kvm_preinit;
>>       ac->init_machine = kvm_init;
>>       ac->has_memory = kvm_accel_has_memory;
>>       ac->allowed = &kvm_allowed;
>> diff --git a/accel/xen/xen-all.c b/accel/xen/xen-all.c
>> index 0bdefce..dfcb90c 100644
>> --- a/accel/xen/xen-all.c
>> +++ b/accel/xen/xen-all.c
>> @@ -75,10 +75,8 @@ static void xen_setup_post(MachineState *ms, AccelState *accel)
>>       }
>>   }
>>   
>> -static int xen_init(MachineState *ms)
>> +static int xen_preinit(AccelState *accel)
>>   {
>> -    MachineClass *mc = MACHINE_GET_CLASS(ms);
>> -
>>       xen_xc = xc_interface_open(0, 0, 0);
>>       if (xen_xc == NULL) {
>>           xen_pv_printf(NULL, 0, "can't open xen interface\n");
>> @@ -97,6 +95,12 @@ static int xen_init(MachineState *ms)
>>           xc_interface_close(xen_xc);
>>           return -1;
>>       }
>> +    return 0;
>> +}
>> +
>> +static int xen_init(MachineState *ms)
>> +{
>> +    MachineClass *mc = MACHINE_GET_CLASS(ms);
>>   
>>       /*
>>        * The XenStore write would fail when running restricted so don't attempt
>> @@ -125,6 +129,7 @@ static void xen_accel_class_init(ObjectClass *oc, void *data)
>>       };
>>   
>>       ac->name = "Xen";
>> +    ac->preinit = xen_preinit;
>>       ac->init_machine = xen_init;
>>       ac->setup_post = xen_setup_post;
>>       ac->allowed = &xen_allowed;
>> diff --git a/include/qemu/accel.h b/include/qemu/accel.h
>> index 972a849..6b3b1cf 100644
>> --- a/include/qemu/accel.h
>> +++ b/include/qemu/accel.h
>> @@ -37,6 +37,7 @@ typedef struct AccelClass {
>>       /*< public >*/
>>   
>>       const char *name;
>> +    int (*preinit)(AccelState *accel);
>>       int (*init_machine)(MachineState *ms);
> 
> Might be nice to add some comment on what should be part of preinit() and
> what should be part of init_machine().
> 
> AFAIU the preinit() was about probing whether an accel is supported. Maybe
> rename it to probe()?  Then it's also clear why some accel (e.g. tcg)
> doesn't need that, because it is always supported and no probe is needed.
> 
>>   #ifndef CONFIG_USER_ONLY
>>       void (*setup_post)(MachineState *ms, AccelState *accel);
>> diff --git a/target/i386/nvmm/nvmm-all.c b/target/i386/nvmm/nvmm-all.c
>> index 65768ac..ce858a0 100644
>> --- a/target/i386/nvmm/nvmm-all.c
>> +++ b/target/i386/nvmm/nvmm-all.c
>> @@ -1153,7 +1153,7 @@ static struct RAMBlockNotifier nvmm_ram_notifier = {
>>   /* -------------------------------------------------------------------------- */
>>   
>>   static int
>> -nvmm_accel_init(MachineState *ms)
>> +nvmm_accel_preinit(MachineState *ms)
>>   {
>>       int ret, err;
>>   
>> @@ -1178,6 +1178,13 @@ nvmm_accel_init(MachineState *ms)
>>           error_report("NVMM: Wrong state size %u", qemu_mach.cap.state_size);
>>           return -EPROGMISMATCH;
>>       }
>> +    return 0;
>> +}
>> +
>> +static int
>> +nvmm_accel_init(MachineState *ms)
>> +{
>> +    int ret, err;
>>   
>>       ret = nvmm_machine_create(&qemu_mach.mach);
>>       if (ret == -1) {
>> @@ -1204,6 +1211,7 @@ nvmm_accel_class_init(ObjectClass *oc, void *data)
>>   {
>>       AccelClass *ac = ACCEL_CLASS(oc);
>>       ac->name = "NVMM";
>> +    ac->preinit = nvmm_accel_preinit;
>>       ac->init_machine = nvmm_accel_init;
>>       ac->allowed = &nvmm_allowed;
>>   }
>> diff --git a/target/i386/whpx/whpx-all.c b/target/i386/whpx/whpx-all.c
>> index a6674a8..50bfc19 100644
>> --- a/target/i386/whpx/whpx-all.c
>> +++ b/target/i386/whpx/whpx-all.c
>> @@ -2516,6 +2516,14 @@ static void whpx_set_kernel_irqchip(Object *obj, Visitor *v,
>>    * Partition support
>>    */
>>   
>> +static int whpx_accel_preinit(AccelState *accel)
>> +{
>> +    if (!init_whp_dispatch()) {
>> +        return -ENOSYS;
>> +    }
>> +    return 0;
>> +}
>> +
>>   static int whpx_accel_init(MachineState *ms)
>>   {
>>       struct whpx_state *whpx;
>> @@ -2529,11 +2537,6 @@ static int whpx_accel_init(MachineState *ms)
>>   
>>       whpx = &whpx_global;
>>   
>> -    if (!init_whp_dispatch()) {
>> -        ret = -ENOSYS;
>> -        goto error;
>> -    }
>> -
>>       whpx->mem_quota = ms->ram_size;
>>   
>>       hr = whp_dispatch.WHvGetCapability(
>> @@ -2713,6 +2716,7 @@ static void whpx_accel_class_init(ObjectClass *oc, void *data)
>>   {
>>       AccelClass *ac = ACCEL_CLASS(oc);
>>       ac->name = "WHPX";
>> +    ac->preinit = whpx_accel_preinit;
>>       ac->init_machine = whpx_accel_init;
>>       ac->allowed = &whpx_allowed;
>>   
>> -- 
>> 1.8.3.1
>>
> 



  reply	other threads:[~2024-10-23 16:13 UTC|newest]

Thread overview: 63+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-10-17 15:14 [RFC V1 00/14] precreate phase Steve Sistare
2024-10-17 15:14 ` [RFC V1 01/14] accel: encapsulate search state Steve Sistare
2024-10-21 20:03   ` Fabiano Rosas
2024-10-17 15:14 ` [RFC V1 02/14] accel: accel preinit function Steve Sistare
2024-10-17 15:26   ` Steven Sistare
2024-10-21 14:54   ` Peter Xu
2024-10-23 16:13     ` Steven Sistare [this message]
2024-10-23 15:53   ` Paolo Bonzini
2024-10-23 16:25     ` Steven Sistare
2024-10-17 15:14 ` [RFC V1 03/14] accel: split configure_accelerators Steve Sistare
2024-10-17 15:14 ` [RFC V1 04/14] accel: set accelerator and machine props earlier Steve Sistare
2024-10-18 15:08   ` Fabiano Rosas
2024-10-18 15:32     ` Steven Sistare
2024-10-18 15:40       ` Steven Sistare
2024-10-18 19:15         ` Steven Sistare
2024-10-21 16:20           ` Peter Xu
2024-10-23 17:16             ` Paolo Bonzini
2024-10-22  8:30           ` David Hildenbrand
2024-10-23 20:28             ` Steven Sistare
2024-10-21 15:19   ` Peter Xu
2024-10-23 20:29     ` Steven Sistare
2024-10-23 16:00   ` Paolo Bonzini
2024-10-23 17:18     ` Paolo Bonzini
2024-10-23 20:29     ` Steven Sistare
2024-10-17 15:14 ` [RFC V1 05/14] migration: init and listen during precreate Steve Sistare
2024-10-21 16:41   ` Peter Xu
2024-10-21 21:05   ` Fabiano Rosas
2024-10-23 16:01     ` Steven Sistare
2024-10-17 15:14 ` [RFC V1 06/14] vl: precreate phase Steve Sistare
2024-10-23 14:03   ` Fabiano Rosas
2024-10-17 15:14 ` [RFC V1 07/14] monitor: chardev name Steve Sistare
2024-10-17 15:14 ` [RFC V1 08/14] qom: get properties Steve Sistare
2024-10-17 15:14 ` [RFC V1 09/14] qemu-option: filtered foreach Steve Sistare
2024-10-17 15:14 ` [RFC V1 10/14] qemu-options: pass object to filter Steve Sistare
2024-10-17 15:14 ` [RFC V1 11/14] monitor: connect in precreate Steve Sistare
2024-10-21 19:28   ` Peter Xu
2024-10-23 17:34     ` Steven Sistare
2024-10-23 16:05   ` Paolo Bonzini
2024-10-23 17:35     ` Steven Sistare
2024-10-17 15:14 ` [RFC V1 12/14] qtest: " Steve Sistare
2024-10-17 15:14 ` [RFC V1 13/14] net: cleanup for precreate phase Steve Sistare
2024-10-17 15:27   ` Steven Sistare
2024-10-21 19:20   ` Peter Xu
2024-10-23 17:43     ` Steven Sistare
2024-10-17 15:14 ` [RFC V1 14/14] migration: allow commands during precreate and preconfig Steve Sistare
2024-10-21 19:36   ` Peter Xu
2024-10-23 17:50     ` Steven Sistare
2024-10-17 15:19 ` [RFC V1 00/14] precreate phase Steven Sistare
2024-10-17 15:53   ` Peter Xu
2024-10-21 15:56     ` Steven Sistare
2024-10-23 16:31 ` Paolo Bonzini
2024-10-24 21:16   ` Steven Sistare
2024-10-25  8:46     ` Daniel P. Berrangé
2024-10-25 13:33       ` Steven Sistare
2024-10-25 13:43         ` Daniel P. Berrangé
2024-10-25 14:32           ` Steven Sistare
2024-10-25 14:49             ` Daniel P. Berrangé
2024-10-28 21:56           ` Peter Xu
2024-10-29  9:09             ` Daniel P. Berrangé
2024-10-29 11:13           ` Daniel P. Berrangé
2024-10-29 13:20             ` Fabiano Rosas
2024-10-29 15:18               ` Peter Xu
2024-10-29 15:58               ` Daniel P. Berrangé

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=2512f1ce-e191-4ee4-8c37-43801fd5e2f7@oracle.com \
    --to=steven.sistare@oracle.com \
    --cc=armbru@redhat.com \
    --cc=berrange@redhat.com \
    --cc=david@redhat.com \
    --cc=eduardo@habkost.net \
    --cc=farosas@suse.de \
    --cc=marcel.apfelbaum@gmail.com \
    --cc=pbonzini@redhat.com \
    --cc=peterx@redhat.com \
    --cc=philmd@linaro.org \
    --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).