From patchwork Wed Nov 2 23:18:38 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 14531 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp199942wru; Wed, 2 Nov 2022 16:22:36 -0700 (PDT) X-Google-Smtp-Source: AMsMyM7wvlOggeQyyk8zlrz1R5ZrJaKEP0tRAhdywUhUQsm9xU0Eok0EvfztOObz++1/Z+ZpcA9Q X-Received: by 2002:a17:902:8215:b0:178:6946:a282 with SMTP id x21-20020a170902821500b001786946a282mr27363788pln.162.1667431356116; Wed, 02 Nov 2022 16:22:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1667431356; cv=none; d=google.com; s=arc-20160816; b=eUT0UJJIJw77nAJJ2gbbtzZ0QGTAJlRDkGjXqWzOU6a5Q6XHPF3qUP6Or1IKEcW2cg m1UpqhaxbS7bARtr+/X3RxTTR0pvrtmwW4eK4yJrIRbpUPyVt97omWxosuOMztbt7Tui Q7ssFkroeGkoyjPWcCc55s0LPuvEhp5uFokgh7IXRwuxDjdEL6ggNtrOGl+/GWIznd33 MdG/KhDMTbFVAUY9ciEfznc4X9qWX6IdWMLOhV5OW1UpKy+oPTE16KWMVTEoFd8BJF75 aeS54R+sAM6L0J3e7Phjf10wZabdufIkdmUwShcbnM8phb0B2aFlgVrdVnc1OckOq8Uv 09AQ== 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=hsvwPn56EufMwWrr4BQ3wXsJaNpmOPQxZWWpLewxvhQ=; b=YM1is1UTHFCBx/rbrZBNq66JaNL5PtBvdBiRfsVhufAfFha2tDDqchwzTzL3logm9D rWNZoyOwsOZ0J44StV2iuJBMXs2IPbZ6U7AF3Nm1fkJ/7YQMV6ZSS2vEd+RX3VECe/Uq jmdGuRmYAj+sf7It5DSQ1IHG/YcH8n5YsJiQIaCOJIxSksGcdZyzSkNKBO5xl1hd4/MZ p8O7sG6D/+w5iNp41hXsZCCxT/VZL6lC6yvtRGSEjmpDX6nL0u6jKlC6EqqfPo1FZpbx eMwSMRTP2t32W6PmjaBafslkCNHTTLf9XHkmm2bHXqRtiCnOQMK8mDDb7Vl6oERp0Z4P wLfA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=Y+WCubtq; 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: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id a5-20020a63e845000000b0046f3717229fsi19022111pgk.302.2022.11.02.16.22.23; Wed, 02 Nov 2022 16:22:36 -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=Y+WCubtq; 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 S230306AbiKBXUr (ORCPT + 99 others); Wed, 2 Nov 2022 19:20:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:32862 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230516AbiKBXUG (ORCPT ); Wed, 2 Nov 2022 19:20:06 -0400 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 173F826EF for ; Wed, 2 Nov 2022 16:19:33 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id l188-20020a2525c5000000b006cbcff0ab41so394468ybl.4 for ; Wed, 02 Nov 2022 16:19:33 -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=hsvwPn56EufMwWrr4BQ3wXsJaNpmOPQxZWWpLewxvhQ=; b=Y+WCubtqC448k8BS6Mqnaj28SHAW+AQfgFNGCLm2bJu3u1mJtG3sPN/6hEm3u5WKxX gjm0xAmdiy0BNLmCTgESqNLWBkKXwZKx8x3UovP6tMr0tuTqm6uAbtXaFSg2tQ6IY8UY VXa2F9flrbUexIM+erBgHE9ssHvFwc+slKWkiJrFDAVzrbJZgmqNXlASKZWSF88G1RsF 3WXrtRdOPomKp62p5k4CbApbNG6yrgaBuDlDbRhFN/CbQ2b9hIJBRdTCZXqQmNDbMEE4 ZnCQdPnQlkYKD1izSyGfMR40m61f2ixRTRCzDV7N9xc1b7MsAKZPHOhhZhsGAWMnpSoY ov8A== 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=hsvwPn56EufMwWrr4BQ3wXsJaNpmOPQxZWWpLewxvhQ=; b=VrV//P2Bt821nCjrGPiMalgbS8Fw+v/SQPesXHsoksZ1PvYK9Axi5zktVE6FFTosPY 459/5tvZM31nnHoLyWA6wpU+ki3Tj6DzlPsJeJ00pWpLUn6Z/Eys2+hhYxqnZOX/AwWP sTtaMV8jIMWsGfspQtATGEaAtBz7MdMT19Of0z0kv0ae0Tc2LqVL4sCNCzpmE2Op9FEd eGJ3uKhnbAICMJXLXBjigqfg+T9hyfErLvLTLcmjnLti6e4PD1lGDLXyh4ne4LV42A2A e+LeME6Ba5y4PFFovWgCdQ1oFpJocnfg0rPqL1w7hLLqLYYkQJeMM9sLonz5pHkbTIkV fo9w== X-Gm-Message-State: ACrzQf03B2ItC8Mr7oIitEagttrQALtT3GAaMLPFVKno770C1vGKYDJh dfMi5tnvJ8xdlBuSV+zxlqYDK2oxupA= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a25:550:0:b0:6ca:5991:1abf with SMTP id 77-20020a250550000000b006ca59911abfmr26235720ybf.371.1667431172753; Wed, 02 Nov 2022 16:19:32 -0700 (PDT) Reply-To: Sean Christopherson Date: Wed, 2 Nov 2022 23:18:38 +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-12-seanjc@google.com> Subject: [PATCH 11/44] KVM: x86: Move guts of kvm_arch_init() to standalone helper 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 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1748428501832748123?= X-GMAIL-MSGID: =?utf-8?q?1748428501832748123?= Move the guts of kvm_arch_init() to a new helper, kvm_x86_vendor_init(), so that VMX can do _all_ arch and vendor initialization before calling kvm_init(). Calling kvm_init() must be the _very_ last step during init, as kvm_init() exposes /dev/kvm to userspace, i.e. allows creating VMs. No functional change intended. Signed-off-by: Sean Christopherson --- arch/x86/include/asm/kvm_host.h | 3 +++ arch/x86/kvm/svm/svm.c | 23 +++++++++++++++++++++-- arch/x86/kvm/vmx/vmx.c | 9 ++++++++- arch/x86/kvm/x86.c | 15 +++++++++++++-- 4 files changed, 45 insertions(+), 5 deletions(-) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index 415113dea951..58a7cb8d8e96 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1695,6 +1695,9 @@ extern struct kvm_x86_ops kvm_x86_ops; #define KVM_X86_OP_OPTIONAL_RET0 KVM_X86_OP #include +int kvm_x86_vendor_init(struct kvm_x86_init_ops *ops); +void kvm_x86_vendor_exit(void); + #define __KVM_HAVE_ARCH_VM_ALLOC static inline struct kvm *kvm_arch_alloc_vm(void) { diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index d22a809d9233..f48d07bfc3d7 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -5132,15 +5132,34 @@ static struct kvm_x86_init_ops svm_init_ops __initdata = { static int __init svm_init(void) { + int r; + __unused_size_checks(); - return kvm_init(&svm_init_ops, sizeof(struct vcpu_svm), - __alignof__(struct vcpu_svm), THIS_MODULE); + r = kvm_x86_vendor_init(&svm_init_ops); + if (r) + return r; + + /* + * Common KVM initialization _must_ come last, after this, /dev/kvm is + * exposed to userspace! + */ + r = kvm_init(&svm_init_ops, sizeof(struct vcpu_svm), + __alignof__(struct vcpu_svm), THIS_MODULE); + if (r) + goto err_kvm_init; + + return 0; + +err_kvm_init: + kvm_x86_vendor_exit(); + return r; } static void __exit svm_exit(void) { kvm_exit(); + kvm_x86_vendor_exit(); } module_init(svm_init) diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index b3fd4049de01..118d9b29b339 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -8501,6 +8501,7 @@ static void vmx_exit(void) #endif kvm_exit(); + kvm_x86_vendor_exit(); hv_cleanup_evmcs(); @@ -8516,13 +8517,17 @@ static int __init vmx_init(void) hv_setup_evmcs(); + r = kvm_x86_vendor_init(&vmx_init_ops); + 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 kvm_init() so enable_ept is properly set + * Must be called after common x86 init so enable_ept is properly set * up. Hand the parameter mitigation value in which was stored in * the pre module init parser. If no parameter was given, it will * contain 'auto' which will be turned into the default 'cond' @@ -8559,6 +8564,8 @@ static int __init vmx_init(void) err_l1d_flush: vmx_exit(); err_kvm_init: + kvm_x86_vendor_exit(); +err_x86_init: hv_cleanup_evmcs(); return r; } diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 40d4bfaa17a4..a0ca401d3cdf 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -9272,7 +9272,16 @@ static inline void kvm_ops_update(struct kvm_x86_init_ops *ops) int kvm_arch_init(void *opaque) { - struct kvm_x86_init_ops *ops = opaque; + return 0; +} + +void kvm_arch_exit(void) +{ + +} + +int kvm_x86_vendor_init(struct kvm_x86_init_ops *ops) +{ u64 host_pat; int r; @@ -9404,8 +9413,9 @@ int kvm_arch_init(void *opaque) kmem_cache_destroy(x86_emulator_cache); return r; } +EXPORT_SYMBOL_GPL(kvm_x86_vendor_init); -void kvm_arch_exit(void) +void kvm_x86_vendor_exit(void) { kvm_unregister_perf_callbacks(); @@ -9434,6 +9444,7 @@ void kvm_arch_exit(void) WARN_ON(static_branch_unlikely(&kvm_xen_enabled.key)); #endif } +EXPORT_SYMBOL_GPL(kvm_x86_vendor_exit); static int __kvm_emulate_halt(struct kvm_vcpu *vcpu, int state, int reason) {