From nobody Tue Apr 7 00:50:42 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 61ADAECAAD4 for ; Wed, 31 Aug 2022 20:12:57 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232265AbiHaUM4 (ORCPT ); Wed, 31 Aug 2022 16:12:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38518 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232221AbiHaUMv (ORCPT ); Wed, 31 Aug 2022 16:12:51 -0400 Received: from mx0a-001b2d01.pphosted.com (mx0a-001b2d01.pphosted.com [148.163.156.1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1F794C2774; Wed, 31 Aug 2022 13:12:51 -0700 (PDT) Received: from pps.filterd (m0098399.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.17.1.5/8.17.1.5) with ESMTP id 27VJoCwf022150; Wed, 31 Aug 2022 20:12:41 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=t2KcrvxT2E4IlMaQGptE2kb/DhxLdfg+hmehqxWfXWQ=; b=OB7B6i1W3RP/6435wRiND6spuels8MDVZiSXBiv68SjOjxjycKYmLNpdFjCTQMubfqOZ lHqzHnTdA1SRo/Jfi2P9OwTZRDa7mIcGIs2d2+kaimSQneiBO9/uas38mWqKtWnhRJN6 t9+r6g+PZ66Kxuzl1f9xE78pK4JI8AOCwFHSgQhxqFt3hBh+TNA6a429KL5YvlOExhia 0FPAmIC5/jDcNKXV+k5lTd8BZ5NRTo3oJDasbXhs3H6Vo0YGazSHA4dA1Nb9hrboDU36 WpXXURW7rziw8imvqrqYavF8KKZG2lG1WUhUD0mfskrtb8FTLwvfIIAJW0Z2qKlhV0nW 6g== Received: from ppma04dal.us.ibm.com (7a.29.35a9.ip4.static.sl-reverse.com [169.53.41.122]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 3jae518vfq-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Wed, 31 Aug 2022 20:12:41 +0000 Received: from pps.filterd (ppma04dal.us.ibm.com [127.0.0.1]) by ppma04dal.us.ibm.com (8.16.1.2/8.16.1.2) with SMTP id 27VK5GrM019094; Wed, 31 Aug 2022 20:12:40 GMT Received: from b01cxnp23034.gho.pok.ibm.com (b01cxnp23034.gho.pok.ibm.com [9.57.198.29]) by ppma04dal.us.ibm.com with ESMTP id 3j7awa3q11-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Wed, 31 Aug 2022 20:12:40 +0000 Received: from b01ledav006.gho.pok.ibm.com (b01ledav006.gho.pok.ibm.com [9.57.199.111]) by b01cxnp23034.gho.pok.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 27VKCdTs61407552 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 31 Aug 2022 20:12:39 GMT Received: from b01ledav006.gho.pok.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id F22FAAC059; Wed, 31 Aug 2022 20:12:38 +0000 (GMT) Received: from b01ledav006.gho.pok.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 3B3EDAC060; Wed, 31 Aug 2022 20:12:38 +0000 (GMT) Received: from li-2311da4c-2e09-11b2-a85c-c003041e9174.ibm.com.com (unknown [9.160.77.30]) by b01ledav006.gho.pok.ibm.com (Postfix) with ESMTP; Wed, 31 Aug 2022 20:12:38 +0000 (GMT) From: Matthew Rosato To: iommu@lists.linux.dev Cc: linux-s390@vger.kernel.org, schnelle@linux.ibm.com, pmorel@linux.ibm.com, 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 v4 1/2] iommu/s390: Fix race with release_device ops Date: Wed, 31 Aug 2022 16:12:35 -0400 Message-Id: <20220831201236.77595-2-mjrosato@linux.ibm.com> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20220831201236.77595-1-mjrosato@linux.ibm.com> References: <20220831201236.77595-1-mjrosato@linux.ibm.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-TM-AS-GCONF: 00 X-Proofpoint-ORIG-GUID: d4woSmgW6jzLnJeicPHVgzEci5xEpaUA X-Proofpoint-GUID: d4woSmgW6jzLnJeicPHVgzEci5xEpaUA X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.205,Aquarius:18.0.895,Hydra:6.0.517,FMLib:17.11.122.1 definitions=2022-08-31_12,2022-08-31_03,2022-06-22_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 impostorscore=0 lowpriorityscore=0 mlxscore=0 malwarescore=0 clxscore=1015 mlxlogscore=999 spamscore=0 phishscore=0 bulkscore=0 adultscore=0 suspectscore=0 priorityscore=1501 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2207270000 definitions=main-2208310097 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" With commit fa7e9ecc5e1c ("iommu/s390: Tolerate repeat attach_dev calls") s390-iommu is supposed to handle dynamic switching between IOMMU domains and the DMA API handling. However, this commit does not sufficiently handle the case where the device is released via a call to the release_device op as it may occur at the same time as an opposing attach_dev or detach_dev since the group mutex is not held over release_device. This was observed if the device is deconfigured during a small window during vfio-pci initialization and can result in WARNs and potential kernel panics. Handle this by tracking when the device is probed/released via dev_iommu_priv_set/get(). Ensure that once the device is released only release_device handles the re-init of the device DMA. Fixes: fa7e9ecc5e1c ("iommu/s390: Tolerate repeat attach_dev calls") Signed-off-by: Matthew Rosato --- arch/s390/include/asm/pci.h | 1 + arch/s390/pci/pci.c | 1 + drivers/iommu/s390-iommu.c | 39 ++++++++++++++++++++++++++++++++++--- 3 files changed, 38 insertions(+), 3 deletions(-) diff --git a/arch/s390/include/asm/pci.h b/arch/s390/include/asm/pci.h index 7b4cdadbc023..080251e7b275 100644 --- a/arch/s390/include/asm/pci.h +++ b/arch/s390/include/asm/pci.h @@ -157,6 +157,7 @@ struct zpci_dev { /* DMA stuff */ unsigned long *dma_table; spinlock_t dma_table_lock; + struct mutex dma_domain_lock; /* protects s390_domain value */ int tlb_refresh; =20 spinlock_t iommu_bitmap_lock; diff --git a/arch/s390/pci/pci.c b/arch/s390/pci/pci.c index 73cdc5539384..973edd32ecc9 100644 --- a/arch/s390/pci/pci.c +++ b/arch/s390/pci/pci.c @@ -832,6 +832,7 @@ struct zpci_dev *zpci_create_device(u32 fid, u32 fh, en= um zpci_state state) kref_init(&zdev->kref); mutex_init(&zdev->lock); mutex_init(&zdev->kzdev_lock); + mutex_init(&zdev->dma_domain_lock); =20 rc =3D zpci_init_iommu(zdev); if (rc) diff --git a/drivers/iommu/s390-iommu.c b/drivers/iommu/s390-iommu.c index c898bcbbce11..1137d669e849 100644 --- a/drivers/iommu/s390-iommu.c +++ b/drivers/iommu/s390-iommu.c @@ -99,6 +99,14 @@ static int s390_iommu_attach_device(struct iommu_domain = *domain, if (!domain_device) return -ENOMEM; =20 + /* Leave now if the device has already been released */ + mutex_lock(&zdev->dma_domain_lock); + if (!dev_iommu_priv_get(dev)) { + mutex_unlock(&zdev->dma_domain_lock); + kfree(domain_device); + return 0; + } + if (zdev->dma_table && !zdev->s390_domain) { cc =3D zpci_dma_exit_device(zdev); if (cc) { @@ -132,9 +140,10 @@ static int s390_iommu_attach_device(struct iommu_domai= n *domain, goto out_restore; } domain_device->zdev =3D zdev; - zdev->s390_domain =3D s390_domain; list_add(&domain_device->list, &s390_domain->devices); spin_unlock_irqrestore(&s390_domain->list_lock, flags); + zdev->s390_domain =3D s390_domain; + mutex_unlock(&zdev->dma_domain_lock); =20 return 0; =20 @@ -147,6 +156,7 @@ static int s390_iommu_attach_device(struct iommu_domain= *domain, virt_to_phys(zdev->dma_table)); } out_free: + mutex_unlock(&zdev->dma_domain_lock); kfree(domain_device); =20 return rc; @@ -176,17 +186,22 @@ static void s390_iommu_detach_device(struct iommu_dom= ain *domain, } spin_unlock_irqrestore(&s390_domain->list_lock, flags); =20 - if (found && (zdev->s390_domain =3D=3D s390_domain)) { + mutex_lock(&zdev->dma_domain_lock); + if (found && (zdev->s390_domain =3D=3D s390_domain) && + dev_iommu_priv_get(dev)) { zdev->s390_domain =3D NULL; zpci_unregister_ioat(zdev, 0); zpci_dma_init_device(zdev); } + mutex_unlock(&zdev->dma_domain_lock); } =20 static struct iommu_device *s390_iommu_probe_device(struct device *dev) { struct zpci_dev *zdev =3D to_zpci_dev(dev); =20 + dev_iommu_priv_set(dev, zdev); + return &zdev->iommu_dev; } =20 @@ -206,10 +221,28 @@ static void s390_iommu_release_device(struct device *= dev) * * So let's call detach_dev from here if it hasn't been called before. */ - if (zdev && zdev->s390_domain) { + if (zdev) { + /* + * Clear priv to block further attaches for this device, + * ensure detaches don't init DMA. Hold the domain lock + * to ensure that attach/detach get a consistent view of + * whether or not the device is released. + */ + mutex_lock(&zdev->dma_domain_lock); + dev_iommu_priv_set(dev, NULL); + mutex_unlock(&zdev->dma_domain_lock); + /* Make sure this device is removed from the domain list */ domain =3D iommu_get_domain_for_dev(dev); if (domain) s390_iommu_detach_device(domain, dev); + /* Now ensure DMA is initialized from here */ + mutex_lock(&zdev->dma_domain_lock); + if (zdev->s390_domain) { + zdev->s390_domain =3D NULL; + zpci_unregister_ioat(zdev, 0); + zpci_dma_init_device(zdev); + } + mutex_unlock(&zdev->dma_domain_lock); } } =20 --=20 2.37.2 From nobody Tue Apr 7 00:50:42 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 8214FECAAD4 for ; Wed, 31 Aug 2022 20:13:02 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232283AbiHaUNB (ORCPT ); Wed, 31 Aug 2022 16:13:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38524 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232228AbiHaUMw (ORCPT ); Wed, 31 Aug 2022 16:12:52 -0400 Received: from mx0a-001b2d01.pphosted.com (mx0a-001b2d01.pphosted.com [148.163.156.1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 247F9286CE; Wed, 31 Aug 2022 13:12:52 -0700 (PDT) Received: from pps.filterd (m0187473.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.17.1.5/8.17.1.5) with ESMTP id 27VK03Cv009124; Wed, 31 Aug 2022 20:12:42 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=8UaAcdsSep4uV8uaXMURFWomBBOpmEtS8j6S/xj9pgg=; b=Rb9GNk9RjeyndqN8mYbDn1X1uq0wCMKYgBjS4y1AZ1IqjYl+XS2W4fUrt+1YVYPERLi5 xbsCxNtYdtsEiwDLSXniIssRD+0J19u0bjS09Y7YLI9+RkCX/cheWQWb+IMHptIZJ8U8 8s9DyjlEh3pfX6zXa5w4oo7lAieu2nZoREd+kB4wZl2FOZ7LV07J+OLczI/9H4JpQDv0 AIrcT19C/xPii9HM6LW4pGrGqoiFYxqJamwd/zRySDmVynKPn7KLM58Hj/PQVuf3LiGx yR8fE/1Mh5gV70G+q4dwDiwPreZQBTVZLfMruBL3IKGjgSONY3VJxkFyCmEZgbmowGBq Dg== Received: from ppma05wdc.us.ibm.com (1b.90.2fa9.ip4.static.sl-reverse.com [169.47.144.27]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 3jae9qgn6h-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Wed, 31 Aug 2022 20:12:41 +0000 Received: from pps.filterd (ppma05wdc.us.ibm.com [127.0.0.1]) by ppma05wdc.us.ibm.com (8.16.1.2/8.16.1.2) with SMTP id 27VK65I2030788; Wed, 31 Aug 2022 20:12:40 GMT Received: from b01cxnp23034.gho.pok.ibm.com (b01cxnp23034.gho.pok.ibm.com [9.57.198.29]) by ppma05wdc.us.ibm.com with ESMTP id 3j7aw9vkft-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Wed, 31 Aug 2022 20:12:40 +0000 Received: from b01ledav006.gho.pok.ibm.com (b01ledav006.gho.pok.ibm.com [9.57.199.111]) by b01cxnp23034.gho.pok.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 27VKCdib39584230 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 31 Aug 2022 20:12:39 GMT Received: from b01ledav006.gho.pok.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id CC155AC059; Wed, 31 Aug 2022 20:12:39 +0000 (GMT) Received: from b01ledav006.gho.pok.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 1333FAC05F; Wed, 31 Aug 2022 20:12:39 +0000 (GMT) Received: from li-2311da4c-2e09-11b2-a85c-c003041e9174.ibm.com.com (unknown [9.160.77.30]) by b01ledav006.gho.pok.ibm.com (Postfix) with ESMTP; Wed, 31 Aug 2022 20:12:38 +0000 (GMT) From: Matthew Rosato To: iommu@lists.linux.dev Cc: linux-s390@vger.kernel.org, schnelle@linux.ibm.com, pmorel@linux.ibm.com, 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 v4 2/2] iommu/s390: fix leak of s390_domain_device Date: Wed, 31 Aug 2022 16:12:36 -0400 Message-Id: <20220831201236.77595-3-mjrosato@linux.ibm.com> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20220831201236.77595-1-mjrosato@linux.ibm.com> References: <20220831201236.77595-1-mjrosato@linux.ibm.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-TM-AS-GCONF: 00 X-Proofpoint-ORIG-GUID: QHMWXiIgYlc2TrNwErWCpsynqGM0Hb-3 X-Proofpoint-GUID: QHMWXiIgYlc2TrNwErWCpsynqGM0Hb-3 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.205,Aquarius:18.0.895,Hydra:6.0.517,FMLib:17.11.122.1 definitions=2022-08-31_12,2022-08-31_03,2022-06-22_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 lowpriorityscore=0 clxscore=1015 suspectscore=0 mlxlogscore=999 impostorscore=0 mlxscore=0 priorityscore=1501 adultscore=0 spamscore=0 bulkscore=0 malwarescore=0 phishscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2207270000 definitions=main-2208310097 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Since allowing multiple domains to be attached via fa7e9ecc5e1c, it's now possible that a kfree of s390_domain_device is missed, either because a corresponding detach_dev was never called or because a repeat attach_dev was called for the same device and domain pair (resulting in unnecessary duplicates). Check for duplicates during attach_dev and ensure the list of s390_domain_device structures is cleared up when the domain is freed. Fixes: fa7e9ecc5e1c ("iommu/s390: Tolerate repeat attach_dev calls") Signed-off-by: Matthew Rosato --- drivers/iommu/s390-iommu.c | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/drivers/iommu/s390-iommu.c b/drivers/iommu/s390-iommu.c index 1137d669e849..db4dfbcf161b 100644 --- a/drivers/iommu/s390-iommu.c +++ b/drivers/iommu/s390-iommu.c @@ -78,7 +78,17 @@ static struct iommu_domain *s390_domain_alloc(unsigned d= omain_type) static void s390_domain_free(struct iommu_domain *domain) { struct s390_domain *s390_domain =3D to_s390_domain(domain); + struct s390_domain_device *domain_device, *tmp; + unsigned long flags; =20 + /* Ensure all device entries are cleaned up */ + spin_lock_irqsave(&s390_domain->list_lock, flags); + list_for_each_entry_safe(domain_device, tmp, &s390_domain->devices, + list) { + list_del(&domain_device->list); + kfree(domain_device); + } + spin_unlock_irqrestore(&s390_domain->list_lock, flags); dma_cleanup_tables(s390_domain->dma_table); kfree(s390_domain); } @@ -88,7 +98,7 @@ static int s390_iommu_attach_device(struct iommu_domain *= 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; + struct s390_domain_device *domain_device, *ddev; unsigned long flags; int cc, rc; =20 @@ -140,7 +150,16 @@ static int s390_iommu_attach_device(struct iommu_domai= n *domain, goto out_restore; } domain_device->zdev =3D zdev; - list_add(&domain_device->list, &s390_domain->devices); + /* If already attached don't add another instance */ + list_for_each_entry(ddev, &s390_domain->devices, list) { + if (ddev->zdev =3D=3D zdev) { + kfree(domain_device); + domain_device =3D NULL; + break; + } + } + if (domain_device) + list_add(&domain_device->list, &s390_domain->devices); spin_unlock_irqrestore(&s390_domain->list_lock, flags); zdev->s390_domain =3D s390_domain; mutex_unlock(&zdev->dma_domain_lock); --=20 2.37.2