From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pl1-f201.google.com (mail-pl1-f201.google.com [209.85.214.201]) (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 97A5D31C584 for ; Mon, 27 Oct 2025 19:20:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1761592816; cv=none; b=WmfCjiVhoIkasCS81eU30L2jsFgGhuCre/MqiTqU7Ou8f9jn3eMx4fpuOo+dh5lMfHHjG6E2emUQu23OQ3RCUsqgsJoUS3Lk1yiSSDKeVsSIGAIz4saEgORg4hJBFqtHZbiZY23AL+um1fS3r7Y9cJiTW5ETsxKhq6OrgUgiCEc= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1761592816; c=relaxed/simple; bh=VJkG8MUTQ0ibSOiw5y5AGTqWzk7c71W2hsTJzLp4sxo=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=l1RilDro10r+G10i8h5ivcy7XU1u4q4oT7sWZ0kWZXnkSm7h5InnMDZXbtbcLpDHlCgL54ZGSgG4jap1MISiFytLUddqiNg9jYtctN3i021ftMHS0mnNIRl9OTtsbrb1ZogH/HlFxliopF/lM9NlVFKQuFEntkCHMWFL9JWyRNQ= 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.201 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-f201.google.com with SMTP id d9443c01a7336-28c58e009d1so108170555ad.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=tfBIWPPEhtn6kSYQe6P4fvomoB+4+0XOEVcUpNVUOpRAPqLsPXsxzczBGv77f6j1EH uiHfKGahqa7lNgMk3f8TJF74zYEGbt+H40ZaiCS+94WOcm//piK2/04AehpZ6GH4LXrS nkozholP7zPOv9x3SU5HvGyhEmErQDdrcZ3Pkrhu79iDhRxWtDPcRKBrAEKecGgfC6OY /b/yUXlTA4nLBz+RUtuKRkNz1z1lFOvzUBTVMP8mG7u3Tgof2LyYqtR+97ufQ0vwYB3U ek2q/onlyjsaGWvISJ2qf8nXtB7Qs7WDMzxV29NkcnL1DW4QyBLcWNRkNVUwNWZv0CzT 1qNw== X-Forwarded-Encrypted: i=1; AJvYcCWsfPqf+OB7YOaezhMXNbCP9qhFL44D6+ukkyMqVFvO9qbllJbQ2e2fYXaVgeWBkbLn1LPlyrEx6y4e@vger.kernel.org X-Gm-Message-State: AOJu0Yzv5RlICZzVG15LVoOH6jfmq0QvJvJBT9hxU5DgWht033TaBKF2 LtI4/6MIyhicVvd66pzisUGXaJzGrILTbK9pzAVNJpS3znCGbT7aZWUDLkpxANygtWGp61OzhJB UejJWrQ== 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: linux-mips@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.