Commit 351ffcb1 authored by Lu Baolu's avatar Lu Baolu Committed by Joerg Roedel

iommu: Make iommu_queue_iopf() more generic

Make iommu_queue_iopf() more generic by making the iopf_group a minimal
set of iopf's that an iopf handler of domain should handle and respond
to. Add domain parameter to struct iopf_group so that the handler can
retrieve and use it directly.

Change iommu_queue_iopf() to forward groups of iopf's to the domain's
iopf handler. This is also a necessary step to decouple the sva iopf
handling code from this interface.
Signed-off-by: default avatarLu Baolu <baolu.lu@linux.intel.com>
Reviewed-by: default avatarKevin Tian <kevin.tian@intel.com>
Reviewed-by: default avatarJason Gunthorpe <jgg@nvidia.com>
Reviewed-by: default avatarYi Liu <yi.l.liu@intel.com>
Tested-by: default avatarYan Zhao <yan.y.zhao@intel.com>
Tested-by: default avatarLongfang Liu <liulongfang@huawei.com>
Link: https://lore.kernel.org/r/20240212012227.119381-10-baolu.lu@linux.intel.comSigned-off-by: default avatarJoerg Roedel <jroedel@suse.de>
parent 24b5d268
...@@ -13,6 +13,9 @@ ...@@ -13,6 +13,9 @@
#include "iommu-sva.h" #include "iommu-sva.h"
enum iommu_page_response_code
iommu_sva_handle_mm(struct iommu_fault *fault, struct mm_struct *mm);
static void iopf_free_group(struct iopf_group *group) static void iopf_free_group(struct iopf_group *group)
{ {
struct iopf_fault *iopf, *next; struct iopf_fault *iopf, *next;
...@@ -45,29 +48,48 @@ static void iopf_handler(struct work_struct *work) ...@@ -45,29 +48,48 @@ static void iopf_handler(struct work_struct *work)
{ {
struct iopf_fault *iopf; struct iopf_fault *iopf;
struct iopf_group *group; struct iopf_group *group;
struct iommu_domain *domain;
enum iommu_page_response_code status = IOMMU_PAGE_RESP_SUCCESS; enum iommu_page_response_code status = IOMMU_PAGE_RESP_SUCCESS;
group = container_of(work, struct iopf_group, work); group = container_of(work, struct iopf_group, work);
domain = iommu_get_domain_for_dev_pasid(group->dev,
group->last_fault.fault.prm.pasid, 0);
if (!domain || !domain->iopf_handler)
status = IOMMU_PAGE_RESP_INVALID;
list_for_each_entry(iopf, &group->faults, list) { list_for_each_entry(iopf, &group->faults, list) {
/* /*
* For the moment, errors are sticky: don't handle subsequent * For the moment, errors are sticky: don't handle subsequent
* faults in the group if there is an error. * faults in the group if there is an error.
*/ */
if (status == IOMMU_PAGE_RESP_SUCCESS) if (status != IOMMU_PAGE_RESP_SUCCESS)
status = domain->iopf_handler(&iopf->fault, break;
domain->fault_data);
status = iommu_sva_handle_mm(&iopf->fault, group->domain->mm);
} }
iopf_complete_group(group->dev, &group->last_fault, status); iopf_complete_group(group->dev, &group->last_fault, status);
iopf_free_group(group); iopf_free_group(group);
} }
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 = iommu_get_domain_for_dev_pasid(dev, fault->prm.pasid, 0);
if (IS_ERR(domain))
domain = NULL;
} else {
domain = 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;
}
/** /**
* iommu_queue_iopf - IO Page Fault handler * iommu_queue_iopf - IO Page Fault handler
* @fault: fault event * @fault: fault event
...@@ -112,6 +134,7 @@ int iommu_queue_iopf(struct iommu_fault *fault, struct device *dev) ...@@ -112,6 +134,7 @@ int iommu_queue_iopf(struct iommu_fault *fault, struct device *dev)
{ {
int ret; int ret;
struct iopf_group *group; struct iopf_group *group;
struct iommu_domain *domain;
struct iopf_fault *iopf, *next; struct iopf_fault *iopf, *next;
struct iommu_fault_param *iopf_param; struct iommu_fault_param *iopf_param;
struct dev_iommu *param = dev->iommu; struct dev_iommu *param = dev->iommu;
...@@ -143,6 +166,12 @@ int iommu_queue_iopf(struct iommu_fault *fault, struct device *dev) ...@@ -143,6 +166,12 @@ int iommu_queue_iopf(struct iommu_fault *fault, struct device *dev)
return 0; return 0;
} }
domain = get_domain_for_iopf(dev, fault);
if (!domain) {
ret = -EINVAL;
goto cleanup_partial;
}
group = kzalloc(sizeof(*group), GFP_KERNEL); group = kzalloc(sizeof(*group), GFP_KERNEL);
if (!group) { if (!group) {
/* /*
...@@ -157,8 +186,8 @@ int iommu_queue_iopf(struct iommu_fault *fault, struct device *dev) ...@@ -157,8 +186,8 @@ int iommu_queue_iopf(struct iommu_fault *fault, struct device *dev)
group->dev = dev; group->dev = dev;
group->last_fault.fault = *fault; group->last_fault.fault = *fault;
INIT_LIST_HEAD(&group->faults); INIT_LIST_HEAD(&group->faults);
group->domain = domain;
list_add(&group->last_fault.list, &group->faults); list_add(&group->last_fault.list, &group->faults);
INIT_WORK(&group->work, iopf_handler);
/* See if we have partial faults for this group */ /* See if we have partial faults for this group */
list_for_each_entry_safe(iopf, next, &iopf_param->partial, list) { list_for_each_entry_safe(iopf, next, &iopf_param->partial, list) {
...@@ -167,9 +196,13 @@ int iommu_queue_iopf(struct iommu_fault *fault, struct device *dev) ...@@ -167,9 +196,13 @@ int iommu_queue_iopf(struct iommu_fault *fault, struct device *dev)
list_move(&iopf->list, &group->faults); list_move(&iopf->list, &group->faults);
} }
queue_work(iopf_param->queue->wq, &group->work); mutex_unlock(&iopf_param->lock);
return 0; ret = domain->iopf_handler(group);
mutex_lock(&iopf_param->lock);
if (ret)
iopf_free_group(group);
return ret;
cleanup_partial: cleanup_partial:
list_for_each_entry_safe(iopf, next, &iopf_param->partial, list) { list_for_each_entry_safe(iopf, next, &iopf_param->partial, list) {
if (iopf->fault.prm.grpid == fault->prm.grpid) { if (iopf->fault.prm.grpid == fault->prm.grpid) {
...@@ -181,6 +214,17 @@ int iommu_queue_iopf(struct iommu_fault *fault, struct device *dev) ...@@ -181,6 +214,17 @@ int iommu_queue_iopf(struct iommu_fault *fault, struct device *dev)
} }
EXPORT_SYMBOL_GPL(iommu_queue_iopf); EXPORT_SYMBOL_GPL(iommu_queue_iopf);
int iommu_sva_handle_iopf(struct iopf_group *group)
{
struct iommu_fault_param *fault_param = group->dev->iommu->fault_param;
INIT_WORK(&group->work, iopf_handler);
if (!queue_work(fault_param->queue->wq, &group->work))
return -EBUSY;
return 0;
}
/** /**
* iopf_queue_flush_dev - Ensure that all queued faults have been processed * iopf_queue_flush_dev - Ensure that all queued faults have been processed
* @dev: the endpoint whose faults need to be flushed. * @dev: the endpoint whose faults need to be flushed.
......
...@@ -163,11 +163,10 @@ EXPORT_SYMBOL_GPL(iommu_sva_get_pasid); ...@@ -163,11 +163,10 @@ EXPORT_SYMBOL_GPL(iommu_sva_get_pasid);
* I/O page fault handler for SVA * I/O page fault handler for SVA
*/ */
enum iommu_page_response_code enum iommu_page_response_code
iommu_sva_handle_iopf(struct iommu_fault *fault, void *data) iommu_sva_handle_mm(struct iommu_fault *fault, struct mm_struct *mm)
{ {
vm_fault_t ret; vm_fault_t ret;
struct vm_area_struct *vma; struct vm_area_struct *vma;
struct mm_struct *mm = data;
unsigned int access_flags = 0; unsigned int access_flags = 0;
unsigned int fault_flags = FAULT_FLAG_REMOTE; unsigned int fault_flags = FAULT_FLAG_REMOTE;
struct iommu_fault_page_request *prm = &fault->prm; struct iommu_fault_page_request *prm = &fault->prm;
......
...@@ -22,8 +22,7 @@ int iopf_queue_flush_dev(struct device *dev); ...@@ -22,8 +22,7 @@ int iopf_queue_flush_dev(struct device *dev);
struct iopf_queue *iopf_queue_alloc(const char *name); struct iopf_queue *iopf_queue_alloc(const char *name);
void iopf_queue_free(struct iopf_queue *queue); void iopf_queue_free(struct iopf_queue *queue);
int iopf_queue_discard_partial(struct iopf_queue *queue); int iopf_queue_discard_partial(struct iopf_queue *queue);
enum iommu_page_response_code int iommu_sva_handle_iopf(struct iopf_group *group);
iommu_sva_handle_iopf(struct iommu_fault *fault, void *data);
#else /* CONFIG_IOMMU_SVA */ #else /* CONFIG_IOMMU_SVA */
static inline int iommu_queue_iopf(struct iommu_fault *fault, struct device *dev) static inline int iommu_queue_iopf(struct iommu_fault *fault, struct device *dev)
...@@ -62,8 +61,7 @@ static inline int iopf_queue_discard_partial(struct iopf_queue *queue) ...@@ -62,8 +61,7 @@ static inline int iopf_queue_discard_partial(struct iopf_queue *queue)
return -ENODEV; return -ENODEV;
} }
static inline enum iommu_page_response_code static inline int iommu_sva_handle_iopf(struct iopf_group *group)
iommu_sva_handle_iopf(struct iommu_fault *fault, void *data)
{ {
return IOMMU_PAGE_RESP_INVALID; return IOMMU_PAGE_RESP_INVALID;
} }
......
...@@ -130,6 +130,7 @@ struct iopf_group { ...@@ -130,6 +130,7 @@ struct iopf_group {
struct list_head faults; struct list_head faults;
struct work_struct work; struct work_struct work;
struct device *dev; struct device *dev;
struct iommu_domain *domain;
}; };
/** /**
...@@ -209,8 +210,7 @@ struct iommu_domain { ...@@ -209,8 +210,7 @@ struct iommu_domain {
unsigned long pgsize_bitmap; /* Bitmap of page sizes in use */ unsigned long pgsize_bitmap; /* Bitmap of page sizes in use */
struct iommu_domain_geometry geometry; struct iommu_domain_geometry geometry;
struct iommu_dma_cookie *iova_cookie; struct iommu_dma_cookie *iova_cookie;
enum iommu_page_response_code (*iopf_handler)(struct iommu_fault *fault, int (*iopf_handler)(struct iopf_group *group);
void *data);
void *fault_data; void *fault_data;
union { union {
struct { struct {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment