Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932141AbdHYIhV (ORCPT ); Fri, 25 Aug 2017 04:37:21 -0400 Received: from mail-db5eur01on0042.outbound.protection.outlook.com ([104.47.2.42]:44352 "EHLO EUR01-DB5-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1755021AbdHYIc0 (ORCPT ); Fri, 25 Aug 2017 04:32:26 -0400 Authentication-Results: spf=none (sender IP is ) smtp.mailfrom=Florent.Revest@arm.com; From: Florent Revest To: linux-arm-kernel@lists.infradead.org Cc: matt@codeblueprint.co.uk, ard.biesheuvel@linaro.org, pbonzini@redhat.com, rkrcmar@redhat.com, christoffer.dall@linaro.org, catalin.marinas@arm.com, will.deacon@arm.com, mark.rutland@arm.com, marc.zyngier@arm.com, linux-efi@vger.kernel.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, kvmarm@lists.cs.columbia.edu, leif.lindholm@arm.com, revestflo@gmail.com, Florent Revest Subject: [RFC 03/11] KVM: Allow VM lifecycle management without userspace Date: Fri, 25 Aug 2017 09:31:33 +0100 Message-Id: <1503649901-5834-4-git-send-email-florent.revest@arm.com> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1503649901-5834-1-git-send-email-florent.revest@arm.com> References: <1503649901-5834-1-git-send-email-florent.revest@arm.com> MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [217.140.96.140] X-ClientProxiedBy: HE1PR09CA0051.eurprd09.prod.outlook.com (2603:10a6:7:3c::19) To HE1PR08MB0812.eurprd08.prod.outlook.com (2a01:111:e400:59b1::30) X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 77fb048a-92cc-49ce-c965-08d4eb93cb5a X-MS-Office365-Filtering-HT: Tenant X-Microsoft-Antispam: UriScan:;BCL:0;PCL:0;RULEID:(300000500095)(300135000095)(300000501095)(300135300095)(22001)(300000502095)(300135100095)(2017030254152)(48565401081)(300000503095)(300135400095)(201703131423075)(201703031133081)(201702281549075)(300000504095)(300135200095)(300000505095)(300135600095)(300000506095)(300135500095);SRVR:HE1PR08MB0812; X-Microsoft-Exchange-Diagnostics: 1;HE1PR08MB0812;3:anQd3ltRHLAs1HFPCx9+GKLWGhy9VL9MUzuUjhkzGZHVXqjyiX7N7w2D7cAxO5tdVzAw5HMg+wJRlgmGlFWuV6bNNx369diJ8w7wXyU/E3oB4zkjDY9pMztBTVIf77BfuhveSWwuVLM94t2AqFlTHb4T0FdS1n60KRBoeD/V4w4vYbNfRYJ2SXO0EACju3Q/2Cc/5BB/s4GykqJ8mC99JwDFIB2pzRhO8uw788EvzCXL6GPsOkCpI+REFVi04sgG;25:odJssI4PVhp76gZ+9GInLiG2nlH4S//131xUI99Pe4WvQgDvk0digvzGE6BD4uUxDWWJNIFtbJCV0hshM7To5m46Aqnvuy8Fd8WZH3ODg0lGsasIrXMCoNxGlu7SskBxH4e6fQlHZe4IKnGQ41+FTtHTIKU/vv7C9rxQ6jQSaXsRUxsWtOu12fkbbqHg0Vdy0qyoKFX62uf6IkBcicN2Sd0YHjs/IafWXgqkWC9sJ5Fv+EbVApEIsYojNgs+bteBnYgB5YiYIF0y3luoRSg1PDq7RsSR/TD6mPOrR9CX8VqbCf0w5HvpnEcz3FByhi+pv0LQOmf8pyJYVRqEFzmCDw==;31:7IfRcYnV2vVEDe7kHWzvlsybFsBM1wTdajQ3PwcEniOqCrhMS/VfZL8K9dDTbllSGe7AW9Z6fgAn4W6dQMw2jx4IdtRh8bnmRS5SarjhktjeSkkr9hWpVtA2n2XuRstI2bliUByISucSXxa3k938on4YuYMKS3GD+Aq8Jag5tWsup0abqSTEndfmSyjKM2bP9VPncjvaib6dIiDnNDXTMXQfEDjP5nYScci/XE9h/PE= X-MS-TrafficTypeDiagnostic: HE1PR08MB0812: X-Microsoft-Exchange-Diagnostics: 1;HE1PR08MB0812;20:kvq+rZwPEx8NPnsyDp/pfJ0ZT0vuGWE7zBi6f+lCCpmYXjW37KEFkrdgJHz3RfavU2c02DwVKvUcSarAvJMA9dHMtB0vromRVA+GxXZw5oDlCBa35CdQ7C8ZQ+OnO9hcrD8CgWCyz/tXD80X7WgiYT+SjKQKN546o8UhDySzs3AtGCtVPvcrGyKekjVcKhZtKPJE5Ml7TpUOnLlhqdxlEhd96EXcAxQBqNtbA1kVJwlTBHLCr27Jg9qljJL7v2OFGKO50gxIOjEb8k878Ls271trQRGwSP+wN2VaOWkiRG5DP2aee/sS88ywuBc13H41axm628OVYswzgLgdKJB4bZqARCVd49s3Fw3zWUtK5bfuLcuJ50ruXdZP5E0rTqrqY1p4/N7gN5rA2v51GGNIR6i5E1+e8+R0s0NVg40sJ5RB6P7YWWuBapgIY2UIhekmyAHnVuAthbZTJqP7zomwJCDDwq2/oK6/r9JHU5ToEp3pSzdWJCMUoWZtZTorv2dd;4:+HtvHEZUljOkDY4ZNeNayM59adDkdNsfbgC++bHuSsyjv6/f9+iMVcKy0YLPoj92FTr0oyFDPzjE4qyoRONP+hvAt+1S4WApfDwV4xMVTmnf6Y9iuLeCOsGztX2No+LPUgO3yXa3dIB2FZANUP9g1lBvIL8EkpwJma5BCCMva8DVTrEpXzEFiCPYgVEgIxCE+KaSOzIp5f/0xohf9P2+9v7kXSvQqMH1l796ahlK6Ht27wXuyfLxyefRl9p0qHaHzGk7q2dJd1cp5Zzfi/KZetPwqEDQCkgOFmYOGfO6HXLOoToYJSJk4tbZRCnx3QTcDK+9gHyVz+OS2VpzYeetyg== X-Exchange-Antispam-Report-Test: UriScan:(180628864354917)(209352067349851); X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-CFA-Test: BCL:0;PCL:0;RULEID:(100000700101)(100105000095)(100000701101)(100105300095)(100000702101)(100105100095)(6040450)(601004)(2401047)(8121501046)(5005006)(10201501046)(3002001)(93006095)(93001095)(100000703101)(100105400095)(6055026)(6041248)(20161123560025)(20161123558100)(201703131423075)(201702281528075)(201703061421075)(201703061406153)(20161123562025)(20161123564025)(20161123555025)(6072148)(201708071742011)(100000704101)(100105200095)(100000705101)(100105500095);SRVR:HE1PR08MB0812;BCL:0;PCL:0;RULEID:(100000800101)(100110000095)(100000801101)(100110300095)(100000802101)(100110100095)(100000803101)(100110400095)(100000804101)(100110200095)(100000805101)(100110500095);SRVR:HE1PR08MB0812; X-Forefront-PRVS: 041032FF37 X-Forefront-Antispam-Report: SFV:NSPM;SFS:(10009020)(4630300001)(7370300001)(6009001)(39860400002)(199003)(40434004)(189002)(478600001)(68736007)(76176999)(25786009)(5660300001)(97736004)(36756003)(6486002)(50226002)(47776003)(50986999)(50466002)(4326008)(48376002)(7350300001)(110136004)(66066001)(7416002)(8676002)(189998001)(305945005)(5890100001)(6916009)(2351001)(105586002)(42186005)(6116002)(2361001)(8746002)(3846002)(86362001)(2906002)(72206003)(81156014)(81166006)(2950100002)(106356001)(6666003)(7736002)(33646002)(101416001)(5003940100001)(53936002)(573474001);DIR:OUT;SFP:1101;SCL:1;SRVR:HE1PR08MB0812;H:e106757-lin.cambridge.arm.com;FPR:;SPF:None;PTR:InfoNoRecords;A:1;MX:1;LANG:en; X-Microsoft-Exchange-Diagnostics: =?us-ascii?Q?1;HE1PR08MB0812;23:BNIxVx8BFZKkIalns1bsQG3ECQQKkfcp43VutLnib?= =?us-ascii?Q?WxXKHyNLjk19liRLB1Vx2k96UNKenbJEjpKExLWKYFKKJKMcp1vhMTZCcIEC?= =?us-ascii?Q?3dEjDJj2gvIRS1RCgTWV+iyQ/l4rtyb3XKj4+B0eKhrl2wO4NUClfASttd44?= =?us-ascii?Q?SwjzHPVYdCipSkbFOR9s1dIOMJy2B6WVbAznUDntCFceDp0uqFutiLqTg0Bp?= =?us-ascii?Q?UzpyipW7N9d4reWpwXUeHIDkbDtJSRpx1sBXPkXsLRIG/rJVFlXR4y9pzl+I?= =?us-ascii?Q?OSOnBTTHysOqhD4bSXEiyc2DxfA3Kzi3Sd/9/Ry2lqemtphX/LP4NYTceNWf?= =?us-ascii?Q?9GaOO0FFtwgsfLzd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ZHPOv5Bi/i4?= =?us-ascii?Q?rMMQlS08HhHkGSEm7Y=3D?= X-Microsoft-Exchange-Diagnostics: 1;HE1PR08MB0812;6:pcNrjynJ7gfTOFc8biCo0L9X/Smx+uHoKnhzEvV0sP3hRp6FTKjv3T5g3shgrtc7wwlyK8FjYZq6mTs00sGXZaE0Utf9q+1c0FJhVqupYu5ollMq2/Novn4kwTdQmqE1GJPgDPUT9PcSg87Hbj6vEWLpSaBWXkNr9znRGFtinLZdizo8TT/8bRbdZ0VWEYxdlu1sVurtsjqKLCSWyX/Ql491vjFr/y9nmCm63SK71qwRAYUybkHEFC0IGqehF/Ehabxf+wr6VXDZ+mUD9lnLM6Qm6DXTA2z+D2U+TGierYQdt4dHvCGfiqWZ39GEPboR4WeEpYN7qLebnRBDvUZTZw==;5:8uzB0NVdZsOx9bmr5+B3pC/wa+vgu2bsUeeUPfHaNZ3l7Y4ywd6cx4/b4o3eUKgme35vXULZRfzcf6/5t2UNykWs9sOg8c1qT4dD8+xoSTrbHJB/MAPohoDZUUweK8gpQgj5qU7ootaVs6QxaZiThA==;24:OweyOo2uy0AOsss7m4PyLdjaWeQta3Je7Rexex/JBf7Z4Iw4yQrS8S+RrYwSmv2bgrZZ17tAr9c2qqmgH5S76I3QdhBD/Es6siFCxYZQGFM=;7:zZvtZI03Ohc1+TFXd+JNbtEQXeVoBRD2RqH3EVI9NF5HY6Cx85pIahLDtqDwpF3RX0W9OFj6mPKvPtbExDnuykuRqrOONR0tAMWA4TRJ0zk+XYlyLsT9OtyQ8Ov6q174UaJHBBaBF60j9q8/PlQyEvNlmkfaiqSJAroHItphh/mS+AKBJGIfRWJpkBtuubJaeyqQTsgICYlh2lbPW60eEw+HdJBBfH1Ra2gks0ZAwS0= SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-OriginatorOrg: arm.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 25 Aug 2017 08:32:16.0057 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-Transport-CrossTenantHeadersStamped: HE1PR08MB0812 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: 8bit X-MIME-Autoconverted: from quoted-printable to 8bit by nfs id v7P8bSVN010031 Content-Length: 6624 Lines: 187 The current codebase of KVM makes many assumptions regarding the origin of the virtual machine being executed or configured. Indeed, the KVM API implementation has been written with userspace usage in mind and lots of userspace-specific code is used (namely preempt_notifiers, eventfd, mmu notifiers, current->mm...) The aim of this patch is to make the KVM API (create_vm, create_vcpu etc) usable from a kernel context. A simple trick is used to distinguish userspace VMs (coming from QEMU or LKVM...) from internal VMs. (coming from other subsystems, for example for sandboxing purpose): - When a VM is created from an ioctl, kvm->mm is set to current->mm - When a VM is created from the kernel, kvm->mm must be set to NULL This ensures that no userspace program can create internal VMs and allows to easily check whether a given VM is attached to a process or is internal. This patch simply encloses the userspace-specific pieces of code of kvm_main in conditions checking if kvm->mm is present and modifies the prototype of kvm_create_vm to enable NULL mm. Signed-off-by: Florent Revest --- virt/kvm/kvm_main.c | 64 ++++++++++++++++++++++++++++++++++------------------- 1 file changed, 41 insertions(+), 23 deletions(-) diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 15252d7..2e7af1a 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -154,7 +154,8 @@ int vcpu_load(struct kvm_vcpu *vcpu) if (mutex_lock_killable(&vcpu->mutex)) return -EINTR; cpu = get_cpu(); - preempt_notifier_register(&vcpu->preempt_notifier); + if (vcpu->kvm->mm) + preempt_notifier_register(&vcpu->preempt_notifier); kvm_arch_vcpu_load(vcpu, cpu); put_cpu(); return 0; @@ -165,7 +166,8 @@ void vcpu_put(struct kvm_vcpu *vcpu) { preempt_disable(); kvm_arch_vcpu_put(vcpu); - preempt_notifier_unregister(&vcpu->preempt_notifier); + if (vcpu->kvm->mm) + preempt_notifier_unregister(&vcpu->preempt_notifier); preempt_enable(); mutex_unlock(&vcpu->mutex); } @@ -640,7 +642,7 @@ static int kvm_create_vm_debugfs(struct kvm *kvm, int fd) return 0; } -static struct kvm *kvm_create_vm(unsigned long type) +static struct kvm *kvm_create_vm(unsigned long type, struct mm_struct *mm) { int r, i; struct kvm *kvm = kvm_arch_alloc_vm(); @@ -649,9 +651,11 @@ static struct kvm *kvm_create_vm(unsigned long type) return ERR_PTR(-ENOMEM); spin_lock_init(&kvm->mmu_lock); - mmgrab(current->mm); - kvm->mm = current->mm; - kvm_eventfd_init(kvm); + kvm->mm = mm; + if (mm) { + mmgrab(current->mm); + kvm_eventfd_init(kvm); + } mutex_init(&kvm->lock); mutex_init(&kvm->irq_lock); mutex_init(&kvm->slots_lock); @@ -697,15 +701,18 @@ static struct kvm *kvm_create_vm(unsigned long type) goto out_err; } - r = kvm_init_mmu_notifier(kvm); - if (r) - goto out_err; + if (mm) { + r = kvm_init_mmu_notifier(kvm); + if (r) + goto out_err; + } spin_lock(&kvm_lock); list_add(&kvm->vm_list, &vm_list); spin_unlock(&kvm_lock); - preempt_notifier_inc(); + if (mm) + preempt_notifier_inc(); return kvm; @@ -721,7 +728,8 @@ static struct kvm *kvm_create_vm(unsigned long type) for (i = 0; i < KVM_ADDRESS_SPACE_NUM; i++) kvm_free_memslots(kvm, __kvm_memslots(kvm, i)); kvm_arch_free_vm(kvm); - mmdrop(current->mm); + if (mm) + mmdrop(mm); return ERR_PTR(r); } @@ -772,9 +780,11 @@ static void kvm_destroy_vm(struct kvm *kvm) cleanup_srcu_struct(&kvm->irq_srcu); cleanup_srcu_struct(&kvm->srcu); kvm_arch_free_vm(kvm); - preempt_notifier_dec(); + if (mm) + preempt_notifier_dec(); hardware_disable_all(); - mmdrop(mm); + if (mm) + mmdrop(mm); } void kvm_get_kvm(struct kvm *kvm) @@ -1269,6 +1279,9 @@ unsigned long kvm_host_page_size(struct kvm *kvm, gfn_t gfn) if (kvm_is_error_hva(addr)) return PAGE_SIZE; + if (!kvm->mm) + return PAGE_SIZE; + down_read(¤t->mm->mmap_sem); vma = find_vma(current->mm, addr); if (!vma) @@ -2486,9 +2499,11 @@ static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, u32 id) if (r) goto vcpu_destroy; - r = kvm_create_vcpu_debugfs(vcpu); - if (r) - goto vcpu_destroy; + if (kvm->mm) { + r = kvm_create_vcpu_debugfs(vcpu); + if (r) + goto vcpu_destroy; + } mutex_lock(&kvm->lock); if (kvm_get_vcpu_by_id(kvm, id)) { @@ -2499,11 +2514,13 @@ static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, u32 id) BUG_ON(kvm->vcpus[atomic_read(&kvm->online_vcpus)]); /* Now it's all set up, let userspace reach it */ - kvm_get_kvm(kvm); - r = create_vcpu_fd(vcpu); - if (r < 0) { - kvm_put_kvm(kvm); - goto unlock_vcpu_destroy; + if (kvm->mm) { + kvm_get_kvm(kvm); + r = create_vcpu_fd(vcpu); + if (r < 0) { + kvm_put_kvm(kvm); + goto unlock_vcpu_destroy; + } } kvm->vcpus[atomic_read(&kvm->online_vcpus)] = vcpu; @@ -2521,7 +2538,8 @@ static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, u32 id) unlock_vcpu_destroy: mutex_unlock(&kvm->lock); - debugfs_remove_recursive(vcpu->debugfs_dentry); + if (kvm->mm) + debugfs_remove_recursive(vcpu->debugfs_dentry); vcpu_destroy: kvm_arch_vcpu_destroy(vcpu); vcpu_decrement: @@ -3191,7 +3209,7 @@ static int kvm_dev_ioctl_create_vm(unsigned long type) struct kvm *kvm; struct file *file; - kvm = kvm_create_vm(type); + kvm = kvm_create_vm(type, current->mm); if (IS_ERR(kvm)) return PTR_ERR(kvm); #ifdef CONFIG_KVM_MMIO -- 1.9.1 IMPORTANT NOTICE: The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you.