]> www.infradead.org Git - users/dwmw2/linux.git/commitdiff
KVM: x86/xen: domid allocation
authorJoao Martins <joao.m.martins@oracle.com>
Fri, 8 Feb 2019 16:19:18 +0000 (11:19 -0500)
committerJoao Martins <joao.m.martins@oracle.com>
Wed, 20 Feb 2019 17:30:52 +0000 (12:30 -0500)
Userspace requests a free @domid to be assigned to itself, or
explicitly selects one by setting @any to 0. The @domid is then
used for various interdomain/unbound event purposes.

Signed-off-by: Joao Martins <joao.m.martins@oracle.com>
arch/x86/include/asm/kvm_host.h
arch/x86/kvm/x86.c
arch/x86/kvm/xen.c
arch/x86/kvm/xen.h
include/uapi/linux/kvm.h

index c629fedb2e215321f10b38b7e8aba9a8eb95dbca..384247fc433d8b4e3db7a2c95c06b88f060b67a2 100644 (file)
@@ -27,6 +27,7 @@
 #include <linux/clocksource.h>
 #include <linux/irqbypass.h>
 #include <linux/hyperv.h>
+#include <xen/interface/xen.h>
 
 #include <asm/apic.h>
 #include <asm/pvclock-abi.h>
@@ -862,6 +863,7 @@ struct kvm_hv {
 /* Xen emulation context */
 struct kvm_xen {
        u64 xen_hypercall;
+       domid_t domid;
 
        gfn_t shinfo_addr;
        struct shared_info *shinfo;
index b1d9045d7989c84d9382d84458c3c81b12612cab..cb95f7f8bed9e76da8cba0d7dc3dd1294b2a6c74 100644 (file)
@@ -6986,6 +6986,7 @@ int kvm_arch_init(void *opaque)
        if (hypervisor_is_type(X86_HYPER_MS_HYPERV))
                set_hv_tscchange_cb(kvm_hyperv_tsc_notifier);
 #endif
+       kvm_xen_init();
 
        return 0;
 
@@ -6999,6 +7000,7 @@ out:
 
 void kvm_arch_exit(void)
 {
+       kvm_xen_exit();
 #ifdef CONFIG_X86_64
        if (hypervisor_is_type(X86_HYPER_MS_HYPERV))
                clear_hv_tscchange_cb();
index 07066402737d57ef089492705c66cf97cfaca58f..e570c9b26563cb0c549b5cb916ee2bf09ad68924 100644 (file)
@@ -36,6 +36,48 @@ struct evtchnfd {
 static int kvm_xen_evtchn_send(struct kvm_vcpu *vcpu, int port);
 static void *xen_vcpu_info(struct kvm_vcpu *v);
 
+#define XEN_DOMID_MIN  1
+#define XEN_DOMID_MAX  (DOMID_FIRST_RESERVED - 1)
+
+static rwlock_t domid_lock;
+static struct idr domid_to_kvm;
+
+static int kvm_xen_domid_init(struct kvm *kvm, bool any, domid_t domid)
+{
+       u16 min = XEN_DOMID_MIN, max = XEN_DOMID_MAX;
+       struct kvm_xen *xen = &kvm->arch.xen;
+       int ret;
+
+       if (!any) {
+               min = domid;
+               max = domid + 1;
+       }
+
+       write_lock_bh(&domid_lock);
+       ret = idr_alloc(&domid_to_kvm, kvm, min, max, GFP_ATOMIC);
+       write_unlock_bh(&domid_lock);
+
+       if (ret < 0)
+               return ret;
+
+       xen->domid = ret;
+       return 0;
+}
+
+int kvm_xen_free_domid(struct kvm *kvm)
+{
+       struct kvm_xen *xen = &kvm->arch.xen;
+       struct kvm *vm;
+
+       write_lock_bh(&domid_lock);
+       vm = idr_remove(&domid_to_kvm, xen->domid);
+       write_unlock_bh(&domid_lock);
+
+       synchronize_srcu(&kvm->srcu);
+
+       return vm == kvm;
+}
+
 int kvm_xen_has_interrupt(struct kvm_vcpu *vcpu)
 {
        struct kvm_vcpu_xen *vcpu_xen = vcpu_to_xen_vcpu(vcpu);
@@ -460,6 +502,17 @@ int kvm_xen_hvm_set_attr(struct kvm *kvm, struct kvm_xen_hvm_attr *data)
                r = kvm_vm_ioctl_xen_eventfd(kvm, &xevfd);
                break;
        }
+       case KVM_XEN_ATTR_TYPE_DOMID: {
+               domid_t domid = (u16) data->u.dom.domid;
+               bool any = (data->u.dom.domid < 0);
+
+               /* Domain ID 0 or >= 0x7ff0 are reserved */
+               if (!any && (!domid || (domid >= XEN_DOMID_MAX)))
+                       return -EINVAL;
+
+               r = kvm_xen_domid_init(kvm, any, domid);
+               break;
+       }
        default:
                break;
        }
@@ -489,6 +542,11 @@ int kvm_xen_hvm_get_attr(struct kvm *kvm, struct kvm_xen_hvm_attr *data)
                r = 0;
                break;
        }
+       case KVM_XEN_ATTR_TYPE_DOMID: {
+               data->u.dom.domid = kvm->arch.xen.domid;
+               r = 0;
+               break;
+       }
        default:
                break;
        }
@@ -909,6 +967,18 @@ void kvm_xen_destroy_vm(struct kvm *kvm)
 
        if (xen->shinfo)
                put_page(virt_to_page(xen->shinfo));
+
+       kvm_xen_free_domid(kvm);
+}
+
+void kvm_xen_init(void)
+{
+       idr_init(&domid_to_kvm);
+       rwlock_init(&domid_lock);
+}
+
+void kvm_xen_exit(void)
+{
 }
 
 static int kvm_xen_eventfd_update(struct kvm *kvm, struct idr *port_to_evt,
index f82b8b5b3345468842083bb51a0488d78a1dee5c..76ef2150c650089fddb68d041852935c0b1f2003 100644 (file)
@@ -39,6 +39,8 @@ void kvm_xen_destroy_vm(struct kvm *kvm);
 int kvm_vm_ioctl_xen_eventfd(struct kvm *kvm, struct kvm_xen_eventfd *args);
 void kvm_xen_vcpu_init(struct kvm_vcpu *vcpu);
 void kvm_xen_vcpu_uninit(struct kvm_vcpu *vcpu);
+void kvm_xen_init(void);
+void kvm_xen_exit(void);
 
 void __kvm_migrate_xen_timer(struct kvm_vcpu *vcpu);
 int kvm_xen_has_pending_timer(struct kvm_vcpu *vcpu);
index 1b3ecce5f92edabd757ac63bcc29783c83d882b6..3212cad732ddf6685b2935395f65a27d6fea7fa2 100644 (file)
@@ -1500,6 +1500,9 @@ struct kvm_xen_hvm_attr {
                                __u32 padding[2];
                        };
                } evtchn;
+               struct {
+                       __s32 domid;
+               } dom;
        } u;
 };
 
@@ -1510,6 +1513,7 @@ struct kvm_xen_hvm_attr {
 #define KVM_XEN_ATTR_TYPE_VCPU_RUNSTATE     0x3
 /* Available with KVM_CAP_XEN_HVM_EVTCHN */
 #define KVM_XEN_ATTR_TYPE_EVTCHN            0x4
+#define KVM_XEN_ATTR_TYPE_DOMID             0x5
 
 /* Secure Encrypted Virtualization command */
 enum sev_cmd_id {