From nobody Mon Nov 23 22:31:33 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; 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=quarantine dis=none) header.from=edera.dev ARC-Seal: i=1; a=rsa-sha256; t=1793952571; cv=none; d=zohomail.com; s=zohoarc; b=g4HavmwZPvBTdK/8cgF7/QueCz1VC+H7ajtNHeTpW32XuAFFmz0fVxZBNNIPyDZDPn89pyzB8TwOPfcwiJrq+XliSisAIvjHxoT3V8kJwcgrK31QMlOMJEURcicQyrhIV+b0mcgw3IM/F8hFsSC9yLudNdZdJcZefUjygsjbAIY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1793952571; h=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:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=uRraAnsdRi1Ln76teebfsxImy84+5j63KoqKWqZx2lA=; b=l0tBLlg7FhRALzjeIpf5Mboe9FczFKJTlHrjl9kYl8TgqWzYzAxc+pQEIs04DbS5u6s4OpowUJVxGXTTJnNEkwNPfVjPUCLApcHNAVEoIBbYrhKWCIyerKfwbe2sEESKiHQ1eYbD77IUMsysHOQSIlX1kQuSPquyVG6SLAUxfGM= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; 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=quarantine dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1793952571209551.9311308775909; Fri, 6 Nov 2026 00:09:31 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.1476313.1682460 (Exim 4.92) (envelope-from ) id 1xPF0f-0007My-JN; Fri, 06 Nov 2026 08:09:17 +0000 Received: by outflank-mailman (output) from mailman id 1476313.1682460; Fri, 06 Nov 2026 08:09: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 1xPF0f-0007Mr-GR; Fri, 06 Nov 2026 08:09:17 +0000 Received: by outflank-mailman (input) for mailman id 1476313; Fri, 06 Nov 2026 08:09:16 +0000 Received: from mx.expurgate.net ([195.190.135.20]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xPF0e-0007MX-LC for xen-devel@lists.xenproject.org; Fri, 06 Nov 2026 08:09:16 +0000 Received: from mx.expurgate.net (helo=localhost) by mx.expurgate.net with esmtp id 1xPF0e-000tHV-1e for xen-devel@lists.xenproject.org; Fri, 06 Nov 2026 09:09:16 +0100 Received: from [10.42.69.9] (helo=localhost) by localhost with ESMTP (eXpurgate MTA 0.9.1) (envelope-from ) id 6aed8b2a-8faa-0a2a0a5109dd-0a2a4509b500-4 for ; Fri, 06 Nov 2026 09:09:16 +0100 Received: from [209.85.221.51] (helo=mail-wr1-f51.google.com) by tlsNG-bad1c0.mxtls.expurgate.net with ESMTPS (eXpurgate 4.57.1) (envelope-from ) id 6aed8b2b-be1a-0a2a45090019-d155dd33c02d-3 for ; Fri, 06 Nov 2026 09:09:16 +0100 Received: by mail-wr1-f51.google.com with SMTP id ffacd0b85a97d-491aaed7565so194971f8f.2 for ; Fri, 06 Nov 2026 00:09:15 -0800 (PST) Received: from framework13.home ([2a01:e11:3:1ff0:831:b00b:e409:f875]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-492ff85585esm1534507f8f.5.2026.11.06.00.09.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 06 Nov 2026 00:09:14 -0800 (PST) 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=google header.d=edera.dev header.i="@edera.dev" header.h="Content-Transfer-Encoding:MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=edera.dev; s=google; t=1793952555; x=1794557355; darn=lists.xenproject.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=uRraAnsdRi1Ln76teebfsxImy84+5j63KoqKWqZx2lA=; b=fc0Ve2Zbi8PeHNvfcym1N+7QkOKBhkl+mAAp2xvZR9931YFM+klJWIDfR00XdDtzaT YsDg8GajtnYsUcdVvj8OqiGBO/jCaw1485oaco4GNfuFEVrXEC/2nfMMpGxDYL2dDbES Twn+CtlUkh/ih7d+BKBP5NQe8a8UYfy3wlnxyf4pi/J1r+sN0HXA9o95e6GkZp014n50 SVa/HndDDGSyk3kPdiIkHCp2kecXx762eFD8fz9JOjNzd9jP3uHv64Ypw/J8gjumk1mn vTQEJXUHO39Eh7734DkW7RtT0HBOTAXsabpdwTDYBv3RY5/bSD0yAQliOOKyT4Tcj/jm veEg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1793952555; x=1794557355; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=uRraAnsdRi1Ln76teebfsxImy84+5j63KoqKWqZx2lA=; b=JpJiDprcd4QZLlarePNJs+5Yc/JZ0ICrgtF+ATnZIoi8vQFVCEjJccTFfG86qXuaYN B5hHOai4LdLgurH3qLe47MIScTXVYjAU6bInSqZj0D57LKkDhQqkMOMQ7FL7BgLyfLha K3Ap/MIAT8x0LDVbNtZaqE8eYuadDJfC0YoD+biW0tVkaMRpAzwQVveWlkkyHJ4d4fbc l8A9Tvf7bWMNnmBfj0xOGTeqFS+D2a6tv86fUo58jaavjqLqSddaOXmu+CMx488NS8ta 0p8TD22lId9sJBIbr1BFSJldXAIMYYyp/O1m/9JCfWPTAYg8DhRBA3O+XRlHAZQUqMQt av0A== X-Gm-Message-State: APuU4Z+Yb0lC78Ux3ayAt0sBGx9S4/0YiETSk5d2S484MCBltc9f35ee 9w7nzovxqu0o5rPlv6fGnklWb5wEJtNv28Wxhuer6hvbD4ZP//B1oCvEBTE7rhPrDbzQ+9y8Y7Z sPHUQGOE= X-Gm-Gg: AYBFou3POQguwUu5F48oynJj6NFgVp+ykkYUaWc4Y42qHkS+oO3Q/inLsi+rvBYBkzs D0BPbKi+gtNd5hItjCOE8Exnlh39Yo6cKhpiMPwXLp8tIAXfh4sVDNx2X2F3FMAQJ4DtDC+6eqq MfSAYD0LlzqUnfuR5Uh67CQATOIZ7cb3/nUpYhxpcBvHCHNTwGo8UCwyY9Yocm2BKEUdYI9R2qV N9eV5qvlWeTvgYnV7ebcwDZhaTPC0A3pprcEyBxRUL7Yt1BKvIdW9ZiQQk6RJa5KiNeAbWtxsry 3uGQ+RY6UoPGYemXMKixNXb8OyWdYbVPb8f0WwwzFgcy7wWbVdmN9rQc5gWAj72aHLVp9DOtYJb egWkOQEHzlqwg36WD+6ZtMIPi2UQaEqxhguWqhLc5e8sVnyF2F/WSNuf3+M19WmtEFcJiOvfNHU htD9QuFo1qW6XZq5AWkBnA6fcHOodsHQGSPh2CZVtBDHoP3UrRxg5iMZpduX5erNTrh6vKcjdHJ +WuBAs+TuSPdlziPcoARJoG8TKKSFN0eLWVHnGkPmp0C1QvNfm8r7mq3iRJzOe5wGt6pMrnVNnJ SkqdOQ1QyFddf0hmmuqCzomrAw== X-Received: by 2002:a05:6000:4819:b0:48e:75a1:1e38 with SMTP id ffacd0b85a97d-492784555f4mr3482395f8f.56.1793952555402; Fri, 06 Nov 2026 00:09:15 -0800 (PST) From: Luca Di Maio To: xen-devel@lists.xenproject.org Cc: Luca Di Maio , Jan Beulich , Andrew Cooper , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Teddy Astie , Stefano Stabellini , Julien Grall , Bertrand Marquis , Michal Orzel , Volodymyr Babchuk , Anthony PERARD Subject: [PATCH v2 1/4] VT-d: round IOTLB flush ranges up to an aligned power of two Date: Fri, 6 Nov 2026 09:08:51 +0100 Message-ID: <20261106080859.2285309-2-luca@edera.dev> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20261106080859.2285309-1-luca@edera.dev> References: <20261106080859.2285309-1-luca@edera.dev> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-purgate-ID: tlsNG-bad1c0/1793952556-3AAD8034-83FEFFB8/0/0 X-purgate-type: clean X-purgate-size: 1356 X-ZohoMail-DKIM: pass (identity @edera.dev) X-ZM-MESSAGEID: 1793952572964158500 Content-Type: text/plain; charset="utf-8" Page-selective invalidation takes only aligned power-of-two ranges, so iommu_flush_iotlb() flushes any other range as the whole domain. A later patch flushes one range per group of grant unmaps, which rarely has that shape. Round the range up to the smallest aligned power of two covering it. Orders the IOMMU doesn't support still get a domain-selective flush, and device TLB invalidations use the same range. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Luca Di Maio --- Changes in v2: - New patch, taking the rounding out of the grant unmap batch (Teddy). --- xen/drivers/passthrough/vtd/iommu.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/xen/drivers/passthrough/vtd/iommu.c b/xen/drivers/passthrough/= vtd/iommu.c index b41a1d41c8..faf8255f26 100644 --- a/xen/drivers/passthrough/vtd/iommu.c +++ b/xen/drivers/passthrough/vtd/iommu.c @@ -742,6 +742,10 @@ static int __must_check cf_check iommu_flush_iotlb(str= uct domain *d, dfn_t dfn, { ASSERT(page_count && !dfn_eq(dfn, INVALID_DFN)); ASSERT(flush_flags); + + /* PSI takes only aligned powers of two: cover the range with one.= */ + page_count =3D 1UL << flsl(dfn_x(dfn) ^ (dfn_x(dfn) + page_count -= 1)); + dfn =3D _dfn(dfn_x(dfn) & ~(page_count - 1)); } =20 /* --=20 2.47.3 From nobody Mon Nov 23 22:31:33 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; 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=quarantine dis=none) header.from=edera.dev ARC-Seal: i=1; a=rsa-sha256; t=1793952574; cv=none; d=zohomail.com; s=zohoarc; b=RFKnBwkYz8/ZhH/EzLaiVsnkDzlC7005JeX+8vl7DSUUtkfxGxXWjn8lgPghgmmcURUnyIceJUJWb0tXKFJUnjDwFBZ3Y1mWmmvWAqClkPdnK87EIJGT/ARgBzubqx8XvMj+dVkv3Yu+ec+phjJ57saOWa1b3fJUelh7mbEdfqA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1793952574; h=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:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=IEKAEyHBNOdxjTO8grr2RcMiadnMYmahTPI5OWv+TvE=; b=Yhy5UtpFJI+jvSyv1gIdK101JJ/7lnsF45lKlbnJPfIwsLO5QKEOC5oBjP73fB/s/05/SG+5B51YbAcEv/xqEnVR7nK3X1fphwtLRkxiAwI28+t19UT9hTDYgdnf7Ty69J3z2R7VuakUwZgvIoMq5f3IMTDpaWb6HrzNB7CHQzw= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; 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=quarantine dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1793952574233665.020568886623; Fri, 6 Nov 2026 00:09:34 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.1476314.1682469 (Exim 4.92) (envelope-from ) id 1xPF0i-0007bd-PW; Fri, 06 Nov 2026 08:09:20 +0000 Received: by outflank-mailman (output) from mailman id 1476314.1682469; Fri, 06 Nov 2026 08:09:20 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xPF0i-0007bT-MM; Fri, 06 Nov 2026 08:09:20 +0000 Received: by outflank-mailman (input) for mailman id 1476314; Fri, 06 Nov 2026 08:09:19 +0000 Received: from mx.expurgate.net ([195.190.135.20]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xPF0h-0007Zw-5k for xen-devel@lists.xenproject.org; Fri, 06 Nov 2026 08:09:19 +0000 Received: from mx.expurgate.net (helo=localhost) by mx.expurgate.net with esmtp id 1xPF0g-000tMz-J0 for xen-devel@lists.xenproject.org; Fri, 06 Nov 2026 09:09:18 +0100 Received: from [10.42.69.9] (helo=localhost) by localhost with ESMTP (eXpurgate MTA 0.9.1) (envelope-from ) id 6aed8b2a-8faa-0a2a0a5109dd-0a2a4509b500-10 for ; Fri, 06 Nov 2026 09:09:18 +0100 Received: from [209.85.221.48] (helo=mail-wr1-f48.google.com) by tlsNG-bad1c0.mxtls.expurgate.net with ESMTPS (eXpurgate 4.57.1) (envelope-from ) id 6aed8b2e-be1a-0a2a45090019-d155dd30cd6a-3 for ; Fri, 06 Nov 2026 09:09:18 +0100 Received: by mail-wr1-f48.google.com with SMTP id ffacd0b85a97d-48dfc0421d9so338353f8f.1 for ; Fri, 06 Nov 2026 00:09:18 -0800 (PST) Received: from framework13.home ([2a01:e11:3:1ff0:831:b00b:e409:f875]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-492ff85585esm1534507f8f.5.2026.11.06.00.09.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 06 Nov 2026 00:09:17 -0800 (PST) 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=google header.d=edera.dev header.i="@edera.dev" header.h="Content-Transfer-Encoding:MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=edera.dev; s=google; t=1793952558; x=1794557358; darn=lists.xenproject.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=IEKAEyHBNOdxjTO8grr2RcMiadnMYmahTPI5OWv+TvE=; b=De2RiAaiB2O14DOFE0sO/fUsQzGpTH+zjsT2Y811te+E9w6w141GKwDfPh2QWIHBdw X0k/tDzWDwWzAcAR2eZ8LOR5TkTlfNgiDyvP9rzA/PWUKzpnliSyRq6Y/iRa5N76kajP bDoWbs6nhsaCLd8ZBacVUxBnDOIOxZp+qOb+mWyGhaMadHtxb+mtXk0UbDtaXw4Z+zsA 7PZthVM0HAmn5Dz68Evv80PBMsHzT0MkHgWwWFSWVMwEFA6siEzjlgto0YA3asqqmldm oVCGvLoat0O6NnmlsiRqnVC3K+Lipskr4wW9qHDUn0zk7dg4Lq814wAOigCvn+T+e1w/ IZVQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1793952558; x=1794557358; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=IEKAEyHBNOdxjTO8grr2RcMiadnMYmahTPI5OWv+TvE=; b=0Y00uN/LgLcjbo6TAsHLKEL37HqEEZy+iFDa8y5iKOHBt79apdOqTgfrEQNYeBQCYs lwhSp26W4l7PATIpkpB2vsZFDJdafEgeCNA+0vDVHM/xme0fK8pQZj8A3RDkZdKKuiCE 6sRz/dO1S+V8lZttxsDrEWBpmGtarnU6gKm0Ra0WXWdRF97mO3XLuyPnXxscWIZ9+Yt9 zsJAjnYtuuPBMPiEevlMrwWKCcM2fX9qza8Rd2jWGeOwfoWGdbj6orW2W4siFKg977G6 boFGwityzmGD5MbgsHzYUM1AyzT2Am+KIesxXFMshqdexsd2gBPMGgtVReddJooIlHvN wIpg== X-Gm-Message-State: APuU4Z+Tc1kBOwhKcbCUYf2jWxqSgh/S4fSg/zfH1QgAqZb52eM5HXPw ZYv3MsHxVrs59YFIDvSWipCieF/AgQX1FtlPSnOa52hwhcdz9jwDd0MqsdZYQRjAgBUL8fJ/RWV /J57pksc= X-Gm-Gg: AYBFou3iW5T4FxUs4eBfbu4lukaeGPE+rVRIDe6AGdAvGR3J7/KaXVXJYReR/fn5SBa RDa9CgT+UDcCvabHen8bK46aLpi7Xl7Z5kNF7waCdEbaI2pKtgZ2jEITeuSGlJONCtHuFVz/y8L s9FJ391Z7zXBLjp/b7lseaOaYm3kRMFkzdQr9MdmYYsxI8X8aY2WxmxjhpJV4NDGkKFlp/W8OLb W81q2afTmnXaGvcDlVFlAyVYyX0lUwlAQozJM9NoYvE5Os8rnDoBKTg+mmCJbVGrQ1WqAO9T0Uc rst/XyLctqn44uYysNKDDPWB+foLc/79anBkyr5FnfnLob8U6TNtx8xIhCV85g2nYU2r4CF+Aud BmXGCIZB6zJKa5T639GIimmQMKbelw9G584gPULuZ+LJVE6APMz0ONE9dQA0Nzw/+oISpDXyGLs EWo6/jfADb45tr0QNxTjqzzBvtmusj0JC4VFDrUDL5R4T8uWwAyzmJIyHAr+CHiD2H7ajxjFjQY 48JvxmVULmgw1ibLooV6gEsTyijLCpWPKd6Z5UUG3LfyLV6DuMqaW5zWhCMClWy1683Rdqtivdf +VzPsxVwykJWNQLJNgB+UpIkgA== X-Received: by 2002:a05:6000:2dc2:b0:48d:db77:d124 with SMTP id ffacd0b85a97d-49273e5bcb5mr3324321f8f.6.1793952557946; Fri, 06 Nov 2026 00:09:17 -0800 (PST) From: Luca Di Maio To: xen-devel@lists.xenproject.org Cc: Luca Di Maio , Jan Beulich , Andrew Cooper , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Teddy Astie , Stefano Stabellini , Julien Grall , Bertrand Marquis , Michal Orzel , Volodymyr Babchuk , Anthony PERARD Subject: [PATCH v2 2/4] IOMMU: record the flushes skipped under iommu_dont_flush_iotlb Date: Fri, 6 Nov 2026 09:08:52 +0100 Message-ID: <20261106080859.2285309-3-luca@edera.dev> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20261106080859.2285309-1-luca@edera.dev> References: <20261106080859.2285309-1-luca@edera.dev> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-purgate-ID: tlsNG-bad1c0/1793952558-BFAD0034-3315047D/0/0 X-purgate-type: clean X-purgate-size: 4385 X-ZohoMail-DKIM: pass (identity @edera.dev) X-ZM-MESSAGEID: 1793952574935158500 Content-Type: text/plain; charset="utf-8" While iommu_dont_flush_iotlb is set, iommu_legacy_map(), iommu_legacy_unmap() and the shared-EPT path of ept_set_entry() skip their IOTLB flush and drop its range and flags, so a caller deferring the flush has to work out what to flush on its own. Check the flag in iommu_iotlb_flush() instead, which all three call, and record what it skips in the per-CPU iommu_skipped_flush: the smallest range covering the skipped flushes, and their flags. No other caller of iommu_iotlb_flush() runs while the flag is set, and the code setting it clears it before its own flush. The next patch uses the record for grant unmaps. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Luca Di Maio --- Changes in v2: - Check the flag in iommu_iotlb_flush() rather than in its callers, which also drops the duplicated flags in ept_set_entry() (Teddy). - Record the range of the skipped flushes as well as their flags (Teddy). - Store the flags as unsigned int in struct iommu_skipped_flush, instead of a uint8_t checked by a BUILD_BUG_ON() (Teddy, Jan). --- xen/arch/x86/mm/p2m-ept.c | 2 +- xen/drivers/passthrough/iommu.c | 17 +++++++++++++++-- xen/include/xen/iommu.h | 6 ++++++ 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/xen/arch/x86/mm/p2m-ept.c b/xen/arch/x86/mm/p2m-ept.c index 204bdb054a..33c3d8fc58 100644 --- a/xen/arch/x86/mm/p2m-ept.c +++ b/xen/arch/x86/mm/p2m-ept.c @@ -976,7 +976,7 @@ out: if ( rc =3D=3D 0 && p2m_is_hostp2m(p2m) && need_modify_vtd_table ) { - if ( iommu_use_hap_pt(d) && !this_cpu(iommu_dont_flush_iotlb) ) + if ( iommu_use_hap_pt(d) ) rc =3D iommu_iotlb_flush(d, _dfn(gfn), 1ul << order, (iommu_flags ? IOMMU_FLUSHF_added : 0) | (vtd_pte_present ? IOMMU_FLUSHF_modified diff --git a/xen/drivers/passthrough/iommu.c b/xen/drivers/passthrough/iomm= u.c index 4b22ecfe65..8e97086601 100644 --- a/xen/drivers/passthrough/iommu.c +++ b/xen/drivers/passthrough/iommu.c @@ -64,6 +64,7 @@ bool __read_mostly iommu_hap_pt_share =3D true; bool __read_mostly iommu_debug; =20 DEFINE_PER_CPU(bool, iommu_dont_flush_iotlb); +DEFINE_PER_CPU(struct iommu_skipped_flush, iommu_skipped_flush); =20 static int __init cf_check parse_iommu_param(const char *s) { @@ -390,7 +391,7 @@ int iommu_legacy_map(struct domain *d, dfn_t dfn, mfn_t= mfn, ASSERT(!(flags & IOMMUF_preempt)); rc =3D iommu_map(d, dfn, mfn, page_count, flags, &flush_flags); =20 - if ( !this_cpu(iommu_dont_flush_iotlb) && !rc ) + if ( !rc ) rc =3D iommu_iotlb_flush(d, dfn, page_count, flush_flags); =20 return rc; @@ -458,7 +459,7 @@ int iommu_legacy_unmap(struct domain *d, dfn_t dfn, uns= igned long page_count) unsigned int flush_flags =3D 0; int rc =3D iommu_unmap(d, dfn, page_count, 0, &flush_flags); =20 - if ( !this_cpu(iommu_dont_flush_iotlb) && !rc ) + if ( !rc ) rc =3D iommu_iotlb_flush(d, dfn, page_count, flush_flags); =20 return rc; @@ -488,6 +489,18 @@ int iommu_iotlb_flush(struct domain *d, dfn_t dfn, uns= igned long page_count, if ( dfn_eq(dfn, INVALID_DFN) ) return -EINVAL; =20 + if ( this_cpu(iommu_dont_flush_iotlb) ) + { + struct iommu_skipped_flush *skipped =3D &this_cpu(iommu_skipped_fl= ush); + + skipped->first =3D _dfn(min(dfn_x(skipped->first), dfn_x(dfn))); + skipped->last =3D _dfn(max(dfn_x(skipped->last), + dfn_x(dfn) + page_count - 1)); + skipped->flags |=3D flush_flags; + + return 0; + } + rc =3D iommu_call(hd->platform_ops, iotlb_flush, d, dfn, page_count, flush_flags); if ( unlikely(rc) ) diff --git a/xen/include/xen/iommu.h b/xen/include/xen/iommu.h index 37c4a1dc82..a2aeaf8d41 100644 --- a/xen/include/xen/iommu.h +++ b/xen/include/xen/iommu.h @@ -502,6 +502,12 @@ void iommu_dev_iotlb_flush_timeout(struct domain *d, s= truct pci_dev *pdev); */ DECLARE_PER_CPU(bool, iommu_dont_flush_iotlb); =20 +struct iommu_skipped_flush { + dfn_t first, last; + unsigned int flags; +}; +DECLARE_PER_CPU(struct iommu_skipped_flush, iommu_skipped_flush); + bool arch_iommu_use_permitted(const struct domain *d); =20 #ifdef CONFIG_X86 --=20 2.47.3 From nobody Mon Nov 23 22:31:33 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; 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=quarantine dis=none) header.from=edera.dev ARC-Seal: i=1; a=rsa-sha256; t=1793952585; cv=none; d=zohomail.com; s=zohoarc; b=hQkyam6HTk85QcO8CedUUcmqf+iU2KTGdHWhAbfHTp+ZXms2OfCCak/pajnYDnapGM29ruJvEOi0w3ZBfyDpZGCRh1ssn4i9Zx0Lcjef8623K1C1xxx4IwKT2FYfTDK1yKQ3aeZS0f2n0aztUgZl32JSeELrCG4gxK7cv4/uZbI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1793952585; h=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:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=QoFzDdd2lf6UxmsRhpAJjlVL9PKwQJXX6SC5k0LavRI=; b=K9YX8rD27/Ih5lkpxhOhwtFkvXtoNZKDeUfFVM8PVHPQBiL68v7R2GyphpDDPSlS61doHP6E/hyp2kCGJ0G6/H+iJPTD4pudIgSIsKA7eYJAp2gWfuf1AKVib29PPmPJtlh6HqPEjGqEX4a6byvTPEVts/dMF4XvTHjAAPdcsi0= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; 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=quarantine dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 179395258511829.124749797158756; Fri, 6 Nov 2026 00:09:45 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.1476315.1682478 (Exim 4.92) (envelope-from ) id 1xPF0m-0007sF-4l; Fri, 06 Nov 2026 08:09:24 +0000 Received: by outflank-mailman (output) from mailman id 1476315.1682478; Fri, 06 Nov 2026 08:09: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 1xPF0m-0007s8-0b; Fri, 06 Nov 2026 08:09:24 +0000 Received: by outflank-mailman (input) for mailman id 1476315; Fri, 06 Nov 2026 08:09:22 +0000 Received: from mx.expurgate.net ([194.145.224.10]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xPF0k-0007pV-1x for xen-devel@lists.xenproject.org; Fri, 06 Nov 2026 08:09:22 +0000 Received: from mx.expurgate.net (helo=localhost) by mx.expurgate.net with esmtp id 1xPF0j-005S4K-Eq for xen-devel@lists.xenproject.org; Fri, 06 Nov 2026 09:09:21 +0100 Received: from [10.42.69.10] (helo=localhost) by localhost with ESMTP (eXpurgate MTA 0.9.1) (envelope-from ) id 6aed8b2e-bab6-0a2a0a5309dd-0a2a450a95a4-16 for ; Fri, 06 Nov 2026 09:09:21 +0100 Received: from [209.85.128.43] (helo=mail-wm1-f43.google.com) by tlsNG-4011c0.mxtls.expurgate.net with ESMTPS (eXpurgate 4.57.1) (envelope-from ) id 6aed8b31-f2d2-0a2a450a0019-d155802bddc5-3 for ; Fri, 06 Nov 2026 09:09:21 +0100 Received: by mail-wm1-f43.google.com with SMTP id 5b1f17b1804b1-4b4571ff080so3659665e9.1 for ; Fri, 06 Nov 2026 00:09:21 -0800 (PST) Received: from framework13.home ([2a01:e11:3:1ff0:831:b00b:e409:f875]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-492ff85585esm1534507f8f.5.2026.11.06.00.09.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 06 Nov 2026 00:09:20 -0800 (PST) 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=google header.d=edera.dev header.i="@edera.dev" header.h="Content-Transfer-Encoding:MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=edera.dev; s=google; t=1793952561; x=1794557361; darn=lists.xenproject.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=QoFzDdd2lf6UxmsRhpAJjlVL9PKwQJXX6SC5k0LavRI=; b=k+0SzDoSEuOQWH7eqtjicU4suJwSA9VdEG27Sjpr15j/YCv2h1xY3nGTYlEv8P2s/J C+VpFAWXBjVdhuP59M9i+/mvi1SPkshqnOKHnaWBzgHfqzlIjcaDbbpDN5JSfrCX1sm8 gJTdS6j76WwZ2tJdmIX8086vIoClWRIV3IUsdfMItDbOyWaarBDzdNCeyehlL+fQMY9E zA7RP7Pf5MrDNyP74yZKWeDKiDQ2cBWib8GZA7EjHCJRpq7+bGaWKtgTs03LFuxViG1x 6KldP9cyt8uZ5fzEnjWFlWEVJfamMjbpk9xWOfDMCPrvnwSdPYf40emz5O2qZl5Z0Tow 0R5g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1793952561; x=1794557361; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=QoFzDdd2lf6UxmsRhpAJjlVL9PKwQJXX6SC5k0LavRI=; b=ztsQNXbuRI3O51IX2D2cNzckFIbe7vrh//I2XnJTolTv7sbWvjw8+ZHXFPlPgAHLxt 4YRU3h1VK/0rhV7N0IN9HpiJ+0mma5DWJZHg3GMIvrFB10ahYNz0rMvE5stj6Zfdd4VL TydTPHHA/6o8ulzNOcCYsHPfaMJWRnA3nsRMYobczirV0ulmu5ScbE+6OEZD9Klo7ikM R+PCkldq51QBZLXUyt0TU0vpWjiGfh6K5EGiQfOoFEPUi7ZHE7KL6bhyt074+UxaIpAw F9UQcM44LqzmCg5+BLV96kLvOpiVXYCsDmAWny3SJ1FoIvyIUoPip6zlVCL1JSmr1XQg x0cg== X-Gm-Message-State: APuU4Z9mWDiPTJl+oQrD28yutPhxf3mfcsm5MbVtc9c00v/vTSMTD36L c7gMv3B4WKm0O9r8xycJ71WbQOtUEshW04hXg/53MzQvVfaSsjVzSuiBdC5kjw+cb9B0RcTv2UM IP1Pfmxc= X-Gm-Gg: AYBFou0FZlCcUTZ7lZqC2qegMtrON1VUFpOBYRe78VxVpuQqJ3Expac8ifEGPeICcy2 jf0kLwpV4XOaWS8qbUZR0UKCWRDlarkGjbgwtPfUin+NeaLT+YKtuG5MJxa63Sxc38v6AAyjMhU TH/WZVDrf4Ui5XI0W2wYARMpB+MZjc3bnqY5HSOGCJOV5PXZD9Kqam0dD8JZVZSRyZ/gDI4RW+P mrko6vDV8zEDNtL9URK+6UTRg2EdjbSCK/6cSI0vfel0fV3lDxg7can1L5h0ypdsqxGY94AA0Ia bDfW+A53j7yhH/DhWCMJi//pUAVpm3U6l6A43B+iqP87Z91vpYH1IElhYZtTH5pi3OXdiRCn5bO qaz/B/lAoTbMCrSMCGofHh1RhqqzynY857WzbzLDAskQswmcdKVtZI0c7o8F2ziiF59fXewBubC xCHjzEdZNsfXUqAsS0TlRnCvnAU6r7rrKuQvCHB0+D9AKc4HrYPp0NloeMRosuIwgx5w+vYJeJ5 1lKOLKXWFY4jfvTy3ZsKGwLqx4F1uHH2f/TKmEy9HWLmjN6ykNneHm8JBrQHqYlG634enuubCgL 2ecFE1WtkH1Dg7ZMzTk48SmJpg== X-Received: by 2002:a05:6000:2488:b0:48e:2448:ea23 with SMTP id ffacd0b85a97d-4927807f0a8mr3734805f8f.55.1793952560685; Fri, 06 Nov 2026 00:09:20 -0800 (PST) From: Luca Di Maio To: xen-devel@lists.xenproject.org Cc: Luca Di Maio , Jan Beulich , Andrew Cooper , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Teddy Astie , Stefano Stabellini , Julien Grall , Bertrand Marquis , Michal Orzel , Volodymyr Babchuk , Anthony PERARD Subject: [PATCH v2 3/4] x86/HVM: defer IOTLB flushes across a grant unmap batch Date: Fri, 6 Nov 2026 09:08:53 +0100 Message-ID: <20261106080859.2285309-4-luca@edera.dev> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20261106080859.2285309-1-luca@edera.dev> References: <20261106080859.2285309-1-luca@edera.dev> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-purgate-ID: tlsNG-4011c0/1793952561-59DDFCFC-56EE003E/0/0 X-purgate-type: clean X-purgate-size: 8732 X-ZohoMail-DKIM: pass (identity @edera.dev) X-ZM-MESSAGEID: 1793952587146158502 Content-Type: text/plain; charset="utf-8" gnttab_unmap_grant_ref() flushes the TLBs of a PV caller once per group of GNTTAB_UNMAP_BATCH_SIZE unmaps. A translated caller instead gets an IOTLB flush per page from the p2m code, so netback in a PVH dom0 issues one for every page a domU transmits. Open a batch for translated callers before each group, and end it next to gnttab_flush_tlb(), so before unmap_common_complete() releases the pages. Ending the batch issues one flush covering the flushes the unmaps skipped, as iommu_skipped_flush recorded them. The batch lives in the vCPU, and iommu_dont_flush_iotlb isn't held across the guest copies between the unmaps, as those can sleep on a vm_event ring and change the p2m, e.g. by unsharing a page. If the flush fails, the pages may still be reachable through the IOMMU, so the group isn't completed and its pages leak. PV callers, Arm, and other callers of replace_grant_host_mapping() are unchanged. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Luca Di Maio --- Changes in v2: - Flush the range recorded in iommu_skipped_flush, rather than one derived from the unmapped gfns (Teddy). - Leave rounding the range to an aligned power of two to VT-d, in patch 1 (Teddy). --- xen/arch/arm/include/asm/grant_table.h | 3 ++ xen/arch/x86/hvm/grant_table.c | 49 +++++++++++++++++++++- xen/arch/x86/include/asm/grant_table.h | 13 ++++++ xen/arch/x86/include/asm/hvm/grant_table.h | 5 +++ xen/arch/x86/include/asm/hvm/vcpu.h | 9 ++++ xen/common/grant_table.c | 6 +++ 6 files changed, 84 insertions(+), 1 deletion(-) diff --git a/xen/arch/arm/include/asm/grant_table.h b/xen/arch/arm/include/= asm/grant_table.h index 7661a95341..66f90083db 100644 --- a/xen/arch/arm/include/asm/grant_table.h +++ b/xen/arch/arm/include/asm/grant_table.h @@ -40,6 +40,9 @@ int create_grant_host_mapping(uint64_t gpaddr, mfn_t fram= e, int replace_grant_host_mapping(uint64_t gpaddr, mfn_t frame, uint64_t new_gpaddr, unsigned int flags); =20 +static inline void gnttab_unmap_batch_begin(void) {} +static inline int gnttab_unmap_batch_end(void) { return 0; } + /* * The region used by Xen on the memory will never be mapped in DOM0 * memory layout. Therefore it can be used for the grant table. diff --git a/xen/arch/x86/hvm/grant_table.c b/xen/arch/x86/hvm/grant_table.c index afe449d888..5ecba6db65 100644 --- a/xen/arch/x86/hvm/grant_table.c +++ b/xen/arch/x86/hvm/grant_table.c @@ -41,6 +41,8 @@ int replace_grant_p2m_mapping(uint64_t addr, mfn_t frame, p2m_type_t type; mfn_t old_mfn; struct domain *d =3D current->domain; + struct hvm_grant_unmap_batch *batch =3D ¤t->arch.hvm.grant_unmap= _batch; + int rc; =20 if ( new_addr !=3D 0 || (flags & GNTMAP_contains_pte) ) return GNTST_general_error; @@ -54,7 +56,29 @@ int replace_grant_p2m_mapping(uint64_t addr, mfn_t frame, type, mfn_x(old_mfn), mfn_x(frame)); return GNTST_general_error; } - if ( p2m_remove_page(d, _gfn(gfn), frame, PAGE_ORDER_4K) ) + + /* Not across the guest copies, which can change the p2m or sleep. */ + if ( batch->open ) + { + this_cpu(iommu_skipped_flush) =3D + (struct iommu_skipped_flush){ .first =3D INVALID_DFN }; + this_cpu(iommu_dont_flush_iotlb) =3D true; + } + + rc =3D p2m_remove_page(d, _gfn(gfn), frame, PAGE_ORDER_4K); + + if ( batch->open ) + { + const struct iommu_skipped_flush *skipped =3D + &this_cpu(iommu_skipped_flush); + + this_cpu(iommu_dont_flush_iotlb) =3D false; + batch->iommu_flags |=3D skipped->flags; + batch->dfn_first =3D min(batch->dfn_first, dfn_x(skipped->first)); + batch->dfn_last =3D max(batch->dfn_last, dfn_x(skipped->last)); + } + + if ( rc ) { put_gfn(d, gfn); return GNTST_general_error; @@ -64,6 +88,29 @@ int replace_grant_p2m_mapping(uint64_t addr, mfn_t frame, return GNTST_okay; } =20 +void grant_p2m_unmap_batch_begin(void) +{ + struct hvm_grant_unmap_batch *batch =3D ¤t->arch.hvm.grant_unmap= _batch; + + ASSERT(!batch->open); + *batch =3D (struct hvm_grant_unmap_batch){ .open =3D true, .dfn_first = =3D ~0UL }; +} + +int grant_p2m_unmap_batch_end(void) +{ + struct domain *d =3D current->domain; + struct hvm_grant_unmap_batch *batch =3D ¤t->arch.hvm.grant_unmap= _batch; + + batch->open =3D false; + + if ( !batch->iommu_flags ) + return 0; + + return iommu_iotlb_flush(d, _dfn(batch->dfn_first), + batch->dfn_last - batch->dfn_first + 1, + batch->iommu_flags); +} + /* * Local variables: * mode: C diff --git a/xen/arch/x86/include/asm/grant_table.h b/xen/arch/x86/include/= asm/grant_table.h index e939c9bacb..3aae47092f 100644 --- a/xen/arch/x86/include/asm/grant_table.h +++ b/xen/arch/x86/include/asm/grant_table.h @@ -32,6 +32,19 @@ static inline int replace_grant_host_mapping(uint64_t ad= dr, mfn_t frame, return replace_grant_pv_mapping(addr, frame, new_addr, flags); } =20 +static inline void gnttab_unmap_batch_begin(void) +{ + if ( paging_mode_external(current->domain) ) + grant_p2m_unmap_batch_begin(); +} + +static inline int __must_check gnttab_unmap_batch_end(void) +{ + if ( paging_mode_external(current->domain) ) + return grant_p2m_unmap_batch_end(); + return 0; +} + #define gnttab_get_frame_gfn(gt, st, idx) ({ \ mfn_t mfn_ =3D (st) ? gnttab_status_mfn(gt, idx) = \ : gnttab_shared_mfn(gt, idx); \ diff --git a/xen/arch/x86/include/asm/hvm/grant_table.h b/xen/arch/x86/incl= ude/asm/hvm/grant_table.h index 01e23f79b8..9877262f51 100644 --- a/xen/arch/x86/include/asm/hvm/grant_table.h +++ b/xen/arch/x86/include/asm/hvm/grant_table.h @@ -19,6 +19,8 @@ int create_grant_p2m_mapping(uint64_t addr, mfn_t frame, unsigned int cache_flags); int replace_grant_p2m_mapping(uint64_t addr, mfn_t frame, uint64_t new_addr, unsigned int flags); +void grant_p2m_unmap_batch_begin(void); +int grant_p2m_unmap_batch_end(void); =20 #else =20 @@ -37,6 +39,9 @@ static inline int replace_grant_p2m_mapping(uint64_t addr= , mfn_t frame, return GNTST_general_error; } =20 +static inline void grant_p2m_unmap_batch_begin(void) {} +static inline int grant_p2m_unmap_batch_end(void) { return 0; } + #endif =20 #endif /* __X86_HVM_GRANT_TABLE_H__ */ diff --git a/xen/arch/x86/include/asm/hvm/vcpu.h b/xen/arch/x86/include/asm= /hvm/vcpu.h index 876c230784..990411e2c7 100644 --- a/xen/arch/x86/include/asm/hvm/vcpu.h +++ b/xen/arch/x86/include/asm/hvm/vcpu.h @@ -108,6 +108,13 @@ struct altp2mvcpu { =20 #define vcpu_altp2m(v) ((v)->arch.hvm.avcpu) =20 +/* Per vCPU, as the guest copies between the unmaps can sleep. */ +struct hvm_grant_unmap_batch { + bool open; + unsigned int iommu_flags; + unsigned long dfn_first, dfn_last; +}; + struct hvm_vcpu { /* Guest control-register and EFER values, just as the guest sees them= . */ unsigned long guest_cr[5]; @@ -172,6 +179,8 @@ struct hvm_vcpu { /* Pending hw/sw interrupt (.vector =3D -1 means nothing pending). */ struct x86_event inject_event; =20 + struct hvm_grant_unmap_batch grant_unmap_batch; + #ifdef CONFIG_VIRIDIAN struct viridian_vcpu *viridian; #endif diff --git a/xen/common/grant_table.c b/xen/common/grant_table.c index b15805f3f9..ade42b4cb4 100644 --- a/xen/common/grant_table.c +++ b/xen/common/grant_table.c @@ -1722,6 +1722,7 @@ gnttab_unmap_grant_ref( c =3D min(count, (unsigned int)GNTTAB_UNMAP_BATCH_SIZE); partial_done =3D 0; =20 + gnttab_unmap_batch_begin(); for ( i =3D 0; i < c; i++ ) { if ( unlikely(__copy_from_guest(&op, uop, 1)) ) @@ -1734,6 +1735,9 @@ gnttab_unmap_grant_ref( } =20 gnttab_flush_tlb(current->domain); + /* After a failed flush the pages may still be reachable: leak the= m. */ + if ( gnttab_unmap_batch_end() ) + partial_done =3D 0; =20 for ( i =3D 0; i < partial_done; i++ ) unmap_common_complete(&common[i]); @@ -1749,6 +1753,8 @@ gnttab_unmap_grant_ref( =20 fault: gnttab_flush_tlb(current->domain); + if ( gnttab_unmap_batch_end() ) + partial_done =3D 0; =20 for ( i =3D 0; i < partial_done; i++ ) unmap_common_complete(&common[i]); --=20 2.47.3 From nobody Mon Nov 23 22:31:33 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; 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=quarantine dis=none) header.from=edera.dev ARC-Seal: i=1; a=rsa-sha256; t=1793952579; cv=none; d=zohomail.com; s=zohoarc; b=IVzedc0nnuJEXPgbqrooX6mRJNTrJPBamy6MchVus/2i/lssUb+Cxdh6xAp+YPcoNNjNQtnfyPoIKEO8qZx/62QRm6Bdf3j0WdDBAgFkrzaIdsjkHuwEC1960Hr0rtewmR+Y8hCfGwkHV2baIjIvpQRuspf8uDFmUVzFKh7EDBU= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1793952579; h=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:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=wt1ABLaNm0GH4SvP3FkeGPhSrwsLp9qxqWi2r0AAmsc=; b=dYzTHhAZ8E63z6ujBffmEBXyj+JCquXHv6BR6RvUBKXWELe9neBmn/3EXULtHGvQO5vuIOyuxMKSp53d5kkq6+ljyvyA5TNPUloKNvo5x7+es4E3z3OufXTw4UQhkdCgDOwErx9VyO7rRwPRUsVHQospXKIgKecBsHvoY/5BXtU= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; 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=quarantine dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1793952579852876.0052812557074; Fri, 6 Nov 2026 00:09:39 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.1476317.1682486 (Exim 4.92) (envelope-from ) id 1xPF0n-00086R-Az; Fri, 06 Nov 2026 08:09:25 +0000 Received: by outflank-mailman (output) from mailman id 1476317.1682486; Fri, 06 Nov 2026 08:09:25 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xPF0n-00086K-8I; Fri, 06 Nov 2026 08:09:25 +0000 Received: by outflank-mailman (input) for mailman id 1476317; Fri, 06 Nov 2026 08:09:24 +0000 Received: from mx.expurgate.net ([194.145.224.10]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xPF0m-0007ux-Gg for xen-devel@lists.xenproject.org; Fri, 06 Nov 2026 08:09:24 +0000 Received: from mx.expurgate.net (helo=localhost) by mx.expurgate.net with esmtp id 1xPF0l-00GWrl-TX for xen-devel@lists.xenproject.org; Fri, 06 Nov 2026 09:09:23 +0100 Received: from [10.42.69.8] (helo=localhost) by localhost with ESMTP (eXpurgate MTA 0.9.1) (envelope-from ) id 6aed8b2a-2eae-0a2a0a5409dd-0a2a450891e4-24 for ; Fri, 06 Nov 2026 09:09:23 +0100 Received: from [209.85.221.53] (helo=mail-wr1-f53.google.com) by tlsNG-c1860d.mxtls.expurgate.net with ESMTPS (eXpurgate 4.57.1) (envelope-from ) id 6aed8b33-f659-0a2a45080019-d155dd35c870-3 for ; Fri, 06 Nov 2026 09:09:23 +0100 Received: by mail-wr1-f53.google.com with SMTP id ffacd0b85a97d-490c3ae81d3so176779f8f.2 for ; Fri, 06 Nov 2026 00:09:23 -0800 (PST) Received: from framework13.home ([2a01:e11:3:1ff0:831:b00b:e409:f875]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-492ff85585esm1534507f8f.5.2026.11.06.00.09.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 06 Nov 2026 00:09:22 -0800 (PST) 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=google header.d=edera.dev header.i="@edera.dev" header.h="Content-Transfer-Encoding:MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=edera.dev; s=google; t=1793952563; x=1794557363; darn=lists.xenproject.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=wt1ABLaNm0GH4SvP3FkeGPhSrwsLp9qxqWi2r0AAmsc=; b=j0tn5hGKaEwOO1LrIgd5jb6K95JyZ4Y8esW5lxTxtXxlQKhcHVE1ZRIeqTtRHrsrk5 wEIuYGQO4L77weSWrhc+RiOVgR/27sWAKU3AvYpDRqoguGt0u7eVYSAcuIOCzYSUe5eF YX61Lkko65DgSEjar2WIhjsSueP1ICRyUi0/eN1QL2TAgy/zt5O8QIKqdsBYDiokJ0dY FsGS1xvbjGT9fhm8afLcsuZ/jGn3IB8t/Lpv8TnEqsW75uDM6Xxo1xpBCP3aIavpr4YZ M1NE6sdXVonhaXKO0lT7cbkfo8AG3qGgrrx6dM5vgufn6g+XrTGZagotAEGJvxQUEBMB e3rw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1793952563; x=1794557363; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=wt1ABLaNm0GH4SvP3FkeGPhSrwsLp9qxqWi2r0AAmsc=; b=SxSN12f5QdxeXgJwy062AzM+5fVInjMGgxIrXbu8PQLL2PqdL+Ajn8RChbgmK3zGTC YgYIIxblNZYvU2vctR9FqUAwL+wxJx404RVLChRT5/lVEXrUYaTnEWePPzG0PK78Bn74 8yDfVoQoxZDV/lg5nZW6aMXix/xUfxL/XjOOZU4Coz9nUEgWn8/kOthJwDW/cE5vWDjN CABYPIE/XmHCxj7h2gOwkR7Y2d2HPUiTmwpiCDpDKDBCQ82vR8pwNulVY0YgN2hFaWmF bsqJGmGBzv1kjNcRHNKkPtIllwRQJuzhz+Xs/5H7J2U+2hpVcSoyA9ZyzC+pMWTDvpYj cHQA== X-Gm-Message-State: APuU4Z93sq1uw3wtxXzqoU/uASKDIwOUm7KYcotzVWsWJfV9+SM9PtqO 0nXYt7XerVtzM9Iv5CT6FKzBSQBHzESsDB51lVtmBf1PiqGl4Fe9s0nCfeFAw/qObVjLGB82p9Q 1umleHrw= X-Gm-Gg: AYBFou3kuXtUKRy9JCBaYbseEKOKBkLoTUzyH9J/JGyQeaS5xG+MIPheCO9WZuwHu4Z pW7eLytGKS82k/xhnDzmdAK4iLwwMuGpyMbum9UNihRDdjJOsUcOrpiXv5Ffy6D5xaqIqkwuCd5 YHgLoxUJu9xwdLung5+u3VGc+VDPwnmPkWqnEspOW2Ma4D9lRml1gyvOjVg5zANXx/GQ3cIL4Lf KVVXHB0Xv2/i0n/VPIAVbS2y7YAt/vpm20h1QXuwFkuXOWvSga8Y6JH+KR07I7gbIMHSO8gdEmA wzEyl2MtugMHE5VCTcaMjqBY+/TVPpg0G16U+g8UyZWL+db+Ilqd1iV05Wx/Hehyd8HaWkGHhR3 JBAC9Pq/CN/N+7+AnmviM3tJPMSr0iuoc/xF/FPOZrU1LnWD8hUI1n6E3w0+TFQjuBwRoXNa0pX UH3mhuPXXcy5NvFnPYna1fZNK35gYBQ70SCrtHwQKUDZF2HaLjh9Cr3qWsEYlssbemK0xvKMyTM vsG1VtLuk2YHcRSHtmyrgnCUwaLAs9Ij7t9QgLsW5sFeofnsBQSTi/qlpnzfaMUDeECNR88pyjn D8o4KK6p2lzrR6f/wRzsQi894Q== X-Received: by 2002:a05:6000:2887:b0:48e:c0:9441 with SMTP id ffacd0b85a97d-49275372225mr3961197f8f.29.1793952563263; Fri, 06 Nov 2026 00:09:23 -0800 (PST) From: Luca Di Maio To: xen-devel@lists.xenproject.org Cc: Luca Di Maio , Jan Beulich , Andrew Cooper , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Teddy Astie , Stefano Stabellini , Julien Grall , Bertrand Marquis , Michal Orzel , Volodymyr Babchuk , Anthony PERARD Subject: [PATCH v2 4/4] x86/HVM: defer p2m TLB flushes across a grant unmap batch Date: Fri, 6 Nov 2026 09:08:54 +0100 Message-ID: <20261106080859.2285309-5-luca@edera.dev> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20261106080859.2285309-1-luca@edera.dev> References: <20261106080859.2285309-1-luca@edera.dev> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-purgate-ID: tlsNG-c1860d/1793952563-D5D4887B-94D9F5C0/0/0 X-purgate-type: clean X-purgate-size: 3042 X-ZohoMail-DKIM: pass (identity @edera.dev) X-ZM-MESSAGEID: 1793952581020158500 Content-Type: text/plain; charset="utf-8" A grant unmap by a translated caller also flushes the guest TLBs: p2m_remove_page() sets need_flush of the host p2m, and put_gfn() flushes when it drops the last level of the p2m lock. Each flush sends an IPI to every other CPU in d->dirty_cpumask and waits for all of them, so a PVH dom0 with busy vCPUs pays a round of IPIs for every page it unmaps. Defer that flush to the end of the batch, still before the pages are released. Under the gfn lock of get_gfn_query() only the removal can set need_flush, as the query neither allocates nor unshares, so no other change's flush is deferred. p2m_free_ptp() still flushes before it frees a table. The final flush reaches the CPUs in d->dirty_cpumask at that time. A CPU that left the mask in between has flushed already, as __context_switch() clears it from the mask only after write_ptbase() has flushed the guest TLBs. With EPT, each change also marks every CPU for an INVEPT before its next VM entry. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Luca Di Maio --- Changes in v2: - None. --- xen/arch/x86/hvm/grant_table.c | 6 ++++++ xen/arch/x86/include/asm/hvm/vcpu.h | 1 + 2 files changed, 7 insertions(+) diff --git a/xen/arch/x86/hvm/grant_table.c b/xen/arch/x86/hvm/grant_table.c index 5ecba6db65..7a2201d926 100644 --- a/xen/arch/x86/hvm/grant_table.c +++ b/xen/arch/x86/hvm/grant_table.c @@ -41,6 +41,7 @@ int replace_grant_p2m_mapping(uint64_t addr, mfn_t frame, p2m_type_t type; mfn_t old_mfn; struct domain *d =3D current->domain; + struct p2m_domain *p2m =3D p2m_get_hostp2m(d); struct hvm_grant_unmap_batch *batch =3D ¤t->arch.hvm.grant_unmap= _batch; int rc; =20 @@ -76,6 +77,8 @@ int replace_grant_p2m_mapping(uint64_t addr, mfn_t frame, batch->iommu_flags |=3D skipped->flags; batch->dfn_first =3D min(batch->dfn_first, dfn_x(skipped->first)); batch->dfn_last =3D max(batch->dfn_last, dfn_x(skipped->last)); + batch->tlb_flush |=3D p2m->need_flush; + p2m->need_flush =3D false; } =20 if ( rc ) @@ -99,9 +102,12 @@ void grant_p2m_unmap_batch_begin(void) int grant_p2m_unmap_batch_end(void) { struct domain *d =3D current->domain; + struct p2m_domain *p2m =3D p2m_get_hostp2m(d); struct hvm_grant_unmap_batch *batch =3D ¤t->arch.hvm.grant_unmap= _batch; =20 batch->open =3D false; + if ( batch->tlb_flush ) + p2m->tlb_flush(p2m); =20 if ( !batch->iommu_flags ) return 0; diff --git a/xen/arch/x86/include/asm/hvm/vcpu.h b/xen/arch/x86/include/asm= /hvm/vcpu.h index 990411e2c7..ff6856928d 100644 --- a/xen/arch/x86/include/asm/hvm/vcpu.h +++ b/xen/arch/x86/include/asm/hvm/vcpu.h @@ -111,6 +111,7 @@ struct altp2mvcpu { /* Per vCPU, as the guest copies between the unmaps can sleep. */ struct hvm_grant_unmap_batch { bool open; + bool tlb_flush; unsigned int iommu_flags; unsigned long dfn_first, dfn_last; }; --=20 2.47.3