From: Yan Zhao <yan.y.zhao@intel.com>
To: Paolo Bonzini <pbonzini@redhat.com>
Cc: <linux-kernel@vger.kernel.org>, <kvm@vger.kernel.org>,
<seanjc@google.com>, Rick Edgecombe <rick.p.edgecombe@intel.com>,
"Isaku Yamahata" <isaku.yamahata@intel.com>,
Tony Lindgren <tony.lindgren@linux.intel.com>,
Sean Christopherson <sean.j.christopherson@intel.com>,
Kai Huang <kai.huang@intel.com>
Subject: Re: [PATCH 20/30] KVM: TDX: create/destroy VM structure
Date: Fri, 21 Feb 2025 20:25:47 +0800 [thread overview]
Message-ID: <Z7hwy1K00fqBkUrK@yzhao56-desk.sh.intel.com> (raw)
In-Reply-To: <20250220170604.2279312-21-pbonzini@redhat.com>
> +/* TDH.PHYMEM.PAGE.RECLAIM is allowed only when destroying the TD. */
> +static int __tdx_reclaim_page(struct page *page)
> +{
> + u64 err, rcx, rdx, r8;
> + int i;
> +
> + for (i = TDX_SEAMCALL_RETRIES; i > 0; i--) {
Yes, the retries can be dropped according to the previous analysis at
https://lore.kernel.org/kvm/ZyMAD0tSZiadZ%2FYx@yzhao56-desk.sh.intel.com.
Currently, all TD pages are 4K and even freed before the hkid is released,
before the control of tdh_phymem_page_reclaim().
Non-TD control pages are also 4K and are guaranteed to be reclaimed before TDR
pages are reclaimed.
So, for basic TDX, contentions in tdh_phymem_page_reclaim() are not expected.
> + err = tdh_phymem_page_reclaim(page, &rcx, &rdx, &r8);
> +
> + /*
> + * TDH.PHYMEM.PAGE.RECLAIM is allowed only when TD is shutdown.
> + * state. i.e. destructing TD.
> + * TDH.PHYMEM.PAGE.RECLAIM requires TDR and target page.
> + * Because we're destructing TD, it's rare to contend with TDR.
> + */
> + switch (err) {
> + case TDX_OPERAND_BUSY | TDX_OPERAND_ID_RCX:
> + case TDX_OPERAND_BUSY | TDX_OPERAND_ID_TDR:
> + cond_resched();
> + continue;
> + default:
> + goto out;
> + }
> + }
> +
> +out:
> + if (WARN_ON_ONCE(err)) {
> + pr_tdx_error_3(TDH_PHYMEM_PAGE_RECLAIM, err, rcx, rdx, r8);
> + return -EIO;
> + }
> + return 0;
> +}
> +
...
> +static void smp_func_do_phymem_cache_wb(void *unused)
> +{
> + u64 err = 0;
> + bool resume;
> + int i;
> +
> + /*
> + * TDH.PHYMEM.CACHE.WB flushes caches associated with any TDX private
> + * KeyID on the package or core. The TDX module may not finish the
> + * cache flush but return TDX_INTERRUPTED_RESUMEABLE instead. The
> + * kernel should retry it until it returns success w/o rescheduling.
> + */
> + for (i = TDX_SEAMCALL_RETRIES; i > 0; i--) {
> + resume = !!err;
> + err = tdh_phymem_cache_wb(resume);
> + switch (err) {
> + case TDX_INTERRUPTED_RESUMABLE:
> + continue;
These retries may not be removable as tdh_phymem_cache_wb() is an interruptible
and restartable function. If a pending interrupt is detected during operation,
tdh_phymem_cache_wb() returns with a TDX_INTERRUPED_RESUMABLE status in RAX.
KVM needs complete the cache write-back operation by resuming
tdh_phymem_cache_wb().
> + case TDX_NO_HKID_READY_TO_WBCACHE:
> + err = TDX_SUCCESS; /* Already done by other thread */
> + fallthrough;
> + default:
> + goto out;
> + }
> + }
> +
> +out:
> + if (WARN_ON_ONCE(err))
> + pr_tdx_error(TDH_PHYMEM_CACHE_WB, err);
> +}
> +
> +void tdx_mmu_release_hkid(struct kvm *kvm)
> +{
> + bool packages_allocated, targets_allocated;
> + struct kvm_tdx *kvm_tdx = to_kvm_tdx(kvm);
> + cpumask_var_t packages, targets;
> + u64 err;
> + int i;
> +
> + if (!is_hkid_assigned(kvm_tdx))
> + return;
> +
> + /* KeyID has been allocated but guest is not yet configured */
> + if (!kvm_tdx->td.tdr_page) {
> + tdx_hkid_free(kvm_tdx);
> + return;
> + }
> +
> + packages_allocated = zalloc_cpumask_var(&packages, GFP_KERNEL);
> + targets_allocated = zalloc_cpumask_var(&targets, GFP_KERNEL);
> + cpus_read_lock();
> +
> + /*
> + * TDH.PHYMEM.CACHE.WB tries to acquire the TDX module global lock
> + * and can fail with TDX_OPERAND_BUSY when it fails to get the lock.
> + * Multiple TDX guests can be destroyed simultaneously. Take the
> + * mutex to prevent it from getting error.
> + */
> + mutex_lock(&tdx_lock);
> +
> + /*
> + * Releasing HKID is in vm_destroy().
> + * After the above flushing vps, there should be no more vCPU
> + * associations, as all vCPU fds have been released at this stage.
> + */
> + for_each_online_cpu(i) {
> + if (packages_allocated &&
> + cpumask_test_and_set_cpu(topology_physical_package_id(i),
> + packages))
> + continue;
> + if (targets_allocated)
> + cpumask_set_cpu(i, targets);
> + }
> + if (targets_allocated)
> + on_each_cpu_mask(targets, smp_func_do_phymem_cache_wb, NULL, true);
> + else
> + on_each_cpu(smp_func_do_phymem_cache_wb, NULL, true);
If either packages_allocated or targets_allocated is false,
tdh_phymem_cache_wb() will be executed on each core. Then TDX_OPERAND_BUSY is
possible since tdh_phymem_cache_wb() needs to acquire a per package wbt_entries.
So, should we add the retries for this rare case or just simply leave it to the
WARN_ON()?
> + /*
> + * In the case of error in smp_func_do_phymem_cache_wb(), the following
> + * tdh_mng_key_freeid() will fail.
> + */
> + err = tdh_mng_key_freeid(&kvm_tdx->td);
> + if (KVM_BUG_ON(err, kvm)) {
> + pr_tdx_error(TDH_MNG_KEY_FREEID, err);
> + pr_err("tdh_mng_key_freeid() failed. HKID %d is leaked.\n",
> + kvm_tdx->hkid);
> + } else {
> + tdx_hkid_free(kvm_tdx);
> + }
> +
> + mutex_unlock(&tdx_lock);
> + cpus_read_unlock();
> + free_cpumask_var(targets);
> + free_cpumask_var(packages);
> +}
> +
next prev parent reply other threads:[~2025-02-21 12:27 UTC|newest]
Thread overview: 50+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-02-20 17:05 [PATCH v3 00/30] TDX initialization + vCPU/VM creation Paolo Bonzini
2025-02-20 17:05 ` [PATCH 01/30] x86/virt/tdx: Add SEAMCALL wrappers for TDX KeyID management Paolo Bonzini
2025-02-20 17:05 ` [PATCH 02/30] x86/virt/tdx: Add SEAMCALL wrappers for TDX TD creation Paolo Bonzini
2025-02-20 17:05 ` [PATCH 03/30] x86/virt/tdx: Add SEAMCALL wrappers for TDX vCPU creation Paolo Bonzini
2025-02-20 17:05 ` [PATCH 04/30] x86/virt/tdx: Add SEAMCALL wrappers for TDX page cache management Paolo Bonzini
2025-02-20 17:05 ` [PATCH 05/30] x86/virt/tdx: Add SEAMCALL wrappers for TDX VM/vCPU field access Paolo Bonzini
2025-02-20 17:05 ` [PATCH 06/30] x86/virt/tdx: Add SEAMCALL wrappers for TDX flush operations Paolo Bonzini
2025-02-20 17:05 ` [PATCH 07/30] x86/virt/tdx: allocate tdx_sys_info in static memory Paolo Bonzini
2025-02-20 21:59 ` Huang, Kai
2025-02-20 23:37 ` Edgecombe, Rick P
2025-02-20 17:05 ` [PATCH 08/30] x86/virt/tdx: Read essential global metadata for KVM Paolo Bonzini
2025-02-20 17:05 ` [PATCH 09/30] x86/virt/tdx: Add tdx_guest_keyid_alloc/free() to alloc and free TDX guest KeyID Paolo Bonzini
2025-02-20 17:05 ` [PATCH 10/30] KVM: Export hardware virtualization enabling/disabling functions Paolo Bonzini
2025-02-20 17:05 ` [PATCH 11/30] KVM: VMX: Refactor VMX module init/exit functions Paolo Bonzini
2025-02-20 21:55 ` Huang, Kai
2025-02-20 17:05 ` [PATCH 12/30] KVM: VMX: Initialize TDX during KVM module load Paolo Bonzini
2025-02-20 23:27 ` Huang, Kai
2025-02-24 18:57 ` Paolo Bonzini
2025-02-24 21:31 ` Huang, Kai
2025-02-20 17:05 ` [PATCH 13/30] KVM: TDX: Get TDX global information Paolo Bonzini
2025-02-21 0:12 ` Huang, Kai
2025-02-20 17:05 ` [PATCH 14/30] KVM: TDX: Add placeholders for TDX VM/vCPU structures Paolo Bonzini
2025-02-20 17:05 ` [PATCH 15/30] KVM: TDX: Define TDX architectural definitions Paolo Bonzini
2025-02-20 17:05 ` [PATCH 16/30] KVM: TDX: Add TDX "architectural" error codes Paolo Bonzini
2025-02-20 17:05 ` [PATCH 17/30] KVM: TDX: Add helper functions to print TDX SEAMCALL error Paolo Bonzini
2025-02-20 17:05 ` [PATCH 18/30] KVM: TDX: Add place holder for TDX VM specific mem_enc_op ioctl Paolo Bonzini
2025-02-25 10:50 ` Huang, Kai
2025-02-20 17:05 ` [PATCH 19/30] KVM: TDX: Get system-wide info about TDX module on initialization Paolo Bonzini
2025-02-20 17:05 ` [PATCH 20/30] KVM: TDX: create/destroy VM structure Paolo Bonzini
2025-02-21 0:55 ` Sean Christopherson
2025-02-21 1:08 ` Sean Christopherson
2025-02-22 0:30 ` Edgecombe, Rick P
2025-02-22 1:38 ` Sean Christopherson
2025-02-24 8:32 ` Yan Zhao
2025-02-21 11:04 ` Yan Zhao
2025-02-21 19:43 ` Sean Christopherson
2025-02-21 12:25 ` Yan Zhao [this message]
2025-02-25 16:24 ` Xiaoyao Li
2025-02-20 17:05 ` [PATCH 21/30] KVM: TDX: Support per-VM KVM_CAP_MAX_VCPUS extension check Paolo Bonzini
2025-02-20 17:05 ` [PATCH 22/30] KVM: x86: expose cpuid_entry2_find for TDX Paolo Bonzini
2025-02-20 17:05 ` [PATCH 23/30] KVM: TDX: initialize VM with TDX specific parameters Paolo Bonzini
2025-02-21 2:31 ` Xiaoyao Li
2025-02-25 17:28 ` Paolo Bonzini
2025-02-20 17:05 ` [PATCH 24/30] KVM: TDX: Make pmu_intel.c ignore guest TD case Paolo Bonzini
2025-02-20 17:05 ` [PATCH 25/30] KVM: TDX: Don't offline the last cpu of one package when there's TDX guest Paolo Bonzini
2025-02-20 17:06 ` [PATCH 26/30] KVM: TDX: create/free TDX vcpu structure Paolo Bonzini
2025-02-20 17:06 ` [PATCH 27/30] KVM: TDX: Do TDX specific vcpu initialization Paolo Bonzini
2025-02-20 17:06 ` [PATCH 28/30] KVM: x86: Introduce KVM_TDX_GET_CPUID Paolo Bonzini
2025-02-20 17:06 ` [PATCH 29/30] KVM: x86/mmu: Taking guest pa into consideration when calculate tdp level Paolo Bonzini
2025-02-20 17:06 ` [PATCH 30/30] KVM: TDX: Register TDX host key IDs to cgroup misc controller Paolo Bonzini
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=Z7hwy1K00fqBkUrK@yzhao56-desk.sh.intel.com \
--to=yan.y.zhao@intel.com \
--cc=isaku.yamahata@intel.com \
--cc=kai.huang@intel.com \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=pbonzini@redhat.com \
--cc=rick.p.edgecombe@intel.com \
--cc=sean.j.christopherson@intel.com \
--cc=seanjc@google.com \
--cc=tony.lindgren@linux.intel.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®