Patchwork [1/2] Move KVM init to arch_init.c, compile vl.c once

login
register
mail settings
Submitter Blue Swirl
Date April 1, 2010, 8:07 p.m.
Message ID <l2vf43fc5581004011307m28f5f783nc27e4350acedef81@mail.gmail.com>
Download mbox | patch
Permalink /patch/49239/
State New
Headers show

Comments

Blue Swirl - April 1, 2010, 8:07 p.m.
Remove dependency of vl.c to KVM, then we can partially revert
b33612d03540fda7fa67485f1c20395beb7a2bf0.

Signed-off-by: Blue Swirl <blauwirbel@gmail.com>
---
 Makefile.objs   |    2 +-
 Makefile.target |    2 +-
 arch_init.c     |   20 ++++++++++++++++++++
 arch_init.h     |    2 ++
 vl.c            |   14 ++------------
 5 files changed, 26 insertions(+), 14 deletions(-)

                 usb_enabled = 1;
@@ -3574,15 +3572,7 @@ int main(int argc, char **argv, char **envp)
         exit(1);
     }

-    if (kvm_enabled()) {
-        int ret;
-
-        ret = kvm_init(smp_cpus);
-        if (ret < 0) {
-            fprintf(stderr, "failed to initialize KVM\n");
-            exit(1);
-        }
-    }
+    kvm_maybe_init(smp_cpus);

     if (qemu_init_main_loop()) {
         fprintf(stderr, "qemu_init_main_loop failed\n");
Anthony Liguori - April 1, 2010, 8:15 p.m.
On 04/01/2010 03:07 PM, Blue Swirl wrote:
> Remove dependency of vl.c to KVM, then we can partially revert
> b33612d03540fda7fa67485f1c20395beb7a2bf0.
>
> Signed-off-by: Blue Swirl<blauwirbel@gmail.com>
> ---
>   Makefile.objs   |    2 +-
>   Makefile.target |    2 +-
>   arch_init.c     |   20 ++++++++++++++++++++
>   arch_init.h     |    2 ++
>   vl.c            |   14 ++------------
>   5 files changed, 26 insertions(+), 14 deletions(-)
>
> diff --git a/Makefile.objs b/Makefile.objs
> index 74d7a3d..4cc8ea6 100644
> --- a/Makefile.objs
> +++ b/Makefile.objs
> @@ -128,7 +128,7 @@ user-obj-y += cutils.o cache-utils.o
>   # libhw
>
>   hw-obj-y =
> -hw-obj-y += loader.o
> +hw-obj-y += vl.o loader.o
>   hw-obj-y += virtio.o virtio-console.o
>   hw-obj-y += fw_cfg.o pci.o pci_host.o pcie_host.o
>   hw-obj-y += watchdog.o
> diff --git a/Makefile.target b/Makefile.target
> index 167fc8d..2aa02f5 100644
> --- a/Makefile.target
> +++ b/Makefile.target
> @@ -161,7 +161,7 @@ endif #CONFIG_BSD_USER
>   # System emulator target
>   ifdef CONFIG_SOFTMMU
>
> -obj-y = arch_init.o cpus.o monitor.o machine.o gdbstub.o vl.o
> +obj-y = arch_init.o cpus.o monitor.o machine.o gdbstub.o
>   # virtio has to be here due to weird dependency between PCI and virtio-net.
>   # need to fix this properly
>   obj-y += virtio-blk.o virtio-balloon.o virtio-net.o virtio-pci.o
> virtio-serial-bus.o
> diff --git a/arch_init.c b/arch_init.c
> index cfc03ea..001c560 100644
> --- a/arch_init.c
> +++ b/arch_init.c
> @@ -41,6 +41,8 @@
>   #include "gdbstub.h"
>   #include "hw/smbios.h"
>
> +int kvm_allowed = 0;
> +
>   #ifdef TARGET_SPARC
>   int graphic_width = 1024;
>   int graphic_height = 768;
> @@ -508,3 +510,21 @@ int xen_available(void)
>       return 0;
>   #endif
>   }
> +
> +void enable_kvm(void)
> +{
> +    kvm_allowed = 1;
> +}
>    

