From nobody Mon Nov 23 04:44:10 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=reject dis=none) header.from=linux.ibm.com ARC-Seal: i=1; a=rsa-sha256; t=1792424295; cv=none; d=zohomail.com; s=zohoarc; b=Vn5Z1mBcpwxMF8ZEBfnFP2/DdZINwXtLSOZQKNFjr96J81r53cnnSD301sm6DiO8iO2yXLUmjLFb/7ZmnStdH1p+cq7/KxSizDZ/q8raBanYisBRjglkL0jCGx5Xng8+wbnYF91RoqBdoA+tap5bmbeRQr8LirwHepEY6gw/ZrQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1792424295; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=3+XGRcJe/oeVl43Gv1FbUgMkahmod8g5y8FaH/hNQPk=; b=G6cpxUV5/LCLgMr4kknlAlxnR8XlZlAJxzfzVaUyLsQu140z+J7AxBn7tKkL9hTucHkqRqtl+EGyRoWNmhcVzuabnsaLZhZ2C7PxDMJ9qNVNc15U2DqxsNv4fHprvEi8EaILTZQ22+887U2nkrKUeX+RvTtbY7qe7/VG8DybbgE= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=reject dis=none) Return-Path: Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1792424295021583.594141909222; Mon, 19 Oct 2026 08:38:15 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xIpR1-00059S-HO; Mon, 19 Oct 2026 11:37:59 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xIpQw-000593-8v; Mon, 19 Oct 2026 11:37:54 -0400 Received: from mx0a-001b2d01.pphosted.com ([148.163.156.1]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xIpQs-0004uV-PR; Mon, 19 Oct 2026 11:37:53 -0400 Received: from pps.filterd (m0356517.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.18.1.11/8.18.1.11) with ESMTP id 69JEElEE4015180; Mon, 19 Oct 2026 15:37:47 GMT Received: from ppma22.wdc07v.mail.ibm.com (5c.69.3da9.ip4.static.sl-reverse.com [169.61.105.92]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 4hc0r7m340-1 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Mon, 19 Oct 2026 15:37:47 +0000 (GMT) Received: from pps.filterd (ppma22.wdc07v.mail.ibm.com [127.0.0.1]) by ppma22.wdc07v.mail.ibm.com (8.18.1.11/8.18.1.11) with ESMTP id 69JEJoRB020234; Mon, 19 Oct 2026 15:37:46 GMT Received: from smtprelay02.wdc07v.mail.ibm.com ([172.16.1.69]) by ppma22.wdc07v.mail.ibm.com (PPS) with ESMTPS id 4hckqvee40-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Mon, 19 Oct 2026 15:37:46 +0000 (GMT) Received: from smtpav01.wdc07v.mail.ibm.com (smtpav01.wdc07v.mail.ibm.com [10.39.53.228]) by smtprelay02.wdc07v.mail.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 69JFbhBj21627404 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Mon, 19 Oct 2026 15:37:44 GMT Received: from smtpav01.wdc07v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id DA04A58055; Mon, 19 Oct 2026 15:37:43 +0000 (GMT) Received: from smtpav01.wdc07v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id CED8B58065; Mon, 19 Oct 2026 15:37:42 +0000 (GMT) Received: from Mac.ibm.com (unknown [9.61.242.107]) by smtpav01.wdc07v.mail.ibm.com (Postfix) with ESMTP; Mon, 19 Oct 2026 15:37:42 +0000 (GMT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ibm.com; h=cc :content-transfer-encoding:date:from:message-id:mime-version :subject:to; s=pp1; bh=3+XGRcJe/oeVl43Gv1FbUgMkahmod8g5y8FaH/hNQ Pk=; b=L4AgJ4PP47AkFaVwSfZfQqO6H0rGBKRiDISyiXbMMrNmXqanmuI/KvYKP IkjYOzJwOJgKtG3t+EcEAJWH0uoGRroH8m++oOlzvfUyLuhi3rIE2jMB697dNxBp gm+h2PRmLFcprIPdMnBiRmqdIa/z6o5D8Zsz/ozNYhouMejJQocL81MM2SnNOYf1 NppZ6wRJzS990moEjWyCVnl2yANRr/uYYHXCfuYkcnklRHK3dim//enaB0JzcUiE ZfqQwjEefCMbEeZPRqpUnrzE/2DPjS92fs3sJ8ZsPn3mdBCrz321HNLYJvkfs0Je 9zjbuSHdrKH1o20F10d+l+WUb+JNw== From: Omar Elghoul To: qemu-devel@nongnu.org, qemu-s390x@nongnu.org Cc: mjrosato@linux.ibm.com, alifm@linux.ibm.com, cam@linux.ibm.com, farman@linux.ibm.com, qemu-stable@nongnu.org Subject: [PATCH v2] s390x/pci: re-fetch DMA slot shadow count from VFIO upon IOMMU disable Date: Mon, 19 Oct 2026 11:37:29 -0400 Message-ID: <20261019153729.40823-1-oelghoul@linux.ibm.com> X-Mailer: git-send-email 2.56.0 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-TM-AS-GCONF: 00 X-Proofpoint-Spam-Info: AW1haW4tMjYxMDE5MDA2MCBTYWx0ZWRfX4FRP9e6Af9C/ hQh3AvaXfwQf0wEjc//u3zw21jHMPbxK0INFDTam8Is5fSHKgY2tRG5AoNtVSu1GiNJCFx9oj9M 0x9ahILQCX6Hjk/NBSq+1uwCSeGaDYo= X-Authority-Analysis: v=2.4 cv=PrUG/AM3 c=1 sm=1 tr=0 ts=6ad6394b cx=c_pps a=5BHTudwdYE3Te8bg5FgnPg==:117 a=5BHTudwdYE3Te8bg5FgnPg==:17 a=660iZSQnnn4A:10 a=VkNPw1HP01LnGYTKEx00:22 a=RnoormkPH1_aCDwRdu11:22 a=U7nrCbtTmkRpXpFmAIza:22 a=69wJf7TsAAAA:8 a=VnNF1IyMAAAA:8 a=DP0TDp8FZMFJ59EArGYA:9 a=Fg1AiH1G6rFz08G2ETeA:22 X-Proofpoint-Spam-Details-Enc: AW1haW4tMjYxMDE5MDA2MCBTYWx0ZWRfX/06ovu9SEHKc EPSQA06rkt+CXc2+h2kaO41cVVB6dfJbK/saX4+ZGzeWhxH0viAHwjoJ593rtsguGWb8oM4js3O dxysOOI+9q33HSGCYl7RKhOgcq++AHx84UBrZFtb+P/zr3uam4fAx3d4pXCgS/3azoi1m3sB6cI HzU/WlTgX36VgpdYr6LafjKkPr/qjp0jS5PZHhXDeADkh6SYCFPeYcYUSUA54ONzPU0Rvow2zOW NpBBU2px/jE3n6AUHFkDllJzq7OBppy/OhNX6TtAeUjM0wUq13saN5Q1AOrKHgUhOc0yFN6FC5p qumH5oMVgqUo2C4c2WErjdNvjoYM8f/nsqNomwRvYtYrxtLzS6I2bv2piSmZXstg5Sp8yE6Dq1P MZquyV+evmFcuxnMG2CYLGiqcSCOWXxEdrn7EnwWQu1EoQzV1W2Sjy8PBda1IRW/dFYUh2TldOd WGE6iRrTXJI9CGJrxcQ== X-Proofpoint-GUID: MMUM8aT1bjfDwEyU1fy6_2vEGmUEKkty X-Proofpoint-ORIG-GUID: MMUM8aT1bjfDwEyU1fy6_2vEGmUEKkty X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1176,Hydra:6.1.134,FMLib:17.12.100.49 definitions=2026-10-19_04,2026-10-14_01,2025-10-01_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 lowpriorityscore=0 adultscore=0 bulkscore=0 impostorscore=0 phishscore=0 clxscore=1015 priorityscore=1501 malwarescore=0 suspectscore=0 spamscore=0 classifier=typeunknown authscore=0 authtc= authcc= route=outbound adjust=0 reason=mlx scancount=1 engine=8.22.0-2610080000 definitions=main-2610190060 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists1p.gnu.org; Received-SPF: pass client-ip=148.163.156.1; envelope-from=oelghoul@linux.ibm.com; helo=mx0a-001b2d01.pphosted.com X-Spam_score_int: -26 X-Spam_score: -2.7 X-Spam_bar: -- X-Spam_report: (-2.7 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: qemu development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @ibm.com) X-ZM-MESSAGEID: 1792424296081158500 Content-Type: text/plain; charset="utf-8" s390_pci_iommu_disable() destroys the I/O TLB and unpins the host's mappings, but it doesn't reset the available DMA slot counter back to the VFIO-advertised limit. This is problematic when an IOMMU is disabled and re-enabled while reusing the container (e.g. during a guest reboot), which may cause the RPCIT path to fail with DMA slot exhaustion errors, manifesting as unexplained ENOMEMs in the guest. Fix it by refactoring s390_pci_update_dma_avail() to take a pointer to S390PCIBusDevice instead of the plain file descriptor. Call it in s390_pci_iommu_disable() after the host mappings have been unpinned. Further print a warning if the VFIO-advertised counter doesn't match the in-use shadow counter, to reduce the likelihood of introducing new similar DMA accounting bugs in the future. Cc: qemu-stable@nongnu.org Fixes: 37fa32de7073 ("s390x/pci: Honor DMA limits set by vfio") Signed-off-by: Omar Elghoul --- v1 -> v2: rebase on latest master, ensure that the device is a vfio-pci device before calling vfio-specific code. --- hw/s390x/s390-pci-bus.c | 21 +++++++++++++++++++++ hw/s390x/s390-pci-vfio-stubs.c | 2 +- hw/s390x/s390-pci-vfio.c | 18 ++++++++++++------ include/hw/s390x/s390-pci-vfio.h | 2 +- 4 files changed, 35 insertions(+), 8 deletions(-) diff --git a/hw/s390x/s390-pci-bus.c b/hw/s390x/s390-pci-bus.c index 2724b27ec2..9938191c4d 100644 --- a/hw/s390x/s390-pci-bus.c +++ b/hw/s390x/s390-pci-bus.c @@ -825,6 +825,8 @@ void s390_pci_iommu_direct_map_enable(S390PCIBusDevice = *pbdev) void s390_pci_iommu_disable(S390PCIBusDevice *pbdev) { S390PCIIOMMU *iommu =3D pbdev->iommu; + unsigned int dma_avail; + pbdev->iommu_enabled =3D false; g_hash_table_remove_all(pbdev->iotlb); if (pbdev->dm_mr) { @@ -837,6 +839,25 @@ void s390_pci_iommu_disable(S390PCIBusDevice *pbdev) MEMORY_REGION(&pbdev->iommu_mr)); object_unparent(OBJECT(&pbdev->iommu_mr)); } + + /* + * update shadow counter from vfio after having unpinned all host mapp= ings. + * if this value mismatches our previous shadow count, then we likely = have + * DMA remapping, unmapping, or accounting bugs + */ + if (object_dynamic_cast(OBJECT(pbdev->pdev), "vfio-pci")) { + if (s390_pci_update_dma_avail(pbdev, &dma_avail)) { + if (pbdev->max_dma_limit !=3D dma_avail) { + warn_report("DMA limit %u from vfio !=3D %" PRIu64 " shado= wed", + dma_avail, pbdev->max_dma_limit); + pbdev->max_dma_limit =3D dma_avail; + } + + if (pbdev->dma_limit) { + pbdev->dma_limit->avail =3D dma_avail; + } + } + } } =20 static void s390_pci_iommu_free(S390pciState *s, PCIBus *bus, int32_t devf= n) diff --git a/hw/s390x/s390-pci-vfio-stubs.c b/hw/s390x/s390-pci-vfio-stubs.c index 9fc84ca135..5e0782ac0e 100644 --- a/hw/s390x/s390-pci-vfio-stubs.c +++ b/hw/s390x/s390-pci-vfio-stubs.c @@ -7,7 +7,7 @@ #include "qemu/osdep.h" #include "hw/s390x/s390-pci-vfio.h" =20 -bool s390_pci_update_dma_avail(int fd, unsigned int *avail) +bool s390_pci_update_dma_avail(S390PCIBusDevice *pbdev, unsigned int *avai= l) { return false; } diff --git a/hw/s390x/s390-pci-vfio.c b/hw/s390x/s390-pci-vfio.c index e65e2be473..f9a454ef7d 100644 --- a/hw/s390x/s390-pci-vfio.c +++ b/hw/s390x/s390-pci-vfio.c @@ -30,13 +30,23 @@ * limiting DMA requests, false otherwise. The current available count re= ad * from vfio is returned in avail. */ -bool s390_pci_update_dma_avail(int fd, unsigned int *avail) +bool s390_pci_update_dma_avail(S390PCIBusDevice *pbdev, unsigned int *avai= l) { + VFIOPCIDevice *vpdev; uint32_t argsz =3D sizeof(struct vfio_iommu_type1_info); g_autofree struct vfio_iommu_type1_info *info =3D g_malloc0(argsz); + int fd; =20 + assert(pbdev); assert(avail); =20 + vpdev =3D VFIO_PCI_DEVICE(pbdev->pdev); + if (!vpdev->vbasedev.group || !vpdev->vbasedev.group->container) { + return false; + } + + fd =3D vpdev->vbasedev.group->container->fd; + /* * If the specified argsz is not large enough to contain all capabilit= ies * it will be updated upon return from the ioctl. Retry until we have @@ -69,16 +79,12 @@ S390PCIDMACount *s390_pci_start_dma_count(S390pciState = *s, =20 assert(vpdev); =20 - if (!vpdev->vbasedev.group) { + if (!s390_pci_update_dma_avail(pbdev, &avail)) { return NULL; } =20 id =3D vpdev->vbasedev.group->container->fd; =20 - if (!s390_pci_update_dma_avail(id, &avail)) { - return NULL; - } - QTAILQ_FOREACH(cnt, &s->zpci_dma_limit, link) { if (cnt->id =3D=3D id) { cnt->users++; diff --git a/include/hw/s390x/s390-pci-vfio.h b/include/hw/s390x/s390-pci-v= fio.h index c7886b63ea..25d9e74f94 100644 --- a/include/hw/s390x/s390-pci-vfio.h +++ b/include/hw/s390x/s390-pci-vfio.h @@ -14,7 +14,7 @@ =20 #include "hw/s390x/s390-pci-bus.h" =20 -bool s390_pci_update_dma_avail(int fd, unsigned int *avail); +bool s390_pci_update_dma_avail(S390PCIBusDevice *pbdev, unsigned int *avai= l); S390PCIDMACount *s390_pci_start_dma_count(S390pciState *s, S390PCIBusDevice *pbdev); void s390_pci_end_dma_count(S390pciState *s, S390PCIDMACount *cnt); --=20 2.56.0