Received: by 2002:a05:6358:c692:b0:131:369:b2a3 with SMTP id fe18csp278924rwb; Tue, 25 Jul 2023 16:00:12 -0700 (PDT) X-Google-Smtp-Source: APBJJlEB8d22q5XzoESGcsRf8WAdizBN1iReRXUHZjW6KltcsBn4zSTiDxJTSIOCtjR2bLMZO3VB X-Received: by 2002:a50:ed81:0:b0:51f:f079:875f with SMTP id h1-20020a50ed81000000b0051ff079875fmr614569edr.4.1690326012711; Tue, 25 Jul 2023 16:00:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690326012; cv=none; d=google.com; s=arc-20160816; b=KFDaojFKa8gt6QLvQm69j4DdfZsY+VtatQ90jT+zh8lMoM+toTvFQgbb1GkPdYuPWr CgPt3Yf60OJlegTLLVl7j6gNN32Wi4XraGec5SBTpbOXQdwmTf0k4BTo98Q3r/xqHSN7 pEVG4ywu8mBb1amayfGm89/U7Ezm6mvgj8yRcEIa7Vd8QnrhO12UGwIkw1U4meC8lW11 kKrvBeHylFQQ1azKRhcEUk4Hr8dptgqSbOs4O8ft++d121f2IKOa9BIe/BGA97VNg9Te afKSwqOz2LNSxO4dCH/Yesuzm++D10tpOJ2LCQ3AkRmDdKriCHLf/ZfNU2nZCvXINnsC Ztiw== 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=SrSFcI0tM9PRGz99NVDR2EJcbwu8uqeltBcYFx7cizo=; fh=nqVTrw1WLVOp+hSy+As951w6pmrUw644QkvmVnK6+uU=; b=jhKgvrkCqgLj6P5Vpz+XF9iWzsYLaTogSoJUNO+kTazf2piYK6l/f/NXfiGk3uJS49 grmc1T+lEXI7OC4CiGkHY4L/EmwtCl3eFeyRYSBQxfDfPRI2LtQqlRv0Or9tJ4bJ3OEq Eew83Dka6a0um6bUFG6f9v01/XsLhKXkZ1tWe9Hg18Lo2vp3KpQzF/tQh+JYi29rP1FC 67wpGvUVYJMMwtIM4LcaSk4+7TnfgHrbzv2vXyVPJzIwSDQVMCIoFjS9WoJJtWbTrNOS r4I1Et3lULgcqYW7PKRU8O0Q+O7a9glUxHPqXyaeuxStj0FGhkt5l4ro2fAhBHQ+cK3L lQoQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=TuPusrrG; 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 q26-20020aa7d45a000000b00522561facd7si1171995edr.143.2023.07.25.15.59.25; Tue, 25 Jul 2023 16:00:12 -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=@intel.com header.s=Intel header.b=TuPusrrG; 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 S231193AbjGYWW0 (ORCPT + 99 others); Tue, 25 Jul 2023 18:22:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39318 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232562AbjGYWVa (ORCPT ); Tue, 25 Jul 2023 18:21:30 -0400 Received: from mga02.intel.com (mga02.intel.com [134.134.136.20]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 346EC2D79; Tue, 25 Jul 2023 15:17:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1690323443; x=1721859443; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=HSYqTrf4S+axzOfvfShXkPansWqRKfM/KCR4xdDb8/g=; b=TuPusrrGOvEKf0jLrmeRwzPWAJS30qpg8O3qXLcmRPs4BjwZF7wadHQf v7hBEuBhF+bzb4o5DZXKSimVxQEQ5FYUGx+198H+KOCilKjeQDBmIvg1i p6CrrJNcXCXnbvBVlB+RdB6PPJD7EDXuPdrDAhxaFQ3eTNqrHB64UnhkK txEbKh7Qdj5G/h+tbz7FscSzdfpje3Lk6QaqGdYF++FrFD3q4yvZItXqX MdcRxiff0yp7oplR5a6qgnRaFNiLisYmVFmQ+TBJ5q1wT/rx0WKKUUyTu x72nibrTCeEFNdKTCWxapdm+/oF+mEZvsbKcpGYERr59PEb3bqT88peXJ w==; X-IronPort-AV: E=McAfee;i="6600,9927,10782"; a="357863316" X-IronPort-AV: E=Sophos;i="6.01,231,1684825200"; d="scan'208";a="357863316" Received: from fmsmga005.fm.intel.com ([10.253.24.32]) by orsmga101.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 25 Jul 2023 15:15:42 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10782"; a="1056938960" X-IronPort-AV: E=Sophos;i="6.01,231,1684825200"; d="scan'208";a="1056938960" Received: from ls.sc.intel.com (HELO localhost) ([172.25.112.31]) by fmsmga005-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 25 Jul 2023 15:15:41 -0700 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 , erdemaktas@google.com, Sean Christopherson , Sagi Shahar , David Matlack , Kai Huang , Zhi Wang , chen.bo@intel.com, hang.yuan@intel.com, tina.zhang@intel.com, Yuan Yao Subject: [PATCH v15 053/115] KVM: TDX: Retry seamcall when TDX_OPERAND_BUSY with operand SEPT Date: Tue, 25 Jul 2023 15:14:04 -0700 Message-Id: <56def5772292451d8dd002c388c07ff42c3b4414.1690322424.git.isaku.yamahata@intel.com> 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.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE, T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED 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: Yuan Yao TDX module internally uses locks to protect internal resources. It tries to acquire the locks. If it fails to obtain the lock, it returns TDX_OPERAND_BUSY error without spin because its execution time limitation. TDX SEAMCALL API reference describes what resources are used. It's known which TDX SEAMCALL can cause contention with which resources. VMM can avoid contention inside the TDX module by avoiding contentious TDX SEAMCALL with, for example, spinlock. Because OS knows better its process scheduling and its scalability, a lock at OS/VMM layer would work better than simply retrying TDX SEAMCALLs. TDH.MEM.* API except for TDH.MEM.TRACK operates on a secure EPT tree and the TDX module internally tries to acquire the lock of the secure EPT tree. They return TDX_OPERAND_BUSY | TDX_OPERAND_ID_SEPT in case of failure to get the lock. TDX KVM allows sept callbacks to return error so that TDP MMU layer can retry. TDH.VP.ENTER is an exception with zero-step attack mitigation. Normally TDH.VP.ENTER uses only TD vcpu resources and it doesn't cause contention. When a zero-step attack is suspected, it obtains a secure EPT tree lock and tracks the GPAs causing a secure EPT fault. Thus TDG.VP.ENTER may result in TDX_OPERAND_BUSY | TDX_OPERAND_ID_SEPT. Also TDH.MEM.* SEAMCALLs may result in TDX_OPERAN_BUSY | TDX_OPERAND_ID_SEPT. Retry TDX TDH.MEM.* API and TDH.VP.ENTER on the error because the error is a rare event caused by zero-step attack mitigation and spinlock can not be used for TDH.VP.ENTER due to indefinite time execution. Signed-off-by: Yuan Yao Signed-off-by: Isaku Yamahata --- arch/x86/kvm/vmx/tdx_ops.h | 42 ++++++++++++++++++++++++++++++++------ 1 file changed, 36 insertions(+), 6 deletions(-) diff --git a/arch/x86/kvm/vmx/tdx_ops.h b/arch/x86/kvm/vmx/tdx_ops.h index 9db19c0711a9..c7819abd61b0 100644 --- a/arch/x86/kvm/vmx/tdx_ops.h +++ b/arch/x86/kvm/vmx/tdx_ops.h @@ -46,6 +46,36 @@ static inline u64 tdx_seamcall(u64 op, u64 rcx, u64 rdx, u64 r8, u64 r9, void pr_tdx_error(u64 op, u64 error_code, const struct tdx_module_output *out); #endif +/* + * TDX module acquires its internal lock for resources. It doesn't spin to get + * locks because of its restrictions of allowed execution time. Instead, it + * returns TDX_OPERAND_BUSY with an operand id. + * + * Multiple VCPUs can operate on SEPT. Also with zero-step attack mitigation, + * TDH.VP.ENTER may rarely acquire SEPT lock and release it when zero-step + * attack is suspected. It results in TDX_OPERAND_BUSY | TDX_OPERAND_ID_SEPT + * with TDH.MEM.* operation. Note: TDH.MEM.TRACK is an exception. + * + * Because TDP MMU uses read lock for scalability, spin lock around SEAMCALL + * spoils TDP MMU effort. Retry several times with the assumption that SEPT + * lock contention is rare. But don't loop forever to avoid lockup. Let TDP + * MMU retry. + */ +#define TDX_ERROR_SEPT_BUSY (TDX_OPERAND_BUSY | TDX_OPERAND_ID_SEPT) + +static inline u64 tdx_seamcall_sept(u64 op, u64 rcx, u64 rdx, u64 r8, u64 r9, + struct tdx_module_output *out) +{ +#define SEAMCALL_RETRY_MAX 16 + int retry = SEAMCALL_RETRY_MAX; + u64 ret; + + do { + ret = tdx_seamcall(op, rcx, rdx, r8, r9, out); + } while (ret == TDX_ERROR_SEPT_BUSY && retry-- > 0); + return ret; +} + static inline u64 tdh_mng_addcx(hpa_t tdr, hpa_t addr) { clflush_cache_range(__va(addr), PAGE_SIZE); @@ -56,14 +86,14 @@ static inline u64 tdh_mem_page_add(hpa_t tdr, gpa_t gpa, hpa_t hpa, hpa_t source struct tdx_module_output *out) { clflush_cache_range(__va(hpa), PAGE_SIZE); - return tdx_seamcall(TDH_MEM_PAGE_ADD, gpa, tdr, hpa, source, out); + return tdx_seamcall_sept(TDH_MEM_PAGE_ADD, gpa, tdr, hpa, source, out); } static inline u64 tdh_mem_sept_add(hpa_t tdr, gpa_t gpa, int level, hpa_t page, struct tdx_module_output *out) { clflush_cache_range(__va(page), PAGE_SIZE); - return tdx_seamcall(TDH_MEM_SEPT_ADD, gpa | level, tdr, page, 0, out); + return tdx_seamcall_sept(TDH_MEM_SEPT_ADD, gpa | level, tdr, page, 0, out); } static inline u64 tdh_mem_sept_remove(hpa_t tdr, gpa_t gpa, int level, @@ -89,13 +119,13 @@ static inline u64 tdh_mem_page_aug(hpa_t tdr, gpa_t gpa, hpa_t hpa, struct tdx_module_output *out) { clflush_cache_range(__va(hpa), PAGE_SIZE); - return tdx_seamcall(TDH_MEM_PAGE_AUG, gpa, tdr, hpa, 0, out); + return tdx_seamcall_sept(TDH_MEM_PAGE_AUG, gpa, tdr, hpa, 0, out); } static inline u64 tdh_mem_range_block(hpa_t tdr, gpa_t gpa, int level, struct tdx_module_output *out) { - return tdx_seamcall(TDH_MEM_RANGE_BLOCK, gpa | level, tdr, 0, 0, out); + return tdx_seamcall_sept(TDH_MEM_RANGE_BLOCK, gpa | level, tdr, 0, 0, out); } static inline u64 tdh_mng_key_config(hpa_t tdr) @@ -177,7 +207,7 @@ static inline u64 tdh_phymem_page_reclaim(hpa_t page, static inline u64 tdh_mem_page_remove(hpa_t tdr, gpa_t gpa, int level, struct tdx_module_output *out) { - return tdx_seamcall(TDH_MEM_PAGE_REMOVE, gpa | level, tdr, 0, 0, out); + return tdx_seamcall_sept(TDH_MEM_PAGE_REMOVE, gpa | level, tdr, 0, 0, out); } static inline u64 tdh_sys_lp_shutdown(void) @@ -193,7 +223,7 @@ static inline u64 tdh_mem_track(hpa_t tdr) static inline u64 tdh_mem_range_unblock(hpa_t tdr, gpa_t gpa, int level, struct tdx_module_output *out) { - return tdx_seamcall(TDH_MEM_RANGE_UNBLOCK, gpa | level, tdr, 0, 0, out); + return tdx_seamcall_sept(TDH_MEM_RANGE_UNBLOCK, gpa | level, tdr, 0, 0, out); } static inline u64 tdh_phymem_cache_wb(bool resume) -- 2.25.1