From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1A1BEC433FE for ; Fri, 8 Apr 2022 16:46:17 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233602AbiDHQsT (ORCPT ); Fri, 8 Apr 2022 12:48:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44452 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233541AbiDHQsO (ORCPT ); Fri, 8 Apr 2022 12:48:14 -0400 Received: from mail-pl1-x631.google.com (mail-pl1-x631.google.com [IPv6:2607:f8b0:4864:20::631]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6B20D26124 for ; Fri, 8 Apr 2022 09:46:10 -0700 (PDT) Received: by mail-pl1-x631.google.com with SMTP id y6so8385158plg.2 for ; Fri, 08 Apr 2022 09:46:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=KjGzlPUuopnzOxDb70wPeYUUzIj9x1BrlBL7vAL3tEU=; b=nMUAsD6DVHxasPy+bHqB5yAyTxDamuiDmy7FuYy+5YCzQjXg5KrVCglUnu2RwZ7wDO VgUgFHdgmXbomd+tDzupYzwVUMcDOVfbohBWX9cbf33EaAI/OEasRPiFIV29b9A/408p 2jRsSnMB7pf4mlzcGNKqGnI3PhClL5e7SL0+4o1nYt+weKwj+NG7sKlMDs81oppC4S/E k0camdZClD+mxlZhX4kfxSCiK2oUXQjXyYylESC/QLV8+g+NBBGzKbRkl11spdr/Vv6s DQuKmHtidbNWwSr/KLU7DGMxB64gp1TbS36mA++3AuZGeCdYhVDfkX7yS5eSKLmWgwbG spmQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=KjGzlPUuopnzOxDb70wPeYUUzIj9x1BrlBL7vAL3tEU=; b=aXrx+naE6I9oy9XvY9vpDkcodZHWhjqnpy+z5zn0ETvAfepJCMU4/8exQ9ftGCkPyg Ybo6RkAopC9s5Iu93JowNlkIb4MknF8Ri60fPSy2x9EvKVUV2TxCVq4aYwljEMtGx6PE ISkjicF+77efXUyZNyeYCxpaxwgVjIhEpsjC+HlrP2EiNntsgzsL+CBsDC60jhK8kBJD BS1f0ykiMm3WXOB7mZk+3Fy4S/j/bLjKNNmtLTSWsl9JOYN99IWqIMLIl1DHQWUIH1cu A/M4+CkXQWWGj0YH6Ugvs6GAIlNEM7WP4Z6X/KCNYfvVI/nvYO8EHCVto8tVZWOcr2Nk qTCw== X-Gm-Message-State: AOAM531xizahrpM+LOykKKfTe2zzTAjbBTHHOPsTbe/JhhK6UVVZbS27 p5nHHCaETKAHQd3dd/9QiQ2nbw== X-Google-Smtp-Source: ABdhPJz0iwytqOS32LIa00b7FHRew+z1BX9zsSQNqylpLVDQVJoWyNnMedQm0cD5GiqNLfHfoAXT5Q== X-Received: by 2002:a17:902:f649:b0:156:1609:79e9 with SMTP id m9-20020a170902f64900b00156160979e9mr20434203plg.69.1649436369344; Fri, 08 Apr 2022 09:46:09 -0700 (PDT) Received: from google.com (157.214.185.35.bc.googleusercontent.com. [35.185.214.157]) by smtp.gmail.com with ESMTPSA id d16-20020a056a00245000b004f771b48736sm28031213pfj.194.2022.04.08.09.46.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 08 Apr 2022 09:46:08 -0700 (PDT) Date: Fri, 8 Apr 2022 16:46:04 +0000 From: Sean Christopherson To: isaku.yamahata@intel.com Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, isaku.yamahata@gmail.com, Paolo Bonzini , Jim Mattson , erdemaktas@google.com, Connor Kuehl Subject: Re: [RFC PATCH v5 003/104] KVM: TDX: Detect CPU feature on kernel module initialization Message-ID: References: <70201fd686c6cc6e03f5af8a9f59af67bdc81194.1646422845.git.isaku.yamahata@intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <70201fd686c6cc6e03f5af8a9f59af67bdc81194.1646422845.git.isaku.yamahata@intel.com> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Mar 04, 2022, isaku.yamahata@intel.com wrote: > diff --git a/arch/x86/kvm/vmx/tdx.c b/arch/x86/kvm/vmx/tdx.c > new file mode 100644 > index 000000000000..1acf08c310c4 > --- /dev/null > +++ b/arch/x86/kvm/vmx/tdx.c > @@ -0,0 +1,53 @@ > +// SPDX-License-Identifier: GPL-2.0 > +#include > + > +#include > + > +#include "capabilities.h" > +#include "x86_ops.h" > + > +#undef pr_fmt > +#define pr_fmt(fmt) "tdx: " fmt > + > +static bool __read_mostly enable_tdx = true; > +module_param_named(tdx, enable_tdx, bool, 0644); This is comically unsafe, userspace must not be allowed to toggle enable_tdx after KVM is loaded. > +static u64 hkid_mask __ro_after_init; > +static u8 hkid_start_pos __ro_after_init; > + > +static int __init __tdx_hardware_setup(struct kvm_x86_ops *x86_ops) > +{ > + u32 max_pa; > + > + if (!enable_ept) { > + pr_warn("Cannot enable TDX with EPT disabled\n"); > + return -EINVAL; > + } > + > + if (!platform_has_tdx()) { > + pr_warn("Cannot enable TDX with SEAMRR disabled\n"); > + return -ENODEV; > + } > + > + if (WARN_ON_ONCE(x86_ops->tlb_remote_flush)) > + return -EIO; > + > + max_pa = cpuid_eax(0x80000008) & 0xff; > + hkid_start_pos = boot_cpu_data.x86_phys_bits; > + hkid_mask = GENMASK_ULL(max_pa - 1, hkid_start_pos); > + > + return 0; > +} > + > +void __init tdx_hardware_setup(struct kvm_x86_ops *x86_ops) > +{ > + /* > + * This function is called at the initialization. No need to protect > + * enable_tdx. > + */ > + if (!enable_tdx) > + return; > + > + if (__tdx_hardware_setup(&vt_x86_ops)) > + enable_tdx = false; Clearing enable_tdx here unnecessarily risks introducing bugs in the caller, e.g. acting on enable_tdx before tdx_hardware_setup() is invoked. I'm guessing this was the result of trying to defer module load until VM creation. With that gone, the flag can be moved to vmx/main.c, as there should be zero reason for tdx.c to check/modify enable_tdx, i.e. functions in tdx.c should never be called if enabled_tdx = false. An alteranative to if (enable_tdx && tdx_hardware_setup(&vt_x86_ops)) enable_tdx = false; would be enable_tdx = enable_tdx && !tdx_hardware_setup(&vt_x86_ops); I actually prefer the latter (no "if"), but I already generated and wiped the below diff before thinking of that. diff --git a/arch/x86/kvm/vmx/main.c b/arch/x86/kvm/vmx/main.c index b79fcc8d81dd..43e13c2a804e 100644 --- a/arch/x86/kvm/vmx/main.c +++ b/arch/x86/kvm/vmx/main.c @@ -6,6 +6,9 @@ #include "nested.h" #include "pmu.h" +static bool __read_mostly enable_tdx = IS_ENABLED(CONFIG_INTEL_TDX_HOST); +module_param_named(tdx, enable_tdx, bool, 0644); + static __init int vt_hardware_setup(void) { int ret; @@ -14,7 +17,8 @@ static __init int vt_hardware_setup(void) if (ret) return ret; - tdx_hardware_setup(&vt_x86_ops); + if (enable_tdx && tdx_hardware_setup(&vt_x86_ops)) + enable_tdx = false; return 0; } diff --git a/arch/x86/kvm/vmx/tdx.c b/arch/x86/kvm/vmx/tdx.c index 1acf08c310c4..3f660f323426 100644 --- a/arch/x86/kvm/vmx/tdx.c +++ b/arch/x86/kvm/vmx/tdx.c @@ -9,13 +9,10 @@ #undef pr_fmt #define pr_fmt(fmt) "tdx: " fmt -static bool __read_mostly enable_tdx = true; -module_param_named(tdx, enable_tdx, bool, 0644); - static u64 hkid_mask __ro_after_init; static u8 hkid_start_pos __ro_after_init; -static int __init __tdx_hardware_setup(struct kvm_x86_ops *x86_ops) +static int __init tdx_hardware_setup(struct kvm_x86_ops *x86_ops) { u32 max_pa; @@ -38,16 +35,3 @@ static int __init __tdx_hardware_setup(struct kvm_x86_ops *x86_ops) return 0; } - -void __init tdx_hardware_setup(struct kvm_x86_ops *x86_ops) -{ - /* - * This function is called at the initialization. No need to protect - * enable_tdx. - */ - if (!enable_tdx) - return; - - if (__tdx_hardware_setup(&vt_x86_ops)) - enable_tdx = false; -} diff --git a/arch/x86/kvm/vmx/x86_ops.h b/arch/x86/kvm/vmx/x86_ops.h index ccf98e79d8c3..fd60128eb10a 100644 --- a/arch/x86/kvm/vmx/x86_ops.h +++ b/arch/x86/kvm/vmx/x86_ops.h @@ -124,9 +124,9 @@ void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu); void vmx_setup_mce(struct kvm_vcpu *vcpu); #ifdef CONFIG_INTEL_TDX_HOST -void __init tdx_hardware_setup(struct kvm_x86_ops *x86_ops); +int __init tdx_hardware_setup(struct kvm_x86_ops *x86_ops); #else -static inline void tdx_hardware_setup(struct kvm_x86_ops *x86_ops) {} +static inline int void tdx_hardware_setup(struct kvm_x86_ops *x86_ops) { return 0; } #endif #endif /* __KVM_X86_VMX_X86_OPS_H */