Can we get away with keeping a local to vl.c use_kvm flag and then call 
kvm_init() if use_kvm in vl.c?

We can stick a stub kvm_init() in arch_init.c if !CONFIG_KVM.

Regards,

Anthony LIguori

> +void kvm_maybe_init(int smp_cpus)
> +{
> +    if (kvm_enabled()) {
> +        int ret;
> +
> +        ret = kvm_init(smp_cpus);
> +        if (ret<  0) {
> +            fprintf(stderr, "failed to initialize KVM\n");
> +            exit(1);
> +        }
> +    }
> +}
> diff --git a/arch_init.h b/arch_init.h
> index 682890c..dde4309 100644
> --- a/arch_init.h
> +++ b/arch_init.h
> @@ -29,5 +29,7 @@ void cpudef_init(void);
>   int audio_available(void);
>   int kvm_available(void);
>   int xen_available(void);
> +void enable_kvm(void);
> +void kvm_maybe_init(int smp_cpus);
>
>   #endif
> diff --git a/vl.c b/vl.c
> index 03fccbf..cc214dd 100644
> --- a/vl.c
> +++ b/vl.c
> @@ -145,7 +145,6 @@ int main(int argc, char **argv)
>   #include "dma.h"
>   #include "audio/audio.h"
>   #include "migration.h"
> -#include "kvm.h"
>   #include "balloon.h"
>   #include "qemu-option.h"
>   #include "qemu-config.h"
> @@ -241,7 +240,6 @@ uint8_t qemu_uuid[16];
>   static QEMUBootSetHandler *boot_set_handler;
>   static void *boot_set_opaque;
>
> -int kvm_allowed = 0;
>   uint32_t xen_domid;
>   enum xen_mode xen_mode = XEN_EMULATE;
>
> @@ -3228,7 +3226,7 @@ int main(int argc, char **argv, char **envp)
>                       printf("Option %s not supported for this
> target\n", popt->name);
>                       exit(1);
>                   }
> -                kvm_allowed = 1;
> +                enable_kvm();
>                   break;
>               case QEMU_OPTION_usb:
>                   usb_enabled = 1;
> @@ -3574,15 +3572,7 @@ int main(int argc, char **argv, char **envp)
>           exit(1);
>       }
>
> -    if (kvm_enabled()) {
> -        int ret;
> -
> -        ret = kvm_init(smp_cpus);
> -        if (ret<  0) {
> -            fprintf(stderr, "failed to initialize KVM\n");
> -            exit(1);
> -        }
> -    }
> +    kvm_maybe_init(smp_cpus);
>
>       if (qemu_init_main_loop()) {
>           fprintf(stderr, "qemu_init_main_loop failed\n");
>
Paolo Bonzini - April 1, 2010, 8:23 p.m.
On 04/01/2010 10:07 PM, Blue Swirl wrote:
> Remove dependency of vl.c to KVM, then we can partially revert
> b33612d03540fda7fa67485f1c20395beb7a2bf0.

This is ugly...

Michael Tsirkin said in commit ca821806:

  Comment on kvm usage: rather than require users to do if(kvm_enabled())
  and/or ifdefs, this patch adds an API that, internally, is defined to
  stub function on non-kvm build, and checks kvm_enabled for non-kvm
  run.

  While rest of qemu code still uses if (kvm_enabled()), I think this
  approach is cleaner, and we should convert rest of code to it
  long term.

Maybe we can start doing this for kvm_init?

> +void enable_kvm(void)
> +{
> +    kvm_allowed = 1;
> +}

If you're adding this, this conditional from vl.c

  if (!(kvm_available())) {
      printf("Option %s not supported for this target\n", popt->name);
      exit(1);
  }

belongs in arch_init.c too, like in do_smbios_option and 
do_acpitable_option.

Alternatively, with the approach I gave above, you can test in 
kvm_maybe_init if kvm_init returns -ENOSYS, and print the error above 
instead of "failed to initialize KVM".

Paolo
Blue Swirl - April 1, 2010, 8:27 p.m.
On 4/1/10, Anthony Liguori <anthony@codemonkey.ws> wrote:
> On 04/01/2010 03:07 PM, Blue Swirl wrote:
>
> > Remove dependency of vl.c to KVM, then we can partially revert
> > b33612d03540fda7fa67485f1c20395beb7a2bf0.
> >
> > Signed-off-by: Blue Swirl<blauwirbel@gmail.com>
> > ---
> >  Makefile.objs   |    2 +-
> >  Makefile.target |    2 +-
> >  arch_init.c     |   20 ++++++++++++++++++++
> >  arch_init.h     |    2 ++
> >  vl.c            |   14 ++------------
> >  5 files changed, 26 insertions(+), 14 deletions(-)
> >
> > diff --git a/Makefile.objs b/Makefile.objs
> > index 74d7a3d..4cc8ea6 100644
> > --- a/Makefile.objs
> > +++ b/Makefile.objs
> > @@ -128,7 +128,7 @@ user-obj-y += cutils.o cache-utils.o
> >  # libhw
> >
> >  hw-obj-y =
> > -hw-obj-y += loader.o
> > +hw-obj-y += vl.o loader.o
> >  hw-obj-y += virtio.o virtio-console.o
> >  hw-obj-y += fw_cfg.o pci.o pci_host.o pcie_host.o
> >  hw-obj-y += watchdog.o
> > diff --git a/Makefile.target b/Makefile.target
> > index 167fc8d..2aa02f5 100644
> > --- a/Makefile.target
> > +++ b/Makefile.target
> > @@ -161,7 +161,7 @@ endif #CONFIG_BSD_USER
> >  # System emulator target
> >  ifdef CONFIG_SOFTMMU
> >
> > -obj-y = arch_init.o cpus.o monitor.o machine.o gdbstub.o vl.o
> > +obj-y = arch_init.o cpus.o monitor.o machine.o gdbstub.o
> >  # virtio has to be here due to weird dependency between PCI and
> virtio-net.
> >  # need to fix this properly
> >  obj-y += virtio-blk.o virtio-balloon.o virtio-net.o virtio-pci.o
> > virtio-serial-bus.o
> > diff --git a/arch_init.c b/arch_init.c
> > index cfc03ea..001c560 100644
> > --- a/arch_init.c
> > +++ b/arch_init.c
> > @@ -41,6 +41,8 @@
> >  #include "gdbstub.h"
> >  #include "hw/smbios.h"
> >
> > +int kvm_allowed = 0;
> > +
> >  #ifdef TARGET_SPARC
> >  int graphic_width = 1024;
> >  int graphic_height = 768;
> > @@ -508,3 +510,21 @@ int xen_available(void)
> >      return 0;
> >  #endif
> >  }
> > +
> > +void enable_kvm(void)
> > +{
> > +    kvm_allowed = 1;
> > +}
> >
> >
>
>  Can we get away with keeping a local to vl.c use_kvm flag and then call
> kvm_init() if use_kvm in vl.c?

