linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] iommu: iommu_group_alloc_default_domain: Fix ops->default_domain codepath
@ 2024-01-23 16:58 ovidiu.panait
  2024-01-23 17:49 ` Jason Gunthorpe
  0 siblings, 1 reply; 3+ messages in thread
From: ovidiu.panait @ 2024-01-23 16:58 UTC (permalink / raw)
  To: linux-kernel, iommu; +Cc: robin.murphy, will, joro, jgg, Ovidiu Panait

From: Ovidiu Panait <ovidiu.panait@windriver.com>

Since commit [1], FSL_PAMU initialization on t4240rdb board fails
with: "fsl-pamu-domain: pamu_domain_init: Can't register iommu device".

Commit [1] changed the behavior for drivers that set ops->default_domain,
as the function iommu_group_alloc_default_domain() is now called with
"req_type == IOMMU_DOMAIN_IDENTITY", rather than "req_type == 0". This
causes the following EINVAL condition to be hit during FSL_PAMU
initialization:
  ...
       if (ops->default_domain) {
               if (req_type)
                       return ERR_PTR(-EINVAL);
               return ops->default_domain;
       }
  ...

To avoid this issue when ops->default_domain != NULL, remove the check for
non-zero req_type and always return the proper default domain, as set in
the driver.

  [1] 0f6a90436a57 ("iommu: Do not use IOMMU_DOMAIN_DMA [...]")

Fixes: 0f6a90436a57 ("iommu: Do not use IOMMU_DOMAIN_DMA if CONFIG_IOMMU_DMA is not enabled")
Signed-off-by: Ovidiu Panait <ovidiu.panait@windriver.com>
---
 drivers/iommu/iommu.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)

diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c
index 68e648b55767..8e79a074e560 100644
--- a/drivers/iommu/iommu.c
+++ b/drivers/iommu/iommu.c
@@ -1798,11 +1798,8 @@ iommu_group_alloc_default_domain(struct iommu_group *group, int req_type)
 	 * domain. This should always be either an IDENTITY/BLOCKED/PLATFORM
 	 * domain. Do not use in new drivers.
 	 */
-	if (ops->default_domain) {
-		if (req_type)
-			return ERR_PTR(-EINVAL);
+	if (ops->default_domain)
 		return ops->default_domain;
-	}
 
 	if (req_type)
 		return __iommu_group_alloc_default_domain(group, req_type);
-- 
2.34.1


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

* Re: [PATCH] iommu: iommu_group_alloc_default_domain: Fix ops->default_domain codepath
  2024-01-23 16:58 [PATCH] iommu: iommu_group_alloc_default_domain: Fix ops->default_domain codepath ovidiu.panait
@ 2024-01-23 17:49 ` Jason Gunthorpe
  2024-01-25 10:56   ` Ovidiu Panait
  0 siblings, 1 reply; 3+ messages in thread
From: Jason Gunthorpe @ 2024-01-23 17:49 UTC (permalink / raw)
  To: ovidiu.panait; +Cc: linux-kernel, iommu, robin.murphy, will, joro

On Tue, Jan 23, 2024 at 06:58:29PM +0200, ovidiu.panait@windriver.com wrote:
> From: Ovidiu Panait <ovidiu.panait@windriver.com>
> 
> Since commit [1], FSL_PAMU initialization on t4240rdb board fails
> with: "fsl-pamu-domain: pamu_domain_init: Can't register iommu device".
> 
> Commit [1] changed the behavior for drivers that set ops->default_domain,
> as the function iommu_group_alloc_default_domain() is now called with
> "req_type == IOMMU_DOMAIN_IDENTITY", rather than "req_type == 0". This
> causes the following EINVAL condition to be hit during FSL_PAMU
> initialization:
>   ...
>        if (ops->default_domain) {
>                if (req_type)
>                        return ERR_PTR(-EINVAL);
>                return ops->default_domain;
>        }
>   ...
> 
> To avoid this issue when ops->default_domain != NULL, remove the check for
> non-zero req_type and always return the proper default domain, as set in
> the driver.
> 
>   [1] 0f6a90436a57 ("iommu: Do not use IOMMU_DOMAIN_DMA [...]")
> 
> Fixes: 0f6a90436a57 ("iommu: Do not use IOMMU_DOMAIN_DMA if CONFIG_IOMMU_DMA is not enabled")
> Signed-off-by: Ovidiu Panait <ovidiu.panait@windriver.com>
> ---
>  drivers/iommu/iommu.c | 5 +----
>  1 file changed, 1 insertion(+), 4 deletions(-)

Oh, that is a neat combination of things

Removing the test will cause problems for the sys flow - I think the
right thing is to get the correct req_type earlier. paml wants to use
a specific domain type so it should not be switching to identity at
all.

Does the below work for you?

Also, what is your interest here? I have been wanting to delete this
driver, would your system still be OK for your usage if it was
removed?

Thanks,
Jason

diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c
index 68e648b5576706..7fa3ccdc3286e6 100644
--- a/drivers/iommu/iommu.c
+++ b/drivers/iommu/iommu.c
@@ -1799,7 +1799,7 @@ iommu_group_alloc_default_domain(struct iommu_group *group, int req_type)
 	 * domain. Do not use in new drivers.
 	 */
 	if (ops->default_domain) {
-		if (req_type)
+		if (req_type != ops->default_domain->type)
 			return ERR_PTR(-EINVAL);
 		return ops->default_domain;
 	}
@@ -1871,10 +1871,18 @@ static int iommu_get_def_domain_type(struct iommu_group *group,
 	const struct iommu_ops *ops = dev_iommu_ops(dev);
 	int type;
 
-	if (!ops->def_domain_type)
-		return cur_type;
-
-	type = ops->def_domain_type(dev);
+	if (ops->default_domain) {
+		/*
+		 * Drivers that declare a global static default_domain will
+		 * always choose that.
+		 */
+		type = ops->default_domain->type;
+	} else {
+		if (ops->def_domain_type)
+			type = ops->def_domain_type(dev);
+		else
+			type = cur_type;
+	}
 	if (!type || cur_type == type)
 		return cur_type;
 	if (!cur_type)

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

* Re: [PATCH] iommu: iommu_group_alloc_default_domain: Fix ops->default_domain codepath
  2024-01-23 17:49 ` Jason Gunthorpe
