From nobody Tue May 5 10:13:32 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass header.i=teddy.astie@vates.tech; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=none dis=none) header.from=vates.tech ARC-Seal: i=1; a=rsa-sha256; t=1776259957; cv=none; d=zohomail.com; s=zohoarc; b=VmeUyWcrQto+0Jrc6pQfJ+W6VZEBpfklsQc6dIy8wusdeG7eHrx0bIMpuYYsdiijUC/6i4EXRLHy/orGMZwWYlexGDjN+RnhIvGiuOtqRtb4Ahb1wQCcnBj2H7VkpogdJJFQXfUfmz5wzq29ffEbDxI7TiFByieyTCVUvSSujAs= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1776259957; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=3eORvMGgdV2JgrkelULgZntYhi01D/h/hCD6psmBUzQ=; b=SLdmLfdL1Lq5t0HePq0GmcBH8bqej/Sv18/ygmuyMNWJTv/CmzpTuhFuRoITtx+4x/d/W6S/9BK4K7lLEAWQpAtZwM/r9sYMc7VyRdya2ujhmiKHq4BuzkMtzBJ0gkCR7EqjOxypWAw7MZplLHBoGCoviBhIBCRHMtJ81x/F11g= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass header.i=teddy.astie@vates.tech; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1776259957458564.7515187831385; Wed, 15 Apr 2026 06:32:37 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1282447.1565020 (Exim 4.92) (envelope-from ) id 1wD0Lp-0008UO-DY; Wed, 15 Apr 2026 13:32:17 +0000 Received: by outflank-mailman (output) from mailman id 1282447.1565020; Wed, 15 Apr 2026 13:32:17 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1wD0Lp-0008UH-Ak; Wed, 15 Apr 2026 13:32:17 +0000 Received: by outflank-mailman (input) for mailman id 1282447; Wed, 15 Apr 2026 13:32:16 +0000 Received: from mx.expurgate.net ([195.190.135.10]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1wD0Lo-0008UB-Mh for xen-devel@lists.xenproject.org; Wed, 15 Apr 2026 13:32:16 +0000 Received: from mx.expurgate.net (helo=localhost) by mx.expurgate.net with esmtp id 1wD0Lo-00Ad2n-35 for xen-devel@lists.xenproject.org; Wed, 15 Apr 2026 15:32:16 +0200 Received: from [10.42.69.12] (helo=localhost) by localhost with ESMTP (eXpurgate MTA 0.9.1) (envelope-from ) id 69df9358-2eae-0a2a0a5409dd-0a2a450cbe98-26 for ; Wed, 15 Apr 2026 15:32:16 +0200 Received: from [198.2.187.14] (helo=mail187-14.suw11.mandrillapp.com) by tlsNG-d25034.mxtls.expurgate.net with ESMTPS (eXpurgate 4.56.0) (envelope-from ) id 69df935e-f40c-0a2a450c0019-c602bb0e1bb3-3 for ; Wed, 15 Apr 2026 15:32:15 +0200 Received: from pmta09.mandrill.prod.suw01.rsglab.com (localhost [127.0.0.1]) by mail187-14.suw11.mandrillapp.com (Mailchimp) with ESMTP id 4fwhrt474bz8XSCwc for ; Wed, 15 Apr 2026 13:32:14 +0000 (GMT) Received: from [37.26.189.201] by mandrillapp.com id 1a795d0610064f5681462d6652086e28; Wed, 15 Apr 2026 13:32:14 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" Authentication-Results: eu.smtp.expurgate.cloud; dkim=pass header.s=mte1 header.d=mandrillapp.com header.i="@mandrillapp.com" header.h="From:Subject:To:Cc:Message-Id:In-Reply-To:References:Feedback-ID:Date:MIME-Version:Content-Type:Content-Transfer-Encoding"; dkim=pass header.s=mte1 header.d=vates.tech header.i="teddy.astie@vates.tech" header.h="From:Subject:To:Cc:Message-Id:In-Reply-To:References:Feedback-ID:Date:MIME-Version:Content-Type:Content-Transfer-Encoding" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=mandrillapp.com; s=mte1; t=1776259934; x=1776529934; bh=3eORvMGgdV2JgrkelULgZntYhi01D/h/hCD6psmBUzQ=; h=From:Subject:To:Cc:Message-Id:In-Reply-To:References:Feedback-ID: Date:MIME-Version:Content-Type:Content-Transfer-Encoding:CC:Date: Subject:From; b=15Gve7/aVyAjshyU9xqF45HVlaLk1cz2WuA1ckl8tNQwsy90JF/oFEeCrjj0xw276 R+XcfCczNFmqTM8hr8IBzKxkXui5UbNwCXO+NdoSiu70KgoMZjbZDEusRsVJrm8wdB y/WGMjtAt2ZZ+o6oKt5xXHzhH0DbfbEggDD53sG20tR/+g1u3MMx15bcF8Gw0W88M8 4+AqL8+JdBeynymMfBd8N3O20FongqlicGGCdv1aEsB1FX/bqj9bQkYwWGwYtnPqXE D/K5lsLZeFPFiDa5I6PvyCnV8gtvkHaeSgip9Rrkga3DFngA9FqHgUiua4S8Sio+m1 Jd0lhbd7Hl5Iw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=vates.tech; s=mte1; t=1776259934; x=1776520434; i=teddy.astie@vates.tech; bh=3eORvMGgdV2JgrkelULgZntYhi01D/h/hCD6psmBUzQ=; h=From:Subject:To:Cc:Message-Id:In-Reply-To:References:Feedback-ID: Date:MIME-Version:Content-Type:Content-Transfer-Encoding:CC:Date: Subject:From; b=aewLOIUc21gakTYdJKuHjfeDFOm3ibWen26ACcWO78RBFuXCYlAaocdFPenc56U8T UsMH9fXjPDtKuvSiPrvyzmlXCpgsRi81mAu7lPxPl+Tp88Sg08b4Y4KpqR/Ukrvtav JrxaNgimXczfB6bbpaajTnMUSs7DT96FcG+TEorDqKye7Qk2b2IrlkgVJodVhdL6rS KlNux50R7fFsQhZ7BSDY7c0ai9tDLP+2ph7uTykQ0savGllAhYoDsRiSu/HjQQfMF/ BhY7X3BQN3Ez8sI8ov30Xdz/896wzIqVbwLDEtqLGlaIKX3Bmdx6C7jf67Qh0oE/2O 4Us0B80lMQMLw== From: "Teddy Astie" Subject: =?utf-8?Q?[RFC=20PATCH=201/7]=20vmx:=20Introduce=20vcpu=20single=20context=20VPID=20invalidation?= X-Mailer: git-send-email 2.52.0 X-Bm-Disclaimer: Yes X-Bm-Milter-Handled: 4ffbd6c1-ee69-4e1b-aabd-f977039bd3e2 X-Bm-Transport-Timestamp: 1776259933722 To: xen-devel@lists.xenproject.org Cc: "Teddy Astie" , "Jan Beulich" , "Andrew Cooper" , "=?utf-8?Q?Roger=20Pau=20Monn=C3=A9?=" Message-Id: <50c96e2306538eb7cd28ddfcb21163c628fae0a4.1776259594.git.teddy.astie@vates.tech> In-Reply-To: References: X-Native-Encoded: 1 X-Report-Abuse: =?UTF-8?Q?Please=20forward=20a=20copy=20of=20this=20message,=20including=20all=20headers,=20to=20abuse@mandrill.com.=20You=20can=20also=20report=20abuse=20here:=20https://mandrillapp.com/contact/abuse=3Fid=3D30504962.1a795d0610064f5681462d6652086e28?= X-Mandrill-User: md_30504962 Feedback-ID: 30504962:30504962.20260415:md Date: Wed, 15 Apr 2026 13:32:14 +0000 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-purgate-ID: tlsNG-d25034/1776259936-FF547A3D-0EE0BC28/0/0 X-purgate-type: clean X-purgate-size: 1506 X-ZohoMail-DKIM: pass (identity teddy.astie@vates.tech) (identity @mandrillapp.com) X-ZM-MESSAGEID: 1776259959795154100 Content-Type: text/plain; charset="utf-8" Introduce vpid_sync_vcpu_context to do a single-context invalidation on the vpid attached to the vcpu as a alternative to per-gva and all-context invlidations. Signed-off-by: Teddy Astie --- xen/arch/x86/include/asm/hvm/vmx/vmx.h | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/xen/arch/x86/include/asm/hvm/vmx/vmx.h b/xen/arch/x86/include/= asm/hvm/vmx/vmx.h index da04752e17..3524cb3536 100644 --- a/xen/arch/x86/include/asm/hvm/vmx/vmx.h +++ b/xen/arch/x86/include/asm/hvm/vmx/vmx.h @@ -452,6 +452,27 @@ static inline void ept_sync_all(void) =20 void ept_sync_domain(struct p2m_domain *p2m); =20 +static inline void vpid_sync_vcpu_context(const struct vcpu *v) +{ + int type =3D INVVPID_SINGLE_CONTEXT; + + /* + * If single context invalidation is not supported, we escalate to + * use all context invalidation. + */ + if ( likely(cpu_has_vmx_vpid_invvpid_single_context) ) + goto execute_invvpid; + + /* + * If single context invalidation is not supported, we escalate to + * use all context invalidation. + */ + type =3D INVVPID_ALL_CONTEXT; + +execute_invvpid: + __invvpid(type, v->arch.hvm.n1asid.asid, 0); +} + static inline void vpid_sync_vcpu_gva(struct vcpu *v, unsigned long gva) { int type =3D INVVPID_INDIVIDUAL_ADDR; --=20 2.52.0 -- Teddy Astie | Vates XCP-ng Developer XCP-ng & Xen Orchestra - Vates solutions web: https://vates.tech From nobody Tue May 5 10:13:32 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass header.i=teddy.astie@vates.tech; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=none dis=none) header.from=vates.tech ARC-Seal: i=1; a=rsa-sha256; t=1776259964; cv=none; d=zohomail.com; s=zohoarc; b=dO5lE1Dac0edeN1FI2qyLwaIFz8llOuFQurolhJhoySCVvGo1AQ8BQuSsjJpB0Auje6Gk9JfzjVlgH8n+jVpFJbHCfJYUpGUt4P9biEnqvkEnVrxYR606NJ6aj8GkPH/PpJ2wUJoN3twKfGgNGJRX+3EkWGKO9/HqKHhzNm4Sfc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1776259964; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=V1xNEuLkT/UoGYapzOaT2ccLIg+S5oeSeb8h+8Yriq0=; b=mhX2ZQQtqVODjvyINCPVBPw/HM/ebulsjxhD7fdliLeTuiQLOJO/bqjepxh8MpuXrCgrS6Nm9rM01iCyKTMzOUxyknwzzxHB+qIFxI5+hH/4vx5o4CafjUkX6z2chdhNTwPlkSK94lU+yMOboKdI8V9CChWJSpNR2/Le5HqA8Cc= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass header.i=teddy.astie@vates.tech; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1776259964120287.2186956371031; Wed, 15 Apr 2026 06:32:44 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1282448.1565029 (Exim 4.92) (envelope-from ) id 1wD0Lr-0000Gj-Ko; Wed, 15 Apr 2026 13:32:19 +0000 Received: by outflank-mailman (output) from mailman id 1282448.1565029; Wed, 15 Apr 2026 13:32:19 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1wD0Lr-0000Gc-Hv; Wed, 15 Apr 2026 13:32:19 +0000 Received: by outflank-mailman (input) for mailman id 1282448; Wed, 15 Apr 2026 13:32:18 +0000 Received: from mx.expurgate.net ([195.190.135.10]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1wD0Lq-0000GL-Or for xen-devel@lists.xenproject.org; Wed, 15 Apr 2026 13:32:18 +0000 Received: from mx.expurgate.net (helo=localhost) by mx.expurgate.net with esmtp id 1wD0Lq-00DDma-1o for xen-devel@lists.xenproject.org; Wed, 15 Apr 2026 15:32:18 +0200 Received: from [10.42.69.1] (helo=localhost) by localhost with ESMTP (eXpurgate MTA 0.9.1) (envelope-from ) id 69df9357-e002-0a2a0a5209dd-0a2a4501b888-48 for ; Wed, 15 Apr 2026 15:32:18 +0200 Received: from [198.2.187.14] (helo=mail187-14.suw11.mandrillapp.com) by tlsNG-d62444.mxtls.expurgate.net with ESMTPS (eXpurgate 4.56.1) (envelope-from ) id 69df9360-c1f2-0a2a45010019-c602bb0eba87-3 for ; Wed, 15 Apr 2026 15:32:17 +0200 Received: from pmta09.mandrill.prod.suw01.rsglab.com (localhost [127.0.0.1]) by mail187-14.suw11.mandrillapp.com (Mailchimp) with ESMTP id 4fwhrw3phtz8XSHSC for ; Wed, 15 Apr 2026 13:32:16 +0000 (GMT) Received: from [37.26.189.201] by mandrillapp.com id 2f204a884a51440aa20c387e5f905bb7; Wed, 15 Apr 2026 13:32:16 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" Authentication-Results: eu.smtp.expurgate.cloud; dkim=pass header.s=mte1 header.d=mandrillapp.com header.i="@mandrillapp.com" header.h="From:Subject:To:Cc:Message-Id:In-Reply-To:References:Feedback-ID:Date:MIME-Version:Content-Type:Content-Transfer-Encoding"; dkim=pass header.s=mte1 header.d=vates.tech header.i="teddy.astie@vates.tech" header.h="From:Subject:To:Cc:Message-Id:In-Reply-To:References:Feedback-ID:Date:MIME-Version:Content-Type:Content-Transfer-Encoding" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=mandrillapp.com; s=mte1; t=1776259936; x=1776529936; bh=V1xNEuLkT/UoGYapzOaT2ccLIg+S5oeSeb8h+8Yriq0=; h=From:Subject:To:Cc:Message-Id:In-Reply-To:References:Feedback-ID: Date:MIME-Version:Content-Type:Content-Transfer-Encoding:CC:Date: Subject:From; b=WXZh8Dzd8Jh/HUh0lgwEq2jolZ+EXZZ7ZvGjDjnMle6r3u7Zrw44uVMOCQml9LTay j3+E734Z/i7cr7aEbZu8QrOq75ZZ8u27s9OPQi3lttmksmCcKgdh/C0zwEOAF3R/x8 EcFhVuDLe7KZS4l+suQkXOJH6FQ5BeY4cjehcHjwQECprEnySKOKoxby4G0hKGFiCI acGELDQbHM4RFVy1tnccCtM6YIUJXzdrqdJMLZ5TsSck9YqL49VyeJamgPAFEvYpMt WP0dIuE7bT0d+WSPb+fC4ArvJ6eql1bA0F1y13iZgxDHw2mMUK6wpC92Bh7oXsjcIM LqVKnwMiAY6Mw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=vates.tech; s=mte1; t=1776259936; x=1776520436; i=teddy.astie@vates.tech; bh=V1xNEuLkT/UoGYapzOaT2ccLIg+S5oeSeb8h+8Yriq0=; h=From:Subject:To:Cc:Message-Id:In-Reply-To:References:Feedback-ID: Date:MIME-Version:Content-Type:Content-Transfer-Encoding:CC:Date: Subject:From; b=QqmXDzKPu0zPeEW63cqKXrsHH/05zS9IyAZVmh4ynlQ6NM3HoYbDH7vKPhF2b2hO6 JbAUd+ssbjHpS4jvZ/kGHPnhzeEqmuYLRZV/EjofnYLBwFXgUbQlhaBjTruq5mu4Zz BaX4mk+Uqlg5OmcorAOMDhndUvYIpKeg9HyKrUgDbA5UDr1YKJUandjrSuayg2SILl KQu6g0R7buUUn92ddGU3a/luHF2d+YmAqbd8csK/b5AqoMjMqxA3Eemi1zjKoN/WgE M+JTHsWMg0L4ESL+tSBQ9cUyl73F4HzvrJBoyuc+VQ7XSkAeGjrGcChoQR/RkMM5e3 4GlwhHOp9Bbww== From: "Teddy Astie" Subject: =?utf-8?Q?[RFC=20PATCH=202/7]=20common:=20Track=20latest=20pCPU=20that=20ran=20the=20vCPU?= X-Mailer: git-send-email 2.52.0 X-Bm-Disclaimer: Yes X-Bm-Milter-Handled: 4ffbd6c1-ee69-4e1b-aabd-f977039bd3e2 X-Bm-Transport-Timestamp: 1776259935319 To: xen-devel@lists.xenproject.org Cc: "Teddy Astie" , "Jan Beulich" , "Andrew Cooper" , "=?utf-8?Q?Roger=20Pau=20Monn=C3=A9?=" , "Jason Andryuk" , "Anthony PERARD" , "Michal Orzel" , "Julien Grall" , "Stefano Stabellini" Message-Id: In-Reply-To: References: X-Native-Encoded: 1 X-Report-Abuse: =?UTF-8?Q?Please=20forward=20a=20copy=20of=20this=20message,=20including=20all=20headers,=20to=20abuse@mandrill.com.=20You=20can=20also=20report=20abuse=20here:=20https://mandrillapp.com/contact/abuse=3Fid=3D30504962.2f204a884a51440aa20c387e5f905bb7?= X-Mandrill-User: md_30504962 Feedback-ID: 30504962:30504962.20260415:md Date: Wed, 15 Apr 2026 13:32:16 +0000 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-purgate-ID: tlsNG-d62444/1776259938-B5E6BFF4-9814C635/0/0 X-purgate-type: clean X-purgate-size: 3524 X-ZohoMail-DKIM: pass (identity teddy.astie@vates.tech) (identity @mandrillapp.com) X-ZM-MESSAGEID: 1776259966514158501 Content-Type: text/plain; charset="utf-8" Track on which pCPU each vCPU of a domain ran. This will be used to know whether a TLB flush is required or not when the vCPU is migrated on another pCPU. Signed-off-by: Teddy Astie --- I wonder if there is a way to move=20 curr->domain->latest_vcpu[cpu] =3D curr->vcpu_id into a (at least more) common code ? xen/arch/x86/hvm/svm/svm.c | 3 +++ xen/arch/x86/hvm/vmx/vmx.c | 3 +++ xen/common/domain.c | 8 ++++++++ xen/include/xen/sched.h | 4 ++++ 4 files changed, 18 insertions(+) diff --git a/xen/arch/x86/hvm/svm/svm.c b/xen/arch/x86/hvm/svm/svm.c index ced6166847..58e927ae04 100644 --- a/xen/arch/x86/hvm/svm/svm.c +++ b/xen/arch/x86/hvm/svm/svm.c @@ -967,6 +967,7 @@ void asmlinkage svm_vmenter_helper(void) const struct cpu_user_regs *regs =3D guest_cpu_user_regs(); struct vcpu *curr =3D current; struct vmcb_struct *vmcb =3D curr->arch.hvm.svm.vmcb; + unsigned int cpu =3D smp_processor_id(); =20 ASSERT(hvmemul_cache_disabled(curr)); =20 @@ -977,6 +978,8 @@ void asmlinkage svm_vmenter_helper(void) =20 svm_sync_vmcb(curr, vmcb_needs_vmsave); =20 + curr->domain->latest_vcpu[cpu] =3D curr->vcpu_id; + vmcb->rax =3D regs->rax; vmcb->rip =3D regs->rip; vmcb->rsp =3D regs->rsp; diff --git a/xen/arch/x86/hvm/vmx/vmx.c b/xen/arch/x86/hvm/vmx/vmx.c index 269ca56433..ec0a790336 100644 --- a/xen/arch/x86/hvm/vmx/vmx.c +++ b/xen/arch/x86/hvm/vmx/vmx.c @@ -4934,6 +4934,7 @@ bool asmlinkage vmx_vmenter_helper(const struct cpu_u= ser_regs *regs) u32 new_asid, old_asid; struct hvm_vcpu_asid *p_asid; bool need_flush; + unsigned int cpu =3D smp_processor_id(); =20 ASSERT(hvmemul_cache_disabled(curr)); =20 @@ -4977,6 +4978,8 @@ bool asmlinkage vmx_vmenter_helper(const struct cpu_u= ser_regs *regs) if ( unlikely(need_flush) ) vpid_sync_all(); =20 + currd->latest_vcpu[cpu] =3D curr->vcpu_id; + if ( paging_mode_hap(curr->domain) ) { struct ept_data *ept =3D &p2m_get_hostp2m(currd)->ept; diff --git a/xen/common/domain.c b/xen/common/domain.c index bb9e210c28..7867166411 100644 --- a/xen/common/domain.c +++ b/xen/common/domain.c @@ -758,6 +758,7 @@ static void _domain_destroy(struct domain *d) rangeset_domain_destroy(d); =20 free_cpumask_var(d->dirty_cpumask); + xfree(d->latest_vcpu); =20 xsm_free_security_domain(d); =20 @@ -992,6 +993,13 @@ struct domain *domain_create(domid_t domid, if ( !zalloc_cpumask_var(&d->dirty_cpumask) ) goto fail; =20 + err =3D -ENOMEM; + d->latest_vcpu =3D xmalloc_array(int, nr_cpu_ids); + if ( !d->latest_vcpu ) + goto fail; + for (unsigned int i =3D 0; i < nr_cpu_ids; i++) + d->latest_vcpu[i] =3D -1; + rangeset_domain_initialise(d); =20 if ( is_idle_domain(d) ) diff --git a/xen/include/xen/sched.h b/xen/include/xen/sched.h index 212c7d765c..4b8ae21b51 100644 --- a/xen/include/xen/sched.h +++ b/xen/include/xen/sched.h @@ -567,6 +567,10 @@ struct domain /* Bitmask of CPUs which are holding onto this domain's state. */ cpumask_var_t dirty_cpumask; =20 + /* Mapping of the latest vCPU that ran on a specific CPU + * (-1 if the vCPU hasn't ran yet) */ + int *latest_vcpu; + struct arch_domain arch; =20 void *ssid; /* sHype security subject identifier */ --=20 2.52.0 -- Teddy Astie | Vates XCP-ng Developer XCP-ng & Xen Orchestra - Vates solutions web: https://vates.tech From nobody Tue May 5 10:13:32 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass header.i=teddy.astie@vates.tech; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=none dis=none) header.from=vates.tech ARC-Seal: i=1; a=rsa-sha256; t=1776261292; cv=none; d=zohomail.com; s=zohoarc; b=VuTRy9xzKhBb21uttDB1+iZDJwWZQ8+MLTZuyO8g4nQMotTRF1/pvNN005Lbf3wXZnzhBfOqu5CDyFqHKbuTc7EdZmPpdfkkfccHglqOUIa5eOGS6xaRbcJvOAcVt+5AQ6j4Z59yQ4M168D7/2MhwQiCMrsnEDqKhksLpos+oPI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1776261292; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=cDN1jWQB46rVs3zOCKAJTcEcv7n7xu0idZzLK6Twz+k=; b=ABz/qjqaGqQbMzc3arHsouT2ALBWwSQyaBJPv/K+afDiCezs8sJmTpAS0wySoVRwR47/SboqHtsj5TAwZRLfec32O7T23maqxWCtxR3wBKTcfiM5VyQA326/Z8GcxrGpGb7mG6b70o/0MJ8GbY8I/8mWOUNVJIvKxtJNYxzo+xI= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass header.i=teddy.astie@vates.tech; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1776261292782717.8941670395001; Wed, 15 Apr 2026 06:54:52 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1282510.1565083 (Exim 4.92) (envelope-from ) id 1wD0hM-0005zL-GV; Wed, 15 Apr 2026 13:54:32 +0000 Received: by outflank-mailman (output) from mailman id 1282510.1565083; Wed, 15 Apr 2026 13:54:32 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1wD0hM-0005zE-Du; Wed, 15 Apr 2026 13:54:32 +0000 Received: by outflank-mailman (input) for mailman id 1282510; Wed, 15 Apr 2026 13:54:30 +0000 Received: from mx.expurgate.net ([195.190.135.10]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1wD0hK-0005mE-Rh for xen-devel@lists.xenproject.org; Wed, 15 Apr 2026 13:54:30 +0000 Received: from mx.expurgate.net (helo=localhost) by mx.expurgate.net with esmtp id 1wD0hK-002UaC-75 for xen-devel@lists.xenproject.org; Wed, 15 Apr 2026 15:54:30 +0200 Received: from [10.42.69.4] (helo=localhost) by localhost with ESMTP (eXpurgate MTA 0.9.1) (envelope-from ) id 69df9895-2eae-0a2a0a5409dd-0a2a4504a5da-6 for ; Wed, 15 Apr 2026 15:54:30 +0200 Received: from [198.2.187.1] (helo=mail187-1.suw11.mandrillapp.com) by tlsNG-ebf023.mxtls.expurgate.net with ESMTPS (eXpurgate 4.56.0) (envelope-from ) id 69df9894-bb33-0a2a45040019-c602bb01fc7d-3 for ; Wed, 15 Apr 2026 15:54:29 +0200 Received: from pmta09.mandrill.prod.suw01.rsglab.com (localhost [127.0.0.1]) by mail187-1.suw11.mandrillapp.com (Mailchimp) with ESMTP id 4fwhrx69gkzBsbN6V for ; Wed, 15 Apr 2026 13:32:17 +0000 (GMT) Received: from [37.26.189.201] by mandrillapp.com id 58b0a3bb1a944e24a0083559f9875d46; Wed, 15 Apr 2026 13:32:17 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" Authentication-Results: eu.smtp.expurgate.cloud; dkim=pass header.s=mte1 header.d=mandrillapp.com header.i="@mandrillapp.com" header.h="From:Subject:To:Cc:Message-Id:In-Reply-To:References:Feedback-ID:Date:MIME-Version:Content-Type:Content-Transfer-Encoding"; dkim=pass header.s=mte1 header.d=vates.tech header.i="teddy.astie@vates.tech" header.h="From:Subject:To:Cc:Message-Id:In-Reply-To:References:Feedback-ID:Date:MIME-Version:Content-Type:Content-Transfer-Encoding" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=mandrillapp.com; s=mte1; t=1776259937; x=1776529937; bh=cDN1jWQB46rVs3zOCKAJTcEcv7n7xu0idZzLK6Twz+k=; h=From:Subject:To:Cc:Message-Id:In-Reply-To:References:Feedback-ID: Date:MIME-Version:Content-Type:Content-Transfer-Encoding:CC:Date: Subject:From; b=b7YKXzwBRqXevTTdluknHhPQVsV4/0GoTs6tYB6Fl1/uxvl6Il6b+bvwDmz9w4DKS 1ASvxd8tvRDh7cED8IQQci6fjhrNwua4ceUfLUM2pgU/fn2GHfIVaqJ3CcMbA7OJdW BeN/oB+VMZf4Ti008he9IbImrciNxrTbbgbhcZBSFOccWu4MpLgCdrjXuzxiKOyzfY 8U6HdKOwesVOGC8/Pdw3nHkTT4N4l7S2M/M8ypcTGKnMNkKvtbiyEXd8MIAJ3OBbSU zjnUAAZcXrnZOGxgaIZYMARWpKZyRHXhmmOWdLIdynJVaJJGcFWk+5/7E2JT09klrq ZkLTRqhpZKv9Q== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=vates.tech; s=mte1; t=1776259937; x=1776520437; i=teddy.astie@vates.tech; bh=cDN1jWQB46rVs3zOCKAJTcEcv7n7xu0idZzLK6Twz+k=; h=From:Subject:To:Cc:Message-Id:In-Reply-To:References:Feedback-ID: Date:MIME-Version:Content-Type:Content-Transfer-Encoding:CC:Date: Subject:From; b=KOXLsXqTIZ85Ipkyr+nrJkb5ifofsFfgCVQK8WLLIfkdBHiwZNLwzRy2Jas7SZx+p ZcH2lCfnXHQbV95qHbyn6H1jYqgHgefHdykZDPl2UUsoEnsnezHTh0MR22Hz2S+UAp YcJUeC9PLgawjOn3Zbkl2MyUtjzT5yXfAgAI6U788QAv/Z0a7n9bxh/7/SqIJShNT7 lqM4S8si0zgj1+XiMePpZLjZ36qgfPiwGY+yuq7EKfcAwEr0CYU+1ntzeIfCWDJUkb hmlmigMNhcXdun8dQXzWudpPoMmzebfazBzI2qgeE1UTAsb2gzftQp9CaJMeujZYxy KjFAUVnNyuFAg== From: "Teddy Astie" Subject: =?utf-8?Q?[RFC=20PATCH=203/7]=20common:=20Introduce=20needs=5Ftlb=5Fflush=20vcpu=20field?= X-Mailer: git-send-email 2.52.0 X-Bm-Disclaimer: Yes X-Bm-Milter-Handled: 4ffbd6c1-ee69-4e1b-aabd-f977039bd3e2 X-Bm-Transport-Timestamp: 1776259936778 To: xen-devel@lists.xenproject.org Cc: "Teddy Astie" , "Dario Faggioli" , "Juergen Gross" , "George Dunlap" , "Andrew Cooper" , "Anthony PERARD" , "Michal Orzel" , "Jan Beulich" , "Julien Grall" , "=?utf-8?Q?Roger=20Pau=20Monn=C3=A9?=" , "Stefano Stabellini" Message-Id: <76d570a1b444a0bf2308389d9ed8bef8878b0c96.1776259594.git.teddy.astie@vates.tech> In-Reply-To: References: X-Native-Encoded: 1 X-Report-Abuse: =?UTF-8?Q?Please=20forward=20a=20copy=20of=20this=20message,=20including=20all=20headers,=20to=20abuse@mandrill.com.=20You=20can=20also=20report=20abuse=20here:=20https://mandrillapp.com/contact/abuse=3Fid=3D30504962.58b0a3bb1a944e24a0083559f9875d46?= X-Mandrill-User: md_30504962 Feedback-ID: 30504962:30504962.20260415:md Date: Wed, 15 Apr 2026 13:32:17 +0000 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-purgate-ID: tlsNG-ebf023/1776261270-B112D51B-F48AC452/0/0 X-purgate-type: clean X-purgate-size: 1892 X-ZohoMail-DKIM: pass (identity @mandrillapp.com) (identity teddy.astie@vates.tech) X-ZM-MESSAGEID: 1776261294660154100 Content-Type: text/plain; charset="utf-8" This field is meant to be used to schedule a TLB flush on the vCPU before waking it up. This field can be set from another vCPU at any time. Schedule a TLB flush when the vCPU is migrated to another CPU. This is needed as the vCPU-related TLB entries may be out of sync with what happened on another core. Currently, no architecture use this mechanism, but it is meant to be used as a way to schedule a TLB flush on the vCPU. Signed-off-by: Teddy Astie --- xen/common/sched/core.c | 5 +++++ xen/include/xen/sched.h | 2 ++ 2 files changed, 7 insertions(+) diff --git a/xen/common/sched/core.c b/xen/common/sched/core.c index a57d5dd929..f8e615b3af 100644 --- a/xen/common/sched/core.c +++ b/xen/common/sched/core.c @@ -1188,7 +1188,12 @@ static void sched_unit_migrate_finish(struct sched_u= nit *unit) =20 /* Wake on new CPU. */ for_each_sched_unit_vcpu ( unit, v ) + { + if ( old_cpu !=3D new_cpu ) + /* Migrating to another CPU needs TLB flush */ + v->needs_tlb_flush =3D true; vcpu_wake(v); + } } =20 static bool sched_check_affinity_broken(const struct sched_unit *unit) diff --git a/xen/include/xen/sched.h b/xen/include/xen/sched.h index 4b8ae21b51..a26c571015 100644 --- a/xen/include/xen/sched.h +++ b/xen/include/xen/sched.h @@ -224,6 +224,8 @@ struct vcpu bool defer_shutdown; /* VCPU is paused following shutdown request (d->is_shutting_down)? */ bool paused_for_shutdown; + /* VCPU needs its TLB flushed before waking. */ + bool needs_tlb_flush; /* VCPU need affinity restored */ uint8_t affinity_broken; #define VCPU_AFFINITY_OVERRIDE 0x01 --=20 2.52.0 -- Teddy Astie | Vates XCP-ng Developer XCP-ng & Xen Orchestra - Vates solutions web: https://vates.tech From nobody Tue May 5 10:13:32 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass header.i=teddy.astie@vates.tech; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=none dis=none) header.from=vates.tech ARC-Seal: i=1; a=rsa-sha256; t=1776259961; cv=none; d=zohomail.com; s=zohoarc; b=BzwQ8eiAfqGaW8FxMBa6cE7sEWLmT7itaLlyHAxxAjmWlwDaUdoDXyCd4sjgBHKI34utRu4pbpdkyvIPdMm1569YFYUzABOMccc/nhyqVRXGiv+keheIHRInLAZCUGmpmcthbszAvwkReSnXLhouihTZEYIqlMiNtcBeSg3hF0g= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1776259961; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=6GgW9zqjU7eafFgo+aMF5dqgD8VnWAi/yFVJIMHbHCg=; b=go/0AA9JLXc/tCLSMe3Zc72aRohL7LVd3f0PjDiJIwBUhF/pcV4yu2ltlGEzWq8H+0qpxxeY04IYi9uqI3fS9fqHr5O5hMtbs1trZM3anxdjjyfdsU/nl0DqlZ24KLqNuaVK89u8xa9D2Zrvn/A8fMSVJOgB0Nidnmxx+QHm+SA= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass header.i=teddy.astie@vates.tech; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1776259961017193.11197655765943; Wed, 15 Apr 2026 06:32:41 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1282449.1565039 (Exim 4.92) (envelope-from ) id 1wD0Lw-0000XS-S6; Wed, 15 Apr 2026 13:32:24 +0000 Received: by outflank-mailman (output) from mailman id 1282449.1565039; Wed, 15 Apr 2026 13:32:24 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1wD0Lw-0000XL-PS; Wed, 15 Apr 2026 13:32:24 +0000 Received: by outflank-mailman (input) for mailman id 1282449; Wed, 15 Apr 2026 13:32:22 +0000 Received: from mx.expurgate.net ([195.190.135.10]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1wD0Lu-0000Vi-R3 for xen-devel@lists.xenproject.org; Wed, 15 Apr 2026 13:32:22 +0000 Received: from mx.expurgate.net (helo=localhost) by mx.expurgate.net with esmtp id 1wD0Lu-002WU8-7f for xen-devel@lists.xenproject.org; Wed, 15 Apr 2026 15:32:22 +0200 Received: from [10.42.69.6] (helo=localhost) by localhost with ESMTP (eXpurgate MTA 0.9.1) (envelope-from ) id 69df935d-bab6-0a2a0a5309dd-0a2a45068222-44 for ; Wed, 15 Apr 2026 15:32:22 +0200 Received: from [198.2.186.1] (helo=mail186-1.suw21.mandrillapp.com) by tlsNG-16d1c6.mxtls.expurgate.net with ESMTPS (eXpurgate 4.56.0) (envelope-from ) id 69df9365-0df0-0a2a45060019-c602ba012713-3 for ; Wed, 15 Apr 2026 15:32:21 +0200 Received: from pmta10.mandrill.prod.suw01.rsglab.com (localhost [127.0.0.1]) by mail186-1.suw21.mandrillapp.com (Mailchimp) with ESMTP id 4fwhs05lXXzBsV0jL for ; Wed, 15 Apr 2026 13:32:20 +0000 (GMT) Received: from [37.26.189.201] by mandrillapp.com id 549f1328209a4bf6a574e4c88ba3cd82; Wed, 15 Apr 2026 13:32:20 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" Authentication-Results: eu.smtp.expurgate.cloud; dkim=pass header.s=mte1 header.d=mandrillapp.com header.i="@mandrillapp.com" header.h="From:Subject:To:Cc:Message-Id:In-Reply-To:References:Feedback-ID:Date:MIME-Version:Content-Type:Content-Transfer-Encoding"; dkim=pass header.s=mte1 header.d=vates.tech header.i="teddy.astie@vates.tech" header.h="From:Subject:To:Cc:Message-Id:In-Reply-To:References:Feedback-ID:Date:MIME-Version:Content-Type:Content-Transfer-Encoding" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=mandrillapp.com; s=mte1; t=1776259940; x=1776529940; bh=6GgW9zqjU7eafFgo+aMF5dqgD8VnWAi/yFVJIMHbHCg=; h=From:Subject:To:Cc:Message-Id:In-Reply-To:References:Feedback-ID: Date:MIME-Version:Content-Type:Content-Transfer-Encoding:CC:Date: Subject:From; b=M/DcqyVjR/cY6vyn1nwNEyOdFK41VqfPRkEf8M2hmsDIRAFT8OsZ/mMoK9FpeSdCu M4XM5s9vYiVDx7WOixvesMgEEsvJ3iEWNPEn8LAFoUvH2eXy28TGFtJyDiRmxjmaJI SiuAe+e61IuADQEmQc7TtAUGSyoWO+z2PWz9EnR4XLHjdFDSx0He8zcdVD/pBV7o7u 8+VgCnCQ02WSqJn81P0c7+zRQPJHTKn89PACppphLIRwwDjroL3TQAK/icmlLEDrLU Fnf1ZH+nWfbnPhD57jfaC4XnPr4ndRFz/NHFxKgtH1tuMFJFsWyNL/69Mnh7wLBxJb bgxaTo/QXk82w== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=vates.tech; s=mte1; t=1776259940; x=1776520440; i=teddy.astie@vates.tech; bh=6GgW9zqjU7eafFgo+aMF5dqgD8VnWAi/yFVJIMHbHCg=; h=From:Subject:To:Cc:Message-Id:In-Reply-To:References:Feedback-ID: Date:MIME-Version:Content-Type:Content-Transfer-Encoding:CC:Date: Subject:From; b=jithJSCaP3cYKzWK9+hoC7D6Nl4asINeWNx5+ssXORJ6d6cfKP2b9rHme4Nl1PnnW zRsj6HYHAFchkQNHVhBL22Tj+TvmyC/+2/dpX2GEf9anFD1N6HqtMmrxAsTsCpSKFG S5Oedq0nfY7ebLatQHsX2xq/HN0jr2qskwpSJp2Roc60xhwCF/KZJxta2skzvJU6TN YUgrgDzLBKUpcT9GBtQodtK4jG8C1EtfU0LoolYWoIrj6rQAaMSpWhQFPO2J7HogEh 4FIcUXORTsJLaUF5Lucy6JUMInZAe+bb/1rZ00JOBqX4zl9koGel8mEonC6I05kDVI DpWuXF/w4DvLw== From: "Teddy Astie" Subject: =?utf-8?Q?[RFC=20PATCH=204/7]=20x86:=20Set=20v->needs=5Ftlb=5Fflush=20when=20needed?= X-Mailer: git-send-email 2.52.0 X-Bm-Disclaimer: Yes X-Bm-Milter-Handled: 4ffbd6c1-ee69-4e1b-aabd-f977039bd3e2 X-Bm-Transport-Timestamp: 1776259938505 To: xen-devel@lists.xenproject.org Cc: "Teddy Astie" , "Jan Beulich" , "Andrew Cooper" , "=?utf-8?Q?Roger=20Pau=20Monn=C3=A9?=" , "Jason Andryuk" , "Tim Deegan" Message-Id: <3415abe736e46b3f36ccb045aa5144a371774fba.1776259594.git.teddy.astie@vates.tech> In-Reply-To: References: X-Native-Encoded: 1 X-Report-Abuse: =?UTF-8?Q?Please=20forward=20a=20copy=20of=20this=20message,=20including=20all=20headers,=20to=20abuse@mandrill.com.=20You=20can=20also=20report=20abuse=20here:=20https://mandrillapp.com/contact/abuse=3Fid=3D30504962.549f1328209a4bf6a574e4c88ba3cd82?= X-Mandrill-User: md_30504962 Feedback-ID: 30504962:30504962.20260415:md Date: Wed, 15 Apr 2026 13:32:20 +0000 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-purgate-ID: tlsNG-16d1c6/1776259942-5FB3B3D8-53BADBDA/0/0 X-purgate-type: clean X-purgate-size: 7788 X-ZohoMail-DKIM: pass (identity teddy.astie@vates.tech) (identity @mandrillapp.com) X-ZM-MESSAGEID: 1776259963748154100 Content-Type: text/plain; charset="utf-8" Sets v->needs_tlb_flush in where a tlb flush is expected to be scheduled on the vCPU. Signed-off-by: Teddy Astie --- The goal here is to avoid too much noise in [1], hence it currently cohexists with hvm_asid_flush_vcpu(), but [1] will drop hvm_asid_flush_vcpu() and only keep needs_tlb_flush. [1] x86/hvm: Transition to needs_tlb_flush logic, use per-domain ASID xen/arch/x86/flushtlb.c | 4 ++++ xen/arch/x86/hvm/emulate.c | 1 + xen/arch/x86/hvm/hvm.c | 1 + xen/arch/x86/hvm/svm/svm.c | 5 +++++ xen/arch/x86/hvm/vmx/vmcs.c | 1 + xen/arch/x86/hvm/vmx/vmx.c | 3 +++ xen/arch/x86/hvm/vmx/vvmx.c | 1 + xen/arch/x86/mm/p2m.c | 4 ++++ xen/arch/x86/mm/paging.c | 1 + xen/arch/x86/mm/shadow/multi.c | 1 + 10 files changed, 22 insertions(+) diff --git a/xen/arch/x86/flushtlb.c b/xen/arch/x86/flushtlb.c index 23721bb52c..8ee2385bba 100644 --- a/xen/arch/x86/flushtlb.c +++ b/xen/arch/x86/flushtlb.c @@ -324,7 +324,11 @@ unsigned int guest_flush_tlb_flags(const struct domain= *d) void guest_flush_tlb_mask(const struct domain *d, const cpumask_t *mask) { unsigned int flags =3D guest_flush_tlb_flags(d); + struct vcpu *v; =20 if ( flags ) flush_mask(mask, flags); + + for_each_vcpu(d, v) + v->needs_tlb_flush =3D true; } diff --git a/xen/arch/x86/hvm/emulate.c b/xen/arch/x86/hvm/emulate.c index f3aae158e9..3bc1d321cc 100644 --- a/xen/arch/x86/hvm/emulate.c +++ b/xen/arch/x86/hvm/emulate.c @@ -2657,6 +2657,7 @@ static int cf_check hvmemul_tlb_op( if ( x86emul_invpcid_type(aux) !=3D X86_INVPCID_INDIV_ADDR ) { hvm_asid_flush_vcpu(current); + current->needs_tlb_flush =3D true; break; } aux =3D x86emul_invpcid_pcid(aux); diff --git a/xen/arch/x86/hvm/hvm.c b/xen/arch/x86/hvm/hvm.c index 4a81afce02..0f0b0e242f 100644 --- a/xen/arch/x86/hvm/hvm.c +++ b/xen/arch/x86/hvm/hvm.c @@ -1613,6 +1613,7 @@ int hvm_vcpu_initialise(struct vcpu *v) struct domain *d =3D v->domain; =20 hvm_asid_flush_vcpu(v); + v->needs_tlb_flush =3D true; =20 spin_lock_init(&v->arch.hvm.tm_lock); INIT_LIST_HEAD(&v->arch.hvm.tm_list); diff --git a/xen/arch/x86/hvm/svm/svm.c b/xen/arch/x86/hvm/svm/svm.c index 58e927ae04..64c08432fd 100644 --- a/xen/arch/x86/hvm/svm/svm.c +++ b/xen/arch/x86/hvm/svm/svm.c @@ -138,6 +138,8 @@ static void cf_check svm_update_guest_cr( { if ( !(flags & HVM_UPDATE_GUEST_CR3_NOFLUSH) ) hvm_asid_flush_vcpu(v); + =20 + v->needs_tlb_flush =3D true; } else if ( nestedhvm_vmswitch_in_progress(v) ) ; /* CR3 switches during VMRUN/VMEXIT do not flush the TLB. */ @@ -944,6 +946,7 @@ static void noreturn cf_check svm_do_resume(void) hvm_migrate_pirqs(v); /* Migrating to another ASID domain. Request a new ASID. */ hvm_asid_flush_vcpu(v); + v->needs_tlb_flush =3D true; } =20 if ( !vcpu_guestmode && !vlapic_hw_disabled(vlapic) ) @@ -2306,6 +2309,8 @@ static void cf_check svm_invlpg(struct vcpu *v, unsig= ned long linear) { /* Safe fallback. Take a new ASID. */ hvm_asid_flush_vcpu(v); + /* Schedule a tlb flush on the VCPU. */ + v->needs_tlb_flush =3D true; } =20 static bool cf_check svm_get_pending_event( diff --git a/xen/arch/x86/hvm/vmx/vmcs.c b/xen/arch/x86/hvm/vmx/vmcs.c index 8e52ef4d49..4efe13e07f 100644 --- a/xen/arch/x86/hvm/vmx/vmcs.c +++ b/xen/arch/x86/hvm/vmx/vmcs.c @@ -1904,6 +1904,7 @@ void cf_check vmx_do_resume(void) v->arch.hvm.vmx.hostenv_migrated =3D 1; =20 hvm_asid_flush_vcpu(v); + v->needs_tlb_flush =3D true; } =20 debug_state =3D v->domain->debugger_attached diff --git a/xen/arch/x86/hvm/vmx/vmx.c b/xen/arch/x86/hvm/vmx/vmx.c index ec0a790336..0e4f9f9c3d 100644 --- a/xen/arch/x86/hvm/vmx/vmx.c +++ b/xen/arch/x86/hvm/vmx/vmx.c @@ -1511,6 +1511,7 @@ static void cf_check vmx_handle_cd(struct vcpu *v, un= signed long value) =20 wbinvd(); /* flush possibly polluted cache */ hvm_asid_flush_vcpu(v); /* invalidate memory type cached in TL= B */ + v->needs_tlb_flush =3D true; /* invalidate memory type cached = in TLB */ v->arch.hvm.vmx.cache_mode =3D CACHE_MODE_NO_FILL; } else @@ -1520,6 +1521,7 @@ static void cf_check vmx_handle_cd(struct vcpu *v, un= signed long value) if ( !is_iommu_enabled(v->domain) || iommu_snoop ) vmx_clear_msr_intercept(v, MSR_IA32_CR_PAT, VMX_MSR_RW); hvm_asid_flush_vcpu(v); /* no need to flush cache */ + v->needs_tlb_flush =3D true; } } } @@ -1872,6 +1874,7 @@ static void cf_check vmx_update_guest_cr( =20 if ( !(flags & HVM_UPDATE_GUEST_CR3_NOFLUSH) ) hvm_asid_flush_vcpu(v); + v->needs_tlb_flush =3D true; break; =20 default: diff --git a/xen/arch/x86/hvm/vmx/vvmx.c b/xen/arch/x86/hvm/vmx/vvmx.c index e4cdfe55c1..16d6f1d61b 100644 --- a/xen/arch/x86/hvm/vmx/vvmx.c +++ b/xen/arch/x86/hvm/vmx/vvmx.c @@ -1254,6 +1254,7 @@ static void virtual_vmentry(struct cpu_user_regs *reg= s) if ( nvmx->guest_vpid !=3D new_vpid ) { hvm_asid_flush_vcpu_asid(&vcpu_nestedhvm(v).nv_n2asid); + v->needs_tlb_flush =3D true; nvmx->guest_vpid =3D new_vpid; } } diff --git a/xen/arch/x86/mm/p2m.c b/xen/arch/x86/mm/p2m.c index fddecdf978..910623ac93 100644 --- a/xen/arch/x86/mm/p2m.c +++ b/xen/arch/x86/mm/p2m.c @@ -25,6 +25,7 @@ #include #include #include +#include #include #include #include @@ -1439,6 +1440,7 @@ p2m_flush(struct vcpu *v, struct p2m_domain *p2m) vcpu_nestedhvm(v).nv_p2m =3D NULL; p2m_flush_table(p2m); hvm_asid_flush_vcpu(v); + v->needs_tlb_flush =3D true; } =20 void @@ -1498,6 +1500,7 @@ static void assign_np2m(struct vcpu *v, struct p2m_do= main *p2m) static void nvcpu_flush(struct vcpu *v) { hvm_asid_flush_vcpu(v); + v->needs_tlb_flush =3D true; vcpu_nestedhvm(v).stale_np2m =3D true; } =20 @@ -1618,6 +1621,7 @@ void np2m_schedule(int dir) { /* This vCPU's np2m was flushed while it was not runnable = */ hvm_asid_flush_core(); + curr->needs_tlb_flush =3D true; vcpu_nestedhvm(curr).nv_p2m =3D NULL; } else diff --git a/xen/arch/x86/mm/paging.c b/xen/arch/x86/mm/paging.c index 2396f81ad5..b0b3bef753 100644 --- a/xen/arch/x86/mm/paging.c +++ b/xen/arch/x86/mm/paging.c @@ -939,6 +939,7 @@ void paging_update_nestedmode(struct vcpu *v) /* TODO: shadow-on-shadow */ v->arch.paging.nestedmode =3D NULL; hvm_asid_flush_vcpu(v); + v->needs_tlb_flush =3D true; } =20 int __init paging_set_allocation(struct domain *d, unsigned int pages, diff --git a/xen/arch/x86/mm/shadow/multi.c b/xen/arch/x86/mm/shadow/multi.c index 80cd3299fa..2df2842138 100644 --- a/xen/arch/x86/mm/shadow/multi.c +++ b/xen/arch/x86/mm/shadow/multi.c @@ -3165,6 +3165,7 @@ sh_update_linear_entries(struct vcpu *v) * without this change, it would fetch the wrong value due to a stale = TLB. */ sh_flush_local(d); + v->needs_tlb_flush =3D true; } =20 static pagetable_t cf_check sh_update_cr3(struct vcpu *v, bool noflush) --=20 2.52.0 -- Teddy Astie | Vates XCP-ng Developer XCP-ng & Xen Orchestra - Vates solutions web: https://vates.tech From nobody Tue May 5 10:13:32 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass header.i=teddy.astie@vates.tech; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=none dis=none) header.from=vates.tech ARC-Seal: i=1; a=rsa-sha256; t=1776261291; cv=none; d=zohomail.com; s=zohoarc; b=ctKPT4pDsDCysezRCEZ3GRhB/OLW9IMLQKrh4c2AarVN3keM3huTOkGfkhZH0AVC8w8Lehw8MJNpuIdnJDSp9PsFFn1D3gKpsEfNgX6xxPc+jMxofiGcxEOYhK/t5ZEn9d7aA54YRJr/1mPd/8rsp4uVXujn6Y9mTNOmbjjoG08= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1776261291; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=BuRU+2TlBcBTTb5Q+25wdwHHW2PQBda97rbEBk2HBMg=; b=X1rGjrB2WJyN+yXLGPkmup/d/ovS77vbx22SamRQ49s/0/kVxm2wSZvNbSSW4vAoGBIkfUAr8bcuTLxrlMg61XdG4P9wsEYDmaJFiEjAYM7EB9mwDp/NFvIl7fawij5pclWvWqTjbat3DEwICCrBWWnvg0eQPGsh3Mp5WemBwso= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass header.i=teddy.astie@vates.tech; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1776261291329495.9730469378942; Wed, 15 Apr 2026 06:54:51 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1282512.1565094 (Exim 4.92) (envelope-from ) id 1wD0hR-0006HS-TD; Wed, 15 Apr 2026 13:54:37 +0000 Received: by outflank-mailman (output) from mailman id 1282512.1565094; Wed, 15 Apr 2026 13:54:37 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1wD0hR-0006HJ-Oy; Wed, 15 Apr 2026 13:54:37 +0000 Received: by outflank-mailman (input) for mailman id 1282512; Wed, 15 Apr 2026 13:54:37 +0000 Received: from mx.expurgate.net ([195.190.135.10]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1wD0hR-0006Gm-6I for xen-devel@lists.xenproject.org; Wed, 15 Apr 2026 13:54:37 +0000 Received: from mx.expurgate.net (helo=localhost) by mx.expurgate.net with esmtp id 1wD0hQ-00AiEv-J7 for xen-devel@lists.xenproject.org; Wed, 15 Apr 2026 15:54:36 +0200 Received: from [10.42.69.10] (helo=localhost) by localhost with ESMTP (eXpurgate MTA 0.9.1) (envelope-from ) id 69df9880-e002-0a2a0a5209dd-0a2a450ab858-42 for ; Wed, 15 Apr 2026 15:54:36 +0200 Received: from [198.2.187.1] (helo=mail187-1.suw11.mandrillapp.com) by tlsNG-4011c0.mxtls.expurgate.net with ESMTPS (eXpurgate 4.56.0) (envelope-from ) id 69df989b-ee98-0a2a450a0019-c602bb012713-3 for ; Wed, 15 Apr 2026 15:54:36 +0200 Received: from pmta09.mandrill.prod.suw01.rsglab.com (localhost [127.0.0.1]) by mail187-1.suw11.mandrillapp.com (Mailchimp) with ESMTP id 4fwhs210V9zBsbVnk for ; Wed, 15 Apr 2026 13:32:22 +0000 (GMT) Received: from [37.26.189.201] by mandrillapp.com id 4a8938bac7ba4f1da0b2eca74ce48353; Wed, 15 Apr 2026 13:32:22 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" Authentication-Results: eu.smtp.expurgate.cloud; dkim=pass header.s=mte1 header.d=mandrillapp.com header.i="@mandrillapp.com" header.h="From:Subject:To:Cc:Message-Id:In-Reply-To:References:Feedback-ID:Date:MIME-Version:Content-Type:Content-Transfer-Encoding"; dkim=pass header.s=mte1 header.d=vates.tech header.i="teddy.astie@vates.tech" header.h="From:Subject:To:Cc:Message-Id:In-Reply-To:References:Feedback-ID:Date:MIME-Version:Content-Type:Content-Transfer-Encoding" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=mandrillapp.com; s=mte1; t=1776259942; x=1776529942; bh=BuRU+2TlBcBTTb5Q+25wdwHHW2PQBda97rbEBk2HBMg=; h=From:Subject:To:Cc:Message-Id:In-Reply-To:References:Feedback-ID: Date:MIME-Version:Content-Type:Content-Transfer-Encoding:CC:Date: Subject:From; b=apafzCn1D7dkgCAP+0AjQlCovSuawJ7U/aO/D63ik+eRQX5BuBCfKkFM0hys+l4MS yCt7cCc2xU91kZ48za/Eu+Rn3C7g8CtdCiJ+xDH64IcWxSSMNYIpfTh7cmYy4dx6sv nuoB4tDfEYzJakMU6ziW2flgBRMnIVA08FWOcLEGgQJeNAxs88j78lJHJrwv/LTOzu 09LdE9FaEi6u5x+a0MEfU9b9d19PrkUrG6y2msQalKXtW7VJq2iffuGWM2VIzHCiuT 5u+OUB1dTUZ06cd1sggMHedQF7NabDHv/rAhdSba94ppi8ZGMiGAHHNsdzS93+QctT GDh1mcioHVj0Q== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=vates.tech; s=mte1; t=1776259942; x=1776520442; i=teddy.astie@vates.tech; bh=BuRU+2TlBcBTTb5Q+25wdwHHW2PQBda97rbEBk2HBMg=; h=From:Subject:To:Cc:Message-Id:In-Reply-To:References:Feedback-ID: Date:MIME-Version:Content-Type:Content-Transfer-Encoding:CC:Date: Subject:From; b=hI2Bdf1HB2413BPp4DlZ3xzYuvIoZXqhgjQwJTZT95WWdYYyvv1uTmZrqBSxwG/kV gbi9lsO0BQLwyIJHb00h2ces0yaBJhWBuL5CZhc5X/s94EHKXF87eo2Pv2vlzwOJQ4 1sWyBN4sdrsibmP2RnQyaSX/1V0364bh+XG2+ZtAvmvJyOl4QusgTISD1Hsq7q29um 4HnSZu1I5qN6AJRSmdjR+0N5PvU0Y+gOzDtmw/FIai6aKYW8vOjrxiIhjYCvtBj+37 gxRIl/8BP10jHRKB/0soWoYwi9PCDO+cKdDvp8Ilwz+RIiml/H7UuvEJznFP4Yifgl irKu+6nyNL52A== From: "Teddy Astie" Subject: =?utf-8?Q?[RFC=20PATCH=205/7]=20x86/hvm:=20Flush=20TLB=20on=20vCPU=20overlaps=20on=20the=20same=20pCPU?= X-Mailer: git-send-email 2.52.0 X-Bm-Disclaimer: Yes X-Bm-Milter-Handled: 4ffbd6c1-ee69-4e1b-aabd-f977039bd3e2 X-Bm-Transport-Timestamp: 1776259941217 To: xen-devel@lists.xenproject.org Cc: "Teddy Astie" , "Jan Beulich" , "Andrew Cooper" , "=?utf-8?Q?Roger=20Pau=20Monn=C3=A9?=" , "Jason Andryuk" Message-Id: In-Reply-To: References: X-Native-Encoded: 1 X-Report-Abuse: =?UTF-8?Q?Please=20forward=20a=20copy=20of=20this=20message,=20including=20all=20headers,=20to=20abuse@mandrill.com.=20You=20can=20also=20report=20abuse=20here:=20https://mandrillapp.com/contact/abuse=3Fid=3D30504962.4a8938bac7ba4f1da0b2eca74ce48353?= X-Mandrill-User: md_30504962 Feedback-ID: 30504962:30504962.20260415:md Date: Wed, 15 Apr 2026 13:32:22 +0000 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-purgate-ID: tlsNG-4011c0/1776261276-0FF430B1-C81FA595/0/0 X-purgate-type: clean X-purgate-size: 2201 X-ZohoMail-DKIM: pass (identity @mandrillapp.com) (identity teddy.astie@vates.tech) X-ZM-MESSAGEID: 1776261294708154100 Content-Type: text/plain; charset="utf-8" When using the same ASID/VPID for all vCPU of a domain, we need to make sure that when context switching between 2 vCPUs of the same domain on the same pCPU don't miss a TLB flush, as they may be using different CR3. Flush the TLB if the latest vCPU that ran on this core differ to the one we're currently on. Signed-off-by: Teddy Astie --- Would it be preferable to move it to common code (or shared logic for x86). xen/arch/x86/hvm/svm/svm.c | 8 ++++++++ xen/arch/x86/hvm/vmx/vmx.c | 7 +++++++ 2 files changed, 15 insertions(+) diff --git a/xen/arch/x86/hvm/svm/svm.c b/xen/arch/x86/hvm/svm/svm.c index 64c08432fd..8714fb18ec 100644 --- a/xen/arch/x86/hvm/svm/svm.c +++ b/xen/arch/x86/hvm/svm/svm.c @@ -981,6 +981,14 @@ void asmlinkage svm_vmenter_helper(void) =20 svm_sync_vmcb(curr, vmcb_needs_vmsave); =20 + /** + * Check if we were the latest vCPU of this domain that ran on this pC= PU. + * Flush the TLB if it is not, as the TLB entries are the ones from th= e previous + * vCPU. vCPU migration from a CPU to another always imply a TLB flush. + */ + if ( curr->domain->latest_vcpu[cpu] !=3D curr->vcpu_id ) + curr->needs_tlb_flush =3D true; + curr->domain->latest_vcpu[cpu] =3D curr->vcpu_id; =20 vmcb->rax =3D regs->rax; diff --git a/xen/arch/x86/hvm/vmx/vmx.c b/xen/arch/x86/hvm/vmx/vmx.c index 0e4f9f9c3d..dceff2f221 100644 --- a/xen/arch/x86/hvm/vmx/vmx.c +++ b/xen/arch/x86/hvm/vmx/vmx.c @@ -4980,6 +4980,13 @@ bool asmlinkage vmx_vmenter_helper(const struct cpu_= user_regs *regs) =20 if ( unlikely(need_flush) ) vpid_sync_all(); + /** + * Check if we were the latest vCPU of this domain that ran on this pC= PU. + * Flush the TLB if it is not, as the TLB entries are the ones from th= e previous + * vCPU. vCPU migration from a CPU to another always imply a TLB flush. + */ + if ( currd->latest_vcpu[cpu] !=3D curr->vcpu_id ) + curr->needs_tlb_flush =3D true; =20 currd->latest_vcpu[cpu] =3D curr->vcpu_id; =20 --=20 2.52.0 -- Teddy Astie | Vates XCP-ng Developer XCP-ng & Xen Orchestra - Vates solutions web: https://vates.tech From nobody Tue May 5 10:13:32 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass header.i=teddy.astie@vates.tech; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=none dis=none) header.from=vates.tech ARC-Seal: i=1; a=rsa-sha256; t=1776259967; cv=none; d=zohomail.com; s=zohoarc; b=AimH2KgjgcdkEpkPMNQwnulQJTu8nsnXrtPT7S7lQ7R8bGo2Gc8wbX0LOUjhsxfNOhJJ2ne02AIYf7z/Q3x+kK3dNgvuueI6v/AsYIV15O5EGDlM0itcXbM7mL8t9RqRVYawhdwcJoMVANN4asW9TgmBbAzVM2H1ItqQtL7/b1c= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1776259967; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=V9h2LeDrWd+hcIIUXMFjjufE73oFJ5PXheM8EpWf9EU=; b=lssZByKsaJeL3cOWbIQccSfjaXSHFlfWNhxjE6gRMW1Ip8ieU/RRdx2eup9PtBzeXHxma1Qkg/600SU6nGSXeKJqdUhNjULilg7Q/BdRKFssMfBVbm1cktG7Ze6a/ZhxFvFs0dtjWMOe5k6yM7o+lUB7GyZ3f/KtXtgTxJ2AgvA= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass header.i=teddy.astie@vates.tech; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1776259966825831.3036939006561; Wed, 15 Apr 2026 06:32:46 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1282455.1565048 (Exim 4.92) (envelope-from ) id 1wD0M4-0000sY-7m; Wed, 15 Apr 2026 13:32:32 +0000 Received: by outflank-mailman (output) from mailman id 1282455.1565048; Wed, 15 Apr 2026 13:32:32 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1wD0M4-0000sN-4B; Wed, 15 Apr 2026 13:32:32 +0000 Received: by outflank-mailman (input) for mailman id 1282455; Wed, 15 Apr 2026 13:32:30 +0000 Received: from mx.expurgate.net ([195.190.135.10]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1wD0M2-0000q7-1H for xen-devel@lists.xenproject.org; Wed, 15 Apr 2026 13:32:30 +0000 Received: from mx.expurgate.net (helo=localhost) by mx.expurgate.net with esmtp id 1wD0M1-006OmM-DH for xen-devel@lists.xenproject.org; Wed, 15 Apr 2026 15:32:29 +0200 Received: from [10.42.69.8] (helo=localhost) by localhost with ESMTP (eXpurgate MTA 0.9.1) (envelope-from ) id 69df936d-e002-0a2a0a5209dd-0a2a4508b786-2 for ; Wed, 15 Apr 2026 15:32:29 +0200 Received: from [198.2.186.1] (helo=mail186-1.suw21.mandrillapp.com) by tlsNG-c1860d.mxtls.expurgate.net with ESMTPS (eXpurgate 4.56.0) (envelope-from ) id 69df936c-fab6-0a2a45080019-c602ba01ada0-3 for ; Wed, 15 Apr 2026 15:32:29 +0200 Received: from pmta10.mandrill.prod.suw01.rsglab.com (localhost [127.0.0.1]) by mail186-1.suw21.mandrillapp.com (Mailchimp) with ESMTP id 4fwhs766YJzBsX13v for ; Wed, 15 Apr 2026 13:32:27 +0000 (GMT) Received: from [37.26.189.201] by mandrillapp.com id 23fe8188e6a9447487e3112d66dc3e1a; Wed, 15 Apr 2026 13:32:27 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" Authentication-Results: eu.smtp.expurgate.cloud; dkim=pass header.s=mte1 header.d=mandrillapp.com header.i="@mandrillapp.com" header.h="From:Subject:To:Cc:Message-Id:In-Reply-To:References:Feedback-ID:Date:MIME-Version:Content-Type:Content-Transfer-Encoding"; dkim=pass header.s=mte1 header.d=vates.tech header.i="teddy.astie@vates.tech" header.h="From:Subject:To:Cc:Message-Id:In-Reply-To:References:Feedback-ID:Date:MIME-Version:Content-Type:Content-Transfer-Encoding" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=mandrillapp.com; s=mte1; t=1776259947; x=1776529947; bh=V9h2LeDrWd+hcIIUXMFjjufE73oFJ5PXheM8EpWf9EU=; h=From:Subject:To:Cc:Message-Id:In-Reply-To:References:Feedback-ID: Date:MIME-Version:Content-Type:Content-Transfer-Encoding:CC:Date: Subject:From; b=BLUWRz5ltob2x4MQ72Sidnl1M4Rj7Xo26zp6CfZ/eOx9U9szMwmGG6Cr+e7In9Z/U 2aegQxcEOm2bnlD+4B2q4huWHpX+5kFzpueYW+hlrVagZyHEVk/sqeD2wjFJzOlVqL ANf7VvijRwtX5LJswWwPRtb0AjnHjEf9VhhgjGqGbzaSwbH5+OgrQYRdlS+4rVfyFX jYcfrA09RfC8pf1/IKZQDFVl/VnutiiFNLP+qxVqAybH55yHP5rez2YdRTzH1QQ9M9 x6k3/pw9p4k29YV+3nZuuC84F3NC13/9ilODIGJZYTzsGcHu+lFrXF5Zb2kZfidqYI WJm8azYKbdilQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=vates.tech; s=mte1; t=1776259947; x=1776520447; i=teddy.astie@vates.tech; bh=V9h2LeDrWd+hcIIUXMFjjufE73oFJ5PXheM8EpWf9EU=; h=From:Subject:To:Cc:Message-Id:In-Reply-To:References:Feedback-ID: Date:MIME-Version:Content-Type:Content-Transfer-Encoding:CC:Date: Subject:From; b=WwAN6xK8C6OYy2JJXwfERhI1QDvdeUeYmm4R4jTvlKcEPXC9vjywJaOz10d+arYAw KnCa/eA9SXQopN62WYN9ZlN8aI4nbFGUfoejCpMfznKIrfFvhZTWQUfz0m3lksaqSY rBlNjmpqpPDgUw72eEuOlbCOfEvD/TRt/qyxSftwZPfP98LFmphJMeLWf03jWGfRlL E21GraGfuDGd7tjweHj53QOq25/C+M0B8v1rf76nhAbcE9nisIUHWWcPq8nQ27gFTu m3FYYXZJFBWGGSIZKrTKCHJaue1vDELCnaXx9OjXnJHDdaQpuB+6i/GGVfL345yAQ9 HoMBybpFl9MPw== From: "Teddy Astie" Subject: =?utf-8?Q?[RFC=20PATCH=206/7]=20x86/hvm:=20Transition=20to=20needs=5Ftlb=5Fflush=20logic,=20use=20per-domain=20ASID?= X-Mailer: git-send-email 2.52.0 X-Bm-Disclaimer: Yes X-Bm-Milter-Handled: 4ffbd6c1-ee69-4e1b-aabd-f977039bd3e2 X-Bm-Transport-Timestamp: 1776259945929 To: xen-devel@lists.xenproject.org Cc: "Teddy Astie" , "Andrew Cooper" , "Anthony PERARD" , "Michal Orzel" , "Jan Beulich" , "Julien Grall" , "=?utf-8?Q?Roger=20Pau=20Monn=C3=A9?=" , "Stefano Stabellini" , "Jason Andryuk" , "Tim Deegan" Message-Id: <2775da1be0c767a2dc7fcdf5caaf43278da2ab74.1776259594.git.teddy.astie@vates.tech> In-Reply-To: References: X-Native-Encoded: 1 X-Report-Abuse: =?UTF-8?Q?Please=20forward=20a=20copy=20of=20this=20message,=20including=20all=20headers,=20to=20abuse@mandrill.com.=20You=20can=20also=20report=20abuse=20here:=20https://mandrillapp.com/contact/abuse=3Fid=3D30504962.23fe8188e6a9447487e3112d66dc3e1a?= X-Mandrill-User: md_30504962 Feedback-ID: 30504962:30504962.20260415:md Date: Wed, 15 Apr 2026 13:32:27 +0000 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-purgate-ID: tlsNG-c1860d/1776259949-77348497-BD68DE05/0/0 X-purgate-type: clean X-purgate-size: 42911 X-ZohoMail-DKIM: pass (identity @mandrillapp.com) (identity teddy.astie@vates.tech) X-ZM-MESSAGEID: 1776259968556158500 Content-Type: text/plain; charset="utf-8" Change the ASID model where all vCPU of a domain share the same ASID as required by AMD SEV and broadcast TLB flushing features (AMD INVLPGB). ASID 1 is reserved as a placeholder for "no domain ASID", and used when either ASID are not supported or no more ASID is available for use. In this case, we always flush the TLB when from and to such domain's vCPU. Moreover, centralize the TLB flushing logic to use needs_tlb_flush, if a full TLB flush needs to be performed for the vCPU, either through SVM tlb_control or VMX invvpid before entering the guest. As a result, drop ASID tickling logic, which is now redundant with the needs_tlb_flush mechanism introduced previously. Also take the opportunity to drop some now unused helpers now that FLUSH_HVM_ASID_CORE is dropped. Signed-off-by: Teddy Astie --- Nested virt makes some aspects tricky and is hard to reason about, especially since there are 2 ASID in such case and complex TLB flushing logic. docs/misc/xen-command-line.pandoc | 2 +- xen/arch/x86/flushtlb.c | 18 +-- xen/arch/x86/hvm/asid.c | 169 +++++++++++-------------- xen/arch/x86/hvm/emulate.c | 1 - xen/arch/x86/hvm/hvm.c | 24 +++- xen/arch/x86/hvm/nestedhvm.c | 7 +- xen/arch/x86/hvm/svm/asid.c | 67 +++++++--- xen/arch/x86/hvm/svm/nestedsvm.c | 2 +- xen/arch/x86/hvm/svm/svm.c | 31 +++-- xen/arch/x86/hvm/svm/svm.h | 4 - xen/arch/x86/hvm/vmx/vmcs.c | 5 +- xen/arch/x86/hvm/vmx/vmx.c | 64 +++++----- xen/arch/x86/hvm/vmx/vvmx.c | 3 +- xen/arch/x86/include/asm/flushtlb.h | 7 - xen/arch/x86/include/asm/hvm/asid.h | 26 ++-- xen/arch/x86/include/asm/hvm/domain.h | 1 + xen/arch/x86/include/asm/hvm/hvm.h | 15 +-- xen/arch/x86/include/asm/hvm/svm.h | 5 + xen/arch/x86/include/asm/hvm/vcpu.h | 10 +- xen/arch/x86/include/asm/hvm/vmx/vmx.h | 4 +- xen/arch/x86/mm/hap/hap.c | 9 +- xen/arch/x86/mm/p2m.c | 3 - xen/arch/x86/mm/paging.c | 1 - xen/arch/x86/mm/shadow/multi.c | 11 +- 24 files changed, 228 insertions(+), 261 deletions(-) diff --git a/docs/misc/xen-command-line.pandoc b/docs/misc/xen-command-line= .pandoc index 6c77129732..099b718df9 100644 --- a/docs/misc/xen-command-line.pandoc +++ b/docs/misc/xen-command-line.pandoc @@ -208,7 +208,7 @@ to appropriate auditing by Xen. Argo is disabled by de= fault. > Default: `true` =20 Permit Xen to use Address Space Identifiers. This is an optimisation which -tags the TLB entries with an ID per vcpu. This allows for guest TLB flush= es +tags the TLB entries with an ID per domain. This allows for guest TLB flu= shes to be performed without the overhead of a complete TLB flush. =20 ### async-show-all (x86) diff --git a/xen/arch/x86/flushtlb.c b/xen/arch/x86/flushtlb.c index 8ee2385bba..f60d2df8fd 100644 --- a/xen/arch/x86/flushtlb.c +++ b/xen/arch/x86/flushtlb.c @@ -13,6 +13,7 @@ #include #include #include +#include #include #include #include @@ -117,7 +118,6 @@ void switch_cr3_cr4(unsigned long cr3, unsigned long cr= 4) =20 if ( tlb_clk_enabled ) t =3D pre_flush(); - hvm_flush_guest_tlbs(); =20 old_cr4 =3D read_cr4(); ASSERT(!(old_cr4 & X86_CR4_PCIDE) || !(old_cr4 & X86_CR4_PGE)); @@ -221,9 +221,6 @@ unsigned int flush_area_local(const void *va, unsigned = int flags) do_tlb_flush(); } =20 - if ( flags & FLUSH_HVM_ASID_CORE ) - hvm_flush_guest_tlbs(); - if ( flags & (FLUSH_CACHE_EVICT | FLUSH_CACHE_WRITEBACK) ) { const struct cpuinfo_x86 *c =3D ¤t_cpu_data; @@ -313,21 +310,12 @@ void cache_writeback(const void *addr, unsigned int s= ize) asm volatile ("sfence" ::: "memory"); } =20 -unsigned int guest_flush_tlb_flags(const struct domain *d) -{ - bool shadow =3D paging_mode_shadow(d); - bool asid =3D is_hvm_domain(d) && (cpu_has_svm || shadow); - - return (shadow ? FLUSH_TLB : 0) | (asid ? FLUSH_HVM_ASID_CORE : 0); -} - void guest_flush_tlb_mask(const struct domain *d, const cpumask_t *mask) { - unsigned int flags =3D guest_flush_tlb_flags(d); struct vcpu *v; =20 - if ( flags ) - flush_mask(mask, flags); + if ( paging_mode_shadow(d) ) + flush_tlb_mask(mask); =20 for_each_vcpu(d, v) v->needs_tlb_flush =3D true; diff --git a/xen/arch/x86/hvm/asid.c b/xen/arch/x86/hvm/asid.c index 935cae3901..1a21125161 100644 --- a/xen/arch/x86/hvm/asid.c +++ b/xen/arch/x86/hvm/asid.c @@ -5,138 +5,115 @@ * Copyright (c) 2009, Citrix Systems, Inc. */ =20 +#include #include #include #include -#include -#include -#include +#include +#include + +#include #include =20 /* Xen command-line option to enable ASIDs */ static bool __read_mostly opt_asid_enabled =3D true; boolean_param("asid", opt_asid_enabled); =20 +bool __read_mostly asid_enabled =3D false; +static unsigned long __ro_after_init *asid_bitmap; +static unsigned long __ro_after_init asid_count; +static DEFINE_SPINLOCK(asid_lock); + /* - * ASIDs partition the physical TLB. In the current implementation ASIDs = are - * introduced to reduce the number of TLB flushes. Each time the guest's - * virtual address space changes (e.g. due to an INVLPG, MOV-TO-{CR3, CR4} - * operation), instead of flushing the TLB, a new ASID is assigned. This - * reduces the number of TLB flushes to at most 1/#ASIDs. The biggest - * advantage is that hot parts of the hypervisor's code and data retain in - * the TLB. - * * Sketch of the Implementation: + * ASIDs are assigned uniquely per domain and doesn't change during the li= fecycle of the + * domain. Once vcpus are initialized and are up, we assign the same ASID = to all vcpus + * of that domain at the first VMRUN. In order to process a TLB flush on a= vcpu, we set + * needs_tlb_flush to schedule a TLB flush for the next VMRUN (e.g using t= lb control=20 + * field of VMCB). * - * ASIDs are a CPU-local resource. As preemption of ASIDs is not possible, - * ASIDs are assigned in a round-robin scheme. To minimize the overhead of - * ASID invalidation, at the time of a TLB flush, ASIDs are tagged with a - * 64-bit generation. Only on a generation overflow the code needs to - * invalidate all ASID information stored at the VCPUs with are run on the - * specific physical processor. This overflow appears after about 2^80 - * host processor cycles, so we do not optimize this case, but simply disa= ble - * ASID useage to retain correctness. + * We reserve ASID=3D1 as being the ASID used when none other is available= (or with asid + * use disabled). Multiples domains may use this ASID, thus we need to sys= tematically + * flush the TLB for this one when switching between vCPUs with ASID=3D1. */ =20 -/* Per-CPU ASID management. */ -struct hvm_asid_data { - uint64_t core_asid_generation; - uint32_t next_asid; - uint32_t max_asid; - bool disabled; -}; - -static DEFINE_PER_CPU(struct hvm_asid_data, hvm_asid_data); - -void hvm_asid_init(unsigned int nasids) +int __init hvm_asid_init(unsigned long nasids) { - static int8_t __ro_after_init g_disabled =3D -1; - struct hvm_asid_data *data =3D &this_cpu(hvm_asid_data); + ASSERT(nasids); =20 - data->max_asid =3D nasids - 1; - data->disabled =3D !opt_asid_enabled || (nasids <=3D 1); + asid_count =3D nasids; + asid_enabled =3D opt_asid_enabled && (nasids > 1); =20 - if ( g_disabled < 0 ) - { - g_disabled =3D data->disabled; - printk("HVM: ASIDs %sabled\n", data->disabled ? "dis" : "en"); - } - else if ( g_disabled !=3D data->disabled ) - printk("HVM: CPU%u: ASIDs %sabled\n", smp_processor_id(), - data->disabled ? "dis" : "en"); + asid_bitmap =3D xvzalloc_array(unsigned long, BITS_TO_LONGS(asid_count= + 1)); + if ( !asid_bitmap ) + return -ENOMEM; =20 - /* Zero indicates 'invalid generation', so we start the count at one. = */ - data->core_asid_generation =3D 1; + printk("HVM: ASIDs %sabled (count=3D%lu)\n", asid_enabled ? "en" : "di= s", asid_count); =20 - /* Zero indicates 'ASIDs disabled', so we start the count at one. */ - data->next_asid =3D 1; -} + /* ASID 0 and 1 are reserved, mark it as permanently used */ + set_bit(0, asid_bitmap); + set_bit(1, asid_bitmap); =20 -void hvm_asid_flush_vcpu_asid(struct hvm_vcpu_asid *asid) -{ - write_atomic(&asid->generation, 0); + return 0; } =20 -void hvm_asid_flush_vcpu(struct vcpu *v) +int hvm_asid_alloc(struct hvm_asid *asid) { - hvm_asid_flush_vcpu_asid(&v->arch.hvm.n1asid); - hvm_asid_flush_vcpu_asid(&vcpu_nestedhvm(v).nv_n2asid); -} + unsigned long new_asid; =20 -void hvm_asid_flush_core(void) -{ - struct hvm_asid_data *data =3D &this_cpu(hvm_asid_data); + if ( !asid_enabled ) + { + asid->asid =3D 1; + return 0; + } =20 - if ( data->disabled ) - return; + spin_lock(&asid_lock); + new_asid =3D find_first_zero_bit(asid_bitmap, asid_count); + if ( new_asid > asid_count ) + return -ENOSPC; =20 - if ( likely(++data->core_asid_generation !=3D 0) ) - return; + set_bit(new_asid, asid_bitmap); =20 - /* - * ASID generations are 64 bit. Overflow of generations never happens. - * For safety, we simply disable ASIDs, so correctness is established;= it - * only runs a bit slower. - */ - printk("HVM: ASID generation overrun. Disabling ASIDs.\n"); - data->disabled =3D 1; + asid->asid =3D new_asid; + spin_unlock(&asid_lock); + return 0; } =20 -bool hvm_asid_handle_vmenter(struct hvm_vcpu_asid *asid) +int hvm_asid_alloc_range(struct hvm_asid *asid, unsigned long min, unsigne= d long max) { - struct hvm_asid_data *data =3D &this_cpu(hvm_asid_data); + unsigned long new_asid; + =20 + if ( WARN_ON(min >=3D asid_count) ) + return -EINVAL; =20 - /* On erratum #170 systems we must flush the TLB.=20 - * Generation overruns are taken here, too. */ - if ( data->disabled ) - goto disabled; + if ( !asid_enabled ) + return -EOPNOTSUPP; =20 - /* Test if VCPU has valid ASID. */ - if ( read_atomic(&asid->generation) =3D=3D data->core_asid_generation ) - return 0; + spin_lock(&asid_lock); + new_asid =3D find_next_zero_bit(asid_bitmap, asid_count, min); + if ( new_asid > max || new_asid > asid_count ) + return -ENOSPC; =20 - /* If there are no free ASIDs, need to go to a new generation */ - if ( unlikely(data->next_asid > data->max_asid) ) - { - hvm_asid_flush_core(); - data->next_asid =3D 1; - if ( data->disabled ) - goto disabled; - } + set_bit(new_asid, asid_bitmap); =20 - /* Now guaranteed to be a free ASID. */ - asid->asid =3D data->next_asid++; - write_atomic(&asid->generation, data->core_asid_generation); + asid->asid =3D new_asid; + spin_unlock(&asid_lock); + return 0; +} =20 - /* - * When we assign ASID 1, flush all TLB entries as we are starting a n= ew - * generation, and all old ASID allocations are now stale.=20 - */ - return (asid->asid =3D=3D 1); +void hvm_asid_free(struct hvm_asid *asid) +{ + ASSERT( asid->asid ); =20 - disabled: - asid->asid =3D 0; - return 0; + if ( !asid_enabled || asid->asid =3D=3D 1 ) + return; + + ASSERT( asid->asid < asid_count ); + + spin_lock(&asid_lock); + WARN_ON(!test_bit(asid->asid, asid_bitmap)); + clear_bit(asid->asid, asid_bitmap); + spin_unlock(&asid_lock); } =20 /* diff --git a/xen/arch/x86/hvm/emulate.c b/xen/arch/x86/hvm/emulate.c index 3bc1d321cc..efdf236df1 100644 --- a/xen/arch/x86/hvm/emulate.c +++ b/xen/arch/x86/hvm/emulate.c @@ -2656,7 +2656,6 @@ static int cf_check hvmemul_tlb_op( case x86emul_invpcid: if ( x86emul_invpcid_type(aux) !=3D X86_INVPCID_INDIV_ADDR ) { - hvm_asid_flush_vcpu(current); current->needs_tlb_flush =3D true; break; } diff --git a/xen/arch/x86/hvm/hvm.c b/xen/arch/x86/hvm/hvm.c index 0f0b0e242f..2ab625d6e0 100644 --- a/xen/arch/x86/hvm/hvm.c +++ b/xen/arch/x86/hvm/hvm.c @@ -520,11 +520,13 @@ static bool hvm_get_pending_event(struct vcpu *v, str= uct x86_event *info) =20 void hvm_do_resume(struct vcpu *v) { + struct domain *currd =3D v->domain;; + check_wakeup_from_wait(); =20 pt_restore_timer(v); =20 - if ( has_vpci(v->domain) && vpci_process_pending(v) ) + if ( has_vpci(currd) && vpci_process_pending(v) ) { raise_softirq(SCHEDULE_SOFTIRQ); return; @@ -559,6 +561,13 @@ void hvm_do_resume(struct vcpu *v) v->arch.monitor.next_interrupt_enabled =3D false; } } + + if ( unlikely(currd->arch.hvm.asid.asid =3D=3D 1) ) + /* + * As ASID=3D1 may be shared across multiples domains, we can't ea= sily track the + * state of the TLB, we always flush the TLB before resuming this = vCPU. + */ + v->needs_tlb_flush =3D true; } =20 static int cf_check hvm_print_line( @@ -714,6 +723,10 @@ int hvm_domain_initialise(struct domain *d, if ( rc ) goto fail2; =20 + rc =3D hvm_asid_alloc(&d->arch.hvm.asid); + if ( rc ) + goto fail2; + rc =3D alternative_call(hvm_funcs.domain_initialise, d); if ( rc !=3D 0 ) goto fail2; @@ -794,7 +807,7 @@ void hvm_domain_destroy(struct domain *d) list_del(&ioport->list); xfree(ioport); } - + hvm_asid_free(&d->arch.hvm.asid); destroy_vpci_mmcfg(d); } =20 @@ -1612,7 +1625,6 @@ int hvm_vcpu_initialise(struct vcpu *v) int rc; struct domain *d =3D v->domain; =20 - hvm_asid_flush_vcpu(v); v->needs_tlb_flush =3D true; =20 spin_lock_init(&v->arch.hvm.tm_lock); @@ -4085,6 +4097,11 @@ static void hvm_s3_resume(struct domain *d) } } =20 +int hvm_flush_tlb(const unsigned long *vcpu_bitmap) +{ + return current->domain->arch.paging.flush_tlb(vcpu_bitmap); +} + static int hvmop_flush_tlb_all(void) { if ( !is_hvm_domain(current->domain) ) @@ -5464,4 +5481,3 @@ int hvm_copy_context_and_params(struct domain *dst, s= truct domain *src) * indent-tabs-mode: nil * End: */ - diff --git a/xen/arch/x86/hvm/nestedhvm.c b/xen/arch/x86/hvm/nestedhvm.c index bddd77d810..61e866b771 100644 --- a/xen/arch/x86/hvm/nestedhvm.c +++ b/xen/arch/x86/hvm/nestedhvm.c @@ -12,6 +12,7 @@ #include #include /* for local_event_delivery_(en|dis)able */ #include /* for paging_mode_hap() */ +#include =20 static unsigned long *shadow_io_bitmap[3]; =20 @@ -36,13 +37,11 @@ nestedhvm_vcpu_reset(struct vcpu *v) hvm_unmap_guest_frame(nv->nv_vvmcx, 1); nv->nv_vvmcx =3D NULL; nv->nv_vvmcxaddr =3D INVALID_PADDR; - nv->nv_flushp2m =3D 0; + nv->nv_flushp2m =3D true; nv->nv_p2m =3D NULL; nv->stale_np2m =3D false; nv->np2m_generation =3D 0; =20 - hvm_asid_flush_vcpu_asid(&nv->nv_n2asid); - alternative_vcall(hvm_funcs.nhvm_vcpu_reset, v); =20 /* vcpu is in host mode */ @@ -86,7 +85,7 @@ static void cf_check nestedhvm_flushtlb_ipi(void *info) * This is cheaper than flush_tlb_local() and has * the same desired effect. */ - hvm_asid_flush_core(); + WARN_ON(hvm_flush_tlb(NULL)); vcpu_nestedhvm(v).nv_p2m =3D NULL; vcpu_nestedhvm(v).stale_np2m =3D true; } diff --git a/xen/arch/x86/hvm/svm/asid.c b/xen/arch/x86/hvm/svm/asid.c index 53aa5d0512..44d2138895 100644 --- a/xen/arch/x86/hvm/svm/asid.c +++ b/xen/arch/x86/hvm/svm/asid.c @@ -1,39 +1,46 @@ /* SPDX-License-Identifier: GPL-2.0-only */ /* - * asid.c: handling ASIDs in SVM. + * asid.c: handling ASIDs/VPIDs. * Copyright (c) 2007, Advanced Micro Devices, Inc. */ =20 +#include + #include #include #include +#include =20 #include "svm.h" #include "vmcb.h" =20 -void svm_asid_init(const struct cpuinfo_x86 *c) +void __init svm_asid_init(void) { - unsigned int nasids =3D 0; + unsigned int cpu, nasids =3D cpuid_ebx(0x8000000aU); + + if ( !nasids ) + nasids =3D 1; =20 - /* Check for erratum #170, and leave ASIDs disabled if it's present. */ - if ( !cpu_has_amd_erratum(c, AMD_ERRATUM_170) ) - nasids =3D cpuid_ebx(0x8000000aU); + for_each_present_cpu(cpu) + { + /* Check for erratum #170, and leave ASIDs disabled if it's presen= t. */ + if ( cpu_has_amd_erratum(&cpu_data[cpu], AMD_ERRATUM_170) ) + { + printk(XENLOG_WARNING "Disabling ASID due to errata 170 on CPU= %u\n", cpu); + nasids =3D 1; + } + } =20 - hvm_asid_init(nasids); + BUG_ON(hvm_asid_init(nasids)); } =20 /* - * Called directly before VMRUN. Checks if the VCPU needs a new ASID, - * assigns it, and if required, issues required TLB flushes. + * Called directly at the first VMRUN/VMENTER of a vcpu to assign the ASID= /VPID. */ -void svm_asid_handle_vmrun(void) +void svm_vcpu_assign_asid(struct vcpu *v) { - struct vcpu *curr =3D current; - struct vmcb_struct *vmcb =3D curr->arch.hvm.svm.vmcb; - struct hvm_vcpu_asid *p_asid =3D - nestedhvm_vcpu_in_guestmode(curr) - ? &vcpu_nestedhvm(curr).nv_n2asid : &curr->arch.hvm.n1asid; - bool need_flush =3D hvm_asid_handle_vmenter(p_asid); + struct vmcb_struct *vmcb =3D v->arch.hvm.svm.vmcb; + struct hvm_asid *p_asid =3D &v->domain->arch.hvm.asid; =20 /* ASID 0 indicates that ASIDs are disabled. */ if ( p_asid->asid =3D=3D 0 ) @@ -44,11 +51,31 @@ void svm_asid_handle_vmrun(void) return; } =20 - if ( vmcb_get_asid(vmcb) !=3D p_asid->asid ) - vmcb_set_asid(vmcb, p_asid->asid); + /* In case ASIDs are disabled, as ASID =3D 0 is reserved, guest can us= e 1 instead. */ + vmcb_set_asid(vmcb, asid_enabled ? p_asid->asid : 1); +} + +/* Call to make a TLB flush at the next VMRUN. */ +void svm_vcpu_set_tlb_control(struct vcpu *v) +{ + struct vmcb_struct *vmcb =3D v->arch.hvm.svm.vmcb; + + /* + * If the vcpu is already running, the tlb control flag may not be + * processed and will be cleared at the next VMEXIT, which will undo + * what we are trying to do. + */ + WARN_ON(v !=3D current && v->is_running); + + vmcb->tlb_control =3D + cpu_has_svm_flushbyasid ? TLB_CTRL_FLUSH_ASID : TLB_CTRL_FLUSH_ALL; +} + +void svm_vcpu_clear_tlb_control(struct vcpu *v) +{ + struct vmcb_struct *vmcb =3D v->arch.hvm.svm.vmcb; =20 - /* We can't rely on TLB_CTRL_FLUSH_ASID as all ASIDs are stale here. */ - vmcb->tlb_control =3D need_flush ? TLB_CTRL_FLUSH_ALL : TLB_CTRL_NO_FL= USH; + vmcb->tlb_control =3D TLB_CTRL_NO_FLUSH; } =20 /* diff --git a/xen/arch/x86/hvm/svm/nestedsvm.c b/xen/arch/x86/hvm/svm/nested= svm.c index ef6fa5d23b..d9a84f9388 100644 --- a/xen/arch/x86/hvm/svm/nestedsvm.c +++ b/xen/arch/x86/hvm/svm/nestedsvm.c @@ -5,6 +5,7 @@ * */ =20 +#include #include #include #include @@ -638,7 +639,6 @@ nsvm_vcpu_vmentry(struct vcpu *v, struct cpu_user_regs = *regs, if ( svm->ns_asid !=3D vmcb_get_asid(ns_vmcb)) { nv->nv_flushp2m =3D 1; - hvm_asid_flush_vcpu_asid(&vcpu_nestedhvm(v).nv_n2asid); svm->ns_asid =3D vmcb_get_asid(ns_vmcb); } =20 diff --git a/xen/arch/x86/hvm/svm/svm.c b/xen/arch/x86/hvm/svm/svm.c index 8714fb18ec..ba22acb8a6 100644 --- a/xen/arch/x86/hvm/svm/svm.c +++ b/xen/arch/x86/hvm/svm/svm.c @@ -27,6 +27,7 @@ #include #include #include +#include #include #include #include @@ -137,16 +138,18 @@ static void cf_check svm_update_guest_cr( if ( !nestedhvm_enabled(v->domain) ) { if ( !(flags & HVM_UPDATE_GUEST_CR3_NOFLUSH) ) - hvm_asid_flush_vcpu(v); =20 v->needs_tlb_flush =3D true; } else if ( nestedhvm_vmswitch_in_progress(v) ) ; /* CR3 switches during VMRUN/VMEXIT do not flush the TLB. */ else if ( !(flags & HVM_UPDATE_GUEST_CR3_NOFLUSH) ) - hvm_asid_flush_vcpu_asid( - nestedhvm_vcpu_in_guestmode(v) - ? &vcpu_nestedhvm(v).nv_n2asid : &v->arch.hvm.n1asid); + { + if (nestedhvm_vcpu_in_guestmode(v)) + vcpu_nestedhvm(v).nv_flushp2m =3D true; + else + v->needs_tlb_flush =3D true; + } break; case 4: value =3D HVM_CR4_HOST_MASK; @@ -944,8 +947,6 @@ static void noreturn cf_check svm_do_resume(void) v->arch.hvm.svm.launch_core =3D smp_processor_id(); hvm_migrate_timers(v); hvm_migrate_pirqs(v); - /* Migrating to another ASID domain. Request a new ASID. */ - hvm_asid_flush_vcpu(v); v->needs_tlb_flush =3D true; } =20 @@ -974,8 +975,6 @@ void asmlinkage svm_vmenter_helper(void) =20 ASSERT(hvmemul_cache_disabled(curr)); =20 - svm_asid_handle_vmrun(); - TRACE_TIME(TRC_HVM_VMENTRY | (nestedhvm_vcpu_in_guestmode(curr) ? TRC_HVM_NESTEDFLAG : 0= )); =20 @@ -991,6 +990,9 @@ void asmlinkage svm_vmenter_helper(void) =20 curr->domain->latest_vcpu[cpu] =3D curr->vcpu_id; =20 + if ( test_and_clear_bool(curr->needs_tlb_flush) ) + svm_vcpu_set_tlb_control(curr); + vmcb->rax =3D regs->rax; vmcb->rip =3D regs->rip; vmcb->rsp =3D regs->rsp; @@ -1111,6 +1113,8 @@ static int cf_check svm_vcpu_initialise(struct vcpu *= v) return rc; } =20 + svm_vcpu_assign_asid(v); + return 0; } =20 @@ -1536,9 +1540,6 @@ static int _svm_cpu_up(bool bsp) /* check for erratum 383 */ svm_init_erratum_383(c); =20 - /* Initialize core's ASID handling. */ - svm_asid_init(c); - /* Initialize OSVW bits to be used by guests */ svm_host_osvw_init(); =20 @@ -2293,7 +2294,7 @@ static void svm_invlpga_intercept( { svm_invlpga(linear, (asid =3D=3D 0) - ? v->arch.hvm.n1asid.asid + ? v->domain->arch.hvm.asid.asid : vcpu_nestedhvm(v).nv_n2asid.asid); } =20 @@ -2315,8 +2316,6 @@ static bool cf_check is_invlpg( =20 static void cf_check svm_invlpg(struct vcpu *v, unsigned long linear) { - /* Safe fallback. Take a new ASID. */ - hvm_asid_flush_vcpu(v); /* Schedule a tlb flush on the VCPU. */ v->needs_tlb_flush =3D true; } @@ -2486,6 +2485,8 @@ const struct hvm_function_table * __init start_svm(vo= id) svm_function_table.caps.hap_superpage_2mb =3D true; svm_function_table.caps.hap_superpage_1gb =3D cpu_has_page1gb; =20 + svm_asid_init(); + return &svm_function_table; } =20 @@ -2542,6 +2543,8 @@ void asmlinkage svm_vmexit_handler(void) (vlapic_get_reg(vlapic, APIC_TASKPRI) & 0x0F)); } =20 + svm_vcpu_clear_tlb_control(v); + exit_reason =3D vmcb->exitcode; =20 if ( hvm_long_mode_active(v) ) diff --git a/xen/arch/x86/hvm/svm/svm.h b/xen/arch/x86/hvm/svm/svm.h index cfa411ad5a..901354e914 100644 --- a/xen/arch/x86/hvm/svm/svm.h +++ b/xen/arch/x86/hvm/svm/svm.h @@ -12,12 +12,8 @@ #include =20 struct cpu_user_regs; -struct cpuinfo_x86; struct vcpu; =20 -void svm_asid_init(const struct cpuinfo_x86 *c); -void svm_asid_handle_vmrun(void); - unsigned long *svm_msrbit(unsigned long *msr_bitmap, uint32_t msr); void __update_guest_eip(struct cpu_user_regs *regs, unsigned int inst_len); =20 diff --git a/xen/arch/x86/hvm/vmx/vmcs.c b/xen/arch/x86/hvm/vmx/vmcs.c index 4efe13e07f..88808e26e7 100644 --- a/xen/arch/x86/hvm/vmx/vmcs.c +++ b/xen/arch/x86/hvm/vmx/vmcs.c @@ -20,6 +20,7 @@ #include #include #include +#include #include #include #include @@ -778,8 +779,6 @@ static int _vmx_cpu_up(bool bsp) =20 this_cpu(vmxon) =3D 1; =20 - hvm_asid_init(cpu_has_vmx_vpid ? (1u << VMCS_VPID_WIDTH) : 0); - if ( cpu_has_vmx_ept ) ept_sync_all(); =20 @@ -1903,7 +1902,6 @@ void cf_check vmx_do_resume(void) */ v->arch.hvm.vmx.hostenv_migrated =3D 1; =20 - hvm_asid_flush_vcpu(v); v->needs_tlb_flush =3D true; } =20 @@ -2117,7 +2115,6 @@ void vmcs_dump_vcpu(struct vcpu *v) (SECONDARY_EXEC_ENABLE_VPID | SECONDARY_EXEC_ENABLE_VM_FUNCTIONS)= ) printk("Virtual processor ID =3D 0x%04x VMfunc controls =3D %016lx= \n", vmr16(VIRTUAL_PROCESSOR_ID), vmr(VM_FUNCTION_CONTROL)); - vmx_vmcs_exit(v); } =20 diff --git a/xen/arch/x86/hvm/vmx/vmx.c b/xen/arch/x86/hvm/vmx/vmx.c index dceff2f221..15989cf136 100644 --- a/xen/arch/x86/hvm/vmx/vmx.c +++ b/xen/arch/x86/hvm/vmx/vmx.c @@ -25,6 +25,7 @@ #include #include #include +#include #include #include #include @@ -834,6 +835,18 @@ static void cf_check vmx_cpuid_policy_changed(struct v= cpu *v) vmx_update_secondary_exec_control(v); } =20 + if ( asid_enabled ) + { + v->arch.hvm.vmx.secondary_exec_control |=3D SECONDARY_EXEC_ENABLE_= VPID; + vmx_update_secondary_exec_control(v); + } + else + { + v->arch.hvm.vmx.secondary_exec_control &=3D ~SECONDARY_EXEC_ENABLE= _VPID; + vmx_update_secondary_exec_control(v); + } + + /* * We can safely pass MSR_SPEC_CTRL through to the guest, even if STIBP * isn't enumerated in hardware, as SPEC_CTRL_STIBP is ignored. @@ -1510,7 +1523,6 @@ static void cf_check vmx_handle_cd(struct vcpu *v, un= signed long value) vmx_set_msr_intercept(v, MSR_IA32_CR_PAT, VMX_MSR_RW); =20 wbinvd(); /* flush possibly polluted cache */ - hvm_asid_flush_vcpu(v); /* invalidate memory type cached in TL= B */ v->needs_tlb_flush =3D true; /* invalidate memory type cached = in TLB */ v->arch.hvm.vmx.cache_mode =3D CACHE_MODE_NO_FILL; } @@ -1520,7 +1532,6 @@ static void cf_check vmx_handle_cd(struct vcpu *v, un= signed long value) vmx_set_guest_pat(v, *pat); if ( !is_iommu_enabled(v->domain) || iommu_snoop ) vmx_clear_msr_intercept(v, MSR_IA32_CR_PAT, VMX_MSR_RW); - hvm_asid_flush_vcpu(v); /* no need to flush cache */ v->needs_tlb_flush =3D true; } } @@ -1873,7 +1884,6 @@ static void cf_check vmx_update_guest_cr( __vmwrite(GUEST_CR3, v->arch.hvm.hw_cr[3]); =20 if ( !(flags & HVM_UPDATE_GUEST_CR3_NOFLUSH) ) - hvm_asid_flush_vcpu(v); v->needs_tlb_flush =3D true; break; =20 @@ -3171,6 +3181,8 @@ const struct hvm_function_table * __init start_vmx(vo= id) lbr_tsx_fixup_check(); ler_to_fixup_check(); =20 + BUG_ON(hvm_asid_init(cpu_has_vmx_vpid ? (1u << VMCS_VPID_WIDTH) : 1)); + return &vmx_function_table; } =20 @@ -4934,9 +4946,7 @@ bool asmlinkage vmx_vmenter_helper(const struct cpu_u= ser_regs *regs) { struct vcpu *curr =3D current; struct domain *currd =3D curr->domain; - u32 new_asid, old_asid; - struct hvm_vcpu_asid *p_asid; - bool need_flush; + struct hvm_asid *p_asid; unsigned int cpu =3D smp_processor_id(); =20 ASSERT(hvmemul_cache_disabled(curr)); @@ -4948,38 +4958,16 @@ bool asmlinkage vmx_vmenter_helper(const struct cpu= _user_regs *regs) if ( curr->domain->arch.hvm.pi_ops.vcpu_block ) vmx_pi_do_resume(curr); =20 - if ( !cpu_has_vmx_vpid ) + if ( !asid_enabled ) goto out; if ( nestedhvm_vcpu_in_guestmode(curr) ) p_asid =3D &vcpu_nestedhvm(curr).nv_n2asid; else - p_asid =3D &curr->arch.hvm.n1asid; + p_asid =3D &currd->arch.hvm.asid; =20 - old_asid =3D p_asid->asid; - need_flush =3D hvm_asid_handle_vmenter(p_asid); - new_asid =3D p_asid->asid; + __vmwrite(VIRTUAL_PROCESSOR_ID, p_asid->asid); =20 - if ( unlikely(new_asid !=3D old_asid) ) - { - __vmwrite(VIRTUAL_PROCESSOR_ID, new_asid); - if ( !old_asid && new_asid ) - { - /* VPID was disabled: now enabled. */ - curr->arch.hvm.vmx.secondary_exec_control |=3D - SECONDARY_EXEC_ENABLE_VPID; - vmx_update_secondary_exec_control(curr); - } - else if ( old_asid && !new_asid ) - { - /* VPID was enabled: now disabled. */ - curr->arch.hvm.vmx.secondary_exec_control &=3D - ~SECONDARY_EXEC_ENABLE_VPID; - vmx_update_secondary_exec_control(curr); - } - } =20 - if ( unlikely(need_flush) ) - vpid_sync_all(); /** * Check if we were the latest vCPU of this domain that ran on this pC= PU. * Flush the TLB if it is not, as the TLB entries are the ones from th= e previous @@ -4993,16 +4981,21 @@ bool asmlinkage vmx_vmenter_helper(const struct cpu= _user_regs *regs) if ( paging_mode_hap(curr->domain) ) { struct ept_data *ept =3D &p2m_get_hostp2m(currd)->ept; - unsigned int cpu =3D smp_processor_id(); unsigned int inv =3D 0; /* None =3D> Single =3D> All */ struct ept_data *single =3D NULL; /* Single eptp, iff inv =3D=3D 1= */ =20 + if ( test_and_clear_bool(curr->needs_tlb_flush) ) + { + inv =3D 1; + single =3D ept; + } + if ( cpumask_test_cpu(cpu, ept->invalidate) ) { cpumask_clear_cpu(cpu, ept->invalidate); =20 /* Automatically invalidate all contexts if nested. */ - inv +=3D 1 + nestedhvm_enabled(currd); + inv =3D 1 + nestedhvm_enabled(currd); single =3D ept; } =20 @@ -5031,6 +5024,11 @@ bool asmlinkage vmx_vmenter_helper(const struct cpu_= user_regs *regs) __invept(inv =3D=3D 1 ? INVEPT_SINGLE_CONTEXT : INVEPT_ALL_CON= TEXT, inv =3D=3D 1 ? single->eptp : 0); } + else /* Shadow paging */ + { + if ( test_and_clear_bool(curr->needs_tlb_flush) ) + vpid_sync_vcpu_context(curr); + } =20 out: if ( unlikely(curr->arch.hvm.vmx.lbr_flags & LBR_FIXUP_MASK) ) diff --git a/xen/arch/x86/hvm/vmx/vvmx.c b/xen/arch/x86/hvm/vmx/vvmx.c index 16d6f1d61b..7314aa5633 100644 --- a/xen/arch/x86/hvm/vmx/vvmx.c +++ b/xen/arch/x86/hvm/vmx/vvmx.c @@ -1253,7 +1253,6 @@ static void virtual_vmentry(struct cpu_user_regs *reg= s) =20 if ( nvmx->guest_vpid !=3D new_vpid ) { - hvm_asid_flush_vcpu_asid(&vcpu_nestedhvm(v).nv_n2asid); v->needs_tlb_flush =3D true; nvmx->guest_vpid =3D new_vpid; } @@ -2053,7 +2052,7 @@ static int nvmx_handle_invvpid(struct cpu_user_regs *= regs) case INVVPID_INDIVIDUAL_ADDR: case INVVPID_SINGLE_CONTEXT: case INVVPID_ALL_CONTEXT: - hvm_asid_flush_vcpu_asid(&vcpu_nestedhvm(current).nv_n2asid); + hvm_flush_tlb(NULL); break; default: vmfail(regs, VMX_INSN_INVEPT_INVVPID_INVALID_OP); diff --git a/xen/arch/x86/include/asm/flushtlb.h b/xen/arch/x86/include/asm= /flushtlb.h index 7bcbca2b7f..d8167aca18 100644 --- a/xen/arch/x86/include/asm/flushtlb.h +++ b/xen/arch/x86/include/asm/flushtlb.h @@ -125,12 +125,6 @@ void switch_cr3_cr4(unsigned long cr3, unsigned long c= r4); #define FLUSH_VCPU_STATE 0x1000 /* Flush the per-cpu root page table */ #define FLUSH_ROOT_PGTBL 0x2000 -#if CONFIG_HVM - /* Flush all HVM guests linear TLB (using ASID/VPID) */ -#define FLUSH_HVM_ASID_CORE 0x4000 -#else -#define FLUSH_HVM_ASID_CORE 0 -#endif #if defined(CONFIG_PV) || defined(CONFIG_SHADOW_PAGING) /* * Adding this to the flags passed to flush_area_mask will prevent using t= he @@ -190,7 +184,6 @@ void flush_area_mask(const cpumask_t *mask, const void = *va, =20 static inline void flush_page_to_ram(unsigned long mfn, bool sync_icache) = {} =20 -unsigned int guest_flush_tlb_flags(const struct domain *d); void guest_flush_tlb_mask(const struct domain *d, const cpumask_t *mask); =20 #endif /* __FLUSHTLB_H__ */ diff --git a/xen/arch/x86/include/asm/hvm/asid.h b/xen/arch/x86/include/asm= /hvm/asid.h index 25ba57e768..13ea357f70 100644 --- a/xen/arch/x86/include/asm/hvm/asid.h +++ b/xen/arch/x86/include/asm/hvm/asid.h @@ -8,25 +8,21 @@ #ifndef __ASM_X86_HVM_ASID_H__ #define __ASM_X86_HVM_ASID_H__ =20 +#include +#include =20 -struct vcpu; -struct hvm_vcpu_asid; +struct hvm_asid { + uint32_t asid; +}; =20 -/* Initialise ASID management for the current physical CPU. */ -void hvm_asid_init(unsigned int nasids); +extern bool asid_enabled; =20 -/* Invalidate a particular ASID allocation: forces re-allocation. */ -void hvm_asid_flush_vcpu_asid(struct hvm_vcpu_asid *asid); +/* Initialise ASID management distributed across all CPUs. */ +int hvm_asid_init(unsigned long nasids); =20 -/* Invalidate all ASID allocations for specified VCPU: forces re-allocatio= n. */ -void hvm_asid_flush_vcpu(struct vcpu *v); - -/* Flush all ASIDs on this processor core. */ -void hvm_asid_flush_core(void); - -/* Called before entry to guest context. Checks ASID allocation, returns a - * boolean indicating whether all ASIDs must be flushed. */ -bool hvm_asid_handle_vmenter(struct hvm_vcpu_asid *asid); +int hvm_asid_alloc(struct hvm_asid *asid); +int hvm_asid_alloc_range(struct hvm_asid *asid, unsigned long min, unsigne= d long max); +void hvm_asid_free(struct hvm_asid *asid); =20 #endif /* __ASM_X86_HVM_ASID_H__ */ =20 diff --git a/xen/arch/x86/include/asm/hvm/domain.h b/xen/arch/x86/include/a= sm/hvm/domain.h index abf9bc448d..03fe6c8f7b 100644 --- a/xen/arch/x86/include/asm/hvm/domain.h +++ b/xen/arch/x86/include/asm/hvm/domain.h @@ -139,6 +139,7 @@ struct hvm_domain { } write_map; =20 struct hvm_pi_ops pi_ops; + struct hvm_asid asid; =20 union { struct vmx_domain vmx; diff --git a/xen/arch/x86/include/asm/hvm/hvm.h b/xen/arch/x86/include/asm/= hvm/hvm.h index e7c1364802..935d9e7548 100644 --- a/xen/arch/x86/include/asm/hvm/hvm.h +++ b/xen/arch/x86/include/asm/hvm/hvm.h @@ -274,6 +274,8 @@ int hvm_domain_initialise(struct domain *d, void hvm_domain_relinquish_resources(struct domain *d); void hvm_domain_destroy(struct domain *d); =20 +int hvm_flush_tlb(const unsigned long *vcpu_bitmap); + int hvm_vcpu_initialise(struct vcpu *v); void hvm_vcpu_destroy(struct vcpu *v); void hvm_vcpu_down(struct vcpu *v); @@ -497,17 +499,6 @@ static inline void hvm_set_tsc_offset(struct vcpu *v, = uint64_t offset) alternative_vcall(hvm_funcs.set_tsc_offset, v, offset); } =20 -/* - * Called to ensure than all guest-specific mappings in a tagged TLB are=20 - * flushed; does *not* flush Xen's TLB entries, and on processors without = a=20 - * tagged TLB it will be a noop. - */ -static inline void hvm_flush_guest_tlbs(void) -{ - if ( hvm_enabled ) - hvm_asid_flush_core(); -} - static inline unsigned int hvm_get_cpl(struct vcpu *v) { @@ -901,8 +892,6 @@ static inline int hvm_cpu_up(void) =20 static inline void hvm_cpu_down(void) {} =20 -static inline void hvm_flush_guest_tlbs(void) {} - static inline void hvm_invlpg(const struct vcpu *v, unsigned long linear) { ASSERT_UNREACHABLE(); diff --git a/xen/arch/x86/include/asm/hvm/svm.h b/xen/arch/x86/include/asm/= hvm/svm.h index a35a61273b..1877bb149a 100644 --- a/xen/arch/x86/include/asm/hvm/svm.h +++ b/xen/arch/x86/include/asm/hvm/svm.h @@ -9,6 +9,11 @@ #ifndef __ASM_X86_HVM_SVM_H__ #define __ASM_X86_HVM_SVM_H__ =20 +void svm_asid_init(void); +void svm_vcpu_assign_asid(struct vcpu *v); +void svm_vcpu_set_tlb_control(struct vcpu *v); +void svm_vcpu_clear_tlb_control(struct vcpu *v); + /* * PV context switch helpers. Prefetching the VMCB area itself has been s= hown * to be useful for performance. diff --git a/xen/arch/x86/include/asm/hvm/vcpu.h b/xen/arch/x86/include/asm= /hvm/vcpu.h index 836138a4a6..7472cdd0f2 100644 --- a/xen/arch/x86/include/asm/hvm/vcpu.h +++ b/xen/arch/x86/include/asm/hvm/vcpu.h @@ -9,6 +9,7 @@ #define __ASM_X86_HVM_VCPU_H__ =20 #include +#include #include #include #include @@ -16,11 +17,6 @@ #include #include =20 -struct hvm_vcpu_asid { - uint64_t generation; - uint32_t asid; -}; - struct hvm_vcpu_io { /* * HVM emulation: @@ -78,7 +74,7 @@ struct nestedvcpu { bool stale_np2m; /* True when p2m_base in VMCx02 is no longer valid */ uint64_t np2m_generation; =20 - struct hvm_vcpu_asid nv_n2asid; + struct hvm_asid nv_n2asid; =20 bool nv_vmentry_pending; bool nv_vmexit_pending; @@ -142,8 +138,6 @@ struct hvm_vcpu { /* (MFN) hypervisor page table */ pagetable_t monitor_table; =20 - struct hvm_vcpu_asid n1asid; - u64 msr_tsc_adjust; =20 union { diff --git a/xen/arch/x86/include/asm/hvm/vmx/vmx.h b/xen/arch/x86/include/= asm/hvm/vmx/vmx.h index 3524cb3536..4f307ae123 100644 --- a/xen/arch/x86/include/asm/hvm/vmx/vmx.h +++ b/xen/arch/x86/include/asm/hvm/vmx/vmx.h @@ -470,7 +470,7 @@ static inline void vpid_sync_vcpu_context(const struct = vcpu *v) type =3D INVVPID_ALL_CONTEXT; =20 execute_invvpid: - __invvpid(type, v->arch.hvm.n1asid.asid, 0); + __invvpid(type, v->domain->arch.hvm.asid.asid, 0); } =20 static inline void vpid_sync_vcpu_gva(struct vcpu *v, unsigned long gva) @@ -494,7 +494,7 @@ static inline void vpid_sync_vcpu_gva(struct vcpu *v, u= nsigned long gva) type =3D INVVPID_ALL_CONTEXT; =20 execute_invvpid: - __invvpid(type, v->arch.hvm.n1asid.asid, (u64)gva); + __invvpid(type, v->domain->arch.hvm.asid.asid, (u64)gva); } =20 static inline void vpid_sync_all(void) diff --git a/xen/arch/x86/mm/hap/hap.c b/xen/arch/x86/mm/hap/hap.c index 5ccb80bda5..156734d3e0 100644 --- a/xen/arch/x86/mm/hap/hap.c +++ b/xen/arch/x86/mm/hap/hap.c @@ -27,6 +27,7 @@ #include #include #include +#include #include #include =20 @@ -750,18 +751,16 @@ static bool cf_check flush_tlb(const unsigned long *v= cpu_bitmap) if ( !flush_vcpu(v, vcpu_bitmap) ) continue; =20 - hvm_asid_flush_vcpu(v); - cpu =3D read_atomic(&v->dirty_cpu); if ( cpu !=3D this_cpu && is_vcpu_dirty_cpu(cpu) && v->is_running ) __cpumask_set_cpu(cpu, mask); } =20 + guest_flush_tlb_mask(d, mask); + /* * Trigger a vmexit on all pCPUs with dirty vCPU state in order to for= ce an - * ASID/VPID change and hence accomplish a guest TLB flush. Note that = vCPUs - * not currently running will already be flushed when scheduled becaus= e of - * the ASID tickle done in the loop above. + * ASID/VPID flush and hence accomplish a guest TLB flush. */ on_selected_cpus(mask, NULL, NULL, 0); =20 diff --git a/xen/arch/x86/mm/p2m.c b/xen/arch/x86/mm/p2m.c index 910623ac93..6eb6035f09 100644 --- a/xen/arch/x86/mm/p2m.c +++ b/xen/arch/x86/mm/p2m.c @@ -1439,7 +1439,6 @@ p2m_flush(struct vcpu *v, struct p2m_domain *p2m) ASSERT(v->domain =3D=3D p2m->domain); vcpu_nestedhvm(v).nv_p2m =3D NULL; p2m_flush_table(p2m); - hvm_asid_flush_vcpu(v); v->needs_tlb_flush =3D true; } =20 @@ -1499,7 +1498,6 @@ static void assign_np2m(struct vcpu *v, struct p2m_do= main *p2m) =20 static void nvcpu_flush(struct vcpu *v) { - hvm_asid_flush_vcpu(v); v->needs_tlb_flush =3D true; vcpu_nestedhvm(v).stale_np2m =3D true; } @@ -1620,7 +1618,6 @@ void np2m_schedule(int dir) if ( !np2m_valid ) { /* This vCPU's np2m was flushed while it was not runnable = */ - hvm_asid_flush_core(); curr->needs_tlb_flush =3D true; vcpu_nestedhvm(curr).nv_p2m =3D NULL; } diff --git a/xen/arch/x86/mm/paging.c b/xen/arch/x86/mm/paging.c index b0b3bef753..af5584a777 100644 --- a/xen/arch/x86/mm/paging.c +++ b/xen/arch/x86/mm/paging.c @@ -938,7 +938,6 @@ void paging_update_nestedmode(struct vcpu *v) else /* TODO: shadow-on-shadow */ v->arch.paging.nestedmode =3D NULL; - hvm_asid_flush_vcpu(v); v->needs_tlb_flush =3D true; } =20 diff --git a/xen/arch/x86/mm/shadow/multi.c b/xen/arch/x86/mm/shadow/multi.c index 2df2842138..0eef92bc5b 100644 --- a/xen/arch/x86/mm/shadow/multi.c +++ b/xen/arch/x86/mm/shadow/multi.c @@ -81,12 +81,6 @@ const char *const fetch_type_names[] =3D { =20 static pagetable_t cf_check sh_update_cr3(struct vcpu *v, bool noflush); =20 -/* Helper to perform a local TLB flush. */ -static void sh_flush_local(const struct domain *d) -{ - flush_local(guest_flush_tlb_flags(d)); -} - #if GUEST_PAGING_LEVELS >=3D 4 && defined(CONFIG_PV32) #define ASSERT_VALID_L2(t) \ ASSERT((t) =3D=3D SH_type_l2_shadow || (t) =3D=3D SH_type_l2h_shadow) @@ -2947,7 +2941,8 @@ static bool cf_check sh_invlpg(struct vcpu *v, unsign= ed long linear) if ( mfn_to_page(sl1mfn)->u.sh.type =3D=3D SH_type_fl1_shadow ) { - sh_flush_local(v->domain); + flush_tlb_local(); + v->needs_tlb_flush =3D true; return false; } =20 @@ -3164,7 +3159,7 @@ sh_update_linear_entries(struct vcpu *v) * linear pagetable to read a top-level shadow page table entry. But, * without this change, it would fetch the wrong value due to a stale = TLB. */ - sh_flush_local(d); + flush_tlb_local(); v->needs_tlb_flush =3D true; } =20 --=20 2.52.0 -- Teddy Astie | Vates XCP-ng Developer XCP-ng & Xen Orchestra - Vates solutions web: https://vates.tech From nobody Tue May 5 10:13:32 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass header.i=teddy.astie@vates.tech; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=none dis=none) header.from=vates.tech ARC-Seal: i=1; a=rsa-sha256; t=1776259965; cv=none; d=zohomail.com; s=zohoarc; b=IFLa36o77PXlGD5HxEVRWc4ZgrNI/jOFfRK8c7bqsC83JX07tjpHtTKJ3pv3zIhcU8jy+X3Awn3DymJYfC31KZFugVBL3mbMlVrvUD1qIb+5Cx39GTVErb5irGDUZzzgMNZ45E6S4vr+n8U2QFgpiJetP4jwWZZYwk51SrdKFjw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1776259965; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=PxrQtMDpZ4zK9NM/vPWDKrs+JHdyCRhp9oAg6MOgvbY=; b=RsS2zLRY6iNGE35TrqYsVF9RJij7tsgzoOCT8ms2/rCH/F02r6hMb8HftKvaM4Vx6tziIYIB+nFVV/1Z0X/GZ+t1FonU3uFrqnepie365I1qbGAIwdF6hN4LaTrfsafW0NsetrALZqxGCd5IqFpwCTURtCB//aClJ09TTTBX8KI= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass header.i=teddy.astie@vates.tech; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1776259965194253.7966069153482; Wed, 15 Apr 2026 06:32:45 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1282456.1565053 (Exim 4.92) (envelope-from ) id 1wD0M4-0000yz-MS; Wed, 15 Apr 2026 13:32:32 +0000 Received: by outflank-mailman (output) from mailman id 1282456.1565053; Wed, 15 Apr 2026 13:32:32 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1wD0M4-0000xS-Ix; Wed, 15 Apr 2026 13:32:32 +0000 Received: by outflank-mailman (input) for mailman id 1282456; Wed, 15 Apr 2026 13:32:31 +0000 Received: from mx.expurgate.net ([195.190.135.10]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1wD0M3-0000rk-IL for xen-devel@lists.xenproject.org; Wed, 15 Apr 2026 13:32:31 +0000 Received: from mx.expurgate.net (helo=localhost) by mx.expurgate.net with esmtp id 1wD0M2-006OmM-UK for xen-devel@lists.xenproject.org; Wed, 15 Apr 2026 15:32:30 +0200 Received: from [10.42.69.8] (helo=localhost) by localhost with ESMTP (eXpurgate MTA 0.9.1) (envelope-from ) id 69df936d-e002-0a2a0a5209dd-0a2a4508b786-8 for ; Wed, 15 Apr 2026 15:32:30 +0200 Received: from [198.2.187.14] (helo=mail187-14.suw11.mandrillapp.com) by tlsNG-c1860d.mxtls.expurgate.net with ESMTPS (eXpurgate 4.56.0) (envelope-from ) id 69df936d-fab6-0a2a45080019-c602bb0e6efc-3 for ; Wed, 15 Apr 2026 15:32:30 +0200 Received: from pmta09.mandrill.prod.suw01.rsglab.com (localhost [127.0.0.1]) by mail187-14.suw11.mandrillapp.com (Mailchimp) with ESMTP id 4fwhs93YTkz8XSS6N for ; Wed, 15 Apr 2026 13:32:29 +0000 (GMT) Received: from [37.26.189.201] by mandrillapp.com id 0c12d93e9f5c4829941f2040aaaea336; Wed, 15 Apr 2026 13:32:29 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" Authentication-Results: eu.smtp.expurgate.cloud; dkim=pass header.s=mte1 header.d=mandrillapp.com header.i="@mandrillapp.com" header.h="From:Subject:To:Cc:Message-Id:In-Reply-To:References:Feedback-ID:Date:MIME-Version:Content-Type:Content-Transfer-Encoding"; dkim=pass header.s=mte1 header.d=vates.tech header.i="teddy.astie@vates.tech" header.h="From:Subject:To:Cc:Message-Id:In-Reply-To:References:Feedback-ID:Date:MIME-Version:Content-Type:Content-Transfer-Encoding" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=mandrillapp.com; s=mte1; t=1776259949; x=1776529949; bh=PxrQtMDpZ4zK9NM/vPWDKrs+JHdyCRhp9oAg6MOgvbY=; h=From:Subject:To:Cc:Message-Id:In-Reply-To:References:Feedback-ID: Date:MIME-Version:Content-Type:Content-Transfer-Encoding:CC:Date: Subject:From; b=oVZdRPBp1MTtk98qliB0H0nRsGwTVPFph7/ZjEQ8768eeQDmTSSpYfKFvHrtQcaUa 4uOZj7xW/xq1MqSXcerjQg9gsiAyW5mbPTHDDDkqe0ZC5qpxdPhpmKOvXMNZH5VGgG dES0Sf3STEa6vMnvcvmd5tEEfnyyYj0MuUlfyobpEtOjYTqL7HvjqFdlF6zLoqjg7w EMkQKCfjBbh5zlfQ+GXRnfau5rMnHMuoqXQgvdf/siTEMysaQKYFBiGZ6heUKM9hFz ig2rE/idtKk1q03yMD56Jr0cWowqNZ9kHIm/zBJztOp2/lkR8MBeW/a1hx8AN+sO9T U1/24lnP1qJVA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=vates.tech; s=mte1; t=1776259949; x=1776520449; i=teddy.astie@vates.tech; bh=PxrQtMDpZ4zK9NM/vPWDKrs+JHdyCRhp9oAg6MOgvbY=; h=From:Subject:To:Cc:Message-Id:In-Reply-To:References:Feedback-ID: Date:MIME-Version:Content-Type:Content-Transfer-Encoding:CC:Date: Subject:From; b=mmEcEjbID8EA1NDPxstRncJDX6FMeMCENCqKnJYaTtJyyqoR/i32ckL1o+Xfor5pH leYnLEKkPK5G4ORftlm/I0yqLKjBDA0F7zGbCS61tx3D+/MkbztTWv1LqBExPOjKSl lSsOHKPmg5Le9lO+ZNuDc+G0Wj8a3AmexFd7zRjQ3dO9YyA4gMk0F4t99Ljqd2azIu ZsUIqy4YN3hyUjL3+CvnNxceBhVtEzmKhJWAem6/oGC2eDv1U+2q35KogEVwEkT7dE zF3lFgkPZx6p70oqGz92bRn7VEAJ/Ca7L6dKnypK97VBTidmFMxc5/6JpVwyzc9jHx VMnp0wnAp0vww== From: "Teddy Astie" Subject: =?utf-8?Q?[RFC=20PATCH=207/7]=20hvm:=20Allow=20specifying=20a=20prefered=20asid=20minimum?= X-Mailer: git-send-email 2.52.0 X-Bm-Disclaimer: Yes X-Bm-Milter-Handled: 4ffbd6c1-ee69-4e1b-aabd-f977039bd3e2 X-Bm-Transport-Timestamp: 1776259947257 To: xen-devel@lists.xenproject.org Cc: "Teddy Astie" , "Jan Beulich" , "Andrew Cooper" , "=?utf-8?Q?Roger=20Pau=20Monn=C3=A9?=" Message-Id: <1603515ebdf6f7b26d5503c99ab5e5e2ac8fe278.1776259595.git.teddy.astie@vates.tech> In-Reply-To: References: X-Native-Encoded: 1 X-Report-Abuse: =?UTF-8?Q?Please=20forward=20a=20copy=20of=20this=20message,=20including=20all=20headers,=20to=20abuse@mandrill.com.=20You=20can=20also=20report=20abuse=20here:=20https://mandrillapp.com/contact/abuse=3Fid=3D30504962.0c12d93e9f5c4829941f2040aaaea336?= X-Mandrill-User: md_30504962 Feedback-ID: 30504962:30504962.20260415:md Date: Wed, 15 Apr 2026 13:32:29 +0000 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-purgate-ID: tlsNG-c1860d/1776259950-75358497-AD69C7F1/0/0 X-purgate-type: clean X-purgate-size: 1837 X-ZohoMail-DKIM: pass (identity teddy.astie@vates.tech) (identity @mandrillapp.com) X-ZM-MESSAGEID: 1776259967730154100 Content-Type: text/plain; charset="utf-8" To avoid clobbering all ASIDs that are below SEV-enabled guest maximum, we want to allocate if possible all ASID over a "prefered minimum" and fallback to below otherwise. Signed-off-by: Teddy Astie --- xen/arch/x86/hvm/asid.c | 8 ++++++++ xen/arch/x86/include/asm/hvm/asid.h | 1 + 2 files changed, 9 insertions(+) diff --git a/xen/arch/x86/hvm/asid.c b/xen/arch/x86/hvm/asid.c index 1a21125161..4ad3200c96 100644 --- a/xen/arch/x86/hvm/asid.c +++ b/xen/arch/x86/hvm/asid.c @@ -22,6 +22,9 @@ boolean_param("asid", opt_asid_enabled); bool __read_mostly asid_enabled =3D false; static unsigned long __ro_after_init *asid_bitmap; static unsigned long __ro_after_init asid_count; + +/* Default minimum ASID to use */ +unsigned long __read_mostly asid_default_min =3D 0; static DEFINE_SPINLOCK(asid_lock); =20 /* @@ -67,6 +70,11 @@ int hvm_asid_alloc(struct hvm_asid *asid) return 0; } =20 + /* Try to allocate above default minimum */ + if ( asid_default_min && + !hvm_asid_alloc_range(asid, asid_default_min, asid_count) ) + return 0; + spin_lock(&asid_lock); new_asid =3D find_first_zero_bit(asid_bitmap, asid_count); if ( new_asid > asid_count ) diff --git a/xen/arch/x86/include/asm/hvm/asid.h b/xen/arch/x86/include/asm= /hvm/asid.h index 13ea357f70..e989ebbe8c 100644 --- a/xen/arch/x86/include/asm/hvm/asid.h +++ b/xen/arch/x86/include/asm/hvm/asid.h @@ -16,6 +16,7 @@ struct hvm_asid { }; =20 extern bool asid_enabled; +extern unsigned long asid_default_min; =20 /* Initialise ASID management distributed across all CPUs. */ int hvm_asid_init(unsigned long nasids); --=20 2.52.0 -- Teddy Astie | Vates XCP-ng Developer XCP-ng & Xen Orchestra - Vates solutions web: https://vates.tech