Received: by 2002:ac0:a679:0:0:0:0:0 with SMTP id p54csp960991imp; Wed, 20 Feb 2019 12:19:36 -0800 (PST) X-Google-Smtp-Source: AHgI3Ib2khipc7TJkrDMS77OCiiaMyOphtaQTTpwA+NM9hdZvud75xTVZrEn2GZotqmwKG/mmFR0 X-Received: by 2002:a63:d814:: with SMTP id b20mr31304394pgh.312.1550693976757; Wed, 20 Feb 2019 12:19:36 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1550693976; cv=none; d=google.com; s=arc-20160816; b=i9x2jIlz0u323vnaeLUoFe+I6GLXbGViYIOR7P9DcY9cFYf9Wq+QbhdVuwjAOW1ILz s8xlaCdTDtavZkNYEdGAkRljar99lDSvD1kAzFuoHtX8eeNaRgFosT3/GQfrBIUsZf+a OQxjRZiwUZ9dusbDyi5hGhQ/0XUDGfp0PQDxa7OzgfTC7P9o3cK8hoP94Vre39RIw3Dw FSExiQR9fLI9JTGMxKKUZYjtV4OJ671u2xYwwg3aQs2KY8fAPIKOancqwO8bMgsygeQX q9uEAYLMAzPZQEsqVFHEy2PPKRSky6wGXt1lqP5gtRfRUNs1GJfHZyDGjwZtUUk0EES0 8Rjg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=Ocu54pk3NtEKCI8wPFFHUf3OhFUmnOlU+mn28ZBsLxs=; b=HAHFxSB2DzsRrr188BzlN0Q9TF1WmxBhQqKPMOdvJdGf4OzGtMAudX8TqMLrbRbKgF X+3ZJtPNgi9JrVrHFFivwOCUx8jPD+hTDw29PUD1xWVscVwSrc6KW0bz+MsxL//NH9QS ftlN3JI8Azz8QE418r0MYDh0GORuRhXWfuW2WPZrvrBirRXxIxXsZi4i4+abWcJuGOz4 gytoJtgiaiIMiL3Qu6gBDZw4D0Tk8/Gx8BS82pEzft7luV1tmDllJlkjutGo5pyZgLUI qu+XLr1zDG9cKYLORYPGxMJVbE4tYe//MAJMUk4llW7BVCEG1qpK/ZRz/PxRrti/SGCu jkDA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@oracle.com header.s=corp-2018-07-02 header.b=eyDCvbxc; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=oracle.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id v22si3113999pfj.148.2019.02.20.12.19.21; Wed, 20 Feb 2019 12:19:36 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@oracle.com header.s=corp-2018-07-02 header.b=eyDCvbxc; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=oracle.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727845AbfBTUSW (ORCPT + 99 others); Wed, 20 Feb 2019 15:18:22 -0500 Received: from aserp2130.oracle.com ([141.146.126.79]:33644 "EHLO aserp2130.oracle.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727813AbfBTUSV (ORCPT ); Wed, 20 Feb 2019 15:18:21 -0500 Received: from pps.filterd (aserp2130.oracle.com [127.0.0.1]) by aserp2130.oracle.com (8.16.0.27/8.16.0.27) with SMTP id x1KK8Y1B087453; Wed, 20 Feb 2019 20:18:07 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-type : content-transfer-encoding; s=corp-2018-07-02; bh=Ocu54pk3NtEKCI8wPFFHUf3OhFUmnOlU+mn28ZBsLxs=; b=eyDCvbxcxlnQUnAhXMWTq12RS2kENa36wky7g0bR1uaDpPp0n12Qhvrbw8nqE73ssvaN gS9dCp2T+M5HyF/lEevsnAGGLa0bzokAJSf4pXTnSUmLwewez/yjWISzLmKl3NTElg36 Sp3G9VGWANNuTWww/+KtnC4ouasEH6SM3DGDdkLgbItPQ6lm7jrcbY+eapIHTUUiexgx iygX6DtGHYMnBmmwJlzYnCkEsRGnhU8f/2kcGSj22Cvvcm6ilvRqWoCppiue7M3Dp6eU pQ4m6z1r87WWBFyDsewGFF+4Wxov7EEp1WG6b9jv83U3PHWmCdlwLMVBi7uK8pmViWhz gA== Received: from userv0021.oracle.com (userv0021.oracle.com [156.151.31.71]) by aserp2130.oracle.com with ESMTP id 2qp81ec3cn-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 20 Feb 2019 20:18:07 +0000 Received: from aserv0122.oracle.com (aserv0122.oracle.com [141.146.126.236]) by userv0021.oracle.com (8.14.4/8.14.4) with ESMTP id x1KKI0fu027446 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 20 Feb 2019 20:18:01 GMT Received: from abhmp0022.oracle.com (abhmp0022.oracle.com [141.146.116.28]) by aserv0122.oracle.com (8.14.4/8.14.4) with ESMTP id x1KKI0x3009753; Wed, 20 Feb 2019 20:18:00 GMT Received: from paddy.lan (/94.61.137.133) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Wed, 20 Feb 2019 12:18:00 -0800 From: Joao Martins To: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Ankur Arora , Boris Ostrovsky , Joao Martins , Paolo Bonzini , =?UTF-8?q?Radim=20Kr=C4=8Dm=C3=A1=C5=99?= , Thomas Gleixner , Ingo Molnar , Borislav Petkov , "H. Peter Anvin" , x86@kernel.org Subject: [PATCH RFC 21/39] KVM: x86/xen: domid allocation Date: Wed, 20 Feb 2019 20:15:51 +0000 Message-Id: <20190220201609.28290-22-joao.m.martins@oracle.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20190220201609.28290-1-joao.m.martins@oracle.com> References: <20190220201609.28290-1-joao.m.martins@oracle.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Proofpoint-Virus-Version: vendor=nai engine=5900 definitions=9173 signatures=668683 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 priorityscore=1501 malwarescore=0 suspectscore=2 phishscore=0 bulkscore=0 spamscore=0 clxscore=1015 lowpriorityscore=0 mlxscore=0 impostorscore=0 mlxlogscore=636 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1810050000 definitions=main-1902200138 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 --- arch/x86/include/asm/kvm_host.h | 2 ++ arch/x86/kvm/x86.c | 2 ++ arch/x86/kvm/xen.c | 70 +++++++++++++++++++++++++++++++++++++++++ arch/x86/kvm/xen.h | 2 ++ include/uapi/linux/kvm.h | 4 +++ 5 files changed, 80 insertions(+) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index c629fedb2e21..384247fc433d 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -27,6 +27,7 @@ #include #include #include +#include #include #include @@ -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; diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index b1d9045d7989..cb95f7f8bed9 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -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 @@ int kvm_arch_init(void *opaque) void kvm_arch_exit(void) { + kvm_xen_exit(); #ifdef CONFIG_X86_64 if (hypervisor_is_type(X86_HYPER_MS_HYPERV)) clear_hv_tscchange_cb(); diff --git a/arch/x86/kvm/xen.c b/arch/x86/kvm/xen.c index 07066402737d..e570c9b26563 100644 --- a/arch/x86/kvm/xen.c +++ b/arch/x86/kvm/xen.c @@ -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, diff --git a/arch/x86/kvm/xen.h b/arch/x86/kvm/xen.h index f82b8b5b3345..76ef2150c650 100644 --- a/arch/x86/kvm/xen.h +++ b/arch/x86/kvm/xen.h @@ -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); diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h index 1b3ecce5f92e..3212cad732dd 100644 --- a/include/uapi/linux/kvm.h +++ b/include/uapi/linux/kvm.h @@ -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 { -- 2.11.0