Received: by 2002:a05:6358:111d:b0:dc:6189:e246 with SMTP id f29csp652856rwi; Wed, 2 Nov 2022 16:46:32 -0700 (PDT) X-Google-Smtp-Source: AMsMyM4Y3XJyI8Tsn+aZHS5etrWEW5K0XV2//jCoYuloF/NQ3/9fHZ27EzAHHW1W/DFlS3nqYpvw X-Received: by 2002:a17:907:86a8:b0:7ad:988e:a93 with SMTP id qa40-20020a17090786a800b007ad988e0a93mr27654867ejc.704.1667432792587; Wed, 02 Nov 2022 16:46:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1667432792; cv=none; d=google.com; s=arc-20160816; b=cPeVLEk5pbdY1HNkO3h0bpdseXI/6yQNeF/iHwJVrCH54Bz4VdMHspVa0akV8ff/sz SVuOUU3x0o4xW7zh4/kKJ2UVwxtjdALOeC/oPPTVAYLdFod5xeFz0TACdlx/eJYik0E2 g2v3YTrZiTFY+hnYWnFBXUe5PMOS32+PFnsnxuHTMrx/O7owv5toEjGg5fSqDw/nwlS+ YwbZaF/77nJyntR7go8JrPwGYUpZgeGdnk9Iqn7tWOkIzmY35y9MS+aza5cneqFUxfWR CzpgK7IkJIv5hMVS34g1IFSES3sqTPrvX/UlHLZpVRo9BPsUSXLc1nXEEYeeEqs2Et84 6LEw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:reply-to:dkim-signature; bh=hS5bVpzKUqQDTwIOc5biqhWk5bOmOk+1edZQlwf3ubM=; b=G3BTcKQeAQ0riIoj6yTz52O/h5cr5vS1oP2zZ0U0++lXRY9ftibA2UtOGi0w+PXziu Nxm20QZyzjWKS/8dwQjFsRynJXpJWZJ6yOV2aLCUxf9B554vC7xDsiIY901ahT2QUTmw 7T7P6aCAPDNRXxH/dnh5zhgog4JIndQi/aLSHXx5npf7tb7MXxn5wMYvNjDln+XLE88N PeFOgQE1IkDth2G+RYCeFrevw0yKzX8B2I/ZOslTJ0CmgnGWwjUagxbN/EAwAHAT2kv4 /Quh2kA15xqebO2Xiqef/9Dy82IFskol4wY5kRDKa9Qyc/AzzSf7/+btKW9EJmU5eR6e aW0g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=szPHLkhF; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id g3-20020a1709065d0300b007ade82b9333si9932070ejt.43.2022.11.02.16.45.53; Wed, 02 Nov 2022 16:46:32 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=szPHLkhF; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230495AbiKBXVD (ORCPT + 97 others); Wed, 2 Nov 2022 19:21:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33896 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231143AbiKBXUI (ORCPT ); Wed, 2 Nov 2022 19:20:08 -0400 Received: from mail-yw1-x114a.google.com (mail-yw1-x114a.google.com [IPv6:2607:f8b0:4864:20::114a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6B8482723 for ; Wed, 2 Nov 2022 16:19:35 -0700 (PDT) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-352e29ff8c2so493737b3.21 for ; Wed, 02 Nov 2022 16:19:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=hS5bVpzKUqQDTwIOc5biqhWk5bOmOk+1edZQlwf3ubM=; b=szPHLkhFGnRo6kaiXnFdGUAqKFDyeLG3Vql25w1uL10gFIqtapkpUXMYdCQdli+eqs cbcj9hSkNeNK0P7+t1BZFEIlTNuiaIp2mlW4xEBL0WtOOQIpQ9D+xxA8r7FTE3/hx8Ou 8DfXEw5LuqLAgTGoPS8P3pICN4y6IJD/Snp87sY45+PuVlEkpSraOhTndfuv/3GEK8T+ hn9gmRaXJDkisGpZk2f61kkZWmIpTFHWF2nSKiSU6rb6VwxKCNie2hpdgPJEzJyQa//E mMEwJkz2TFE7q1HlBlgTUEk3XIDzTUi+TWq+4KEAGQJ1QDIj47GdP3s/hYuMvyLPXFzC fAOA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=hS5bVpzKUqQDTwIOc5biqhWk5bOmOk+1edZQlwf3ubM=; b=K4N4F9ri1hrGlMzuMEeBxvxpL/EJ3amRAg0i5/Y5IsOJfTv6WK2qY2yZJA/Kb2z4LB Sb8RfuetKYKdlytw4y9pqV9V1PmIoFS3vf2AFDNBuYe+3nIBoxjSESRzTIYUmAOR7E2R DMmeAg0INahL0Lzl12zdSgDLOCq+Q4M2rCqIAIyHB59WKV/jhGku2g4ddiUhNY3/dhkZ 0/JUOTzTPP0z0cRkBPEjntb6H7Fr1g06M1XFJv8xfylgGxfnYoGxsvRzN8Mks2YJLhD1 pNWfeeF0dRG7Wjdfj1uNIElneUa8O0dAfZZGvETDRzvFGAQWvicYmDStVqgZ8Fh429vF t/IQ== X-Gm-Message-State: ACrzQf0vRYuki39smgV00AzSuggR5HH+Rdt+KtKbU9J2OPJH9MMG9rBB 2HQ9+3wUaq4Z+/ipWnwR0gPxfjwlqbs= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6902:705:b0:6ca:a3b0:e7f with SMTP id k5-20020a056902070500b006caa3b00e7fmr26597392ybt.373.1667431174399; Wed, 02 Nov 2022 16:19:34 -0700 (PDT) Reply-To: Sean Christopherson Date: Wed, 2 Nov 2022 23:18:39 +0000 In-Reply-To: <20221102231911.3107438-1-seanjc@google.com> Mime-Version: 1.0 References: <20221102231911.3107438-1-seanjc@google.com> X-Mailer: git-send-email 2.38.1.431.g37b22c650d-goog Message-ID: <20221102231911.3107438-13-seanjc@google.com> Subject: [PATCH 12/44] KVM: VMX: Do _all_ initialization before exposing /dev/kvm to userspace From: Sean Christopherson To: Paolo Bonzini , Marc Zyngier , Huacai Chen , Aleksandar Markovic , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , Matthew Rosato , Eric Farman , Sean Christopherson , Vitaly Kuznetsov Cc: James Morse , Alexandru Elisei , Suzuki K Poulose , Oliver Upton , Atish Patra , David Hildenbrand , kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, kvmarm@lists.cs.columbia.edu, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-s390@vger.kernel.org, linux-kernel@vger.kernel.org, Isaku Yamahata , Fabiano Rosas , Michael Ellerman , Chao Gao , Thomas Gleixner , Yuan Yao Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Call kvm_init() only after _all_ setup is complete, as kvm_init() exposes /dev/kvm to userspace and thus allows userspace to create VMs (and call other ioctls). E.g. KVM will encounter a NULL pointer when attempting to add a vCPU to the per-CPU loaded_vmcss_on_cpu list if userspace is able to create a VM before vmx_init() configures said list. BUG: kernel NULL pointer dereference, address: 0000000000000008 #PF: supervisor write access in kernel mode #PF: error_code(0x0002) - not-present page PGD 0 P4D 0 Oops: 0002 [#1] SMP CPU: 6 PID: 1143 Comm: stable Not tainted 6.0.0-rc7+ #988 Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS 0.0.0 02/06/2015 RIP: 0010:vmx_vcpu_load_vmcs+0x68/0x230 [kvm_intel] vmx_vcpu_load+0x16/0x60 [kvm_intel] kvm_arch_vcpu_load+0x32/0x1f0 [kvm] vcpu_load+0x2f/0x40 [kvm] kvm_arch_vcpu_create+0x231/0x310 [kvm] kvm_vm_ioctl+0x79f/0xe10 [kvm] ? handle_mm_fault+0xb1/0x220 __x64_sys_ioctl+0x80/0xb0 do_syscall_64+0x2b/0x50 entry_SYSCALL_64_after_hwframe+0x46/0xb0 RIP: 0033:0x7f5a6b05743b Modules linked in: vhost_net vhost vhost_iotlb tap kvm_intel(+) kvm irqbypass Cc: stable@vger.kernel.org Signed-off-by: Sean Christopherson --- arch/x86/kvm/vmx/vmx.c | 38 +++++++++++++++++++++++--------------- 1 file changed, 23 insertions(+), 15 deletions(-) diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index 118d9b29b339..6adb60485839 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -8493,21 +8493,25 @@ static void vmx_cleanup_l1d_flush(void) l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_AUTO; } -static void vmx_exit(void) +static void __vmx_exit(void) { + allow_smaller_maxphyaddr = false; + #ifdef CONFIG_KEXEC_CORE RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL); synchronize_rcu(); #endif - - kvm_exit(); - kvm_x86_vendor_exit(); - - hv_cleanup_evmcs(); - vmx_cleanup_l1d_flush(); +} - allow_smaller_maxphyaddr = false; +static void vmx_exit(void) +{ + kvm_exit(); + kvm_x86_vendor_exit(); + + __vmx_exit(); + + hv_cleanup_evmcs(); } module_exit(vmx_exit); @@ -8521,11 +8525,6 @@ static int __init vmx_init(void) if (r) goto err_x86_init; - r = kvm_init(&vmx_init_ops, sizeof(struct vcpu_vmx), - __alignof__(struct vcpu_vmx), THIS_MODULE); - if (r) - goto err_kvm_init; - /* * Must be called after common x86 init so enable_ept is properly set * up. Hand the parameter mitigation value in which was stored in @@ -8559,11 +8558,20 @@ static int __init vmx_init(void) if (!enable_ept) allow_smaller_maxphyaddr = true; + /* + * Common KVM initialization _must_ come last, after this, /dev/kvm is + * exposed to userspace! + */ + r = kvm_init(&vmx_init_ops, sizeof(struct vcpu_vmx), + __alignof__(struct vcpu_vmx), THIS_MODULE); + if (r) + goto err_kvm_init; + return 0; -err_l1d_flush: - vmx_exit(); err_kvm_init: + __vmx_exit(); +err_l1d_flush: kvm_x86_vendor_exit(); err_x86_init: hv_cleanup_evmcs(); -- 2.38.1.431.g37b22c650d-goog