Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1707570 > unrolled thread

kvm: WARNING in kvm_arch_vcpu_ioctl_run

Started byDmitry Vyukov <dvyukov@google.com>
First post2017-08-09 19:10 +0200
Last post2017-08-10 07:40 +0200
Articles 5 — 3 participants

Back to article view | Back to linux.kernel


Contents

  kvm: WARNING in kvm_arch_vcpu_ioctl_run Dmitry Vyukov <dvyukov@google.com> - 2017-08-09 19:10 +0200
    Re: kvm: WARNING in kvm_arch_vcpu_ioctl_run David Hildenbrand <david@redhat.com> - 2017-08-09 22:30 +0200
      Re: kvm: WARNING in kvm_arch_vcpu_ioctl_run Dmitry Vyukov <dvyukov@google.com> - 2017-08-09 22:40 +0200
    Re: kvm: WARNING in kvm_arch_vcpu_ioctl_run Wanpeng Li <kernellwp@gmail.com> - 2017-08-10 00:00 +0200
    Re: kvm: WARNING in kvm_arch_vcpu_ioctl_run Wanpeng Li <kernellwp@gmail.com> - 2017-08-10 07:40 +0200

#1707570 — kvm: WARNING in kvm_arch_vcpu_ioctl_run

FromDmitry Vyukov <dvyukov@google.com>
Date2017-08-09 19:10 +0200
Subjectkvm: WARNING in kvm_arch_vcpu_ioctl_run
Message-ID<ucCEy-1Ng-35@gated-at.bofh.it>
Hello,

syzkaller fuzzer has hit the following WARNING in kvm_arch_vcpu_ioctl_run.
This is easily reproducible and reproducer is attached at the bottom.
The report is on upstream commit
26c5cebfdb6ca799186f1e56be7d6f2480c5012c. This requires setting
kvm-intel.unrestricted_guest=0 on the machine, with
unrestricted_guest=1 the WARNING does not happen. Output of the
program is:

ret1=0 exit_reason=17 suberror=1
ret2=0 exit_reason=8 suberror=65530


WARNING: CPU: 1 PID: 2850 at arch/x86/kvm/x86.c:7223
kvm_arch_vcpu_ioctl_run+0x213/0x5870 arch/x86/kvm/x86.c:7223
CPU: 1 PID: 2850 Comm: a.out Not tainted 4.13.0-rc3+ #445
Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS Bochs 01/01/2011
Call Trace:
 __dump_stack lib/dump_stack.c:16 [inline]
 dump_stack+0x194/0x257 lib/dump_stack.c:52
 panic+0x1e4/0x417 kernel/panic.c:180
 __warn+0x1c4/0x1d9 kernel/panic.c:541
 report_bug+0x211/0x2d0 lib/bug.c:183
 fixup_bug+0x40/0x90 arch/x86/kernel/traps.c:190
 do_trap_no_signal arch/x86/kernel/traps.c:224 [inline]
 do_trap+0x260/0x390 arch/x86/kernel/traps.c:273
 do_error_trap+0x120/0x390 arch/x86/kernel/traps.c:310
 do_invalid_op+0x1b/0x20 arch/x86/kernel/traps.c:323
 invalid_op+0x1e/0x30 arch/x86/entry/entry_64.S:846
RIP: 0010:kvm_arch_vcpu_ioctl_run+0x213/0x5870 arch/x86/kvm/x86.c:7222
RSP: 0018:ffff8800627cf670 EFLAGS: 00010297
RAX: ffff880066c44480 RBX: ffff88006b07f000 RCX: ffff880067d0845c
RDX: 0000000000000000 RSI: 0000000000000000 RDI: ffff880067d08260
RBP: ffff8800627cfa40 R08: 0000000000000001 R09: 0000000000000000
R10: ffff8800627cfa58 R11: 0000000000000000 R12: 0000000000000001
R13: 0000000000000000 R14: ffff88006b934880 R15: ffff880067d08040
 kvm_vcpu_ioctl+0x64c/0x1010 arch/x86/kvm/../../../virt/kvm/kvm_main.c:2592
 vfs_ioctl fs/ioctl.c:45 [inline]
 do_vfs_ioctl+0x1b1/0x1520 fs/ioctl.c:685
 SYSC_ioctl fs/ioctl.c:700 [inline]
 SyS_ioctl+0x8f/0xc0 fs/ioctl.c:691
 entry_SYSCALL_64_fastpath+0x1f/0xbe
