Add an xarray in iommu_fault_param as place holder for per-{device, pasid} fault cookie. They could be used in varous cases. For example, SVA needs to handle IO page faults through work queues to batch process all faults in a fault group. It needs a fault cookie as a temporary storage of partial faults, together with other meta data. The iommufd will also use the fault cookie to store the mapping of device object ID and a pasid of a device. This allows the iommufd to quickly retrieve the device object ID for a given {device, pasid} pair in the hot path of IO page fault delivery. Signed-off-by: Lu Baolu <baolu.lu@xxxxxxxxxxxxxxx> --- include/linux/iommu.h | 16 ++++++++++++++++ drivers/iommu/iommu.c | 44 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) diff --git a/include/linux/iommu.h b/include/linux/iommu.h index c86ff10b40f3..ffd6fe1317f4 100644 --- a/include/linux/iommu.h +++ b/include/linux/iommu.h @@ -539,10 +539,12 @@ struct iommu_fault_event { * struct iommu_fault_param - per-device IOMMU fault data * @faults: holds the pending faults which needs response * @lock: protect pending faults list + * @pasid_cookie: per-pasid fault cookie */ struct iommu_fault_param { struct list_head faults; struct mutex lock; + struct xarray pasid_cookie; }; /** @@ -636,6 +638,8 @@ extern void iommu_set_fault_handler(struct iommu_domain *domain, iommu_fault_handler_t handler, void *token); void iommu_domain_set_iopf_handler(struct iommu_domain *domain, iommu_iopf_handler_t handler, void *data); +void *iommu_set_device_fault_cookie(struct device *dev, ioasid_t pasid, void *cookie); +void *iommu_get_device_fault_cookie(struct device *dev, ioasid_t pasid); extern void iommu_get_resv_regions(struct device *dev, struct list_head *list); extern void iommu_put_resv_regions(struct device *dev, struct list_head *list); @@ -965,6 +969,18 @@ static inline void iommu_domain_set_iopf_handler(struct iommu_domain *domain, { } +static inline void *iommu_set_device_fault_cookie(struct device *dev, + ioasid_t pasid, void *cookie) +{ + return ERR_PTR(-ENODEV); +} + +static inline void *iommu_get_device_fault_cookie(struct device *dev, + ioasid_t pasid) +{ + return ERR_PTR(-ENODEV); +} + static inline void iommu_get_resv_regions(struct device *dev, struct list_head *list) { diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c index 3dc59af24208..d52b827982a4 100644 --- a/drivers/iommu/iommu.c +++ b/drivers/iommu/iommu.c @@ -1336,6 +1336,50 @@ int iommu_page_response(struct device *dev, } EXPORT_SYMBOL_GPL(iommu_page_response); +/** + * iommu_set_device_fault_cookie - Set a fault cookie for per-{device, pasid} + * @dev: the device to set the cookie + * @pasid: the pasid on this device + * @cookie: the opaque data + * + * Return the old cookie on success, or ERR_PTR(err#) on failure. + */ +void *iommu_set_device_fault_cookie(struct device *dev, ioasid_t pasid, + void *cookie) +{ + struct iommu_fault_param *fault_param; + void *curr; + + if (!dev->iommu || !dev->iommu->fault_param) + return ERR_PTR(-ENODEV); + + fault_param = dev->iommu->fault_param; + curr = xa_store(&fault_param->pasid_cookie, pasid, cookie, GFP_KERNEL); + + return xa_is_err(curr) ? ERR_PTR(xa_err(curr)) : curr; +} +EXPORT_SYMBOL_GPL(iommu_set_device_fault_cookie); + +/** + * iommu_get_device_fault_cookie - Get the fault cookie for {device, pasid} + * @dev: the device to set the cookie + * @pasid: the pasid on this device + * + * Return the cookie on success, or ERR_PTR(err#) on failure. + */ +void *iommu_get_device_fault_cookie(struct device *dev, ioasid_t pasid) +{ + struct iommu_fault_param *fault_param; + + if (!dev->iommu || !dev->iommu->fault_param) + return ERR_PTR(-ENODEV); + + fault_param = dev->iommu->fault_param; + + return xa_load(&fault_param->pasid_cookie, pasid); +} +EXPORT_SYMBOL_GPL(iommu_get_device_fault_cookie); + /** * iommu_group_id - Return ID for a group * @group: the group to ID -- 2.34.1