From nobody Thu Dec 18 09:59:22 2025 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 2668E5680 for ; Wed, 12 Feb 2025 00:08:15 +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=1739318897; cv=none; b=E9d1AOBGYGEbnDcPm/PQ/s/32Kd8FFv/9NklwZy8I054SORDokWe0MPGcV4rJUA+GQIccI1tHlkDQKVoSKneZ89RZ1WlG/+hUq4P9eRq4T+qNCQvW3fo4MQpku71gfd+4Kx8xlRlSO4XGYj975ek7/u/aMqy5DlZ53tWcKYzW0Q= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1739318897; c=relaxed/simple; bh=ehLYTG6Xoh6RePEuOdsJO1YiF66iXfldRHD0ATxH28M=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=upld0w9x9orLk5oAMZzC7s3jUwSgcbgVfcW1XT2SIWe3XKWYE5V9NKLZPUjVrP+MoVRsVmI+Ascwv/QHgErQhjwZitgC8ODJsxnpGbJg3xpgp/NuZBH28fvdwbsIMTnPkKkx7I0SQ9DL8ZoxxKaxg4Mpq6rcZXa5wuXiWMiBxSY= 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=d5aFYlFc; 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--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="d5aFYlFc" Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-21f6b2d49e3so91680585ad.3 for ; Tue, 11 Feb 2025 16:08:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1739318895; x=1739923695; 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=aflUMaVdfiypUqtPtl++K+1MuoyNGKota/M1UlzbiMc=; b=d5aFYlFcPz68rfIoI3Nm0O3H2FUUA8m++SrnBOhazh85Qe//glBuoSQ6VnjdLvZkQU M+KNMbhmIzG26UFs/RQbIZBPfo+Mz8N0xSx6UAgMS/U+oFYUEetGlTJ/K2TNO4NucEy4 qYPDKrVIh/ryDIZ1Dz6Zus7agIycoLtjD0VGzQes991YuW1ZXpp+v61e+cCKnwZBKqUw ZfZEOz+on9x0gf/bDoueuUQQ6R3J3jrAgiSwSKnFKLzGMjCRYmdS2tDchrtvcKQIbHwT OAgPcFuxjmrIaGN2r5eiwwbI7foiB48AedsKpjIYSOgwuqcT+9UKS8FQdU9YFGRvAkKV RYDQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1739318895; x=1739923695; 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=aflUMaVdfiypUqtPtl++K+1MuoyNGKota/M1UlzbiMc=; b=D25+CtMTYFeNWXHfk5Zt9HeSdXZOXbp6vCvx43Ha/c5ALFFz8Aqyk/IumzHBgmOBcI LkxqtmsN7thz7DIB3Ec16l7nX5z7wwKVOOL2xLNef6Nc2sqn6D1FXuKuCCAq31hRBZL2 TXv1G+ljwIJ88TfwzGyqXWeqEzRJ5FFPMuCdyC3LVap9mKU/IAKnBxNd11PnCnelAxFu J9D+gzHWuB/HxM69I0mZF4TA8oo6dqH/vwAfOJzKHUPdHNqaKE7yp4ZaMeBuxIiWKRi0 MRrkxPC6kW1zWvU3ZJZmKR1yMhB5AmBG8W3X8tGdsIwOIqWuz1bMbupqQWccU6sGtF5C 5G3A== X-Forwarded-Encrypted: i=1; AJvYcCXTGPArhPx7LHMVzSgfbx5eR0FHkPoUYNwKIKZu8j+Z+3GvYV5GSv9iLtopTiIUiYbsJ70L5g7VcxyNIJo=@vger.kernel.org X-Gm-Message-State: AOJu0Yz0//vlYrT6m1tlVy5I2zH3yp2K9rI1J5NLcX13kFDbqHWDbUBS R+lTE+yp1tl576jVS3DcUQLYzuZGVzIO8nlnIDk2qWWc0fd3R7qiVNVv1xF1Kvh4PwLr416VZpA eQT/SFKTS0c/4Wy2ptQ== X-Google-Smtp-Source: AGHT+IHPxfAdpM1iOOY8z1AmixqeOmuG/IEf4j5AgcwJnfrRV4HF54kPCepPnvhuZO3QjCmSQoEz/uA6jsv32fdY X-Received: from pgtq26.prod.google.com ([2002:a65:685a:0:b0:ad5:444f:7093]) (user=vannapurve job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a20:2450:b0:1e1:bdae:e04d with SMTP id adf61e73a8af0-1ee5c85db6bmr2527873637.36.1739318895379; Tue, 11 Feb 2025 16:08:15 -0800 (PST) Date: Wed, 12 Feb 2025 00:07:44 +0000 In-Reply-To: <20250212000747.3403836-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: <20250212000747.3403836-1-vannapurve@google.com> X-Mailer: git-send-email 2.48.1.502.g6dc24dfdaf-goog Message-ID: <20250212000747.3403836-2-vannapurve@google.com> Subject: [PATCH V4 1/4] x86/paravirt: Move halt paravirt calls under CONFIG_PARAVIRT From: Vishal Annapurve To: x86@kernel.org, linux-kernel@vger.kernel.org Cc: pbonzini@redhat.com, seanjc@google.com, erdemaktas@google.com, ackerleytng@google.com, jxgao@google.com, sagis@google.com, oupton@google.com, pgonda@google.com, kirill@shutemov.name, dave.hansen@linux.intel.com, linux-coco@lists.linux.dev, chao.p.peng@linux.intel.com, isaku.yamahata@gmail.com, sathyanarayanan.kuppuswamy@linux.intel.com, "Kirill A. Shutemov" , stable@vger.kernel.org, Andi Kleen , Tony Luck , Vishal Annapurve Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: "Kirill A. Shutemov" CONFIG_PARAVIRT_XXL is mainly defined/used by XEN PV guests. For other VM guest types, features supported under CONFIG_PARAVIRT are self sufficient. CONFIG_PARAVIRT mainly provides support for TLB flush operations and time related operations. For TDX guest as well, paravirt calls under CONFIG_PARVIRT meets most of its requirement except the need of HLT and SAFE_HLT paravirt calls, which is currently defined under CONFIG_PARAVIRT_XXL. Since enabling CONFIG_PARAVIRT_XXL is too bloated for TDX guest like platforms, move HLT and SAFE_HLT paravirt calls under CONFIG_PARAVIRT. Moving HLT and SAFE_HLT paravirt calls are not fatal and should not break any functionality for current users of CONFIG_PARAVIRT. Cc: stable@vger.kernel.org Fixes: bfe6ed0c6727 ("x86/tdx: Add HLT support for TDX guests") Co-developed-by: Kuppuswamy Sathyanarayanan Signed-off-by: Kuppuswamy Sathyanarayanan Signed-off-by: Kirill A. Shutemov Reviewed-by: Andi Kleen Reviewed-by: Tony Luck Signed-off-by: Vishal Annapurve --- arch/x86/include/asm/irqflags.h | 40 +++++++++++++++------------ arch/x86/include/asm/paravirt.h | 20 +++++++------- arch/x86/include/asm/paravirt_types.h | 3 +- arch/x86/kernel/paravirt.c | 14 ++++++---- 4 files changed, 41 insertions(+), 36 deletions(-) diff --git a/arch/x86/include/asm/irqflags.h b/arch/x86/include/asm/irqflag= s.h index cf7fc2b8e3ce..1c2db11a2c3c 100644 --- a/arch/x86/include/asm/irqflags.h +++ b/arch/x86/include/asm/irqflags.h @@ -76,6 +76,28 @@ static __always_inline void native_local_irq_restore(uns= igned long flags) =20 #endif =20 +#ifndef CONFIG_PARAVIRT +#ifndef __ASSEMBLY__ +/* + * Used in the idle loop; sti takes one instruction cycle + * to complete: + */ +static __always_inline void arch_safe_halt(void) +{ + native_safe_halt(); +} + +/* + * Used when interrupts are already enabled or to + * shutdown the processor: + */ +static __always_inline void halt(void) +{ + native_halt(); +} +#endif /* __ASSEMBLY__ */ +#endif /* CONFIG_PARAVIRT */ + #ifdef CONFIG_PARAVIRT_XXL #include #else @@ -97,24 +119,6 @@ static __always_inline void arch_local_irq_enable(void) native_irq_enable(); } =20 -/* - * Used in the idle loop; sti takes one instruction cycle - * to complete: - */ -static __always_inline void arch_safe_halt(void) -{ - native_safe_halt(); -} - -/* - * Used when interrupts are already enabled or to - * shutdown the processor: - */ -static __always_inline void halt(void) -{ - native_halt(); -} - /* * For spinlocks, etc: */ diff --git a/arch/x86/include/asm/paravirt.h b/arch/x86/include/asm/paravir= t.h index 041aff51eb50..29e7331a0c98 100644 --- a/arch/x86/include/asm/paravirt.h +++ b/arch/x86/include/asm/paravirt.h @@ -107,6 +107,16 @@ static inline void notify_page_enc_status_changed(unsi= gned long pfn, PVOP_VCALL3(mmu.notify_page_enc_status_changed, pfn, npages, enc); } =20 +static __always_inline void arch_safe_halt(void) +{ + PVOP_VCALL0(irq.safe_halt); +} + +static inline void halt(void) +{ + PVOP_VCALL0(irq.halt); +} + #ifdef CONFIG_PARAVIRT_XXL static inline void load_sp0(unsigned long sp0) { @@ -170,16 +180,6 @@ static inline void __write_cr4(unsigned long x) PVOP_VCALL1(cpu.write_cr4, x); } =20 -static __always_inline void arch_safe_halt(void) -{ - PVOP_VCALL0(irq.safe_halt); -} - -static inline void halt(void) -{ - PVOP_VCALL0(irq.halt); -} - static inline u64 paravirt_read_msr(unsigned msr) { return PVOP_CALL1(u64, cpu.read_msr, msr); diff --git a/arch/x86/include/asm/paravirt_types.h b/arch/x86/include/asm/p= aravirt_types.h index fea56b04f436..abccfccc2e3f 100644 --- a/arch/x86/include/asm/paravirt_types.h +++ b/arch/x86/include/asm/paravirt_types.h @@ -120,10 +120,9 @@ struct pv_irq_ops { struct paravirt_callee_save save_fl; struct paravirt_callee_save irq_disable; struct paravirt_callee_save irq_enable; - +#endif void (*safe_halt)(void); void (*halt)(void); -#endif } __no_randomize_layout; =20 struct pv_mmu_ops { diff --git a/arch/x86/kernel/paravirt.c b/arch/x86/kernel/paravirt.c index 1ccaa3397a67..c5bb980b8a67 100644 --- a/arch/x86/kernel/paravirt.c +++ b/arch/x86/kernel/paravirt.c @@ -110,6 +110,11 @@ int paravirt_disable_iospace(void) return request_resource(&ioport_resource, &reserve_ioports); } =20 +static noinstr void pv_native_safe_halt(void) +{ + native_safe_halt(); +} + #ifdef CONFIG_PARAVIRT_XXL static noinstr void pv_native_write_cr2(unsigned long val) { @@ -125,11 +130,6 @@ static noinstr void pv_native_set_debugreg(int regno, = unsigned long val) { native_set_debugreg(regno, val); } - -static noinstr void pv_native_safe_halt(void) -{ - native_safe_halt(); -} #endif =20 struct pv_info pv_info =3D { @@ -186,9 +186,11 @@ struct paravirt_patch_template pv_ops =3D { .irq.save_fl =3D __PV_IS_CALLEE_SAVE(pv_native_save_fl), .irq.irq_disable =3D __PV_IS_CALLEE_SAVE(pv_native_irq_disable), .irq.irq_enable =3D __PV_IS_CALLEE_SAVE(pv_native_irq_enable), +#endif /* CONFIG_PARAVIRT_XXL */ + + /* Irq HLT ops. */ .irq.safe_halt =3D pv_native_safe_halt, .irq.halt =3D native_halt, -#endif /* CONFIG_PARAVIRT_XXL */ =20 /* Mmu ops. */ .mmu.flush_tlb_user =3D native_flush_tlb_local, --=20 2.48.1.502.g6dc24dfdaf-goog From nobody Thu Dec 18 09:59:22 2025 Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) (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 1C7FE79FD for ; Wed, 12 Feb 2025 00:08:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1739318900; cv=none; b=AhU3KC1rWOgSs+RSTh3C1+yg/Zv2ulsTC3hir2N4ji045Rr/d4owWg1wctiUH6LdA7sbWw/Z5VxOwfOOIVwTjZ9CYavHt+ogFQiaH9gVZ9wmrF5rijO/eHfjZymx/NlfjcIEtWWpsU1AN7Mch83+iTqJ/9eU3Hsd+3GYy0v4QXE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1739318900; c=relaxed/simple; bh=qPyAwicKpZpLg3GmtysNq22b8SvhJ9Kkp+IXaoaTFnU=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=DOfVp3xaE9/h+3RS6DeqTqPHaws3iuzUjCWfhJLAWWFswH7ZbfZDZkjyh+cg/h/jogCITMyx9t4fVlqnvFEgKXmJ1GUPqnl5l70TmICl5yWn/qnER/WrokSO/EmOj3ntYytqq4SQm7Xudlc4mS3DYK+u3MTmjH2a3WEi6mocI4M= 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=0W4+BWTt; arc=none smtp.client-ip=209.85.216.74 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="0W4+BWTt" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-2f5538a2356so13035251a91.2 for ; Tue, 11 Feb 2025 16:08:18 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1739318898; x=1739923698; 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=rhFKIyqyWccfVyhYNzTyKMpa/Pu1oM/LLWYuIAGv7wA=; b=0W4+BWTt/oWdsQ6efyp1JIT778fbfnHUNFjprU9IjG8JvSPV9OK6gEIOK8o7SjXMBj cxpf1agmm1vmLJdY8G18PHbuFP6Zo4HiAw4qFM1Jf14O8gtUDlPFmqYJ5ocMokEmotXI qR3v9IXFAjD/z2kIgcer3mZ3HN/WMXiryPNHrFYkORgmS4DLFY++YkjwF2wJPPQuSkbm pnfMlJ1xeiw3S1eTVVfinzKSwjMh0TgZmPC4o4OdOvST4F/hhswmmW3732/4sYJqrSUR dl4DVtycLHe78SaGT5ZmfDxyyEulSN0pDtd/vtzCiTLjT5QzyCy0uOkOSKE1J5EDfCUW ueGQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1739318898; x=1739923698; 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=rhFKIyqyWccfVyhYNzTyKMpa/Pu1oM/LLWYuIAGv7wA=; b=M749EkXMjYMgn1rzFBMys34n0rrtw5G9Xv04fy6U1ukYklZWFe2gMwxdiZho9yd1VY Eq+bDZft8qcpV1TecmRSbNb5lpadY7YO5IOVjk9bXj3qPlvX3PPkvgpwJx3WjUNySuE/ qalUyU4TMvXjMNnW3Wb2kR0prlkxK6NzmCeGP450+tANVOSBB6yn3eVdyvDyIQsiHnFC LXoej37t57qLHN0scOs1FDa3/tDvLMuuyXM9ao9a2tLnoeA4byNgiv4xcKNBfjwqsnoT aDY1ZNLjPROIQQSQ+mOxlylfc2cnqrJHH65BSYeIEFP6ebVWK9hNNgYqQz7vbSFuBTTa fF/A== X-Forwarded-Encrypted: i=1; AJvYcCX1p5NsQvFXAB2odFemYVW6vzy27wuMEMsY2HHhrP92lmvn5qAufyCnwGCEl+wKOGqrFndQkYiKoKLpAUs=@vger.kernel.org X-Gm-Message-State: AOJu0YzW7nNqzyAOpAU5DLPA+2c7go5x1ByMpolZHjQuyyQIGmoGs/79 NSfh1FPhogguPpD2LbT/Zc+wbG3GGAqC20Nsa9c6CA4g3LYcvoX3yc8vyQ8Pn8flc0iUmF/3+bN rUzkkmKjusS+JiDrWHg== X-Google-Smtp-Source: AGHT+IGIIh1a6f5LCEQr8t1bLmoqA5VXctBMBQSaTYWM2+xjlnEVTI47jUBbtXW0wJOYn/WoZYxJQiGIyuC2R8tL X-Received: from pfoa23.prod.google.com ([2002:aa7:8657:0:b0:730:759d:5049]) (user=vannapurve job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:cd2:b0:730:7499:4036 with SMTP id d2e1a72fcca58-7322c443c7bmr1611687b3a.22.1739318898459; Tue, 11 Feb 2025 16:08:18 -0800 (PST) Date: Wed, 12 Feb 2025 00:07:45 +0000 In-Reply-To: <20250212000747.3403836-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: <20250212000747.3403836-1-vannapurve@google.com> X-Mailer: git-send-email 2.48.1.502.g6dc24dfdaf-goog Message-ID: <20250212000747.3403836-3-vannapurve@google.com> Subject: [PATCH V4 2/4] x86/tdx: Route safe halt execution via tdx_safe_halt() From: Vishal Annapurve To: x86@kernel.org, linux-kernel@vger.kernel.org Cc: pbonzini@redhat.com, seanjc@google.com, erdemaktas@google.com, ackerleytng@google.com, jxgao@google.com, sagis@google.com, oupton@google.com, pgonda@google.com, kirill@shutemov.name, dave.hansen@linux.intel.com, linux-coco@lists.linux.dev, chao.p.peng@linux.intel.com, isaku.yamahata@gmail.com, sathyanarayanan.kuppuswamy@linux.intel.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. safe_halt() routines execute HLT in STI-shadow so IRQs need to remain disabled until the TDCALL to ensure that pending IRQs are correctly treated as wake events. So "sti;hlt" sequence needs to be replaced with "TDCALL; raw_local_irq_enable()" for TDX VMs. Commit bfe6ed0c6727 ("x86/tdx: Add HLT support for TDX guests") prevented the idle routines from using "sti;hlt". 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() Modify tdx_safe_halt() to implement the sequence "TDCALL; raw_local_irq_enable()" and invoke tdx_halt() from idle routine which just executes TDCALL without changing state of interrupts. Introduce dependency on CONFIG_PARAVIRT and override paravirt halt()/safe_halt() routines for TDX VMs. Cc: stable@vger.kernel.org Fixes: bfe6ed0c6727 ("x86/tdx: Add HLT support for TDX guests") Signed-off-by: Vishal Annapurve --- arch/x86/Kconfig | 1 + arch/x86/coco/tdx/tdx.c | 18 +++++++++++++++++- arch/x86/include/asm/tdx.h | 2 +- arch/x86/kernel/process.c | 2 +- 4 files changed, 20 insertions(+), 3 deletions(-) diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig index 87198d957e2f..afcdbc9693dc 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..ee67c1870e70 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,12 @@ void __cpuidle tdx_safe_halt(void) WARN_ONCE(1, "HLT instruction emulation failed\n"); } =20 +static void __cpuidle tdx_safe_halt(void) +{ + tdx_halt(); + raw_local_irq_enable(); +} + static int read_msr(struct pt_regs *regs, struct ve_info *ve) { struct tdx_module_args args =3D { @@ -1109,6 +1116,15 @@ 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 + /* + * "sti;hlt" execution in TDX guests will induce a #VE in the STI-shadow + * which will enable interrupts before HLT TDCALL inocation possibly + * resulting in missed wakeup events. Modify all possible HLT + * execution paths to use TDCALL for performance/reliability reasons. + */ + 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.502.g6dc24dfdaf-goog From nobody Thu Dec 18 09:59:22 2025 Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) (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 510C7FBF6 for ; Wed, 12 Feb 2025 00:08:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1739318902; cv=none; b=E30q0SG69d6CGM/ICslnX2Fe/xVyqL/HyCOCi3IDvjic5333PmqTq8GFJ/XXkAQ2I9Qj1Il2u7W6lNQN5O8roz6VlSIJEmx+/MLK3EBn2D6X1UyQ0dx4hQzMT2vmLM5uEYiH3Id5Q6Skn0mv+rgbPm+USJ17Uya+JhvUSL+7UVw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1739318902; c=relaxed/simple; bh=oLYsr5K/EnOE6Ps4r/pTLeznQ2IgY4c6G0iy2zWPqsM=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=AXW3KQ3dxLk/eANSLo8r2ip92BZywk0Ixy4o+3ZsUtptSSqkAtsv0iFiBfZV7FVLcI4b8tQXbDjYvFBNBL+Nx3k/Y9mIFXvRdulmoR3wTES5QCCzcQzUMY3CIFxsfAl4zA4onF6G47DutOJVkiKo0BEVMsB2t8Ii6+l1u6KYJNI= 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=TslQGlfy; arc=none smtp.client-ip=209.85.216.73 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="TslQGlfy" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-2fa34df4995so13318905a91.0 for ; Tue, 11 Feb 2025 16:08:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1739318900; x=1739923700; 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=h1XiCMBzc5zO/l6anZRBbtwTnK6rTlocvFZ6R2e8ekk=; b=TslQGlfyCdtaTcXqVxnV0PEfi56qHRVm8n1Nk9GA4W4vHhj4DtUVVQlejw1SvnMaV1 5BnQqYpif3a9U5O+hoPK0UTXRyRNN/OqUsPmZLp9vWlpYofyigUvKw2Xh6LseKhXSzT4 ZKSHfSXBiVC4IRjBwkxzdySUcg+Jjua1lMVgKo1+PrmwFjs2kDNDiQ9hVv3iRTak6G6R 44mTPIGk4U3xydd7A1mbkhwwLluyMYwu8HLGEyT5Ca+rQhtUs82WCvYf11RVjPIQ6puk UoA5hp+Bid4BMX4Sh9jGwyX7q5Y3VD/gNC/KTdLuNWYlADFPjOk/KHZClJuoSUWp1yIl 5gVw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1739318900; x=1739923700; 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=h1XiCMBzc5zO/l6anZRBbtwTnK6rTlocvFZ6R2e8ekk=; b=N3nLzXUpSAxxjmAWQAzEnZJVJYOQMB4l3ToljxmXWdrb/OadMlTFmqPXyCpMBmbEaU LAZu5xI1bJ3IDsaK6MVzaDgZHXUuuqkyDMJnguK8p2VOKEoWQtuTPim2BgF2+atLJjUm zqfwL69vzdVRehYmbYDJ5rMHPzyytdhZqYfLC8cvNB59jiatLowb7gjC9bZVqBNX011O npUufXx673Y08S6qR9wnZbWE5/u5p48Z+c4vo+t+9BLhkuhSKOTnxmlGR0DS4AePqGZv 96ZqOAw3Ib61M3H9+3TIXYFdWVxu2dq2YQZla+z5hv9fm0iyrDWR1o2frZsJBNNDnbmu MAgQ== X-Forwarded-Encrypted: i=1; AJvYcCVEr0YvOMsH75yULfSfFudMvc5z3Sya/FgiVRE2uNEr7elrwDsdpgQkENhNgNZYUA3Of4LbutNS+HGRZv8=@vger.kernel.org X-Gm-Message-State: AOJu0Ywly6H2BGkpMSTHpemB2952aO51vJd0BBvamgy3eJZz1AXu3zcQ 20s3DXpekAxEVG628RjCSgYBvBkaigtK5RQ0uaXOP+JgtMx7QvBYrJPDU8Kizk0M6MghJ7OD8W/ kFNBuNYyYCY+BTlYstA== X-Google-Smtp-Source: AGHT+IH4Aastc89G3lK7Kpren76shMv1ca6utCycZ+bUZsHekNg+thxZju67dX6Db0XVVQa0m/oRTqmDN+BLbf48 X-Received: from pfbit9.prod.google.com ([2002:a05:6a00:4589:b0:730:8636:67d7]) (user=vannapurve job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:1c8d:b0:730:9860:1240 with SMTP id d2e1a72fcca58-7322c39c33bmr1528792b3a.13.1739318900587; Tue, 11 Feb 2025 16:08:20 -0800 (PST) Date: Wed, 12 Feb 2025 00:07:46 +0000 In-Reply-To: <20250212000747.3403836-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: <20250212000747.3403836-1-vannapurve@google.com> X-Mailer: git-send-email 2.48.1.502.g6dc24dfdaf-goog Message-ID: <20250212000747.3403836-4-vannapurve@google.com> Subject: [PATCH V4 3/4] x86/tdx: Emit warning if IRQs are enabled during HLT #VE handling From: Vishal Annapurve To: x86@kernel.org, linux-kernel@vger.kernel.org Cc: pbonzini@redhat.com, seanjc@google.com, erdemaktas@google.com, ackerleytng@google.com, jxgao@google.com, sagis@google.com, oupton@google.com, pgonda@google.com, kirill@shutemov.name, dave.hansen@linux.intel.com, linux-coco@lists.linux.dev, chao.p.peng@linux.intel.com, isaku.yamahata@gmail.com, sathyanarayanan.kuppuswamy@linux.intel.com, Vishal Annapurve , "Kirill A. Shutemov" 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. safe_halt() routines execute HLT in STI-shadow so IRQs need to remain disabled until the TDCALL to ensure that pending IRQs are correctly treated as wake events. Emit warning and fail emulation if IRQs are enabled during HLT #VE handling to avoid running into scenarios where IRQ wake events are lost resulting in indefinite HLT execution times. Reviewed-by: Kirill A. Shutemov Signed-off-by: Vishal Annapurve --- arch/x86/coco/tdx/tdx.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/arch/x86/coco/tdx/tdx.c b/arch/x86/coco/tdx/tdx.c index ee67c1870e70..54baf93d9218 100644 --- a/arch/x86/coco/tdx/tdx.c +++ b/arch/x86/coco/tdx/tdx.c @@ -393,6 +393,14 @@ static int handle_halt(struct ve_info *ve) { const bool irq_disabled =3D irqs_disabled(); =20 + /* + * HLT with IRQs enabled is unsafe, as an IRQ that is intended to be a + * wake event may be consumed before requesting HLT emulation, leaving + * the vCPU blocking indefinitely. + */ + if (WARN_ONCE(!irq_disabled, "HLT emulation with IRQs enabled")) + return -EIO; + if (__halt(irq_disabled)) return -EIO; =20 --=20 2.48.1.502.g6dc24dfdaf-goog From nobody Thu Dec 18 09:59:22 2025 Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) (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 9CB841EB2F for ; Wed, 12 Feb 2025 00:08:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1739318905; cv=none; b=mJtLUWrtD1EYaBGTgGI5cVVoTJJeqJO2JCJeYKPsoMvnGKnspLjZFwdgtMChVBdnu5xue33ozLofhniShz4Dq3uSPqjijKwQrNdBCbS9V5QN6xmS44Ol2zNewuWsUHwrXZo9ACg/TqTUsm0etlJCmUm1msHnbW52T29nNF2g5wQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1739318905; c=relaxed/simple; bh=Ry//zRj6xrQGvb+QLd5PGQ2AmxS5vbX9LInUxHl69YU=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=fN+ToQf3trAZsf4PifNprMzZK+66+IVPi6F6UjlVfUGnAIXmKsn109bU6Sc7PyNYYYHNsiqioio8Sy3dzmfyZ6EGUBk84UwT0BOXNzr2Aor3qoc7Ehm4x5Q/4TdQJbRJcKhEfNPiQOE23cX6x92BKnL+8zBewHgW2u+vtrc+v2Y= 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=YvAFwhB6; arc=none smtp.client-ip=209.85.216.74 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="YvAFwhB6" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-2fa3b466245so8116624a91.0 for ; Tue, 11 Feb 2025 16:08:23 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1739318903; x=1739923703; 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=qWVkozr8oT2oqbRq082DQxpQr7C1Te5FFbGPk2fd3qk=; b=YvAFwhB64JA5wiZwsx8qNLS60M8Tox7r5/9iByoBX3t6JYCycU5FqQwhttL4ZxYiK8 FWz+hRs5Yccjg3xveJZVHKqaavqrEAuaRWPlDXG6oC19dWKlQreqNWr7nZLr1e0olaZd 9UXx70T0t//Jwf66ZjlLmzZasFxYiHkg0G9P3pS75oy8zCgu/geCguFO4Bg9F1glfmKg Du7tSZ6n4Sh+Z0+e5/GL5Sdb/GfpwMUjIGg+tOQTtccsx5MQR78g9Yhcs5Nw/7AQGaPD gfSAY7LhhQKVyHz2au9oWBjRlU4FwoVAxHuokiPRJVwvz+8F7uGflBsBv52fS/pilShp ezaA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1739318903; x=1739923703; 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=qWVkozr8oT2oqbRq082DQxpQr7C1Te5FFbGPk2fd3qk=; b=YF8+lOJ9Yk/zDgNMblOMy2aTROWco7Zmm/Id87AN1DZ353JjPd/WPcWL2vRQbnJnjk C0+M66bY3aNolB1wfmcWXixhY0Fh66J/RFik9bfWYs4bVTvLOz4lDaMttI+y9iDqJhXd GRnMVkkdyrcLgg1VYgdNIcJJlo0qr6jwd8bmoNf7I9FmshGKoNh2jRtlQL1RAM18Emsp /EyGKUHWxrNiVvMP0I1qqtEhLdSuugIo3vmwDaZJJ5mgfHH/oiHGnnPhDXuKIwgyaHC5 J4TkTVclSjQdjoqLk+wGK7Kcr0sV9qGtFdAZK+cngJ3chNP3uN4DQXiV8noBQKfkZb5z EBQQ== X-Forwarded-Encrypted: i=1; AJvYcCW9NaV04R1erXY5OHS/fYrF9sLo/z6JXnkX4QF9lNni6ulxSC01prAUudv93pI48Xmaw4fV5m17ycwmO34=@vger.kernel.org X-Gm-Message-State: AOJu0YxBa2Q6BnF1F8G91pTsYB46RrjMEgDEpo5Fdj6YPJzhL/mLMYl2 oJRW3YRk3PAgntPs7l929bBCRtRb3kUBVdMWsqWKViZkgDhKxyydJkayoxmGi0K4Qvq2Iu4zgb6 wvPWhisD+jqV1ae7v3w== X-Google-Smtp-Source: AGHT+IEEYDooFC6yZwCAZXg+Qe55IRgCOFsgi1vdwniEiq9a0JjJhwh4h37DZWM3PfdySr35aQ8I4TeUG3Jg4tag X-Received: from pjbpv10.prod.google.com ([2002:a17:90b:3c8a:b0:2f2:ea3f:34c3]) (user=vannapurve job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:51c6:b0:2f2:ab09:c256 with SMTP id 98e67ed59e1d1-2fbf5c6abf5mr1891723a91.33.1739318902913; Tue, 11 Feb 2025 16:08:22 -0800 (PST) Date: Wed, 12 Feb 2025 00:07:47 +0000 In-Reply-To: <20250212000747.3403836-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: <20250212000747.3403836-1-vannapurve@google.com> X-Mailer: git-send-email 2.48.1.502.g6dc24dfdaf-goog Message-ID: <20250212000747.3403836-5-vannapurve@google.com> Subject: [PATCH V4 4/4] x86/tdx: Remove TDX specific idle routine From: Vishal Annapurve To: x86@kernel.org, linux-kernel@vger.kernel.org Cc: pbonzini@redhat.com, seanjc@google.com, erdemaktas@google.com, ackerleytng@google.com, jxgao@google.com, sagis@google.com, oupton@google.com, pgonda@google.com, kirill@shutemov.name, dave.hansen@linux.intel.com, linux-coco@lists.linux.dev, chao.p.peng@linux.intel.com, isaku.yamahata@gmail.com, sathyanarayanan.kuppuswamy@linux.intel.com, Vishal Annapurve Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" With explicit dependency on CONFIG_PARAVIRT and TDX specific halt()/safe_halt() routines in place, default_idle() is safe to execute for TDX VMs. Remove TDX specific idle routine override which is now redundant. Signed-off-by: Vishal Annapurve --- arch/x86/coco/tdx/tdx.c | 2 +- arch/x86/include/asm/tdx.h | 2 -- arch/x86/kernel/process.c | 3 --- 3 files changed, 1 insertion(+), 6 deletions(-) diff --git a/arch/x86/coco/tdx/tdx.c b/arch/x86/coco/tdx/tdx.c index 54baf93d9218..3578f7f7a502 100644 --- a/arch/x86/coco/tdx/tdx.c +++ b/arch/x86/coco/tdx/tdx.c @@ -407,7 +407,7 @@ static int handle_halt(struct ve_info *ve) return ve_instr_len(ve); } =20 -void __cpuidle tdx_halt(void) +static void __cpuidle tdx_halt(void) { const bool irq_disabled =3D false; =20 diff --git a/arch/x86/include/asm/tdx.h b/arch/x86/include/asm/tdx.h index 393ee2dfaab1..6769d1da4c80 100644 --- a/arch/x86/include/asm/tdx.h +++ b/arch/x86/include/asm/tdx.h @@ -58,8 +58,6 @@ 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_halt(void); - bool tdx_early_handle_ve(struct pt_regs *regs); =20 int tdx_mcall_get_report0(u8 *reportdata, u8 *tdreport); diff --git a/arch/x86/kernel/process.c b/arch/x86/kernel/process.c index d11956a178df..9b21989c283b 100644 --- a/arch/x86/kernel/process.c +++ b/arch/x86/kernel/process.c @@ -932,9 +932,6 @@ void __init select_idle_routine(void) if (prefer_mwait_c1_over_halt()) { pr_info("using mwait in idle threads\n"); 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_halt); } else { static_call_update(x86_idle, default_idle); } --=20 2.48.1.502.g6dc24dfdaf-goog