@ 2024-01-25 10:56   ` Ovidiu Panait
  0 siblings, 0 replies; 3+ messages in thread
From: Ovidiu Panait @ 2024-01-25 10:56 UTC (permalink / raw)
  To: Jason Gunthorpe; +Cc: linux-kernel, iommu, robin.murphy, will, joro

Hi Jason,

On 23.01.2024 19:49, Jason Gunthorpe wrote:
> CAUTION: This email comes from a non Wind River email account!
> Do not click links or open attachments unless you recognize the sender and know the content is safe.
>
> On Tue, Jan 23, 2024 at 06:58:29PM +0200, ovidiu.panait@windriver.com wrote:
>> From: Ovidiu Panait <ovidiu.panait@windriver.com>
>>
>> Since commit [1], FSL_PAMU initialization on t4240rdb board fails
>> with: "fsl-pamu-domain: pamu_domain_init: Can't register iommu device".
>>
>> Commit [1] changed the behavior for drivers that set ops->default_domain,
>> as the function iommu_group_alloc_default_domain() is now called with
>> "req_type == IOMMU_DOMAIN_IDENTITY", rather than "req_type == 0". This
>> causes the following EINVAL condition to be hit during FSL_PAMU
>> initialization:
>>    ...
>>         if (ops->default_domain) {
>>                 if (req_type)
>>                         return ERR_PTR(-EINVAL);
>>                 return ops->default_domain;
>>         }
>>    ...
>>
>> To avoid this issue when ops->default_domain != NULL, remove the check for
>> non-zero req_type and always return the proper default domain, as set in
>> the driver.
>>
>>    [1] 0f6a90436a57 ("iommu: Do not use IOMMU_DOMAIN_DMA [...]")
>>
>> Fixes: 0f6a90436a57 ("iommu: Do not use IOMMU_DOMAIN_DMA if CONFIG_IOMMU_DMA is not enabled")
>> Signed-off-by: Ovidiu Panait <ovidiu.panait@windriver.com>
>> ---
>>   drivers/iommu/iommu.c | 5 +----
>>   1 file changed, 1 insertion(+), 4 deletions(-)
> Oh, that is a neat combination of things
>
> Removing the test will cause problems for the sys flow - I think the
> right thing is to get the correct req_type earlier. paml wants to use
> a specific domain type so it should not be switching to identity at
> all.
>
> Does the below work for you?
Yes, this works, thanks a lot!
> Also, what is your interest here? I have been wanting to delete this
> driver, would your system still be OK for your usage if it was
> removed?
CONFIG_FSL_PAMU is enabled by default in corenet64_smp_defconfig, and it 
looks
like many platforms are based on this config. I actually noticed this issue
while boot testing the mainline kernel on the t4240rdb board with the 
default
corenet64_smp_defconfig.

In this particular case, the board still boots fine with CONFIG_FSL_PAMU 
disabled,
but I'm not sure how/if removing the PAMU driver will impact other 
platforms.

Ovidiu
> Thanks,
> Jason
>
> diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c
> index 68e648b5576706..7fa3ccdc3286e6 100644
> --- a/drivers/iommu/iommu.c
> +++ b/drivers/iommu/iommu.c
> @@ -1799,7 +1799,7 @@ iommu_group_alloc_default_domain(struct iommu_group *group, int req_type)
>           * domain. Do not use in new drivers.
>           */
>          if (ops->default_domain) {
> -               if (req_type)
> +               if (req_type != ops->default_domain->type)
>                          return ERR_PTR(-EINVAL);
>                  return ops->default_domain;
>          }
> @@ -1871,10 +1871,18 @@ static int iommu_get_def_domain_type(struct iommu_group *group,
>          const struct iommu_ops *ops = dev_iommu_ops(dev);
>          int type;
>
> -       if (!ops->def_domain_type)
> -               return cur_type;
> -
> -       type = ops->def_domain_type(dev);
> +       if (ops->default_domain) {
> +               /*
> +                * Drivers that declare a global static default_domain will
> +                * always choose that.
> +                */
> +               type = ops->default_domain->type;
> +       } else {
> +               if (ops->def_domain_type)
> +                       type = ops->def_domain_type(dev);
> +               else
> +                       type = cur_type;
> +       }
>          if (!type || cur_type == type)
>                  return cur_type;
>          if (!cur_type)

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

end of thread, other threads:[~2024-01-25 11:02 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-01-23 16:58 [PATCH] iommu: iommu_group_alloc_default_domain: Fix ops->default_domain codepath ovidiu.panait
2024-01-23 17:49 ` Jason Gunthorpe
2024-01-25 10:56   ` Ovidiu Panait

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).