[6/6] iommu/vt-d: Add domain_alloc_user op

Message ID 20230919092523.39286-7-yi.l.liu@intel.com
State New
Headers
Series iommufd support allocating nested parent domain |

Commit Message

Yi Liu Sept. 19, 2023, 9:25 a.m. UTC
  This adds the domain_alloc_user op implementation. It supports allocating
domains to be used as parent under nested translation.

Signed-off-by: Yi Liu <yi.l.liu@intel.com>
---
 drivers/iommu/intel/iommu.c | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
  

Comments

Baolu Lu Sept. 20, 2023, 5:28 a.m. UTC | #1
On 9/19/23 5:25 PM, Yi Liu wrote:
> This adds the domain_alloc_user op implementation. It supports allocating
> domains to be used as parent under nested translation.

Documentation/process/submitting-patches.rst:

Describe your changes in imperative mood, e.g. "make xyzzy do frotz"
instead of "[This patch] makes xyzzy do frotz" or "[I] changed xyzzy
to do frotz", as if you are giving orders to the codebase to change
its behaviour.

So how about,

Add the domain_alloc_user callback to support allocating domains used as
parent under nested translation.

?

> 
> Signed-off-by: Yi Liu <yi.l.liu@intel.com>
> ---
>   drivers/iommu/intel/iommu.c | 20 ++++++++++++++++++++
>   1 file changed, 20 insertions(+)
> 
> diff --git a/drivers/iommu/intel/iommu.c b/drivers/iommu/intel/iommu.c
> index 5db283c17e0d..491bcde1ff96 100644
> --- a/drivers/iommu/intel/iommu.c
> +++ b/drivers/iommu/intel/iommu.c
> @@ -4074,6 +4074,25 @@ static struct iommu_domain *intel_iommu_domain_alloc(unsigned type)
>   	return NULL;
>   }
>   
> +static struct iommu_domain *
> +intel_iommu_domain_alloc_user(struct device *dev, u32 flags)
> +{
> +	struct iommu_domain *domain;
> +	struct intel_iommu *iommu;
> +
> +	iommu = device_to_iommu(dev, NULL, NULL);
> +	if (!iommu)
> +		return ERR_PTR(-ENODEV);
> +
> +	if ((flags & IOMMU_HWPT_ALLOC_NEST_PARENT) && !ecap_nest(iommu->ecap))
> +		return ERR_PTR(-EOPNOTSUPP);
> +
> +	domain = iommu_domain_alloc(dev->bus);

No need to bounce between core and driver. Just,

	intel_iommu_domain_alloc(IOMMU_DOMAIN_UNMANAGED);

and fully initialize it before return.

> +	if (!domain)
> +		domain = ERR_PTR(-ENOMEM);
> +	return domain;
> +}
> +
>   static void intel_iommu_domain_free(struct iommu_domain *domain)
>   {
>   	if (domain != &si_domain->domain && domain != &blocking_domain)
> @@ -4807,6 +4826,7 @@ const struct iommu_ops intel_iommu_ops = {
>   	.capable		= intel_iommu_capable,
>   	.hw_info		= intel_iommu_hw_info,
>   	.domain_alloc		= intel_iommu_domain_alloc,
> +	.domain_alloc_user	= intel_iommu_domain_alloc_user,
>   	.probe_device		= intel_iommu_probe_device,
>   	.probe_finalize		= intel_iommu_probe_finalize,
>   	.release_device		= intel_iommu_release_device,

Best regards,
baolu
  
Yang, Weijiang Sept. 20, 2023, 5:41 a.m. UTC | #2
On 9/19/2023 5:25 PM, Yi Liu wrote:
> This adds the domain_alloc_user op implementation. It supports allocating
> domains to be used as parent under nested translation.
>
> Signed-off-by: Yi Liu <yi.l.liu@intel.com>
> ---
>   drivers/iommu/intel/iommu.c | 20 ++++++++++++++++++++
>   1 file changed, 20 insertions(+)
>
> diff --git a/drivers/iommu/intel/iommu.c b/drivers/iommu/intel/iommu.c
> index 5db283c17e0d..491bcde1ff96 100644
> --- a/drivers/iommu/intel/iommu.c
> +++ b/drivers/iommu/intel/iommu.c
> @@ -4074,6 +4074,25 @@ static struct iommu_domain *intel_iommu_domain_alloc(unsigned type)
>   	return NULL;
>   }
>   
> +static struct iommu_domain *
> +intel_iommu_domain_alloc_user(struct device *dev, u32 flags)
> +{
> +	struct iommu_domain *domain;
> +	struct intel_iommu *iommu;
> +
> +	iommu = device_to_iommu(dev, NULL, NULL);
> +	if (!iommu)
> +		return ERR_PTR(-ENODEV);
> +
> +	if ((flags & IOMMU_HWPT_ALLOC_NEST_PARENT) && !ecap_nest(iommu->ecap))
> +		return ERR_PTR(-EOPNOTSUPP);

The outer caller has checked (flags & IOMMU_HWPT_ALLOC_NEST_PARENT) before it comes here.
If this callback is dedicated for nested domain allocation, then you may omit the condition here.
  
Jason Gunthorpe Sept. 20, 2023, 1:05 p.m. UTC | #3
On Wed, Sep 20, 2023 at 01:28:41PM +0800, Baolu Lu wrote:
> > 
> > diff --git a/drivers/iommu/intel/iommu.c b/drivers/iommu/intel/iommu.c
> > index 5db283c17e0d..491bcde1ff96 100644
> > --- a/drivers/iommu/intel/iommu.c
> > +++ b/drivers/iommu/intel/iommu.c
> > @@ -4074,6 +4074,25 @@ static struct iommu_domain *intel_iommu_domain_alloc(unsigned type)
> >   	return NULL;
> >   }
> > +static struct iommu_domain *
> > +intel_iommu_domain_alloc_user(struct device *dev, u32 flags)
> > +{
> > +	struct iommu_domain *domain;
> > +	struct intel_iommu *iommu;
> > +
> > +	iommu = device_to_iommu(dev, NULL, NULL);
> > +	if (!iommu)
> > +		return ERR_PTR(-ENODEV);
> > +
> > +	if ((flags & IOMMU_HWPT_ALLOC_NEST_PARENT) && !ecap_nest(iommu->ecap))
> > +		return ERR_PTR(-EOPNOTSUPP);

There is a check missing for supported flags

 if (flags & (~IOMMU_HWPT_ALLOC_NEST_PARENT))
	return ERR_PTR(-EOPNOTSUPP);

> > +
> > +	domain = iommu_domain_alloc(dev->bus);
> 
> No need to bounce between core and driver. Just,
> 
> 	intel_iommu_domain_alloc(IOMMU_DOMAIN_UNMANAGED);
> 
> and fully initialize it before return.

If you are going to do that then intel_iommu_domain_alloc() should
fully initialize the domain, not here.

Jason
  
Jason Gunthorpe Sept. 20, 2023, 1:06 p.m. UTC | #4
On Wed, Sep 20, 2023 at 01:41:07PM +0800, Yang, Weijiang wrote:
> On 9/19/2023 5:25 PM, Yi Liu wrote:
> > This adds the domain_alloc_user op implementation. It supports allocating
> > domains to be used as parent under nested translation.
> > 
> > Signed-off-by: Yi Liu <yi.l.liu@intel.com>
> > ---
> >   drivers/iommu/intel/iommu.c | 20 ++++++++++++++++++++
> >   1 file changed, 20 insertions(+)
> > 
> > diff --git a/drivers/iommu/intel/iommu.c b/drivers/iommu/intel/iommu.c
> > index 5db283c17e0d..491bcde1ff96 100644
> > --- a/drivers/iommu/intel/iommu.c
> > +++ b/drivers/iommu/intel/iommu.c
> > @@ -4074,6 +4074,25 @@ static struct iommu_domain *intel_iommu_domain_alloc(unsigned type)
> >   	return NULL;
> >   }
> > +static struct iommu_domain *
> > +intel_iommu_domain_alloc_user(struct device *dev, u32 flags)
> > +{
> > +	struct iommu_domain *domain;
> > +	struct intel_iommu *iommu;
> > +
> > +	iommu = device_to_iommu(dev, NULL, NULL);
> > +	if (!iommu)
> > +		return ERR_PTR(-ENODEV);
> > +
> > +	if ((flags & IOMMU_HWPT_ALLOC_NEST_PARENT) && !ecap_nest(iommu->ecap))
> > +		return ERR_PTR(-EOPNOTSUPP);
> 
> The outer caller has checked (flags & IOMMU_HWPT_ALLOC_NEST_PARENT) before it comes here.
> If this callback is dedicated for nested domain allocation, then you may omit the condition here.

No, please don't.

The point of the flags is to be passed to the driver. The driver
should validate them, not the core code.

We will add more flags, I don't want to change every driver to do
this.

Jason
  
Yi Liu Sept. 20, 2023, 1:10 p.m. UTC | #5
> From: Jason Gunthorpe <jgg@nvidia.com>
> Sent: Wednesday, September 20, 2023 9:05 PM
> 
> On Wed, Sep 20, 2023 at 01:28:41PM +0800, Baolu Lu wrote:
> > >
> > > diff --git a/drivers/iommu/intel/iommu.c b/drivers/iommu/intel/iommu.c
> > > index 5db283c17e0d..491bcde1ff96 100644
> > > --- a/drivers/iommu/intel/iommu.c
> > > +++ b/drivers/iommu/intel/iommu.c
> > > @@ -4074,6 +4074,25 @@ static struct iommu_domain
> *intel_iommu_domain_alloc(unsigned type)
> > >   	return NULL;
> > >   }
> > > +static struct iommu_domain *
> > > +intel_iommu_domain_alloc_user(struct device *dev, u32 flags)
> > > +{
> > > +	struct iommu_domain *domain;
> > > +	struct intel_iommu *iommu;
> > > +
> > > +	iommu = device_to_iommu(dev, NULL, NULL);
> > > +	if (!iommu)
> > > +		return ERR_PTR(-ENODEV);
> > > +
> > > +	if ((flags & IOMMU_HWPT_ALLOC_NEST_PARENT) && !ecap_nest(iommu-
> >ecap))
> > > +		return ERR_PTR(-EOPNOTSUPP);
> 
> There is a check missing for supported flags
> 
>  if (flags & (~IOMMU_HWPT_ALLOC_NEST_PARENT))
> 	return ERR_PTR(-EOPNOTSUPP);

Well, the iommufd has such check. But I also noticed your another
reply to Weijiang. So your preference is to do the flags validation
in iommu driver instead of iommufd. Isn't it?

> > > +
> > > +	domain = iommu_domain_alloc(dev->bus);
> >
> > No need to bounce between core and driver. Just,
> >
> > 	intel_iommu_domain_alloc(IOMMU_DOMAIN_UNMANAGED);
> >
> > and fully initialize it before return.
> 
> If you are going to do that then intel_iommu_domain_alloc() should
> fully initialize the domain, not here.

I've also considered what Baolu described, but it requires to do some
extra initialization which is duplicated with iommu_domain_alloc().
So I chose this simple way.

Regards,
Yi Liu
  
Yi Liu Sept. 20, 2023, 1:15 p.m. UTC | #6
> From: Yang, Weijiang <weijiang.yang@intel.com>
> Sent: Wednesday, September 20, 2023 1:41 PM
> On 9/19/2023 5:25 PM, Yi Liu wrote:
> > This adds the domain_alloc_user op implementation. It supports allocating
> > domains to be used as parent under nested translation.
> >
> > Signed-off-by: Yi Liu <yi.l.liu@intel.com>
> > ---
> >   drivers/iommu/intel/iommu.c | 20 ++++++++++++++++++++
> >   1 file changed, 20 insertions(+)
> >
> > diff --git a/drivers/iommu/intel/iommu.c b/drivers/iommu/intel/iommu.c
> > index 5db283c17e0d..491bcde1ff96 100644
> > --- a/drivers/iommu/intel/iommu.c
> > +++ b/drivers/iommu/intel/iommu.c
> > @@ -4074,6 +4074,25 @@ static struct iommu_domain
> *intel_iommu_domain_alloc(unsigned type)
> >   	return NULL;
> >   }
> >
> > +static struct iommu_domain *
> > +intel_iommu_domain_alloc_user(struct device *dev, u32 flags)
> > +{
> > +	struct iommu_domain *domain;
> > +	struct intel_iommu *iommu;
> > +
> > +	iommu = device_to_iommu(dev, NULL, NULL);
> > +	if (!iommu)
> > +		return ERR_PTR(-ENODEV);
> > +
> > +	if ((flags & IOMMU_HWPT_ALLOC_NEST_PARENT) && !ecap_nest(iommu-
> >ecap))
> > +		return ERR_PTR(-EOPNOTSUPP);
> 
> The outer caller has checked (flags & IOMMU_HWPT_ALLOC_NEST_PARENT) before it
> comes here.
> If this callback is dedicated for nested domain allocation, then you may omit the
> condition here.

This check is different. It aims to fail the call if iommu hw does not support nested.
I just realized that it may need to check if scalable mode is enabled. This should
be more accurate.

Regards,
Yi Liu
  
Yi Liu Sept. 20, 2023, 1:18 p.m. UTC | #7
> From: Baolu Lu <baolu.lu@linux.intel.com>
> Sent: Wednesday, September 20, 2023 1:29 PM
> 
> On 9/19/23 5:25 PM, Yi Liu wrote:
> > This adds the domain_alloc_user op implementation. It supports allocating
> > domains to be used as parent under nested translation.
> 
> Documentation/process/submitting-patches.rst:
> 
> Describe your changes in imperative mood, e.g. "make xyzzy do frotz"
> instead of "[This patch] makes xyzzy do frotz" or "[I] changed xyzzy
> to do frotz", as if you are giving orders to the codebase to change
> its behaviour.
> 
> So how about,
> 
> Add the domain_alloc_user callback to support allocating domains used as
> parent under nested translation.
> 

Sure.
  
Jason Gunthorpe Sept. 20, 2023, 1:18 p.m. UTC | #8
On Wed, Sep 20, 2023 at 01:10:04PM +0000, Liu, Yi L wrote:
> > From: Jason Gunthorpe <jgg@nvidia.com>
> > Sent: Wednesday, September 20, 2023 9:05 PM
> > 
> > On Wed, Sep 20, 2023 at 01:28:41PM +0800, Baolu Lu wrote:
> > > >
> > > > diff --git a/drivers/iommu/intel/iommu.c b/drivers/iommu/intel/iommu.c
> > > > index 5db283c17e0d..491bcde1ff96 100644
> > > > --- a/drivers/iommu/intel/iommu.c
> > > > +++ b/drivers/iommu/intel/iommu.c
> > > > @@ -4074,6 +4074,25 @@ static struct iommu_domain
> > *intel_iommu_domain_alloc(unsigned type)
> > > >   	return NULL;
> > > >   }
> > > > +static struct iommu_domain *
> > > > +intel_iommu_domain_alloc_user(struct device *dev, u32 flags)
> > > > +{
> > > > +	struct iommu_domain *domain;
> > > > +	struct intel_iommu *iommu;
> > > > +
> > > > +	iommu = device_to_iommu(dev, NULL, NULL);
> > > > +	if (!iommu)
> > > > +		return ERR_PTR(-ENODEV);
> > > > +
> > > > +	if ((flags & IOMMU_HWPT_ALLOC_NEST_PARENT) && !ecap_nest(iommu-
> > >ecap))
> > > > +		return ERR_PTR(-EOPNOTSUPP);
> > 
> > There is a check missing for supported flags
> > 
> >  if (flags & (~IOMMU_HWPT_ALLOC_NEST_PARENT))
> > 	return ERR_PTR(-EOPNOTSUPP);
> 
> Well, the iommufd has such check. But I also noticed your another
> reply to Weijiang. So your preference is to do the flags validation
> in iommu driver instead of iommufd. Isn't it?

The core code should check that only kernel known bits are set

The driver code should check that only driver supported bits are set.

Today there is only one bit so the checks are the same code.

Tomorrow when we add a new bit the checks will not be the same

Jason
  
Baolu Lu Sept. 21, 2023, 1:31 a.m. UTC | #9
On 9/20/23 9:10 PM, Liu, Yi L wrote:
>>>> +
>>>> +	domain = iommu_domain_alloc(dev->bus);
>>> No need to bounce between core and driver. Just,
>>>
>>> 	intel_iommu_domain_alloc(IOMMU_DOMAIN_UNMANAGED);
>>>
>>> and fully initialize it before return.
>> If you are going to do that then intel_iommu_domain_alloc() should
>> fully initialize the domain, not here.
> I've also considered what Baolu described, but it requires to do some
> extra initialization which is duplicated with iommu_domain_alloc().
> So I chose this simple way.

Okay, got you.

Once Jason's paging domain and Robin's bus->iommu_ops retirement series
have landed, the VT-d driver will need some refactoring. Therefore, I'm
fine with you using a simpler approach here. I'll refactor everything
later.

Best regards,
baolu
  
Yi Liu Sept. 25, 2023, 6:36 a.m. UTC | #10
On 2023/9/21 09:31, Baolu Lu wrote:
> On 9/20/23 9:10 PM, Liu, Yi L wrote:
>>>>> +
>>>>> +    domain = iommu_domain_alloc(dev->bus);
>>>> No need to bounce between core and driver. Just,
>>>>
>>>>     intel_iommu_domain_alloc(IOMMU_DOMAIN_UNMANAGED);
>>>>
>>>> and fully initialize it before return.
>>> If you are going to do that then intel_iommu_domain_alloc() should
>>> fully initialize the domain, not here.
>> I've also considered what Baolu described, but it requires to do some
>> extra initialization which is duplicated with iommu_domain_alloc().
>> So I chose this simple way.
> 
> Okay, got you.
> 
> Once Jason's paging domain and Robin's bus->iommu_ops retirement series
> have landed, the VT-d driver will need some refactoring. Therefore, I'm
> fine with you using a simpler approach here. I'll refactor everything
> later.

yes.
  
Yi Liu Sept. 25, 2023, 6:37 a.m. UTC | #11
On 2023/9/20 21:18, Jason Gunthorpe wrote:
> On Wed, Sep 20, 2023 at 01:10:04PM +0000, Liu, Yi L wrote:
>>> From: Jason Gunthorpe <jgg@nvidia.com>
>>> Sent: Wednesday, September 20, 2023 9:05 PM
>>>
>>> On Wed, Sep 20, 2023 at 01:28:41PM +0800, Baolu Lu wrote:
>>>>>
>>>>> diff --git a/drivers/iommu/intel/iommu.c b/drivers/iommu/intel/iommu.c
>>>>> index 5db283c17e0d..491bcde1ff96 100644
>>>>> --- a/drivers/iommu/intel/iommu.c
>>>>> +++ b/drivers/iommu/intel/iommu.c
>>>>> @@ -4074,6 +4074,25 @@ static struct iommu_domain
>>> *intel_iommu_domain_alloc(unsigned type)
>>>>>    	return NULL;
>>>>>    }
>>>>> +static struct iommu_domain *
>>>>> +intel_iommu_domain_alloc_user(struct device *dev, u32 flags)
>>>>> +{
>>>>> +	struct iommu_domain *domain;
>>>>> +	struct intel_iommu *iommu;
>>>>> +
>>>>> +	iommu = device_to_iommu(dev, NULL, NULL);
>>>>> +	if (!iommu)
>>>>> +		return ERR_PTR(-ENODEV);
>>>>> +
>>>>> +	if ((flags & IOMMU_HWPT_ALLOC_NEST_PARENT) && !ecap_nest(iommu-
>>>> ecap))
>>>>> +		return ERR_PTR(-EOPNOTSUPP);
>>>
>>> There is a check missing for supported flags
>>>
>>>   if (flags & (~IOMMU_HWPT_ALLOC_NEST_PARENT))
>>> 	return ERR_PTR(-EOPNOTSUPP);
>>
>> Well, the iommufd has such check. But I also noticed your another
>> reply to Weijiang. So your preference is to do the flags validation
>> in iommu driver instead of iommufd. Isn't it?
> 
> The core code should check that only kernel known bits are set
> 
> The driver code should check that only driver supported bits are set.
> 
> Today there is only one bit so the checks are the same code.
> 
> Tomorrow when we add a new bit the checks will not be the same

fair enough. I'll have the check in both core and iommu driver.

    if (flags & (~IOMMU_HWPT_ALLOC_NEST_PARENT))
  	return ERR_PTR(-EOPNOTSUPP);
  
Tian, Kevin Sept. 26, 2023, 5:36 a.m. UTC | #12
> From: Baolu Lu <baolu.lu@linux.intel.com>
> Sent: Thursday, September 21, 2023 9:31 AM
> 
> On 9/20/23 9:10 PM, Liu, Yi L wrote:
> >>>> +
> >>>> +	domain = iommu_domain_alloc(dev->bus);
> >>> No need to bounce between core and driver. Just,
> >>>
> >>> 	intel_iommu_domain_alloc(IOMMU_DOMAIN_UNMANAGED);
> >>>
> >>> and fully initialize it before return.
> >> If you are going to do that then intel_iommu_domain_alloc() should
> >> fully initialize the domain, not here.
> > I've also considered what Baolu described, but it requires to do some
> > extra initialization which is duplicated with iommu_domain_alloc().
> > So I chose this simple way.
> 
> Okay, got you.
> 

Please add a comment for this temporary option.
  

Patch

diff --git a/drivers/iommu/intel/iommu.c b/drivers/iommu/intel/iommu.c
index 5db283c17e0d..491bcde1ff96 100644
--- a/drivers/iommu/intel/iommu.c
+++ b/drivers/iommu/intel/iommu.c
@@ -4074,6 +4074,25 @@  static struct iommu_domain *intel_iommu_domain_alloc(unsigned type)
 	return NULL;
 }
 
+static struct iommu_domain *
+intel_iommu_domain_alloc_user(struct device *dev, u32 flags)
+{
+	struct iommu_domain *domain;
+	struct intel_iommu *iommu;
+
+	iommu = device_to_iommu(dev, NULL, NULL);
+	if (!iommu)
+		return ERR_PTR(-ENODEV);
+
+	if ((flags & IOMMU_HWPT_ALLOC_NEST_PARENT) && !ecap_nest(iommu->ecap))
+		return ERR_PTR(-EOPNOTSUPP);
+
+	domain = iommu_domain_alloc(dev->bus);
+	if (!domain)
+		domain = ERR_PTR(-ENOMEM);
+	return domain;
+}
+
 static void intel_iommu_domain_free(struct iommu_domain *domain)
 {
 	if (domain != &si_domain->domain && domain != &blocking_domain)
@@ -4807,6 +4826,7 @@  const struct iommu_ops intel_iommu_ops = {
 	.capable		= intel_iommu_capable,
 	.hw_info		= intel_iommu_hw_info,
 	.domain_alloc		= intel_iommu_domain_alloc,
+	.domain_alloc_user	= intel_iommu_domain_alloc_user,
 	.probe_device		= intel_iommu_probe_device,
 	.probe_finalize		= intel_iommu_probe_finalize,
 	.release_device		= intel_iommu_release_device,