RIP: 0033:0x44ccc9
RSP: 002b:00007f9ecbc37db8 EFLAGS: 00000297 ORIG_RAX: 0000000000000010
RAX: ffffffffffffffda RBX: 0000000000000000 RCX: 000000000044ccc9
RDX: 0000000000000000 RSI: 000000000000ae80 RDI: 0000000000000005
RBP: 0000000000000082 R08: 00007f9ecbc38700 R09: 0000000000000000
R10: 00007f9ecbc38700 R11: 0000000000000297 R12: 0000000000000000
R13: 0000000000000000 R14: 00007f9ecbc389c0 R15: 00007f9ecbc38700



// autogenerated by syzkaller (http://github.com/google/syzkaller)
#define _GNU_SOURCE
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <unistd.h>
#include <linux/kvm.h>
#include <stdio.h>

int kvmcpu;
struct kvm_run *run;

void* thr(void* arg)
{
  int res;
  res = ioctl(kvmcpu, KVM_RUN, 0);
  printf("ret1=%d exit_reason=%d suberror=%d\n",
      res, run->exit_reason, run->internal.suberror);
  return 0;
}

void test()
{
  int i, kvm, kvmvm;
  pthread_t th[4];

  kvm = open("/dev/kvm", O_RDWR);
  kvmvm = ioctl(kvm, KVM_CREATE_VM, 0);
  kvmcpu = ioctl(kvmvm, KVM_CREATE_VCPU, 0);
  run = (struct kvm_run*)mmap(0, 4096, PROT_READ|PROT_WRITE,
MAP_SHARED, kvmcpu, 0);
  srand(getpid());
  for (i = 0; i < 4; i++) {
    pthread_create(&th[i], 0, thr, 0);
    usleep(rand() % 10000);
  }
  for (i = 0; i < 4; i++)
    pthread_join(th[i], 0);
}

int main()
{
  for (;;) {
    int pid = fork();
    if (pid < 0)
      exit(1);
    if (pid == 0) {
      test();
      exit(0);
    }
    int status;
    while (waitpid(pid, &status, __WALL) != pid) {}
  }
  return 0;
}

[toc] | [next] | [standalone]


#1707845

FromDavid Hildenbrand <david@redhat.com>
Date2017-08-09 22:30 +0200
Message-ID<ucFM6-3O1-13@gated-at.bofh.it>
In reply to#1707570
On 09.08.2017 19:07, Dmitry Vyukov wrote:
> Hello,
> 
> syzkaller fuzzer has hit the following WARNING in kvm_arch_vcpu_ioctl_run.
> This is easily reproducible and reproducer is attached at the bottom.
> The report is on upstream commit
> 26c5cebfdb6ca799186f1e56be7d6f2480c5012c. This requires setting
> kvm-intel.unrestricted_guest=0 on the machine, with
> unrestricted_guest=1 the WARNING does not happen. Output of the
> program is:
> 
> ret1=0 exit_reason=17 suberror=1
> ret2=0 exit_reason=8 suberror=65530
> 
> 

I wonder if the following is possible:

1) we set vcpu->arch.pio.count != 0

2) we set vcpu->arch.complete_userspace_io = complete_emulated_pio;
(in x86_emulate_instruction)

3) in kvm_arch_vcpu_ioctl_run(), we execute
vcpu->arch.complete_userspace_io and set it to NULL.

4. complete_emulated_pio() calls complete_emulated_io(), which fails
(in some different way) and leaves vcpu->arch.pio.count set.

5. we now have vcpu->arch.pio.count set but
vcpu->arch.complete_userspace_io cleared.

Same should not apply to vcpu->mmio_needed, as we clear it before
calling complete_emulated_io() in complete_emulated_mmio().

AFAIK, we cannot simply clear vcpu->arch.pio.count before calling
complete_emulated_io().

Maybe something like this could help: (untested, wild guess)


diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index 86242ee..60296fef 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -7227,10 +7227,14 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
                int (*cui)(struct kvm_vcpu *) = vcpu->arch.complete_userspace_io;
                vcpu->arch.complete_userspace_io = NULL;
                r = cui(vcpu);