It will not be safe to use kvm_enabled() in vl.c, so there needs to be
a target dependent helper. The call to kvm_init could remain in vl.c,
likewise it's not strictly needed to move kvm_allowed to arch_init.c.
They just caused problems with the poisoning check in patch 2/2. But
with this approach, because kvm.h is no longer included, there will
not be any problems.

>
>  We can stick a stub kvm_init() in arch_init.c if !CONFIG_KVM.
>
>  Regards,
>
>  Anthony LIguori
>
>
>
> > +void kvm_maybe_init(int smp_cpus)
> > +{
> > +    if (kvm_enabled()) {
> > +        int ret;
> > +
> > +        ret = kvm_init(smp_cpus);
> > +        if (ret<  0) {
> > +            fprintf(stderr, "failed to initialize KVM\n");
> > +            exit(1);
> > +        }
> > +    }
> > +}
> > diff --git a/arch_init.h b/arch_init.h
> > index 682890c..dde4309 100644
> > --- a/arch_init.h
> > +++ b/arch_init.h
> > @@ -29,5 +29,7 @@ void cpudef_init(void);
> >  int audio_available(void);
> >  int kvm_available(void);
> >  int xen_available(void);
> > +void enable_kvm(void);
> > +void kvm_maybe_init(int smp_cpus);
> >
> >  #endif
> > diff --git a/vl.c b/vl.c
> > index 03fccbf..cc214dd 100644
> > --- a/vl.c
> > +++ b/vl.c
> > @@ -145,7 +145,6 @@ int main(int argc, char **argv)
> >  #include "dma.h"
> >  #include "audio/audio.h"
> >  #include "migration.h"
> > -#include "kvm.h"
> >  #include "balloon.h"
> >  #include "qemu-option.h"
> >  #include "qemu-config.h"
> > @@ -241,7 +240,6 @@ uint8_t qemu_uuid[16];
> >  static QEMUBootSetHandler *boot_set_handler;
> >  static void *boot_set_opaque;
> >
> > -int kvm_allowed = 0;
> >  uint32_t xen_domid;
> >  enum xen_mode xen_mode = XEN_EMULATE;
> >
> > @@ -3228,7 +3226,7 @@ int main(int argc, char **argv, char **envp)
> >                      printf("Option %s not supported for this
> > target\n", popt->name);
> >                      exit(1);
> >                  }
> > -                kvm_allowed = 1;
> > +                enable_kvm();
> >                  break;
> >              case QEMU_OPTION_usb:
> >                  usb_enabled = 1;
> > @@ -3574,15 +3572,7 @@ int main(int argc, char **argv, char **envp)
> >          exit(1);
> >      }
> >
> > -    if (kvm_enabled()) {
> > -        int ret;
> > -
> > -        ret = kvm_init(smp_cpus);
> > -        if (ret<  0) {
> > -            fprintf(stderr, "failed to initialize KVM\n");
> > -            exit(1);
> > -        }
> > -    }
> > +    kvm_maybe_init(smp_cpus);
> >
> >      if (qemu_init_main_loop()) {
> >          fprintf(stderr, "qemu_init_main_loop failed\n");
> >
> >
>
>
Blue Swirl - April 2, 2010, 2:20 p.m.
On 4/1/10, Paolo Bonzini <pbonzini@redhat.com> wrote:
> On 04/01/2010 10:07 PM, Blue Swirl wrote:
>
> > Remove dependency of vl.c to KVM, then we can partially revert
> > b33612d03540fda7fa67485f1c20395beb7a2bf0.
> >
>
>  This is ugly...
>
>  Michael Tsirkin said in commit ca821806:
>
>   Comment on kvm usage: rather than require users to do if(kvm_enabled())
>   and/or ifdefs, this patch adds an API that, internally, is defined to
>   stub function on non-kvm build, and checks kvm_enabled for non-kvm
>   run.
>
>   While rest of qemu code still uses if (kvm_enabled()), I think this
>   approach is cleaner, and we should convert rest of code to it
>   long term.
>
>  Maybe we can start doing this for kvm_init?

I see now. Yes, this would be better approach, I think this is also
what Anthony proposed.

> > +void enable_kvm(void)
> > +{
> > +    kvm_allowed = 1;
> > +}
> >
>
>  If you're adding this, this conditional from vl.c
>
>   if (!(kvm_available())) {
>      printf("Option %s not supported for this target\n", popt->name);
>      exit(1);
>   }
>
>  belongs in arch_init.c too, like in do_smbios_option and
> do_acpitable_option.
>
>  Alternatively, with the approach I gave above, you can test in
> kvm_maybe_init if kvm_init returns -ENOSYS, and print the error above
> instead of "failed to initialize KVM".

Also kvm_allowed could be set in kvm-all.c.

Patch

diff --git a/Makefile.objs b/Makefile.objs
index 74d7a3d..4cc8ea6 100644
--- a/Makefile.objs
+++ b/Makefile.objs
@@ -128,7 +128,7 @@  user-obj-y += cutils.o cache-utils.o
 # libhw

 hw-obj-y =
-hw-obj-y += loader.o
+hw-obj-y += vl.o loader.o
 hw-obj-y += virtio.o virtio-console.o
 hw-obj-y += fw_cfg.o pci.o pci_host.o pcie_host.o
 hw-obj-y += watchdog.o
diff --git a/Makefile.target b/Makefile.target
index 167fc8d..2aa02f5 100644
--- a/Makefile.target
+++ b/Makefile.target
@@ -161,7 +161,7 @@  endif #CONFIG_BSD_USER
 # System emulator target
 ifdef CONFIG_SOFTMMU

-obj-y = arch_init.o cpus.o monitor.o machine.o gdbstub.o vl.o
+obj-y = arch_init.o cpus.o monitor.o machine.o gdbstub.o
 # virtio has to be here due to weird dependency between PCI and virtio-net.
 # need to fix this properly
 obj-y += virtio-blk.o virtio-balloon.o virtio-net.o virtio-pci.o
virtio-serial-bus.o
diff --git a/arch_init.c b/arch_init.c
index cfc03ea..001c560 100644
--- a/arch_init.c
+++ b/arch_init.c
@@ -41,6 +41,8 @@ 
 #include "gdbstub.h"
 #include "hw/smbios.h"

+int kvm_allowed = 0;
+
 #ifdef TARGET_SPARC
 int graphic_width = 1024;
 int graphic_height = 768;
@@ -508,3 +510,21 @@  int xen_available(void)
     return 0;
 #endif
 }
