The Linux Kernel Mailing List
 help / color / mirror / Atom feed
From: Peter Fang <peter.fang@intel.com>
To: Dave Hansen <dave.hansen@linux.intel.com>,
	Kiryl Shutsemau <kas@kernel.org>,
	Rick Edgecombe <rick.p.edgecombe@intel.com>,
	"Sean Christopherson" <seanjc@google.com>,
	Paolo Bonzini <pbonzini@redhat.com>
Cc: Thomas Gleixner <tglx@kernel.org>, Ingo Molnar <mingo@redhat.com>,
	Borislav Petkov <bp@alien8.de>, <x86@kernel.org>,
	"H. Peter Anvin" <hpa@zytor.com>, <linux-kernel@vger.kernel.org>,
	<linux-coco@lists.linux.dev>, <kvm@vger.kernel.org>,
	Xu Yilun <yilun.xu@linux.intel.com>,
	Jason Gunthorpe <jgg@nvidia.com>,
	Tony Lindgren <tony.lindgren@linux.intel.com>,
	Chao Gao <chao.gao@intel.com>, Xiaoyao Li <xiaoyao.li@intel.com>,
	Binbin Wu <binbin.wu@linux.intel.com>,
	Sohil Mehta <sohil.mehta@intel.com>,
	Adrian Hunter <adrian.hunter@intel.com>,
	"Kishen Maloor" <kishen.maloor@intel.com>,
	Nikolay Borisov <nik.borisov@suse.com>,
	Elena Reshetova <elena.reshetova@intel.com>,
	Peter Fang <peter.fang@intel.com>
Subject: [PATCH v3 4/8] KVM: TDX: Add KVM_TDX_GET_QUOTE sub-ioctl for quoting
Date: Fri,  4 Dec 2026 10:18:25 -0800	[thread overview]
Message-ID: <20261204182022.324230-5-peter.fang@intel.com> (raw)
In-Reply-To: <20261204182022.324230-1-peter.fang@intel.com>

Add a new VM sub-ioctl, KVM_TDX_GET_QUOTE, so userspace can generate a
DICE-based Quote through the TDX-Module if it supports quoting.

To convert the guest's local report into a remotely verifiable Quote,
the host currently forwards the guest's GetQuote request to an SGX
Quoting Enclave in userspace. Now the host can also request a DICE-based
Quote from the TDX-Module. These Quotes leverage Intel's security
hardware, such as the S3M, so they work without SGX. The GetQuote flow
remains unchanged, so existing TDX setups can continue to work.

The TDX-Module needs its internal TD states to generate a Quote, so the
quoting SEAMCALL (TDH.QUOTE.GET) takes the TD's TDR page as input. Make
KVM_TDX_GET_QUOTE a TDX VM sub-ioctl accordingly.

Only allow quoting after the TD is finalized, so it doesn't bump into
the TD build ioctls. These ioctls use SEAMCALLs that take an exclusive
lock on the TD, so the quoting SEAMCALL can't take a shared lock at the
same time. Doing so could fail a build-time SEAMCALL and bug the VM.

Use a plain read for the TD state without tdx_vm_state_guard. The read
could race with the following:

  - KVM_TDX_INIT_VM: sets the state to initialized, so a racy read would
    never observe a finalized state. Updating the state only changes its
    lowest byte, so this is true even with a torn access.
  - KVM_TDX_FINALIZE_VM: the only place that sets the state to
    finalized, which is the stable state of a running TD.

Being a VM ioctl like KVM_TDX_FINALIZE_VM, KVM_TDX_GET_QUOTE's
observation of the TD state is governed by the VFS layer. Dropping and
grabbing a reference to the VM file have release and acquire semantics.
This guarantees that if userspace calls KVM_TDX_GET_QUOTE after
KVM_TDX_FINALIZE_VM returns, it must observe the state change fully. A
racy userspace implementation would at worst fail with -EINVAL, which
just means "not ready yet".

Add a mutex to allow one Quote request per TD at a time. This lets the
checks at TDX init guarantee that quoting sessions never run out when
there is no oversubscription of TDs. Each request has a unique "session
ID", so create a session ID allocator to support concurrent requests. To
keep the uABI predictable, always use the number of logical CPUs as the
session limit, since userspace has no way of knowing how many sessions
the TDX-Module actually supports. If userspace decides to oversubscribe
TDs, it must be prepared to handle -EBUSY, as KVM could run out of
quoting sessions in this case.

