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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id C8283CCF9EB for ; Mon, 27 Oct 2025 19:20:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:From:Subject:Message-ID: References:Mime-Version:In-Reply-To:Date:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=DtxYclXOMusgnIqwIcPgZgX3FDXgUJG1wwUb5ZgagCY=; b=p24Pirj9pbqkoYfqa2taHDYMVb 8UTpiA0dPrm1OdLpx7I1CuYrcfyBNtjWUdJqauQPKyzWugLTiGyy1K7LKcDC7L43Hp9vtZ1rvc2QJ wMCe0RYvoR1Ha3ZaJz0UDSQvLd48TbUiR0ERJsuuvtjMCSueFHfl3cLnOoz9wayl6wEkSLXF6k2z7 sH3ONOrz0wK5XEXoTGNE1H4fUlI7lHnH3RW2lVZkxHcX5yfzvyDFeluHR8/UDi/Rpm5NgPjRe2TUm nAvktPSfytBA3IXEEgnQnrIZbmhxwBeo0KILdXp0gfa557mWNvhhJSkw6OSfV4qWtE7g/0833u4x2 mYu935Ag==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98.2 #2 (Red Hat Linux)) id 1vDSlN-0000000EeKL-1J50; Mon, 27 Oct 2025 19:20:17 +0000 Received: from mail-pl1-x64a.google.com ([2607:f8b0:4864:20::64a]) by bombadil.infradead.org with esmtps (Exim 4.98.2 #2 (Red Hat Linux)) id 1vDSlJ-0000000EeIN-1cPq for kvm-riscv@lists.infradead.org; Mon, 27 Oct 2025 19:20:15 +0000 Received: by mail-pl1-x64a.google.com with SMTP id d9443c01a7336-294880e7ca4so85603435ad.0 for ; Mon, 27 Oct 2025 12:20:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1761592812; x=1762197612; darn=lists.infradead.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=GJ/ihzhnBg2h+OIog5s51f1nDJzu1yBHwy2SCCUQ1bo=; b=n4y/XYJpxZT47gkE17oanNSpUzIHRSa8/lHwHWyIGnDreQveRJsc9TtirCII2alHmC C6wB9mu16rlgSy288A4eyouvM/QgvGv+4/NYOB+vIW/3mKavVFZmg4QO+bWjPGRszmRm yVge6w1sfoiBdWthgMmrQAdn1EZlE2BYgpIPaptncXr1zyffos/a8m5b54iXDqHBB5Nq aGCnvFGxUFS7leQAgnn/qFlD69PjLBF38v8UeDeYX/TsNK5CXhalbIg+393nXn6bBESY UIAKzaEN5fpAoVzv3mRgTN3BuZH+xwNDLOHKDJ7tg5l2Ozws+u2qsWXKcRpbXm7mmQUS kcQw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1761592812; x=1762197612; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=GJ/ihzhnBg2h+OIog5s51f1nDJzu1yBHwy2SCCUQ1bo=; b=hJ/em7v/c+1vxoleHV1fwzpSjeqda1ddlcA1IXhlitVttUzu43aX6jniDX0sBenY8M H7h940YGiIvYSlv49Odwu9CIbYe3Dp4b73mqJQJ2ab+IyGjPiuUOuugLUq8NpD/VtJZK wS6S9vTOssAm2wvEKDWiumqyn27nVk+uH56//ubnjss9RGaPHSE7ngZyBDjJfpiEuKxc BXKaptWgPtiriUJQvtQDdvBE4zoWlo7e6xZ/Dd2x/THTK/Yabzd8ULd0LmPjgCmH01hX MWbTJ+2zC8ZMiGaFa8y+MV+S0rWF4dSUS0algitIssbAQgebvfJ1RtOF+un1v0Uloh5j YdmQ== X-Forwarded-Encrypted: i=1; AJvYcCVThFkV6TM9Y4vaJRxWmlbWFIma95GD3L6vhMuox4t9rGbAw1yEurGwkLaXW/6ZMX8/550S3YkamVQ=@lists.infradead.org X-Gm-Message-State: AOJu0YyVCBFoYLtqyPH0PcJaCRiAmebhFpRkd/WARau0FcQTPAZ/Olp/ re2qiM1OUZCCJxmLdElBnk1zsSBe5PeAn5i2la1WDIZ0gaRcFHPms6JCYRtrMSY2m/Udngk6Hez GUdZ57w== X-Google-Smtp-Source: AGHT+IGEzxKOsu0TIx9JeJ8jo0iJqVMcVQCTPniC3ps1TgJ3BGzDUda18OgqbILlA0+mvKP7+G2jd9/+Iec= X-Received: from pjbrs15.prod.google.com ([2002:a17:90b:2b8f:b0:33b:51fe:1a73]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:902:e5c4:b0:25d:1640:1d59 with SMTP id d9443c01a7336-294cb378610mr8002455ad.8.1761592811798; Mon, 27 Oct 2025 12:20:11 -0700 (PDT) Date: Mon, 27 Oct 2025 12:20:10 -0700 In-Reply-To: <4809644b0ba02d0987ac56f4be7c426d0724cdef.camel@intel.com> Mime-Version: 1.0 References: <20251017003244.186495-1-seanjc@google.com> <20251017003244.186495-21-seanjc@google.com> <4809644b0ba02d0987ac56f4be7c426d0724cdef.camel@intel.com> Message-ID: Subject: Re: [PATCH v3 20/25] KVM: TDX: Add macro to retry SEAMCALLs when forcing vCPUs out of guest From: Sean Christopherson To: Kai Huang Cc: "chenhuacai@kernel.org" , "frankja@linux.ibm.com" , "maz@kernel.org" , "borntraeger@linux.ibm.com" , "pjw@kernel.org" , "aou@eecs.berkeley.edu" , "kas@kernel.org" , "maobibo@loongson.cn" , "pbonzini@redhat.com" , "maddy@linux.ibm.com" , "palmer@dabbelt.com" , "imbrenda@linux.ibm.com" , "zhaotianrui@loongson.cn" , "anup@brainfault.org" , "oliver.upton@linux.dev" , "kvm@vger.kernel.org" , "linux-coco@lists.linux.dev" , Yan Y Zhao , "michael.roth@amd.com" , "linux-kernel@vger.kernel.org" , Ira Weiny , "loongarch@lists.linux.dev" , "binbin.wu@linux.intel.com" , "ackerleytng@google.com" , "kvmarm@lists.linux.dev" , "kvm-riscv@lists.infradead.org" , Vishal Annapurve , "linuxppc-dev@lists.ozlabs.org" , Rick P Edgecombe , "linux-mips@vger.kernel.org" , "linux-arm-kernel@lists.infradead.org" , "linux-riscv@lists.infradead.org" , "x86@kernel.org" X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20251027_122013_455478_334A825A X-CRM114-Status: GOOD ( 23.31 ) X-BeenThere: kvm-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "kvm-riscv" Errors-To: kvm-riscv-bounces+kvm-riscv=archiver.kernel.org@lists.infradead.org On Fri, Oct 24, 2025, Kai Huang wrote: > On Thu, 2025-10-16 at 17:32 -0700, Sean Christopherson wrote: > > Add a macro to handle kicking vCPUs out of the guest and retrying > > SEAMCALLs on -EBUSY instead of providing small helpers to be used by each > > SEAMCALL. Wrapping the SEAMCALLs in a macro makes it a little harder to > > tease out which SEAMCALL is being made, but significantly reduces the > > amount of copy+paste code and makes it all but impossible to leave an > > elevated wait_for_sept_zap. > > > > Signed-off-by: Sean Christopherson > > --- > > arch/x86/kvm/vmx/tdx.c | 72 ++++++++++++++---------------------------- > > 1 file changed, 23 insertions(+), 49 deletions(-) > > > > diff --git a/arch/x86/kvm/vmx/tdx.c b/arch/x86/kvm/vmx/tdx.c > > index f6782b0ffa98..2e2dab89c98f 100644 > > --- a/arch/x86/kvm/vmx/tdx.c > > +++ b/arch/x86/kvm/vmx/tdx.c > > @@ -294,25 +294,24 @@ static inline void tdx_disassociate_vp(struct kvm_vcpu *vcpu) > > vcpu->cpu = -1; > > } > > > > -static void tdx_no_vcpus_enter_start(struct kvm *kvm) > > -{ > > - struct kvm_tdx *kvm_tdx = to_kvm_tdx(kvm); > > - > > - lockdep_assert_held_write(&kvm->mmu_lock); > > - > > - WRITE_ONCE(kvm_tdx->wait_for_sept_zap, true); > > - > > - kvm_make_all_cpus_request(kvm, KVM_REQ_OUTSIDE_GUEST_MODE); > > -} > > - > > -static void tdx_no_vcpus_enter_stop(struct kvm *kvm) > > -{ > > - struct kvm_tdx *kvm_tdx = to_kvm_tdx(kvm); > > - > > - lockdep_assert_held_write(&kvm->mmu_lock); > > - > > - WRITE_ONCE(kvm_tdx->wait_for_sept_zap, false); > > -} > > +#define tdh_do_no_vcpus(tdh_func, kvm, args...) \ > > +({ \ > > + struct kvm_tdx *__kvm_tdx = to_kvm_tdx(kvm); \ > > + u64 __err; \ > > + \ > > + lockdep_assert_held_write(&kvm->mmu_lock); \ > > + \ > > + __err = tdh_func(args); \ > > + if (unlikely(tdx_operand_busy(__err))) { \ > > + WRITE_ONCE(__kvm_tdx->wait_for_sept_zap, true); \ > > + kvm_make_all_cpus_request(kvm, KVM_REQ_OUTSIDE_GUEST_MODE); \ > > + \ > > + __err = tdh_func(args); \ > > + \ > > + WRITE_ONCE(__kvm_tdx->wait_for_sept_zap, false); \ > > + } \ > > + __err; \ > > +}) > > The comment which says "the second retry should succeed" is lost, could we > add it to tdh_do_no_vcpus()? +1, definitely needs a comment. /* * Execute a SEAMCALL related to removing/blocking S-EPT entries, with a single * retry (if necessary) after forcing vCPUs to exit and wait for the operation * to complete. All flows that remove/block S-EPT entries run with mmu_lock * held for write, i.e. are mutually exlusive with each other, but they aren't * mutually exclusive with vCPUs running (because that would be overkill), and * so can fail with "operand busy" if a vCPU acquires a required lock in the * TDX-Module. * * Note, the retry is guaranteed to succeed, absent KVM and/or TDX-Module bugs. */ > Also, perhaps we can just TDX_BUG_ON() inside tdh_do_no_vcpus() when the > second call of tdh_func() fails? Heh, this also caught my eye when typing up the comment. Unfortunately, I don't think it's worth doing the TDX_BUG_ON() inside the macro as that would require plumbing in the UPPERCASE name, and doesn't work well with the variadic arguments, e.g. TRACK wants TDX_BUG_ON(), but REMOVE and BLOCK want TDX_BUG_ON_2(). Given that REMOVE and BLOCK need to check the return value, getting the TDX_BUG_ON() call into the macro wouldn't buy that much. -- kvm-riscv mailing list kvm-riscv@lists.infradead.org http://lists.infradead.org/mailman/listinfo/kvm-riscv From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pl1-f202.google.com (mail-pl1-f202.google.com [209.85.214.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 71AAD2E7F05 for ; Mon, 27 Oct 2025 19:20:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.202 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1761592815; cv=none; b=mDk822Y9gpNU1p2zE0cdyOEh1KuyNW+juKWShdRj3bLyxnVGSHjXOX13oocKa6PqVO35vTSd89Dje9FOERYL1twrALoHSNtGTZIGz6p959woW3bzX1TBiDJDaw4fhTV/l0/cWUixq7cJ6x9WX8U6zMek5lSDctAiY/uRlZ/Xq+M= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1761592815; c=relaxed/simple; bh=VJkG8MUTQ0ibSOiw5y5AGTqWzk7c71W2hsTJzLp4sxo=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=PStisOYyKAkVbQO7t+/Zk8CeHWUvoty0qHXz2uxb1EtfdMebvw91c+wBp75pYzHqyCn5EI7hKjslkdKsoqduyw7iqRvw9DreRZfGQZpiMpAMybxjSWLoP1jNU6q587guPsAhaTr8ZEjrb78QLO22yEe/C9ru4TSJSR/omgxkAlE= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=Qhi4iDs3; arc=none smtp.client-ip=209.85.214.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="Qhi4iDs3" Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-28c58e009d1so108170565ad.3 for ; Mon, 27 Oct 2025 12:20:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1761592812; x=1762197612; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=GJ/ihzhnBg2h+OIog5s51f1nDJzu1yBHwy2SCCUQ1bo=; b=Qhi4iDs3Myf7H67dWp307gfFjDizFJCEizyOsUEySFxshjwcSeehuNjbPejwImqCWd jzUlvlknzkXQhBXzZZcaeuYP+0OU5UnTI4zxVrDIjfyyFgmq5gBhla+VLLnUA3Hf4LPH NJx6jGnhvDJ0sgsodj3jWbNYA8xDXNCXn/I3uJDMW1Kd2+I6VvIfz+LhUPSH7lx91wON P0WGzlbhTW6izGpmxd3x5XT3BGVm5TgGDpi+H25OfQwgpdU69LtZdQglplOsQAbMl3ma 9X1S1r2LBTZlm1FxhaeTAu9kEOVzXMaBtFDmea6TVnVrOf0G6b5Wm6VYiM1vONQP4qXa SgOg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1761592812; x=1762197612; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=GJ/ihzhnBg2h+OIog5s51f1nDJzu1yBHwy2SCCUQ1bo=; b=CnX0nYCRNZFiZgMPqYe2pO5RnpS8FvUn41KGkAjK0GFLDLyvamMUfMkvJvW58TsrY8 K5RiOpPl0v9124NLsK4JroCgRyuGqMQ3QzhAT5pN55EpJtSsTE7YBwmcjppHjptQ0xC3 PfvV2mDk+GRDIJRKrR9jkxevOkjU7ZQ1aBlp29XQRani1xpKsEPnh4kMWuGMvVQgmY5r i9c4sksxD5fLqQpxKSIZ5haJVR5EamY7kj+hadEaNlvLPytXjyiojHEY6x8lmDC/SCSg y8s3R/oXQVq4AYAJmzPUyhtInWkqI2OR6ec11q/v5pmEn+PTctJ/96do0XWR0qYQo6AP gR+Q== X-Forwarded-Encrypted: i=1; AJvYcCUs7Y1o7xwkSJSahx874IOTSHGVbgi85TJZ9sM6jbjKsbq7hgOdBk/RgTTNsyFrcOqdGbY=@vger.kernel.org X-Gm-Message-State: AOJu0YzddSAmgpzFbZSEjzJApPC5DlitMeO4cJB1vWuSWt7DbCQfe5MB XzzdABuGrfnMz0d9wNU0MjT5sGBI55bItubagYwFKrAiUtbd8jOzP4DW76GLSN/i9pdrjLJugJZ he+J2bQ== X-Google-Smtp-Source: AGHT+IGEzxKOsu0TIx9JeJ8jo0iJqVMcVQCTPniC3ps1TgJ3BGzDUda18OgqbILlA0+mvKP7+G2jd9/+Iec= X-Received: from pjbrs15.prod.google.com ([2002:a17:90b:2b8f:b0:33b:51fe:1a73]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:902:e5c4:b0:25d:1640:1d59 with SMTP id d9443c01a7336-294cb378610mr8002455ad.8.1761592811798; Mon, 27 Oct 2025 12:20:11 -0700 (PDT) Date: Mon, 27 Oct 2025 12:20:10 -0700 In-Reply-To: <4809644b0ba02d0987ac56f4be7c426d0724cdef.camel@intel.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20251017003244.186495-1-seanjc@google.com> <20251017003244.186495-21-seanjc@google.com> <4809644b0ba02d0987ac56f4be7c426d0724cdef.camel@intel.com> Message-ID: Subject: Re: [PATCH v3 20/25] KVM: TDX: Add macro to retry SEAMCALLs when forcing vCPUs out of guest From: Sean Christopherson To: Kai Huang Cc: "chenhuacai@kernel.org" , "frankja@linux.ibm.com" , "maz@kernel.org" , "borntraeger@linux.ibm.com" , "pjw@kernel.org" , "aou@eecs.berkeley.edu" , "kas@kernel.org" , "maobibo@loongson.cn" , "pbonzini@redhat.com" , "maddy@linux.ibm.com" , "palmer@dabbelt.com" , "imbrenda@linux.ibm.com" , "zhaotianrui@loongson.cn" , "anup@brainfault.org" , "oliver.upton@linux.dev" , "kvm@vger.kernel.org" , "linux-coco@lists.linux.dev" , Yan Y Zhao , "michael.roth@amd.com" , "linux-kernel@vger.kernel.org" , Ira Weiny , "loongarch@lists.linux.dev" , "binbin.wu@linux.intel.com" , "ackerleytng@google.com" , "kvmarm@lists.linux.dev" , "kvm-riscv@lists.infradead.org" , Vishal Annapurve , "linuxppc-dev@lists.ozlabs.org" , Rick P Edgecombe , "linux-mips@vger.kernel.org" , "linux-arm-kernel@lists.infradead.org" , "linux-riscv@lists.infradead.org" , "x86@kernel.org" Content-Type: text/plain; charset="us-ascii" On Fri, Oct 24, 2025, Kai Huang wrote: > On Thu, 2025-10-16 at 17:32 -0700, Sean Christopherson wrote: > > Add a macro to handle kicking vCPUs out of the guest and retrying > > SEAMCALLs on -EBUSY instead of providing small helpers to be used by each > > SEAMCALL. Wrapping the SEAMCALLs in a macro makes it a little harder to > > tease out which SEAMCALL is being made, but significantly reduces the > > amount of copy+paste code and makes it all but impossible to leave an > > elevated wait_for_sept_zap. > > > > Signed-off-by: Sean Christopherson > > --- > > arch/x86/kvm/vmx/tdx.c | 72 ++++++++++++++---------------------------- > > 1 file changed, 23 insertions(+), 49 deletions(-) > > > > diff --git a/arch/x86/kvm/vmx/tdx.c b/arch/x86/kvm/vmx/tdx.c > > index f6782b0ffa98..2e2dab89c98f 100644 > > --- a/arch/x86/kvm/vmx/tdx.c > > +++ b/arch/x86/kvm/vmx/tdx.c > > @@ -294,25 +294,24 @@ static inline void tdx_disassociate_vp(struct kvm_vcpu *vcpu) > > vcpu->cpu = -1; > > } > > > > -static void tdx_no_vcpus_enter_start(struct kvm *kvm) > > -{ > > - struct kvm_tdx *kvm_tdx = to_kvm_tdx(kvm); > > - > > - lockdep_assert_held_write(&kvm->mmu_lock); > > - > > - WRITE_ONCE(kvm_tdx->wait_for_sept_zap, true); > > - > > - kvm_make_all_cpus_request(kvm, KVM_REQ_OUTSIDE_GUEST_MODE); > > -} > > - > > -static void tdx_no_vcpus_enter_stop(struct kvm *kvm) > > -{ > > - struct kvm_tdx *kvm_tdx = to_kvm_tdx(kvm); > > - > > - lockdep_assert_held_write(&kvm->mmu_lock); > > - > > - WRITE_ONCE(kvm_tdx->wait_for_sept_zap, false); > > -} > > +#define tdh_do_no_vcpus(tdh_func, kvm, args...) \ > > +({ \ > > + struct kvm_tdx *__kvm_tdx = to_kvm_tdx(kvm); \ > > + u64 __err; \ > > + \ > > + lockdep_assert_held_write(&kvm->mmu_lock); \ > > + \ > > + __err = tdh_func(args); \ > > + if (unlikely(tdx_operand_busy(__err))) { \ > > + WRITE_ONCE(__kvm_tdx->wait_for_sept_zap, true); \ > > + kvm_make_all_cpus_request(kvm, KVM_REQ_OUTSIDE_GUEST_MODE); \ > > + \ > > + __err = tdh_func(args); \ > > + \ > > + WRITE_ONCE(__kvm_tdx->wait_for_sept_zap, false); \ > > + } \ > > + __err; \ > > +}) > > The comment which says "the second retry should succeed" is lost, could we > add it to tdh_do_no_vcpus()? +1, definitely needs a comment. /* * Execute a SEAMCALL related to removing/blocking S-EPT entries, with a single * retry (if necessary) after forcing vCPUs to exit and wait for the operation * to complete. All flows that remove/block S-EPT entries run with mmu_lock * held for write, i.e. are mutually exlusive with each other, but they aren't * mutually exclusive with vCPUs running (because that would be overkill), and * so can fail with "operand busy" if a vCPU acquires a required lock in the * TDX-Module. * * Note, the retry is guaranteed to succeed, absent KVM and/or TDX-Module bugs. */ > Also, perhaps we can just TDX_BUG_ON() inside tdh_do_no_vcpus() when the > second call of tdh_func() fails? Heh, this also caught my eye when typing up the comment. Unfortunately, I don't think it's worth doing the TDX_BUG_ON() inside the macro as that would require plumbing in the UPPERCASE name, and doesn't work well with the variadic arguments, e.g. TRACK wants TDX_BUG_ON(), but REMOVE and BLOCK want TDX_BUG_ON_2(). Given that REMOVE and BLOCK need to check the return value, getting the TDX_BUG_ON() call into the macro wouldn't buy that much. 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id AA84DCCF9EB for ; Mon, 27 Oct 2025 19:20:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:From:Subject:Message-ID: References:Mime-Version:In-Reply-To:Date:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=T0Cx6lW38b3kDtq2VYHZqGjnH9x0s+0zAIqQ563UIyE=; b=xaTlp7An47GlkThYirGe07GLR1 hEMWDwx17II0pmVShBuFKFG5ftOUHSPF/fcDl5UwTMG3kJ1jbPFF71xP1tJdI+yiOouRaKS5wHDrx 0e56yiYa5EgsZ1ZSoFEEkHtLfDqd/4Y1yP09xKlIZ88xSyCchdYXgtFNChGoypj2q1CczLVYqndzk X2eOa3mlvpNtLA+oC80+5KX0hGncpQDNhcRH47Ga+sgsuE4u7hbnxK8EjQQdn7OH9exFwBeyZ9TUh ytnRLaDibaWRbwcjdR7nlj5gMdbhCpvfpyW449VlnR5VA/21P+1bSlcYll5J7WpV+/DleJmQ7Cqym u9gkYl/g==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98.2 #2 (Red Hat Linux)) id 1vDSlL-0000000EeJr-2qkf; Mon, 27 Oct 2025 19:20:15 +0000 Received: from mail-pl1-x64a.google.com ([2607:f8b0:4864:20::64a]) by bombadil.infradead.org with esmtps (Exim 4.98.2 #2 (Red Hat Linux)) id 1vDSlJ-0000000EeIP-1cMW for linux-riscv@lists.infradead.org; Mon, 27 Oct 2025 19:20:14 +0000 Received: by mail-pl1-x64a.google.com with SMTP id d9443c01a7336-294880e7ca4so85603485ad.0 for ; Mon, 27 Oct 2025 12:20:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1761592812; x=1762197612; darn=lists.infradead.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=GJ/ihzhnBg2h+OIog5s51f1nDJzu1yBHwy2SCCUQ1bo=; b=n4y/XYJpxZT47gkE17oanNSpUzIHRSa8/lHwHWyIGnDreQveRJsc9TtirCII2alHmC C6wB9mu16rlgSy288A4eyouvM/QgvGv+4/NYOB+vIW/3mKavVFZmg4QO+bWjPGRszmRm yVge6w1sfoiBdWthgMmrQAdn1EZlE2BYgpIPaptncXr1zyffos/a8m5b54iXDqHBB5Nq aGCnvFGxUFS7leQAgnn/qFlD69PjLBF38v8UeDeYX/TsNK5CXhalbIg+393nXn6bBESY UIAKzaEN5fpAoVzv3mRgTN3BuZH+xwNDLOHKDJ7tg5l2Ozws+u2qsWXKcRpbXm7mmQUS kcQw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1761592812; x=1762197612; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=GJ/ihzhnBg2h+OIog5s51f1nDJzu1yBHwy2SCCUQ1bo=; b=BBypSQew+VJq8r4seT3jcUzbDfOGUPDdL7YkJtbIP0wTulSNcH4NPEYKph0OddV+NQ ZeTFaprOoMsmaNRW3CfA8IIv0/cspbbODBalqb1aWfWfSb+qsQX0VMre3CCJaJUedOFL zQzc03L9fPis+2t3gDZNdPZP7XCUmMlhQVyNoD6lSk76dPHRg7L9G5zzyhTQYjkKaJo+ hDU+C2onkckGr/KuHXGg12FbXuKTZ4lJ9oJiEWYXMq9nOtunt9WeHbrDkVTkn6gsd4oV TwNnt/BAKfw+pmM67oCZn+6KdnYJ5iw0gZRlUcxOuUhbNXTg/OL31kJwlxUU1+K3ZaJ1 Cp8g== X-Forwarded-Encrypted: i=1; AJvYcCUWzzvbtXUL4bn2AlM+H6IPSzI962QlU1NcBaZuXrUsqLCYJQUNkhd9L0HjnntrnBlBOLuC29kay9nBqg==@lists.infradead.org X-Gm-Message-State: AOJu0Yw+/PL0qpxBhSNqAweJg1MFdwWT800XJLFBJ3ILlYGlCg8NKewy /mSsdcrAiwEKezdsecjLwBlnAeQBqxAnlXqEjhWFCBUdHmYsnNsmxv+kj2Ju3/VBmheePTS1BlF +tv9VIg== X-Google-Smtp-Source: AGHT+IGEzxKOsu0TIx9JeJ8jo0iJqVMcVQCTPniC3ps1TgJ3BGzDUda18OgqbILlA0+mvKP7+G2jd9/+Iec= X-Received: from pjbrs15.prod.google.com ([2002:a17:90b:2b8f:b0:33b:51fe:1a73]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:902:e5c4:b0:25d:1640:1d59 with SMTP id d9443c01a7336-294cb378610mr8002455ad.8.1761592811798; Mon, 27 Oct 2025 12:20:11 -0700 (PDT) Date: Mon, 27 Oct 2025 12:20:10 -0700 In-Reply-To: <4809644b0ba02d0987ac56f4be7c426d0724cdef.camel@intel.com> Mime-Version: 1.0 References: <20251017003244.186495-1-seanjc@google.com> <20251017003244.186495-21-seanjc@google.com> <4809644b0ba02d0987ac56f4be7c426d0724cdef.camel@intel.com> Message-ID: Subject: Re: [PATCH v3 20/25] KVM: TDX: Add macro to retry SEAMCALLs when forcing vCPUs out of guest From: Sean Christopherson To: Kai Huang Cc: "chenhuacai@kernel.org" , "frankja@linux.ibm.com" , "maz@kernel.org" , "borntraeger@linux.ibm.com" , "pjw@kernel.org" , "aou@eecs.berkeley.edu" , "kas@kernel.org" , "maobibo@loongson.cn" , "pbonzini@redhat.com" , "maddy@linux.ibm.com" , "palmer@dabbelt.com" , "imbrenda@linux.ibm.com" , "zhaotianrui@loongson.cn" , "anup@brainfault.org" , "oliver.upton@linux.dev" , "kvm@vger.kernel.org" , "linux-coco@lists.linux.dev" , Yan Y Zhao , "michael.roth@amd.com" , "linux-kernel@vger.kernel.org" , Ira Weiny , "loongarch@lists.linux.dev" , "binbin.wu@linux.intel.com" , "ackerleytng@google.com" , "kvmarm@lists.linux.dev" , "kvm-riscv@lists.infradead.org" , Vishal Annapurve , "linuxppc-dev@lists.ozlabs.org" , Rick P Edgecombe , "linux-mips@vger.kernel.org" , "linux-arm-kernel@lists.infradead.org" , "linux-riscv@lists.infradead.org" , "x86@kernel.org" X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20251027_122013_453353_DDAEA527 X-CRM114-Status: GOOD ( 23.31 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org On Fri, Oct 24, 2025, Kai Huang wrote: > On Thu, 2025-10-16 at 17:32 -0700, Sean Christopherson wrote: > > Add a macro to handle kicking vCPUs out of the guest and retrying > > SEAMCALLs on -EBUSY instead of providing small helpers to be used by each > > SEAMCALL. Wrapping the SEAMCALLs in a macro makes it a little harder to > > tease out which SEAMCALL is being made, but significantly reduces the > > amount of copy+paste code and makes it all but impossible to leave an > > elevated wait_for_sept_zap. > > > > Signed-off-by: Sean Christopherson > > --- > > arch/x86/kvm/vmx/tdx.c | 72 ++++++++++++++---------------------------- > > 1 file changed, 23 insertions(+), 49 deletions(-) > > > > diff --git a/arch/x86/kvm/vmx/tdx.c b/arch/x86/kvm/vmx/tdx.c > > index f6782b0ffa98..2e2dab89c98f 100644 > > --- a/arch/x86/kvm/vmx/tdx.c > > +++ b/arch/x86/kvm/vmx/tdx.c > > @@ -294,25 +294,24 @@ static inline void tdx_disassociate_vp(struct kvm_vcpu *vcpu) > > vcpu->cpu = -1; > > } > > > > -static void tdx_no_vcpus_enter_start(struct kvm *kvm) > > -{ > > - struct kvm_tdx *kvm_tdx = to_kvm_tdx(kvm); > > - > > - lockdep_assert_held_write(&kvm->mmu_lock); > > - > > - WRITE_ONCE(kvm_tdx->wait_for_sept_zap, true); > > - > > - kvm_make_all_cpus_request(kvm, KVM_REQ_OUTSIDE_GUEST_MODE); > > -} > > - > > -static void tdx_no_vcpus_enter_stop(struct kvm *kvm) > > -{ > > - struct kvm_tdx *kvm_tdx = to_kvm_tdx(kvm); > > - > > - lockdep_assert_held_write(&kvm->mmu_lock); > > - > > - WRITE_ONCE(kvm_tdx->wait_for_sept_zap, false); > > -} > > +#define tdh_do_no_vcpus(tdh_func, kvm, args...) \ > > +({ \ > > + struct kvm_tdx *__kvm_tdx = to_kvm_tdx(kvm); \ > > + u64 __err; \ > > + \ > > + lockdep_assert_held_write(&kvm->mmu_lock); \ > > + \ > > + __err = tdh_func(args); \ > > + if (unlikely(tdx_operand_busy(__err))) { \ > > + WRITE_ONCE(__kvm_tdx->wait_for_sept_zap, true); \ > > + kvm_make_all_cpus_request(kvm, KVM_REQ_OUTSIDE_GUEST_MODE); \ > > + \ > > + __err = tdh_func(args); \ > > + \ > > + WRITE_ONCE(__kvm_tdx->wait_for_sept_zap, false); \ > > + } \ > > + __err; \ > > +}) > > The comment which says "the second retry should succeed" is lost, could we > add it to tdh_do_no_vcpus()? +1, definitely needs a comment. /* * Execute a SEAMCALL related to removing/blocking S-EPT entries, with a single * retry (if necessary) after forcing vCPUs to exit and wait for the operation * to complete. All flows that remove/block S-EPT entries run with mmu_lock * held for write, i.e. are mutually exlusive with each other, but they aren't * mutually exclusive with vCPUs running (because that would be overkill), and * so can fail with "operand busy" if a vCPU acquires a required lock in the * TDX-Module. * * Note, the retry is guaranteed to succeed, absent KVM and/or TDX-Module bugs. */ > Also, perhaps we can just TDX_BUG_ON() inside tdh_do_no_vcpus() when the > second call of tdh_func() fails? Heh, this also caught my eye when typing up the comment. Unfortunately, I don't think it's worth doing the TDX_BUG_ON() inside the macro as that would require plumbing in the UPPERCASE name, and doesn't work well with the variadic arguments, e.g. TRACK wants TDX_BUG_ON(), but REMOVE and BLOCK want TDX_BUG_ON_2(). Given that REMOVE and BLOCK need to check the return value, getting the TDX_BUG_ON() call into the macro wouldn't buy that much. _______________________________________________ linux-riscv mailing list linux-riscv@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-riscv