From nobody Mon Apr 6 22:48:47 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0D86AC433FE for ; Fri, 7 Oct 2022 09:50:38 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229634AbiJGJuf (ORCPT ); Fri, 7 Oct 2022 05:50:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46622 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229484AbiJGJu0 (ORCPT ); Fri, 7 Oct 2022 05:50:26 -0400 Received: from mx0b-001b2d01.pphosted.com (mx0b-001b2d01.pphosted.com [148.163.158.5]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D0FE18FD54; Fri, 7 Oct 2022 02:50:23 -0700 (PDT) Received: from pps.filterd (m0098417.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.17.1.5/8.17.1.5) with ESMTP id 2979krAR019435; Fri, 7 Oct 2022 09:50:12 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ibm.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-transfer-encoding; s=pp1; bh=sklmgDUMERqlDzjCiD0nFNyQwB/Fcgy2eHm6qe3R6mA=; b=n753fckrOfgRxWDCDb9j5v5tznObujXSOyb2Q/UbZOqxBCB+QHLH9KWOab8Nns+8m0Jl K7I7slFeMo7pqUzZpPKWgMBJmZCSrzXlbYRcKLBgSL60BGSV7U9B3HQYmntXB7rOcCwl yjimaH1ocvcsSrfp6T51yX9IhFb3VJxMw+gLbK9cdz0V82l8l40w5I8/ftclbHs3BM94 OYUMfdmb0hkcQrexS1Im82GSX2Jf3pWU4M8lM3QGE5F9ozp7fjsUsyQMjsQ+9Ygddbnj wOlw2J+NAn0JoQtWC1Ir7sPzk64SS4tI23ux0/sqGSfNzUSmLXb+Xpg4Wv8LsIvoZU/T lQ== Received: from ppma02fra.de.ibm.com (47.49.7a9f.ip4.static.sl-reverse.com [159.122.73.71]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 3k2hsj81v0-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Fri, 07 Oct 2022 09:50:11 +0000 Received: from pps.filterd (ppma02fra.de.ibm.com [127.0.0.1]) by ppma02fra.de.ibm.com (8.16.1.2/8.16.1.2) with SMTP id 2979ZVtp019672; Fri, 7 Oct 2022 09:50:10 GMT Received: from b06cxnps4074.portsmouth.uk.ibm.com (d06relay11.portsmouth.uk.ibm.com [9.149.109.196]) by ppma02fra.de.ibm.com with ESMTP id 3jxd68p737-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Fri, 07 Oct 2022 09:50:10 +0000 Received: from d06av23.portsmouth.uk.ibm.com (d06av23.portsmouth.uk.ibm.com [9.149.105.59]) by b06cxnps4074.portsmouth.uk.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 2979o6BX63242710 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 7 Oct 2022 09:50:06 GMT Received: from d06av23.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id CC798A4040; Fri, 7 Oct 2022 09:50:06 +0000 (GMT) Received: from d06av23.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 5A23DA404D; Fri, 7 Oct 2022 09:50:06 +0000 (GMT) Received: from tuxmaker.boeblingen.de.ibm.com (unknown [9.152.85.9]) by d06av23.portsmouth.uk.ibm.com (Postfix) with ESMTP; Fri, 7 Oct 2022 09:50:06 +0000 (GMT) From: Niklas Schnelle To: Matthew Rosato , Pierre Morel , iommu@lists.linux.dev Cc: linux-s390@vger.kernel.org, borntraeger@linux.ibm.com, hca@linux.ibm.com, gor@linux.ibm.com, gerald.schaefer@linux.ibm.com, agordeev@linux.ibm.com, svens@linux.ibm.com, joro@8bytes.org, will@kernel.org, robin.murphy@arm.com, jgg@nvidia.com, linux-kernel@vger.kernel.org Subject: [PATCH v6 1/6] iommu/s390: Fix duplicate domain attachments Date: Fri, 7 Oct 2022 11:50:00 +0200 Message-Id: <20221007095005.2017126-2-schnelle@linux.ibm.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20221007095005.2017126-1-schnelle@linux.ibm.com> References: <20221007095005.2017126-1-schnelle@linux.ibm.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-TM-AS-GCONF: 00 X-Proofpoint-ORIG-GUID: Fb4RTMEZ1rwf00mpNJeuAXapRIFcUCli X-Proofpoint-GUID: Fb4RTMEZ1rwf00mpNJeuAXapRIFcUCli X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.205,Aquarius:18.0.895,Hydra:6.0.528,FMLib:17.11.122.1 definitions=2022-10-06_05,2022-10-06_02,2022-06-22_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 bulkscore=0 priorityscore=1501 mlxscore=0 malwarescore=0 lowpriorityscore=0 suspectscore=0 phishscore=0 spamscore=0 mlxlogscore=999 adultscore=0 impostorscore=0 clxscore=1015 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2209130000 definitions=main-2210070058 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Since commit fa7e9ecc5e1c ("iommu/s390: Tolerate repeat attach_dev calls") we can end up with duplicates in the list of devices attached to a domain. This is inefficient and confusing since only one domain can actually be in control of the IOMMU translations for a device. Fix this by detaching the device from the previous domain, if any, on attach. Add a WARN_ON() in case we still have attached devices on freeing the domain. While here remove the re-attach on failure dance as it was determined to be unlikely to help and may confuse debug and recovery. Fixes: fa7e9ecc5e1c ("iommu/s390: Tolerate repeat attach_dev calls") Signed-off-by: Niklas Schnelle Reviewed-by: Matthew Rosato --- v5->v6: - Only set zdev->dma_table once zpci_register_ioat() succeeded (Matt) v4->v5: - Unregister IOAT and set zdev->dma_table on error (Matt) drivers/iommu/s390-iommu.c | 106 ++++++++++++++++--------------------- 1 file changed, 45 insertions(+), 61 deletions(-) diff --git a/drivers/iommu/s390-iommu.c b/drivers/iommu/s390-iommu.c index c898bcbbce11..96173cfee324 100644 --- a/drivers/iommu/s390-iommu.c +++ b/drivers/iommu/s390-iommu.c @@ -79,10 +79,36 @@ static void s390_domain_free(struct iommu_domain *domai= n) { struct s390_domain *s390_domain =3D to_s390_domain(domain); =20 + WARN_ON(!list_empty(&s390_domain->devices)); dma_cleanup_tables(s390_domain->dma_table); kfree(s390_domain); } =20 +static void __s390_iommu_detach_device(struct zpci_dev *zdev) +{ + struct s390_domain *s390_domain =3D zdev->s390_domain; + struct s390_domain_device *domain_device, *tmp; + unsigned long flags; + + if (!s390_domain) + return; + + spin_lock_irqsave(&s390_domain->list_lock, flags); + list_for_each_entry_safe(domain_device, tmp, &s390_domain->devices, + list) { + if (domain_device->zdev =3D=3D zdev) { + list_del(&domain_device->list); + kfree(domain_device); + break; + } + } + spin_unlock_irqrestore(&s390_domain->list_lock, flags); + + zpci_unregister_ioat(zdev, 0); + zdev->s390_domain =3D NULL; + zdev->dma_table =3D NULL; +} + static int s390_iommu_attach_device(struct iommu_domain *domain, struct device *dev) { @@ -90,7 +116,7 @@ static int s390_iommu_attach_device(struct iommu_domain = *domain, struct zpci_dev *zdev =3D to_zpci_dev(dev); struct s390_domain_device *domain_device; unsigned long flags; - int cc, rc; + int cc, rc =3D 0; =20 if (!zdev) return -ENODEV; @@ -99,24 +125,18 @@ static int s390_iommu_attach_device(struct iommu_domai= n *domain, if (!domain_device) return -ENOMEM; =20 - if (zdev->dma_table && !zdev->s390_domain) { - cc =3D zpci_dma_exit_device(zdev); - if (cc) { - rc =3D -EIO; - goto out_free; - } - } - if (zdev->s390_domain) - zpci_unregister_ioat(zdev, 0); + __s390_iommu_detach_device(zdev); + else if (zdev->dma_table) + zpci_dma_exit_device(zdev); =20 - zdev->dma_table =3D s390_domain->dma_table; cc =3D zpci_register_ioat(zdev, 0, zdev->start_dma, zdev->end_dma, - virt_to_phys(zdev->dma_table)); + virt_to_phys(s390_domain->dma_table)); if (cc) { rc =3D -EIO; - goto out_restore; + goto out_free; } + zdev->dma_table =3D s390_domain->dma_table; =20 spin_lock_irqsave(&s390_domain->list_lock, flags); /* First device defines the DMA range limits */ @@ -127,9 +147,9 @@ static int s390_iommu_attach_device(struct iommu_domain= *domain, /* Allow only devices with identical DMA range limits */ } else if (domain->geometry.aperture_start !=3D zdev->start_dma || domain->geometry.aperture_end !=3D zdev->end_dma) { - rc =3D -EINVAL; spin_unlock_irqrestore(&s390_domain->list_lock, flags); - goto out_restore; + rc =3D -EINVAL; + goto out_unregister; } domain_device->zdev =3D zdev; zdev->s390_domain =3D s390_domain; @@ -138,14 +158,9 @@ static int s390_iommu_attach_device(struct iommu_domai= n *domain, =20 return 0; =20 -out_restore: - if (!zdev->s390_domain) { - zpci_dma_init_device(zdev); - } else { - zdev->dma_table =3D zdev->s390_domain->dma_table; - zpci_register_ioat(zdev, 0, zdev->start_dma, zdev->end_dma, - virt_to_phys(zdev->dma_table)); - } +out_unregister: + zpci_unregister_ioat(zdev, 0); + zdev->dma_table =3D NULL; out_free: kfree(domain_device); =20 @@ -155,32 +170,12 @@ static int s390_iommu_attach_device(struct iommu_doma= in *domain, static void s390_iommu_detach_device(struct iommu_domain *domain, struct device *dev) { - struct s390_domain *s390_domain =3D to_s390_domain(domain); struct zpci_dev *zdev =3D to_zpci_dev(dev); - struct s390_domain_device *domain_device, *tmp; - unsigned long flags; - int found =3D 0; =20 - if (!zdev) - return; + WARN_ON(zdev->s390_domain !=3D to_s390_domain(domain)); =20 - spin_lock_irqsave(&s390_domain->list_lock, flags); - list_for_each_entry_safe(domain_device, tmp, &s390_domain->devices, - list) { - if (domain_device->zdev =3D=3D zdev) { - list_del(&domain_device->list); - kfree(domain_device); - found =3D 1; - break; - } - } - spin_unlock_irqrestore(&s390_domain->list_lock, flags); - - if (found && (zdev->s390_domain =3D=3D s390_domain)) { - zdev->s390_domain =3D NULL; - zpci_unregister_ioat(zdev, 0); - zpci_dma_init_device(zdev); - } + __s390_iommu_detach_device(zdev); + zpci_dma_init_device(zdev); } =20 static struct iommu_device *s390_iommu_probe_device(struct device *dev) @@ -193,24 +188,13 @@ static struct iommu_device *s390_iommu_probe_device(s= truct device *dev) static void s390_iommu_release_device(struct device *dev) { struct zpci_dev *zdev =3D to_zpci_dev(dev); - struct iommu_domain *domain; =20 /* - * This is a workaround for a scenario where the IOMMU API common code - * "forgets" to call the detach_dev callback: After binding a device - * to vfio-pci and completing the VFIO_SET_IOMMU ioctl (which triggers - * the attach_dev), removing the device via - * "echo 1 > /sys/bus/pci/devices/.../remove" won't trigger detach_dev, - * only release_device will be called via the BUS_NOTIFY_REMOVED_DEVICE - * notifier. - * - * So let's call detach_dev from here if it hasn't been called before. + * release_device is expected to detach any domain currently attached + * to the device, but keep it attached to other devices in the group. */ - if (zdev && zdev->s390_domain) { - domain =3D iommu_get_domain_for_dev(dev); - if (domain) - s390_iommu_detach_device(domain, dev); - } + if (zdev) + __s390_iommu_detach_device(zdev); } =20 static int s390_iommu_update_trans(struct s390_domain *s390_domain, --=20 2.34.1 From nobody Mon Apr 6 22:48:47 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id DEA34C43217 for ; Fri, 7 Oct 2022 09:50:34 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229658AbiJGJuc (ORCPT ); Fri, 7 Oct 2022 05:50:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46624 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229479AbiJGJu0 (ORCPT ); Fri, 7 Oct 2022 05:50:26 -0400 Received: from mx0b-001b2d01.pphosted.com (mx0b-001b2d01.pphosted.com [148.163.158.5]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 805B99082D; Fri, 7 Oct 2022 02:50:24 -0700 (PDT) Received: from pps.filterd (m0127361.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.17.1.5/8.17.1.5) with ESMTP id 2979nodW039002; Fri, 7 Oct 2022 09:50:13 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ibm.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-transfer-encoding; s=pp1; bh=eiv4fa9LQYx/DHFqzsQokv7TRRJkGniPK07zniwRYrU=; b=W2/T7OW2S92uGX/WyiLm/3dULAY38BX5+F8ncJNChD8LXUiQ+9lklqy4HZWuPlvMg2GD c/yJtDYEBgm5QEj3Z6S8OQnihzqP/FcYB9qFxr9DesNS5CNyLQ51xeYPnTnKERDAShYB YUcU+aCq6CE5h21GzphX2EHWY+09lD3NnSsP9jNb7emdaPOoh3oCpG3lbIkxwy9PG/1I VgCNQbR1woEiblGhDLt4zXVMVRZ8MQVzr+Vb0D+0ZDumcEP/dks15w50hXrUmrMNIyPs WTju04mN3LpRd3J8CbwgLEPT7eRrymZmiITdYrHtHWuRUcJxFlgqXkdF4+ZtUcHjSq0P 3A== Received: from ppma04ams.nl.ibm.com (63.31.33a9.ip4.static.sl-reverse.com [169.51.49.99]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 3k2hty80bj-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Fri, 07 Oct 2022 09:50:12 +0000 Received: from pps.filterd (ppma04ams.nl.ibm.com [127.0.0.1]) by ppma04ams.nl.ibm.com (8.16.1.2/8.16.1.2) with SMTP id 2979aUof006710; Fri, 7 Oct 2022 09:50:11 GMT Received: from b06cxnps4075.portsmouth.uk.ibm.com (d06relay12.portsmouth.uk.ibm.com [9.149.109.197]) by ppma04ams.nl.ibm.com with ESMTP id 3jxd698dqn-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Fri, 07 Oct 2022 09:50:10 +0000 Received: from d06av23.portsmouth.uk.ibm.com (d06av23.portsmouth.uk.ibm.com [9.149.105.59]) by b06cxnps4075.portsmouth.uk.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 2979o71p918132 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 7 Oct 2022 09:50:07 GMT Received: from d06av23.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 53D99A4040; Fri, 7 Oct 2022 09:50:07 +0000 (GMT) Received: from d06av23.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id D83ECA4051; Fri, 7 Oct 2022 09:50:06 +0000 (GMT) Received: from tuxmaker.boeblingen.de.ibm.com (unknown [9.152.85.9]) by d06av23.portsmouth.uk.ibm.com (Postfix) with ESMTP; Fri, 7 Oct 2022 09:50:06 +0000 (GMT) From: Niklas Schnelle To: Matthew Rosato , Pierre Morel , iommu@lists.linux.dev Cc: linux-s390@vger.kernel.org, borntraeger@linux.ibm.com, hca@linux.ibm.com, gor@linux.ibm.com, gerald.schaefer@linux.ibm.com, agordeev@linux.ibm.com, svens@linux.ibm.com, joro@8bytes.org, will@kernel.org, robin.murphy@arm.com, jgg@nvidia.com, linux-kernel@vger.kernel.org Subject: [PATCH v6 2/6] iommu/s390: Get rid of s390_domain_device Date: Fri, 7 Oct 2022 11:50:01 +0200 Message-Id: <20221007095005.2017126-3-schnelle@linux.ibm.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20221007095005.2017126-1-schnelle@linux.ibm.com> References: <20221007095005.2017126-1-schnelle@linux.ibm.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-TM-AS-GCONF: 00 X-Proofpoint-GUID: O1AxhZpoAGfNlHVOh7MDM_Dntdoht2AA X-Proofpoint-ORIG-GUID: O1AxhZpoAGfNlHVOh7MDM_Dntdoht2AA X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.205,Aquarius:18.0.895,Hydra:6.0.528,FMLib:17.11.122.1 definitions=2022-10-06_05,2022-10-06_02,2022-06-22_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 mlxscore=0 bulkscore=0 suspectscore=0 clxscore=1015 spamscore=0 impostorscore=0 adultscore=0 phishscore=0 lowpriorityscore=0 mlxlogscore=999 malwarescore=0 priorityscore=1501 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2209130000 definitions=main-2210070058 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" The struct s390_domain_device serves the sole purpose as list entry for the devices list of a struct s390_domain. As it contains no additional information besides a list_head and a pointer to the struct zpci_dev we can simplify things and just thread the device list through struct zpci_dev directly. This removes the need to allocate during domain attach and gets rid of one level of indirection during mapping operations. Reviewed-by: Matthew Rosato Signed-off-by: Niklas Schnelle --- v5->v6: - Drop Jason's R-b as he didn't explicitly extend it for the rollback change arch/s390/include/asm/pci.h | 1 + drivers/iommu/s390-iommu.c | 37 +++++++------------------------------ 2 files changed, 8 insertions(+), 30 deletions(-) diff --git a/arch/s390/include/asm/pci.h b/arch/s390/include/asm/pci.h index 108e732d7b14..15f8714ca9b7 100644 --- a/arch/s390/include/asm/pci.h +++ b/arch/s390/include/asm/pci.h @@ -117,6 +117,7 @@ struct zpci_bus { struct zpci_dev { struct zpci_bus *zbus; struct list_head entry; /* list of all zpci_devices, needed for hotplug,= etc. */ + struct list_head iommu_list; struct kref kref; struct hotplug_slot hotplug_slot; =20 diff --git a/drivers/iommu/s390-iommu.c b/drivers/iommu/s390-iommu.c index 96173cfee324..399c31b97f65 100644 --- a/drivers/iommu/s390-iommu.c +++ b/drivers/iommu/s390-iommu.c @@ -29,11 +29,6 @@ struct s390_domain { spinlock_t list_lock; }; =20 -struct s390_domain_device { - struct list_head list; - struct zpci_dev *zdev; -}; - static struct s390_domain *to_s390_domain(struct iommu_domain *dom) { return container_of(dom, struct s390_domain, domain); @@ -87,21 +82,13 @@ static void s390_domain_free(struct iommu_domain *domai= n) static void __s390_iommu_detach_device(struct zpci_dev *zdev) { struct s390_domain *s390_domain =3D zdev->s390_domain; - struct s390_domain_device *domain_device, *tmp; unsigned long flags; =20 if (!s390_domain) return; =20 spin_lock_irqsave(&s390_domain->list_lock, flags); - list_for_each_entry_safe(domain_device, tmp, &s390_domain->devices, - list) { - if (domain_device->zdev =3D=3D zdev) { - list_del(&domain_device->list); - kfree(domain_device); - break; - } - } + list_del_init(&zdev->iommu_list); spin_unlock_irqrestore(&s390_domain->list_lock, flags); =20 zpci_unregister_ioat(zdev, 0); @@ -114,17 +101,12 @@ static int s390_iommu_attach_device(struct iommu_doma= in *domain, { struct s390_domain *s390_domain =3D to_s390_domain(domain); struct zpci_dev *zdev =3D to_zpci_dev(dev); - struct s390_domain_device *domain_device; unsigned long flags; int cc, rc =3D 0; =20 if (!zdev) return -ENODEV; =20 - domain_device =3D kzalloc(sizeof(*domain_device), GFP_KERNEL); - if (!domain_device) - return -ENOMEM; - if (zdev->s390_domain) __s390_iommu_detach_device(zdev); else if (zdev->dma_table) @@ -132,10 +114,8 @@ static int s390_iommu_attach_device(struct iommu_domai= n *domain, =20 cc =3D zpci_register_ioat(zdev, 0, zdev->start_dma, zdev->end_dma, virt_to_phys(s390_domain->dma_table)); - if (cc) { - rc =3D -EIO; - goto out_free; - } + if (cc) + return -EIO; zdev->dma_table =3D s390_domain->dma_table; =20 spin_lock_irqsave(&s390_domain->list_lock, flags); @@ -151,9 +131,8 @@ static int s390_iommu_attach_device(struct iommu_domain= *domain, rc =3D -EINVAL; goto out_unregister; } - domain_device->zdev =3D zdev; zdev->s390_domain =3D s390_domain; - list_add(&domain_device->list, &s390_domain->devices); + list_add(&zdev->iommu_list, &s390_domain->devices); spin_unlock_irqrestore(&s390_domain->list_lock, flags); =20 return 0; @@ -161,8 +140,6 @@ static int s390_iommu_attach_device(struct iommu_domain= *domain, out_unregister: zpci_unregister_ioat(zdev, 0); zdev->dma_table =3D NULL; -out_free: - kfree(domain_device); =20 return rc; } @@ -201,10 +178,10 @@ static int s390_iommu_update_trans(struct s390_domain= *s390_domain, phys_addr_t pa, dma_addr_t dma_addr, size_t size, int flags) { - struct s390_domain_device *domain_device; phys_addr_t page_addr =3D pa & PAGE_MASK; dma_addr_t start_dma_addr =3D dma_addr; unsigned long irq_flags, nr_pages, i; + struct zpci_dev *zdev; unsigned long *entry; int rc =3D 0; =20 @@ -229,8 +206,8 @@ static int s390_iommu_update_trans(struct s390_domain *= s390_domain, } =20 spin_lock(&s390_domain->list_lock); - list_for_each_entry(domain_device, &s390_domain->devices, list) { - rc =3D zpci_refresh_trans((u64) domain_device->zdev->fh << 32, + list_for_each_entry(zdev, &s390_domain->devices, iommu_list) { + rc =3D zpci_refresh_trans((u64)zdev->fh << 32, start_dma_addr, nr_pages * PAGE_SIZE); if (rc) break; --=20 2.34.1 From nobody Mon Apr 6 22:48:47 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5F530C433FE for ; Fri, 7 Oct 2022 09:50:41 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229459AbiJGJuj (ORCPT ); Fri, 7 Oct 2022 05:50:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46680 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229591AbiJGJu2 (ORCPT ); Fri, 7 Oct 2022 05:50:28 -0400 Received: from mx0a-001b2d01.pphosted.com (mx0a-001b2d01.pphosted.com [148.163.156.1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6CBDD9D527; Fri, 7 Oct 2022 02:50:24 -0700 (PDT) Received: from pps.filterd (m0098409.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.17.1.5/8.17.1.5) with ESMTP id 2979jcWx004633; Fri, 7 Oct 2022 09:50:14 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ibm.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-transfer-encoding; s=pp1; bh=k23RzXU4mgAv59rxzFKF7MDBNPemeo6Xe8BqpTM84Mc=; b=HB2zYL4Ma7+AJ9YPDGdfstwffeeu6ni5KEGkgMQQ43yUihvShfyC/hexrayBXtApc7gA 4K3aY/fib7F6MBdAcQF8eB9ZcMZb/qxi+BwBmqVqRgoMgf65lckRxnSnag8elgZoaok8 KgYZviyjyOvC1rr58tzymvDyVV82Y8b8HGIZOzPHzzGaF82NE3bCLPcwh9GGmGj/GXiw z1YoXLhoSY+BOByUGx9fxuQPgNA5xAl+iPG+TN0st7Jg6qWwyDnxblXKhRpe+9y3UH/6 chyh5Z8p6sZVPQmWmSH+b2oZeuLaIlDwT+2v3wrDe/NFi9ib3IcGqQfyVnroGbP8uccF Qw== Received: from ppma06fra.de.ibm.com (48.49.7a9f.ip4.static.sl-reverse.com [159.122.73.72]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 3k2hs00451-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Fri, 07 Oct 2022 09:50:13 +0000 Received: from pps.filterd (ppma06fra.de.ibm.com [127.0.0.1]) by ppma06fra.de.ibm.com (8.16.1.2/8.16.1.2) with SMTP id 2979ajwW014491; Fri, 7 Oct 2022 09:50:11 GMT Received: from b06cxnps4075.portsmouth.uk.ibm.com (d06relay12.portsmouth.uk.ibm.com [9.149.109.197]) by ppma06fra.de.ibm.com with ESMTP id 3jxcthx6d2-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Fri, 07 Oct 2022 09:50:11 +0000 Received: from d06av23.portsmouth.uk.ibm.com (d06av23.portsmouth.uk.ibm.com [9.149.105.59]) by b06cxnps4075.portsmouth.uk.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 2979o7Vb43778364 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 7 Oct 2022 09:50:08 GMT Received: from d06av23.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id CFDADA4040; Fri, 7 Oct 2022 09:50:07 +0000 (GMT) Received: from d06av23.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 5FD9CA404D; Fri, 7 Oct 2022 09:50:07 +0000 (GMT) Received: from tuxmaker.boeblingen.de.ibm.com (unknown [9.152.85.9]) by d06av23.portsmouth.uk.ibm.com (Postfix) with ESMTP; Fri, 7 Oct 2022 09:50:07 +0000 (GMT) From: Niklas Schnelle To: Matthew Rosato , Pierre Morel , iommu@lists.linux.dev Cc: linux-s390@vger.kernel.org, borntraeger@linux.ibm.com, hca@linux.ibm.com, gor@linux.ibm.com, gerald.schaefer@linux.ibm.com, agordeev@linux.ibm.com, svens@linux.ibm.com, joro@8bytes.org, will@kernel.org, robin.murphy@arm.com, jgg@nvidia.com, linux-kernel@vger.kernel.org Subject: [PATCH v6 3/6] iommu/s390: Fix potential s390_domain aperture shrinking Date: Fri, 7 Oct 2022 11:50:02 +0200 Message-Id: <20221007095005.2017126-4-schnelle@linux.ibm.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20221007095005.2017126-1-schnelle@linux.ibm.com> References: <20221007095005.2017126-1-schnelle@linux.ibm.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-TM-AS-GCONF: 00 X-Proofpoint-ORIG-GUID: kYD_OPVCcLN_mYM_zzT21-zi6gKZUO9c X-Proofpoint-GUID: kYD_OPVCcLN_mYM_zzT21-zi6gKZUO9c X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.205,Aquarius:18.0.895,Hydra:6.0.528,FMLib:17.11.122.1 definitions=2022-10-06_05,2022-10-06_02,2022-06-22_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 priorityscore=1501 spamscore=0 malwarescore=0 suspectscore=0 adultscore=0 impostorscore=0 lowpriorityscore=0 mlxlogscore=999 clxscore=1015 phishscore=0 mlxscore=0 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2209130000 definitions=main-2210070058 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" The s390 IOMMU driver currently sets the IOMMU domain's aperture to match the device specific DMA address range of the device that is first attached. This is not ideal. For one if the domain has no device attached in the meantime the aperture could be shrunk allowing translations outside the aperture to exist in the translation tables. Also this is a bit of a misuse of the aperture which really should describe what addresses can be translated and not some device specific limitations. Instead of misusing the aperture like this we can instead create reserved ranges for the ranges inaccessible to the attached devices allowing devices with overlapping ranges to still share an IOMMU domain. This also significantly simplifies s390_iommu_attach_device() allowing us to move the aperture check to the beginning of the function and removing the need to hold the device list's lock to check the aperture. As we then use the same aperture for all domains and it only depends on the table properties we can already check zdev->start_dma/end_dma at probe time and turn the check on attach into a WARN_ON(). Suggested-by: Jason Gunthorpe Reviewed-by: Matthew Rosato Signed-off-by: Niklas Schnelle --- v5->v6: - Return -EINVAL after WARN_ON() in attach v4->v5: - Make aperture check in attach a WARN_ON() and fail in probe if zdev->start_dma/end_dma doesn't git in aperture (Jason) drivers/iommu/s390-iommu.c | 63 ++++++++++++++++++++++++++------------ 1 file changed, 43 insertions(+), 20 deletions(-) diff --git a/drivers/iommu/s390-iommu.c b/drivers/iommu/s390-iommu.c index 399c31b97f65..65835a5ca328 100644 --- a/drivers/iommu/s390-iommu.c +++ b/drivers/iommu/s390-iommu.c @@ -62,6 +62,9 @@ static struct iommu_domain *s390_domain_alloc(unsigned do= main_type) kfree(s390_domain); return NULL; } + s390_domain->domain.geometry.force_aperture =3D true; + s390_domain->domain.geometry.aperture_start =3D 0; + s390_domain->domain.geometry.aperture_end =3D ZPCI_TABLE_SIZE_RT - 1; =20 spin_lock_init(&s390_domain->dma_table_lock); spin_lock_init(&s390_domain->list_lock); @@ -102,11 +105,15 @@ static int s390_iommu_attach_device(struct iommu_doma= in *domain, struct s390_domain *s390_domain =3D to_s390_domain(domain); struct zpci_dev *zdev =3D to_zpci_dev(dev); unsigned long flags; - int cc, rc =3D 0; + int cc; =20 if (!zdev) return -ENODEV; =20 + if (WARN_ON(domain->geometry.aperture_start > zdev->end_dma || + domain->geometry.aperture_end < zdev->start_dma)) + return -EINVAL; + if (zdev->s390_domain) __s390_iommu_detach_device(zdev); else if (zdev->dma_table) @@ -118,30 +125,14 @@ static int s390_iommu_attach_device(struct iommu_doma= in *domain, return -EIO; zdev->dma_table =3D s390_domain->dma_table; =20 - spin_lock_irqsave(&s390_domain->list_lock, flags); - /* First device defines the DMA range limits */ - if (list_empty(&s390_domain->devices)) { - domain->geometry.aperture_start =3D zdev->start_dma; - domain->geometry.aperture_end =3D zdev->end_dma; - domain->geometry.force_aperture =3D true; - /* Allow only devices with identical DMA range limits */ - } else if (domain->geometry.aperture_start !=3D zdev->start_dma || - domain->geometry.aperture_end !=3D zdev->end_dma) { - spin_unlock_irqrestore(&s390_domain->list_lock, flags); - rc =3D -EINVAL; - goto out_unregister; - } + zdev->dma_table =3D s390_domain->dma_table; zdev->s390_domain =3D s390_domain; + + spin_lock_irqsave(&s390_domain->list_lock, flags); list_add(&zdev->iommu_list, &s390_domain->devices); spin_unlock_irqrestore(&s390_domain->list_lock, flags); =20 return 0; - -out_unregister: - zpci_unregister_ioat(zdev, 0); - zdev->dma_table =3D NULL; - - return rc; } =20 static void s390_iommu_detach_device(struct iommu_domain *domain, @@ -155,10 +146,41 @@ static void s390_iommu_detach_device(struct iommu_dom= ain *domain, zpci_dma_init_device(zdev); } =20 +static void s390_iommu_get_resv_regions(struct device *dev, + struct list_head *list) +{ + struct zpci_dev *zdev =3D to_zpci_dev(dev); + struct iommu_resv_region *region; + + if (zdev->start_dma) { + region =3D iommu_alloc_resv_region(0, zdev->start_dma, 0, + IOMMU_RESV_RESERVED); + if (!region) + return; + list_add_tail(®ion->list, list); + } + + if (zdev->end_dma < ZPCI_TABLE_SIZE_RT - 1) { + region =3D iommu_alloc_resv_region(zdev->end_dma + 1, + ZPCI_TABLE_SIZE_RT - zdev->end_dma - 1, + 0, IOMMU_RESV_RESERVED); + if (!region) + return; + list_add_tail(®ion->list, list); + } +} + static struct iommu_device *s390_iommu_probe_device(struct device *dev) { struct zpci_dev *zdev =3D to_zpci_dev(dev); =20 + if (zdev->start_dma > zdev->end_dma || + zdev->start_dma > ZPCI_TABLE_SIZE_RT - 1) + return ERR_PTR(-EINVAL); + + if (zdev->end_dma > ZPCI_TABLE_SIZE_RT - 1) + zdev->end_dma =3D ZPCI_TABLE_SIZE_RT - 1; + return &zdev->iommu_dev; } =20 @@ -337,6 +359,7 @@ static const struct iommu_ops s390_iommu_ops =3D { .release_device =3D s390_iommu_release_device, .device_group =3D generic_device_group, .pgsize_bitmap =3D S390_IOMMU_PGSIZES, + .get_resv_regions =3D s390_iommu_get_resv_regions, .default_domain_ops =3D &(const struct iommu_domain_ops) { .attach_dev =3D s390_iommu_attach_device, .detach_dev =3D s390_iommu_detach_device, --=20 2.34.1 From nobody Mon Apr 6 22:48:47 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8133DC433FE for ; Fri, 7 Oct 2022 09:50:52 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229728AbiJGJuu (ORCPT ); Fri, 7 Oct 2022 05:50:50 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46714 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229469AbiJGJu3 (ORCPT ); Fri, 7 Oct 2022 05:50:29 -0400 Received: from mx0a-001b2d01.pphosted.com (mx0b-001b2d01.pphosted.com [148.163.158.5]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6CB569D525; Fri, 7 Oct 2022 02:50:24 -0700 (PDT) Received: from pps.filterd (m0098420.ppops.net [127.0.0.1]) by mx0b-001b2d01.pphosted.com (8.17.1.5/8.17.1.5) with ESMTP id 2979IcK5006691; Fri, 7 Oct 2022 09:50:13 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ibm.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-transfer-encoding; s=pp1; bh=k+I5WhOS16FY7irvxpfSnfTvyCVCLvAaTyU/MnPz2/Y=; b=iAs/gUAwVK/BUwf4DfzSV29WP+nfJbsu7d+GIH3703iZ8ORUoZDLmUf3sXkFDUD5hmDe dAv8b+MgvVJpjA1EivolbYwqczeFGdI4xBlAzvNvkGmKZhGs7Ww2vunGREPx3Zln9ft9 DgP4f/fW2hqR5LFGct3I5Vthwxg2tFvzJTn4czZvvVK2wbylOBnKnJ3MAG57PgbMUnC/ R8KJ2YL1isYpbsVz7xRnc7hobs0N0So09PddwZEsZJYs6XpdA76N6DI2RC2h7iUP3m8j bL1IxOAPQJpktZzvx3KzWd2N0rmOel/qB7AXmjbG5ZoxwAMetHEMOwK5pqFHpO9lqVMo yQ== Received: from ppma01fra.de.ibm.com (46.49.7a9f.ip4.static.sl-reverse.com [159.122.73.70]) by mx0b-001b2d01.pphosted.com (PPS) with ESMTPS id 3k2hc2gsbd-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Fri, 07 Oct 2022 09:50:13 +0000 Received: from pps.filterd (ppma01fra.de.ibm.com [127.0.0.1]) by ppma01fra.de.ibm.com (8.16.1.2/8.16.1.2) with SMTP id 2979ZqIO012610; Fri, 7 Oct 2022 09:50:11 GMT Received: from b06cxnps4076.portsmouth.uk.ibm.com (d06relay13.portsmouth.uk.ibm.com [9.149.109.198]) by ppma01fra.de.ibm.com with ESMTP id 3jxd68x6gm-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Fri, 07 Oct 2022 09:50:11 +0000 Received: from d06av23.portsmouth.uk.ibm.com (d06av23.portsmouth.uk.ibm.com [9.149.105.59]) by b06cxnps4076.portsmouth.uk.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 2979o8WE3473978 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 7 Oct 2022 09:50:08 GMT Received: from d06av23.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 5EDB2A4055; Fri, 7 Oct 2022 09:50:08 +0000 (GMT) Received: from d06av23.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id DBA43A4051; Fri, 7 Oct 2022 09:50:07 +0000 (GMT) Received: from tuxmaker.boeblingen.de.ibm.com (unknown [9.152.85.9]) by d06av23.portsmouth.uk.ibm.com (Postfix) with ESMTP; Fri, 7 Oct 2022 09:50:07 +0000 (GMT) From: Niklas Schnelle To: Matthew Rosato , Pierre Morel , iommu@lists.linux.dev Cc: linux-s390@vger.kernel.org, borntraeger@linux.ibm.com, hca@linux.ibm.com, gor@linux.ibm.com, gerald.schaefer@linux.ibm.com, agordeev@linux.ibm.com, svens@linux.ibm.com, joro@8bytes.org, will@kernel.org, robin.murphy@arm.com, jgg@nvidia.com, linux-kernel@vger.kernel.org Subject: [PATCH v6 4/6] iommu/s390: Fix incorrect aperture check Date: Fri, 7 Oct 2022 11:50:03 +0200 Message-Id: <20221007095005.2017126-5-schnelle@linux.ibm.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20221007095005.2017126-1-schnelle@linux.ibm.com> References: <20221007095005.2017126-1-schnelle@linux.ibm.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-TM-AS-GCONF: 00 X-Proofpoint-GUID: IUk6XtExZIj4tAYOXSjRIt3c33xd6Cyg X-Proofpoint-ORIG-GUID: IUk6XtExZIj4tAYOXSjRIt3c33xd6Cyg X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.205,Aquarius:18.0.895,Hydra:6.0.528,FMLib:17.11.122.1 definitions=2022-10-06_05,2022-10-06_02,2022-06-22_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 clxscore=1015 mlxlogscore=999 phishscore=0 malwarescore=0 impostorscore=0 suspectscore=0 mlxscore=0 spamscore=0 bulkscore=0 adultscore=0 lowpriorityscore=0 priorityscore=1501 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2209130000 definitions=main-2210070058 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" The domain->geometry.aperture_end specifies the last valid address treat it as such when checking if a DMA address is valid. Reviewed-by: Pierre Morel Reviewed-by: Matthew Rosato Signed-off-by: Niklas Schnelle --- drivers/iommu/s390-iommu.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/iommu/s390-iommu.c b/drivers/iommu/s390-iommu.c index 65835a5ca328..a4c6a1a63fef 100644 --- a/drivers/iommu/s390-iommu.c +++ b/drivers/iommu/s390-iommu.c @@ -208,7 +208,7 @@ static int s390_iommu_update_trans(struct s390_domain *= s390_domain, int rc =3D 0; =20 if (dma_addr < s390_domain->domain.geometry.aperture_start || - dma_addr + size > s390_domain->domain.geometry.aperture_end) + (dma_addr + size - 1) > s390_domain->domain.geometry.aperture_end) return -EINVAL; =20 nr_pages =3D PAGE_ALIGN(size) >> PAGE_SHIFT; --=20 2.34.1 From nobody Mon Apr 6 22:48:47 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 152E9C433F5 for ; Fri, 7 Oct 2022 09:50:48 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229696AbiJGJuq (ORCPT ); Fri, 7 Oct 2022 05:50:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46716 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229638AbiJGJu3 (ORCPT ); Fri, 7 Oct 2022 05:50:29 -0400 Received: from mx0a-001b2d01.pphosted.com (mx0b-001b2d01.pphosted.com [148.163.158.5]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6CD259DF9E; Fri, 7 Oct 2022 02:50:24 -0700 (PDT) Received: from pps.filterd (m0098419.ppops.net [127.0.0.1]) by mx0b-001b2d01.pphosted.com (8.17.1.5/8.17.1.5) with ESMTP id 2978W9aj019016; Fri, 7 Oct 2022 09:50:14 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ibm.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-transfer-encoding; s=pp1; bh=90xBI4t5xFLU8Bn8ZUxT40mDfH6QxRmqURoTJJEAiuM=; b=owU5v+TLgaAgRu36VA1p76MERDO1TtjeCAL2aQH2b13OEwPgj3itTKizVPEyXXh0g6sq YY1iCGlNeOp2lMik2zBI5AP8Glue3V2p1vEMzXC/h2vmHVT4X5YbzdSOrf5TagHRxeCD yjLOe37kGyBEAhYqHmKTkxqCwKjvaEEJ0OzQPW9PoP4dfOLQOAMubZReeStDqNcTJq5L Wjl6hbFpm9ifpd/6BLUUV9y16vjy3yOFUlaTQmQpAF3r+zx7UgiG1wxmXlib4opczEkh au/JB6/PFmVaNj0CAnSNlz7a6BPXNTt6Z2DPk1tu9MSxnQQYnSgcC6U0ALGyQ+4sA1uB wA== Received: from ppma03ams.nl.ibm.com (62.31.33a9.ip4.static.sl-reverse.com [169.51.49.98]) by mx0b-001b2d01.pphosted.com (PPS) with ESMTPS id 3k2gphjbq4-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Fri, 07 Oct 2022 09:50:13 +0000 Received: from pps.filterd (ppma03ams.nl.ibm.com [127.0.0.1]) by ppma03ams.nl.ibm.com (8.16.1.2/8.16.1.2) with SMTP id 2979aPQW004228; Fri, 7 Oct 2022 09:50:12 GMT Received: from b06cxnps4076.portsmouth.uk.ibm.com (d06relay13.portsmouth.uk.ibm.com [9.149.109.198]) by ppma03ams.nl.ibm.com with ESMTP id 3jxd698dyk-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Fri, 07 Oct 2022 09:50:11 +0000 Received: from d06av23.portsmouth.uk.ibm.com (d06av23.portsmouth.uk.ibm.com [9.149.105.59]) by b06cxnps4076.portsmouth.uk.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 2979o8f11114756 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 7 Oct 2022 09:50:08 GMT Received: from d06av23.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id D959DA4053; Fri, 7 Oct 2022 09:50:08 +0000 (GMT) Received: from d06av23.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 6B179A404D; Fri, 7 Oct 2022 09:50:08 +0000 (GMT) Received: from tuxmaker.boeblingen.de.ibm.com (unknown [9.152.85.9]) by d06av23.portsmouth.uk.ibm.com (Postfix) with ESMTP; Fri, 7 Oct 2022 09:50:08 +0000 (GMT) From: Niklas Schnelle To: Matthew Rosato , Pierre Morel , iommu@lists.linux.dev Cc: linux-s390@vger.kernel.org, borntraeger@linux.ibm.com, hca@linux.ibm.com, gor@linux.ibm.com, gerald.schaefer@linux.ibm.com, agordeev@linux.ibm.com, svens@linux.ibm.com, joro@8bytes.org, will@kernel.org, robin.murphy@arm.com, jgg@nvidia.com, linux-kernel@vger.kernel.org Subject: [PATCH v6 5/6] iommu/s390: Fix incorrect pgsize_bitmap Date: Fri, 7 Oct 2022 11:50:04 +0200 Message-Id: <20221007095005.2017126-6-schnelle@linux.ibm.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20221007095005.2017126-1-schnelle@linux.ibm.com> References: <20221007095005.2017126-1-schnelle@linux.ibm.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-TM-AS-GCONF: 00 X-Proofpoint-GUID: atYLBN8AcIWGtL_Q3HK4I3UcQj3Skjpw X-Proofpoint-ORIG-GUID: atYLBN8AcIWGtL_Q3HK4I3UcQj3Skjpw X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.205,Aquarius:18.0.895,Hydra:6.0.528,FMLib:17.11.122.1 definitions=2022-10-06_05,2022-10-06_02,2022-06-22_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 bulkscore=0 spamscore=0 suspectscore=0 phishscore=0 mlxscore=0 adultscore=0 clxscore=1015 priorityscore=1501 lowpriorityscore=0 malwarescore=0 mlxlogscore=999 impostorscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2209130000 definitions=main-2210070058 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" The .pgsize_bitmap property of struct iommu_ops is not a page mask but rather has a bit set for each size of pages the IOMMU supports. As the comment correctly pointed out at this moment the code only support 4K pages so simply use SZ_4K here. Reviewed-by: Matthew Rosato Reviewed-by: Jason Gunthorpe Signed-off-by: Niklas Schnelle --- drivers/iommu/s390-iommu.c | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/drivers/iommu/s390-iommu.c b/drivers/iommu/s390-iommu.c index a4c6a1a63fef..1524f18f8523 100644 --- a/drivers/iommu/s390-iommu.c +++ b/drivers/iommu/s390-iommu.c @@ -12,13 +12,6 @@ #include #include =20 -/* - * Physically contiguous memory regions can be mapped with 4 KiB alignment, - * we allow all page sizes that are an order of 4KiB (no special large page - * support so far). - */ -#define S390_IOMMU_PGSIZES (~0xFFFUL) - static const struct iommu_ops s390_iommu_ops; =20 struct s390_domain { @@ -358,7 +351,7 @@ static const struct iommu_ops s390_iommu_ops =3D { .probe_device =3D s390_iommu_probe_device, .release_device =3D s390_iommu_release_device, .device_group =3D generic_device_group, - .pgsize_bitmap =3D S390_IOMMU_PGSIZES, + .pgsize_bitmap =3D SZ_4K, .get_resv_regions =3D s390_iommu_get_resv_regions, .default_domain_ops =3D &(const struct iommu_domain_ops) { .attach_dev =3D s390_iommu_attach_device, --=20 2.34.1 From nobody Mon Apr 6 22:48:47 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 06415C433F5 for ; Fri, 7 Oct 2022 09:50:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229665AbiJGJum (ORCPT ); Fri, 7 Oct 2022 05:50:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46678 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229620AbiJGJu2 (ORCPT ); Fri, 7 Oct 2022 05:50:28 -0400 Received: from mx0a-001b2d01.pphosted.com (mx0b-001b2d01.pphosted.com [148.163.158.5]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6C93D9B875; Fri, 7 Oct 2022 02:50:25 -0700 (PDT) Received: from pps.filterd (m0098419.ppops.net [127.0.0.1]) by mx0b-001b2d01.pphosted.com (8.17.1.5/8.17.1.5) with ESMTP id 2978WfnQ020298; Fri, 7 Oct 2022 09:50:14 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ibm.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-transfer-encoding; s=pp1; bh=Pnjt2OOO8qE1hDm1ninA2Mlu1FF+DFaOILjhwISHd1c=; b=TmTYf7q62NChp4bD6gOtX6ekvHLYOYXaSiahVhxNRHYcWkyhkfgy7J9hanvbkpUqQRfk B/EgMSJ1kuHNYc4kfaOvJ7+D4d8LfT+CCT7ELQZ8TDKZsywFwSvFoSCo5T7251cO/sdN ab+VeVuk3n2pNbi7QdrJYqsEpyhI0eJzmVDQyDQJVy3hImhrBDz1G9zQ/BuLrD3vTERH zwkTik2/mCKU28ExYxPV2erXjwS3fmpk7B/171qYeQNqu9/RDz8s+IM30+yCjku08Emw +BfxSwyDwxLx8/hGYduXz+NURGqWrH+S0GejG6NHcjCZdWkAZjCHqPuQcyd7+hSQmS+8 5Q== Received: from ppma01fra.de.ibm.com (46.49.7a9f.ip4.static.sl-reverse.com [159.122.73.70]) by mx0b-001b2d01.pphosted.com (PPS) with ESMTPS id 3k2gphjbqk-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Fri, 07 Oct 2022 09:50:14 +0000 Received: from pps.filterd (ppma01fra.de.ibm.com [127.0.0.1]) by ppma01fra.de.ibm.com (8.16.1.2/8.16.1.2) with SMTP id 2979ZwVY012633; Fri, 7 Oct 2022 09:50:12 GMT Received: from b06cxnps3074.portsmouth.uk.ibm.com (d06relay09.portsmouth.uk.ibm.com [9.149.109.194]) by ppma01fra.de.ibm.com with ESMTP id 3jxd68x6gp-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Fri, 07 Oct 2022 09:50:12 +0000 Received: from d06av23.portsmouth.uk.ibm.com (d06av23.portsmouth.uk.ibm.com [9.149.105.59]) by b06cxnps3074.portsmouth.uk.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 2979o9NM54919496 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 7 Oct 2022 09:50:09 GMT Received: from d06av23.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 5E913A4051; Fri, 7 Oct 2022 09:50:09 +0000 (GMT) Received: from d06av23.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id E4EAFA4040; Fri, 7 Oct 2022 09:50:08 +0000 (GMT) Received: from tuxmaker.boeblingen.de.ibm.com (unknown [9.152.85.9]) by d06av23.portsmouth.uk.ibm.com (Postfix) with ESMTP; Fri, 7 Oct 2022 09:50:08 +0000 (GMT) From: Niklas Schnelle To: Matthew Rosato , Pierre Morel , iommu@lists.linux.dev Cc: linux-s390@vger.kernel.org, borntraeger@linux.ibm.com, hca@linux.ibm.com, gor@linux.ibm.com, gerald.schaefer@linux.ibm.com, agordeev@linux.ibm.com, svens@linux.ibm.com, joro@8bytes.org, will@kernel.org, robin.murphy@arm.com, jgg@nvidia.com, linux-kernel@vger.kernel.org Subject: [PATCH v6 6/6] iommu/s390: Implement map_pages()/unmap_pages() instead of map()/unmap() Date: Fri, 7 Oct 2022 11:50:05 +0200 Message-Id: <20221007095005.2017126-7-schnelle@linux.ibm.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20221007095005.2017126-1-schnelle@linux.ibm.com> References: <20221007095005.2017126-1-schnelle@linux.ibm.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-TM-AS-GCONF: 00 X-Proofpoint-GUID: d35xkZ4Kh9R-Ndzkee94zGzqGOcByGsR X-Proofpoint-ORIG-GUID: d35xkZ4Kh9R-Ndzkee94zGzqGOcByGsR X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.205,Aquarius:18.0.895,Hydra:6.0.528,FMLib:17.11.122.1 definitions=2022-10-06_05,2022-10-06_02,2022-06-22_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 bulkscore=0 spamscore=0 suspectscore=0 phishscore=0 mlxscore=0 adultscore=0 clxscore=1015 priorityscore=1501 lowpriorityscore=0 malwarescore=0 mlxlogscore=944 impostorscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2209130000 definitions=main-2210070058 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" While s390-iommu currently implements the map_page()/unmap_page() operations which only map/unmap a single page at a time the internal s390_iommu_update_trans() API already supports mapping/unmapping a range of pages at once. Take advantage of this by implementing the map_pages()/unmap_pages() operations instead thus allowing users of the IOMMU drivers to map multiple pages in a single call followed by a single I/O TLB flush if needed. Reviewed-by: Matthew Rosato Signed-off-by: Niklas Schnelle --- v5->v6: - WARN_ON() aperture violation on .unmap_pages (Matt) drivers/iommu/s390-iommu.c | 48 +++++++++++++++++++++++++------------- 1 file changed, 32 insertions(+), 16 deletions(-) diff --git a/drivers/iommu/s390-iommu.c b/drivers/iommu/s390-iommu.c index 1524f18f8523..77acfad6f919 100644 --- a/drivers/iommu/s390-iommu.c +++ b/drivers/iommu/s390-iommu.c @@ -191,20 +191,15 @@ static void s390_iommu_release_device(struct device *= dev) =20 static int s390_iommu_update_trans(struct s390_domain *s390_domain, phys_addr_t pa, dma_addr_t dma_addr, - size_t size, int flags) + unsigned long nr_pages, int flags) { phys_addr_t page_addr =3D pa & PAGE_MASK; dma_addr_t start_dma_addr =3D dma_addr; - unsigned long irq_flags, nr_pages, i; + unsigned long irq_flags, i; struct zpci_dev *zdev; unsigned long *entry; int rc =3D 0; =20 - if (dma_addr < s390_domain->domain.geometry.aperture_start || - (dma_addr + size - 1) > s390_domain->domain.geometry.aperture_end) - return -EINVAL; - - nr_pages =3D PAGE_ALIGN(size) >> PAGE_SHIFT; if (!nr_pages) return 0; =20 @@ -247,11 +242,24 @@ static int s390_iommu_update_trans(struct s390_domain= *s390_domain, return rc; } =20 -static int s390_iommu_map(struct iommu_domain *domain, unsigned long iova, - phys_addr_t paddr, size_t size, int prot, gfp_t gfp) +static int s390_iommu_map_pages(struct iommu_domain *domain, + unsigned long iova, phys_addr_t paddr, + size_t pgsize, size_t pgcount, + int prot, gfp_t gfp, size_t *mapped) { struct s390_domain *s390_domain =3D to_s390_domain(domain); int flags =3D ZPCI_PTE_VALID, rc =3D 0; + size_t size =3D pgcount << __ffs(pgsize); + + if (pgsize !=3D SZ_4K) + return -EINVAL; + + if (iova < s390_domain->domain.geometry.aperture_start || + (iova + size - 1) > s390_domain->domain.geometry.aperture_end) + return -EINVAL; + + if (!IS_ALIGNED(iova | paddr, pgsize)) + return -EINVAL; =20 if (!(prot & IOMMU_READ)) return -EINVAL; @@ -260,7 +268,9 @@ static int s390_iommu_map(struct iommu_domain *domain, = unsigned long iova, flags |=3D ZPCI_TABLE_PROTECTED; =20 rc =3D s390_iommu_update_trans(s390_domain, paddr, iova, - size, flags); + pgcount, flags); + if (!rc) + *mapped =3D size; =20 return rc; } @@ -296,21 +306,27 @@ static phys_addr_t s390_iommu_iova_to_phys(struct iom= mu_domain *domain, return phys; } =20 -static size_t s390_iommu_unmap(struct iommu_domain *domain, - unsigned long iova, size_t size, - struct iommu_iotlb_gather *gather) +static size_t s390_iommu_unmap_pages(struct iommu_domain *domain, + unsigned long iova, + size_t pgsize, size_t pgcount, + struct iommu_iotlb_gather *gather) { struct s390_domain *s390_domain =3D to_s390_domain(domain); + size_t size =3D pgcount << __ffs(pgsize); int flags =3D ZPCI_PTE_INVALID; phys_addr_t paddr; int rc; =20 + if (WARN_ON(iova < s390_domain->domain.geometry.aperture_start || + (iova + size - 1) > s390_domain->domain.geometry.aperture_end)) + return 0; + paddr =3D s390_iommu_iova_to_phys(domain, iova); if (!paddr) return 0; =20 rc =3D s390_iommu_update_trans(s390_domain, paddr, iova, - size, flags); + pgcount, flags); if (rc) return 0; =20 @@ -356,8 +372,8 @@ static const struct iommu_ops s390_iommu_ops =3D { .default_domain_ops =3D &(const struct iommu_domain_ops) { .attach_dev =3D s390_iommu_attach_device, .detach_dev =3D s390_iommu_detach_device, - .map =3D s390_iommu_map, - .unmap =3D s390_iommu_unmap, + .map_pages =3D s390_iommu_map_pages, + .unmap_pages =3D s390_iommu_unmap_pages, .iova_to_phys =3D s390_iommu_iova_to_phys, .free =3D s390_domain_free, } --=20 2.34.1