Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sys/dev/nvmm Implement nvmm_vcpu::stop, a race-free exit fro...



details:   https://anonhg.NetBSD.org/src/rev/4da5a3741446
branches:  trunk
changeset: 981913:4da5a3741446
user:      reinoud <reinoud%NetBSD.org@localhost>
date:      Fri Mar 26 15:59:53 2021 +0000

description:
Implement nvmm_vcpu::stop, a race-free exit from nvmm_vcpu_run() without
signals. This introduces a new kernel and userland NVMM version indicating
this support.

Patch by Kamil Rytarowski <kamil%netbsd.org@localhost> and committed on his request.

diffstat:

 sys/dev/nvmm/nvmm.c             |   8 ++++----
 sys/dev/nvmm/nvmm.h             |  12 ++++++++++--
 sys/dev/nvmm/nvmm_internal.h    |  12 ++++++++++--
 sys/dev/nvmm/x86/nvmm_x86.h     |   3 ++-
 sys/dev/nvmm/x86/nvmm_x86_svm.c |   6 +++---
 sys/dev/nvmm/x86/nvmm_x86_vmx.c |   6 +++---
 6 files changed, 32 insertions(+), 15 deletions(-)

diffs (173 lines):

diff -r a1e652d074ce -r 4da5a3741446 sys/dev/nvmm/nvmm.c
--- a/sys/dev/nvmm/nvmm.c       Fri Mar 26 10:35:08 2021 +0000
+++ b/sys/dev/nvmm/nvmm.c       Fri Mar 26 15:59:53 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: nvmm.c,v 1.41 2020/09/08 16:58:38 maxv Exp $   */
+/*     $NetBSD: nvmm.c,v 1.42 2021/03/26 15:59:53 reinoud Exp $        */
 
 /*
  * Copyright (c) 2018-2020 Maxime Villard, m00nbsd.net
@@ -29,7 +29,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: nvmm.c,v 1.41 2020/09/08 16:58:38 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: nvmm.c,v 1.42 2021/03/26 15:59:53 reinoud Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -574,8 +574,7 @@
 
        while (1) {
                /* Got a signal? Or pending resched? Leave. */
-               if (__predict_false(nvmm_return_needed())) {
-                       exit->reason = NVMM_VCPU_EXIT_NONE;
+               if (__predict_false(nvmm_return_needed(vcpu, exit))) {
                        return 0;
                }
 
@@ -620,6 +619,7 @@
 
 out:
        nvmm_machine_put(mach);
+       vcpu->comm->stop = 0;
        return error;
 }
 
diff -r a1e652d074ce -r 4da5a3741446 sys/dev/nvmm/nvmm.h
--- a/sys/dev/nvmm/nvmm.h       Fri Mar 26 10:35:08 2021 +0000
+++ b/sys/dev/nvmm/nvmm.h       Fri Mar 26 15:59:53 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: nvmm.h,v 1.15 2020/09/05 07:22:25 maxv Exp $   */
+/*     $NetBSD: nvmm.h,v 1.16 2021/03/26 15:59:53 reinoud Exp $        */
 
 /*
  * Copyright (c) 2018-2020 Maxime Villard, m00nbsd.net
@@ -47,7 +47,12 @@
 #include <dev/nvmm/x86/nvmm_x86.h>
 #endif
 
-#define NVMM_KERN_VERSION              1
+#define NVMM_KERN_VERSION              2
+
+/*
+ * Version 1 - Initial release in NetBSD 9.0.
+ * Version 2 - Added nvmm_vcpu::stop.
+ */
 
 struct nvmm_capability {
        uint32_t version;
@@ -80,6 +85,9 @@
        /* Event. */
        bool event_commit;
        struct nvmm_vcpu_event event;
+
+       /* Race-free exit from nvmm_vcpu_run() without signals. */
+       volatile int stop;
 };
 
 /*
diff -r a1e652d074ce -r 4da5a3741446 sys/dev/nvmm/nvmm_internal.h
--- a/sys/dev/nvmm/nvmm_internal.h      Fri Mar 26 10:35:08 2021 +0000
+++ b/sys/dev/nvmm/nvmm_internal.h      Fri Mar 26 15:59:53 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: nvmm_internal.h,v 1.19 2020/09/06 02:18:53 riastradh Exp $     */
+/*     $NetBSD: nvmm_internal.h,v 1.20 2021/03/26 15:59:53 reinoud Exp $       */
 
 /*
  * Copyright (c) 2018-2020 Maxime Villard, m00nbsd.net
@@ -136,14 +136,22 @@
 #endif
 
 static inline bool
-nvmm_return_needed(void)
+nvmm_return_needed(struct nvmm_cpu *vcpu, struct nvmm_vcpu_exit *exit)
 {
+
        if (preempt_needed()) {
+               exit->reason = NVMM_VCPU_EXIT_NONE;
                return true;
        }
        if (curlwp->l_flag & LW_USERRET) {
+               exit->reason = NVMM_VCPU_EXIT_NONE;
                return true;
        }
+       if (vcpu->comm->stop) {
+               exit->reason = NVMM_VCPU_EXIT_STOPPED;
+               return true;
+       }
+
        return false;
 }
 
diff -r a1e652d074ce -r 4da5a3741446 sys/dev/nvmm/x86/nvmm_x86.h
--- a/sys/dev/nvmm/x86/nvmm_x86.h       Fri Mar 26 10:35:08 2021 +0000
+++ b/sys/dev/nvmm/x86/nvmm_x86.h       Fri Mar 26 15:59:53 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: nvmm_x86.h,v 1.20 2020/09/05 07:22:26 maxv Exp $       */
+/*     $NetBSD: nvmm_x86.h,v 1.21 2021/03/26 15:59:53 reinoud Exp $    */
 
 /*
  * Copyright (c) 2018-2020 Maxime Villard, m00nbsd.net
@@ -74,6 +74,7 @@
 
 /* Generic. */
 #define NVMM_VCPU_EXIT_NONE            0x0000000000000000ULL
