Received: by 2002:a05:6a10:9afc:0:0:0:0 with SMTP id t28csp2241889pxm; Fri, 4 Mar 2022 12:09:05 -0800 (PST) X-Google-Smtp-Source: ABdhPJykNG1ydimU7cStS3LBBZOkgJ93YSWVFn/54a8CBZ+0oRuQq8WQGV3UlIoKOZsHVTs/wnRs X-Received: by 2002:a17:902:eb8f:b0:151:63fa:f59b with SMTP id q15-20020a170902eb8f00b0015163faf59bmr30618plg.93.1646424545618; Fri, 04 Mar 2022 12:09:05 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1646424545; cv=none; d=google.com; s=arc-20160816; b=GxpfMW7CP6Nrc+feSsyk1xx4Vg4qlV3UZZUYmiykb5msQmW2Zwx9S5bmcxsj9SA8Hd Zve6Ub+tgpvFCIayeRfVSp+jcuSQUF85JPp26TL2CXBUg5jBSGtETSNhGcexTZ+m6qxj pwQb5fHWxdb1UI/5HtTvtOmjWN99dhGpMT8DB0tGnmlR/MGigDTTKWNmugGneJHtKWAh Jy8vmkYkF/gVTM5ggptURo7DbkEyhvq8gZvbTtu+8aJEYQXzqgr8qL91Fi8YAc0mkOHR 8ZOl+VipjU1EY4a/qaQ2D8hYUIveTnzGeHUipLg2cJt6VwRyAkgEBsft6AHx0Hw/Y3Qo tcIA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=VeCBjG3FEwHTCgjVU/39IarYDy+30Lmi+LmgnyVFLG8=; b=w7up5HCUNiNhFG/7oeSahn4dWysaulbJvhD012+mapS1lnGUgdEbB6aCknFGiCOU82 4aAU8nZ+LUH9uZRFxhMrd8PMuOS8oXYMnQQQh60o0BFaMkUtTXHI5BsL15yhXyRAE76X iM7A9zQplg1E/bMlcV1X3u8x2HJ3MWoE2VBI9ZWSQFL0jF2u+8YPlrvsqzU4zHfRj1TO iB20PYrMhJhikBAmTruidP2GFrCxKO2xTY2wcaxxkWT1GxGkhWzd4xwDsyctEN7X7P+m xFxnErncG+SCIOxJsXh9KyaatwfOOnjAYujZfivedWuH48c1ICoJDc+8n12UHLJN9cNn PNmw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=Xtw+KfuQ; 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=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id a65-20020a639044000000b0037c9258a1aesi2981587pge.630.2022.03.04.12.08.48; Fri, 04 Mar 2022 12:09:05 -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=@intel.com header.s=Intel header.b=Xtw+KfuQ; 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=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230344AbiCDUHi (ORCPT + 99 others); Fri, 4 Mar 2022 15:07:38 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53108 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230376AbiCDUHA (ORCPT ); Fri, 4 Mar 2022 15:07:00 -0500 Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2196CA76FD; Fri, 4 Mar 2022 12:01:37 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1646424097; x=1677960097; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=Zhkd9vMW4IoFvLpPP2WdaDbfud+1lrbpgFdFlKEXWIw=; b=Xtw+KfuQGKjvX5po7xEKUzAik6Ma++SegukFzj/Wr2Xun7Uj3fcU0izl W6KRzJRStBzn+qCUEksHVtpzcC+4hM6TWNg1cY2aN2RjOGWoyjko5DjGu /IuE2uzsFR3MahAczvgeZbQc5NaiubQqqpbikWAX9+cvAv/U3rIRjb8Ed ZGby3iVYny1LS0jxVqcxikytz3HiAuSUG+6v7rUIkjMfn3q+fd9i7cm3a N0InC3ueZTrvTKAht5e0M3t2G/j40N5VzE9F2z6ljv0VFIpb6ogCR/jUI r1/5Eg0iLJ8rRdQeRPTVBk93iCUNKs/8uliE4UvLGf4x9Mkii6HYZUDa5 w==; X-IronPort-AV: E=McAfee;i="6200,9189,10276"; a="253983376" X-IronPort-AV: E=Sophos;i="5.90,156,1643702400"; d="scan'208";a="253983376" Received: from orsmga008.jf.intel.com ([10.7.209.65]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 04 Mar 2022 11:50:12 -0800 X-IronPort-AV: E=Sophos;i="5.90,156,1643702400"; d="scan'208";a="552344222" Received: from ls.sc.intel.com (HELO localhost) ([143.183.96.54]) by orsmga008-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 04 Mar 2022 11:50:12 -0800 From: isaku.yamahata@intel.com To: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Cc: isaku.yamahata@intel.com, isaku.yamahata@gmail.com, Paolo Bonzini , Jim Mattson , erdemaktas@google.com, Connor Kuehl , Sean Christopherson Subject: [RFC PATCH v5 020/104] KVM: TDX: allocate per-package mutex Date: Fri, 4 Mar 2022 11:48:36 -0800 Message-Id: X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-4.8 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_NONE,T_SCC_BODY_TEXT_LINE 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 From: Isaku Yamahata Several TDX SEAMCALLs are per-package scope (concretely per memory controller) and they need to be serialized per-package. Allocate mutex for it. Signed-off-by: Isaku Yamahata --- arch/x86/kvm/vmx/main.c | 8 +++++++- arch/x86/kvm/vmx/tdx.c | 18 ++++++++++++++++++ arch/x86/kvm/vmx/x86_ops.h | 2 ++ 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/arch/x86/kvm/vmx/main.c b/arch/x86/kvm/vmx/main.c index 8103d1c32cc9..6111c6485d8e 100644 --- a/arch/x86/kvm/vmx/main.c +++ b/arch/x86/kvm/vmx/main.c @@ -25,6 +25,12 @@ static __init int vt_hardware_setup(void) return 0; } +static void vt_hardware_unsetup(void) +{ + tdx_hardware_unsetup(); + vmx_hardware_unsetup(); +} + static int vt_vm_init(struct kvm *kvm) { int ret; @@ -42,7 +48,7 @@ static int vt_vm_init(struct kvm *kvm) struct kvm_x86_ops vt_x86_ops __initdata = { .name = "kvm_intel", - .hardware_unsetup = vmx_hardware_unsetup, + .hardware_unsetup = vt_hardware_unsetup, .hardware_enable = vmx_hardware_enable, .hardware_disable = vmx_hardware_disable, diff --git a/arch/x86/kvm/vmx/tdx.c b/arch/x86/kvm/vmx/tdx.c index e8d293a3c11c..1c8222f54764 100644 --- a/arch/x86/kvm/vmx/tdx.c +++ b/arch/x86/kvm/vmx/tdx.c @@ -34,6 +34,8 @@ struct tdx_capabilities { /* Capabilities of KVM + the TDX module. */ struct tdx_capabilities tdx_caps; +static struct mutex *tdx_mng_key_config_lock; + static u64 hkid_mask __ro_after_init; static u8 hkid_start_pos __ro_after_init; @@ -112,7 +114,9 @@ bool tdx_is_vm_type_supported(unsigned long type) static int __init __tdx_hardware_setup(struct kvm_x86_ops *x86_ops) { + int max_pkgs; u32 max_pa; + int i; if (!enable_ept) { pr_warn("Cannot enable TDX with EPT disabled\n"); @@ -127,6 +131,14 @@ static int __init __tdx_hardware_setup(struct kvm_x86_ops *x86_ops) if (WARN_ON_ONCE(x86_ops->tlb_remote_flush)) return -EIO; + max_pkgs = topology_max_packages(); + tdx_mng_key_config_lock = kcalloc(max_pkgs, sizeof(*tdx_mng_key_config_lock), + GFP_KERNEL); + if (!tdx_mng_key_config_lock) + return -ENOMEM; + for (i = 0; i < max_pkgs; i++) + mutex_init(&tdx_mng_key_config_lock[i]); + 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); @@ -147,6 +159,12 @@ void __init tdx_hardware_setup(struct kvm_x86_ops *x86_ops) enable_tdx = false; } +void tdx_hardware_unsetup(void) +{ + /* kfree accepts NULL. */ + kfree(tdx_mng_key_config_lock); +} + void __init tdx_pre_kvm_init(unsigned int *vcpu_size, unsigned int *vcpu_align, unsigned int *vm_size) { diff --git a/arch/x86/kvm/vmx/x86_ops.h b/arch/x86/kvm/vmx/x86_ops.h index 78331dbc29f7..da32b4b86b19 100644 --- a/arch/x86/kvm/vmx/x86_ops.h +++ b/arch/x86/kvm/vmx/x86_ops.h @@ -131,11 +131,13 @@ void __init tdx_pre_kvm_init(unsigned int *vcpu_size, unsigned int *vcpu_align, unsigned int *vm_size); bool tdx_is_vm_type_supported(unsigned long type); void __init tdx_hardware_setup(struct kvm_x86_ops *x86_ops); +void tdx_hardware_unsetup(void); #else static inline void tdx_pre_kvm_init( unsigned int *vcpu_size, unsigned int *vcpu_align, unsigned int *vm_size) {} static inline bool tdx_is_vm_type_supported(unsigned long type) { return false; } static inline void tdx_hardware_setup(struct kvm_x86_ops *x86_ops) {} +static inline void tdx_hardware_unsetup(void) {} #endif #endif /* __KVM_X86_VMX_X86_OPS_H */ -- 2.25.1