Source-Changes-HG archive

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

[src/trunk]: src/sys/dev/nvmm Use the fd_clone approach, to avoid losing refe...



details:   https://anonhg.NetBSD.org/src/rev/1991635ba179
branches:  trunk
changeset: 450267:1991635ba179
user:      maxv <maxv%NetBSD.org@localhost>
date:      Mon Apr 08 18:21:42 2019 +0000

description:
Use the fd_clone approach, to avoid losing references to the registered
VMs during fork(). We attach an nvmm_owner struct to the fd, reference it
in each VM, and identify the process' VMs by just comparing the pointer.

diffstat:

 sys/dev/nvmm/nvmm.c          |  230 +++++++++++++++++++++++++-----------------
 sys/dev/nvmm/nvmm_internal.h |    8 +-
 2 files changed, 142 insertions(+), 96 deletions(-)

diffs (truncated from 500 to 300 lines):

diff -r 361b67ea4589 -r 1991635ba179 sys/dev/nvmm/nvmm.c
--- a/sys/dev/nvmm/nvmm.c       Mon Apr 08 15:35:57 2019 +0000
+++ b/sys/dev/nvmm/nvmm.c       Mon Apr 08 18:21:42 2019 +0000
@@ -1,7 +1,7 @@
-/*     $NetBSD: nvmm.c,v 1.13 2019/04/07 14:05:15 maxv Exp $   */
+/*     $NetBSD: nvmm.c,v 1.14 2019/04/08 18:21:42 maxv Exp $   */
 
 /*
- * Copyright (c) 2018 The NetBSD Foundation, Inc.
+ * Copyright (c) 2018-2019 The NetBSD Foundation, Inc.
  * All rights reserved.
  *
  * This code is derived from software contributed to The NetBSD Foundation
@@ -30,7 +30,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: nvmm.c,v 1.13 2019/04/07 14:05:15 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: nvmm.c,v 1.14 2019/04/08 18:21:42 maxv Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -42,6 +42,8 @@
 #include <sys/module.h>
 #include <sys/proc.h>
 #include <sys/mman.h>
+#include <sys/file.h>
+#include <sys/filedesc.h>
 
 #include <uvm/uvm.h>
 #include <uvm/uvm_page.h>
@@ -98,7 +100,8 @@
 }
 
 static int
-nvmm_machine_get(nvmm_machid_t machid, struct nvmm_machine **ret, bool writer)
+nvmm_machine_get(struct nvmm_owner *owner, nvmm_machid_t machid,
+    struct nvmm_machine **ret, bool writer)
 {
        struct nvmm_machine *mach;
        krw_t op = writer ? RW_WRITER : RW_READER;
@@ -113,7 +116,7 @@
                rw_exit(&mach->lock);
                return ENOENT;
        }
-       if (mach->procid != curproc->p_pid) {
+       if (mach->owner != owner) {
                rw_exit(&mach->lock);
                return EPERM;
        }
@@ -194,7 +197,7 @@
 /* -------------------------------------------------------------------------- */
 
 static void