+#define NVMM_VCPU_EXIT_STOPPED         0xFFFFFFFFFFFFFFFEULL
 #define NVMM_VCPU_EXIT_INVALID         0xFFFFFFFFFFFFFFFFULL
 /* x86: operations. */
 #define NVMM_VCPU_EXIT_MEMORY          0x0000000000000001ULL
diff -r a1e652d074ce -r 4da5a3741446 sys/dev/nvmm/x86/nvmm_x86_svm.c
--- a/sys/dev/nvmm/x86/nvmm_x86_svm.c   Fri Mar 26 10:35:08 2021 +0000
+++ b/sys/dev/nvmm/x86/nvmm_x86_svm.c   Fri Mar 26 15:59:53 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: nvmm_x86_svm.c,v 1.82 2020/10/24 07:14:30 mgorny Exp $ */
+/*     $NetBSD: nvmm_x86_svm.c,v 1.83 2021/03/26 15:59:53 reinoud Exp $        */
 
 /*
  * Copyright (c) 2018-2020 Maxime Villard, m00nbsd.net
@@ -29,7 +29,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: nvmm_x86_svm.c,v 1.82 2020/10/24 07:14:30 mgorny Exp $");
+__KERNEL_RCSID(0, "$NetBSD: nvmm_x86_svm.c,v 1.83 2021/03/26 15:59:53 reinoud Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -1612,7 +1612,7 @@
                }
 
                /* If no reason to return to userland, keep rolling. */
-               if (nvmm_return_needed()) {
+               if (nvmm_return_needed(vcpu, exit)) {
                        break;
                }
                if (exit->reason != NVMM_VCPU_EXIT_NONE) {
diff -r a1e652d074ce -r 4da5a3741446 sys/dev/nvmm/x86/nvmm_x86_vmx.c
--- a/sys/dev/nvmm/x86/nvmm_x86_vmx.c   Fri Mar 26 10:35:08 2021 +0000
+++ b/sys/dev/nvmm/x86/nvmm_x86_vmx.c   Fri Mar 26 15:59:53 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: nvmm_x86_vmx.c,v 1.81 2020/10/24 07:14:30 mgorny Exp $ */
+/*     $NetBSD: nvmm_x86_vmx.c,v 1.82 2021/03/26 15:59:53 reinoud Exp $        */
 
 /*
  * Copyright (c) 2018-2020 Maxime Villard, m00nbsd.net
@@ -29,7 +29,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: nvmm_x86_vmx.c,v 1.81 2020/10/24 07:14:30 mgorny Exp $");
+__KERNEL_RCSID(0, "$NetBSD: nvmm_x86_vmx.c,v 1.82 2021/03/26 15:59:53 reinoud Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -2337,7 +2337,7 @@
                }
 
                /* If no reason to return to userland, keep rolling. */
-               if (nvmm_return_needed()) {
+               if (nvmm_return_needed(vcpu, exit)) {
                        break;
                }
                if (exit->reason != NVMM_VCPU_EXIT_NONE) {



Home | Main Index | Thread Index | Old Index