All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2 1/1] vfio/type1: Add vfio_group_domain()
@ 2020-11-26  1:27 ` Lu Baolu
  0 siblings, 0 replies; 10+ messages in thread
From: Lu Baolu @ 2020-11-26  1:27 UTC (permalink / raw)
  To: Alex Williamson, Cornelia Huck
  Cc: Joerg Roedel, Will Deacon, Robin Murphy, Jean-Philippe Brucker,
	Kevin Tian, Ashok Raj, Dave Jiang, Liu Yi L, Zeng Xin, iommu,
	linux-kernel, kvm, Lu Baolu

Add the API for getting the domain from a vfio group. This could be used
by the physical device drivers which rely on the vfio/mdev framework for
mediated device user level access. The typical use case like below:

	unsigned int pasid;
	struct vfio_group *vfio_group;
	struct iommu_domain *iommu_domain;
	struct device *dev = mdev_dev(mdev);
	struct device *iommu_device = mdev_get_iommu_device(dev);

	if (!iommu_device ||
	    !iommu_dev_feature_enabled(iommu_device, IOMMU_DEV_FEAT_AUX))
		return -EINVAL;

	vfio_group = vfio_group_get_external_user_from_dev(dev);(dev);
	if (IS_ERR_OR_NULL(vfio_group))
		return -EFAULT;

	iommu_domain = vfio_group_domain(vfio_group);
	if (IS_ERR_OR_NULL(iommu_domain)) {
		vfio_group_put_external_user(vfio_group);
		return -EFAULT;
	}

	pasid = iommu_aux_get_pasid(iommu_domain, iommu_device);
	if (pasid < 0) {
		vfio_group_put_external_user(vfio_group);
		return -EFAULT;
	}

	/* Program device context with pasid value. */
	...

Signed-off-by: Lu Baolu <baolu.lu@linux.intel.com>
---
 drivers/vfio/vfio.c             | 18 ++++++++++++++++++
 drivers/vfio/vfio_iommu_type1.c | 23 +++++++++++++++++++++++
 include/linux/vfio.h            |  3 +++
 3 files changed, 44 insertions(+)

Change log:
 - v1: https://lore.kernel.org/linux-iommu/20201112022407.2063896-1-baolu.lu@linux.intel.com/
 - Changed according to comments @ https://lore.kernel.org/linux-iommu/20201116125631.2d043fcd@w520.home/

diff --git a/drivers/vfio/vfio.c b/drivers/vfio/vfio.c
index 2151bc7f87ab..62c652111c88 100644
--- a/drivers/vfio/vfio.c
+++ b/drivers/vfio/vfio.c
@@ -2331,6 +2331,24 @@ int vfio_unregister_notifier(struct device *dev, enum vfio_notify_type type,
 }
 EXPORT_SYMBOL(vfio_unregister_notifier);
 
+struct iommu_domain *vfio_group_domain(struct vfio_group *group)
+{
+	struct vfio_container *container;
+	struct vfio_iommu_driver *driver;
+
+	if (!group)
+		return ERR_PTR(-EINVAL);
+
+	container = group->container;
+	driver = container->iommu_driver;
+	if (likely(driver && driver->ops->group_domain))
+		return driver->ops->group_domain(container->iommu_data,
+						 group->iommu_group);
+	else
+		return ERR_PTR(-ENOTTY);
+}
+EXPORT_SYMBOL(vfio_group_domain);
+
 /**
  * Module/class support
  */
diff --git a/drivers/vfio/vfio_iommu_type1.c b/drivers/vfio/vfio_iommu_type1.c
index 67e827638995..783f18f21b95 100644
--- a/drivers/vfio/vfio_iommu_type1.c
+++ b/drivers/vfio/vfio_iommu_type1.c
@@ -2980,6 +2980,28 @@ static int vfio_iommu_type1_dma_rw(void *iommu_data, dma_addr_t user_iova,
 	return ret;
 }
 
+static void *vfio_iommu_type1_group_domain(void *iommu_data,
+					   struct iommu_group *iommu_group)
+{
+	struct vfio_iommu *iommu = iommu_data;
+	struct iommu_domain *domain = NULL;
+	struct vfio_domain *d;
+
+	if (!iommu || !iommu_group)
+		return ERR_PTR(-EINVAL);
+
+	mutex_lock(&iommu->lock);
+	list_for_each_entry(d, &iommu->domain_list, next) {
+		if (find_iommu_group(d, iommu_group)) {
+			domain = d->domain;
+			break;
+		}
+	}
+	mutex_unlock(&iommu->lock);
+
+	return domain;
+}
+
 static const struct vfio_iommu_driver_ops vfio_iommu_driver_ops_type1 = {
 	.name			= "vfio-iommu-type1",
 	.owner			= THIS_MODULE,
@@ -2993,6 +3015,7 @@ static const struct vfio_iommu_driver_ops vfio_iommu_driver_ops_type1 = {
 	.register_notifier	= vfio_iommu_type1_register_notifier,
 	.unregister_notifier	= vfio_iommu_type1_unregister_notifier,
 	.dma_rw			= vfio_iommu_type1_dma_rw,
+	.group_domain		= vfio_iommu_type1_group_domain,
 };
 
 static int __init vfio_iommu_type1_init(void)
diff --git a/include/linux/vfio.h b/include/linux/vfio.h
index 38d3c6a8dc7e..a0613a6f21cc 100644
--- a/include/linux/vfio.h
+++ b/include/linux/vfio.h
@@ -90,6 +90,7 @@ struct vfio_iommu_driver_ops {
 					       struct notifier_block *nb);
 	int		(*dma_rw)(void *iommu_data, dma_addr_t user_iova,
 				  void *data, size_t count, bool write);
+	void		*(*group_domain)(void *iommu_data, struct iommu_group *group);
 };
 
 extern int vfio_register_iommu_driver(const struct vfio_iommu_driver_ops *ops);
@@ -126,6 +127,8 @@ extern int vfio_group_unpin_pages(struct vfio_group *group,
 extern int vfio_dma_rw(struct vfio_group *group, dma_addr_t user_iova,
 		       void *data, size_t len, bool write);
 
+extern struct iommu_domain *vfio_group_domain(struct vfio_group *group);
+
 /* each type has independent events */
 enum vfio_notify_type {
 	VFIO_IOMMU_NOTIFY = 0,
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* [PATCH v2 1/1] vfio/type1: Add vfio_group_domain()
@ 2020-11-26  1:27 ` Lu Baolu
  0 siblings, 0 replies; 10+ messages in thread
From: Lu Baolu @ 2020-11-26  1:27 UTC (permalink / raw)
  To: Alex Williamson, Cornelia Huck
  Cc: Jean-Philippe Brucker, Kevin Tian, Dave Jiang, Ashok Raj, kvm,
	Will Deacon, linux-kernel, iommu, Robin Murphy

Add the API for getting the domain from a vfio group. This could be used
by the physical device drivers which rely on the vfio/mdev framework for
mediated device user level access. The typical use case like below:

	unsigned int pasid;
	struct vfio_group *vfio_group;
	struct iommu_domain *iommu_domain;
	struct device *dev = mdev_dev(mdev);
	struct device *iommu_device = mdev_get_iommu_device(dev);

	if (!iommu_device ||
	    !iommu_dev_feature_enabled(iommu_device, IOMMU_DEV_FEAT_AUX))
		return -EINVAL;

	vfio_group = vfio_group_get_external_user_from_dev(dev);(dev);
	if (IS_ERR_OR_NULL(vfio_group))
		return -EFAULT;

	iommu_domain = vfio_group_domain(vfio_group);
	if (IS_ERR_OR_NULL(iommu_domain)) {
		vfio_group_put_external_user(vfio_group);
		return -EFAULT;
	}

	pasid = iommu_aux_get_pasid(iommu_domain, iommu_device);
	if (pasid < 0) {
		vfio_group_put_external_user(vfio_group);
		return -EFAULT;
	}

	/* Program device context with pasid value. */
	...

Signed-off-by: Lu Baolu <baolu.lu@linux.intel.com>
---
 drivers/vfio/vfio.c             | 18 ++++++++++++++++++
 drivers/vfio/vfio_iommu_type1.c | 23 +++++++++++++++++++++++
 include/linux/vfio.h            |  3 +++
 3 files changed, 44 insertions(+)

Change log:
 - v1: https://lore.kernel.org/linux-iommu/20201112022407.2063896-1-baolu.lu@linux.intel.com/
 - Changed according to comments @ https://lore.kernel.org/linux-iommu/20201116125631.2d043fcd@w520.home/

diff --git a/drivers/vfio/vfio.c b/drivers/vfio/vfio.c
index 2151bc7f87ab..62c652111c88 100644
--- a/drivers/vfio/vfio.c
+++ b/drivers/vfio/vfio.c
@@ -2331,6 +2331,24 @@ int vfio_unregister_notifier(struct device *dev, enum vfio_notify_type type,
 }
 EXPORT_SYMBOL(vfio_unregister_notifier);
 
+struct iommu_domain *vfio_group_domain(struct vfio_group *group)
+{
+	struct vfio_container *container;
+	struct vfio_iommu_driver *driver;
+
+	if (!group)
+		return ERR_PTR(-EINVAL);
+
+	container = group->container;
+	driver = container->iommu_driver;
+	if (likely(driver && driver->ops->group_domain))
+		return driver->ops->group_domain(container->iommu_data,
+						 group->iommu_group);
+	else
+		return ERR_PTR(-ENOTTY);
+}
+EXPORT_SYMBOL(vfio_group_domain);
+
 /**
  * Module/class support
  */
diff --git a/drivers/vfio/vfio_iommu_type1.c b/drivers/vfio/vfio_iommu_type1.c
index 67e827638995..783f18f21b95 100644
--- a/drivers/vfio/vfio_iommu_type1.c
+++ b/drivers/vfio/vfio_iommu_type1.c
@@ -2980,6 +2980,28 @@ static int vfio_iommu_type1_dma_rw(void *iommu_data, dma_addr_t user_iova,
 	return ret;
 }
 
+static void *vfio_iommu_type1_group_domain(void *iommu_data,
+					   struct iommu_group *iommu_group)
+{
+	struct vfio_iommu *iommu = iommu_data;
+	struct iommu_domain *domain = NULL;
+	struct vfio_domain *d;
+
+	if (!iommu || !iommu_group)
+		return ERR_PTR(-EINVAL);
+
+	mutex_lock(&iommu->lock);
+	list_for_each_entry(d, &iommu->domain_list, next) {
+		if (find_iommu_group(d, iommu_group)) {
+			domain = d->domain;
+			break;
+		}
+	}
+	mutex_unlock(&iommu->lock);
+
+	return domain;
+}
+
 static const struct vfio_iommu_driver_ops vfio_iommu_driver_ops_type1 = {
 	.name			= "vfio-iommu-type1",
 	.owner			= THIS_MODULE,
@@ -2993,6 +3015,7 @@ static const struct vfio_iommu_driver_ops vfio_iommu_driver_ops_type1 = {
 	.register_notifier	= vfio_iommu_type1_register_notifier,
 	.unregister_notifier	= vfio_iommu_type1_unregister_notifier,
 	.dma_rw			= vfio_iommu_type1_dma_rw,
+	.group_domain		= vfio_iommu_type1_group_domain,
 };
 
 static int __init vfio_iommu_type1_init(void)
diff --git a/include/linux/vfio.h b/include/linux/vfio.h
index 38d3c6a8dc7e..a0613a6f21cc 100644
--- a/include/linux/vfio.h
+++ b/include/linux/vfio.h
@@ -90,6 +90,7 @@ struct vfio_iommu_driver_ops {
 					       struct notifier_block *nb);
 	int		(*dma_rw)(void *iommu_data, dma_addr_t user_iova,
 				  void *data, size_t count, bool write);
+	void		*(*group_domain)(void *iommu_data, struct iommu_group *group);
 };
 
 extern int vfio_register_iommu_driver(const struct vfio_iommu_driver_ops *ops);
