Module Name:    src
Committed By:   maxv
Date:           Sat Jan 26 15:25:51 UTC 2019

Modified Files:
        src/sys/dev/nvmm: nvmm.c nvmm_internal.h

Log Message:
Optimize: keep a per-VCPU buffer for the state, and copy in and out
directly on it. The VCPUs are protected by mutexes, so nothing to worry
about.

This saves two kmem_allocs in {get,set}state.


To generate a diff of this commit:
cvs rdiff -u -r1.5 -r1.6 src/sys/dev/nvmm/nvmm.c
cvs rdiff -u -r1.2 -r1.3 src/sys/dev/nvmm/nvmm_internal.h

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/nvmm/nvmm.c
diff -u src/sys/dev/nvmm/nvmm.c:1.5 src/sys/dev/nvmm/nvmm.c:1.6
--- src/sys/dev/nvmm/nvmm.c:1.5	Sun Jan  6 16:10:51 2019
+++ src/sys/dev/nvmm/nvmm.c	Sat Jan 26 15:25:51 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: nvmm.c,v 1.5 2019/01/06 16:10:51 maxv Exp $	*/
+/*	$NetBSD: nvmm.c,v 1.6 2019/01/26 15:25:51 maxv Exp $	*/
 
 /*
  * Copyright (c) 2018 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: nvmm.c,v 1.5 2019/01/06 16:10:51 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: nvmm.c,v 1.6 2019/01/26 15:25:51 maxv Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -142,6 +142,7 @@ nvmm_vcpu_alloc(struct nvmm_machine *mac
 
 		vcpu->present = true;
 		vcpu->cpuid = i;
+		vcpu->state = kmem_zalloc(nvmm_impl->state_size, KM_SLEEP);
 		*ret = vcpu;
 		return 0;
 	}
@@ -154,6 +155,7 @@ nvmm_vcpu_free(struct nvmm_machine *mach
 {
 	KASSERT(mutex_owned(&vcpu->lock));
 	vcpu->present = false;
+	kmem_free(vcpu->state, nvmm_impl->state_size);
 	vcpu->hcpu_last = -1;
 }
 
@@ -404,33 +406,27 @@ nvmm_vcpu_setstate(struct nvmm_ioc_vcpu_
 {
 	struct nvmm_machine *mach;
 	struct nvmm_cpu *vcpu;
-	void *data;
 	int error;
 
-	data = kmem_alloc(nvmm_impl->state_size, KM_SLEEP);
-
 	error = nvmm_machine_get(args->machid, &mach, false);
-	if (error) {
-		kmem_free(data, nvmm_impl->state_size);
+	if (error)
 		return error;
-	}
 
 	error = nvmm_vcpu_get(mach, args->cpuid, &vcpu);
 	if (error)
 		goto out;
 
-	error = copyin(args->state, data, nvmm_impl->state_size);
+	error = copyin(args->state, vcpu->state, nvmm_impl->state_size);
 	if (error) {
 		nvmm_vcpu_put(vcpu);
 		goto out;
 	}
 
-	(*nvmm_impl->vcpu_setstate)(vcpu, data, args->flags);
+	(*nvmm_impl->vcpu_setstate)(vcpu, vcpu->state, args->flags);
 	nvmm_vcpu_put(vcpu);
 
 out:
 	nvmm_machine_put(mach);
-	kmem_free(data, nvmm_impl->state_size);
 	return error;
 }
 
@@ -439,28 +435,22 @@ nvmm_vcpu_getstate(struct nvmm_ioc_vcpu_
 {
 	struct nvmm_machine *mach;
 	struct nvmm_cpu *vcpu;
-	void *data;
 	int error;
 
-	data = kmem_alloc(nvmm_impl->state_size, KM_SLEEP);
-
 	error = nvmm_machine_get(args->machid, &mach, false);
-	if (error) {
-		kmem_free(data, nvmm_impl->state_size);
+	if (error)
 		return error;
-	}
 
 	error = nvmm_vcpu_get(mach, args->cpuid, &vcpu);
 	if (error)
 		goto out;
 
-	(*nvmm_impl->vcpu_getstate)(vcpu, data, args->flags);
+	(*nvmm_impl->vcpu_getstate)(vcpu, vcpu->state, args->flags);
 	nvmm_vcpu_put(vcpu);
-	error = copyout(data, args->state, nvmm_impl->state_size);
+	error = copyout(vcpu->state, args->state, nvmm_impl->state_size);
 
 out:
 	nvmm_machine_put(mach);
-	kmem_free(data, nvmm_impl->state_size);
 	return error;
 }
 

Index: src/sys/dev/nvmm/nvmm_internal.h
diff -u src/sys/dev/nvmm/nvmm_internal.h:1.2 src/sys/dev/nvmm/nvmm_internal.h:1.3
--- src/sys/dev/nvmm/nvmm_internal.h:1.2	Sat Dec 15 13:39:43 2018
+++ src/sys/dev/nvmm/nvmm_internal.h	Sat Jan 26 15:25:51 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: nvmm_internal.h,v 1.2 2018/12/15 13:39:43 maxv Exp $	*/
+/*	$NetBSD: nvmm_internal.h,v 1.3 2019/01/26 15:25:51 maxv Exp $	*/
 
 /*
  * Copyright (c) 2018 The NetBSD Foundation, Inc.
@@ -43,6 +43,9 @@ struct nvmm_cpu {
 	nvmm_cpuid_t cpuid;
 	kmutex_t lock;
 
+	/* State buffer. */
+	void *state;
+
 	/* Last host CPU on which the VCPU ran. */
 	int hcpu_last;
 

Reply via email to