From nobody Fri Dec 19 01:39:19 2025 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 947E3C54EE9 for ; Tue, 13 Sep 2022 14:30:34 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233928AbiIMOaa (ORCPT ); Tue, 13 Sep 2022 10:30:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55710 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233866AbiIMO2g (ORCPT ); Tue, 13 Sep 2022 10:28:36 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5515369F43; Tue, 13 Sep 2022 07:17:58 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 66CA7614C9; Tue, 13 Sep 2022 14:16:25 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 74E03C433C1; Tue, 13 Sep 2022 14:16:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1663078584; bh=U8UxvLJjoGv1JsvME3fKk8Cxt4aR5PN71cGlnevRH3I=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=b8kv6MHR+x+Stuv/Ubg5/D9e+r+4Xe3a+L089Z0qTtUdOdBtxm4JualwKP3L77Mtn 6IWsY9+zxr7/uQdknqGD4Nm3n4qM/mkyv5W1tvSSnqkZD8wn6sARCMcASwzvFGaXxb hLmJ1GAqpEU4O4BpOPSzyBkK2LEnvzRoEoo88gBA= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Robin Murphy , Takashi Iwai , Jason Gunthorpe , Joerg Roedel Subject: [PATCH 5.19 190/192] iommu: Fix false ownership failure on AMD systems with PASID activated Date: Tue, 13 Sep 2022 16:04:56 +0200 Message-Id: <20220913140419.535249247@linuxfoundation.org> X-Mailer: git-send-email 2.37.3 In-Reply-To: <20220913140410.043243217@linuxfoundation.org> References: <20220913140410.043243217@linuxfoundation.org> User-Agent: quilt/0.67 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" From: Jason Gunthorpe commit 2380f1e8195ef612deea1dc7a3d611c5d2b9b56a upstream. The AMD IOMMU driver cannot activate PASID mode on a RID without the RID's translation being set to IDENTITY. Further it requires changing the RID's page table layout from the normal v1 IOMMU_DOMAIN_IDENTITY layout to a different v2 layout. It does this by creating a new iommu_domain, configuring that domain for v2 identity operation and then attaching it to the group, from within the driver. This logic assumes the group is already set to the IDENTITY domain and is being used by the DMA API. However, since the ownership logic is based on the group's domain pointer equaling the default domain to detect DMA API ownership, this causes it to look like the group is not attached to the DMA API any more. This blocks attaching drivers to any other devices in the group. In a real system this manifests itself as the HD-audio devices on some AMD platforms losing their device drivers. Work around this unique behavior of the AMD driver by checking for equality of IDENTITY domains based on their type, not their pointer value. This allows the AMD driver to have two IDENTITY domains for internal purposes without breaking the check. Have the AMD driver properly declare that the special domain it created is actually an IDENTITY domain. Cc: Robin Murphy Cc: stable@vger.kernel.org Fixes: 512881eacfa7 ("bus: platform,amba,fsl-mc,PCI: Add device DMA ownersh= ip management") Reported-by: Takashi Iwai Tested-by: Takashi Iwai Signed-off-by: Jason Gunthorpe Reviewed-by: Robin Murphy Link: https://lore.kernel.org/r/0-v1-ea566e16b06b+811-amd_owner_jgg@nvidia.= com Signed-off-by: Joerg Roedel Signed-off-by: Greg Kroah-Hartman --- drivers/iommu/amd/iommu_v2.c | 2 ++ drivers/iommu/iommu.c | 21 +++++++++++++++++++-- 2 files changed, 21 insertions(+), 2 deletions(-) --- a/drivers/iommu/amd/iommu_v2.c +++ b/drivers/iommu/amd/iommu_v2.c @@ -786,6 +786,8 @@ int amd_iommu_init_device(struct pci_dev if (dev_state->domain =3D=3D NULL) goto out_free_states; =20 + /* See iommu_is_default_domain() */ + dev_state->domain->type =3D IOMMU_DOMAIN_IDENTITY; amd_iommu_domain_direct_map(dev_state->domain); =20 ret =3D amd_iommu_domain_enable_v2(dev_state->domain, pasids); --- a/drivers/iommu/iommu.c +++ b/drivers/iommu/iommu.c @@ -3089,6 +3089,24 @@ out: return ret; } =20 +static bool iommu_is_default_domain(struct iommu_group *group) +{ + if (group->domain =3D=3D group->default_domain) + return true; + + /* + * If the default domain was set to identity and it is still an identity + * domain then we consider this a pass. This happens because of + * amd_iommu_init_device() replacing the default idenytity domain with an + * identity domain that has a different configuration for AMDGPU. + */ + if (group->default_domain && + group->default_domain->type =3D=3D IOMMU_DOMAIN_IDENTITY && + group->domain && group->domain->type =3D=3D IOMMU_DOMAIN_IDENTITY) + return true; + return false; +} + /** * iommu_device_use_default_domain() - Device driver wants to handle device * DMA through the kernel DMA API. @@ -3107,8 +3125,7 @@ int iommu_device_use_default_domain(stru =20 mutex_lock(&group->mutex); if (group->owner_cnt) { - if (group->domain !=3D group->default_domain || - group->owner) { + if (group->owner || !iommu_is_default_domain(group)) { ret =3D -EBUSY; goto unlock_out; }