@@ -126,6 +127,8 @@ extern int vfio_group_unpin_pages(struct vfio_group *group,
 extern int vfio_dma_rw(struct vfio_group *group, dma_addr_t user_iova,
 		       void *data, size_t len, bool write);
 
+extern struct iommu_domain *vfio_group_domain(struct vfio_group *group);
+
 /* each type has independent events */
 enum vfio_notify_type {
 	VFIO_IOMMU_NOTIFY = 0,
-- 
2.25.1

_______________________________________________
iommu mailing list
iommu@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/iommu

^ permalink raw reply related	[flat|nested] 10+ messages in thread

* RE: [PATCH v2 1/1] vfio/type1: Add vfio_group_domain()
  2020-11-26  1:27 ` Lu Baolu
@ 2020-11-26  7:21   ` Liu, Yi L
  -1 siblings, 0 replies; 10+ messages in thread
From: Liu, Yi L @ 2020-11-26  7:21 UTC (permalink / raw)
  To: Lu Baolu, Alex Williamson, Cornelia Huck
  Cc: Joerg Roedel, Will Deacon, Robin Murphy, Jean-Philippe Brucker,
	Tian, Kevin, Raj, Ashok, Jiang, Dave, Zeng, Xin, iommu,
	linux-kernel, kvm

On Thurs, Nov 26, 2020, at 9:27 AM, Lu Baolu wrote:
> Add the API for getting the domain from a vfio group. This could be used
> by the physical device drivers which rely on the vfio/mdev framework for
> mediated device user level access. The typical use case like below:
> 
> 	unsigned int pasid;
> 	struct vfio_group *vfio_group;
> 	struct iommu_domain *iommu_domain;
> 	struct device *dev = mdev_dev(mdev);
> 	struct device *iommu_device = mdev_get_iommu_device(dev);
> 
> 	if (!iommu_device ||
> 	    !iommu_dev_feature_enabled(iommu_device, IOMMU_DEV_FEAT_AUX))
> 		return -EINVAL;
> 
> 	vfio_group = vfio_group_get_external_user_from_dev(dev);(dev);

duplicate (dev); 😊other parts looks good to me. perhaps, you can also
describe that the release function of a sub-device fd should also call
vfio_group_put_external_user() to release its reference on the vfio_group.

Regards,
Yi Liu 

> 	if (IS_ERR_OR_NULL(vfio_group))
> 		return -EFAULT;
> 
> 	iommu_domain = vfio_group_domain(vfio_group);
> 	if (IS_ERR_OR_NULL(iommu_domain)) {
> 		vfio_group_put_external_user(vfio_group);
> 		return -EFAULT;
> 	}
> 
> 	pasid = iommu_aux_get_pasid(iommu_domain, iommu_device);
> 	if (pasid < 0) {
> 		vfio_group_put_external_user(vfio_group);
> 		return -EFAULT;
> 	}
> 
> 	/* Program device context with pasid value. */
> 	...
> 
> Signed-off-by: Lu Baolu <baolu.lu@linux.intel.com>
> ---
>  drivers/vfio/vfio.c             | 18 ++++++++++++++++++
>  drivers/vfio/vfio_iommu_type1.c | 23 +++++++++++++++++++++++
>  include/linux/vfio.h            |  3 +++
>  3 files changed, 44 insertions(+)
> 
> Change log:
>  - v1: https://lore.kernel.org/linux-iommu/20201112022407.2063896-1-baolu.lu@linux.intel.com/
>  - Changed according to comments @ https://lore.kernel.org/linux-iommu/20201116125631.2d043fcd@w520.home/
> 
> diff --git a/drivers/vfio/vfio.c b/drivers/vfio/vfio.c
> index 2151bc7f87ab..62c652111c88 100644
> --- a/drivers/vfio/vfio.c
> +++ b/drivers/vfio/vfio.c
> @@ -2331,6 +2331,24 @@ int vfio_unregister_notifier(struct device *dev,
> enum vfio_notify_type type,
>  }
>  EXPORT_SYMBOL(vfio_unregister_notifier);
> 
> +struct iommu_domain *vfio_group_domain(struct vfio_group *group)
> +{
> +	struct vfio_container *container;
> +	struct vfio_iommu_driver *driver;
> +
> +	if (!group)
> +		return ERR_PTR(-EINVAL);
> +
> +	container = group->container;
> +	driver = container->iommu_driver;
> +	if (likely(driver && driver->ops->group_domain))
> +		return driver->ops->group_domain(container->iommu_data,
> +						 group->iommu_group);
> +	else
> +		return ERR_PTR(-ENOTTY);
> +}
> +EXPORT_SYMBOL(vfio_group_domain);
> +
>  /**
>   * Module/class support
>   */
> diff --git a/drivers/vfio/vfio_iommu_type1.c b/drivers/vfio/vfio_iommu_type1.c
> index 67e827638995..783f18f21b95 100644
> --- a/drivers/vfio/vfio_iommu_type1.c
> +++ b/drivers/vfio/vfio_iommu_type1.c
> @@ -2980,6 +2980,28 @@ static int vfio_iommu_type1_dma_rw(void *iommu_data,
> dma_addr_t user_iova,
>  	return ret;
>  }
> 
> +static void *vfio_iommu_type1_group_domain(void *iommu_data,
> +					   struct iommu_group *iommu_group)
> +{
> +	struct vfio_iommu *iommu = iommu_data;
> +	struct iommu_domain *domain = NULL;
> +	struct vfio_domain *d;
> +
> +	if (!iommu || !iommu_group)
> +		return ERR_PTR(-EINVAL);
> +
> +	mutex_lock(&iommu->lock);
> +	list_for_each_entry(d, &iommu->domain_list, next) {
> +		if (find_iommu_group(d, iommu_group)) {
> +			domain = d->domain;
> +			break;
> +		}
> +	}
> +	mutex_unlock(&iommu->lock);
> +
> +	return domain;
> +}
> +
>  static const struct vfio_iommu_driver_ops vfio_iommu_driver_ops_type1 = {
>  	.name			= "vfio-iommu-type1",
>  	.owner			= THIS_MODULE,
> @@ -2993,6 +3015,7 @@ static const struct vfio_iommu_driver_ops vfio_iommu_driver_ops_type1 = {
>  	.register_notifier	= vfio_iommu_type1_register_notifier,
>  	.unregister_notifier	= vfio_iommu_type1_unregister_notifier,
>  	.dma_rw			= vfio_iommu_type1_dma_rw,
> +	.group_domain		= vfio_iommu_type1_group_domain,
>  };
> 
>  static int __init vfio_iommu_type1_init(void)
> diff --git a/include/linux/vfio.h b/include/linux/vfio.h
> index 38d3c6a8dc7e..a0613a6f21cc 100644
> --- a/include/linux/vfio.h
> +++ b/include/linux/vfio.h
> @@ -90,6 +90,7 @@ struct vfio_iommu_driver_ops {
>  					       struct notifier_block *nb);
>  	int		(*dma_rw)(void *iommu_data, dma_addr_t user_iova,
>  				  void *data, size_t count, bool write);
> +	void		*(*group_domain)(void *iommu_data, struct iommu_group *group);
>  };
> 
>  extern int vfio_register_iommu_driver(const struct vfio_iommu_driver_ops *ops);
> @@ -126,6 +127,8 @@ extern int vfio_group_unpin_pages(struct vfio_group *group,
>  extern int vfio_dma_rw(struct vfio_group *group, dma_addr_t user_iova,
>  		       void *data, size_t len, bool write);
> 
> +extern struct iommu_domain *vfio_group_domain(struct vfio_group *group);
> +
>  /* each type has independent events */
>  enum vfio_notify_type {
>  	VFIO_IOMMU_NOTIFY = 0,
> --
> 2.25.1


^ permalink raw reply	[flat|nested] 10+ messages in thread

* RE: [PATCH v2 1/1] vfio/type1: Add vfio_group_domain()
@ 2020-11-26  7:21   ` Liu, Yi L
  0 siblings, 0 replies; 10+ messages in thread
From: Liu, Yi L @ 2020-11-26  7:21 UTC (permalink / raw)
  To: Lu Baolu, Alex Williamson, Cornelia Huck
  Cc: Jean-Philippe Brucker, Tian, Kevin, Jiang, Dave, Raj,  Ashok,
	kvm, Will Deacon, linux-kernel, iommu, Robin Murphy

On Thurs, Nov 26, 2020, at 9:27 AM, Lu Baolu wrote:
> Add the API for getting the domain from a vfio group. This could be used
> by the physical device drivers which rely on the vfio/mdev framework for
> mediated device user level access. The typical use case like below:
> 
> 	unsigned int pasid;
> 	struct vfio_group *vfio_group;
> 	struct iommu_domain *iommu_domain;
> 	struct device *dev = mdev_dev(mdev);
> 	struct device *iommu_device = mdev_get_iommu_device(dev);
> 
> 	if (!iommu_device ||
> 	    !iommu_dev_feature_enabled(iommu_device, IOMMU_DEV_FEAT_AUX))
> 		return -EINVAL;
> 
> 	vfio_group = vfio_group_get_external_user_from_dev(dev);(dev);

duplicate (dev); 😊other parts looks good to me. perhaps, you can also
describe that the release function of a sub-device fd should also call
vfio_group_put_external_user() to release its reference on the vfio_group.

Regards,
Yi Liu 

> 	if (IS_ERR_OR_NULL(vfio_group))
> 		return -EFAULT;
> 
> 	iommu_domain = vfio_group_domain(vfio_group);
> 	if (IS_ERR_OR_NULL(iommu_domain)) {
> 		vfio_group_put_external_user(vfio_group);
> 		return -EFAULT;
> 	}
> 
> 	pasid = iommu_aux_get_pasid(iommu_domain, iommu_device);
> 	if (pasid < 0) {
> 		vfio_group_put_external_user(vfio_group);
> 		return -EFAULT;
> 	}
> 
> 	/* Program device context with pasid value. */
> 	...
> 
> Signed-off-by: Lu Baolu <baolu.lu@linux.intel.com>
> ---
>  drivers/vfio/vfio.c             | 18 ++++++++++++++++++
>  drivers/vfio/vfio_iommu_type1.c | 23 +++++++++++++++++++++++
>  include/linux/vfio.h            |  3 +++
>  3 files changed, 44 insertions(+)
> 
> Change log:
>  - v1: https://lore.kernel.org/linux-iommu/20201112022407.2063896-1-baolu.lu@linux.intel.com/
>  - Changed according to comments @ https://lore.kernel.org/linux-iommu/20201116125631.2d043fcd@w520.home/
> 
> diff --git a/drivers/vfio/vfio.c b/drivers/vfio/vfio.c
> index 2151bc7f87ab..62c652111c88 100644
> --- a/drivers/vfio/vfio.c
> +++ b/drivers/vfio/vfio.c
> @@ -2331,6 +2331,24 @@ int vfio_unregister_notifier(struct device *dev,
> enum vfio_notify_type type,
>  }
>  EXPORT_SYMBOL(vfio_unregister_notifier);
> 
> +struct iommu_domain *vfio_group_domain(struct vfio_group *group)
> +{
> +	struct vfio_container *container;
> +	struct vfio_iommu_driver *driver;
> +
> +	if (!group)
> +		return ERR_PTR(-EINVAL);
> +
> +	container = group->container;
> +	driver = container->iommu_driver;
> +	if (likely(driver && driver->ops->group_domain))
> +		return driver->ops->group_domain(container->iommu_data,
> +						 group->iommu_group);
> +	else
> +		return ERR_PTR(-ENOTTY);
> +}
> +EXPORT_SYMBOL(vfio_group_domain);
> +
>  /**
>   * Module/class support
>   */
> diff --git a/drivers/vfio/vfio_iommu_type1.c b/drivers/vfio/vfio_iommu_type1.c
> index 67e827638995..783f18f21b95 100644
> --- a/drivers/vfio/vfio_iommu_type1.c
> +++ b/drivers/vfio/vfio_iommu_type1.c
> @@ -2980,6 +2980,28 @@ static int vfio_iommu_type1_dma_rw(void *iommu_data,
> dma_addr_t user_iova,
>  	return ret;
>  }
> 
> +static void *vfio_iommu_type1_group_domain(void *iommu_data,
> +					   struct iommu_group *iommu_group)
> +{
> +	struct vfio_iommu *iommu = iommu_data;
> +	struct iommu_domain *domain = NULL;
> +	struct vfio_domain *d;
> +
> +	if (!iommu || !iommu_group)
> +		return ERR_PTR(-EINVAL);
> +
> +	mutex_lock(&iommu->lock);
> +	list_for_each_entry(d, &iommu->domain_list, next) {
> +		if (find_iommu_group(d, iommu_group)) {
> +			domain = d->domain;
> +			break;
> +		}
> +	}
> +	mutex_unlock(&iommu->lock);
> +
> +	return domain;
> +}
> +
>  static const struct vfio_iommu_driver_ops vfio_iommu_driver_ops_type1 = {
>  	.name			= "vfio-iommu-type1",
>  	.owner			= THIS_MODULE,
> @@ -2993,6 +3015,7 @@ static const struct vfio_iommu_driver_ops vfio_iommu_driver_ops_type1 = {
>  	.register_notifier	= vfio_iommu_type1_register_notifier,
>  	.unregister_notifier	= vfio_iommu_type1_unregister_notifier,
>  	.dma_rw			= vfio_iommu_type1_dma_rw,
> +	.group_domain		= vfio_iommu_type1_group_domain,
>  };
> 
>  static int __init vfio_iommu_type1_init(void)
> diff --git a/include/linux/vfio.h b/include/linux/vfio.h
> index 38d3c6a8dc7e..a0613a6f21cc 100644
> --- a/include/linux/vfio.h
> +++ b/include/linux/vfio.h
> @@ -90,6 +90,7 @@ struct vfio_iommu_driver_ops {
>  					       struct notifier_block *nb);
>  	int		(*dma_rw)(void *iommu_data, dma_addr_t user_iova,
>  				  void *data, size_t count, bool write);
> +	void		*(*group_domain)(void *iommu_data, struct iommu_group *group);
>  };
> 
>  extern int vfio_register_iommu_driver(const struct vfio_iommu_driver_ops *ops);
> @@ -126,6 +127,8 @@ extern int vfio_group_unpin_pages(struct vfio_group *group,
>  extern int vfio_dma_rw(struct vfio_group *group, dma_addr_t user_iova,
>  		       void *data, size_t len, bool write);
> 
> +extern struct iommu_domain *vfio_group_domain(struct vfio_group *group);
> +
>  /* each type has independent events */
>  enum vfio_notify_type {
>  	VFIO_IOMMU_NOTIFY = 0,
> --
> 2.25.1

_______________________________________________
iommu mailing list
iommu@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/iommu

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH v2 1/1] vfio/type1: Add vfio_group_domain()
  2020-11-26  7:21   ` Liu, Yi L
@ 2020-11-26  8:50     ` Lu Baolu
  -1 siblings, 0 replies; 10+ messages in thread
From: Lu Baolu @ 2020-11-26  8:50 UTC (permalink / raw)
  To: Liu, Yi L, Alex Williamson, Cornelia Huck
  Cc: baolu.lu, Joerg Roedel, Will Deacon, Robin Murphy,
	Jean-Philippe Brucker, Tian, Kevin, Raj, Ashok, Jiang, Dave,
	Zeng, Xin, iommu, linux-kernel, kvm

Hi Yi,

On 2020/11/26 15:21, Liu, Yi L wrote:
> On Thurs, Nov 26, 2020, at 9:27 AM, Lu Baolu wrote:
>> Add the API for getting the domain from a vfio group. This could be used
>> by the physical device drivers which rely on the vfio/mdev framework for
>> mediated device user level access. The typical use case like below:
>>
>> 	unsigned int pasid;
>> 	struct vfio_group *vfio_group;
>> 	struct iommu_domain *iommu_domain;
>> 	struct device *dev = mdev_dev(mdev);
>> 	struct device *iommu_device = mdev_get_iommu_device(dev);
>>
>> 	if (!iommu_device ||
>> 	    !iommu_dev_feature_enabled(iommu_device, IOMMU_DEV_FEAT_AUX))
>> 		return -EINVAL;
>>
>> 	vfio_group = vfio_group_get_external_user_from_dev(dev);(dev);
> 
> duplicate (dev); 😊other parts looks good to me. perhaps, you can also

Will fix this typo. Thanks!

> describe that the release function of a sub-device fd should also call
> vfio_group_put_external_user() to release its reference on the vfio_group.

This is just a sample code. The callers are free to decide when to get
and release the vfio_group.

> 
> Regards,
> Yi Liu

Best regards,
baolu

> 
>> 	if (IS_ERR_OR_NULL(vfio_group))
>> 		return -EFAULT;
>>
>> 	iommu_domain = vfio_group_domain(vfio_group);
>> 	if (IS_ERR_OR_NULL(iommu_domain)) {
>> 		vfio_group_put_external_user(vfio_group);
>> 		return -EFAULT;
>> 	}
>>
>> 	pasid = iommu_aux_get_pasid(iommu_domain, iommu_device);
>> 	if (pasid < 0) {
>> 		vfio_group_put_external_user(vfio_group);
>> 		return -EFAULT;
>> 	}
>>
>> 	/* Program device context with pasid value. */
>> 	...
>>
>> Signed-off-by: Lu Baolu <baolu.lu@linux.intel.com>
>> ---
>>   drivers/vfio/vfio.c             | 18 ++++++++++++++++++
>>   drivers/vfio/vfio_iommu_type1.c | 23 +++++++++++++++++++++++
>>   include/linux/vfio.h            |  3 +++
>>   3 files changed, 44 insertions(+)
>>
>> Change log:
>>   - v1: https://lore.kernel.org/linux-iommu/20201112022407.2063896-1-baolu.lu@linux.intel.com/
>>   - Changed according to comments @ https://lore.kernel.org/linux-iommu/20201116125631.2d043fcd@w520.home/
>>
>> diff --git a/drivers/vfio/vfio.c b/drivers/vfio/vfio.c
>> index 2151bc7f87ab..62c652111c88 100644
>> --- a/drivers/vfio/vfio.c
>> +++ b/drivers/vfio/vfio.c
>> @@ -2331,6 +2331,24 @@ int vfio_unregister_notifier(struct device *dev,
>> enum vfio_notify_type type,
>>   }
>>   EXPORT_SYMBOL(vfio_unregister_notifier);
>>
>> +struct iommu_domain *vfio_group_domain(struct vfio_group *group)
>> +{
>> +	struct vfio_container *container;
>> +	struct vfio_iommu_driver *driver;
>> +
>> +	if (!group)
>> +		return ERR_PTR(-EINVAL);
>> +
>> +	container = group->container;
>> +	driver = container->iommu_driver;
>> +	if (likely(driver && driver->ops->group_domain))
>> +		return driver->ops->group_domain(container->iommu_data,
>> +						 group->iommu_group);
>> +	else
>> +		return ERR_PTR(-ENOTTY);
>> +}
>> +EXPORT_SYMBOL(vfio_group_domain);
>> +
>>   /**
>>    * Module/class support
>>    */
>> diff --git a/drivers/vfio/vfio_iommu_type1.c b/drivers/vfio/vfio_iommu_type1.c
>> index 67e827638995..783f18f21b95 100644
>> --- a/drivers/vfio/vfio_iommu_type1.c
>> +++ b/drivers/vfio/vfio_iommu_type1.c
>> @@ -2980,6 +2980,28 @@ static int vfio_iommu_type1_dma_rw(void *iommu_data,
>> dma_addr_t user_iova,
>>   	return ret;
>>   }
>>
>> +static void *vfio_iommu_type1_group_domain(void *iommu_data,
>> +					   struct iommu_group *iommu_group)
>> +{
>> +	struct vfio_iommu *iommu = iommu_data;
>> +	struct iommu_domain *domain = NULL;
>> +	struct vfio_domain *d;
>> +
>> +	if (!iommu || !iommu_group)
>> +		return ERR_PTR(-EINVAL);
>> +
>> +	mutex_lock(&iommu->lock);
>> +	list_for_each_entry(d, &iommu->domain_list, next) {
>> +		if (find_iommu_group(d, iommu_group)) {
>> +			domain = d->domain;
>> +			break;
>> +		}
>> +	}
>> +	mutex_unlock(&iommu->lock);
>> +
>> +	return domain;
>> +}
>> +
>>   static const struct vfio_iommu_driver_ops vfio_iommu_driver_ops_type1 = {
>>   	.name			= "vfio-iommu-type1",
>>   	.owner			= THIS_MODULE,
>> @@ -2993,6 +3015,7 @@ static const struct vfio_iommu_driver_ops vfio_iommu_driver_ops_type1 = {
>>   	.register_notifier	= vfio_iommu_type1_register_notifier,
>>   	.unregister_notifier	= vfio_iommu_type1_unregister_notifier,
>>   	.dma_rw			= vfio_iommu_type1_dma_rw,
>> +	.group_domain		= vfio_iommu_type1_group_domain,
>>   };
>>
>>   static int __init vfio_iommu_type1_init(void)
>> diff --git a/include/linux/vfio.h b/include/linux/vfio.h
>> index 38d3c6a8dc7e..a0613a6f21cc 100644
>> --- a/include/linux/vfio.h
>> +++ b/include/linux/vfio.h
>> @@ -90,6 +90,7 @@ struct vfio_iommu_driver_ops {
>>   					       struct notifier_block *nb);
>>   	int		(*dma_rw)(void *iommu_data, dma_addr_t user_iova,
>>   				  void *data, size_t count, bool write);
>> +	void		*(*group_domain)(void *iommu_data, struct iommu_group *group);
>>   };
>>
>>   extern int vfio_register_iommu_driver(const struct vfio_iommu_driver_ops *ops);
>> @@ -126,6 +127,8 @@ extern int vfio_group_unpin_pages(struct vfio_group *group,
>>   extern int vfio_dma_rw(struct vfio_group *group, dma_addr_t user_iova,
>>   		       void *data, size_t len, bool write);
>>
>> +extern struct iommu_domain *vfio_group_domain(struct vfio_group *group);
>> +
>>   /* each type has independent events */
>>   enum vfio_notify_type {
>>   	VFIO_IOMMU_NOTIFY = 0,
>> --
>> 2.25.1
> 

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH v2 1/1] vfio/type1: Add vfio_group_domain()
@ 2020-11-26  8:50     ` Lu Baolu
  0 siblings, 0 replies; 10+ messages in thread
From: Lu Baolu @ 2020-11-26  8:50 UTC (permalink / raw)
  To: Liu, Yi L, Alex Williamson, Cornelia Huck
  Cc: Jean-Philippe Brucker, Tian, Kevin, Jiang, Dave, Raj, Ashok, kvm,
	Will Deacon, linux-kernel, iommu, Robin Murphy

Hi Yi,

On 2020/11/26 15:21, Liu, Yi L wrote:
> On Thurs, Nov 26, 2020, at 9:27 AM, Lu Baolu wrote:
>> Add the API for getting the domain from a vfio group. This could be used
>> by the physical device drivers which rely on the vfio/mdev framework for
>> mediated device user level access. The typical use case like below:
>>
>> 	unsigned int pasid;
>> 	struct vfio_group *vfio_group;
>> 	struct iommu_domain *iommu_domain;
>> 	struct device *dev = mdev_dev(mdev);
>> 	struct device *iommu_device = mdev_get_iommu_device(dev);
>>
>> 	if (!iommu_device ||
>> 	    !iommu_dev_feature_enabled(iommu_device, IOMMU_DEV_FEAT_AUX))
>> 		return -EINVAL;
>>
>> 	vfio_group = vfio_group_get_external_user_from_dev(dev);(dev);
> 
> duplicate (dev); 😊other parts looks good to me. perhaps, you can also

Will fix this typo. Thanks!

> describe that the release function of a sub-device fd should also call
> vfio_group_put_external_user() to release its reference on the vfio_group.

This is just a sample code. The callers are free to decide when to get
and release the vfio_group.

> 
> Regards,
> Yi Liu

Best regards,
baolu

> 
>> 	if (IS_ERR_OR_NULL(vfio_group))
>> 		return -EFAULT;
>>
>> 	iommu_domain = vfio_group_domain(vfio_group);
>> 	if (IS_ERR_OR_NULL(iommu_domain)) {
>> 		vfio_group_put_external_user(vfio_group);
>> 		return -EFAULT;
>> 	}
>>
>> 	pasid = iommu_aux_get_pasid(iommu_domain, iommu_device);
>> 	if (pasid < 0) {
>> 		vfio_group_put_external_user(vfio_group);
>> 		return -EFAULT;
>> 	}
>>
>> 	/* Program device context with pasid value. */
>> 	...
>>
>> Signed-off-by: Lu Baolu <baolu.lu@linux.intel.com>
>> ---
>>   drivers/vfio/vfio.c             | 18 ++++++++++++++++++
>>   drivers/vfio/vfio_iommu_type1.c | 23 +++++++++++++++++++++++
>>   include/linux/vfio.h            |  3 +++
>>   3 files changed, 44 insertions(+)
>>
>> Change log:
>>   - v1: https://lore.kernel.org/linux-iommu/20201112022407.2063896-1-baolu.lu@linux.intel.com/
>>   - Changed according to comments @ https://lore.kernel.org/linux-iommu/20201116125631.2d043fcd@w520.home/
>>
>> diff --git a/drivers/vfio/vfio.c b/drivers/vfio/vfio.c
>> index 2151bc7f87ab..62c652111c88 100644
>> --- a/drivers/vfio/vfio.c
>> +++ b/drivers/vfio/vfio.c
>> @@ -2331,6 +2331,24 @@ int vfio_unregister_notifier(struct device *dev,
>> enum vfio_notify_type type,
>>   }
>>   EXPORT_SYMBOL(vfio_unregister_notifier);
>>
>> +struct iommu_domain *vfio_group_domain(struct vfio_group *group)
>> +{
>> +	struct vfio_container *container;
>> +	struct vfio_iommu_driver *driver;
>> +
>> +	if (!group)
>> +		return ERR_PTR(-EINVAL);
>> +
>> +	container = group->container;
>> +	driver = container->iommu_driver;
>> +	if (likely(driver && driver->ops->group_domain))
>> +		return driver->ops->group_domain(container->iommu_data,
>> +						 group->iommu_group);
>> +	else
>> +		return ERR_PTR(-ENOTTY);
>> +}
>> +EXPORT_SYMBOL(vfio_group_domain);
>> +
>>   /**
>>    * Module/class support
>>    */
>> diff --git a/drivers/vfio/vfio_iommu_type1.c b/drivers/vfio/vfio_iommu_type1.c
>> index 67e827638995..783f18f21b95 100644
>> --- a/drivers/vfio/vfio_iommu_type1.c
>> +++ b/drivers/vfio/vfio_iommu_type1.c
>> @@ -2980,6 +2980,28 @@ static int vfio_iommu_type1_dma_rw(void *iommu_data,
>> dma_addr_t user_iova,
>>   	return ret;
>>   }
>>
>> +static void *vfio_iommu_type1_group_domain(void *iommu_data,
>> +					   struct iommu_group *iommu_group)
>> +{
>> +	struct vfio_iommu *iommu = iommu_data;
>> +	struct iommu_domain *domain = NULL;
>> +	struct vfio_domain *d;
>> +
>> +	if (!iommu || !iommu_group)
>> +		return ERR_PTR(-EINVAL);
>> +
>> +	mutex_lock(&iommu->lock);
>> +	list_for_each_entry(d, &iommu->domain_list, next) {
>> +		if (find_iommu_group(d, iommu_group)) {
>> +			domain = d->domain;
>> +			break;
>> +		}
>> +	}
>> +	mutex_unlock(&iommu->lock);
>> +
>> +	return domain;
>> +}
>> +
>>   static const struct vfio_iommu_driver_ops vfio_iommu_driver_ops_type1 = {
>>   	.name			= "vfio-iommu-type1",
>>   	.owner			= THIS_MODULE,
>> @@ -2993,6 +3015,7 @@ static const struct vfio_iommu_driver_ops vfio_iommu_driver_ops_type1 = {
>>   	.register_notifier	= vfio_iommu_type1_register_notifier,
>>   	.unregister_notifier	= vfio_iommu_type1_unregister_notifier,
>>   	.dma_rw			= vfio_iommu_type1_dma_rw,
>> +	.group_domain		= vfio_iommu_type1_group_domain,
>>   };
>>
>>   static int __init vfio_iommu_type1_init(void)
>> diff --git a/include/linux/vfio.h b/include/linux/vfio.h
>> index 38d3c6a8dc7e..a0613a6f21cc 100644
>> --- a/include/linux/vfio.h
>> +++ b/include/linux/vfio.h
>> @@ -90,6 +90,7 @@ struct vfio_iommu_driver_ops {
>>   					       struct notifier_block *nb);
>>   	int		(*dma_rw)(void *iommu_data, dma_addr_t user_iova,
>>   				  void *data, size_t count, bool write);
>> +	void		*(*group_domain)(void *iommu_data, struct iommu_group *group);
>>   };
>>
>>   extern int vfio_register_iommu_driver(const struct vfio_iommu_driver_ops *ops);
>> @@ -126,6 +127,8 @@ extern int vfio_group_unpin_pages(struct vfio_group *group,
>>   extern int vfio_dma_rw(struct vfio_group *group, dma_addr_t user_iova,
>>   		       void *data, size_t len, bool write);
>>
>> +extern struct iommu_domain *vfio_group_domain(struct vfio_group *group);
>> +
>>   /* each type has independent events */
>>   enum vfio_notify_type {
>>   	VFIO_IOMMU_NOTIFY = 0,
>> --
>> 2.25.1
> 
_______________________________________________
iommu mailing list
iommu@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/iommu

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH v2 1/1] vfio/type1: Add vfio_group_domain()
  2020-11-26  1:27 ` Lu Baolu
@ 2020-11-30 20:57   ` Alex Williamson
  -1 siblings, 0 replies; 10+ messages in thread
From: Alex Williamson @ 2020-11-30 20:57 UTC (permalink / raw)
  To: Lu Baolu
  Cc: Cornelia Huck, Joerg Roedel, Will Deacon, Robin Murphy,
	Jean-Philippe Brucker, Kevin Tian, Ashok Raj, Dave Jiang,
	Liu Yi L, Zeng Xin, iommu, linux-kernel, kvm

On Thu, 26 Nov 2020 09:27:26 +0800
Lu Baolu <baolu.lu@linux.intel.com> wrote:

> Add the API for getting the domain from a vfio group. This could be used
> by the physical device drivers which rely on the vfio/mdev framework for
> mediated device user level access. The typical use case like below:
> 
> 	unsigned int pasid;
> 	struct vfio_group *vfio_group;
> 	struct iommu_domain *iommu_domain;
> 	struct device *dev = mdev_dev(mdev);
> 	struct device *iommu_device = mdev_get_iommu_device(dev);
> 
> 	if (!iommu_device ||
> 	    !iommu_dev_feature_enabled(iommu_device, IOMMU_DEV_FEAT_AUX))
> 		return -EINVAL;
> 
> 	vfio_group = vfio_group_get_external_user_from_dev(dev);(dev);
> 	if (IS_ERR_OR_NULL(vfio_group))
> 		return -EFAULT;
> 
> 	iommu_domain = vfio_group_domain(vfio_group);
> 	if (IS_ERR_OR_NULL(iommu_domain)) {
> 		vfio_group_put_external_user(vfio_group);
> 		return -EFAULT;
> 	}
> 
> 	pasid = iommu_aux_get_pasid(iommu_domain, iommu_device);
> 	if (pasid < 0) {
> 		vfio_group_put_external_user(vfio_group);
> 		return -EFAULT;
> 	}
> 
> 	/* Program device context with pasid value. */
> 	...
> 
> Signed-off-by: Lu Baolu <baolu.lu@linux.intel.com>
> ---
>  drivers/vfio/vfio.c             | 18 ++++++++++++++++++
>  drivers/vfio/vfio_iommu_type1.c | 23 +++++++++++++++++++++++
>  include/linux/vfio.h            |  3 +++
>  3 files changed, 44 insertions(+)
> 
> Change log:
>  - v1: https://lore.kernel.org/linux-iommu/20201112022407.2063896-1-baolu.lu@linux.intel.com/
>  - Changed according to comments @ https://lore.kernel.org/linux-iommu/20201116125631.2d043fcd@w520.home/
> 
> diff --git a/drivers/vfio/vfio.c b/drivers/vfio/vfio.c
> index 2151bc7f87ab..62c652111c88 100644
> --- a/drivers/vfio/vfio.c
> +++ b/drivers/vfio/vfio.c
> @@ -2331,6 +2331,24 @@ int vfio_unregister_notifier(struct device *dev, enum vfio_notify_type type,
>  }
>  EXPORT_SYMBOL(vfio_unregister_notifier);
>  
> +struct iommu_domain *vfio_group_domain(struct vfio_group *group)
> +{
> +	struct vfio_container *container;
> +	struct vfio_iommu_driver *driver;
> +
> +	if (!group)
> +		return ERR_PTR(-EINVAL);
> +
> +	container = group->container;
> +	driver = container->iommu_driver;
> +	if (likely(driver && driver->ops->group_domain))
> +		return driver->ops->group_domain(container->iommu_data,
> +						 group->iommu_group);
> +	else
> +		return ERR_PTR(-ENOTTY);
> +}
> +EXPORT_SYMBOL(vfio_group_domain);


_GPL?  I don't see that there's a way for a driver to get the
vfio_group pointer that's not already _GPL.


> +
>  /**
>   * Module/class support
>   */
> diff --git a/drivers/vfio/vfio_iommu_type1.c b/drivers/vfio/vfio_iommu_type1.c
> index 67e827638995..783f18f21b95 100644
> --- a/drivers/vfio/vfio_iommu_type1.c
> +++ b/drivers/vfio/vfio_iommu_type1.c
> @@ -2980,6 +2980,28 @@ static int vfio_iommu_type1_dma_rw(void *iommu_data, dma_addr_t user_iova,
>  	return ret;
>  }
>  
> +static void *vfio_iommu_type1_group_domain(void *iommu_data,
> +					   struct iommu_group *iommu_group)
> +{
> +	struct vfio_iommu *iommu = iommu_data;
> +	struct iommu_domain *domain = NULL;
> +	struct vfio_domain *d;
> +
> +	if (!iommu || !iommu_group)
> +		return ERR_PTR(-EINVAL);
> +
> +	mutex_lock(&iommu->lock);
> +	list_for_each_entry(d, &iommu->domain_list, next) {
> +		if (find_iommu_group(d, iommu_group)) {
> +			domain = d->domain;
> +			break;
> +		}
> +	}
> +	mutex_unlock(&iommu->lock);
> +
> +	return domain;
> +}


Why does this return void* rather than struct iommu_domain*, and why
does the error case return an ERR_PTR but the not-found case returns
NULL?  Thanks,

Alex


> +
>  static const struct vfio_iommu_driver_ops vfio_iommu_driver_ops_type1 = {
>  	.name			= "vfio-iommu-type1",
>  	.owner			= THIS_MODULE,
> @@ -2993,6 +3015,7 @@ static const struct vfio_iommu_driver_ops vfio_iommu_driver_ops_type1 = {
>  	.register_notifier	= vfio_iommu_type1_register_notifier,
>  	.unregister_notifier	= vfio_iommu_type1_unregister_notifier,
>  	.dma_rw			= vfio_iommu_type1_dma_rw,
> +	.group_domain		= vfio_iommu_type1_group_domain,
>  };
>  
>  static int __init vfio_iommu_type1_init(void)
> diff --git a/include/linux/vfio.h b/include/linux/vfio.h
> index 38d3c6a8dc7e..a0613a6f21cc 100644
> --- a/include/linux/vfio.h
> +++ b/include/linux/vfio.h
> @@ -90,6 +90,7 @@ struct vfio_iommu_driver_ops {
>  					       struct notifier_block *nb);
>  	int		(*dma_rw)(void *iommu_data, dma_addr_t user_iova,
>  				  void *data, size_t count, bool write);
> +	void		*(*group_domain)(void *iommu_data, struct iommu_group *group);
>  };
>  
>  extern int vfio_register_iommu_driver(const struct vfio_iommu_driver_ops *ops);
> @@ -126,6 +127,8 @@ extern int vfio_group_unpin_pages(struct vfio_group *group,
>  extern int vfio_dma_rw(struct vfio_group *group, dma_addr_t user_iova,
>  		       void *data, size_t len, bool write);
>  
> +extern struct iommu_domain *vfio_group_domain(struct vfio_group *group);
> +
>  /* each type has independent events */
>  enum vfio_notify_type {
>  	VFIO_IOMMU_NOTIFY = 0,


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH v2 1/1] vfio/type1: Add vfio_group_domain()
@ 2020-11-30 20:57   ` Alex Williamson
  0 siblings, 0 replies; 10+ messages in thread
From: Alex Williamson @ 2020-11-30 20:57 UTC (permalink / raw)
  To: Lu Baolu
  Cc: Jean-Philippe Brucker, Kevin Tian, Dave Jiang, Ashok Raj, kvm,
	Will Deacon, Cornelia Huck, linux-kernel, iommu, Robin Murphy

On Thu, 26 Nov 2020 09:27:26 +0800
Lu Baolu <baolu.lu@linux.intel.com> wrote:

> Add the API for getting the domain from a vfio group. This could be used
> by the physical device drivers which rely on the vfio/mdev framework for
> mediated device user level access. The typical use case like below:
> 
> 	unsigned int pasid;
> 	struct vfio_group *vfio_group;
> 	struct iommu_domain *iommu_domain;
> 	struct device *dev = mdev_dev(mdev);
> 	struct device *iommu_device = mdev_get_iommu_device(dev);
> 
> 	if (!iommu_device ||
> 	    !iommu_dev_feature_enabled(iommu_device, IOMMU_DEV_FEAT_AUX))
> 		return -EINVAL;
> 
> 	vfio_group = vfio_group_get_external_user_from_dev(dev);(dev);
> 	if (IS_ERR_OR_NULL(vfio_group))
> 		return -EFAULT;
> 
> 	iommu_domain = vfio_group_domain(vfio_group);
> 	if (IS_ERR_OR_NULL(iommu_domain)) {
> 		vfio_group_put_external_user(vfio_group);
> 		return -EFAULT;
> 	}
> 
> 	pasid = iommu_aux_get_pasid(iommu_domain, iommu_device);
> 	if (pasid < 0) {
> 		vfio_group_put_external_user(vfio_group);
> 		return -EFAULT;
> 	}
> 
> 	/* Program device context with pasid value. */
> 	...
> 
> Signed-off-by: Lu Baolu <baolu.lu@linux.intel.com>
> ---
>  drivers/vfio/vfio.c             | 18 ++++++++++++++++++
>  drivers/vfio/vfio_iommu_type1.c | 23 +++++++++++++++++++++++
>  include/linux/vfio.h            |  3 +++
>  3 files changed, 44 insertions(+)
> 
> Change log:
>  - v1: https://lore.kernel.org/linux-iommu/20201112022407.2063896-1-baolu.lu@linux.intel.com/
>  - Changed according to comments @ https://lore.kernel.org/linux-iommu/20201116125631.2d043fcd@w520.home/
> 
> diff --git a/drivers/vfio/vfio.c b/drivers/vfio/vfio.c
> index 2151bc7f87ab..62c652111c88 100644
> --- a/drivers/vfio/vfio.c
> +++ b/drivers/vfio/vfio.c
> @@ -2331,6 +2331,24 @@ int vfio_unregister_notifier(struct device *dev, enum vfio_notify_type type,
>  }
>  EXPORT_SYMBOL(vfio_unregister_notifier);
>  
> +struct iommu_domain *vfio_group_domain(struct vfio_group *group)
> +{
> +	struct vfio_container *container;
> +	struct vfio_iommu_driver *driver;
> +
> +	if (!group)
> +		return ERR_PTR(-EINVAL);
> +
> +	container = group->container;
> +	driver = container->iommu_driver;
> +	if (likely(driver && driver->ops->group_domain))
> +		return driver->ops->group_domain(container->iommu_data,
> +						 group->iommu_group);
> +	else
> +		return ERR_PTR(-ENOTTY);
> +}
> +EXPORT_SYMBOL(vfio_group_domain);


_GPL?  I don't see that there's a way for a driver to get the
vfio_group pointer that's not already _GPL.


> +
>  /**
>   * Module/class support
>   */
> diff --git a/drivers/vfio/vfio_iommu_type1.c b/drivers/vfio/vfio_iommu_type1.c
> index 67e827638995..783f18f21b95 100644
> --- a/drivers/vfio/vfio_iommu_type1.c
> +++ b/drivers/vfio/vfio_iommu_type1.c
> @@ -2980,6 +2980,28 @@ static int vfio_iommu_type1_dma_rw(void *iommu_data, dma_addr_t user_iova,
>  	return ret;
>  }
>  
> +static void *vfio_iommu_type1_group_domain(void *iommu_data,
> +					   struct iommu_group *iommu_group)
> +{
> +	struct vfio_iommu *iommu = iommu_data;
> +	struct iommu_domain *domain = NULL;
> +	struct vfio_domain *d;
> +
> +	if (!iommu || !iommu_group)
> +		return ERR_PTR(-EINVAL);
> +
> +	mutex_lock(&iommu->lock);
> +	list_for_each_entry(d, &iommu->domain_list, next) {
> +		if (find_iommu_group(d, iommu_group)) {
> +			domain = d->domain;
> +			break;
> +		}
> +	}
> +	mutex_unlock(&iommu->lock);
> +
> +	return domain;
> +}


Why does this return void* rather than struct iommu_domain*, and why
does the error case return an ERR_PTR but the not-found case returns
NULL?  Thanks,

Alex


> +
>  static const struct vfio_iommu_driver_ops vfio_iommu_driver_ops_type1 = {
>  	.name			= "vfio-iommu-type1",
>  	.owner			= THIS_MODULE,
> @@ -2993,6 +3015,7 @@ static const struct vfio_iommu_driver_ops vfio_iommu_driver_ops_type1 = {
>  	.register_notifier	= vfio_iommu_type1_register_notifier,
>  	.unregister_notifier	= vfio_iommu_type1_unregister_notifier,
>  	.dma_rw			= vfio_iommu_type1_dma_rw,
> +	.group_domain		= vfio_iommu_type1_group_domain,
>  };
>  
>  static int __init vfio_iommu_type1_init(void)
> diff --git a/include/linux/vfio.h b/include/linux/vfio.h
> index 38d3c6a8dc7e..a0613a6f21cc 100644
> --- a/include/linux/vfio.h
> +++ b/include/linux/vfio.h
> @@ -90,6 +90,7 @@ struct vfio_iommu_driver_ops {
>  					       struct notifier_block *nb);
>  	int		(*dma_rw)(void *iommu_data, dma_addr_t user_iova,
>  				  void *data, size_t count, bool write);
> +	void		*(*group_domain)(void *iommu_data, struct iommu_group *group);
>  };
>  
>  extern int vfio_register_iommu_driver(const struct vfio_iommu_driver_ops *ops);
> @@ -126,6 +127,8 @@ extern int vfio_group_unpin_pages(struct vfio_group *group,
>  extern int vfio_dma_rw(struct vfio_group *group, dma_addr_t user_iova,
>  		       void *data, size_t len, bool write);
>  
> +extern struct iommu_domain *vfio_group_domain(struct vfio_group *group);
> +
>  /* each type has independent events */
>  enum vfio_notify_type {
>  	VFIO_IOMMU_NOTIFY = 0,

_______________________________________________
iommu mailing list
iommu@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/iommu

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH v2 1/1] vfio/type1: Add vfio_group_domain()
  2020-11-30 20:57   ` Alex Williamson
@ 2020-12-01  1:13     ` Lu Baolu
  -1 siblings, 0 replies; 10+ messages in thread
From: Lu Baolu @ 2020-12-01  1:13 UTC (permalink / raw)
  To: Alex Williamson
  Cc: baolu.lu, Cornelia Huck, Joerg Roedel, Will Deacon, Robin Murphy,
	Jean-Philippe Brucker, Kevin Tian, Ashok Raj, Dave Jiang,
	Liu Yi L, Zeng Xin, iommu, linux-kernel, kvm

Hi Alex,

Thanks a lot for your review comments.

On 12/1/20 4:57 AM, Alex Williamson wrote:
> On Thu, 26 Nov 2020 09:27:26 +0800
> Lu Baolu <baolu.lu@linux.intel.com> wrote:
> 
>> Add the API for getting the domain from a vfio group. This could be used
>> by the physical device drivers which rely on the vfio/mdev framework for
>> mediated device user level access. The typical use case like below:
>>
>> 	unsigned int pasid;
>> 	struct vfio_group *vfio_group;
>> 	struct iommu_domain *iommu_domain;
>> 	struct device *dev = mdev_dev(mdev);
>> 	struct device *iommu_device = mdev_get_iommu_device(dev);
>>
>> 	if (!iommu_device ||
>> 	    !iommu_dev_feature_enabled(iommu_device, IOMMU_DEV_FEAT_AUX))
>> 		return -EINVAL;
>>
>> 	vfio_group = vfio_group_get_external_user_from_dev(dev);(dev);
>> 	if (IS_ERR_OR_NULL(vfio_group))
>> 		return -EFAULT;
>>
>> 	iommu_domain = vfio_group_domain(vfio_group);
>> 	if (IS_ERR_OR_NULL(iommu_domain)) {
>> 		vfio_group_put_external_user(vfio_group);
>> 		return -EFAULT;
>> 	}
>>
>> 	pasid = iommu_aux_get_pasid(iommu_domain, iommu_device);
>> 	if (pasid < 0) {
>> 		vfio_group_put_external_user(vfio_group);
>> 		return -EFAULT;
>> 	}
>>
>> 	/* Program device context with pasid value. */
>> 	...
>>
>> Signed-off-by: Lu Baolu <baolu.lu@linux.intel.com>
>> ---
>>   drivers/vfio/vfio.c             | 18 ++++++++++++++++++
>>   drivers/vfio/vfio_iommu_type1.c | 23 +++++++++++++++++++++++
>>   include/linux/vfio.h            |  3 +++
>>   3 files changed, 44 insertions(+)
>>
>> Change log:
>>   - v1: https://lore.kernel.org/linux-iommu/20201112022407.2063896-1-baolu.lu@linux.intel.com/
>>   - Changed according to comments @ https://lore.kernel.org/linux-iommu/20201116125631.2d043fcd@w520.home/
>>
>> diff --git a/drivers/vfio/vfio.c b/drivers/vfio/vfio.c
>> index 2151bc7f87ab..62c652111c88 100644
>> --- a/drivers/vfio/vfio.c
>> +++ b/drivers/vfio/vfio.c
>> @@ -2331,6 +2331,24 @@ int vfio_unregister_notifier(struct device *dev, enum vfio_notify_type type,
>>   }
>>   EXPORT_SYMBOL(vfio_unregister_notifier);
>>   
>> +struct iommu_domain *vfio_group_domain(struct vfio_group *group)
>> +{
>> +	struct vfio_container *container;
>> +	struct vfio_iommu_driver *driver;
>> +
>> +	if (!group)
>> +		return ERR_PTR(-EINVAL);
>> +
>> +	container = group->container;
>> +	driver = container->iommu_driver;
>> +	if (likely(driver && driver->ops->group_domain))
>> +		return driver->ops->group_domain(container->iommu_data,
>> +						 group->iommu_group);
>> +	else
>> +		return ERR_PTR(-ENOTTY);
>> +}
>> +EXPORT_SYMBOL(vfio_group_domain);
> 
> 
> _GPL?  I don't see that there's a way for a driver to get the
> vfio_group pointer that's not already _GPL.
> 

It should be _GPL. Will fix it.

> 
>> +
>>   /**
>>    * Module/class support
>>    */
>> diff --git a/drivers/vfio/vfio_iommu_type1.c b/drivers/vfio/vfio_iommu_type1.c
>> index 67e827638995..783f18f21b95 100644
>> --- a/drivers/vfio/vfio_iommu_type1.c
>> +++ b/drivers/vfio/vfio_iommu_type1.c
>> @@ -2980,6 +2980,28 @@ static int vfio_iommu_type1_dma_rw(void *iommu_data, dma_addr_t user_iova,
>>   	return ret;
>>   }
>>   
>> +static void *vfio_iommu_type1_group_domain(void *iommu_data,
>> +					   struct iommu_group *iommu_group)
>> +{
>> +	struct vfio_iommu *iommu = iommu_data;
>> +	struct iommu_domain *domain = NULL;
>> +	struct vfio_domain *d;
>> +
>> +	if (!iommu || !iommu_group)
>> +		return ERR_PTR(-EINVAL);
>> +
>> +	mutex_lock(&iommu->lock);
>> +	list_for_each_entry(d, &iommu->domain_list, next) {
>> +		if (find_iommu_group(d, iommu_group)) {
>> +			domain = d->domain;
>> +			break;
>> +		}
>> +	}
>> +	mutex_unlock(&iommu->lock);
>> +
>> +	return domain;
>> +}
> 
> 
> Why does this return void* rather than struct iommu_domain*, and why
> does the error case return an ERR_PTR but the not-found case returns
> NULL?

I will change it to return iommu_domain* and return an error number for
the not-found case.

Best regards,
baolu

>  Thanks,
> 
> Alex
> 
> 
>> +
>>   static const struct vfio_iommu_driver_ops vfio_iommu_driver_ops_type1 = {
>>   	.name			= "vfio-iommu-type1",
>>   	.owner			= THIS_MODULE,
>> @@ -2993,6 +3015,7 @@ static const struct vfio_iommu_driver_ops vfio_iommu_driver_ops_type1 = {
>>   	.register_notifier	= vfio_iommu_type1_register_notifier,
>>   	.unregister_notifier	= vfio_iommu_type1_unregister_notifier,
>>   	.dma_rw			= vfio_iommu_type1_dma_rw,
>> +	.group_domain		= vfio_iommu_type1_group_domain,
>>   };
>>   
>>   static int __init vfio_iommu_type1_init(void)
>> diff --git a/include/linux/vfio.h b/include/linux/vfio.h
>> index 38d3c6a8dc7e..a0613a6f21cc 100644
>> --- a/include/linux/vfio.h
>> +++ b/include/linux/vfio.h
>> @@ -90,6 +90,7 @@ struct vfio_iommu_driver_ops {
>>   					       struct notifier_block *nb);
>>   	int		(*dma_rw)(void *iommu_data, dma_addr_t user_iova,
>>   				  void *data, size_t count, bool write);
>> +	void		*(*group_domain)(void *iommu_data, struct iommu_group *group);
>>   };
>>   
>>   extern int vfio_register_iommu_driver(const struct vfio_iommu_driver_ops *ops);
>> @@ -126,6 +127,8 @@ extern int vfio_group_unpin_pages(struct vfio_group *group,
>>   extern int vfio_dma_rw(struct vfio_group *group, dma_addr_t user_iova,
>>   		       void *data, size_t len, bool write);
>>   
>> +extern struct iommu_domain *vfio_group_domain(struct vfio_group *group);
>> +
>>   /* each type has independent events */
>>   enum vfio_notify_type {
>>   	VFIO_IOMMU_NOTIFY = 0,
> 

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH v2 1/1] vfio/type1: Add vfio_group_domain()
@ 2020-12-01  1:13     ` Lu Baolu
  0 siblings, 0 replies; 10+ messages in thread
From: Lu Baolu @ 2020-12-01  1:13 UTC (permalink / raw)
  To: Alex Williamson
  Cc: Jean-Philippe Brucker, Kevin Tian, Dave Jiang, Ashok Raj, kvm,
	Will Deacon, Cornelia Huck, linux-kernel, iommu, Robin Murphy

Hi Alex,

Thanks a lot for your review comments.

On 12/1/20 4:57 AM, Alex Williamson wrote:
> On Thu, 26 Nov 2020 09:27:26 +0800
> Lu Baolu <baolu.lu@linux.intel.com> wrote:
> 
>> Add the API for getting the domain from a vfio group. This could be used
>> by the physical device drivers which rely on the vfio/mdev framework for
>> mediated device user level access. The typical use case like below:
>>
>> 	unsigned int pasid;
>> 	struct vfio_group *vfio_group;
>> 	struct iommu_domain *iommu_domain;
>> 	struct device *dev = mdev_dev(mdev);
>> 	struct device *iommu_device = mdev_get_iommu_device(dev);
>>
>> 	if (!iommu_device ||
>> 	    !iommu_dev_feature_enabled(iommu_device, IOMMU_DEV_FEAT_AUX))
>> 		return -EINVAL;
>>
>> 	vfio_group = vfio_group_get_external_user_from_dev(dev);(dev);
>> 	if (IS_ERR_OR_NULL(vfio_group))
>> 		return -EFAULT;
>>
>> 	iommu_domain = vfio_group_domain(vfio_group);
>> 	if (IS_ERR_OR_NULL(iommu_domain)) {
>> 		vfio_group_put_external_user(vfio_group);
>> 		return -EFAULT;
>> 	}
>>
>> 	pasid = iommu_aux_get_pasid(iommu_domain, iommu_device);
>> 	if (pasid < 0) {
>> 		vfio_group_put_external_user(vfio_group);
>> 		return -EFAULT;
>> 	}
>>
>> 	/* Program device context with pasid value. */
>> 	...
>>
>> Signed-off-by: Lu Baolu <baolu.lu@linux.intel.com>
>> ---
>>   drivers/vfio/vfio.c             | 18 ++++++++++++++++++
>>   drivers/vfio/vfio_iommu_type1.c | 23 +++++++++++++++++++++++
>>   include/linux/vfio.h            |  3 +++
>>   3 files changed, 44 insertions(+)
>>
>> Change log:
>>   - v1: https://lore.kernel.org/linux-iommu/20201112022407.2063896-1-baolu.lu@linux.intel.com/
>>   - Changed according to comments @ https://lore.kernel.org/linux-iommu/20201116125631.2d043fcd@w520.home/
>>
>> diff --git a/drivers/vfio/vfio.c b/drivers/vfio/vfio.c
>> index 2151bc7f87ab..62c652111c88 100644
>> --- a/drivers/vfio/vfio.c
>> +++ b/drivers/vfio/vfio.c
>> @@ -2331,6 +2331,24 @@ int vfio_unregister_notifier(struct device *dev, enum vfio_notify_type type,
>>   }
>>   EXPORT_SYMBOL(vfio_unregister_notifier);
>>   
>> +struct iommu_domain *vfio_group_domain(struct vfio_group *group)
>> +{
>> +	struct vfio_container *container;
>> +	struct vfio_iommu_driver *driver;
>> +
>> +	if (!group)
>> +		return ERR_PTR(-EINVAL);
>> +
>> +	container = group->container;
>> +	driver = container->iommu_driver;
>> +	if (likely(driver && driver->ops->group_domain))
>> +		return driver->ops->group_domain(container->iommu_data,
>> +						 group->iommu_group);
>> +	else
>> +		return ERR_PTR(-ENOTTY);
>> +}
>> +EXPORT_SYMBOL(vfio_group_domain);
> 
> 
> _GPL?  I don't see that there's a way for a driver to get the
> vfio_group pointer that's not already _GPL.
> 

It should be _GPL. Will fix it.

> 
>> +
>>   /**
>>    * Module/class support
>>    */
>> diff --git a/drivers/vfio/vfio_iommu_type1.c b/drivers/vfio/vfio_iommu_type1.c
>> index 67e827638995..783f18f21b95 100644
>> --- a/drivers/vfio/vfio_iommu_type1.c
>> +++ b/drivers/vfio/vfio_iommu_type1.c
>> @@ -2980,6 +2980,28 @@ static int vfio_iommu_type1_dma_rw(void *iommu_data, dma_addr_t user_iova,
>>   	return ret;
>>   }
>>   
>> +static void *vfio_iommu_type1_group_domain(void *iommu_data,
>> +					   struct iommu_group *iommu_group)
>> +{
>> +	struct vfio_iommu *iommu = iommu_data;
>> +	struct iommu_domain *domain = NULL;
>> +	struct vfio_domain *d;
>> +
>> +	if (!iommu || !iommu_group)
>> +		return ERR_PTR(-EINVAL);
>> +
>> +	mutex_lock(&iommu->lock);
>> +	list_for_each_entry(d, &iommu->domain_list, next) {
>> +		if (find_iommu_group(d, iommu_group)) {
>> +			domain = d->domain;
>> +			break;
>> +		}
>> +	}
>> +	mutex_unlock(&iommu->lock);
>> +
>> +	return domain;
>> +}
> 
> 
> Why does this return void* rather than struct iommu_domain*, and why
> does the error case return an ERR_PTR but the not-found case returns
> NULL?

I will change it to return iommu_domain* and return an error number for
the not-found case.

Best regards,
baolu

>  Thanks,
> 
> Alex
> 
> 
>> +
>>   static const struct vfio_iommu_driver_ops vfio_iommu_driver_ops_type1 = {
>>   	.name			= "vfio-iommu-type1",
>>   	.owner			= THIS_MODULE,
>> @@ -2993,6 +3015,7 @@ static const struct vfio_iommu_driver_ops vfio_iommu_driver_ops_type1 = {
>>   	.register_notifier	= vfio_iommu_type1_register_notifier,
>>   	.unregister_notifier	= vfio_iommu_type1_unregister_notifier,
>>   	.dma_rw			= vfio_iommu_type1_dma_rw,
>> +	.group_domain		= vfio_iommu_type1_group_domain,
>>   };
>>   
>>   static int __init vfio_iommu_type1_init(void)
>> diff --git a/include/linux/vfio.h b/include/linux/vfio.h
>> index 38d3c6a8dc7e..a0613a6f21cc 100644
>> --- a/include/linux/vfio.h
>> +++ b/include/linux/vfio.h
>> @@ -90,6 +90,7 @@ struct vfio_iommu_driver_ops {
>>   					       struct notifier_block *nb);
>>   	int		(*dma_rw)(void *iommu_data, dma_addr_t user_iova,
>>   				  void *data, size_t count, bool write);
>> +	void		*(*group_domain)(void *iommu_data, struct iommu_group *group);
>>   };
>>   
>>   extern int vfio_register_iommu_driver(const struct vfio_iommu_driver_ops *ops);
>> @@ -126,6 +127,8 @@ extern int vfio_group_unpin_pages(struct vfio_group *group,
>>   extern int vfio_dma_rw(struct vfio_group *group, dma_addr_t user_iova,
>>   		       void *data, size_t len, bool write);
>>   
>> +extern struct iommu_domain *vfio_group_domain(struct vfio_group *group);
>> +
>>   /* each type has independent events */
>>   enum vfio_notify_type {
>>   	VFIO_IOMMU_NOTIFY = 0,
> 
_______________________________________________
iommu mailing list
iommu@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/iommu

^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2020-12-01  1:23 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-11-26  1:27 [PATCH v2 1/1] vfio/type1: Add vfio_group_domain() Lu Baolu
2020-11-26  1:27 ` Lu Baolu
2020-11-26  7:21 ` Liu, Yi L
2020-11-26  7:21   ` Liu, Yi L
2020-11-26  8:50   ` Lu Baolu
2020-11-26  8:50     ` Lu Baolu
2020-11-30 20:57 ` Alex Williamson
2020-11-30 20:57   ` Alex Williamson
2020-12-01  1:13   ` Lu Baolu
2020-12-01  1:13     ` Lu Baolu

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.