+
+void enable_kvm(void)
+{
+    kvm_allowed = 1;
+}
+
+void kvm_maybe_init(int smp_cpus)
+{
+    if (kvm_enabled()) {
+        int ret;
+
+        ret = kvm_init(smp_cpus);
+        if (ret < 0) {
+            fprintf(stderr, "failed to initialize KVM\n");
+            exit(1);
+        }
+    }
+}
diff --git a/arch_init.h b/arch_init.h
index 682890c..dde4309 100644
--- a/arch_init.h
+++ b/arch_init.h
@@ -29,5 +29,7 @@  void cpudef_init(void);
 int audio_available(void);
 int kvm_available(void);
 int xen_available(void);
+void enable_kvm(void);
+void kvm_maybe_init(int smp_cpus);

 #endif
diff --git a/vl.c b/vl.c
index 03fccbf..cc214dd 100644
--- a/vl.c
+++ b/vl.c
@@ -145,7 +145,6 @@  int main(int argc, char **argv)
 #include "dma.h"
 #include "audio/audio.h"
 #include "migration.h"
-#include "kvm.h"
 #include "balloon.h"
 #include "qemu-option.h"
 #include "qemu-config.h"
@@ -241,7 +240,6 @@  uint8_t qemu_uuid[16];
 static QEMUBootSetHandler *boot_set_handler;
 static void *boot_set_opaque;

-int kvm_allowed = 0;
 uint32_t xen_domid;
 enum xen_mode xen_mode = XEN_EMULATE;

@@ -3228,7 +3226,7 @@  int main(int argc, char **argv, char **envp)
                     printf("Option %s not supported for this
target\n", popt->name);
                     exit(1);
                 }
-                kvm_allowed = 1;
+                enable_kvm();
                 break;
             case QEMU_OPTION_usb: