Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp3123510rwb; Wed, 30 Nov 2022 15:56:48 -0800 (PST) X-Google-Smtp-Source: AA0mqf47bUHzt2NRxr/0LzCMKIYUSLgNGKe4U5AaF6EaW4pUYgipke1XTbF9u+bBHaoMb3xbf/dN X-Received: by 2002:a17:907:a688:b0:7ba:ba67:f2f with SMTP id vv8-20020a170907a68800b007baba670f2fmr29268993ejc.199.1669852608368; Wed, 30 Nov 2022 15:56:48 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1669852608; cv=none; d=google.com; s=arc-20160816; b=D+XiPc65e23SJ1SVCoMQF8r0LmmQkUFAZncWIScBFN9uUtlp9ladf26wbWjUsBRWoW sL67y+mwNBnCsrZEPN9UnNVRb0tHii/kjwDVbj1ZWDGlZIjXnpw2VltUjb33nSs4EfWq MVj7qTA4luFGF3TR24bYhDe0caMZoKN5EjcdKG7FU51Q79SQnsgX+0uwg13IERNT7SE2 QfOKehnbSZptbsamdOtDRyaWDCqw8UcxEMyKLj5rYU2f1F1G3W+U9DvJ+3wX7Zsv2+X5 xs/GTenrbcuTRmfty8ApSG6DosBxpwbRsqHs/gYm9P3o7IB1URgnvMeJe0EBzV0YQVOp RGzA== 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=di4GPfaF6WKRfgW0XqJub+upfuRM26VzAsGaqEV4LGw=; b=h/jHf7/lqu7NhGWXM0+U4vERD/RkbFsYRwvnQj8PqtkKZBV8fwwHBz/osWi/0om8So voo+hgHms1O5fKAJGqTb+AVDE329gaef5GwyIbWDJO7KvtSKa42545m/V8PhBuesFGuY LB4rO2MGUZ/mdQs22esnwwXzbwZCcv55iOAEoaS6Du/CWKph9sWiHJGKM9h6GhNuD44F wKsskyFxF13UZedVZ+puAUZXk1yOQyfzI4T84/6zMiONhJcOIJ4BsOV+JUqPXsFfOw6t /NgdHn3Pq6q0Bc79say5RjanHPgSnxQ0bHyQ6zfXN6dfQz9uaiGLUOr9WxbUP0QEqzLb pTMQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=Zp0X+tvq; 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 nb38-20020a1709071ca600b00791a67e4c00si2783939ejc.835.2022.11.30.15.56.28; Wed, 30 Nov 2022 15:56:48 -0800 (PST) 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=Zp0X+tvq; 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 S229853AbiK3XMZ (ORCPT + 83 others); Wed, 30 Nov 2022 18:12:25 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47664 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229875AbiK3XLH (ORCPT ); Wed, 30 Nov 2022 18:11:07 -0500 Received: from mail-pf1-x44a.google.com (mail-pf1-x44a.google.com [IPv6:2607:f8b0:4864:20::44a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D6244975D3 for ; Wed, 30 Nov 2022 15:10:16 -0800 (PST) Received: by mail-pf1-x44a.google.com with SMTP id g13-20020a056a000b8d00b0056e28b15757so210131pfj.1 for ; Wed, 30 Nov 2022 15:10:16 -0800 (PST) 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=di4GPfaF6WKRfgW0XqJub+upfuRM26VzAsGaqEV4LGw=; b=Zp0X+tvqlodbVDmXJAlHsgiG3eKVrrt9FqJ/G91Ka+FyaPGocW43mHdutS/PZpQE36 Iieu9/BWXmUBsCNeoXw2w6AGd8PrG42NkgB6lU08wcdcc8UwAyPwoD57C6YJX6W8d8L6 zT+J1YBLZIf8EiLqUI0I1OI8MzvM21Wx5gQBxNbTBtyOAXzto+GobOper+U00pyFgXJf UdMZA+TjdcslZ9rB8fGS/EHatJp3upDo3FA+0llGAcT+1sttGOmsewFGvBZuTST5TDKg atyPT5DCNi18z7jSv+tnMdXu5HV1aXplTTP+Szmkxtce4LN+raVvUsuQA85wOwn+8uqH psnA== 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=di4GPfaF6WKRfgW0XqJub+upfuRM26VzAsGaqEV4LGw=; b=RgaHx0F/Q5dBO+MZCht06PIB0a0XkuvOVbwkT/TWreM86S2kkY1djotWL7FIxlJ7ZT oIShqhVt2WkdX3U1dR+cpzVj1Q4Qod9kDOfmVSuUr6ixuox2ko91/jY353JCHZQgCkKg XRLrGLmMAgyYNHOnJGBRvvg1kEYf1PV12cDsdxjj5Gap/O/FXTFoDSDW4O7pIg2aLsaV AOpvtzNrauspJ7y3oWv8pmVuFr6a8e2BZtyTQnn0d3C3rNCj1m3xV75G1z+242M77/+S 7PT7q44SPok9mfCdSEffRQ7qfiQy/g5ajoErkbOJPwrSOx65OcmE0txgd1l8PugwEFyV mOKw== X-Gm-Message-State: ANoB5pnpbEj5NEkF9FVEZJP9Q+JdCSQSr/odHVfqCqh5qEIBndAu9+bF ssy0WmSEAVyFbon3A7lKSQ3yn0zQ4FI= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6a00:1409:b0:56b:e1d8:e7a1 with SMTP id l9-20020a056a00140900b0056be1d8e7a1mr44197245pfu.28.1669849801157; Wed, 30 Nov 2022 15:10:01 -0800 (PST) Reply-To: Sean Christopherson Date: Wed, 30 Nov 2022 23:08:58 +0000 In-Reply-To: <20221130230934.1014142-1-seanjc@google.com> Mime-Version: 1.0 References: <20221130230934.1014142-1-seanjc@google.com> X-Mailer: git-send-email 2.38.1.584.g0f3c55d4c2-goog Message-ID: <20221130230934.1014142-15-seanjc@google.com> Subject: [PATCH v2 14/50] 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 , David Woodhouse , Paul Durrant 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, Yuan Yao , Cornelia Huck , Isaku Yamahata , "=?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?=" , Fabiano Rosas , Michael Ellerman , Kai Huang , Chao Gao , Thomas Gleixner 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=unavailable 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 | 34 +++++++++++++++++++++------------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index 8e81cd94407d..76185a7a7ded 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -8521,19 +8521,23 @@ 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(); - vmx_cleanup_l1d_flush(); +} - allow_smaller_maxphyaddr = false; +static void vmx_exit(void) +{ + kvm_exit(); + kvm_x86_vendor_exit(); + + __vmx_exit(); } module_exit(vmx_exit); @@ -8551,11 +8555,6 @@ static int __init vmx_init(void) if (r) return r; - 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 @@ -8589,11 +8588,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(); return r; } -- 2.38.1.584.g0f3c55d4c2-goog