The TDX-Module takes quoting data blobs in pages. Rather than having to
support unaligned data in the uABI in perpetuity, require both the
report and the Quote blobs to be page-aligned from day one. This gives
KVM the freedom to change its implementation underneath later (e.g. pin
the report with GUP) without breaking userspace.

Pin the Quote buffer with GUP. DICE-based Quotes can reach several
megabytes in size [1], so using bounce buffers could at worst mean
gigabytes of kernel memory to shuffle data around on a large system.

Signed-off-by: Peter Fang <peter.fang@intel.com>
Link: https://lore.kernel.org/all/20260211001712.1531955-1-sathyanarayanan.kuppuswamy@linux.intel.com/ # [1]
---
v3:
 - Redesign the Quote generation path. Keep the GetQuote flow unchanged,
   and let userspace request a Quote with the new KVM_TDX_GET_QUOTE
   sub-ioctl. [Sean, Rick]
 - Allow one Quote request per TD at a time, and support concurrent
   requests across TDs. [Sean]
---
 arch/x86/include/uapi/asm/kvm.h |  12 +++
 arch/x86/kvm/vmx/tdx.c          | 152 ++++++++++++++++++++++++++++++++
 arch/x86/kvm/vmx/tdx.h          |   6 ++
 3 files changed, 170 insertions(+)

diff --git a/arch/x86/include/uapi/asm/kvm.h b/arch/x86/include/uapi/asm/kvm.h
index 00ce265391e11..9326f48c0d377 100644
--- a/arch/x86/include/uapi/asm/kvm.h
+++ b/arch/x86/include/uapi/asm/kvm.h
@@ -980,6 +980,7 @@ enum kvm_tdx_cmd_id {
 	KVM_TDX_INIT_MEM_REGION,
 	KVM_TDX_FINALIZE_VM,
 	KVM_TDX_GET_CPUID,
+	KVM_TDX_GET_QUOTE,
 
 	KVM_TDX_CMD_NR_MAX,
 };
@@ -1017,6 +1018,17 @@ struct kvm_tdx_capabilities {
 	struct kvm_cpuid2 cpuid;
 };
 
