2022-03-21 16:51:31

by Baolu Lu

[permalink] [raw]
Subject: [PATCH RFC 08/11] iommu: Handle IO page faults directly

Directly call the IO page fault handler in iommu_report_device_fault()
unless the device driver registered its own page fault handler. There
is no need to explicitly register the fault handler in IOMMU drivers.

Signed-off-by: Lu Baolu <[email protected]>
---
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c | 13 +------------
drivers/iommu/intel/iommu.c | 14 ++------------
drivers/iommu/iommu.c | 11 ++++++++---
3 files changed, 11 insertions(+), 27 deletions(-)

diff --git a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c
index 3537a2291d7c..8497425f3c13 100644
--- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c
+++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c
@@ -426,7 +426,6 @@ bool arm_smmu_master_sva_enabled(struct arm_smmu_master *master)

static int arm_smmu_master_sva_enable_iopf(struct arm_smmu_master *master)
{
- int ret;
struct device *dev = master->dev;

/*
@@ -439,16 +438,7 @@ static int arm_smmu_master_sva_enable_iopf(struct arm_smmu_master *master)
if (!master->iopf_enabled)
return -EINVAL;

- ret = iopf_queue_add_device(master->smmu->evtq.iopf, dev);
- if (ret)
- return ret;
-
- ret = iommu_register_device_fault_handler(dev, iommu_queue_iopf, dev);
- if (ret) {
- iopf_queue_remove_device(master->smmu->evtq.iopf, dev);
- return ret;
- }
- return 0;
+ return iopf_queue_add_device(master->smmu->evtq.iopf, dev);
}

static void arm_smmu_master_sva_disable_iopf(struct arm_smmu_master *master)
@@ -458,7 +448,6 @@ static void arm_smmu_master_sva_disable_iopf(struct arm_smmu_master *master)
if (!master->iopf_enabled)
return;

- iommu_unregister_device_fault_handler(dev);
iopf_queue_remove_device(master->smmu->evtq.iopf, dev);
}

diff --git a/drivers/iommu/intel/iommu.c b/drivers/iommu/intel/iommu.c
index 8781305b9a02..8f9cc66787c3 100644
--- a/drivers/iommu/intel/iommu.c
+++ b/drivers/iommu/intel/iommu.c
@@ -4760,7 +4760,6 @@ static int intel_iommu_enable_sva(struct device *dev)
{
struct device_domain_info *info = dev_iommu_priv_get(dev);
struct intel_iommu *iommu;
- int ret;

if (!info || dmar_disabled)
return -EINVAL;
@@ -4778,24 +4777,15 @@ static int intel_iommu_enable_sva(struct device *dev)
if (!info->pasid_enabled || !info->pri_enabled || !info->ats_enabled)
return -EINVAL;

- ret = iopf_queue_add_device(iommu->iopf_queue, dev);
- if (!ret)
- ret = iommu_register_device_fault_handler(dev, iommu_queue_iopf, dev);
-
- return ret;
+ return iopf_queue_add_device(iommu->iopf_queue, dev);
}

static int intel_iommu_disable_sva(struct device *dev)
{
struct device_domain_info *info = dev_iommu_priv_get(dev);
struct intel_iommu *iommu = info->iommu;
- int ret;
-
- ret = iommu_unregister_device_fault_handler(dev);
- if (!ret)
- ret = iopf_queue_remove_device(iommu->iopf_queue, dev);

- return ret;
+ return iopf_queue_remove_device(iommu->iopf_queue, dev);
}

static int intel_iommu_enable_iopf(struct device *dev)
diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c
index c0966fc9b686..4f90b71c6f6e 100644
--- a/drivers/iommu/iommu.c
+++ b/drivers/iommu/iommu.c
@@ -27,6 +27,8 @@
#include <linux/cc_platform.h>
#include <trace/events/iommu.h>

+#include "iommu-sva-lib.h"
+
static struct kset *iommu_group_kset;
static DEFINE_IDA(iommu_group_ida);

@@ -1177,10 +1179,9 @@ int iommu_report_device_fault(struct device *dev, struct iommu_fault_event *evt)
if (!param || !evt)
return -EINVAL;

- /* we only report device fault if there is a handler registered */
mutex_lock(&param->lock);
fparam = param->fault_param;
- if (!fparam || !fparam->handler) {
+ if (!fparam) {
ret = -EINVAL;
goto done_unlock;
}
@@ -1198,7 +1199,11 @@ int iommu_report_device_fault(struct device *dev, struct iommu_fault_event *evt)
mutex_unlock(&fparam->lock);
}

- ret = fparam->handler(&evt->fault, fparam->data);
+ if (fparam->handler)
+ ret = fparam->handler(&evt->fault, fparam->data);
+ else
+ ret = iommu_queue_iopf(&evt->fault, fparam->data);
+
if (ret && evt_pending) {
mutex_lock(&fparam->lock);
list_del(&evt_pending->list);
--
2.25.1


2022-03-21 21:07:51

by Jean-Philippe Brucker

[permalink] [raw]
Subject: Re: [PATCH RFC 08/11] iommu: Handle IO page faults directly

On Sun, Mar 20, 2022 at 02:40:27PM +0800, Lu Baolu wrote:
> diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c
> index c0966fc9b686..4f90b71c6f6e 100644
> --- a/drivers/iommu/iommu.c
> +++ b/drivers/iommu/iommu.c
> @@ -27,6 +27,8 @@
> #include <linux/cc_platform.h>
> #include <trace/events/iommu.h>
>
> +#include "iommu-sva-lib.h"
> +
> static struct kset *iommu_group_kset;
> static DEFINE_IDA(iommu_group_ida);
>
> @@ -1177,10 +1179,9 @@ int iommu_report_device_fault(struct device *dev, struct iommu_fault_event *evt)
> if (!param || !evt)
> return -EINVAL;
>
> - /* we only report device fault if there is a handler registered */
> mutex_lock(&param->lock);
> fparam = param->fault_param;
> - if (!fparam || !fparam->handler) {
> + if (!fparam) {
> ret = -EINVAL;
> goto done_unlock;
> }
> @@ -1198,7 +1199,11 @@ int iommu_report_device_fault(struct device *dev, struct iommu_fault_event *evt)
> mutex_unlock(&fparam->lock);
> }
>
> - ret = fparam->handler(&evt->fault, fparam->data);
> + if (fparam->handler)
> + ret = fparam->handler(&evt->fault, fparam->data);
> + else
> + ret = iommu_queue_iopf(&evt->fault, fparam->data);
> +

I like the change, but we'll need to consolidate this, because now if the
driver registers a fault handler it disables IOPF. We could instead
prevent registration if an IOPF param is present. We could also just merge
fparam->handler but eventually I'd like to make IOPF fall back to the
fault handler registered by device driver, in case of invalid page faults.
I have a couple patches for this but am still missing some bits.

Thanks,
Jean

> if (ret && evt_pending) {
> mutex_lock(&fparam->lock);
> list_del(&evt_pending->list);
> --
> 2.25.1
>

2022-03-22 01:05:57

by Tian, Kevin

[permalink] [raw]
Subject: RE: [PATCH RFC 08/11] iommu: Handle IO page faults directly

> From: Jean-Philippe Brucker <[email protected]>
> Sent: Monday, March 21, 2022 7:36 PM
>
> On Sun, Mar 20, 2022 at 02:40:27PM +0800, Lu Baolu wrote:
> > diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c
> > index c0966fc9b686..4f90b71c6f6e 100644
> > --- a/drivers/iommu/iommu.c
> > +++ b/drivers/iommu/iommu.c
> > @@ -27,6 +27,8 @@
> > #include <linux/cc_platform.h>
> > #include <trace/events/iommu.h>
> >
> > +#include "iommu-sva-lib.h"
> > +
> > static struct kset *iommu_group_kset;
> > static DEFINE_IDA(iommu_group_ida);
> >
> > @@ -1177,10 +1179,9 @@ int iommu_report_device_fault(struct device
> *dev, struct iommu_fault_event *evt)
> > if (!param || !evt)
> > return -EINVAL;
> >
> > - /* we only report device fault if there is a handler registered */
> > mutex_lock(&param->lock);
> > fparam = param->fault_param;
> > - if (!fparam || !fparam->handler) {
> > + if (!fparam) {
> > ret = -EINVAL;
> > goto done_unlock;
> > }
> > @@ -1198,7 +1199,11 @@ int iommu_report_device_fault(struct device
> *dev, struct iommu_fault_event *evt)
> > mutex_unlock(&fparam->lock);
> > }
> >
> > - ret = fparam->handler(&evt->fault, fparam->data);
> > + if (fparam->handler)
> > + ret = fparam->handler(&evt->fault, fparam->data);
> > + else
> > + ret = iommu_queue_iopf(&evt->fault, fparam->data);
> > +
>
> I like the change, but we'll need to consolidate this, because now if the
> driver registers a fault handler it disables IOPF. We could instead
> prevent registration if an IOPF param is present. We could also just merge
> fparam->handler but eventually I'd like to make IOPF fall back to the
> fault handler registered by device driver, in case of invalid page faults.
> I have a couple patches for this but am still missing some bits.
>

Probably we need two different fault reporting interfaces:

iommu_report_device_fault()
iommu_report_domain_fault()

iommu driver knows the type of faults and just calls individual API
accordingly. fallback is also managed by iommu driver if necessary.

Thanks
Kevin