Received: by 2002:a05:7412:bbc7:b0:fc:a2b0:25d7 with SMTP id kh7csp2210527rdb; Sun, 4 Feb 2024 21:41:22 -0800 (PST) X-Google-Smtp-Source: AGHT+IHR4IYSytmJlLVY9hYCBL/a8wZ9slFAjWvx+S1VNS1lRdroFsTU1JTD8u9LWU/YddtJGwWk X-Received: by 2002:a17:906:3086:b0:a37:905c:e336 with SMTP id 6-20020a170906308600b00a37905ce336mr1749181ejv.75.1707111682346; Sun, 04 Feb 2024 21:41:22 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707111682; cv=pass; d=google.com; s=arc-20160816; b=xNJkaJFITJPwrXClxtBtsAksnoEcam0EtZFasbDz13fms4bp4kYFqZcE5J5fbrc/KQ qn/cEAPxeX2NPZDAbsa1XlQhW/yk0PKk+k34gmCqFbfOB3o6aGxImxT2V6RM3FDED2xC n42spzHna2gkHgoYbrNjvqP0Qc2JHaSYAm9tCx+DLA1Je2K1BWvRJA+YLVUGqPsAoZYG OOT8+UqwnQZbn2sfvv76jqDiPpRSJ9od/5DLMBjg+665dJaQNDyqlAHWshD4LdlFZgsU 3lvBVYXvXaBooe0aLm6+o/7sU4OnaflIe53CwDC0BjxhZfZmHCZ0ELbCmtSbAOh2Y1kw cxvQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:in-reply-to:from:references:cc:to:subject :user-agent:mime-version:list-unsubscribe:list-subscribe:list-id :precedence:date:message-id:dkim-signature; bh=E6FaAvM4/HmLpQ5OsONfFHkqDsZ1HiGdpg0Uk8ZFI5U=; fh=U0WPk2dC0EqRknlYaWn0Us0T/v5ORztNy7b8XCC3nf8=; b=Ots5CmQAbPPCzgYkEr1Y3EtmYNWHT+CNhnMHD0C2U9/kM7w+9SPU50FkixWwBAfe53 Dctmq1MOnFyan5orNSOY0eUQdAP5coBWGyhgn9eAZG+xsVHz9VxUzkVU0a5KZJowHVPB /nJCMh0ZOKRakjgPBu5YXSXAAFfIIfanbSJ38/DpIOIm8MkWsSakxUWPn6ll9gVqA22I x/8AbGW80oe+Z0ZjQuu4qf2/V9+bvllnLrnWCCSlrksZkLssL6BQkjQS+ux4M3eCYht1 tNTEq3XzvzOhcD1o8gjOOh/5G/Qt+R0/JVlUAYI2mXDey/UapEqQoNYtsLieUhgdZg// a4Pw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=KCrNTATx; arc=pass (i=1 dkim=pass dkdomain=intel.com dmarc=pass fromdomain=linux.intel.com); spf=pass (google.com: domain of linux-kernel+bounces-52009-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-52009-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com X-Forwarded-Encrypted: i=1; AJvYcCX1hWn9o5Ad64ERmuslfrwLZ8BvtYLG7hh5Gw0W3Vq5CEM78PtlUHK7Jx3ssmV23yYwVhwjHXIgO8j/i+X1Quqw2zusBJhhlxSW89XCeA== Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id u5-20020a170906124500b00a37b05a1dbesi864921eja.536.2024.02.04.21.41.22 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 04 Feb 2024 21:41:22 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-52009-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=KCrNTATx; arc=pass (i=1 dkim=pass dkdomain=intel.com dmarc=pass fromdomain=linux.intel.com); spf=pass (google.com: domain of linux-kernel+bounces-52009-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-52009-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id C73041F2290D for ; Mon, 5 Feb 2024 03:15:06 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 30087B654; Mon, 5 Feb 2024 03:14:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="KCrNTATx" Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.10]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3D031947F; Mon, 5 Feb 2024 03:14:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.10 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707102851; cv=none; b=NRszLPHvPaSUtHBiPUlgIZFLoUYHhj/x3iwFePbH8sMMRGm1fX1/jycG9Qv2JmDS9OhbKlBaAFmeYqk1G/QHe86kVqP0JAyt+zgTI1O8oCRkyUbdYfue9eCgrdvRq1oRFiJvxSSL06SlkB1Dbv2UeoEW0DY/e879UN0GUnEQJiU= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707102851; c=relaxed/simple; bh=MWsnU2xeLcim5q2Ctdfdd7941DBKVOOsy4cTw5k6d8w=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=Eon3qev6Mm6vs4k3GISCXOjVh4Lavt6KJ4LwnsDpFWUUppLkSwDph60ZSZ+R4WeuTfv4EWS5aXvHqEGglE9eM9O57PSwHprbWm0eQCDYbP65y2OLGgbSY7YCRq/XAK+sQ/s6oV7uvp26He5zGXzTwELvyRX5iTTVHWW6rvK9apg= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=none smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=KCrNTATx; arc=none smtp.client-ip=198.175.65.10 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1707102850; x=1738638850; h=message-id:date:mime-version:subject:to:cc:references: from:in-reply-to:content-transfer-encoding; bh=MWsnU2xeLcim5q2Ctdfdd7941DBKVOOsy4cTw5k6d8w=; b=KCrNTATxtDopM2s3MCR1mxSGDYvFgcj7ehy5S7VTaKfcjwek8Rp+oqLK n7fzd4uyYCXpMG06tp9gJh16kXvBSyXS4iKhZGd4KngVCZqZ2w2mbZAwH m+3z9FV79FVtqdWePIlEy2Yy99L0xTdYDJE4SqUjmwAEm7xAPgYmoQZYh xQojHi0EGDCgO7WEGTm5UjGnvKuCTkbGHbsi/NkRVotHN3qClkeckREOO P8UzWzOhQnSpCIdeWqWoM5N51UBq+Ni32wBeYFhl80VcQDDewhiT5EF6f I0ZXnEnaDecXU8u6KDu81Z5KdLoYGo2Riwfxl8gUdK9FvjZ7S5HFTgU9d Q==; X-IronPort-AV: E=McAfee;i="6600,9927,10974"; a="17857324" X-IronPort-AV: E=Sophos;i="6.05,242,1701158400"; d="scan'208";a="17857324" Received: from orviesa005.jf.intel.com ([10.64.159.145]) by orvoesa102.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 04 Feb 2024 19:14:09 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.05,242,1701158400"; d="scan'208";a="5210744" Received: from binbinwu-mobl.ccr.corp.intel.com (HELO [10.238.10.49]) ([10.238.10.49]) by orviesa005-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 04 Feb 2024 19:14:05 -0800 Message-ID: Date: Mon, 5 Feb 2024 11:14:03 +0800 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v18 058/121] KVM: TDX: Retry seamcall when TDX_OPERAND_BUSY with operand SEPT To: isaku.yamahata@intel.com Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, isaku.yamahata@gmail.com, Paolo Bonzini , erdemaktas@google.com, Sean Christopherson , Sagi Shahar , Kai Huang , chen.bo@intel.com, hang.yuan@intel.com, tina.zhang@intel.com, Yuan Yao References: From: Binbin Wu In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit On 1/23/2024 7:53 AM, isaku.yamahata@intel.com wrote: > 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 Should be TDH.VP.ENTER. > in TDX_OPERAND_BUSY | TDX_OPERAND_ID_SEPT. Also TDH.MEM.* SEAMCALLs may > result in TDX_OPERAN_BUSY | TDX_OPERAND_ID_SEPT. s/TDX_OPERAN_BUSY/TDX_OPERAND_BUSY > > 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. Does it retry TDH.VP.ENTER on SEPT busy? I didn't see the related code in this patch. > > Signed-off-by: Yuan Yao > Signed-off-by: Isaku Yamahata > --- > arch/x86/kvm/vmx/tdx_ops.h | 48 +++++++++++++++++++++++++++++++------- > 1 file changed, 39 insertions(+), 9 deletions(-) > > diff --git a/arch/x86/kvm/vmx/tdx_ops.h b/arch/x86/kvm/vmx/tdx_ops.h > index cd12e9c2a421..53a6c3f692b0 100644 > --- a/arch/x86/kvm/vmx/tdx_ops.h > +++ b/arch/x86/kvm/vmx/tdx_ops.h > @@ -52,6 +52,36 @@ static inline u64 tdx_seamcall(u64 op, struct tdx_module_args *in, > void pr_tdx_error(u64 op, u64 error_code, const struct tdx_module_args *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, struct tdx_module_args *in, > + struct tdx_module_args *out) > +{ > +#define SEAMCALL_RETRY_MAX 16 > + int retry = SEAMCALL_RETRY_MAX; > + u64 ret; > + > + do { > + ret = tdx_seamcall(op, in, out); > + } while (ret == TDX_ERROR_SEPT_BUSY && retry-- > 0); > + return ret; > +} > + > static inline u64 tdh_mng_addcx(hpa_t tdr, hpa_t addr) > { > struct tdx_module_args in = { > @@ -74,7 +104,7 @@ static inline u64 tdh_mem_page_add(hpa_t tdr, gpa_t gpa, hpa_t hpa, hpa_t source > }; > > clflush_cache_range(__va(hpa), PAGE_SIZE); > - return tdx_seamcall(TDH_MEM_PAGE_ADD, &in, out); > + return tdx_seamcall_sept(TDH_MEM_PAGE_ADD, &in, out); > } > > static inline u64 tdh_mem_sept_add(hpa_t tdr, gpa_t gpa, int level, hpa_t page, > @@ -87,7 +117,7 @@ static inline u64 tdh_mem_sept_add(hpa_t tdr, gpa_t gpa, int level, hpa_t page, > }; > > clflush_cache_range(__va(page), PAGE_SIZE); > - return tdx_seamcall(TDH_MEM_SEPT_ADD, &in, out); > + return tdx_seamcall_sept(TDH_MEM_SEPT_ADD, &in, out); > } > > static inline u64 tdh_mem_sept_rd(hpa_t tdr, gpa_t gpa, int level, > @@ -98,7 +128,7 @@ static inline u64 tdh_mem_sept_rd(hpa_t tdr, gpa_t gpa, int level, > .rdx = tdr, > }; > > - return tdx_seamcall(TDH_MEM_SEPT_RD, &in, out); > + return tdx_seamcall_sept(TDH_MEM_SEPT_RD, &in, out); > } > > static inline u64 tdh_mem_sept_remove(hpa_t tdr, gpa_t gpa, int level, > @@ -109,7 +139,7 @@ static inline u64 tdh_mem_sept_remove(hpa_t tdr, gpa_t gpa, int level, > .rdx = tdr, > }; > > - return tdx_seamcall(TDH_MEM_SEPT_REMOVE, &in, out); > + return tdx_seamcall_sept(TDH_MEM_SEPT_REMOVE, &in, out); > } > > static inline u64 tdh_vp_addcx(hpa_t tdvpr, hpa_t addr) > @@ -133,7 +163,7 @@ static inline u64 tdh_mem_page_relocate(hpa_t tdr, gpa_t gpa, hpa_t hpa, > }; > > clflush_cache_range(__va(hpa), PAGE_SIZE); > - return tdx_seamcall(TDH_MEM_PAGE_RELOCATE, &in, out); > + return tdx_seamcall_sept(TDH_MEM_PAGE_RELOCATE, &in, out); > } > > static inline u64 tdh_mem_page_aug(hpa_t tdr, gpa_t gpa, hpa_t hpa, > @@ -146,7 +176,7 @@ static inline u64 tdh_mem_page_aug(hpa_t tdr, gpa_t gpa, hpa_t hpa, > }; > > clflush_cache_range(__va(hpa), PAGE_SIZE); > - return tdx_seamcall(TDH_MEM_PAGE_AUG, &in, out); > + return tdx_seamcall_sept(TDH_MEM_PAGE_AUG, &in, out); > } > > static inline u64 tdh_mem_range_block(hpa_t tdr, gpa_t gpa, int level, > @@ -157,7 +187,7 @@ static inline u64 tdh_mem_range_block(hpa_t tdr, gpa_t gpa, int level, > .rdx = tdr, > }; > > - return tdx_seamcall(TDH_MEM_RANGE_BLOCK, &in, out); > + return tdx_seamcall_sept(TDH_MEM_RANGE_BLOCK, &in, out); > } > > static inline u64 tdh_mng_key_config(hpa_t tdr) > @@ -307,7 +337,7 @@ static inline u64 tdh_mem_page_remove(hpa_t tdr, gpa_t gpa, int level, > .rdx = tdr, > }; > > - return tdx_seamcall(TDH_MEM_PAGE_REMOVE, &in, out); > + return tdx_seamcall_sept(TDH_MEM_PAGE_REMOVE, &in, out); > } > > static inline u64 tdh_sys_lp_shutdown(void) > @@ -335,7 +365,7 @@ static inline u64 tdh_mem_range_unblock(hpa_t tdr, gpa_t gpa, int level, > .rdx = tdr, > }; > > - return tdx_seamcall(TDH_MEM_RANGE_UNBLOCK, &in, out); > + return tdx_seamcall_sept(TDH_MEM_RANGE_UNBLOCK, &in, out); > } > > static inline u64 tdh_phymem_cache_wb(bool resume)