+struct kvm_tdx_get_quote {
+	__u64 report_uaddr;
+	__u64 report_size;
+
+	__u64 quote_buf_uaddr;
+	__u64 quote_buf_size;
+	__u64 quote_size;
+
+	__u64 reserved[27];
+};
+
 struct kvm_tdx_init_vm {
 	__u64 attributes;
 	__u64 xfam;
diff --git a/arch/x86/kvm/vmx/tdx.c b/arch/x86/kvm/vmx/tdx.c
index 4ccb41f002dff..704a267e9f8be 100644
--- a/arch/x86/kvm/vmx/tdx.c
+++ b/arch/x86/kvm/vmx/tdx.c
@@ -58,6 +58,8 @@ module_param_named(tdx, enable_tdx, bool, 0444);
 
 static const struct tdx_sys_info *tdx_sysinfo;
 
+static DEFINE_IDA(tdx_quoting_ida);
+
 void tdh_vp_rd_failed(struct vcpu_tdx *tdx, char *uclass, u32 field, u64 err)
 {
 	KVM_BUG_ON(1, tdx->vcpu.kvm);
@@ -658,6 +660,8 @@ int tdx_vm_init(struct kvm *kvm)
 	 */
 	kvm->max_vcpus = min_t(int, kvm->max_vcpus, num_present_cpus());
 
+	mutex_init(&kvm_tdx->quote_lock);
+
 	kvm_tdx->state = TD_STATE_UNINITIALIZED;
 
 	return 0;
@@ -2363,6 +2367,146 @@ static int tdx_get_capabilities(struct kvm_tdx_cmd *cmd)
 	return ret;
 }
 
+/*
+ * No need to take tdx_vm_state_guard here. Quoting takes TD locks as shared, so
+ * it doesn't run up against any other runtime SEAMCALLs.
+ */
+static int tdx_td_get_quote(struct kvm *kvm, struct kvm_tdx_cmd *cmd)
+{
+	struct kvm_tdx *kvm_tdx = to_kvm_tdx(kvm);
+	struct kvm_tdx_get_quote __user *ugq;
+	int num_quote_pages, ret, session_id;
+	struct kvm_tdx_get_quote gq;
+	struct page **quote_pages;
+	u16 sessions, threads;
+	u32 max_quote_size;
+	u64 quote_size;
+	void *report;
+
+	if (cmd->flags)
+		return -EINVAL;
+
+	/*
+	 * TDH.QUOTE.GET needs the TD locks, so avoid colliding with
+	 * build-time SEAMCALLs that lock the TD exclusively.
+	 *
+	 * The VFS syscall path guarantees that any VM ioctl issued after
+	 * KVM_TDX_FINALIZE_VM succeeds observes the state change fully. It also
+	 * ensures that tdx_vm_destroy() can't run and reset the state while
+	 * this call is in progress.
+	 */
+	if (kvm_tdx->state != TD_STATE_RUNNABLE)
+		return -EINVAL;
+
+	/* Grab the lock early to throttle the TD before pinning memory */
+	if (mutex_lock_killable(&kvm_tdx->quote_lock))
+		return -EINTR;
+
+	if (!tdx_quoting_enabled(&max_quote_size, &sessions, &threads)) {
+		ret = -EINVAL;
+		goto out_unlock;
+	}
+
+	ugq = u64_to_user_ptr(cmd->data);
+
+	if (copy_from_user(&gq, ugq, sizeof(gq))) {
+		ret = -EFAULT;
+		goto out_unlock;
+	}
+
+	if (memchr_inv(gq.reserved, 0, sizeof(gq.reserved))) {
+		ret = -EINVAL;
+		goto out_unlock;
+	}
+
+	/* The TDX-Module takes the report in a single 4K page */
+	if (!PAGE_ALIGNED(gq.report_uaddr) || gq.report_size > PAGE_SIZE) {
+		ret = -EINVAL;
+		goto out_unlock;
+	}
+
+	report = kmalloc(PAGE_SIZE, GFP_KERNEL);
+	if (!report) {
+		ret = -ENOMEM;
+		goto out_unlock;
+	}
+
+	if (copy_from_user(report, u64_to_user_ptr(gq.report_uaddr),
+			   gq.report_size)) {
+		ret = -EFAULT;
+		goto out_free_report;
+	}
+
+	/* Avoid pinning an unreasonable number of pages */
+	gq.quote_buf_size = umin(gq.quote_buf_size, max_quote_size);
+
+	if (!PAGE_ALIGNED(gq.quote_buf_uaddr) || !gq.quote_buf_size) {
+		ret = -EINVAL;
+		goto out_free_report;
+	}
+
+	num_quote_pages = PFN_UP(gq.quote_buf_size);
+
+	quote_pages = kmalloc_objs(*quote_pages, num_quote_pages);
+	if (!quote_pages) {
+		ret = -ENOMEM;
+		goto out_free_report;
+	}
+
+	ret = pin_user_pages_fast(gq.quote_buf_uaddr, num_quote_pages,
+				  FOLL_WRITE, quote_pages);
+	if (ret != num_quote_pages) {
+		if (ret > 0)
+			unpin_user_pages(quote_pages, ret);
+		ret = -ENOMEM;
+		goto out_free_quote;
+	}
+
+	/*
+	 * There is a 1:1 mapping between a quoting session in use and a
+	 * quoting thread in the TDX-Module. Since KVM guarantees there are at
+	 * least as many threads as logical CPUs, limit the number of sessions
+	 * to the same number to ensure there is always a free thread. Using
+	 * the module's own limit would make the ABI unpredictable.
+	 *
+	 * In a typical setup where TDs aren't oversubscribed, there is always a
+	 * free session since each TD runs at most one session at a time.
+	 * Otherwise, let userspace handle -EBUSY when sessions run out.
+	 */
+	session_id = ida_alloc_range(&tdx_quoting_ida, 0,
+				     num_present_cpus() - 1, GFP_KERNEL);
+	if (session_id == -ENOSPC)
+		session_id = -EBUSY;
+	if (session_id < 0) {
+		ret = session_id;
+		goto out_unpin_quote;
+	}
+
+	ret = tdx_quote_generate(&kvm_tdx->td, session_id, virt_to_page(report),
+				 gq.report_size, quote_pages, gq.quote_buf_size,
+				 &quote_size, &cmd->hw_error);
+
+	ida_free(&tdx_quoting_ida, session_id);
+
+out_unpin_quote:
+	/*
+	 * The TDX-Module bypasses the kernel's page tables when writing the
+	 * quote. Mark the pages dirty so reclaim doesn't drop them silently.
+	 */
+	unpin_user_pages_dirty_lock(quote_pages, num_quote_pages, true);
+out_free_quote:
+	kfree(quote_pages);
+out_free_report:
+	kfree(report);
+out_unlock:
+	mutex_unlock(&kvm_tdx->quote_lock);
+
+	if (ret)
+		return ret;
+
+	return put_user(quote_size, &ugq->quote_size);
+}
+
 /*
  * KVM reports guest physical address in CPUID.0x800000008.EAX[23:16], which is
  * similar to TDX's GPAW. Use this field as the interface for userspace to
@@ -2982,6 +3126,14 @@ int tdx_vm_ioctl(struct kvm *kvm, void __user *argp)
 	if (tdx_cmd.id == KVM_TDX_CAPABILITIES)
 		return tdx_get_capabilities(&tdx_cmd);
 
+	if (tdx_cmd.id == KVM_TDX_GET_QUOTE) {
+		r = tdx_td_get_quote(kvm, &tdx_cmd);
+		if (copy_to_user(argp, &tdx_cmd, sizeof(struct kvm_tdx_cmd)))
+			return -EFAULT;
+
+		return r;
+	}
+
 	CLASS(tdx_vm_state_guard, guard)(kvm);
 	if (IS_ERR(guard))
 		return PTR_ERR(guard);
diff --git a/arch/x86/kvm/vmx/tdx.h b/arch/x86/kvm/vmx/tdx.h
index fd368e3ee060b..f67d81f1aab73 100644
--- a/arch/x86/kvm/vmx/tdx.h
+++ b/arch/x86/kvm/vmx/tdx.h
@@ -47,6 +47,12 @@ struct kvm_tdx {
 	 * Set/unset is protected with kvm->mmu_lock.
 	 */
 	bool wait_for_sept_zap;
+
+	/*
+	 * Allow one quoting session per TD. This prevents a TD from hogging
+	 * the quoting resources.
+	 */
+	struct mutex quote_lock;
 };
 
 /* TDX module vCPU states */
