Message ID | 20220817012024.3251276-6-baolu.lu@linux.intel.com (mailing list archive) |
---|---|
State | Superseded |
Headers | show |
Series | iommu: SVA and IOPF refactoring | expand |
On Wed, Aug 17, 2022 at 09:20:16AM +0800, Lu Baolu wrote: > +static int __iommu_set_group_pasid(struct iommu_domain *domain, > + struct iommu_group *group, ioasid_t pasid) > +{ > + struct iommu_domain *ops_domain; > + struct group_device *device; > + int ret = 0; > + > + if (domain == group->blocking_domain) > + ops_domain = xa_load(&group->pasid_array, pasid); > + else > + ops_domain = domain; This seems weird, why isn't this just always domain->ops->set_dev_pasid()? > + if (curr) { > + ret = xa_err(curr) ? : -EBUSY; > + goto out_unlock; > + } > + > + ret = __iommu_set_group_pasid(domain, group, pasid); > + if (ret) { > + __iommu_set_group_pasid(group->blocking_domain, group, pasid); > + xa_erase(&group->pasid_array, pasid); I was looking at this trying to figure out why we are having attach/detach semantics vs set and this error handling seems to be the reason Lets add a comment because it is subtle thing: Setting a PASID to a blocking domain cannot fail, so we can always safely error unwind a failure to attach a domain back to the original group configuration of the PASID being unused. > +/* > + * iommu_detach_device_pasid() - Detach the domain from pasid of device > + * @domain: the iommu domain. > + * @dev: the attached device. > + * @pasid: the pasid of the device. > + * > + * The @domain must have been attached to @pasid of the @dev with > + * iommu_attach_device_pasid(). > + */ > +void iommu_detach_device_pasid(struct iommu_domain *domain, struct device *dev, > + ioasid_t pasid) Don't pass domain here? > +/* > + * iommu_get_domain_for_dev_pasid() - Retrieve domain for @pasid of @dev > + * @dev: the queried device > + * @pasid: the pasid of the device > + * > + * This is a variant of iommu_get_domain_for_dev(). It returns the existing > + * domain attached to pasid of a device. It's only for internal use of the > + * IOMMU subsystem. The caller must take care to avoid any possible > + * use-after-free case. How exactly does the caller manage that? > + * > + * Return: attached domain on success, NULL otherwise. > + */ > +struct iommu_domain * > +iommu_get_domain_for_dev_pasid(struct device *dev, ioasid_t pasid) > +{ > + struct iommu_domain *domain; > + struct iommu_group *group; > + > + if (!pasid_valid(pasid)) > + return NULL; Why bother? If the pasid is not valid then it definitely won't be in the xarray. But otherwise this overall thing seems fine to me Jason
On 2022/8/18 21:33, Jason Gunthorpe wrote: > On Wed, Aug 17, 2022 at 09:20:16AM +0800, Lu Baolu wrote: > >> +static int __iommu_set_group_pasid(struct iommu_domain *domain, >> + struct iommu_group *group, ioasid_t pasid) >> +{ >> + struct iommu_domain *ops_domain; >> + struct group_device *device; >> + int ret = 0; >> + >> + if (domain == group->blocking_domain) >> + ops_domain = xa_load(&group->pasid_array, pasid); >> + else >> + ops_domain = domain; > > This seems weird, why isn't this just always > > domain->ops->set_dev_pasid()? Sure. I will fix this in the next version. > >> + if (curr) { >> + ret = xa_err(curr) ? : -EBUSY; >> + goto out_unlock; >> + } >> + >> + ret = __iommu_set_group_pasid(domain, group, pasid); >> + if (ret) { >> + __iommu_set_group_pasid(group->blocking_domain, group, pasid); >> + xa_erase(&group->pasid_array, pasid); > > I was looking at this trying to figure out why we are having > attach/detach semantics vs set and this error handling seems to be the > reason > > Lets add a comment because it is subtle thing: > > Setting a PASID to a blocking domain cannot fail, so we can always > safely error unwind a failure to attach a domain back to the original > group configuration of the PASID being unused. Updated. > >> +/* >> + * iommu_detach_device_pasid() - Detach the domain from pasid of device >> + * @domain: the iommu domain. >> + * @dev: the attached device. >> + * @pasid: the pasid of the device. >> + * >> + * The @domain must have been attached to @pasid of the @dev with >> + * iommu_attach_device_pasid(). >> + */ >> +void iommu_detach_device_pasid(struct iommu_domain *domain, struct device *dev, >> + ioasid_t pasid) > > Don't pass domain here? It is checked in the function to make sure that the detached domain is the same one as the previous attached one. > >> +/* >> + * iommu_get_domain_for_dev_pasid() - Retrieve domain for @pasid of @dev >> + * @dev: the queried device >> + * @pasid: the pasid of the device >> + * >> + * This is a variant of iommu_get_domain_for_dev(). It returns the existing >> + * domain attached to pasid of a device. It's only for internal use of the >> + * IOMMU subsystem. The caller must take care to avoid any possible >> + * use-after-free case. > > How exactly does the caller manage that? "... the returned domain pointer could only be used before detaching from the device PASID." > >> + * >> + * Return: attached domain on success, NULL otherwise. >> + */ >> +struct iommu_domain * >> +iommu_get_domain_for_dev_pasid(struct device *dev, ioasid_t pasid) >> +{ >> + struct iommu_domain *domain; >> + struct iommu_group *group; >> + >> + if (!pasid_valid(pasid)) >> + return NULL; > > Why bother? If the pasid is not valid then it definitely won't be in the xarray. Removed. > But otherwise this overall thing seems fine to me Thank you! Best regards, baolu
diff --git a/include/linux/iommu.h b/include/linux/iommu.h index 2f237c3cd680..f1e8953b1e2e 100644 --- a/include/linux/iommu.h +++ b/include/linux/iommu.h @@ -266,6 +266,7 @@ struct iommu_ops { * struct iommu_domain_ops - domain specific operations * @attach_dev: attach an iommu domain to a device * @detach_dev: detach an iommu domain from a device + * @set_dev_pasid: set an iommu domain to a pasid of device * @map: map a physically contiguous memory region to an iommu domain * @map_pages: map a physically contiguous set of pages of the same size to * an iommu domain. @@ -286,6 +287,8 @@ struct iommu_ops { struct iommu_domain_ops { int (*attach_dev)(struct iommu_domain *domain, struct device *dev); void (*detach_dev)(struct iommu_domain *domain, struct device *dev); + int (*set_dev_pasid)(struct iommu_domain *domain, struct device *dev, + ioasid_t pasid); int (*map)(struct iommu_domain *domain, unsigned long iova, phys_addr_t paddr, size_t size, int prot, gfp_t gfp); @@ -680,6 +683,12 @@ int iommu_group_claim_dma_owner(struct iommu_group *group, void *owner); void iommu_group_release_dma_owner(struct iommu_group *group); bool iommu_group_dma_owner_claimed(struct iommu_group *group); +int iommu_attach_device_pasid(struct iommu_domain *domain, + struct device *dev, ioasid_t pasid); +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); #else /* CONFIG_IOMMU_API */ struct iommu_ops {}; @@ -1047,6 +1056,23 @@ static inline bool iommu_group_dma_owner_claimed(struct iommu_group *group) { return false; } + +static inline int iommu_attach_device_pasid(struct iommu_domain *domain, + struct device *dev, ioasid_t pasid) +{ + return -ENODEV; +} + +static inline void iommu_detach_device_pasid(struct iommu_domain *domain, + struct device *dev, ioasid_t pasid) +{ +} + +static inline struct iommu_domain * +iommu_get_domain_for_dev_pasid(struct device *dev, ioasid_t pasid) +{ + return NULL; +} #endif /* CONFIG_IOMMU_API */ /** diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c index 1d28a74a0511..6f2cbccc0570 100644 --- a/drivers/iommu/iommu.c +++ b/drivers/iommu/iommu.c @@ -39,6 +39,7 @@ struct iommu_group { struct kobject kobj; struct kobject *devices_kobj; struct list_head devices; + struct xarray pasid_array; struct mutex mutex; void *iommu_data; void (*iommu_data_release)(void *iommu_data); @@ -663,6 +664,7 @@ struct iommu_group *iommu_group_alloc(void) mutex_init(&group->mutex); INIT_LIST_HEAD(&group->devices); INIT_LIST_HEAD(&group->entry); + xa_init(&group->pasid_array); ret = ida_alloc(&iommu_group_ida, GFP_KERNEL); if (ret < 0) { @@ -3258,3 +3260,124 @@ bool iommu_group_dma_owner_claimed(struct iommu_group *group) return user; } EXPORT_SYMBOL_GPL(iommu_group_dma_owner_claimed); + +static int __iommu_set_group_pasid(struct iommu_domain *domain, + struct iommu_group *group, ioasid_t pasid) +{ + struct iommu_domain *ops_domain; + struct group_device *device; + int ret = 0; + + if (domain == group->blocking_domain) + ops_domain = xa_load(&group->pasid_array, pasid); + else + ops_domain = domain; + + list_for_each_entry(device, &group->devices, list) { + ret = ops_domain->ops->set_dev_pasid(domain, device->dev, pasid); + if (ret) + break; + } + + return ret; +} + +/* + * iommu_attach_device_pasid() - Attach a domain to pasid of device + * @domain: the iommu domain. + * @dev: the attached device. + * @pasid: the pasid of the device. + * + * Return: 0 on success, or an error. + */ +int iommu_attach_device_pasid(struct iommu_domain *domain, + struct device *dev, ioasid_t pasid) +{ + struct iommu_group *group; + void *curr; + int ret; + + if (!domain->ops->set_dev_pasid) + return -EOPNOTSUPP; + + group = iommu_group_get(dev); + if (!group) + return -ENODEV; + + mutex_lock(&group->mutex); + curr = xa_cmpxchg(&group->pasid_array, pasid, NULL, domain, GFP_KERNEL); + if (curr) { + ret = xa_err(curr) ? : -EBUSY; + goto out_unlock; + } + + ret = __iommu_set_group_pasid(domain, group, pasid); + if (ret) { + __iommu_set_group_pasid(group->blocking_domain, group, pasid); + xa_erase(&group->pasid_array, pasid); + } +out_unlock: + mutex_unlock(&group->mutex); + iommu_group_put(group); + + return ret; +} +EXPORT_SYMBOL_GPL(iommu_attach_device_pasid); + +/* + * iommu_detach_device_pasid() - Detach the domain from pasid of device + * @domain: the iommu domain. + * @dev: the attached device. + * @pasid: the pasid of the device. + * + * The @domain must have been attached to @pasid of the @dev with + * iommu_attach_device_pasid(). + */ +void iommu_detach_device_pasid(struct iommu_domain *domain, struct device *dev, + ioasid_t pasid) +{ + struct iommu_group *group = iommu_group_get(dev); + + mutex_lock(&group->mutex); + __iommu_set_group_pasid(group->blocking_domain, group, pasid); + WARN_ON(xa_erase(&group->pasid_array, pasid) != domain); + mutex_unlock(&group->mutex); + + iommu_group_put(group); +} +EXPORT_SYMBOL_GPL(iommu_detach_device_pasid); + +/* + * iommu_get_domain_for_dev_pasid() - Retrieve domain for @pasid of @dev + * @dev: the queried device + * @pasid: the pasid of the device + * + * This is a variant of iommu_get_domain_for_dev(). It returns the existing + * domain attached to pasid of a device. It's only for internal use of the + * IOMMU subsystem. The caller must take care to avoid any possible + * use-after-free case. + * + * Return: attached domain on success, NULL otherwise. + */ +struct iommu_domain * +iommu_get_domain_for_dev_pasid(struct device *dev, ioasid_t pasid) +{ + struct iommu_domain *domain; + struct iommu_group *group; + + if (!pasid_valid(pasid)) + return NULL; + + group = iommu_group_get(dev); + if (!group) + return NULL; + /* + * The xarray protects its internal state with RCU. Hence the domain + * obtained is either NULL or fully formed. + */ + domain = xa_load(&group->pasid_array, pasid); + iommu_group_put(group); + + return domain; +} +EXPORT_SYMBOL_GPL(iommu_get_domain_for_dev_pasid);