Source-Changes-HG archive

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

[src/trunk]: src/sys/dev/nvmm Gather the conditions to return from the VCPU l...



details:   https://anonhg.NetBSD.org/src/rev/18584fa748bd
branches:  trunk
changeset: 933302:18584fa748bd
user:      maxv <maxv%NetBSD.org@localhost>
date:      Sun May 24 08:08:49 2020 +0000

description:
Gather the conditions to return from the VCPU loops in nvmm_return_needed(),
and use it in nvmm_do_vcpu_run() as well. This fixes two undesired behaviors:

 - When a VM initializes, the many nested page faults that need processing
   could cause the calling thread to occupy the CPU too much if we're unlucky
   and are only getting repeated nested page faults thousands of times in a
   row.

 - When the emulator calls nvmm_vcpu_run() and immediately sends a signal to
   stop the VCPU, it's better to check signals earlier and leave right away,
   rather than doing a round of VCPU run that could increase the time spent
   by the emulator waiting for the return.

diffstat:

 sys/dev/nvmm/nvmm.c             |  12 ++++++++++--
 sys/dev/nvmm/nvmm_internal.h    |  14 +++++++++++++-
 sys/dev/nvmm/x86/nvmm_x86_svm.c |   9 +++------
 sys/dev/nvmm/x86/nvmm_x86_vmx.c |   9 +++------
 4 files changed, 29 insertions(+), 15 deletions(-)

diffs (124 lines):

diff -r d974c67b5891 -r 18584fa748bd sys/dev/nvmm/nvmm.c
--- a/sys/dev/nvmm/nvmm.c       Sun May 24 07:42:51 2020 +0000
+++ b/sys/dev/nvmm/nvmm.c       Sun May 24 08:08:49 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: nvmm.c,v 1.29 2020/05/21 07:43:23 maxv Exp $   */
+/*     $NetBSD: nvmm.c,v 1.30 2020/05/24 08:08:49 maxv Exp $   */
 
 /*
  * Copyright (c) 2018-2019 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: nvmm.c,v 1.29 2020/05/21 07:43:23 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: nvmm.c,v 1.30 2020/05/24 08:08:49 maxv Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -570,11 +570,19 @@
        int ret;
 
        while (1) {
+               /* Got a signal? Or pending resched? Leave. */
+               if (__predict_false(nvmm_return_needed())) {
+                       exit->reason = NVMM_VCPU_EXIT_NONE;
+                       return 0;
+               }
+
+               /* Run the VCPU. */
                ret = (*nvmm_impl->vcpu_run)(mach, vcpu, exit);
                if (__predict_false(ret != 0)) {
                        return ret;
                }
 
+               /* Process nested page faults. */
                if (__predict_true(exit->reason != NVMM_VCPU_EXIT_MEMORY)) {
                        break;
                }
diff -r d974c67b5891 -r 18584fa748bd sys/dev/nvmm/nvmm_internal.h
--- a/sys/dev/nvmm/nvmm_internal.h      Sun May 24 07:42:51 2020 +0000
+++ b/sys/dev/nvmm/nvmm_internal.h      Sun May 24 08:08:49 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: nvmm_internal.h,v 1.14 2020/05/09 08:39:07 maxv Exp $  */
+/*     $NetBSD: nvmm_internal.h,v 1.15 2020/05/24 08:08:49 maxv Exp $  */
 
 /*
  * Copyright (c) 2018-2019 The NetBSD Foundation, Inc.
@@ -121,4 +121,16 @@
 extern const struct nvmm_impl nvmm_x86_svm;
 extern const struct nvmm_impl nvmm_x86_vmx;
 
+static inline bool
+nvmm_return_needed(void)
+{
+       if (preempt_needed()) {
+               return true;
+       }
+       if (curlwp->l_flag & LW_USERRET) {
+               return true;
+       }
+       return false;
+}
+
 #endif /* _NVMM_INTERNAL_H_ */
diff -r d974c67b5891 -r 18584fa748bd sys/dev/nvmm/x86/nvmm_x86_svm.c
--- a/sys/dev/nvmm/x86/nvmm_x86_svm.c   Sun May 24 07:42:51 2020 +0000
+++ b/sys/dev/nvmm/x86/nvmm_x86_svm.c   Sun May 24 08:08:49 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: nvmm_x86_svm.c,v 1.61 2020/05/10 06:24:16 maxv Exp $   */
+/*     $NetBSD: nvmm_x86_svm.c,v 1.62 2020/05/24 08:08:49 maxv Exp $   */
 
 /*
  * Copyright (c) 2018-2020 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: nvmm_x86_svm.c,v 1.61 2020/05/10 06:24:16 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: nvmm_x86_svm.c,v 1.62 2020/05/24 08:08:49 maxv Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -1459,10 +1459,7 @@
                }
 
                /* If no reason to return to userland, keep rolling. */
-               if (preempt_needed()) {
-                       break;
-               }
-               if (curlwp->l_flag & LW_USERRET) {
+               if (nvmm_return_needed()) {
                        break;
                }
                if (exit->reason != NVMM_VCPU_EXIT_NONE) {
diff -r d974c67b5891 -r 18584fa748bd sys/dev/nvmm/x86/nvmm_x86_vmx.c
--- a/sys/dev/nvmm/x86/nvmm_x86_vmx.c   Sun May 24 07:42:51 2020 +0000
+++ b/sys/dev/nvmm/x86/nvmm_x86_vmx.c   Sun May 24 08:08:49 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: nvmm_x86_vmx.c,v 1.58 2020/05/21 07:36:16 maxv Exp $   */
+/*     $NetBSD: nvmm_x86_vmx.c,v 1.59 2020/05/24 08:08:49 maxv Exp $   */
 
 /*
  * Copyright (c) 2018-2020 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: nvmm_x86_vmx.c,v 1.58 2020/05/21 07:36:16 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: nvmm_x86_vmx.c,v 1.59 2020/05/24 08:08:49 maxv Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -2181,10 +2181,7 @@
                }
 
                /* If no reason to return to userland, keep rolling. */
-               if (preempt_needed()) {
-                       break;
-               }
-               if (curlwp->l_flag & LW_USERRET) {
+               if (nvmm_return_needed()) {
                        break;
                }
                if (exit->reason != NVMM_VCPU_EXIT_NONE) {



Home | Main Index | Thread Index | Old Index