From nobody Fri Apr 10 06:23:50 2026 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 A5FE328E3F for ; Tue, 25 Feb 2025 00:47:23 +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=1740444445; cv=none; b=bMY1TuVkMoPhAGzN0QDKJAwpWxRRKzB2pEyC+wnK/gRMRzhWESif5WIdLtQQp7nmmJOe9yga+vM2nX76rmwRqBMphKEsLX2lz8w9Ls6eCXwHyvytTe/e4a9D657mqNMOpxfphROcQawJMXxm0gISdtEND0tHo2iqkbYECAA1hMg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740444445; c=relaxed/simple; bh=iHJaUeFoAWphLfCjsYML3X/VoIYmwlw5kToWD4VsQ0k=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=i8caD1L+gweynbLVyd1at06f1mYQT0c8ieNpr7Ze/eaB7E5Y27JDyf0xl1C3FnKUV9p1ooOvdNaDBOhP+gL1uHj4OBJHaxNlKrOh7lnvSCfsjPXBXa/oV1wM/ZuWmnhHJmImW7UoMUIRIlsOfpSqhSEFzLOo8IUBh/tc2PLT07o= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--vannapurve.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=CFwYHChJ; 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--vannapurve.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="CFwYHChJ" Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-220e62c4fc2so110165795ad.3 for ; Mon, 24 Feb 2025 16:47:23 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1740444443; x=1741049243; 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=GSwXzpaQg6jWvjSpCxDF7Wv8BuKNQfehCGdAdnzxu5w=; b=CFwYHChJivUDw99FvIi6zE9G4hyehkakbESp47UREFTbSfsXdraG/ASxJhD+Lmz9ui fyTMFKuL8hTJovo71INfofVREOnhnbFyQG71xBzQoQO1B2PSIyueS8teqeegows0qAc9 nVx2+1IQ8lW7KOPudreKfCpFsoHgioNqXDWlEfFjBoIWD36izxwiKHmufqw8+r0TivEp jRcyWzD+bOksIctq8MRnMduI4ks5PCKlv+Up5/Mam3g667WzpjkxOu+i/UOeg8V+/53b bNbXzpCuHXST4pdQdSrsOGMaCQZ2tWlEBOhEdtc/pOHKMKxC+T/6QD+p3rYvSjP7tb0D kBhQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1740444443; x=1741049243; 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=GSwXzpaQg6jWvjSpCxDF7Wv8BuKNQfehCGdAdnzxu5w=; b=OTB85vT6CTFJ0r0QgLB9mUZE+7gVeEje/gyzfZ8lLfc6f45rb8lnPtJqHA0ylKRzm0 nQL3iGjEv90ByBQ+3/eMDkjo2SHv9GiPYhJYxjmq7hBcUGZ3VdCJufuBBKpvQ1P5aBBb qs7EBNALuGs5tKA2QJMVC3yI0BJpbFLI7DPZFCzo5f9OK+Q8z+3V0hTU/y5TDiv0OIjQ IIo3JS/u95DYQFq/tk+Hon73XxJ4hksOqrYQrU4Af9nxP3hslPnIaVj8KVzRhz2Asouk LnFmbPJpmj90zs8yCTA3WnRIw4tyWGQQjaOVNr5xhWjeZOp+gdHB4WnsIC1TdWmxBzru 5VVA== X-Forwarded-Encrypted: i=1; AJvYcCX/NUA9sT6mm7rEGHrlYKENRprUnTUsTjI/4T4fzueHZFg10E2ibOt8IRCpOTzutL+jaP2JBzlzf6PRG0o=@vger.kernel.org X-Gm-Message-State: AOJu0YxBP2soXKsbzHRH+3F5uVG47bXvW/W/q/DKm3il8SNmNtpoiRab G+8bycyfI/iix8uFbEM+S0/q4eNWSiTg1+uupDUb7rbclbnxq5Rzw9v1tjafKPMxD7Dss07KBI1 2w7UUBxdJEl1sZfFxZw== X-Google-Smtp-Source: AGHT+IENsRnPp7jn8IuysVjH0YnI4eclJId+egEJWA+BbEYzZDe3tiL9fs29LuBPMsnBKu2yRyxTuOgeH23qbt74 X-Received: from plfn11.prod.google.com ([2002:a17:902:e54b:b0:21f:4ecc:af8d]) (user=vannapurve job=prod-delivery.src-stubby-dispatcher) by 2002:a17:902:f705:b0:220:d257:cdbd with SMTP id d9443c01a7336-2219ffbf52emr272898735ad.48.1740444442957; Mon, 24 Feb 2025 16:47:22 -0800 (PST) Date: Tue, 25 Feb 2025 00:47:03 +0000 In-Reply-To: <20250225004704.603652-1-vannapurve@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250225004704.603652-1-vannapurve@google.com> X-Mailer: git-send-email 2.48.1.658.g4767266eb4-goog Message-ID: <20250225004704.603652-3-vannapurve@google.com> Subject: [PATCH v6 2/3] x86/tdx: Fix arch_safe_halt() execution for TDX VMs From: Vishal Annapurve To: dave.hansen@linux.intel.com, kirill.shutemov@linux.intel.com, jgross@suse.com, ajay.kaher@broadcom.com, ak@linux.intel.com, tony.luck@intel.com, thomas.lendacky@amd.com Cc: tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, hpa@zytor.com, pbonzini@redhat.com, seanjc@google.com, kai.huang@intel.com, chao.p.peng@linux.intel.com, isaku.yamahata@gmail.com, sathyanarayanan.kuppuswamy@linux.intel.com, erdemaktas@google.com, ackerleytng@google.com, jxgao@google.com, sagis@google.com, afranji@google.com, kees@kernel.org, jikos@kernel.org, peterz@infradead.org, x86@kernel.org, linux-kernel@vger.kernel.org, linux-coco@lists.linux.dev, virtualization@lists.linux.dev, bcm-kernel-feedback-list@broadcom.com, Vishal Annapurve , stable@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Direct HLT instruction execution causes #VEs for TDX VMs which is routed to hypervisor via TDCALL. If HLT is executed in STI-shadow, resulting #VE handler will enable interrupts before TDCALL is routed to hypervisor leading to missed wakeup events. Current TDX spec doesn't expose interruptibility state information to allow #VE handler to selectively enable interrupts. To bypass this issue, TDX VMs need to replace "sti;hlt" execution with direct TDCALL followed by explicit interrupt flag update. Commit bfe6ed0c6727 ("x86/tdx: Add HLT support for TDX guests") prevented the idle routines from executing HLT instruction in STI-shadow. But it missed the paravirt routine which can be reached like this as an example: acpi_safe_halt() =3D> raw_safe_halt() =3D> arch_safe_halt() =3D> irq.safe_halt() =3D> pv_native_safe_halt() To reliably handle arch_safe_halt() for TDX VMs, introduce explicit dependency on CONFIG_PARAVIRT and override paravirt halt()/safe_halt() routines with TDX-safe versions that execute direct TDCALL and needed interrupt flag updates. Executing direct TDCALL brings in additional benefit of avoiding HLT related #VEs altogether. Cc: stable@vger.kernel.org Fixes: bfe6ed0c6727 ("x86/tdx: Add HLT support for TDX guests") Signed-off-by: Vishal Annapurve Reviewed-by: Kirill A. Shutemov --- arch/x86/Kconfig | 1 + arch/x86/coco/tdx/tdx.c | 26 +++++++++++++++++++++++++- arch/x86/include/asm/tdx.h | 2 +- arch/x86/kernel/process.c | 2 +- 4 files changed, 28 insertions(+), 3 deletions(-) diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig index be2c311f5118..933c046e8966 100644 --- a/arch/x86/Kconfig +++ b/arch/x86/Kconfig @@ -902,6 +902,7 @@ config INTEL_TDX_GUEST depends on X86_64 && CPU_SUP_INTEL depends on X86_X2APIC depends on EFI_STUB + depends on PARAVIRT select ARCH_HAS_CC_PLATFORM select X86_MEM_ENCRYPT select X86_MCE diff --git a/arch/x86/coco/tdx/tdx.c b/arch/x86/coco/tdx/tdx.c index 32809a06dab4..6aad910d119d 100644 --- a/arch/x86/coco/tdx/tdx.c +++ b/arch/x86/coco/tdx/tdx.c @@ -14,6 +14,7 @@ #include #include #include +#include #include #include #include @@ -398,7 +399,7 @@ static int handle_halt(struct ve_info *ve) return ve_instr_len(ve); } =20 -void __cpuidle tdx_safe_halt(void) +void __cpuidle tdx_halt(void) { const bool irq_disabled =3D false; =20 @@ -409,6 +410,16 @@ void __cpuidle tdx_safe_halt(void) WARN_ONCE(1, "HLT instruction emulation failed\n"); } =20 +static void __cpuidle tdx_safe_halt(void) +{ + tdx_halt(); + /* + * "__cpuidle" section doesn't support instrumentation, so stick + * with raw_* variant that avoids tracing hooks. + */ + raw_local_irq_enable(); +} + static int read_msr(struct pt_regs *regs, struct ve_info *ve) { struct tdx_module_args args =3D { @@ -1109,6 +1120,19 @@ void __init tdx_early_init(void) x86_platform.guest.enc_kexec_begin =3D tdx_kexec_begin; x86_platform.guest.enc_kexec_finish =3D tdx_kexec_finish; =20 + /* + * Avoid "sti;hlt" execution in TDX guests as HLT induces a #VE that + * will enable interrupts before HLT TDCALL invocation if executed + * in STI-shadow, possibly resulting in missed wakeup events. + * + * Modify all possible HLT execution paths to use TDX specific routines + * that directly execute TDCALL and toggle the interrupt state as + * needed after TDCALL completion. This also reduces HLT related #VEs + * in addition to having a reliable halt logic execution. + */ + pv_ops.irq.safe_halt =3D tdx_safe_halt; + pv_ops.irq.halt =3D tdx_halt; + /* * TDX intercepts the RDMSR to read the X2APIC ID in the parallel * bringup low level code. That raises #VE which cannot be handled diff --git a/arch/x86/include/asm/tdx.h b/arch/x86/include/asm/tdx.h index b4b16dafd55e..393ee2dfaab1 100644 --- a/arch/x86/include/asm/tdx.h +++ b/arch/x86/include/asm/tdx.h @@ -58,7 +58,7 @@ void tdx_get_ve_info(struct ve_info *ve); =20 bool tdx_handle_virt_exception(struct pt_regs *regs, struct ve_info *ve); =20 -void tdx_safe_halt(void); +void tdx_halt(void); =20 bool tdx_early_handle_ve(struct pt_regs *regs); =20 diff --git a/arch/x86/kernel/process.c b/arch/x86/kernel/process.c index 6da6769d7254..d11956a178df 100644 --- a/arch/x86/kernel/process.c +++ b/arch/x86/kernel/process.c @@ -934,7 +934,7 @@ void __init select_idle_routine(void) static_call_update(x86_idle, mwait_idle); } else if (cpu_feature_enabled(X86_FEATURE_TDX_GUEST)) { pr_info("using TDX aware idle routine\n"); - static_call_update(x86_idle, tdx_safe_halt); + static_call_update(x86_idle, tdx_halt); } else { static_call_update(x86_idle, default_idle); } --=20 2.48.1.658.g4767266eb4-goog