-nvmm_kill_machines(pid_t pid)
+nvmm_kill_machines(struct nvmm_owner *owner)
 {
        struct nvmm_machine *mach;
        struct nvmm_cpu *vcpu;
@@ -205,7 +208,7 @@
                mach = &machines[i];
 
                rw_enter(&mach->lock, RW_WRITER);
-               if (!mach->present || mach->procid != pid) {
+               if (!mach->present || mach->owner != owner) {
                        rw_exit(&mach->lock);
                        continue;
                }
@@ -237,7 +240,7 @@
 /* -------------------------------------------------------------------------- */
 
 static int
-nvmm_capability(struct nvmm_ioc_capability *args)
+nvmm_capability(struct nvmm_owner *owner, struct nvmm_ioc_capability *args)
 {
        args->cap.version = NVMM_CAPABILITY_VERSION;
        args->cap.state_size = nvmm_impl->state_size;
@@ -251,7 +254,8 @@
 }
 
 static int
-nvmm_machine_create(struct nvmm_ioc_machine_create *args)
+nvmm_machine_create(struct nvmm_owner *owner,
+    struct nvmm_ioc_machine_create *args)
 {
        struct nvmm_machine *mach;
        int error;
@@ -261,7 +265,7 @@
                return error;
 
        /* Curproc owns the machine. */
-       mach->procid = curproc->p_pid;
+       mach->owner = owner;
 
        /* Zero out the host mappings. */
        memset(&mach->hmap, 0, sizeof(mach->hmap));
@@ -280,14 +284,15 @@
 }
 
 static int
-nvmm_machine_destroy(struct nvmm_ioc_machine_destroy *args)
+nvmm_machine_destroy(struct nvmm_owner *owner,
+    struct nvmm_ioc_machine_destroy *args)
 {
        struct nvmm_machine *mach;
        struct nvmm_cpu *vcpu;
        int error;
        size_t i;
 
-       error = nvmm_machine_get(args->machid, &mach, true);
+       error = nvmm_machine_get(owner, args->machid, &mach, true);
        if (error)
                return error;
 
@@ -320,7 +325,8 @@
 }
 
 static int
-nvmm_machine_configure(struct nvmm_ioc_machine_configure *args)
+nvmm_machine_configure(struct nvmm_owner *owner,
+    struct nvmm_ioc_machine_configure *args)
 {
        struct nvmm_machine *mach;
        size_t allocsz;
@@ -334,7 +340,7 @@
        allocsz = nvmm_impl->conf_sizes[args->op];
        data = kmem_alloc(allocsz, KM_SLEEP);
 
-       error = nvmm_machine_get(args->machid, &mach, true);
+       error = nvmm_machine_get(owner, args->machid, &mach, true);
        if (error) {
                kmem_free(data, allocsz);
                return error;
@@ -354,13 +360,13 @@
 }
 
 static int
-nvmm_vcpu_create(struct nvmm_ioc_vcpu_create *args)
+nvmm_vcpu_create(struct nvmm_owner *owner, struct nvmm_ioc_vcpu_create *args)
 {
        struct nvmm_machine *mach;
        struct nvmm_cpu *vcpu;
        int error;
 
-       error = nvmm_machine_get(args->machid, &mach, false);
+       error = nvmm_machine_get(owner, args->machid, &mach, false);
        if (error)
                return error;
 
@@ -383,13 +389,13 @@
 }
 
 static int
-nvmm_vcpu_destroy(struct nvmm_ioc_vcpu_destroy *args)
+nvmm_vcpu_destroy(struct nvmm_owner *owner, struct nvmm_ioc_vcpu_destroy *args)
 {
        struct nvmm_machine *mach;
        struct nvmm_cpu *vcpu;
        int error;
 
-       error = nvmm_machine_get(args->machid, &mach, false);
+       error = nvmm_machine_get(owner, args->machid, &mach, false);
        if (error)
                return error;
 
@@ -407,13 +413,14 @@
 }
 
 static int
-nvmm_vcpu_setstate(struct nvmm_ioc_vcpu_setstate *args)
+nvmm_vcpu_setstate(struct nvmm_owner *owner,
+    struct nvmm_ioc_vcpu_setstate *args)
 {
        struct nvmm_machine *mach;
        struct nvmm_cpu *vcpu;
        int error;
 
-       error = nvmm_machine_get(args->machid, &mach, false);
+       error = nvmm_machine_get(owner, args->machid, &mach, false);
        if (error)
                return error;
 
@@ -436,13 +443,14 @@
 }
 
 static int
-nvmm_vcpu_getstate(struct nvmm_ioc_vcpu_getstate *args)
+nvmm_vcpu_getstate(struct nvmm_owner *owner,
+    struct nvmm_ioc_vcpu_getstate *args)
 {
        struct nvmm_machine *mach;
        struct nvmm_cpu *vcpu;
        int error;
 
-       error = nvmm_machine_get(args->machid, &mach, false);
+       error = nvmm_machine_get(owner, args->machid, &mach, false);
        if (error)
                return error;
 
@@ -460,13 +468,13 @@
 }
 
 static int
-nvmm_vcpu_inject(struct nvmm_ioc_vcpu_inject *args)
+nvmm_vcpu_inject(struct nvmm_owner *owner, struct nvmm_ioc_vcpu_inject *args)
 {
        struct nvmm_machine *mach;
        struct nvmm_cpu *vcpu;
        int error;
 
-       error = nvmm_machine_get(args->machid, &mach, false);
+       error = nvmm_machine_get(owner, args->machid, &mach, false);
        if (error)
                return error;
 
@@ -504,13 +512,13 @@
 }
 
 static int
-nvmm_vcpu_run(struct nvmm_ioc_vcpu_run *args)
+nvmm_vcpu_run(struct nvmm_owner *owner, struct nvmm_ioc_vcpu_run *args)
 {
        struct nvmm_machine *mach;
        struct nvmm_cpu *vcpu;
        int error;
 
-       error = nvmm_machine_get(args->machid, &mach, false);
+       error = nvmm_machine_get(owner, args->machid, &mach, false);
        if (error)
                return error;
 
@@ -636,7 +644,7 @@
 }
 
 static int
-nvmm_hva_map(struct nvmm_ioc_hva_map *args)
+nvmm_hva_map(struct nvmm_owner *owner, struct nvmm_ioc_hva_map *args)
 {
        struct vmspace *vmspace = curproc->p_vmspace;
        struct nvmm_machine *mach;
@@ -644,7 +652,7 @@
        vaddr_t uva;
        int error;
 
-       error = nvmm_machine_get(args->machid, &mach, true);
+       error = nvmm_machine_get(owner, args->machid, &mach, true);
        if (error)
                return error;
 
@@ -680,12 +688,12 @@
 }
 
 static int
-nvmm_hva_unmap(struct nvmm_ioc_hva_unmap *args)
+nvmm_hva_unmap(struct nvmm_owner *owner, struct nvmm_ioc_hva_unmap *args)
 {
        struct nvmm_machine *mach;
        int error;
 
-       error = nvmm_machine_get(args->machid, &mach, true);
+       error = nvmm_machine_get(owner, args->machid, &mach, true);
        if (error)
                return error;
 
@@ -698,7 +706,7 @@
 /* -------------------------------------------------------------------------- */
 
 static int
-nvmm_gpa_map(struct nvmm_ioc_gpa_map *args)
+nvmm_gpa_map(struct nvmm_owner *owner, struct nvmm_ioc_gpa_map *args)
 {
        struct nvmm_machine *mach;
        struct uvm_object *uobj;
@@ -706,7 +714,7 @@
        size_t off;
        int error;
 
-       error = nvmm_machine_get(args->machid, &mach, false);
+       error = nvmm_machine_get(owner, args->machid, &mach, false);
        if (error)
                return error;
 
@@ -768,13 +776,13 @@
 }
 
 static int
-nvmm_gpa_unmap(struct nvmm_ioc_gpa_unmap *args)
+nvmm_gpa_unmap(struct nvmm_owner *owner, struct nvmm_ioc_gpa_unmap *args)
 {
        struct nvmm_machine *mach;
        gpaddr_t gpa;
        int error;
 
-       error = nvmm_machine_get(args->machid, &mach, false);
+       error = nvmm_machine_get(owner, args->machid, &mach, false);
        if (error)
                return error;
 
@@ -855,71 +863,14 @@
 



Home | Main Index | Thread Index | Old Index