From nobody Fri Feb 13 13:24:41 2026 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.21]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 38B42D517 for ; Mon, 27 May 2024 04:07:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.21 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716782870; cv=none; b=kZ1+KK9jGs+2klXCfAH2THwDgp6F4CpQtDB5c0DSn8is91r4TW63k3iKdsh3+hlOJWvWsaElLBkmD3Q/ao8nnlo1h95DQBq0Vjrg4/WpXYX4kU8IVGwtSbx7wAh6cJR9WB6e+F8jTAwcniox67+NkFvM2QIOuaQfYKYmIkVSKs8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716782870; c=relaxed/simple; bh=J9wCRtxD0gbdHyIEhv/YJB4bZC0qR/j8eeGR5NVu5qM=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=SphnAnTLwNrysHirVFWxMXYEoMbe3HJEwLonJoGBKpU8mMogBxGGVmzNbgTC6a2bzdTfFy7I2tjxhA2kT+8ZH+STHVpWxuLW8RnvNQkVhITfLLV0VZ2RxAPHgk6HB71Go0FDd2yyqWNwaOyXXPi9pXk6Kw7qZZp9WvhTjLKDeVw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=none smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=GyteMkrp; arc=none smtp.client-ip=198.175.65.21 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="GyteMkrp" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1716782868; x=1748318868; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=J9wCRtxD0gbdHyIEhv/YJB4bZC0qR/j8eeGR5NVu5qM=; b=GyteMkrpFX4ZDTkR5caKqqHLPsYY++w6784ocA5ztCwgchPX48groMY1 zsr60uuTtc1xWSZkFwQgj6l7KnQ9w87W57GnanNlovtp7ZAxOYhYG3D/H ohJuXuBhKIvlbG/y0V3RwmyrUm9LmS5dK1Catm9CoHBdgsY1uYd66JnAu ffkr+L0fpNl8r5VP4p8l7FKU3SsBYrssRYC3uWMoYjfmF13+vo80qiBhW 1yNIuI0AjtrXN533mqJ5RZ3DEcusu2NibAa/XkDB71wH23oNAprfvy2cm /tukXB+XGu1C4pG7vUjichumHlrTrH7AGsfoBfLtL+6MQ/70Rm0Q0VvkC w==; X-CSE-ConnectionGUID: 5ncr6yApSOanoz3HwZE+/A== X-CSE-MsgGUID: JdJ4EXF3QvGqbI2BUBv11A== X-IronPort-AV: E=McAfee;i="6600,9927,11084"; a="13022662" X-IronPort-AV: E=Sophos;i="6.08,191,1712646000"; d="scan'208";a="13022662" Received: from orviesa003.jf.intel.com ([10.64.159.143]) by orvoesa113.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 May 2024 21:07:29 -0700 X-CSE-ConnectionGUID: KFSkJIPGShaByYscKSv2uw== X-CSE-MsgGUID: AmXwEEv/S3+dzE7OfZJ60Q== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.08,191,1712646000"; d="scan'208";a="39399874" Received: from unknown (HELO allen-box.sh.intel.com) ([10.239.159.127]) by orviesa003.jf.intel.com with ESMTP; 26 May 2024 21:07:25 -0700 From: Lu Baolu To: Jason Gunthorpe , Kevin Tian , Joerg Roedel , Will Deacon , Robin Murphy , Jean-Philippe Brucker , Nicolin Chen , Yi Liu , Jacob Pan , Joel Granados Cc: iommu@lists.linux.dev, virtualization@lists.linux-foundation.org, linux-kernel@vger.kernel.org, Lu Baolu , Jason Gunthorpe Subject: [PATCH v6 01/10] iommu: Introduce domain attachment handle Date: Mon, 27 May 2024 12:05:08 +0800 Message-Id: <20240527040517.38561-2-baolu.lu@linux.intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240527040517.38561-1-baolu.lu@linux.intel.com> References: <20240527040517.38561-1-baolu.lu@linux.intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Currently, when attaching a domain to a device or its PASID, domain is stored within the iommu group. It could be retrieved for use during the window between attachment and detachment. With new features introduced, there's a need to store more information than just a domain pointer. This information essentially represents the association between a domain and a device. For example, the SVA code already has a custom struct iommu_sva which represents a bond between sva domain and a PASID of a device. Looking forward, the IOMMUFD needs a place to store the iommufd_device pointer in the core, so that the device object ID could be quickly retrieved in the critical fault handling path. Introduce domain attachment handle that explicitly represents the attachment relationship between a domain and a device or its PASID. Co-developed-by: Jason Gunthorpe Signed-off-by: Jason Gunthorpe Signed-off-by: Lu Baolu --- include/linux/iommu.h | 18 +++++++++++++++--- drivers/dma/idxd/init.c | 2 +- drivers/iommu/iommu-sva.c | 13 ++++++++----- drivers/iommu/iommu.c | 23 +++++++++++++---------- 4 files changed, 37 insertions(+), 19 deletions(-) diff --git a/include/linux/iommu.h b/include/linux/iommu.h index 7bc8dff7cf6d..b89404acacd6 100644 --- a/include/linux/iommu.h +++ b/include/linux/iommu.h @@ -989,12 +989,22 @@ struct iommu_fwspec { /* ATS is supported */ #define IOMMU_FWSPEC_PCI_RC_ATS (1 << 0) =20 +/* + * An iommu attach handle represents a relationship between an iommu domain + * and a PASID or RID of a device. It is allocated and managed by the comp= onent + * that manages the domain and is stored in the iommu group during the tim= e the + * domain is attached. + */ +struct iommu_attach_handle { + struct iommu_domain *domain; +}; + /** * struct iommu_sva - handle to a device-mm bond */ struct iommu_sva { + struct iommu_attach_handle handle; struct device *dev; - struct iommu_domain *domain; struct list_head handle_item; refcount_t users; }; @@ -1052,7 +1062,8 @@ int iommu_device_claim_dma_owner(struct device *dev, = void *owner); void iommu_device_release_dma_owner(struct device *dev); =20 int iommu_attach_device_pasid(struct iommu_domain *domain, - struct device *dev, ioasid_t pasid); + struct device *dev, ioasid_t pasid, + struct iommu_attach_handle *handle); void iommu_detach_device_pasid(struct iommu_domain *domain, struct device *dev, ioasid_t pasid); struct iommu_domain * @@ -1388,7 +1399,8 @@ static inline int iommu_device_claim_dma_owner(struct= device *dev, void *owner) } =20 static inline int iommu_attach_device_pasid(struct iommu_domain *domain, - struct device *dev, ioasid_t pasid) + struct device *dev, ioasid_t pasid, + struct iommu_attach_handle *handle) { return -ENODEV; } diff --git a/drivers/dma/idxd/init.c b/drivers/dma/idxd/init.c index a7295943fa22..385c488c9cd1 100644 --- a/drivers/dma/idxd/init.c +++ b/drivers/dma/idxd/init.c @@ -584,7 +584,7 @@ static int idxd_enable_system_pasid(struct idxd_device = *idxd) * DMA domain is owned by the driver, it should support all valid * types such as DMA-FQ, identity, etc. */ - ret =3D iommu_attach_device_pasid(domain, dev, pasid); + ret =3D iommu_attach_device_pasid(domain, dev, pasid, NULL); if (ret) { dev_err(dev, "failed to attach device pasid %d, domain type %d", pasid, domain->type); diff --git a/drivers/iommu/iommu-sva.c b/drivers/iommu/iommu-sva.c index 18a35e798b72..0fb923254062 100644 --- a/drivers/iommu/iommu-sva.c +++ b/drivers/iommu/iommu-sva.c @@ -99,7 +99,9 @@ struct iommu_sva *iommu_sva_bind_device(struct device *de= v, struct mm_struct *mm =20 /* Search for an existing domain. */ list_for_each_entry(domain, &mm->iommu_mm->sva_domains, next) { - ret =3D iommu_attach_device_pasid(domain, dev, iommu_mm->pasid); + handle->handle.domain =3D domain; + ret =3D iommu_attach_device_pasid(domain, dev, iommu_mm->pasid, + &handle->handle); if (!ret) { domain->users++; goto out; @@ -113,7 +115,9 @@ struct iommu_sva *iommu_sva_bind_device(struct device *= dev, struct mm_struct *mm goto out_free_handle; } =20 - ret =3D iommu_attach_device_pasid(domain, dev, iommu_mm->pasid); + handle->handle.domain =3D domain; + ret =3D iommu_attach_device_pasid(domain, dev, iommu_mm->pasid, + &handle->handle); if (ret) goto out_free_domain; domain->users =3D 1; @@ -124,7 +128,6 @@ struct iommu_sva *iommu_sva_bind_device(struct device *= dev, struct mm_struct *mm list_add(&handle->handle_item, &mm->iommu_mm->sva_handles); mutex_unlock(&iommu_sva_lock); handle->dev =3D dev; - handle->domain =3D domain; return handle; =20 out_free_domain: @@ -147,7 +150,7 @@ EXPORT_SYMBOL_GPL(iommu_sva_bind_device); */ void iommu_sva_unbind_device(struct iommu_sva *handle) { - struct iommu_domain *domain =3D handle->domain; + struct iommu_domain *domain =3D handle->handle.domain; struct iommu_mm_data *iommu_mm =3D domain->mm->iommu_mm; struct device *dev =3D handle->dev; =20 @@ -170,7 +173,7 @@ EXPORT_SYMBOL_GPL(iommu_sva_unbind_device); =20 u32 iommu_sva_get_pasid(struct iommu_sva *handle) { - struct iommu_domain *domain =3D handle->domain; + struct iommu_domain *domain =3D handle->handle.domain; =20 return mm_get_enqcmd_pasid(domain->mm); } diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c index 9df7cc75c1bc..efd281ddfa63 100644 --- a/drivers/iommu/iommu.c +++ b/drivers/iommu/iommu.c @@ -3352,16 +3352,17 @@ static void __iommu_remove_group_pasid(struct iommu= _group *group, * @domain: the iommu domain. * @dev: the attached device. * @pasid: the pasid of the device. + * @handle: the attach handle. * * Return: 0 on success, or an error. */ int iommu_attach_device_pasid(struct iommu_domain *domain, - struct device *dev, ioasid_t pasid) + struct device *dev, ioasid_t pasid, + struct iommu_attach_handle *handle) { /* Caller must be a probed driver on dev */ struct iommu_group *group =3D dev->iommu_group; struct group_device *device; - void *curr; int ret; =20 if (!domain->ops->set_dev_pasid) @@ -3382,11 +3383,9 @@ int iommu_attach_device_pasid(struct iommu_domain *d= omain, } } =20 - curr =3D xa_cmpxchg(&group->pasid_array, pasid, NULL, domain, GFP_KERNEL); - if (curr) { - ret =3D xa_err(curr) ? : -EBUSY; + ret =3D xa_insert(&group->pasid_array, pasid, handle, GFP_KERNEL); + if (ret) goto out_unlock; - } =20 ret =3D __iommu_set_group_pasid(domain, group, pasid); if (ret) @@ -3414,7 +3413,7 @@ void iommu_detach_device_pasid(struct iommu_domain *d= omain, struct device *dev, =20 mutex_lock(&group->mutex); __iommu_remove_group_pasid(group, pasid, domain); - WARN_ON(xa_erase(&group->pasid_array, pasid) !=3D domain); + xa_erase(&group->pasid_array, pasid); mutex_unlock(&group->mutex); } EXPORT_SYMBOL_GPL(iommu_detach_device_pasid); @@ -3439,15 +3438,19 @@ struct iommu_domain *iommu_get_domain_for_dev_pasid= (struct device *dev, { /* Caller must be a probed driver on dev */ struct iommu_group *group =3D dev->iommu_group; - struct iommu_domain *domain; + struct iommu_attach_handle *handle; + struct iommu_domain *domain =3D NULL; =20 if (!group) return NULL; =20 xa_lock(&group->pasid_array); - domain =3D xa_load(&group->pasid_array, pasid); + handle =3D xa_load(&group->pasid_array, pasid); + if (handle) + domain =3D handle->domain; + if (type && domain && domain->type !=3D type) - domain =3D ERR_PTR(-EBUSY); + domain =3D NULL; xa_unlock(&group->pasid_array); =20 return domain; --=20 2.34.1 From nobody Fri Feb 13 13:24:41 2026 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.21]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 68D8BDDA5 for ; Mon, 27 May 2024 04:07:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.21 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716782873; cv=none; b=Iq4lszMNqhO8DsLKIruaBvXzvj51uI8CaxVvp2kbk9ewcJYRZBBs0sXd52c0lSpl8Wkk4Y2ejl6OuU2rLq8Zb2XoS/NE2WEH+Npvr0TGwDRegWTWexS5ux2/rF3I9qelHCSG7Y2+vTKxW7GOhT8zJvGPSis8U/OXViU5FomM0oU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716782873; c=relaxed/simple; bh=LS8wZ9NUK/jLPQzP1An5gXUR6Pde7W4oWkfEMwi5Ipk=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=jC2YXdRTH8dy2lA2cEaHzaXVNhjyLkQW4yTDUukxl/D4QiRCW/nDZEFEBdx3ucg2bhyDMuWiQiIYpljZI39PVO+ixMQhmyXqZ/X467qY+U5Pl2CKoOsEheYuxQrShhrFMf116wSN+aUrF2QJJNY10WHXnRLMolHJpVW6uFmVLQY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=none smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=Xu19mPZQ; arc=none smtp.client-ip=198.175.65.21 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="Xu19mPZQ" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1716782872; x=1748318872; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=LS8wZ9NUK/jLPQzP1An5gXUR6Pde7W4oWkfEMwi5Ipk=; b=Xu19mPZQPUmLJpBDEqhWdgTA09mzD7RwkzETs6ce42Bh/1sG8NYcpAi7 NhjFSFks0B6UXlPR/doBAR5Lh1eUiVlvixb2ViObn6zTNd1rMnQPDTy0q QWrSPKLRZOMtvc+TCeN82YzhOQ5vjHGFul1wmpb7fLhUPcHRh/kqMfIVl o1L2cegQn/cY6mL/iAUxcxfhSIrES8iRwRVV9nCoKGEGS3UraPkKsQbKu a0cVuvWDTtrIe/OiB24hJNxGnpslbqcNXw9S9hZz6J2UMJkIGDHDzvPOU sk8cLEntN7s649TuYFSPOSp6AoWh9dh8SOLMAXrf1qsb90BPDnV4VqraN w==; X-CSE-ConnectionGUID: TyKrDBgRQKW7fv5i0HRIDA== X-CSE-MsgGUID: 6NJS6u+iTFCwJb+QpxmwzQ== X-IronPort-AV: E=McAfee;i="6600,9927,11084"; a="13022699" X-IronPort-AV: E=Sophos;i="6.08,191,1712646000"; d="scan'208";a="13022699" Received: from orviesa003.jf.intel.com ([10.64.159.143]) by orvoesa113.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 May 2024 21:07:32 -0700 X-CSE-ConnectionGUID: lpY7jq8iRQKFkAjWzFWuFg== X-CSE-MsgGUID: drBZuryTSBm7GdeBX/FEYQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.08,191,1712646000"; d="scan'208";a="39399888" Received: from unknown (HELO allen-box.sh.intel.com) ([10.239.159.127]) by orviesa003.jf.intel.com with ESMTP; 26 May 2024 21:07:29 -0700 From: Lu Baolu To: Jason Gunthorpe , Kevin Tian , Joerg Roedel , Will Deacon , Robin Murphy , Jean-Philippe Brucker , Nicolin Chen , Yi Liu , Jacob Pan , Joel Granados Cc: iommu@lists.linux.dev, virtualization@lists.linux-foundation.org, linux-kernel@vger.kernel.org, Lu Baolu Subject: [PATCH v6 02/10] iommu: Remove sva handle list Date: Mon, 27 May 2024 12:05:09 +0800 Message-Id: <20240527040517.38561-3-baolu.lu@linux.intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240527040517.38561-1-baolu.lu@linux.intel.com> References: <20240527040517.38561-1-baolu.lu@linux.intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The struct sva_iommu represents an association between an SVA domain and a PASID of a device. It's stored in the iommu group's pasid array and also tracked by a list in the per-mm data structure. Removes duplicate tracking of sva_iommu by eliminating the list. Signed-off-by: Lu Baolu --- include/linux/iommu.h | 2 -- drivers/iommu/iommu-priv.h | 3 +++ drivers/iommu/iommu-sva.c | 21 ++++++++++++--------- drivers/iommu/iommu.c | 31 +++++++++++++++++++++++++++++++ 4 files changed, 46 insertions(+), 11 deletions(-) diff --git a/include/linux/iommu.h b/include/linux/iommu.h index b89404acacd6..823fa3bcc2c6 100644 --- a/include/linux/iommu.h +++ b/include/linux/iommu.h @@ -1005,14 +1005,12 @@ struct iommu_attach_handle { struct iommu_sva { struct iommu_attach_handle handle; struct device *dev; - struct list_head handle_item; refcount_t users; }; =20 struct iommu_mm_data { u32 pasid; struct list_head sva_domains; - struct list_head sva_handles; }; =20 int iommu_fwspec_init(struct device *dev, struct fwnode_handle *iommu_fwno= de, diff --git a/drivers/iommu/iommu-priv.h b/drivers/iommu/iommu-priv.h index 5f731d994803..f1536a5ebb0d 100644 --- a/drivers/iommu/iommu-priv.h +++ b/drivers/iommu/iommu-priv.h @@ -28,4 +28,7 @@ void iommu_device_unregister_bus(struct iommu_device *iom= mu, const struct bus_type *bus, struct notifier_block *nb); =20 +struct iommu_attach_handle *iommu_attach_handle_get(struct iommu_group *gr= oup, + ioasid_t pasid, + unsigned int type); #endif /* __LINUX_IOMMU_PRIV_H */ diff --git a/drivers/iommu/iommu-sva.c b/drivers/iommu/iommu-sva.c index 0fb923254062..e85f4ccc9dcc 100644 --- a/drivers/iommu/iommu-sva.c +++ b/drivers/iommu/iommu-sva.c @@ -41,7 +41,6 @@ static struct iommu_mm_data *iommu_alloc_mm_data(struct m= m_struct *mm, struct de } iommu_mm->pasid =3D pasid; INIT_LIST_HEAD(&iommu_mm->sva_domains); - INIT_LIST_HEAD(&iommu_mm->sva_handles); /* * Make sure the write to mm->iommu_mm is not reordered in front of * initialization to iommu_mm fields. If it does, readers may see a @@ -69,11 +68,16 @@ static struct iommu_mm_data *iommu_alloc_mm_data(struct= mm_struct *mm, struct de */ struct iommu_sva *iommu_sva_bind_device(struct device *dev, struct mm_stru= ct *mm) { + struct iommu_group *group =3D dev->iommu_group; + struct iommu_attach_handle *attach_handle; struct iommu_mm_data *iommu_mm; struct iommu_domain *domain; struct iommu_sva *handle; int ret; =20 + if (!group) + return ERR_PTR(-ENODEV); + mutex_lock(&iommu_sva_lock); =20 /* Allocate mm->pasid if necessary. */ @@ -83,12 +87,13 @@ struct iommu_sva *iommu_sva_bind_device(struct device *= dev, struct mm_struct *mm goto out_unlock; } =20 - list_for_each_entry(handle, &mm->iommu_mm->sva_handles, handle_item) { - if (handle->dev =3D=3D dev) { - refcount_inc(&handle->users); - mutex_unlock(&iommu_sva_lock); - return handle; - } + /* A bond already exists, just take a reference`. */ + attach_handle =3D iommu_attach_handle_get(group, iommu_mm->pasid, IOMMU_D= OMAIN_SVA); + if (!IS_ERR(attach_handle)) { + handle =3D container_of(attach_handle, struct iommu_sva, handle); + refcount_inc(&handle->users); + mutex_unlock(&iommu_sva_lock); + return handle; } =20 handle =3D kzalloc(sizeof(*handle), GFP_KERNEL); @@ -125,7 +130,6 @@ struct iommu_sva *iommu_sva_bind_device(struct device *= dev, struct mm_struct *mm =20 out: refcount_set(&handle->users, 1); - list_add(&handle->handle_item, &mm->iommu_mm->sva_handles); mutex_unlock(&iommu_sva_lock); handle->dev =3D dev; return handle; @@ -159,7 +163,6 @@ void iommu_sva_unbind_device(struct iommu_sva *handle) mutex_unlock(&iommu_sva_lock); return; } - list_del(&handle->handle_item); =20 iommu_detach_device_pasid(domain, dev, iommu_mm->pasid); if (--domain->users =3D=3D 0) { diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c index efd281ddfa63..0263814cba6b 100644 --- a/drivers/iommu/iommu.c +++ b/drivers/iommu/iommu.c @@ -3483,3 +3483,34 @@ void iommu_free_global_pasid(ioasid_t pasid) ida_free(&iommu_global_pasid_ida, pasid); } EXPORT_SYMBOL_GPL(iommu_free_global_pasid); + +/** + * iommu_attach_handle_get - Return the attach handle + * @group: the iommu group that domain was attached to + * @pasid: the pasid within the group + * @type: matched domain type, 0 for any match + * + * Return handle or ERR_PTR(-ENOENT) on none, ERR_PTR(-EBUSY) on mismatch. + * + * Return the attach handle to the caller. The life cycle of an iommu atta= ch + * handle is from the time when the domain is attached to the time when the + * domain is detached. Callers are required to synchronize the call of + * iommu_attach_handle_get() with domain attachment and detachment. The at= tach + * handle can only be used during its life cycle. + */ +struct iommu_attach_handle * +iommu_attach_handle_get(struct iommu_group *group, ioasid_t pasid, unsigne= d int type) +{ + struct iommu_attach_handle *handle; + + xa_lock(&group->pasid_array); + handle =3D xa_load(&group->pasid_array, pasid); + if (!handle) + handle =3D ERR_PTR(-ENOENT); + else if (type && handle->domain->type !=3D type) + handle =3D ERR_PTR(-EBUSY); + xa_unlock(&group->pasid_array); + + return handle; +} +EXPORT_SYMBOL_NS_GPL(iommu_attach_handle_get, IOMMUFD_INTERNAL); --=20 2.34.1 From nobody Fri Feb 13 13:24:41 2026 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.21]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5276F10A0C for ; Mon, 27 May 2024 04:07:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.21 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716782875; cv=none; b=CNIf62gPou4kMkAvwSPxPdbLBmBBDMhGKJfxfdMPvApqiZmsebSK7FJsq5eUqkzAHsmVSPJkR1hFTZiWM4S0aU4/RrK7JT1ciaBPDAjqe3kxn5EIw5d5JcV1wpTf+E5UoA5MYe+1Y4/sQLABSYE9R4zKkdggICblJsbwfjZa8nQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716782875; c=relaxed/simple; bh=xeDpc6y+D8uSW+ULNIeE3Wqi4B8D6lEG8uPBD3ayIuU=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=pf+FWRf6OqsuQ6Wk1m1tT42tr505CvBJl8wIGUkzlFnhfZS3OahyegTyjbVo9vYBygLao1DxqZy8baKDV7u9z3BeMgy8XW8x9FuvKJNs34+B9BIyT8FDoHdNZjD4APIoNS3yPVv1lmBZQ7uTZ4ZzG/piyjbxZmVi20Amh7Y7zjc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=none smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=lEs2B3Wy; arc=none smtp.client-ip=198.175.65.21 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="lEs2B3Wy" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1716782875; x=1748318875; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=xeDpc6y+D8uSW+ULNIeE3Wqi4B8D6lEG8uPBD3ayIuU=; b=lEs2B3WyjaOOderQRKO/MhCwCxqft4zBXm+g48Hy4zL7YjRJdJKKO1iH Au5uTSqDYojsL3+912nVis35AlheBrypwjXU3hYpxVGX8mtUhavE341Pl +5+oYYOJYp4oOtF6RCz9rOXVYpJjbJpuvbh2F/I1f5HpOhHr5oY39+b5R E9jDBxWwrMdGTY7rh8WZ1UvRJr+7zwUONjf13CVsI/6UpVXKYz24RtJGw xyQE1/qOqPEnd26J+JMThAHxKQAPoKdL7pxgTujjS88NXLYPayMsJB3kF IV1EbQKIonO3ILLF9a9tcGKl/4BTQl2kuU4i/2Im5tB6Tt6i6QQHPXo+u w==; X-CSE-ConnectionGUID: v7FPxK4xSa+9JD+waMgm5A== X-CSE-MsgGUID: OdyArzgeT4amQEOL0tyK/A== X-IronPort-AV: E=McAfee;i="6600,9927,11084"; a="13022730" X-IronPort-AV: E=Sophos;i="6.08,191,1712646000"; d="scan'208";a="13022730" Received: from orviesa003.jf.intel.com ([10.64.159.143]) by orvoesa113.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 May 2024 21:07:36 -0700 X-CSE-ConnectionGUID: O5qiNgNhQLyJBSoO/z1/uQ== X-CSE-MsgGUID: KqbDLQ4WQQCSjKaXKNpjnQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.08,191,1712646000"; d="scan'208";a="39399901" Received: from unknown (HELO allen-box.sh.intel.com) ([10.239.159.127]) by orviesa003.jf.intel.com with ESMTP; 26 May 2024 21:07:32 -0700 From: Lu Baolu To: Jason Gunthorpe , Kevin Tian , Joerg Roedel , Will Deacon , Robin Murphy , Jean-Philippe Brucker , Nicolin Chen , Yi Liu , Jacob Pan , Joel Granados Cc: iommu@lists.linux.dev, virtualization@lists.linux-foundation.org, linux-kernel@vger.kernel.org, Lu Baolu Subject: [PATCH v6 03/10] iommu: Add attach handle to struct iopf_group Date: Mon, 27 May 2024 12:05:10 +0800 Message-Id: <20240527040517.38561-4-baolu.lu@linux.intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240527040517.38561-1-baolu.lu@linux.intel.com> References: <20240527040517.38561-1-baolu.lu@linux.intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Previously, the domain that a page fault targets is stored in an iopf_group, which represents a minimal set of page faults. With the introduction of attach handle, replace the domain with the handle so that the fault handler can obtain more information as needed when handling the faults. iommu_report_device_fault() is currently used for SVA page faults, which handles the page fault in an internal cycle. The domain is retrieved with iommu_get_domain_for_dev_pasid() if the pasid in the fault message is valid. This doesn't work in IOMMUFD case, where if the pasid table of a device is wholly managed by user space, there is no domain attached to the PASID of the device, and all page faults are forwarded through a NESTING domain attaching to RID. Add a new IOMMU capability flag, IOMMU_CAP_USER_IOASID_TABLE, which indicates if the IOMMU driver supports user-managed PASID tables. In the iopf deliver path, if no attach handle found for the iopf PASID, roll back to RID domain when the IOMMU driver supports this capability. iommu_get_domain_for_dev_pasid() is no longer used and can be removed. Signed-off-by: Lu Baolu --- include/linux/iommu.h | 18 +++++------- drivers/iommu/io-pgfault.c | 59 +++++++++++++++++++++----------------- drivers/iommu/iommu-sva.c | 3 +- drivers/iommu/iommu.c | 39 ------------------------- 4 files changed, 41 insertions(+), 78 deletions(-) diff --git a/include/linux/iommu.h b/include/linux/iommu.h index 823fa3bcc2c6..4067ebdd6232 100644 --- a/include/linux/iommu.h +++ b/include/linux/iommu.h @@ -127,7 +127,7 @@ struct iopf_group { /* list node for iommu_fault_param::faults */ struct list_head pending_node; struct work_struct work; - struct iommu_domain *domain; + struct iommu_attach_handle *attach_handle; /* The device's fault data parameter. */ struct iommu_fault_param *fault_param; }; @@ -249,6 +249,12 @@ enum iommu_cap { */ IOMMU_CAP_DEFERRED_FLUSH, IOMMU_CAP_DIRTY_TRACKING, /* IOMMU supports dirty tracking */ + /* + * IOMMU driver supports user-managed IOASID table. There is no + * user domain for each PASID and the I/O page faults are forwarded + * through the user domain attached to the device RID. + */ + IOMMU_CAP_USER_IOASID_TABLE, }; =20 /* These are the possible reserved region types */ @@ -1064,9 +1070,6 @@ int iommu_attach_device_pasid(struct iommu_domain *do= main, struct iommu_attach_handle *handle); void iommu_detach_device_pasid(struct iommu_domain *domain, struct device *dev, ioasid_t pasid); -struct iommu_domain * -iommu_get_domain_for_dev_pasid(struct device *dev, ioasid_t pasid, - unsigned int type); ioasid_t iommu_alloc_global_pasid(struct device *dev); void iommu_free_global_pasid(ioasid_t pasid); #else /* CONFIG_IOMMU_API */ @@ -1408,13 +1411,6 @@ static inline void iommu_detach_device_pasid(struct = iommu_domain *domain, { } =20 -static inline struct iommu_domain * -iommu_get_domain_for_dev_pasid(struct device *dev, ioasid_t pasid, - unsigned int type) -{ - return NULL; -} - static inline ioasid_t iommu_alloc_global_pasid(struct device *dev) { return IOMMU_PASID_INVALID; diff --git a/drivers/iommu/io-pgfault.c b/drivers/iommu/io-pgfault.c index 06d78fcc79fd..c62fcb67ef02 100644 --- a/drivers/iommu/io-pgfault.c +++ b/drivers/iommu/io-pgfault.c @@ -59,30 +59,6 @@ void iopf_free_group(struct iopf_group *group) } EXPORT_SYMBOL_GPL(iopf_free_group); =20 -static struct iommu_domain *get_domain_for_iopf(struct device *dev, - struct iommu_fault *fault) -{ - struct iommu_domain *domain; - - if (fault->prm.flags & IOMMU_FAULT_PAGE_REQUEST_PASID_VALID) { - domain =3D iommu_get_domain_for_dev_pasid(dev, fault->prm.pasid, 0); - if (IS_ERR(domain)) - domain =3D NULL; - } else { - domain =3D iommu_get_domain_for_dev(dev); - } - - if (!domain || !domain->iopf_handler) { - dev_warn_ratelimited(dev, - "iopf (pasid %d) without domain attached or handler installed\n", - fault->prm.pasid); - - return NULL; - } - - return domain; -} - /* Non-last request of a group. Postpone until the last one. */ static int report_partial_fault(struct iommu_fault_param *fault_param, struct iommu_fault *fault) @@ -206,20 +182,49 @@ void iommu_report_device_fault(struct device *dev, st= ruct iopf_fault *evt) if (group =3D=3D &abort_group) goto err_abort; =20 - group->domain =3D get_domain_for_iopf(dev, fault); - if (!group->domain) + if (fault->prm.flags & IOMMU_FAULT_PAGE_REQUEST_PASID_VALID) { + group->attach_handle =3D iommu_attach_handle_get(dev->iommu_group, + fault->prm.pasid, + 0); + if (IS_ERR(group->attach_handle)) { + if (!device_iommu_capable(dev, IOMMU_CAP_USER_IOASID_TABLE)) + goto err_abort; + + /* + * The iommu driver for this device supports user- + * managed PASID table. Therefore page faults for + * any PASID should go through the NESTING domain + * attached to the device RID. + */ + group->attach_handle =3D + iommu_attach_handle_get(dev->iommu_group, + IOMMU_NO_PASID, + IOMMU_DOMAIN_NESTED); + if (IS_ERR(group->attach_handle)) + goto err_abort; + } + } else { + group->attach_handle =3D + iommu_attach_handle_get(dev->iommu_group, IOMMU_NO_PASID, 0); + if (IS_ERR(group->attach_handle)) + goto err_abort; + } + + if (!group->attach_handle->domain->iopf_handler) goto err_abort; =20 /* * On success iopf_handler must call iopf_group_response() and * iopf_free_group() */ - if (group->domain->iopf_handler(group)) + if (group->attach_handle->domain->iopf_handler(group)) goto err_abort; =20 return; =20 err_abort: + dev_warn_ratelimited(dev, "iopf with pasid %d aborted\n", + fault->prm.pasid); iopf_group_response(group, IOMMU_PAGE_RESP_FAILURE); if (group =3D=3D &abort_group) __iopf_free_group(group); diff --git a/drivers/iommu/iommu-sva.c b/drivers/iommu/iommu-sva.c index e85f4ccc9dcc..36d2862941de 100644 --- a/drivers/iommu/iommu-sva.c +++ b/drivers/iommu/iommu-sva.c @@ -265,7 +265,8 @@ static void iommu_sva_handle_iopf(struct work_struct *w= ork) if (status !=3D IOMMU_PAGE_RESP_SUCCESS) break; =20 - status =3D iommu_sva_handle_mm(&iopf->fault, group->domain->mm); + status =3D iommu_sva_handle_mm(&iopf->fault, + group->attach_handle->domain->mm); } =20 iopf_group_response(group, status); diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c index 0263814cba6b..c506185a2fad 100644 --- a/drivers/iommu/iommu.c +++ b/drivers/iommu/iommu.c @@ -3418,45 +3418,6 @@ void iommu_detach_device_pasid(struct iommu_domain *= domain, struct device *dev, } EXPORT_SYMBOL_GPL(iommu_detach_device_pasid); =20 -/* - * iommu_get_domain_for_dev_pasid() - Retrieve domain for @pasid of @dev - * @dev: the queried device - * @pasid: the pasid of the device - * @type: matched domain type, 0 for any match - * - * This is a variant of iommu_get_domain_for_dev(). It returns the existing - * domain attached to pasid of a device. Callers must hold a lock around t= his - * function, and both iommu_attach/detach_dev_pasid() whenever a domain of - * type is being manipulated. This API does not internally resolve races w= ith - * attach/detach. - * - * Return: attached domain on success, NULL otherwise. - */ -struct iommu_domain *iommu_get_domain_for_dev_pasid(struct device *dev, - ioasid_t pasid, - unsigned int type) -{ - /* Caller must be a probed driver on dev */ - struct iommu_group *group =3D dev->iommu_group; - struct iommu_attach_handle *handle; - struct iommu_domain *domain =3D NULL; - - if (!group) - return NULL; - - xa_lock(&group->pasid_array); - handle =3D xa_load(&group->pasid_array, pasid); - if (handle) - domain =3D handle->domain; - - if (type && domain && domain->type !=3D type) - domain =3D NULL; - xa_unlock(&group->pasid_array); - - return domain; -} -EXPORT_SYMBOL_GPL(iommu_get_domain_for_dev_pasid); - ioasid_t iommu_alloc_global_pasid(struct device *dev) { int ret; --=20 2.34.1 From nobody Fri Feb 13 13:24:41 2026 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.21]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DADE814A9D for ; Mon, 27 May 2024 04:07:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.21 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716782877; cv=none; b=JYLWxgO2tF4MKT8QGKWp5qUzSZcFu9GyGbL2KYvkM4DKz1ULuxCmPcBCMIs7pKz8sjGs0T4//hv9hz+GKGhKNo5MS1mZkVdRWRnQeCg1eIKqLfMdIp4ESnmAvFom+mp7b6zhyrK9QAxwQznmUo763yDvi+gqANQ42P61lUlTqdY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716782877; c=relaxed/simple; bh=zqFfjljFQ7bL1eow/Jlw1FxofoCmHNFJrcdwJL8lKvE=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=qrPFR/TrRC/vHhmFOwl736+LZ9QEQVenJ796eDSd46GqfnAOykU7SRjjyGyhlFvQ96zrI1YTJe7BLtvM8KjbP6mGnubjlaDY3QdsfWLdyQJczItpkWc46oDRUnMh75Ta7q0ZzbaYXwpMWPnt5sE2EGToPy7Cg+hqMPNWT9N9Pdk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=none smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=QhQkyaMm; arc=none smtp.client-ip=198.175.65.21 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="QhQkyaMm" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1716782876; x=1748318876; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=zqFfjljFQ7bL1eow/Jlw1FxofoCmHNFJrcdwJL8lKvE=; b=QhQkyaMmU9lQAnUJAi04K66NJ+fW0yPW2IK9hEocj2PnWOuaqyM9QNHG A7vBOG8fn4xVC4JsoFPlZMjUD0Yi1PNbxYSim71nyS+YF/awK5kKNd2iB 8FEqQBpOVEqLrd2f1RNXkgipioEApfvR3ziw+p59CHftWNmS1z4sX+JXn 3pPJLTX88Cd+8+gJTwAGONbXVkws1ZcAGAfPLpgtRpY14Ds2Z1qJmuwN+ g8dNv6VLXf0cpgUhpMM2F+nIpqMVJnUc6v38oKlkz/LZ5Tga+9Ru3o7JZ wO7XDEo+96xiKujdETu3dOi8C4Qy1F5nnwUuYighcwxOqXecugh7RRGj8 w==; X-CSE-ConnectionGUID: NpwBuiHcSEOoJ50OSg4COg== X-CSE-MsgGUID: uTUW207bTJC4vZSUlIY6xg== X-IronPort-AV: E=McAfee;i="6600,9927,11084"; a="13022753" X-IronPort-AV: E=Sophos;i="6.08,191,1712646000"; d="scan'208";a="13022753" Received: from orviesa003.jf.intel.com ([10.64.159.143]) by orvoesa113.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 May 2024 21:07:40 -0700 X-CSE-ConnectionGUID: xjVXVygwQPatCCxROdW6hw== X-CSE-MsgGUID: Bauamti5QJeECuzxBFWsEw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.08,191,1712646000"; d="scan'208";a="39399914" Received: from unknown (HELO allen-box.sh.intel.com) ([10.239.159.127]) by orviesa003.jf.intel.com with ESMTP; 26 May 2024 21:07:36 -0700 From: Lu Baolu To: Jason Gunthorpe , Kevin Tian , Joerg Roedel , Will Deacon , Robin Murphy , Jean-Philippe Brucker , Nicolin Chen , Yi Liu , Jacob Pan , Joel Granados Cc: iommu@lists.linux.dev, virtualization@lists.linux-foundation.org, linux-kernel@vger.kernel.org, Lu Baolu Subject: [PATCH v6 04/10] iommu: Extend domain attach group with handle support Date: Mon, 27 May 2024 12:05:11 +0800 Message-Id: <20240527040517.38561-5-baolu.lu@linux.intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240527040517.38561-1-baolu.lu@linux.intel.com> References: <20240527040517.38561-1-baolu.lu@linux.intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Unlike the SVA case where each PASID of a device has an SVA domain attached to it, the I/O page faults are handled by the fault handler of the SVA domain. The I/O page faults for a user page table might be handled by the domain attached to RID or the domain attached to the PASID, depending on whether the PASID table is managed by user space or kernel. As a result, there is a need for the domain attach group interfaces to have attach handle support. The attach handle will be forwarded to the fault handler of the user domain. Add some variants of the domain attaching group interfaces so that they could support the attach handle and export them for use in IOMMUFD. Signed-off-by: Lu Baolu --- drivers/iommu/iommu-priv.h | 8 +++ drivers/iommu/iommu.c | 99 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 107 insertions(+) diff --git a/drivers/iommu/iommu-priv.h b/drivers/iommu/iommu-priv.h index f1536a5ebb0d..c37801c32f33 100644 --- a/drivers/iommu/iommu-priv.h +++ b/drivers/iommu/iommu-priv.h @@ -31,4 +31,12 @@ void iommu_device_unregister_bus(struct iommu_device *io= mmu, struct iommu_attach_handle *iommu_attach_handle_get(struct iommu_group *gr= oup, ioasid_t pasid, unsigned int type); +int iommu_attach_group_handle(struct iommu_domain *domain, + struct iommu_group *group, + struct iommu_attach_handle *handle); +void iommu_detach_group_handle(struct iommu_domain *domain, + struct iommu_group *group); +int iommu_replace_group_handle(struct iommu_group *group, + struct iommu_domain *new_domain, + struct iommu_attach_handle *handle); #endif /* __LINUX_IOMMU_PRIV_H */ diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c index c506185a2fad..78e29fc97236 100644 --- a/drivers/iommu/iommu.c +++ b/drivers/iommu/iommu.c @@ -3475,3 +3475,102 @@ iommu_attach_handle_get(struct iommu_group *group, = ioasid_t pasid, unsigned int return handle; } EXPORT_SYMBOL_NS_GPL(iommu_attach_handle_get, IOMMUFD_INTERNAL); + +/** + * iommu_attach_group_handle - Attach an IOMMU domain to an IOMMU group + * @domain: IOMMU domain to attach + * @group: IOMMU group that will be attached + * @handle: attach handle + * + * Returns 0 on success and error code on failure. + * + * This is a variant of iommu_attach_group(). It allows the caller to prov= ide + * an attach handle and use it when the domain is attached. This is curren= tly + * used by IOMMUFD to deliver the I/O page faults. + */ +int iommu_attach_group_handle(struct iommu_domain *domain, + struct iommu_group *group, + struct iommu_attach_handle *handle) +{ + int ret; + + mutex_lock(&group->mutex); + ret =3D xa_insert(&group->pasid_array, IOMMU_NO_PASID, handle, GFP_KERNEL= ); + if (ret) + goto err_unlock; + + ret =3D __iommu_attach_group(domain, group); + if (ret) + goto err_erase; + mutex_unlock(&group->mutex); + + return 0; +err_erase: + xa_erase(&group->pasid_array, IOMMU_NO_PASID); +err_unlock: + mutex_unlock(&group->mutex); + return ret; +} +EXPORT_SYMBOL_NS_GPL(iommu_attach_group_handle, IOMMUFD_INTERNAL); + +/** + * iommu_detach_group_handle - Detach an IOMMU domain from an IOMMU group + * @domain: IOMMU domain to attach + * @group: IOMMU group that will be attached + * + * Detach the specified IOMMU domain from the specified IOMMU group. + * It must be used in conjunction with iommu_attach_group_handle(). + */ +void iommu_detach_group_handle(struct iommu_domain *domain, + struct iommu_group *group) +{ + mutex_lock(&group->mutex); + __iommu_group_set_core_domain(group); + xa_erase(&group->pasid_array, IOMMU_NO_PASID); + mutex_unlock(&group->mutex); +} +EXPORT_SYMBOL_NS_GPL(iommu_detach_group_handle, IOMMUFD_INTERNAL); + +/** + * iommu_replace_group_handle - replace the domain that a group is attache= d to + * @group: IOMMU group that will be attached to the new domain + * @new_domain: new IOMMU domain to replace with + * @handle: attach handle + * + * This is a variant of iommu_group_replace_domain(). It allows the caller= to + * provide an attach handle for the new domain and use it when the domain = is + * attached. + */ +int iommu_replace_group_handle(struct iommu_group *group, + struct iommu_domain *new_domain, + struct iommu_attach_handle *handle) +{ + struct iommu_domain *old_domain =3D group->domain; + void *curr; + int ret; + + if (!new_domain) + return -EINVAL; + + mutex_lock(&group->mutex); + ret =3D __iommu_group_set_domain(group, new_domain); + if (ret) + goto err_unlock; + xa_erase(&group->pasid_array, IOMMU_NO_PASID); + if (handle) { + curr =3D xa_store(&group->pasid_array, IOMMU_NO_PASID, handle, GFP_KERNE= L); + if (xa_err(curr)) { + ret =3D xa_err(curr); + goto err_restore; + } + } + mutex_unlock(&group->mutex); + + return 0; +err_restore: + __iommu_group_set_domain(group, old_domain); +err_unlock: + mutex_unlock(&group->mutex); + return ret; +} +EXPORT_SYMBOL_NS_GPL(iommu_replace_group_handle, IOMMUFD_INTERNAL); --=20 2.34.1 From nobody Fri Feb 13 13:24:41 2026 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.21]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 394B710A0C for ; Mon, 27 May 2024 04:08:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.21 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716782883; cv=none; b=hnQ49+J99MUV8d5g7YUiSp1HbByP1sjezMrLFk8OaeEtNLVVn7HxXhEJjzdc//8Dx5P2+ksIxFSp5RT84zStjzQK+kF15FtUWV0uWNpJWTOBVkpHGRc6r0HCGMdVpc5/PCcSZxEZUBaT/Oia7sX7OiYmS6+cvTMpg9K6NIdg038= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716782883; c=relaxed/simple; bh=Q0ZGANXPztpM2O2KEUVb019jzb2ZSdMGP3pgfGlUZL4=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=kyUaLZsWbYUXesWwixyWtzskf8MvYYhPuR8F1VVTanH8dNZ4pnFtosuCb62k9Vc8pr6Z0hogagTm01ffv8eRx9D856YSe3h1ttE8nVqmWqeq4VFCVFltnJYi4/4JaUAT/xwRBiYJ6BmtRyu/DtFQ3D/Esn2f2s1BY95bDHky1p4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=none smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=DeI9PVtd; arc=none smtp.client-ip=198.175.65.21 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="DeI9PVtd" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1716782883; x=1748318883; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=Q0ZGANXPztpM2O2KEUVb019jzb2ZSdMGP3pgfGlUZL4=; b=DeI9PVtdURzD+SfNSLizWvaWsZbvnqv1JdV/1ijqPiksOWjnrZNKSF90 +ZvZuZ2arLexQEvMd8nGroK0kt+e+9GNfu7nOiVYLj9Gmnm1U+etgQfoW kPM2OJGwYXdO6uGyrQ5Z+ret0Qmtau1rlNu6ijQt2hdSNS20Njg4+DHCO 8OMACoS5snCywKJtmzGP20H9YI7XXbQqg7ZQMarWV9bra8iNDCCZOaPqm 57dlKWyZR36iYn+nb5S/h1eD3WnRdjEEgtcaG1QpoKA4bSzXzisqdPlOe gan1U3xkpo64EBWBvcTlWWAKEZyMQtH8L7bVdKzczG/Xdg2jysiK6MfLD w==; X-CSE-ConnectionGUID: KQxCRby8S9qdf2/hnxIIKg== X-CSE-MsgGUID: kNjWKq8FQ7aAEWhPmrVWMg== X-IronPort-AV: E=McAfee;i="6600,9927,11084"; a="13022779" X-IronPort-AV: E=Sophos;i="6.08,191,1712646000"; d="scan'208";a="13022779" Received: from orviesa003.jf.intel.com ([10.64.159.143]) by orvoesa113.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 May 2024 21:07:43 -0700 X-CSE-ConnectionGUID: a5koAXgESUaeXB9KlQkKaA== X-CSE-MsgGUID: YKy9rx1YRw+f6cKbBCcvCg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.08,191,1712646000"; d="scan'208";a="39399959" Received: from unknown (HELO allen-box.sh.intel.com) ([10.239.159.127]) by orviesa003.jf.intel.com with ESMTP; 26 May 2024 21:07:39 -0700 From: Lu Baolu To: Jason Gunthorpe , Kevin Tian , Joerg Roedel , Will Deacon , Robin Murphy , Jean-Philippe Brucker , Nicolin Chen , Yi Liu , Jacob Pan , Joel Granados Cc: iommu@lists.linux.dev, virtualization@lists.linux-foundation.org, linux-kernel@vger.kernel.org, Lu Baolu Subject: [PATCH v6 05/10] iommufd: Add fault and response message definitions Date: Mon, 27 May 2024 12:05:12 +0800 Message-Id: <20240527040517.38561-6-baolu.lu@linux.intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240527040517.38561-1-baolu.lu@linux.intel.com> References: <20240527040517.38561-1-baolu.lu@linux.intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" iommu_hwpt_pgfaults represent fault messages that the userspace can retrieve. Multiple iommu_hwpt_pgfaults might be put in an iopf group, with the IOMMU_PGFAULT_FLAGS_LAST_PAGE flag set only for the last iommu_hwpt_pgfault. An iommu_hwpt_page_response is a response message that the userspace should send to the kernel after finishing handling a group of fault messages. The @dev_id, @pasid, and @grpid fields in the message identify an outstanding iopf group for a device. The @cookie field, which matches the cookie field of the last fault in the group, will be used by the kernel to look up the pending message. Signed-off-by: Lu Baolu --- include/uapi/linux/iommufd.h | 96 ++++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) diff --git a/include/uapi/linux/iommufd.h b/include/uapi/linux/iommufd.h index 1dfeaa2e649e..2f34d66436fb 100644 --- a/include/uapi/linux/iommufd.h +++ b/include/uapi/linux/iommufd.h @@ -692,4 +692,100 @@ struct iommu_hwpt_invalidate { __u32 __reserved; }; #define IOMMU_HWPT_INVALIDATE _IO(IOMMUFD_TYPE, IOMMUFD_CMD_HWPT_INVALIDAT= E) + +/** + * enum iommu_hwpt_pgfault_flags - flags for struct iommu_hwpt_pgfault + * @IOMMU_PGFAULT_FLAGS_PASID_VALID: The pasid field of the fault data is + * valid. + * @IOMMU_PGFAULT_FLAGS_LAST_PAGE: It's the last fault of a fault group. + */ +enum iommu_hwpt_pgfault_flags { + IOMMU_PGFAULT_FLAGS_PASID_VALID =3D (1 << 0), + IOMMU_PGFAULT_FLAGS_LAST_PAGE =3D (1 << 1), +}; + +/** + * enum iommu_hwpt_pgfault_perm - perm bits for struct iommu_hwpt_pgfault + * @IOMMU_PGFAULT_PERM_READ: request for read permission + * @IOMMU_PGFAULT_PERM_WRITE: request for write permission + * @IOMMU_PGFAULT_PERM_EXEC: (PCIE 10.4.1) request with a PASID that has t= he + * Execute Requested bit set in PASID TLP Prefix. + * @IOMMU_PGFAULT_PERM_PRIV: (PCIE 10.4.1) request with a PASID that has t= he + * Privileged Mode Requested bit set in PASID TLP + * Prefix. + */ +enum iommu_hwpt_pgfault_perm { + IOMMU_PGFAULT_PERM_READ =3D (1 << 0), + IOMMU_PGFAULT_PERM_WRITE =3D (1 << 1), + IOMMU_PGFAULT_PERM_EXEC =3D (1 << 2), + IOMMU_PGFAULT_PERM_PRIV =3D (1 << 3), +}; + +/** + * struct iommu_hwpt_pgfault - iommu page fault data + * @size: sizeof(struct iommu_hwpt_pgfault) + * @flags: Combination of enum iommu_hwpt_pgfault_flags + * @dev_id: id of the originated device + * @pasid: Process Address Space ID + * @grpid: Page Request Group Index + * @perm: Combination of enum iommu_hwpt_pgfault_perm + * @addr: Fault address + * @length: a hint of how much data the requestor is expecting to fetch. F= or + * example, if the PRI initiator knows it is going to do a 10MB + * transfer, it could fill in 10MB and the OS could pre-fault in + * 10MB of IOVA. It's default to 0 if there's no such hint. + * @cookie: kernel-managed cookie identifying a group of fault messages. T= he + * cookie number encoded in the last page fault of the group shou= ld + * be echoed back in the response message. + */ +struct iommu_hwpt_pgfault { + __u32 size; + __u32 flags; + __u32 dev_id; + __u32 pasid; + __u32 grpid; + __u32 perm; + __u64 addr; + __u32 length; + __u32 cookie; +}; + +/** + * enum iommufd_page_response_code - Return status of fault handlers + * @IOMMUFD_PAGE_RESP_SUCCESS: Fault has been handled and the page tables + * populated, retry the access. This is the + * "Success" defined in PCI 10.4.2.1. + * @IOMMUFD_PAGE_RESP_INVALID: Could not handle this fault, don't retry the + * access. This is the "Invalid Request" in PCI + * 10.4.2.1. + * @IOMMUFD_PAGE_RESP_FAILURE: General error. Drop all subsequent faults f= rom + * this device if possible. This is the "Respo= nse + * Failure" in PCI 10.4.2.1. + */ +enum iommufd_page_response_code { + IOMMUFD_PAGE_RESP_SUCCESS =3D 0, + IOMMUFD_PAGE_RESP_INVALID, + IOMMUFD_PAGE_RESP_FAILURE, +}; + +/** + * struct iommu_hwpt_page_response - IOMMU page fault response + * @size: sizeof(struct iommu_hwpt_page_response) + * @flags: Must be set to 0 + * @dev_id: device ID of target device for the response + * @pasid: Process Address Space ID + * @grpid: Page Request Group Index + * @code: One of response code in enum iommufd_page_response_code. + * @cookie: The kernel-managed cookie reported in the fault message. + */ +struct iommu_hwpt_page_response { + __u32 size; + __u32 flags; + __u32 dev_id; + __u32 pasid; + __u32 grpid; + __u32 code; + __u32 cookie; + __u32 reserved; +}; #endif --=20 2.34.1 From nobody Fri Feb 13 13:24:41 2026 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.21]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 22E3F1F93E for ; Mon, 27 May 2024 04:08:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.21 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716782882; cv=none; b=gdxZbDCOp+bBfiq0Qd58d/GYzZ8keIgLmLFyicIeLB1d3mHnHAn92NZIxaY7ZIwLm7rIv0WUqaH0e8kkPfQEC99goRlRUPph7eY1k6FIgfJ0M45xNkkULfXdBgATs+84NR7c7il97noTVu4wG/TNzXfC4ImXZiXbcbYCDhxu0Ts= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716782882; c=relaxed/simple; bh=nkmQ+JilPUgDZHHOygxgTAIyNq6MMtJg+WcoXQ51KBM=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=KbmFFgXQ3JHZRdTXXoxenIwe/bzrkaz/fUfbncf3UM3ctwdNfWZEivMAlmKnZ76yGqSX9fpAMbTQ66rop+SDdRAEhTSiHF2FyiVgfBDeniTZcsyrdJUJdXWzBLQTF/cuLZDB2I/oDHaoni62UokejGPxUspi8UfRWiSiF4wDN3U= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=none smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=grNO+LtA; arc=none smtp.client-ip=198.175.65.21 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="grNO+LtA" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1716782881; x=1748318881; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=nkmQ+JilPUgDZHHOygxgTAIyNq6MMtJg+WcoXQ51KBM=; b=grNO+LtAquozqjFYRFdp6F72adE0lr8xpVYxmCHC391NIt4ctHC7UvKj ozdTermx9JcVvEckdCUcIq4iW2+puJpgStuxfbjkvWym5mP2bgS3eVT1v vQWmFIUukq/W97bVpEA5ooioL/ACE7Ud9PWxV/Nbrr+k3dktatByQqE3d l7hGJJ+ZlzQnBqWu0sZR+a8ptSe0ash6reQgYExTQCmiIPHOBwhqvVWvw tCsh6xp/gyq4UDOI3Enrsi75/fS+gswTTUeZjPLNfLYVjIKGKmd2UHFEm +0XPmL6FODZad5AUDCAQrzKZkVHuNpbX6hCPi7wxPvKBAm3VNhIuxNZ1D A==; X-CSE-ConnectionGUID: jT397GKdS9iblr1Xkf+Ung== X-CSE-MsgGUID: dEi85c9CQ0KCxPwzwyAOLg== X-IronPort-AV: E=McAfee;i="6600,9927,11084"; a="13022796" X-IronPort-AV: E=Sophos;i="6.08,191,1712646000"; d="scan'208";a="13022796" Received: from orviesa003.jf.intel.com ([10.64.159.143]) by orvoesa113.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 May 2024 21:07:47 -0700 X-CSE-ConnectionGUID: Bkts9isxQvqMWm0kVfwBHA== X-CSE-MsgGUID: VnCE2BnBTxWm79MvG8vQPg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.08,191,1712646000"; d="scan'208";a="39399976" Received: from unknown (HELO allen-box.sh.intel.com) ([10.239.159.127]) by orviesa003.jf.intel.com with ESMTP; 26 May 2024 21:07:43 -0700 From: Lu Baolu To: Jason Gunthorpe , Kevin Tian , Joerg Roedel , Will Deacon , Robin Murphy , Jean-Philippe Brucker , Nicolin Chen , Yi Liu , Jacob Pan , Joel Granados Cc: iommu@lists.linux.dev, virtualization@lists.linux-foundation.org, linux-kernel@vger.kernel.org, Lu Baolu Subject: [PATCH v6 06/10] iommufd: Add iommufd fault object Date: Mon, 27 May 2024 12:05:13 +0800 Message-Id: <20240527040517.38561-7-baolu.lu@linux.intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240527040517.38561-1-baolu.lu@linux.intel.com> References: <20240527040517.38561-1-baolu.lu@linux.intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" An iommufd fault object provides an interface for delivering I/O page faults to user space. These objects are created and destroyed by user space, and they can be associated with or dissociated from hardware page table objects during page table allocation or destruction. User space interacts with the fault object through a file interface. This interface offers a straightforward and efficient way for user space to handle page faults. It allows user space to read fault messages sequentially and respond to them by writing to the same file. The file interface supports reading messages in poll mode, so it's recommended that user space applications use io_uring to enhance read and write efficiency. A fault object can be associated with any iopf-capable iommufd_hw_pgtable during the pgtable's allocation. All I/O page faults triggered by devices when accessing the I/O addresses of an iommufd_hw_pgtable are routed through the fault object to user space. Similarly, user space's responses to these page faults are routed back to the iommu device driver through the same fault object. Signed-off-by: Lu Baolu --- include/linux/iommu.h | 4 + drivers/iommu/iommufd/iommufd_private.h | 30 ++++ include/uapi/linux/iommufd.h | 18 ++ drivers/iommu/io-pgfault.c | 2 + drivers/iommu/iommufd/fault.c | 227 ++++++++++++++++++++++++ drivers/iommu/iommufd/main.c | 6 + drivers/iommu/iommufd/Makefile | 1 + 7 files changed, 288 insertions(+) create mode 100644 drivers/iommu/iommufd/fault.c diff --git a/include/linux/iommu.h b/include/linux/iommu.h index 4067ebdd6232..16b3a2da91ef 100644 --- a/include/linux/iommu.h +++ b/include/linux/iommu.h @@ -124,12 +124,16 @@ struct iopf_fault { struct iopf_group { struct iopf_fault last_fault; struct list_head faults; + size_t fault_count; /* list node for iommu_fault_param::faults */ struct list_head pending_node; struct work_struct work; struct iommu_attach_handle *attach_handle; /* The device's fault data parameter. */ struct iommu_fault_param *fault_param; + /* Used by handler provider to hook the group on its own lists. */ + struct list_head node; + u32 cookie; }; =20 /** diff --git a/drivers/iommu/iommufd/iommufd_private.h b/drivers/iommu/iommuf= d/iommufd_private.h index 991f864d1f9b..c8a4519f1405 100644 --- a/drivers/iommu/iommufd/iommufd_private.h +++ b/drivers/iommu/iommufd/iommufd_private.h @@ -128,6 +128,7 @@ enum iommufd_object_type { IOMMUFD_OBJ_HWPT_NESTED, IOMMUFD_OBJ_IOAS, IOMMUFD_OBJ_ACCESS, + IOMMUFD_OBJ_FAULT, #ifdef CONFIG_IOMMUFD_TEST IOMMUFD_OBJ_SELFTEST, #endif @@ -426,6 +427,35 @@ void iopt_remove_access(struct io_pagetable *iopt, u32 iopt_access_list_id); void iommufd_access_destroy_object(struct iommufd_object *obj); =20 +/* + * An iommufd_fault object represents an interface to deliver I/O page fau= lts + * to the user space. These objects are created/destroyed by the user spac= e and + * associated with hardware page table objects during page-table allocatio= n. + */ +struct iommufd_fault { + struct iommufd_object obj; + struct iommufd_ctx *ictx; + struct file *filep; + + /* The lists of outstanding faults protected by below mutex. */ + struct mutex mutex; + struct list_head deliver; + struct xarray response; + + struct wait_queue_head wait_queue; +}; + +struct iommufd_attach_handle { + struct iommu_attach_handle handle; + struct iommufd_device *idev; +}; + +/* Convert an iommu attach handle to iommufd handle. */ +#define to_iommufd_handle(hdl) container_of(hdl, struct iommufd_attach_han= dle, handle) + +int iommufd_fault_alloc(struct iommufd_ucmd *ucmd); +void iommufd_fault_destroy(struct iommufd_object *obj); + #ifdef CONFIG_IOMMUFD_TEST int iommufd_test(struct iommufd_ucmd *ucmd); void iommufd_selftest_destroy(struct iommufd_object *obj); diff --git a/include/uapi/linux/iommufd.h b/include/uapi/linux/iommufd.h index 2f34d66436fb..eba452d4344e 100644 --- a/include/uapi/linux/iommufd.h +++ b/include/uapi/linux/iommufd.h @@ -50,6 +50,7 @@ enum { IOMMUFD_CMD_HWPT_SET_DIRTY_TRACKING, IOMMUFD_CMD_HWPT_GET_DIRTY_BITMAP, IOMMUFD_CMD_HWPT_INVALIDATE, + IOMMUFD_CMD_FAULT_QUEUE_ALLOC, }; =20 /** @@ -788,4 +789,21 @@ struct iommu_hwpt_page_response { __u32 cookie; __u32 reserved; }; + +/** + * struct iommu_fault_alloc - ioctl(IOMMU_FAULT_QUEUE_ALLOC) + * @size: sizeof(struct iommu_fault_alloc) + * @flags: Must be 0 + * @out_fault_id: The ID of the new FAULT + * @out_fault_fd: The fd of the new FAULT + * + * Explicitly allocate a fault handling object. + */ +struct iommu_fault_alloc { + __u32 size; + __u32 flags; + __u32 out_fault_id; + __u32 out_fault_fd; +}; +#define IOMMU_FAULT_QUEUE_ALLOC _IO(IOMMUFD_TYPE, IOMMUFD_CMD_FAULT_QUEUE_= ALLOC) #endif diff --git a/drivers/iommu/io-pgfault.c b/drivers/iommu/io-pgfault.c index c62fcb67ef02..a629d8a93614 100644 --- a/drivers/iommu/io-pgfault.c +++ b/drivers/iommu/io-pgfault.c @@ -110,6 +110,8 @@ static struct iopf_group *iopf_group_alloc(struct iommu= _fault_param *iopf_param, list_add(&group->pending_node, &iopf_param->faults); mutex_unlock(&iopf_param->lock); =20 + group->fault_count =3D list_count_nodes(&group->faults); + return group; } =20 diff --git a/drivers/iommu/iommufd/fault.c b/drivers/iommu/iommufd/fault.c new file mode 100644 index 000000000000..d0dafe761075 --- /dev/null +++ b/drivers/iommu/iommufd/fault.c @@ -0,0 +1,227 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* Copyright (C) 2024 Intel Corporation + */ +#define pr_fmt(fmt) "iommufd: " fmt + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "../iommu-priv.h" +#include "iommufd_private.h" + +void iommufd_fault_destroy(struct iommufd_object *obj) +{ + struct iommufd_fault *fault =3D container_of(obj, struct iommufd_fault, o= bj); + struct iopf_group *group, *next; + + /* + * The iommufd object's reference count is zero at this point. + * We can be confident that no other threads are currently + * accessing this pointer. Therefore, acquiring the mutex here + * is unnecessary. + */ + list_for_each_entry_safe(group, next, &fault->deliver, node) { + list_del(&group->node); + iopf_group_response(group, IOMMU_PAGE_RESP_INVALID); + iopf_free_group(group); + } +} + +static void iommufd_compose_fault_message(struct iommu_fault *fault, + struct iommu_hwpt_pgfault *hwpt_fault, + struct iommufd_device *idev, + u32 cookie) +{ + hwpt_fault->size =3D sizeof(*hwpt_fault); + hwpt_fault->flags =3D fault->prm.flags; + hwpt_fault->dev_id =3D idev->obj.id; + hwpt_fault->pasid =3D fault->prm.pasid; + hwpt_fault->grpid =3D fault->prm.grpid; + hwpt_fault->perm =3D fault->prm.perm; + hwpt_fault->addr =3D fault->prm.addr; + hwpt_fault->length =3D 0; + hwpt_fault->cookie =3D cookie; +} + +static ssize_t iommufd_fault_fops_read(struct file *filep, char __user *bu= f, + size_t count, loff_t *ppos) +{ + size_t fault_size =3D sizeof(struct iommu_hwpt_pgfault); + struct iommufd_fault *fault =3D filep->private_data; + struct iommu_hwpt_pgfault data; + struct iommufd_device *idev; + struct iopf_group *group; + struct iopf_fault *iopf; + size_t done =3D 0; + int rc =3D 0; + + if (*ppos || count % fault_size) + return -ESPIPE; + + mutex_lock(&fault->mutex); + while (!list_empty(&fault->deliver) && count > done) { + group =3D list_first_entry(&fault->deliver, + struct iopf_group, node); + + if (group->fault_count * fault_size > count - done) + break; + + rc =3D xa_alloc(&fault->response, &group->cookie, group, + xa_limit_32b, GFP_KERNEL); + if (rc) + break; + + idev =3D to_iommufd_handle(group->attach_handle)->idev; + list_for_each_entry(iopf, &group->faults, list) { + iommufd_compose_fault_message(&iopf->fault, + &data, idev, + group->cookie); + rc =3D copy_to_user(buf + done, &data, fault_size); + if (rc) { + xa_erase(&fault->response, group->cookie); + break; + } + done +=3D fault_size; + } + + list_del(&group->node); + } + mutex_unlock(&fault->mutex); + + return done =3D=3D 0 ? rc : done; +} + +static ssize_t iommufd_fault_fops_write(struct file *filep, const char __u= ser *buf, + size_t count, loff_t *ppos) +{ + size_t response_size =3D sizeof(struct iommu_hwpt_page_response); + struct iommufd_fault *fault =3D filep->private_data; + struct iommu_hwpt_page_response response; + struct iopf_group *group; + size_t done =3D 0; + int rc =3D 0; + + if (*ppos || count % response_size) + return -ESPIPE; + + mutex_lock(&fault->mutex); + while (count > done) { + rc =3D copy_from_user(&response, buf + done, response_size); + if (rc) + break; + + group =3D xa_erase(&fault->response, response.cookie); + if (!group) { + rc =3D -EINVAL; + break; + } + + iopf_group_response(group, response.code); + iopf_free_group(group); + done +=3D response_size; + } + mutex_unlock(&fault->mutex); + + return done =3D=3D 0 ? rc : done; +} + +static __poll_t iommufd_fault_fops_poll(struct file *filep, + struct poll_table_struct *wait) +{ + struct iommufd_fault *fault =3D filep->private_data; + __poll_t pollflags =3D EPOLLOUT; + + poll_wait(filep, &fault->wait_queue, wait); + mutex_lock(&fault->mutex); + if (!list_empty(&fault->deliver)) + pollflags |=3D EPOLLIN | EPOLLRDNORM; + mutex_unlock(&fault->mutex); + + return pollflags; +} + +static int iommufd_fault_fops_release(struct inode *inode, struct file *fi= lep) +{ + struct iommufd_fault *fault =3D filep->private_data; + + iommufd_ctx_put(fault->ictx); + refcount_dec(&fault->obj.users); + return 0; +} + +static const struct file_operations iommufd_fault_fops =3D { + .owner =3D THIS_MODULE, + .open =3D nonseekable_open, + .read =3D iommufd_fault_fops_read, + .write =3D iommufd_fault_fops_write, + .poll =3D iommufd_fault_fops_poll, + .release =3D iommufd_fault_fops_release, + .llseek =3D no_llseek, +}; + +int iommufd_fault_alloc(struct iommufd_ucmd *ucmd) +{ + struct iommu_fault_alloc *cmd =3D ucmd->cmd; + struct iommufd_fault *fault; + struct file *filep; + int fdno; + int rc; + + if (cmd->flags) + return -EOPNOTSUPP; + + fault =3D iommufd_object_alloc(ucmd->ictx, fault, IOMMUFD_OBJ_FAULT); + if (IS_ERR(fault)) + return PTR_ERR(fault); + + fault->ictx =3D ucmd->ictx; + INIT_LIST_HEAD(&fault->deliver); + xa_init_flags(&fault->response, XA_FLAGS_ALLOC1); + mutex_init(&fault->mutex); + init_waitqueue_head(&fault->wait_queue); + + filep =3D anon_inode_getfile("[iommufd-pgfault]", &iommufd_fault_fops, + fault, O_RDWR); + if (IS_ERR(filep)) { + rc =3D PTR_ERR(filep); + goto out_abort; + } + + refcount_inc(&fault->obj.users); + iommufd_ctx_get(fault->ictx); + fault->filep =3D filep; + + fdno =3D get_unused_fd_flags(O_CLOEXEC); + if (fdno < 0) { + rc =3D fdno; + goto out_fput; + } + + cmd->out_fault_id =3D fault->obj.id; + cmd->out_fault_fd =3D fdno; + + rc =3D iommufd_ucmd_respond(ucmd, sizeof(*cmd)); + if (rc) + goto out_put_fdno; + iommufd_object_finalize(ucmd->ictx, &fault->obj); + + fd_install(fdno, fault->filep); + + return 0; +out_put_fdno: + put_unused_fd(fdno); +out_fput: + fput(filep); + refcount_dec(&fault->obj.users); + iommufd_ctx_put(fault->ictx); +out_abort: + iommufd_object_abort_and_destroy(ucmd->ictx, &fault->obj); + + return rc; +} diff --git a/drivers/iommu/iommufd/main.c b/drivers/iommu/iommufd/main.c index 39b32932c61e..961b2949c06f 100644 --- a/drivers/iommu/iommufd/main.c +++ b/drivers/iommu/iommufd/main.c @@ -332,6 +332,7 @@ union ucmd_buffer { struct iommu_ioas_unmap unmap; struct iommu_option option; struct iommu_vfio_ioas vfio_ioas; + struct iommu_fault_alloc fault; #ifdef CONFIG_IOMMUFD_TEST struct iommu_test_cmd test; #endif @@ -381,6 +382,8 @@ static const struct iommufd_ioctl_op iommufd_ioctl_ops[= ] =3D { val64), IOCTL_OP(IOMMU_VFIO_IOAS, iommufd_vfio_ioas, struct iommu_vfio_ioas, __reserved), + IOCTL_OP(IOMMU_FAULT_QUEUE_ALLOC, iommufd_fault_alloc, struct iommu_fault= _alloc, + out_fault_fd), #ifdef CONFIG_IOMMUFD_TEST IOCTL_OP(IOMMU_TEST_CMD, iommufd_test, struct iommu_test_cmd, last), #endif @@ -513,6 +516,9 @@ static const struct iommufd_object_ops iommufd_object_o= ps[] =3D { .destroy =3D iommufd_hwpt_nested_destroy, .abort =3D iommufd_hwpt_nested_abort, }, + [IOMMUFD_OBJ_FAULT] =3D { + .destroy =3D iommufd_fault_destroy, + }, #ifdef CONFIG_IOMMUFD_TEST [IOMMUFD_OBJ_SELFTEST] =3D { .destroy =3D iommufd_selftest_destroy, diff --git a/drivers/iommu/iommufd/Makefile b/drivers/iommu/iommufd/Makefile index 34b446146961..b94a74366eed 100644 --- a/drivers/iommu/iommufd/Makefile +++ b/drivers/iommu/iommufd/Makefile @@ -6,6 +6,7 @@ iommufd-y :=3D \ ioas.o \ main.o \ pages.o \ + fault.o \ vfio_compat.o =20 iommufd-$(CONFIG_IOMMUFD_TEST) +=3D selftest.o --=20 2.34.1 From nobody Fri Feb 13 13:24:41 2026 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.21]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1FE37C121 for ; Mon, 27 May 2024 04:08:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.21 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716782885; cv=none; b=oSpwHKDHRvDnt6e0VmiOA0tYYY88ZhzfaZAv4gxv+9kNUwJSTZEwfx7yuFF/KE0Um8eFvJORpzWeia7JyDfAcs0Ohoiuro+eqAe32PqgH6q3FoYoQLNLuyun1qfOqv5b6smTfFPCys3d4bGELRtL7bk4fcdqzt8d8Tb5BSr3zjc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716782885; c=relaxed/simple; bh=67Ni+UMZEpU4jUPoW9+IyUuAjMZCgXaGdMMlrt0ANug=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=URSuh2dLcs/O2MgMegvJN59hX37o+eXFRwlD7AtW86/PPr8VshabTwmwhQYAVTGCZ3lIn8gFrB8p1okDFwRZcjgqAxb2FteA7zQBV/AnwojMrUe9lG++3tHCiOI2yo59IH5nRCmAWAagOOwBXjL5T3M1z2qJDWKux2rDNtDuhYw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=none smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=TyE15Y7T; arc=none smtp.client-ip=198.175.65.21 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="TyE15Y7T" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1716782885; x=1748318885; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=67Ni+UMZEpU4jUPoW9+IyUuAjMZCgXaGdMMlrt0ANug=; b=TyE15Y7TC+r+M10Pnh1uvVbinwKMW/BNb7EQqSF8AkyOg1R2dqJ5QMsJ jUldRPSlTHhnge9ViobZvIfO9qtNbR7/4gf9hMjtrm2O+3W90BiWJVsvo b7aqDpRf6vFaGeGLBZ8P9E70aL96OqK/zl2UldUm4S8DPT9k8BjIzyk70 VUPmLfaYJHm+phCsU3xbZ8cGLyUoU374Gbz9n6E7yxU8dpCKPO4TmuXoy Bb2To+hdePUyyBeLsPjUWY7W0G4NhDmOqsRHOA1M3FdKo33GLet3oxIOw DR6vRwqBjjpg45Z/w4lhKUL8Ey7UvErJXYFlaelYpFqOY3M7lMnoZOKJr w==; X-CSE-ConnectionGUID: oHSYzWJKQzmte7OG7wDZsQ== X-CSE-MsgGUID: NUeRiUKUSKSRURoEx/hkFQ== X-IronPort-AV: E=McAfee;i="6600,9927,11084"; a="13022843" X-IronPort-AV: E=Sophos;i="6.08,191,1712646000"; d="scan'208";a="13022843" Received: from orviesa003.jf.intel.com ([10.64.159.143]) by orvoesa113.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 May 2024 21:07:50 -0700 X-CSE-ConnectionGUID: UJXn37RjTSeCr3eD1+GwHQ== X-CSE-MsgGUID: uyVYksVxTgi1t1p7lPqDkw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.08,191,1712646000"; d="scan'208";a="39399985" Received: from unknown (HELO allen-box.sh.intel.com) ([10.239.159.127]) by orviesa003.jf.intel.com with ESMTP; 26 May 2024 21:07:47 -0700 From: Lu Baolu To: Jason Gunthorpe , Kevin Tian , Joerg Roedel , Will Deacon , Robin Murphy , Jean-Philippe Brucker , Nicolin Chen , Yi Liu , Jacob Pan , Joel Granados Cc: iommu@lists.linux.dev, virtualization@lists.linux-foundation.org, linux-kernel@vger.kernel.org, Lu Baolu Subject: [PATCH v6 07/10] iommufd: Fault-capable hwpt attach/detach/replace Date: Mon, 27 May 2024 12:05:14 +0800 Message-Id: <20240527040517.38561-8-baolu.lu@linux.intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240527040517.38561-1-baolu.lu@linux.intel.com> References: <20240527040517.38561-1-baolu.lu@linux.intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add iopf-capable hw page table attach/detach/replace helpers. The pointer to iommufd_device is stored in the domain attachment handle, so that it can be echo'ed back in the iopf_group. The iopf-capable hw page tables can only be attached to devices that support the IOMMU_DEV_FEAT_IOPF feature. On the first attachment of an iopf-capable hw_pagetable to the device, the IOPF feature is enabled on the device. Similarly, after the last iopf-capable hwpt is detached from the device, the IOPF feature is disabled on the device. The current implementation allows a replacement between iopf-capable and non-iopf-capable hw page tables. This matches the nested translation use case, where a parent domain is attached by default and can then be replaced with a nested user domain with iopf support. Signed-off-by: Lu Baolu --- drivers/iommu/iommufd/iommufd_private.h | 12 ++ drivers/iommu/iommufd/device.c | 16 +- drivers/iommu/iommufd/fault.c | 191 ++++++++++++++++++++++++ 3 files changed, 216 insertions(+), 3 deletions(-) diff --git a/drivers/iommu/iommufd/iommufd_private.h b/drivers/iommu/iommuf= d/iommufd_private.h index c8a4519f1405..ba89c86e1af7 100644 --- a/drivers/iommu/iommufd/iommufd_private.h +++ b/drivers/iommu/iommufd/iommufd_private.h @@ -293,6 +293,7 @@ int iommufd_check_iova_range(struct io_pagetable *iopt, struct iommufd_hw_pagetable { struct iommufd_object obj; struct iommu_domain *domain; + struct iommufd_fault *fault; }; =20 struct iommufd_hwpt_paging { @@ -396,6 +397,9 @@ struct iommufd_device { /* always the physical device */ struct device *dev; bool enforce_cache_coherency; + /* protect iopf_enabled counter */ + struct mutex iopf_lock; + unsigned int iopf_enabled; }; =20 static inline struct iommufd_device * @@ -456,6 +460,14 @@ struct iommufd_attach_handle { int iommufd_fault_alloc(struct iommufd_ucmd *ucmd); void iommufd_fault_destroy(struct iommufd_object *obj); =20 +int iommufd_fault_domain_attach_dev(struct iommufd_hw_pagetable *hwpt, + struct iommufd_device *idev); +void iommufd_fault_domain_detach_dev(struct iommufd_hw_pagetable *hwpt, + struct iommufd_device *idev); +int iommufd_fault_domain_replace_dev(struct iommufd_device *idev, + struct iommufd_hw_pagetable *hwpt, + struct iommufd_hw_pagetable *old); + #ifdef CONFIG_IOMMUFD_TEST int iommufd_test(struct iommufd_ucmd *ucmd); void iommufd_selftest_destroy(struct iommufd_object *obj); diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c index 873630c111c1..63681d79b72d 100644 --- a/drivers/iommu/iommufd/device.c +++ b/drivers/iommu/iommufd/device.c @@ -215,6 +215,7 @@ struct iommufd_device *iommufd_device_bind(struct iommu= fd_ctx *ictx, refcount_inc(&idev->obj.users); /* igroup refcount moves into iommufd_device */ idev->igroup =3D igroup; + mutex_init(&idev->iopf_lock); =20 /* * If the caller fails after this success it must call @@ -376,7 +377,10 @@ int iommufd_hw_pagetable_attach(struct iommufd_hw_page= table *hwpt, * attachment. */ if (list_empty(&idev->igroup->device_list)) { - rc =3D iommu_attach_group(hwpt->domain, idev->igroup->group); + if (hwpt->fault) + rc =3D iommufd_fault_domain_attach_dev(hwpt, idev); + else + rc =3D iommu_attach_group(hwpt->domain, idev->igroup->group); if (rc) goto err_unresv; idev->igroup->hwpt =3D hwpt; @@ -402,7 +406,10 @@ iommufd_hw_pagetable_detach(struct iommufd_device *ide= v) mutex_lock(&idev->igroup->lock); list_del(&idev->group_item); if (list_empty(&idev->igroup->device_list)) { - iommu_detach_group(hwpt->domain, idev->igroup->group); + if (hwpt->fault) + iommufd_fault_domain_detach_dev(hwpt, idev); + else + iommu_detach_group(hwpt->domain, idev->igroup->group); idev->igroup->hwpt =3D NULL; } if (hwpt_is_paging(hwpt)) @@ -497,7 +504,10 @@ iommufd_device_do_replace(struct iommufd_device *idev, goto err_unlock; } =20 - rc =3D iommu_group_replace_domain(igroup->group, hwpt->domain); + if (old_hwpt->fault || hwpt->fault) + rc =3D iommufd_fault_domain_replace_dev(idev, hwpt, old_hwpt); + else + rc =3D iommu_group_replace_domain(igroup->group, hwpt->domain); if (rc) goto err_unresv; =20 diff --git a/drivers/iommu/iommufd/fault.c b/drivers/iommu/iommufd/fault.c index d0dafe761075..94dde1f57cfc 100644 --- a/drivers/iommu/iommufd/fault.c +++ b/drivers/iommu/iommufd/fault.c @@ -8,6 +8,7 @@ #include #include #include +#include #include #include #include @@ -15,6 +16,196 @@ #include "../iommu-priv.h" #include "iommufd_private.h" =20 +static int iommufd_fault_iopf_enable(struct iommufd_device *idev) +{ + struct device *dev =3D idev->dev; + int ret; + + /* + * Once we turn on PCI/PRI support for VF, the response failure code + * could not be forwarded to the hardware due to PRI being a shared + * resource between PF and VFs. There is no coordination for this + * shared capability. This waits for a vPRI reset to recover. + */ + if (dev_is_pci(dev) && to_pci_dev(dev)->is_virtfn) + return -EINVAL; + + mutex_lock(&idev->iopf_lock); + /* Device iopf has already been on. */ + if (++idev->iopf_enabled > 1) { + mutex_unlock(&idev->iopf_lock); + return 0; + } + + ret =3D iommu_dev_enable_feature(dev, IOMMU_DEV_FEAT_IOPF); + if (ret) + --idev->iopf_enabled; + mutex_unlock(&idev->iopf_lock); + + return ret; +} + +static void iommufd_fault_iopf_disable(struct iommufd_device *idev) +{ + mutex_lock(&idev->iopf_lock); + if (!WARN_ON(idev->iopf_enabled =3D=3D 0)) { + if (--idev->iopf_enabled =3D=3D 0) + iommu_dev_disable_feature(idev->dev, IOMMU_DEV_FEAT_IOPF); + } + mutex_unlock(&idev->iopf_lock); +} + +static int __fault_domain_attach_dev(struct iommufd_hw_pagetable *hwpt, + struct iommufd_device *idev) +{ + struct iommufd_attach_handle *handle; + int ret; + + handle =3D kzalloc(sizeof(*handle), GFP_KERNEL); + if (!handle) + return -ENOMEM; + + handle->handle.domain =3D hwpt->domain; + handle->idev =3D idev; + ret =3D iommu_attach_group_handle(hwpt->domain, idev->igroup->group, + &handle->handle); + if (ret) + kfree(handle); + + return ret; +} + +int iommufd_fault_domain_attach_dev(struct iommufd_hw_pagetable *hwpt, + struct iommufd_device *idev) +{ + int ret; + + if (!hwpt->fault) + return -EINVAL; + + ret =3D iommufd_fault_iopf_enable(idev); + if (ret) + return ret; + + ret =3D __fault_domain_attach_dev(hwpt, idev); + if (ret) + iommufd_fault_iopf_disable(idev); + + return ret; +} + +static void iommufd_auto_response_faults(struct iommufd_hw_pagetable *hwpt, + struct iommufd_attach_handle *handle) +{ + struct iommufd_fault *fault =3D hwpt->fault; + struct iopf_group *group, *next; + unsigned long index; + + if (!fault) + return; + + mutex_lock(&fault->mutex); + list_for_each_entry_safe(group, next, &fault->deliver, node) { + if (group->attach_handle !=3D &handle->handle) + continue; + list_del(&group->node); + iopf_group_response(group, IOMMU_PAGE_RESP_INVALID); + iopf_free_group(group); + } + + xa_for_each(&fault->response, index, group) { + if (group->attach_handle !=3D &handle->handle) + continue; + xa_erase(&fault->response, index); + iopf_group_response(group, IOMMU_PAGE_RESP_INVALID); + iopf_free_group(group); + } + mutex_unlock(&fault->mutex); +} + +static struct iommufd_attach_handle * +iommufd_device_get_attach_handle(struct iommufd_device *idev) +{ + struct iommu_attach_handle *handle; + + handle =3D iommu_attach_handle_get(idev->igroup->group, IOMMU_NO_PASID, 0= ); + if (!handle) + return NULL; + + return to_iommufd_handle(handle); +} + +void iommufd_fault_domain_detach_dev(struct iommufd_hw_pagetable *hwpt, + struct iommufd_device *idev) +{ + struct iommufd_attach_handle *handle; + + handle =3D iommufd_device_get_attach_handle(idev); + iommu_detach_group_handle(hwpt->domain, idev->igroup->group); + iommufd_auto_response_faults(hwpt, handle); + iommufd_fault_iopf_disable(idev); + kfree(handle); +} + +static int __fault_domain_replace_dev(struct iommufd_device *idev, + struct iommufd_hw_pagetable *hwpt, + struct iommufd_hw_pagetable *old) +{ + struct iommufd_attach_handle *handle, *curr =3D NULL; + int ret; + + if (old->fault) + curr =3D iommufd_device_get_attach_handle(idev); + + if (hwpt->fault) { + handle =3D kzalloc(sizeof(*handle), GFP_KERNEL); + if (!handle) + return -ENOMEM; + + handle->handle.domain =3D hwpt->domain; + handle->idev =3D idev; + ret =3D iommu_replace_group_handle(idev->igroup->group, + hwpt->domain, &handle->handle); + } else { + ret =3D iommu_replace_group_handle(idev->igroup->group, + hwpt->domain, NULL); + } + + if (!ret && curr) { + iommufd_auto_response_faults(old, curr); + kfree(curr); + } + + return ret; +} + +int iommufd_fault_domain_replace_dev(struct iommufd_device *idev, + struct iommufd_hw_pagetable *hwpt, + struct iommufd_hw_pagetable *old) +{ + bool iopf_off =3D !hwpt->fault && old->fault; + bool iopf_on =3D hwpt->fault && !old->fault; + int ret; + + if (iopf_on) { + ret =3D iommufd_fault_iopf_enable(idev); + if (ret) + return ret; + } + + ret =3D __fault_domain_replace_dev(idev, hwpt, old); + if (ret) { + if (iopf_on) + iommufd_fault_iopf_disable(idev); + return ret; + } + + if (iopf_off) + iommufd_fault_iopf_disable(idev); + + return 0; +} + void iommufd_fault_destroy(struct iommufd_object *obj) { struct iommufd_fault *fault =3D container_of(obj, struct iommufd_fault, o= bj); --=20 2.34.1 From nobody Fri Feb 13 13:24:41 2026 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.21]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 12B923B295 for ; Mon, 27 May 2024 04:08:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.21 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716782889; cv=none; b=e32Mm7xjARtvr6O0aHBlYOia5vog2y+/Aks7xPIKVVCl8h9Ozhax6cKIZ7KzbPlfMDuXB/dhbEw5+vvN/ism8f53vz2sukGn6jkI4MtpqufJqflsyI2PwTVIesiCdip5dwk7e1fr4VhMrJEHwWAxUaCTidbhco2xM8SwoyXeJmo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716782889; c=relaxed/simple; bh=fSa4QxhDk60BJQCv6VirqWArTfxsUAjFh9plrUfoEiQ=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=t4MoNFAoX77yHlgSXGu1UXqEYXdnzyYRwIKcCfXnxpmK0f8gKKS6fX6/7pN7Cd37Sf+xfVKWFpg/p9oHn6mpBnXduiYUI5fJLEeN1OvkA1XSKIeGKyezlPfBC5hNCh3R1iwljspy2ZKDgHf2OO7BYbkTsy4RTelQEc3+y/DroXg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=none smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=WOPx25Em; arc=none smtp.client-ip=198.175.65.21 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="WOPx25Em" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1716782888; x=1748318888; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=fSa4QxhDk60BJQCv6VirqWArTfxsUAjFh9plrUfoEiQ=; b=WOPx25EmqJ0cL8E7eEtPPS2V5svUtg5JpM+8TQig95YmGziMdZZfHFED KP/hqofEpHChI8XguNRd0nAuHgQ/6fCRwhhF6YLCE+FUlMtqg/44YDUPu Z99wx9Nk0WTHqMqgYDVCVf7qeEoSTmq9KuPpPasOXxD4wyYBvfVsTOuUj 23OXw4NNS61nxU2NQ5umZwSdc3LQ8ec1Ic7Gg9AAnJI2I+HS0cTMNW7k1 3Vk96n2M9eFx8Do1DvZWBGkFUlnb0ufU4Hevj7+NE8NMNV+IpZ9NBnkix m/x6agZUQlkuQr8taLxAsVu4t0C4GRsqulHB+P8hrb+uJVMeuSAz6YuH9 Q==; X-CSE-ConnectionGUID: vg147w//QruKGKqTe4Jllw== X-CSE-MsgGUID: 6vaOL3H7QsCyCUImPeRomQ== X-IronPort-AV: E=McAfee;i="6600,9927,11084"; a="13022881" X-IronPort-AV: E=Sophos;i="6.08,191,1712646000"; d="scan'208";a="13022881" Received: from orviesa003.jf.intel.com ([10.64.159.143]) by orvoesa113.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 May 2024 21:07:54 -0700 X-CSE-ConnectionGUID: n7XH8z+7QsqI7twCJEfRWg== X-CSE-MsgGUID: /ZtI4c5nRpm38IZXlLqZQw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.08,191,1712646000"; d="scan'208";a="39399999" Received: from unknown (HELO allen-box.sh.intel.com) ([10.239.159.127]) by orviesa003.jf.intel.com with ESMTP; 26 May 2024 21:07:50 -0700 From: Lu Baolu To: Jason Gunthorpe , Kevin Tian , Joerg Roedel , Will Deacon , Robin Murphy , Jean-Philippe Brucker , Nicolin Chen , Yi Liu , Jacob Pan , Joel Granados Cc: iommu@lists.linux.dev, virtualization@lists.linux-foundation.org, linux-kernel@vger.kernel.org, Lu Baolu Subject: [PATCH v6 08/10] iommufd: Associate fault object with iommufd_hw_pgtable Date: Mon, 27 May 2024 12:05:15 +0800 Message-Id: <20240527040517.38561-9-baolu.lu@linux.intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240527040517.38561-1-baolu.lu@linux.intel.com> References: <20240527040517.38561-1-baolu.lu@linux.intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" When allocating a user iommufd_hw_pagetable, the user space is allowed to associate a fault object with the hw_pagetable by specifying the fault object ID in the page table allocation data and setting the IOMMU_HWPT_FAULT_ID_VALID flag bit. On a successful return of hwpt allocation, the user can retrieve and respond to page faults by reading and writing the file interface of the fault object. Once a fault object has been associated with a hwpt, the hwpt is iopf-capable, indicated by hwpt->fault is non NULL. Attaching, detaching, or replacing an iopf-capable hwpt to an RID or PASID will differ from those that are not iopf-capable. Signed-off-by: Lu Baolu Reviewed-by: Kevin Tian --- drivers/iommu/iommufd/iommufd_private.h | 9 ++++++ include/uapi/linux/iommufd.h | 8 +++++ drivers/iommu/iommufd/fault.c | 17 ++++++++++ drivers/iommu/iommufd/hw_pagetable.c | 41 +++++++++++++++++++------ 4 files changed, 66 insertions(+), 9 deletions(-) diff --git a/drivers/iommu/iommufd/iommufd_private.h b/drivers/iommu/iommuf= d/iommufd_private.h index ba89c86e1af7..db50881e76f6 100644 --- a/drivers/iommu/iommufd/iommufd_private.h +++ b/drivers/iommu/iommufd/iommufd_private.h @@ -457,8 +457,17 @@ struct iommufd_attach_handle { /* Convert an iommu attach handle to iommufd handle. */ #define to_iommufd_handle(hdl) container_of(hdl, struct iommufd_attach_han= dle, handle) =20 +static inline struct iommufd_fault * +iommufd_get_fault(struct iommufd_ucmd *ucmd, u32 id) +{ + return container_of(iommufd_get_object(ucmd->ictx, id, + IOMMUFD_OBJ_FAULT), + struct iommufd_fault, obj); +} + int iommufd_fault_alloc(struct iommufd_ucmd *ucmd); void iommufd_fault_destroy(struct iommufd_object *obj); +int iommufd_fault_iopf_handler(struct iopf_group *group); =20 int iommufd_fault_domain_attach_dev(struct iommufd_hw_pagetable *hwpt, struct iommufd_device *idev); diff --git a/include/uapi/linux/iommufd.h b/include/uapi/linux/iommufd.h index eba452d4344e..5391db3a7180 100644 --- a/include/uapi/linux/iommufd.h +++ b/include/uapi/linux/iommufd.h @@ -357,10 +357,13 @@ struct iommu_vfio_ioas { * the parent HWPT in a nesting configurati= on. * @IOMMU_HWPT_ALLOC_DIRTY_TRACKING: Dirty tracking support for device IOM= MU is * enforced on device attachment + * @IOMMU_HWPT_FAULT_ID_VALID: The fault_id field of hwpt allocation data = is + * valid. */ enum iommufd_hwpt_alloc_flags { IOMMU_HWPT_ALLOC_NEST_PARENT =3D 1 << 0, IOMMU_HWPT_ALLOC_DIRTY_TRACKING =3D 1 << 1, + IOMMU_HWPT_FAULT_ID_VALID =3D 1 << 2, }; =20 /** @@ -412,6 +415,9 @@ enum iommu_hwpt_data_type { * @data_type: One of enum iommu_hwpt_data_type * @data_len: Length of the type specific data * @data_uptr: User pointer to the type specific data + * @fault_id: The ID of IOMMUFD_FAULT object. Valid only if flags field of + * IOMMU_HWPT_FAULT_ID_VALID is set. + * @__reserved2: Padding to 64-bit alignment. Must be 0. * * Explicitly allocate a hardware page table object. This is the same obje= ct * type that is returned by iommufd_device_attach() and represents the @@ -442,6 +448,8 @@ struct iommu_hwpt_alloc { __u32 data_type; __u32 data_len; __aligned_u64 data_uptr; + __u32 fault_id; + __u32 __reserved2; }; #define IOMMU_HWPT_ALLOC _IO(IOMMUFD_TYPE, IOMMUFD_CMD_HWPT_ALLOC) =20 diff --git a/drivers/iommu/iommufd/fault.c b/drivers/iommu/iommufd/fault.c index 94dde1f57cfc..dd07e3b1b4c1 100644 --- a/drivers/iommu/iommufd/fault.c +++ b/drivers/iommu/iommufd/fault.c @@ -416,3 +416,20 @@ int iommufd_fault_alloc(struct iommufd_ucmd *ucmd) =20 return rc; } + +int iommufd_fault_iopf_handler(struct iopf_group *group) +{ + struct iommufd_hw_pagetable *hwpt; + struct iommufd_fault *fault; + + hwpt =3D group->attach_handle->domain->fault_data; + fault =3D hwpt->fault; + + mutex_lock(&fault->mutex); + list_add_tail(&group->node, &fault->deliver); + mutex_unlock(&fault->mutex); + + wake_up_interruptible(&fault->wait_queue); + + return 0; +} diff --git a/drivers/iommu/iommufd/hw_pagetable.c b/drivers/iommu/iommufd/h= w_pagetable.c index 33d142f8057d..14a32bf80549 100644 --- a/drivers/iommu/iommufd/hw_pagetable.c +++ b/drivers/iommu/iommufd/hw_pagetable.c @@ -8,6 +8,15 @@ #include "../iommu-priv.h" #include "iommufd_private.h" =20 +static void __iommufd_hwpt_destroy(struct iommufd_hw_pagetable *hwpt) +{ + if (hwpt->domain) + iommu_domain_free(hwpt->domain); + + if (hwpt->fault) + iommufd_put_object(hwpt->fault->ictx, &hwpt->fault->obj); +} + void iommufd_hwpt_paging_destroy(struct iommufd_object *obj) { struct iommufd_hwpt_paging *hwpt_paging =3D @@ -22,9 +31,7 @@ void iommufd_hwpt_paging_destroy(struct iommufd_object *o= bj) hwpt_paging->common.domain); } =20 - if (hwpt_paging->common.domain) - iommu_domain_free(hwpt_paging->common.domain); - + __iommufd_hwpt_destroy(&hwpt_paging->common); refcount_dec(&hwpt_paging->ioas->obj.users); } =20 @@ -49,9 +56,7 @@ void iommufd_hwpt_nested_destroy(struct iommufd_object *o= bj) struct iommufd_hwpt_nested *hwpt_nested =3D container_of(obj, struct iommufd_hwpt_nested, common.obj); =20 - if (hwpt_nested->common.domain) - iommu_domain_free(hwpt_nested->common.domain); - + __iommufd_hwpt_destroy(&hwpt_nested->common); refcount_dec(&hwpt_nested->parent->common.obj.users); } =20 @@ -213,7 +218,8 @@ iommufd_hwpt_nested_alloc(struct iommufd_ctx *ictx, struct iommufd_hw_pagetable *hwpt; int rc; =20 - if (flags || !user_data->len || !ops->domain_alloc_user) + if ((flags & ~IOMMU_HWPT_FAULT_ID_VALID) || + !user_data->len || !ops->domain_alloc_user) return ERR_PTR(-EOPNOTSUPP); if (parent->auto_domain || !parent->nest_parent) return ERR_PTR(-EINVAL); @@ -227,7 +233,8 @@ iommufd_hwpt_nested_alloc(struct iommufd_ctx *ictx, refcount_inc(&parent->common.obj.users); hwpt_nested->parent =3D parent; =20 - hwpt->domain =3D ops->domain_alloc_user(idev->dev, flags, + hwpt->domain =3D ops->domain_alloc_user(idev->dev, + flags & ~IOMMU_HWPT_FAULT_ID_VALID, parent->common.domain, user_data); if (IS_ERR(hwpt->domain)) { rc =3D PTR_ERR(hwpt->domain); @@ -308,13 +315,29 @@ int iommufd_hwpt_alloc(struct iommufd_ucmd *ucmd) goto out_put_pt; } =20 + if (cmd->flags & IOMMU_HWPT_FAULT_ID_VALID) { + struct iommufd_fault *fault; + + fault =3D iommufd_get_fault(ucmd, cmd->fault_id); + if (IS_ERR(fault)) { + rc =3D PTR_ERR(fault); + goto out_hwpt; + } + hwpt->fault =3D fault; + hwpt->domain->iopf_handler =3D iommufd_fault_iopf_handler; + hwpt->domain->fault_data =3D hwpt; + } + cmd->out_hwpt_id =3D hwpt->obj.id; rc =3D iommufd_ucmd_respond(ucmd, sizeof(*cmd)); if (rc) - goto out_hwpt; + goto out_put_fault; iommufd_object_finalize(ucmd->ictx, &hwpt->obj); goto out_unlock; =20 +out_put_fault: + if (cmd->flags & IOMMU_HWPT_FAULT_ID_VALID) + iommufd_put_object(ucmd->ictx, &hwpt->fault->obj); out_hwpt: iommufd_object_abort_and_destroy(ucmd->ictx, &hwpt->obj); out_unlock: --=20 2.34.1 From nobody Fri Feb 13 13:24:41 2026 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.21]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 053F63FE4A for ; Mon, 27 May 2024 04:08:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.21 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716782893; cv=none; b=E/6ShkjO3hiiz4SWUlCBIGnwUcXsqgiNcmlk7BbIfGrP2zT4GxHnoHU+PgTHazQZwuTt2Gv90qawHXxaeXL92KNDp26aFcbmrqDvwS63+vcWxDJD6Vcfvr3OYt3lSkrjsCN6Ec6f34Vrpj6Ev/a3YEEXBdbdSAE9r3Tod4x+TAA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716782893; c=relaxed/simple; bh=Ju3G17O0Dzsql60Guq1FcQXLwqsVgJXNVCWd7rDhOU8=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=LPF6W46rL/3tbTthEH8a3cN7Qqq1+U4MUqtotcEoiYbMAQwc2GlnxqWhOXjRHGJvAhbWnDSWJlri3J5bqTKBy1MNgsOe1MW+4gnjybwQ2GQDDVB3TvwqzMjih3m2wn/y9lFtnUBRjhIYKUlFaiehot8FJzhFBWso2U10+S5a3U4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=none smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=fkNwv5Ja; arc=none smtp.client-ip=198.175.65.21 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="fkNwv5Ja" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1716782892; x=1748318892; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=Ju3G17O0Dzsql60Guq1FcQXLwqsVgJXNVCWd7rDhOU8=; b=fkNwv5JapsYDH7UCtnShQ+lM+Dc2s2YSPLWsSD/Pw0EcsGfw92G76mIL gDKvu6GA2QM4KkNeM5XB9sCgm7ju1JPtqssg4aY7U2u/jMBARpTLdwO83 MpTfJtFcjAq+wCfX6EbcCVdbXYFU3fLDeZyOUUNdE6uHp8dDAmES0l5th BohFUrViOhmT/IhvMZUambxPuOk5TS25KvZ8RXcLCZIKIvEgpvCiNIaI6 +zoufigKUwhSOnESXRhdzHMrJcTFV2vWLBa8Eby3TQJpTdG16Son9JAl1 ATRPu9nM4NJH3l22k41PVDvv2pZuJ12A4Ncrjnzf9ar6J89h3dLRxzuR7 Q==; X-CSE-ConnectionGUID: JZ+s7w+OSPibLkHl2SzV8A== X-CSE-MsgGUID: lxs6xCATSb2ye1da7FTjWA== X-IronPort-AV: E=McAfee;i="6600,9927,11084"; a="13022917" X-IronPort-AV: E=Sophos;i="6.08,191,1712646000"; d="scan'208";a="13022917" Received: from orviesa003.jf.intel.com ([10.64.159.143]) by orvoesa113.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 May 2024 21:07:58 -0700 X-CSE-ConnectionGUID: ogDwHVDMQuaPdPZpSC0J3A== X-CSE-MsgGUID: 7qyVJlQJTx+z8rTQAaZTng== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.08,191,1712646000"; d="scan'208";a="39400015" Received: from unknown (HELO allen-box.sh.intel.com) ([10.239.159.127]) by orviesa003.jf.intel.com with ESMTP; 26 May 2024 21:07:54 -0700 From: Lu Baolu To: Jason Gunthorpe , Kevin Tian , Joerg Roedel , Will Deacon , Robin Murphy , Jean-Philippe Brucker , Nicolin Chen , Yi Liu , Jacob Pan , Joel Granados Cc: iommu@lists.linux.dev, virtualization@lists.linux-foundation.org, linux-kernel@vger.kernel.org, Lu Baolu Subject: [PATCH v6 09/10] iommufd/selftest: Add IOPF support for mock device Date: Mon, 27 May 2024 12:05:16 +0800 Message-Id: <20240527040517.38561-10-baolu.lu@linux.intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240527040517.38561-1-baolu.lu@linux.intel.com> References: <20240527040517.38561-1-baolu.lu@linux.intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Extend the selftest mock device to support generating and responding to an IOPF. Also add an ioctl interface to userspace applications to trigger the IOPF on the mock device. This would allow userspace applications to test the IOMMUFD's handling of IOPFs without having to rely on any real hardware. Signed-off-by: Lu Baolu --- drivers/iommu/iommufd/iommufd_test.h | 8 ++++ drivers/iommu/iommufd/selftest.c | 64 ++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+) diff --git a/drivers/iommu/iommufd/iommufd_test.h b/drivers/iommu/iommufd/i= ommufd_test.h index e854d3f67205..acbbba1c6671 100644 --- a/drivers/iommu/iommufd/iommufd_test.h +++ b/drivers/iommu/iommufd/iommufd_test.h @@ -22,6 +22,7 @@ enum { IOMMU_TEST_OP_MOCK_DOMAIN_FLAGS, IOMMU_TEST_OP_DIRTY, IOMMU_TEST_OP_MD_CHECK_IOTLB, + IOMMU_TEST_OP_TRIGGER_IOPF, }; =20 enum { @@ -127,6 +128,13 @@ struct iommu_test_cmd { __u32 id; __u32 iotlb; } check_iotlb; + struct { + __u32 dev_id; + __u32 pasid; + __u32 grpid; + __u32 perm; + __u64 addr; + } trigger_iopf; }; __u32 last; }; diff --git a/drivers/iommu/iommufd/selftest.c b/drivers/iommu/iommufd/selft= est.c index 7a2199470f31..431eea438226 100644 --- a/drivers/iommu/iommufd/selftest.c +++ b/drivers/iommu/iommufd/selftest.c @@ -494,6 +494,7 @@ static bool mock_domain_capable(struct device *dev, enu= m iommu_cap cap) =20 switch (cap) { case IOMMU_CAP_CACHE_COHERENCY: + case IOMMU_CAP_USER_IOASID_TABLE: return true; case IOMMU_CAP_DIRTY_TRACKING: return !(mdev->flags & MOCK_FLAGS_DEVICE_NO_DIRTY); @@ -504,6 +505,8 @@ static bool mock_domain_capable(struct device *dev, enu= m iommu_cap cap) return false; } =20 +static struct iopf_queue *mock_iommu_iopf_queue; + static struct iommu_device mock_iommu_device =3D { }; =20 @@ -514,6 +517,29 @@ static struct iommu_device *mock_probe_device(struct d= evice *dev) return &mock_iommu_device; } =20 +static void mock_domain_page_response(struct device *dev, struct iopf_faul= t *evt, + struct iommu_page_response *msg) +{ +} + +static int mock_dev_enable_feat(struct device *dev, enum iommu_dev_feature= s feat) +{ + if (feat !=3D IOMMU_DEV_FEAT_IOPF || !mock_iommu_iopf_queue) + return -ENODEV; + + return iopf_queue_add_device(mock_iommu_iopf_queue, dev); +} + +static int mock_dev_disable_feat(struct device *dev, enum iommu_dev_featur= es feat) +{ + if (feat !=3D IOMMU_DEV_FEAT_IOPF || !mock_iommu_iopf_queue) + return -ENODEV; + + iopf_queue_remove_device(mock_iommu_iopf_queue, dev); + + return 0; +} + static const struct iommu_ops mock_ops =3D { /* * IOMMU_DOMAIN_BLOCKED cannot be returned from def_domain_type() @@ -529,6 +555,9 @@ static const struct iommu_ops mock_ops =3D { .capable =3D mock_domain_capable, .device_group =3D generic_device_group, .probe_device =3D mock_probe_device, + .page_response =3D mock_domain_page_response, + .dev_enable_feat =3D mock_dev_enable_feat, + .dev_disable_feat =3D mock_dev_disable_feat, .default_domain_ops =3D &(struct iommu_domain_ops){ .free =3D mock_domain_free, @@ -1375,6 +1404,31 @@ static int iommufd_test_dirty(struct iommufd_ucmd *u= cmd, unsigned int mockpt_id, return rc; } =20 +static int iommufd_test_trigger_iopf(struct iommufd_ucmd *ucmd, + struct iommu_test_cmd *cmd) +{ + struct iopf_fault event =3D { }; + struct iommufd_device *idev; + + idev =3D iommufd_get_device(ucmd, cmd->trigger_iopf.dev_id); + if (IS_ERR(idev)) + return PTR_ERR(idev); + + event.fault.prm.flags =3D IOMMU_FAULT_PAGE_REQUEST_LAST_PAGE; + if (cmd->trigger_iopf.pasid !=3D IOMMU_NO_PASID) + event.fault.prm.flags |=3D IOMMU_FAULT_PAGE_REQUEST_PASID_VALID; + event.fault.type =3D IOMMU_FAULT_PAGE_REQ; + event.fault.prm.addr =3D cmd->trigger_iopf.addr; + event.fault.prm.pasid =3D cmd->trigger_iopf.pasid; + event.fault.prm.grpid =3D cmd->trigger_iopf.grpid; + event.fault.prm.perm =3D cmd->trigger_iopf.perm; + + iommu_report_device_fault(idev->dev, &event); + iommufd_put_object(ucmd->ictx, &idev->obj); + + return 0; +} + void iommufd_selftest_destroy(struct iommufd_object *obj) { struct selftest_obj *sobj =3D container_of(obj, struct selftest_obj, obj); @@ -1450,6 +1504,8 @@ int iommufd_test(struct iommufd_ucmd *ucmd) cmd->dirty.page_size, u64_to_user_ptr(cmd->dirty.uptr), cmd->dirty.flags); + case IOMMU_TEST_OP_TRIGGER_IOPF: + return iommufd_test_trigger_iopf(ucmd, cmd); default: return -EOPNOTSUPP; } @@ -1491,6 +1547,9 @@ int __init iommufd_test_init(void) &iommufd_mock_bus_type.nb); if (rc) goto err_sysfs; + + mock_iommu_iopf_queue =3D iopf_queue_alloc("mock-iopfq"); + return 0; =20 err_sysfs: @@ -1506,6 +1565,11 @@ int __init iommufd_test_init(void) =20 void iommufd_test_exit(void) { + if (mock_iommu_iopf_queue) { + iopf_queue_free(mock_iommu_iopf_queue); + mock_iommu_iopf_queue =3D NULL; + } + iommu_device_sysfs_remove(&mock_iommu_device); iommu_device_unregister_bus(&mock_iommu_device, &iommufd_mock_bus_type.bus, --=20 2.34.1 From nobody Fri Feb 13 13:24:41 2026 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.21]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1C34E4594D for ; Mon, 27 May 2024 04:08:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.21 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716782896; cv=none; b=MTTT6f1zFIDhbrUhozSoKICYK93F3ebx1zVCp2IFUa4kz25EEJVI0oDmw6bJwavbVEhb/z0NlgfUnOuqP+5UiUPaaUKT3GGt8BzQA9o9d2dMZGd0pN4tKIhnRWixgHPGNB7OxroZaje+wAMy0gya8K+1HblyO8LUJQrjZHSxT+Y= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716782896; c=relaxed/simple; bh=4V9sklD42p2zqL0F1s8plgL/3abyqQhvM0nPRltyMjI=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=CU8GWyq83/Myz0+KQ7zLCxkrgbLcNqFySqLJ7wCEoKDkHP1H6z3DekKMMblD62VqFHy6KQToVD/UsQGrGdmdpMF9wnAUpX2icQqiYgL5g4VeyU4grqcFpxpq7EGbWx0fmnEHOhuljnCXytBEldAKbf0JSip5SDwsaN8kseRoknc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=none smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=n75IrPhE; arc=none smtp.client-ip=198.175.65.21 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="n75IrPhE" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1716782896; x=1748318896; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=4V9sklD42p2zqL0F1s8plgL/3abyqQhvM0nPRltyMjI=; b=n75IrPhE1r6lx+qWQl/tqFcl3bi/nquzv2wHbZEdXMpfx8tCKP2hR3hz Weh9GT5TnJmYLjkk4eZmXCPKJmWZyK6An7Mx+zIXEoiIkYGesm2iFuodZ kAcP5h8A4IoRFS0WSGr2X0Zn5zfVAUwon3nYvXF+NkFx1UndOfL9E/m++ JO9UMIMgIsuKklAtOqr32r1jy760ZvUVvn16XPWanlI5Z5De+4jKGzSsp +Uqes7mFBlGVoa9zwkNOCq7fukb0+MPjrrhe6Qnl+Z5X+jIMutdZHWaSr MWbFcuiuB0fMQdaLoKpVYZSnRzhR/FRP7UJYkn8SgFbwnoREpDf+NiZie w==; X-CSE-ConnectionGUID: BRiyQRT8R7yYAVi29dNYkg== X-CSE-MsgGUID: zmlZ8Ud+S4iQiu19jofJCQ== X-IronPort-AV: E=McAfee;i="6600,9927,11084"; a="13022947" X-IronPort-AV: E=Sophos;i="6.08,191,1712646000"; d="scan'208";a="13022947" Received: from orviesa003.jf.intel.com ([10.64.159.143]) by orvoesa113.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 May 2024 21:08:01 -0700 X-CSE-ConnectionGUID: OcasJu0YSeWZ0VbHjPptIQ== X-CSE-MsgGUID: g70BRhNKQSiZKuMVgA3vGg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.08,191,1712646000"; d="scan'208";a="39400023" Received: from unknown (HELO allen-box.sh.intel.com) ([10.239.159.127]) by orviesa003.jf.intel.com with ESMTP; 26 May 2024 21:07:58 -0700 From: Lu Baolu To: Jason Gunthorpe , Kevin Tian , Joerg Roedel , Will Deacon , Robin Murphy , Jean-Philippe Brucker , Nicolin Chen , Yi Liu , Jacob Pan , Joel Granados Cc: iommu@lists.linux.dev, virtualization@lists.linux-foundation.org, linux-kernel@vger.kernel.org, Lu Baolu Subject: [PATCH v6 10/10] iommufd/selftest: Add coverage for IOPF test Date: Mon, 27 May 2024 12:05:17 +0800 Message-Id: <20240527040517.38561-11-baolu.lu@linux.intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240527040517.38561-1-baolu.lu@linux.intel.com> References: <20240527040517.38561-1-baolu.lu@linux.intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Extend the selftest tool to add coverage of testing IOPF handling. This would include the following tests: - Allocating and destroying an iommufd fault object. - Allocating and destroying an IOPF-capable HWPT. - Attaching/detaching/replacing an IOPF-capable HWPT on a device. - Triggering an IOPF on the mock device. - Retrieving and responding to the IOPF through the file interface. Signed-off-by: Lu Baolu --- tools/testing/selftests/iommu/iommufd_utils.h | 84 +++++++++++++++++-- tools/testing/selftests/iommu/iommufd.c | 18 ++++ .../selftests/iommu/iommufd_fail_nth.c | 2 +- 3 files changed, 98 insertions(+), 6 deletions(-) diff --git a/tools/testing/selftests/iommu/iommufd_utils.h b/tools/testing/= selftests/iommu/iommufd_utils.h index 8d2b46b2114d..cb913ec89f96 100644 --- a/tools/testing/selftests/iommu/iommufd_utils.h +++ b/tools/testing/selftests/iommu/iommufd_utils.h @@ -153,7 +153,7 @@ static int _test_cmd_mock_domain_replace(int fd, __u32 = stdev_id, __u32 pt_id, EXPECT_ERRNO(_errno, _test_cmd_mock_domain_replace(self->fd, stdev_id, \ pt_id, NULL)) =20 -static int _test_cmd_hwpt_alloc(int fd, __u32 device_id, __u32 pt_id, +static int _test_cmd_hwpt_alloc(int fd, __u32 device_id, __u32 pt_id, __u3= 2 ft_id, __u32 flags, __u32 *hwpt_id, __u32 data_type, void *data, size_t data_len) { @@ -165,6 +165,7 @@ static int _test_cmd_hwpt_alloc(int fd, __u32 device_id= , __u32 pt_id, .data_type =3D data_type, .data_len =3D data_len, .data_uptr =3D (uint64_t)data, + .fault_id =3D ft_id, }; int ret; =20 @@ -177,24 +178,30 @@ static int _test_cmd_hwpt_alloc(int fd, __u32 device_= id, __u32 pt_id, } =20 #define test_cmd_hwpt_alloc(device_id, pt_id, flags, hwpt_id) = \ - ASSERT_EQ(0, _test_cmd_hwpt_alloc(self->fd, device_id, pt_id, flags, \ + ASSERT_EQ(0, _test_cmd_hwpt_alloc(self->fd, device_id, pt_id, 0, flags, = \ hwpt_id, IOMMU_HWPT_DATA_NONE, NULL, \ 0)) #define test_err_hwpt_alloc(_errno, device_id, pt_id, flags, hwpt_id) \ EXPECT_ERRNO(_errno, _test_cmd_hwpt_alloc( \ - self->fd, device_id, pt_id, flags, \ + self->fd, device_id, pt_id, 0, flags, \ hwpt_id, IOMMU_HWPT_DATA_NONE, NULL, 0)) =20 #define test_cmd_hwpt_alloc_nested(device_id, pt_id, flags, hwpt_id, = \ data_type, data, data_len) \ - ASSERT_EQ(0, _test_cmd_hwpt_alloc(self->fd, device_id, pt_id, flags, \ + ASSERT_EQ(0, _test_cmd_hwpt_alloc(self->fd, device_id, pt_id, 0, flags, \ hwpt_id, data_type, data, data_len)) #define test_err_hwpt_alloc_nested(_errno, device_id, pt_id, flags, hwpt_i= d, \ data_type, data, data_len) \ EXPECT_ERRNO(_errno, \ - _test_cmd_hwpt_alloc(self->fd, device_id, pt_id, flags, \ + _test_cmd_hwpt_alloc(self->fd, device_id, pt_id, 0, flags, \ hwpt_id, data_type, data, data_len)) =20 +#define test_cmd_hwpt_alloc_iopf(device_id, pt_id, fault_id, flags, hwpt_i= d, \ + data_type, data, data_len) \ + ASSERT_EQ(0, _test_cmd_hwpt_alloc(self->fd, device_id, pt_id, fault_id, \ + flags, hwpt_id, data_type, data, \ + data_len)) + #define test_cmd_hwpt_check_iotlb(hwpt_id, iotlb_id, expected) = \ ({ \ struct iommu_test_cmd test_cmd =3D { \ @@ -684,3 +691,70 @@ static int _test_cmd_get_hw_info(int fd, __u32 device_= id, void *data, =20 #define test_cmd_get_hw_capabilities(device_id, caps, mask) \ ASSERT_EQ(0, _test_cmd_get_hw_info(self->fd, device_id, NULL, 0, &caps)) + +static int _test_ioctl_fault_alloc(int fd, __u32 *fault_id, __u32 *fault_f= d) +{ + struct iommu_fault_alloc cmd =3D { + .size =3D sizeof(cmd), + }; + int ret; + + ret =3D ioctl(fd, IOMMU_FAULT_QUEUE_ALLOC, &cmd); + if (ret) + return ret; + *fault_id =3D cmd.out_fault_id; + *fault_fd =3D cmd.out_fault_fd; + return 0; +} + +#define test_ioctl_fault_alloc(fault_id, fault_fd) \ + ({ \ + ASSERT_EQ(0, _test_ioctl_fault_alloc(self->fd, fault_id, \ + fault_fd)); \ + ASSERT_NE(0, *(fault_id)); \ + ASSERT_NE(0, *(fault_fd)); \ + }) + +static int _test_cmd_trigger_iopf(int fd, __u32 device_id, __u32 fault_fd) +{ + struct iommu_test_cmd trigger_iopf_cmd =3D { + .size =3D sizeof(trigger_iopf_cmd), + .op =3D IOMMU_TEST_OP_TRIGGER_IOPF, + .trigger_iopf =3D { + .dev_id =3D device_id, + .pasid =3D 0x1, + .grpid =3D 0x2, + .perm =3D IOMMU_PGFAULT_PERM_READ | IOMMU_PGFAULT_PERM_WRITE, + .addr =3D 0xdeadbeaf, + }, + }; + struct iommu_hwpt_page_response response =3D { + .size =3D sizeof(struct iommu_hwpt_page_response), + .dev_id =3D device_id, + .pasid =3D 0x1, + .grpid =3D 0x2, + .code =3D 0, + }; + struct iommu_hwpt_pgfault fault =3D {}; + ssize_t bytes; + int ret; + + ret =3D ioctl(fd, _IOMMU_TEST_CMD(IOMMU_TEST_OP_TRIGGER_IOPF), &trigger_i= opf_cmd); + if (ret) + return ret; + + bytes =3D read(fault_fd, &fault, sizeof(fault)); + if (bytes <=3D 0) + return -EIO; + + response.cookie =3D fault.cookie; + + bytes =3D write(fault_fd, &response, sizeof(response)); + if (bytes <=3D 0) + return -EIO; + + return 0; +} + +#define test_cmd_trigger_iopf(device_id, fault_fd) \ + ASSERT_EQ(0, _test_cmd_trigger_iopf(self->fd, device_id, fault_fd)) diff --git a/tools/testing/selftests/iommu/iommufd.c b/tools/testing/selfte= sts/iommu/iommufd.c index edf1c99c9936..5b0169875a4d 100644 --- a/tools/testing/selftests/iommu/iommufd.c +++ b/tools/testing/selftests/iommu/iommufd.c @@ -279,6 +279,9 @@ TEST_F(iommufd_ioas, alloc_hwpt_nested) uint32_t parent_hwpt_id =3D 0; uint32_t parent_hwpt_id_not_work =3D 0; uint32_t test_hwpt_id =3D 0; + uint32_t iopf_hwpt_id; + uint32_t fault_id; + uint32_t fault_fd; =20 if (self->device_id) { /* Negative tests */ @@ -326,6 +329,7 @@ TEST_F(iommufd_ioas, alloc_hwpt_nested) sizeof(data)); =20 /* Allocate two nested hwpts sharing one common parent hwpt */ + test_ioctl_fault_alloc(&fault_id, &fault_fd); test_cmd_hwpt_alloc_nested(self->device_id, parent_hwpt_id, 0, &nested_hwpt_id[0], IOMMU_HWPT_DATA_SELFTEST, &data, @@ -334,6 +338,10 @@ TEST_F(iommufd_ioas, alloc_hwpt_nested) &nested_hwpt_id[1], IOMMU_HWPT_DATA_SELFTEST, &data, sizeof(data)); + test_cmd_hwpt_alloc_iopf(self->device_id, parent_hwpt_id, fault_id, + IOMMU_HWPT_FAULT_ID_VALID, &iopf_hwpt_id, + IOMMU_HWPT_DATA_SELFTEST, &data, + sizeof(data)); test_cmd_hwpt_check_iotlb_all(nested_hwpt_id[0], IOMMU_TEST_IOTLB_DEFAULT); test_cmd_hwpt_check_iotlb_all(nested_hwpt_id[1], @@ -504,14 +512,24 @@ TEST_F(iommufd_ioas, alloc_hwpt_nested) _test_ioctl_destroy(self->fd, nested_hwpt_id[1])); test_ioctl_destroy(nested_hwpt_id[0]); =20 + /* Switch from nested_hwpt_id[1] to iopf_hwpt_id */ + test_cmd_mock_domain_replace(self->stdev_id, iopf_hwpt_id); + EXPECT_ERRNO(EBUSY, + _test_ioctl_destroy(self->fd, iopf_hwpt_id)); + /* Trigger an IOPF on the device */ + test_cmd_trigger_iopf(self->device_id, fault_fd); + /* Detach from nested_hwpt_id[1] and destroy it */ test_cmd_mock_domain_replace(self->stdev_id, parent_hwpt_id); test_ioctl_destroy(nested_hwpt_id[1]); + test_ioctl_destroy(iopf_hwpt_id); =20 /* Detach from the parent hw_pagetable and destroy it */ test_cmd_mock_domain_replace(self->stdev_id, self->ioas_id); test_ioctl_destroy(parent_hwpt_id); test_ioctl_destroy(parent_hwpt_id_not_work); + close(fault_fd); + test_ioctl_destroy(fault_id); } else { test_err_hwpt_alloc(ENOENT, self->device_id, self->ioas_id, 0, &parent_hwpt_id); diff --git a/tools/testing/selftests/iommu/iommufd_fail_nth.c b/tools/testi= ng/selftests/iommu/iommufd_fail_nth.c index f590417cd67a..c5d5e69452b0 100644 --- a/tools/testing/selftests/iommu/iommufd_fail_nth.c +++ b/tools/testing/selftests/iommu/iommufd_fail_nth.c @@ -615,7 +615,7 @@ TEST_FAIL_NTH(basic_fail_nth, device) if (_test_cmd_get_hw_info(self->fd, idev_id, &info, sizeof(info), NULL)) return -1; =20 - if (_test_cmd_hwpt_alloc(self->fd, idev_id, ioas_id, 0, &hwpt_id, + if (_test_cmd_hwpt_alloc(self->fd, idev_id, ioas_id, 0, 0, &hwpt_id, IOMMU_HWPT_DATA_NONE, 0, 0)) return -1; =20 --=20 2.34.1