+               if (!vcpu->arch.complete_userspace_io) {
+                       /* no new handler -> everything handled */
+                       vcpu->arch.pio.count = 0;
+                       vcpu->mmio_needed = 0;
+               }
                if (r <= 0)
                        goto out;
-       } else
-               WARN_ON(vcpu->arch.pio.count || vcpu->mmio_needed);
+       }
 
        if (kvm_run->immediate_exit)
                r = -EINTR;


Alternatively, something like this


diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index 86242ee..58be388 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -7127,9 +7127,15 @@ static inline int complete_emulated_io(struct kvm_vcpu *vcpu)
 
 static int complete_emulated_pio(struct kvm_vcpu *vcpu)
 {
+       int ret;
+
        BUG_ON(!vcpu->arch.pio.count);
 
-       return complete_emulated_io(vcpu);
+       ret = complete_emulated_io(vcpu);
+       if (ret && !vcpu->arch.complete_userspace_io)
+               vcpu->arch.pio.count = 0;
+
+       return ret;
 }
 
 /*


Haven't tried to reproduce on upstream yet (at least on my Fedora 25
I can't trigger it), so only wild guesses.

-- 

Thanks,

David

[toc] | [prev] | [next] | [standalone]


#1707860

FromDmitry Vyukov <dvyukov@google.com>
Date2017-08-09 22:40 +0200
Message-ID<ucFVN-3Rs-29@gated-at.bofh.it>
In reply to#1707845
On Wed, Aug 9, 2017 at 10:24 PM, David Hildenbrand <david@redhat.com> wrote:
> On 09.08.2017 19:07, Dmitry Vyukov wrote:
>> Hello,
>>
>> syzkaller fuzzer has hit the following WARNING in kvm_arch_vcpu_ioctl_run.
>> This is easily reproducible and reproducer is attached at the bottom.
>> The report is on upstream commit
>> 26c5cebfdb6ca799186f1e56be7d6f2480c5012c. This requires setting
>> kvm-intel.unrestricted_guest=0 on the machine, with
>> unrestricted_guest=1 the WARNING does not happen. Output of the
>> program is:
>>
>> ret1=0 exit_reason=17 suberror=1
>> ret2=0 exit_reason=8 suberror=65530
>>
>>
>
> I wonder if the following is possible:
>
> 1) we set vcpu->arch.pio.count != 0
>
> 2) we set vcpu->arch.complete_userspace_io = complete_emulated_pio;
> (in x86_emulate_instruction)
>
> 3) in kvm_arch_vcpu_ioctl_run(), we execute
> vcpu->arch.complete_userspace_io and set it to NULL.
>
> 4. complete_emulated_pio() calls complete_emulated_io(), which fails
> (in some different way) and leaves vcpu->arch.pio.count set.
>
> 5. we now have vcpu->arch.pio.count set but
> vcpu->arch.complete_userspace_io cleared.
>
> Same should not apply to vcpu->mmio_needed, as we clear it before
> calling complete_emulated_io() in complete_emulated_mmio().
>
> AFAIK, we cannot simply clear vcpu->arch.pio.count before calling
> complete_emulated_io().
>
> Maybe something like this could help: (untested, wild guess)
>
>
> diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
> index 86242ee..60296fef 100644
> --- a/arch/x86/kvm/x86.c
> +++ b/arch/x86/kvm/x86.c
> @@ -7227,10 +7227,14 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
>                 int (*cui)(struct kvm_vcpu *) = vcpu->arch.complete_userspace_io;
>                 vcpu->arch.complete_userspace_io = NULL;
>                 r = cui(vcpu);
> +               if (!vcpu->arch.complete_userspace_io) {
> +                       /* no new handler -> everything handled */
> +                       vcpu->arch.pio.count = 0;
> +                       vcpu->mmio_needed = 0;
> +               }
>                 if (r <= 0)
>                         goto out;
> -       } else
> -               WARN_ON(vcpu->arch.pio.count || vcpu->mmio_needed);
> +       }
>
>         if (kvm_run->immediate_exit)
>                 r = -EINTR;
>
>
> Alternatively, something like this
>
>
> diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
> index 86242ee..58be388 100644
> --- a/arch/x86/kvm/x86.c
> +++ b/arch/x86/kvm/x86.c
> @@ -7127,9 +7127,15 @@ static inline int complete_emulated_io(struct kvm_vcpu *vcpu)
>
>  static int complete_emulated_pio(struct kvm_vcpu *vcpu)
>  {
> +       int ret;
> +
>         BUG_ON(!vcpu->arch.pio.count);
>
> -       return complete_emulated_io(vcpu);
> +       ret = complete_emulated_io(vcpu);
> +       if (ret && !vcpu->arch.complete_userspace_io)
> +               vcpu->arch.pio.count = 0;
> +
> +       return ret;
>  }
>
>  /*
>
>
> Haven't tried to reproduce on upstream yet (at least on my Fedora 25
> I can't trigger it), so only wild guesses.


It is know to happen back to at least 4.3. Make sure you have
kvm-intel.unrestricted_guest=0, it does not reproduce without it (at
least not with this program).

[toc] | [prev] | [next] | [standalone]


#1707986

FromWanpeng Li <kernellwp@gmail.com>
Date2017-08-10 00:00 +0200
Message-ID<ucHbb-4yf-9@gated-at.bofh.it>
In reply to#1707570
2017-08-10 1:07 GMT+08:00 Dmitry Vyukov <dvyukov@google.com>:
> Hello,
>
> syzkaller fuzzer has hit the following WARNING in kvm_arch_vcpu_ioctl_run.
> This is easily reproducible and reproducer is attached at the bottom.
> The report is on upstream commit
> 26c5cebfdb6ca799186f1e56be7d6f2480c5012c. This requires setting
> kvm-intel.unrestricted_guest=0 on the machine, with
> unrestricted_guest=1 the WARNING does not happen. Output of the
> program is:
>
> ret1=0 exit_reason=17 suberror=1
> ret2=0 exit_reason=8 suberror=65530
>
>
> WARNING: CPU: 1 PID: 2850 at arch/x86/kvm/x86.c:7223
> kvm_arch_vcpu_ioctl_run+0x213/0x5870 arch/x86/kvm/x86.c:7223
> CPU: 1 PID: 2850 Comm: a.out Not tainted 4.13.0-rc3+ #445
> Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS Bochs 01/01/2011
> Call Trace:
>  __dump_stack lib/dump_stack.c:16 [inline]
>  dump_stack+0x194/0x257 lib/dump_stack.c:52
>  panic+0x1e4/0x417 kernel/panic.c:180
>  __warn+0x1c4/0x1d9 kernel/panic.c:541
>  report_bug+0x211/0x2d0 lib/bug.c:183
>  fixup_bug+0x40/0x90 arch/x86/kernel/traps.c:190
>  do_trap_no_signal arch/x86/kernel/traps.c:224 [inline]
>  do_trap+0x260/0x390 arch/x86/kernel/traps.c:273
>  do_error_trap+0x120/0x390 arch/x86/kernel/traps.c:310
>  do_invalid_op+0x1b/0x20 arch/x86/kernel/traps.c:323
>  invalid_op+0x1e/0x30 arch/x86/entry/entry_64.S:846
> RIP: 0010:kvm_arch_vcpu_ioctl_run+0x213/0x5870 arch/x86/kvm/x86.c:7222
> RSP: 0018:ffff8800627cf670 EFLAGS: 00010297
> RAX: ffff880066c44480 RBX: ffff88006b07f000 RCX: ffff880067d0845c
> RDX: 0000000000000000 RSI: 0000000000000000 RDI: ffff880067d08260
> RBP: ffff8800627cfa40 R08: 0000000000000001 R09: 0000000000000000
> R10: ffff8800627cfa58 R11: 0000000000000000 R12: 0000000000000001
> R13: 0000000000000000 R14: ffff88006b934880 R15: ffff880067d08040
>  kvm_vcpu_ioctl+0x64c/0x1010 arch/x86/kvm/../../../virt/kvm/kvm_main.c:2592
>  vfs_ioctl fs/ioctl.c:45 [inline]
>  do_vfs_ioctl+0x1b1/0x1520 fs/ioctl.c:685
>  SYSC_ioctl fs/ioctl.c:700 [inline]
>  SyS_ioctl+0x8f/0xc0 fs/ioctl.c:691
>  entry_SYSCALL_64_fastpath+0x1f/0xbe
> RIP: 0033:0x44ccc9
> RSP: 002b:00007f9ecbc37db8 EFLAGS: 00000297 ORIG_RAX: 0000000000000010
> RAX: ffffffffffffffda RBX: 0000000000000000 RCX: 000000000044ccc9
> RDX: 0000000000000000 RSI: 000000000000ae80 RDI: 0000000000000005
> RBP: 0000000000000082 R08: 00007f9ecbc38700 R09: 0000000000000000
> R10: 00007f9ecbc38700 R11: 0000000000000297 R12: 0000000000000000
> R13: 0000000000000000 R14: 00007f9ecbc389c0 R15: 00007f9ecbc38700
>
>
>
> // autogenerated by syzkaller (http://github.com/google/syzkaller)
> #define _GNU_SOURCE
> #include <pthread.h>
> #include <stdio.h>
> #include <stdlib.h>
> #include <string.h>
> #include <sys/wait.h>
> #include <sys/types.h>
> #include <sys/stat.h>
> #include <sys/mman.h>
> #include <fcntl.h>
> #include <unistd.h>
> #include <linux/kvm.h>
> #include <stdio.h>
>
> int kvmcpu;
> struct kvm_run *run;
>
> void* thr(void* arg)
> {
>   int res;
>   res = ioctl(kvmcpu, KVM_RUN, 0);
>   printf("ret1=%d exit_reason=%d suberror=%d\n",
>       res, run->exit_reason, run->internal.suberror);
>   return 0;
> }
>
> void test()
> {
>   int i, kvm, kvmvm;
>   pthread_t th[4];
>
>   kvm = open("/dev/kvm", O_RDWR);
>   kvmvm = ioctl(kvm, KVM_CREATE_VM, 0);
>   kvmcpu = ioctl(kvmvm, KVM_CREATE_VCPU, 0);
>   run = (struct kvm_run*)mmap(0, 4096, PROT_READ|PROT_WRITE,
> MAP_SHARED, kvmcpu, 0);
>   srand(getpid());
>   for (i = 0; i < 4; i++) {
>     pthread_create(&th[i], 0, thr, 0);
>     usleep(rand() % 10000);
>   }
>   for (i = 0; i < 4; i++)
>     pthread_join(th[i], 0);
> }
>
> int main()
> {
>   for (;;) {
>     int pid = fork();
>     if (pid < 0)
>       exit(1);
>     if (pid == 0) {
>       test();
>       exit(0);
>     }
>     int status;
>     while (waitpid(pid, &status, __WALL) != pid) {}
>   }
>   return 0;
> }

Thanks for the report and I can reproduce, I will take a look.

Regards,
Wanpeng Li

[toc] | [prev] | [next] | [standalone]


#1708221

FromWanpeng Li <kernellwp@gmail.com>
Date2017-08-10 07:40 +0200
Message-ID<ucOml-1d2-1@gated-at.bofh.it>
In reply to#1707570
2017-08-10 1:07 GMT+08:00 Dmitry Vyukov <dvyukov@google.com>:
> Hello,
>
> syzkaller fuzzer has hit the following WARNING in kvm_arch_vcpu_ioctl_run.
> This is easily reproducible and reproducer is attached at the bottom.
> The report is on upstream commit
> 26c5cebfdb6ca799186f1e56be7d6f2480c5012c. This requires setting
> kvm-intel.unrestricted_guest=0 on the machine, with
> unrestricted_guest=1 the WARNING does not happen. Output of the
> program is:
>
> ret1=0 exit_reason=17 suberror=1
> ret2=0 exit_reason=8 suberror=65530

Please have a try. https://lkml.org/lkml/2017/8/10/27

Regards,
Wanpeng Li

>
>
> WARNING: CPU: 1 PID: 2850 at arch/x86/kvm/x86.c:7223
> kvm_arch_vcpu_ioctl_run+0x213/0x5870 arch/x86/kvm/x86.c:7223
> CPU: 1 PID: 2850 Comm: a.out Not tainted 4.13.0-rc3+ #445
> Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS Bochs 01/01/2011
> Call Trace:
>  __dump_stack lib/dump_stack.c:16 [inline]
>  dump_stack+0x194/0x257 lib/dump_stack.c:52
>  panic+0x1e4/0x417 kernel/panic.c:180
>  __warn+0x1c4/0x1d9 kernel/panic.c:541
>  report_bug+0x211/0x2d0 lib/bug.c:183
>  fixup_bug+0x40/0x90 arch/x86/kernel/traps.c:190
>  do_trap_no_signal arch/x86/kernel/traps.c:224 [inline]
>  do_trap+0x260/0x390 arch/x86/kernel/traps.c:273
>  do_error_trap+0x120/0x390 arch/x86/kernel/traps.c:310
>  do_invalid_op+0x1b/0x20 arch/x86/kernel/traps.c:323
>  invalid_op+0x1e/0x30 arch/x86/entry/entry_64.S:846
> RIP: 0010:kvm_arch_vcpu_ioctl_run+0x213/0x5870 arch/x86/kvm/x86.c:7222
> RSP: 0018:ffff8800627cf670 EFLAGS: 00010297
> RAX: ffff880066c44480 RBX: ffff88006b07f000 RCX: ffff880067d0845c
> RDX: 0000000000000000 RSI: 0000000000000000 RDI: ffff880067d08260
> RBP: ffff8800627cfa40 R08: 0000000000000001 R09: 0000000000000000
> R10: ffff8800627cfa58 R11: 0000000000000000 R12: 0000000000000001
> R13: 0000000000000000 R14: ffff88006b934880 R15: ffff880067d08040
>  kvm_vcpu_ioctl+0x64c/0x1010 arch/x86/kvm/../../../virt/kvm/kvm_main.c:2592
>  vfs_ioctl fs/ioctl.c:45 [inline]
>  do_vfs_ioctl+0x1b1/0x1520 fs/ioctl.c:685
>  SYSC_ioctl fs/ioctl.c:700 [inline]
>  SyS_ioctl+0x8f/0xc0 fs/ioctl.c:691
>  entry_SYSCALL_64_fastpath+0x1f/0xbe
> RIP: 0033:0x44ccc9
> RSP: 002b:00007f9ecbc37db8 EFLAGS: 00000297 ORIG_RAX: 0000000000000010
> RAX: ffffffffffffffda RBX: 0000000000000000 RCX: 000000000044ccc9
> RDX: 0000000000000000 RSI: 000000000000ae80 RDI: 0000000000000005
> RBP: 0000000000000082 R08: 00007f9ecbc38700 R09: 0000000000000000
> R10: 00007f9ecbc38700 R11: 0000000000000297 R12: 0000000000000000
> R13: 0000000000000000 R14: 00007f9ecbc389c0 R15: 00007f9ecbc38700
>
>
>
> // autogenerated by syzkaller (http://github.com/google/syzkaller)
> #define _GNU_SOURCE
> #include <pthread.h>
> #include <stdio.h>
> #include <stdlib.h>
> #include <string.h>
> #include <sys/wait.h>
> #include <sys/types.h>
> #include <sys/stat.h>
> #include <sys/mman.h>
> #include <fcntl.h>
> #include <unistd.h>
> #include <linux/kvm.h>
> #include <stdio.h>
>
> int kvmcpu;
> struct kvm_run *run;
>
> void* thr(void* arg)
> {
>   int res;
>   res = ioctl(kvmcpu, KVM_RUN, 0);
>   printf("ret1=%d exit_reason=%d suberror=%d\n",
>       res, run->exit_reason, run->internal.suberror);
>   return 0;
> }
>
> void test()
> {
>   int i, kvm, kvmvm;
>   pthread_t th[4];
>
>   kvm = open("/dev/kvm", O_RDWR);
>   kvmvm = ioctl(kvm, KVM_CREATE_VM, 0);
>   kvmcpu = ioctl(kvmvm, KVM_CREATE_VCPU, 0);
>   run = (struct kvm_run*)mmap(0, 4096, PROT_READ|PROT_WRITE,
> MAP_SHARED, kvmcpu, 0);
>   srand(getpid());
>   for (i = 0; i < 4; i++) {
>     pthread_create(&th[i], 0, thr, 0);
>     usleep(rand() % 10000);
>   }
>   for (i = 0; i < 4; i++)
>     pthread_join(th[i], 0);
> }
>
> int main()
> {
>   for (;;) {
>     int pid = fork();
>     if (pid < 0)
>       exit(1);
>     if (pid == 0) {
>       test();
>       exit(0);
>     }
>     int status;
>     while (waitpid(pid, &status, __WALL) != pid) {}
>   }
>   return 0;
> }

[toc] | [prev] | [standalone]


Back to top | Article view | linux.kernel


csiph-web