Wed Aug 26 16:28:17 2020 UTC ()
nvmm: misc improvements

 - use mach->ncpus to get the number of vcpus, now that we have it
 - don't forget to decrement mach->ncpus when a machine gets killed
 - add more __predict_false()


(maxv)
diff -r1.35 -r1.36 src/sys/dev/nvmm/nvmm.c

cvs diff -r1.35 -r1.36 src/sys/dev/nvmm/nvmm.c (expand / switch to context diff)
--- src/sys/dev/nvmm/nvmm.c 2020/08/18 17:04:37 1.35
+++ src/sys/dev/nvmm/nvmm.c 2020/08/26 16:28:17 1.36
@@ -1,4 +1,4 @@
-/*	$NetBSD: nvmm.c,v 1.35 2020/08/18 17:04:37 maxv Exp $	*/
+/*	$NetBSD: nvmm.c,v 1.36 2020/08/26 16:28:17 maxv Exp $	*/
 
 /*
  * Copyright (c) 2018-2020 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: nvmm.c,v 1.35 2020/08/18 17:04:37 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: nvmm.c,v 1.36 2020/08/26 16:28:17 maxv Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -112,17 +112,17 @@
 	struct nvmm_machine *mach;
 	krw_t op = writer ? RW_WRITER : RW_READER;
 
-	if (machid >= NVMM_MAX_MACHINES) {
+	if (__predict_false(machid >= NVMM_MAX_MACHINES)) {
 		return EINVAL;
 	}
 	mach = &machines[machid];
 
 	rw_enter(&mach->lock, op);
-	if (!mach->present) {
+	if (__predict_false(!mach->present)) {
 		rw_exit(&mach->lock);
 		return ENOENT;
 	}
-	if (owner != &root_owner && mach->owner != owner) {
+	if (__predict_false(mach->owner != owner && owner != &root_owner)) {
 		rw_exit(&mach->lock);
 		return EPERM;
 	}
@@ -179,13 +179,13 @@
 {
 	struct nvmm_cpu *vcpu;
 
-	if (cpuid >= NVMM_MAX_VCPUS) {
+	if (__predict_false(cpuid >= NVMM_MAX_VCPUS)) {
 		return EINVAL;
 	}
 	vcpu = &mach->cpus[cpuid];
 
 	mutex_enter(&vcpu->lock);
-	if (!vcpu->present) {
+	if (__predict_false(!vcpu->present)) {
 		mutex_exit(&vcpu->lock);
 		return ENOENT;
 	}
@@ -227,6 +227,7 @@
 			(*nvmm_impl->vcpu_destroy)(mach, vcpu);
 			nvmm_vcpu_free(mach, vcpu);
 			nvmm_vcpu_put(vcpu);
+			atomic_dec_uint(&mach->ncpus);
 		}
 		(*nvmm_impl->machine_destroy)(mach);
 		uvmspace_free(mach->vm);
@@ -314,6 +315,7 @@
 		(*nvmm_impl->vcpu_destroy)(mach, vcpu);
 		nvmm_vcpu_free(mach, vcpu);
 		nvmm_vcpu_put(vcpu);
+		atomic_dec_uint(&mach->ncpus);
 	}
 
 	(*nvmm_impl->machine_destroy)(mach);
@@ -414,7 +416,6 @@
 	}
 
 	nvmm_vcpu_put(vcpu);
-
 	atomic_inc_uint(&mach->ncpus);
 
 out:
@@ -440,7 +441,6 @@
 	(*nvmm_impl->vcpu_destroy)(mach, vcpu);
 	nvmm_vcpu_free(mach, vcpu);
 	nvmm_vcpu_put(vcpu);
-
 	atomic_dec_uint(&mach->ncpus);
 
 out:
@@ -907,7 +907,6 @@
 {
 	struct nvmm_ctl_mach_info ctl;
 	struct nvmm_machine *mach;
-	struct nvmm_cpu *vcpu;
 	int error;
 	size_t i;
 
@@ -921,14 +920,7 @@
 	if (error)
 		return error;
 
-	ctl.nvcpus = 0;
-	for (i = 0; i < NVMM_MAX_VCPUS; i++) {
-		error = nvmm_vcpu_get(mach, i, &vcpu);
-		if (error)
-			continue;
-		ctl.nvcpus++;
-		nvmm_vcpu_put(vcpu);
-	}
+	ctl.nvcpus = mach->ncpus;
 
 	ctl.nram = 0;
 	for (i = 0; i < NVMM_MAX_HMAPPINGS; i++) {