-- 
2.53.0


  parent reply	other threads:[~2026-12-04 18:22 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-04 18:18 [PATCH v3 0/8] Enable DICE-based TDX quoting Peter Fang
2026-12-04 18:18 ` [PATCH v3 1/8] x86/virt/tdx: Add interface to generate a Quote Peter Fang
2026-12-04 19:15   ` Dave Hansen
2026-12-04 18:18 ` [PATCH v3 2/8] x86/virt/tdx: Add interface to check quoting capabilities Peter Fang
2026-12-04 18:18 ` [PATCH v3 3/8] KVM: TDX: Require at least one quoting session and thread per CPU Peter Fang
2026-12-04 18:18 ` Peter Fang [this message]
2026-12-04 18:18 ` [PATCH v3 5/8] KVM: TDX: Report maximum Quote size in TDX capabilities Peter Fang
2026-12-04 18:18 ` [PATCH v3 6/8] KVM: TDX: Document KVM_TDX_GET_QUOTE Peter Fang
2026-12-04 18:18 ` [PATCH v3 7/8] x86/virt/tdx: Enable the quoting extension Peter Fang
2026-12-04 18:18 ` [PATCH v3 8/8] x86/virt/tdx: Add documentation for DICE-based quoting Peter Fang
2026-12-04 18:40 ` [PATCH v3 0/8] Enable DICE-based TDX quoting Jason Gunthorpe
2026-12-04 18:47   ` Edgecombe, Rick P
2026-12-04 18:50     ` Jason Gunthorpe
2026-12-04 19:11       ` Edgecombe, Rick P

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=20261204182022.324230-5-peter.fang@intel.com \
    --to=peter.fang@intel.com \
    --cc=adrian.hunter@intel.com \
    --cc=binbin.wu@linux.intel.com \
    --cc=bp@alien8.de \
    --cc=chao.gao@intel.com \
    --cc=dave.hansen@linux.intel.com \
    --cc=elena.reshetova@intel.com \
    --cc=hpa@zytor.com \
    --cc=jgg@nvidia.com \
    --cc=kas@kernel.org \
    --cc=kishen.maloor@intel.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-coco@lists.linux.dev \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@redhat.com \
    --cc=nik.borisov@suse.com \
    --cc=pbonzini@redhat.com \
    --cc=rick.p.edgecombe@intel.com \
    --cc=seanjc@google.com \
    --cc=sohil.mehta@intel.com \
    --cc=tglx@kernel.org \
    --cc=tony.lindgren@linux.intel.com \
    --cc=x86@kernel.org \
    --cc=xiaoyao.li@intel.com \
    --cc=yilun.xu@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